[SciPy-User] Interpolate: Derivatives of parametric splines

Warren Weckesser warren.weckesser@enthought....
Thu Nov 12 09:27:47 CST 2009


Raimund,

When you interpolate the curve using splprep, you get a spline 
representation of the parameterized curve (x(u), y(u)).  As Zachary 
pointed out, the derivative values returned by spalde are the 
derivatives of x and y with respect to u.  To get dy/dx, you can compute 
dy/dx = y'(u)/x'(u).

This bit of code shows an example:

----------
import numpy as np
from scipy import interpolate

numpoints = 20

x = np.linspace(0, 2*np.pi, numpoints)
y = np.sin(x)

tckp, u = interpolate.splprep([x, y], s=0, k=5)

u0 = 0.25
ders = interpolate.spalde(u0, tckp)

x = ders[0][0]
y = ders[1][0]
dxdu = ders[0][1]
dydu = ders[1][1]
dydx = dydu / dxdu
print "u =", u0, ": x =", x, " y =", y, " dy/dx = ", dydx
----------

Warren


Raimund Andersen wrote:
> Hello Zachary Pincus,
>
> thanks for your answer. Maybe I didn't get you right.
> The first derivative at pi/2 should be 0 ( cos(pi/2) ).
> What I get from interpolate.spalde(0.25, tckp) is
>
> 7.44935679e+00 and -3.47491248e-01.
>
> Now, how do I get to 0? Why those different 'x' values at all?
> It should be always 1.57079633e+00, no?
>  
>
> -------- Original-Nachricht --------
>   
>> Datum: Thu, 12 Nov 2009 08:19:58 -0500
>> Von: Zachary Pincus <zachary.pincus@yale.edu>
>> An: SciPy Users List <scipy-user@scipy.org>
>> Betreff: Re: [SciPy-User] Interpolate: Derivatives of parametric splines
>>     
>
>   
>> Without thinking deeply about this at all, aren't the derivatives of a  
>> parametric spline [x(p), y(p)] given as dx/dp and dy/dp, not the dx/dy  
>> that you are perhaps expecting?
>>
>>
>> On Nov 12, 2009, at 6:35 AM, anderse@gmx.de wrote:
>>
>>     
>>> Hi,
>>>
>>> I'd like to get the derivatives of parametric splines.
>>> Looking at the tutorial
>>>       
>> (http://docs.scipy.org/doc/scipy/reference/tutorial/interpolate.html 
>>     
>>> )
>>> I get a spline like this:
>>>
>>>       
>>>>>> x = np.arange(0, 2*np.pi + np.pi / 4, 2 * np.pi / 8)
>>>>>> y = np.sin(x)
>>>>>> tck = interpolate.splrep(x, y, s = 0, k = 5)
>>>>>> xnew = np.arange(0, 2 * np.pi, np.pi / 50)
>>>>>> ynew = interpolate.splev(xnew, tck, der = 0)
>>>>>>             
>>> now, the derivatives can be determined like this:
>>>
>>>       
>>>>>> yder = interpolate.splev(xnew, tck, der = 1)
>>>>>> yder2 = interpolate.splev(xnew, tck, der = 2)
>>>>>>             
>>>>>> plt.plot(x, y, 'x', xnew, ynew, xnew, yder, xnew, yder2)
>>>>>>             
>>> The first derivative is about null at pi / 2,
>>> the second one at pi, as they should be:
>>>
>>>       
>>>>>> interpolate.spalde(np.pi, tck)
>>>>>>             
>>> array([  0.00000000e+00,  -1.00064770e+00,  -1.73418916e-17,
>>>         1.00726743e+00,  -2.65046223e-16,  -1.01680119e+00])
>>>
>>>       
>>>>>> interpolate.spalde(np.pi / 2, tck)
>>>>>>             
>>> array([ 1.        , -0.00199181, -0.99629386,  0.02365328,   
>>> 0.90756527,
>>>       -0.1387468 ])
>>>
>>> Of course, the x-range is the same, no matter of der=#.
>>>
>>> Now the parametric version:
>>>
>>>       
>>>>>> tckp, u = interpolate.splprep([x, y], s=0, k=5)
>>>>>> u
>>>>>>             
>>> array([ 0.        ,  0.13941767,  0.25      ,  0.36058233,   
>>> 0.5       ,
>>>        0.63941767,  0.75      ,  0.86058233,  1.        ])
>>>
>>> so pi is at 0.5, pi/2 is at 0.25.
>>>
>>> And this is what I get at these 'x' values:
>>>
>>>       
>>>>>> interpolate.spalde(0.5, tckp)
>>>>>>             
>>> [array([  3.14159265e+00,   5.14754151e+00,   1.10395807e-13,
>>>         1.69542498e+02,  -4.03851332e-11,  -2.01255417e+04]),
>>> array([  7.73894012e-16,  -5.38240284e+00,  -1.31811639e-13,
>>>         7.74093936e+01,   5.58012792e-11,   1.89849315e+04])]
>>>
>>>       
>>>>>> interpolate.spalde(0.25, tckp)
>>>>>>             
>>> [array([  1.57079633e+00,   7.44935679e+00,  -7.65674781e-02,
>>>        -1.85343925e+02,   7.51370411e+01,   2.46939899e+04]),
>>> array([  1.00000000e+00,  -3.47491248e-01,  -5.16420728e+01,
>>>         2.05418849e+02,   3.66866738e+03,  -5.71113127e+04])]
>>>
>>> The first array states the x-values, the second one the y-values,  
>>> respectively, AFAIK.
>>> This makes sense without derivatives, and I get a plot using
>>>
>>>       
>>>>>> unew = np.arange(0, 1.01, 0.01)
>>>>>> out = interpolate.splev(unew, tckp, der = 0)
>>>>>> plt.plot(out[0], out[1])
>>>>>>             
>>> which looks like the one above, but what about the derivatives?
>>>
>>>       
>>>>>> der1 = interpolate.splev(unew, tckp, der = 1)
>>>>>> der2 = interpolate.splev(unew, tckp, der = 2)
>>>>>> plt.plot(der1[0], der1[1], der2[0], der2[1])
>>>>>>             
>>> dont make sense to me at all.
>>>
>>> Thank you in advance for your help.
>>>
>>> Raimund
>>>
>>> -- 
>>> GRATIS für alle GMX-Mitglieder: Die maxdome Movie-FLAT!
>>> Jetzt freischalten unter http://portal.gmx.net/de/go/maxdome01
>>> _______________________________________________
>>> SciPy-User mailing list
>>> SciPy-User@scipy.org
>>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>>       
>> _______________________________________________
>> SciPy-User mailing list
>> SciPy-User@scipy.org
>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>     
>
>   



More information about the SciPy-User mailing list