问题描述
我正在使用interpolate.interp2d()将2D样条曲线拟合到函数上.我如何获得花键w.r.t.的一阶导数每个因变量?到目前为止,这是我的代码,Z是我拥有的网格上的离散点
I'm using interpolate.interp2d() to fit a 2-D spline over a function. How can I get the first derivative of the spline w.r.t. each of the dependent variables? Here is my code so far, Z are the descrete points on a mesh-grid that I have
from scipy import interpolate
YY, XX = np.meshgrid(Y, X)
f = interpolate.interp2d(AA, XX, Z, kind='cubic')
所以,我需要df/dx和df/dy.另请注意,我的Y栅格间距不均匀.我想我可以在数值上区分Z,然后拟合一个新的样条曲线,但这似乎太麻烦了.有没有更简单的方法?
So, I need df/dx and df/dy. Note also that my Y-grid is not evenly spaced. I guess I can numerically differentiate Z and then fit a new spline, but it seemed like too much hassle. Is there an easier way?
推荐答案
您可以使用函数 bisplev
在插值器的tck
属性上,并带有可选参数dx
和.
You can differentiate the output of interp2d
by using the function bisplev
on the tck
property of the interpolant with the optional arguments dx
and dy
.
如果您已插入一些网格化数据:
If you've got some meshed data which you've interpolated:
X = np.arange(5.)
Y = np.arange(6., 11)
Y[0] = 4 # Demonstrate an irregular mesh
YY, XX = np.meshgrid(Y, X)
Z = np.sin(XX*2*np.pi/5 + YY*YY*2*np.pi/11)
f = sp.interpolate.interp2d(XX, YY, Z, kind='cubic')
xt = np.linspace(X.min(), X.max())
yt = np.linspace(Y.min(), Y.max())
然后您可以访问bisplev
的相应结构,如f.tck
:f
相对于x
的偏导数可以评估为
then you can access the appropriate structure for bisplev
as f.tck
: the partial derivative of f
with respect to x
can be evaluated as
Z_x = sp.interpolate.bisplev(xt, yt, f.tck, dx=1, dy=0)
编辑:在此答案中,它看起来像结果interp2d
本身可以采用以下可选参数: dx
和dy
:
Edit: From this answer, it looks like the result of interp2d
can itself take the optional arguments of dx
and dy
:
Z_x = f(xt, yt, dx=1, dy=0)
这篇关于在python中区分2d三次样条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!