Python中3D数据的线性插值

时间:2019-01-03 10:42:58

标签: python interpolation

我正在尝试对软件自动生成的数据进行线性插值,这应该是x,y,z的函数,但是出现以下错误:

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

我有以下代码:

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

数据是自动生成的,所以我不知道如何定义函数w.r.t. x,y,z我的方法有什么错误吗?是否可以在不定义函数的情况下对数据进行线性插值?预先感谢!

1 个答案:

答案 0 :(得分:1)

感谢@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