2

I am trying to do linear interpolation of automatically generated data from software, which should be the function of x,y,z but I am getting following error:

Traceback (most recent call last):
  File "trajectory_cartesian.py", line 67, in <module>
    interpolating_function_Ex = RegularGridInterpolator((x, y, z), Ex1)
  File "/anaconda2/lib/python2.7/site-packages/scipy/interpolate/interpolate.py", line 2418, in __init__
    "dimensions" % (len(points), values.ndim))
ValueError: There are 3 point arrays, but values has 1 dimensions

I have following code:

import numpy as np
from scipy.interpolate import RegularGridInterpolator


#sample array for field components
Ex1 = [8.84138516e+01 8.84138516e+01 7.77498363e+01 5.77080432e+01]
Ey1 = [1.54844696e+02  1.54844696e+02  1.36168141e+02  1.01067698e+02]
Ez1 = [-2.45922135e+03 -2.45922135e+03 -2.45922135e+03 -2.45922135e+03]





#sample array for position
x = [1.94871844   5.61111111   8.59672097  10.54543941]
y = [8.84138516e+01 8.84138516e+01 7.77498363e+01 5.77080432e+01]
z = [30.55555556  30.55555556  30.55555556  30.55555556]



#linear interpolation for Ex, Ey and Ez

interpolating_function_Ex = RegularGridInterpolator((x, y, z), Ex1)
interpolating_function_Ey = RegularGridInterpolator((x, y, z), Ey1)
interpolating_function_Ez = RegularGridInterpolator((x, y, z), Ez1)

#array for new points
x1 = np.linspace(0, 31, 1000)
y1 = np.linspace(0, 10, 1000)
z1 = np.linspace(0, 10, 1000)

X = np.dstack((x1,y1,z1))
points = np.array(X)

fEx = interpolating_function_Ex(points)
fEy = interpolating_function_Ey(points)
fEz = interpolating_function_Ez(points)
print fEx, fEy, fEz

The data are automatically generated so , I don't know how to define the function w.r.t. x, y, z. Is there any mistake in my method? Can I do linear interpolation of data without defining the function? Thanks in advance!

1
  • Can you edit the question to include the exact error? Also, a sample of the input data would also be helpful. Commented Jan 3, 2019 at 10:46

1 Answer 1

4

Thanks @Saullo G. P. Castro

from scipy.interpolate import LinearNDInterpolator
import numpy as np

#sample array for field components
Ex1 = np.array([8.84138516e+01 8.84138516e+01 7.77498363e+01 5.77080432e+01])
Ey1 = np.array([1.54844696e+02  1.54844696e+02  1.36168141e+02  1.01067698e+02])
Ez1 = np.array([-2.45922135e+03 -2.45922135e+03 -2.45922135e+03 -2.45922135e+03])

#sample array for position
x = np.array([1.94871844   5.61111111   8.59672097  10.54543941])
y = np.array([8.84138516e+01 8.84138516e+01 7.77498363e+01 5.77080432e+01])
z = np.array([30.55555556  30.55555556  30.55555556  30.55555556])

#linear interpolation of Ex1, Ey1, Ez1
Exf = LinearNDInterpolator((x, y, z), Ex1)
Eyf = LinearNDInterpolator((x, y, z), Ey1)
Ezf = LinearNDInterpolator((x, y, z), Ez1)

#array of new point
x1 = np.linspace(0, 5, 10)
y1 = np.linspace(0, 7, 10)
z1 = np.linspace(0, 10, 10)

#creating array([x1,y1,z1],[x2,y2,z2],....) for new grids
X = np.dstack((x1,y1,z1))
points = np.array(X)

#Field at new grids after linear interpolation
fEx = Exf(points)
fEy = Eyf(points)
fEz = Ezf(points)
print fEx, fEy, fEz
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.