1

I have recently started using Python and I have a problem that I would like to propose. I have three arrays (X1, X2, and X3). I need to apply linear interpolation. The goal is given new points with coordinates X1 and X2 apply interpolation in order to find the new value of X3.

Below is the code developed:

X1 = [ 0.  , 0.05, 0.1 , 0.15, 0.2 , 0.25, 0.3 , 0.35, 0.4 , 0.45, 0.51,
       0.56, 0.61, 0.66, 0.71, 0.76, 0.81, 0.86, 0.91, 0.96, 1.01, 1.06,
       1.11, 1.16, 1.21, 1.26, 1.31, 1.36, 1.41, 1.46, 1.52, 1.57, 1.62,
       1.67, 1.72, 1.77, 1.82, 1.87, 1.92, 1.97, 2.02, 2.07, 2.12, 2.17,
       2.22, 2.27, 2.32, 2.37, 2.42, 2.47, 2.53, 2.58, 2.63, 2.68, 2.73,
       2.78, 2.83, 2.88, 2.93, 2.98, 3.03, 3.08, 3.13, 3.18, 3.23, 3.28,
       3.33, 3.38, 3.43, 3.48, 3.54, 3.59, 3.64, 3.69, 3.74, 3.79, 3.84,
       3.89, 3.94, 3.99, 4.04, 4.09, 4.14, 4.19, 4.24, 4.29, 4.34, 4.39,
       4.44, 4.49, 4.55, 4.6 , 4.65, 4.7 , 4.75, 4.8 , 4.85, 4.9 , 4.95,
       5.  ]

X2 = [0.01, 0.02, 0.02, 0.02, 0.03, 0.04, 0.04, 0.04, 0.05, 0.06, 0.06,
        0.06, 0.07, 0.08, 0.08, 0.08, 0.09, 0.1 , 0.1 , 0.1 , 0.11, 0.12,
        0.12, 0.12, 0.13, 0.14, 0.14, 0.14, 0.15, 0.16, 0.16, 0.16, 0.17,
        0.18, 0.18, 0.18, 0.19, 0.2 , 0.2 , 0.2 , 0.21, 0.22, 0.22, 0.22,
        0.23, 0.24, 0.24, 0.24, 0.25, 0.26, 0.26, 0.26, 0.27, 0.28, 0.28,
        0.28, 0.29, 0.3 , 0.3 , 0.3 , 0.31, 0.32, 0.32, 0.32, 0.33, 0.34,
        0.34, 0.34, 0.35, 0.36, 0.36, 0.36, 0.37, 0.38, 0.38, 0.38, 0.39,
        0.4 , 0.4 , 0.4 , 0.41, 0.42, 0.42, 0.42, 0.43, 0.44, 0.44, 0.44,
        0.45, 0.46, 0.46, 0.46, 0.47, 0.48, 0.48, 0.48, 0.49, 0.5 , 0.5 ,
        0.5 ]

X3= [1.46, 1.47, 1.47, 1.47, 1.47, 1.48, 1.48, 1.49, 1.49, 1.49, 1.5 ,
        1.5 , 1.51, 1.51, 1.52, 1.52, 1.53, 1.54, 1.54, 1.55, 1.55, 1.56,
        1.56, 1.57, 1.57, 1.58, 1.58, 1.59, 1.6 , 1.6 , 1.61, 1.61, 1.62,
        1.62, 1.63, 1.63, 1.64, 1.64, 1.65, 1.65, 1.65, 1.66, 1.66, 1.67,
        1.67, 1.68, 1.68, 1.68, 1.69, 1.69, 1.7 , 1.7 , 1.7 , 1.71, 1.71,
        1.71, 1.72, 1.72, 1.72, 1.72, 1.73, 1.73, 1.73, 1.73, 1.73, 1.74,
        1.74, 1.74, 1.74, 1.74, 1.74, 1.74, 1.74, 1.75, 1.75, 1.75, 1.75,
        1.75, 1.75, 1.75, 1.74, 1.74, 1.74, 1.74, 1.74, 1.74, 1.74, 1.73,
        1.73, 1.73, 1.73, 1.72, 1.72, 1.72, 1.71, 1.71, 1.7 , 1.7 , 1.69,
        1.69]

import numpy as np
from scipy import interpolate

f = interpolate.interp2d(X1, X2, X3, kind='linear')

# We consider a new point
X1new = 2.5
X2new = 2.5
X3new = f(X1new, X2new)
X3new

The new value of X3 is 0, but it should actually be about 1.7. Do you think there is an error or is there something to add in the code?

Thank you in advance for your cooperation.

1 Answer 1

1

Your values for X2 between [0.01, 0.5]. For interpolation, you need to use values in your range. Try this:

import numpy as np
from scipy import interpolate

f = interpolate.interp2d(X1, X2, X3, kind='linear')

# We consider a new point
X1new = 2.5
X2new = 0.3
X3new = f([X1new], [X2new])
print(X3new)
# array([1.71597061])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.