Differentiate a 2d cubic spline in python

前端 未结 1 944
醉酒成梦
醉酒成梦 2021-01-24 18:10

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 s

相关标签:
1条回答
  • 2021-01-24 18:36

    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())
    

    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)
    

    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)
    
    0 讨论(0)
提交回复
热议问题