[SciPy-User] Interpolate: Derivatives of parametric splines

anderse@g... anderse@g...
Thu Nov 12 05:35:30 CST 2009


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


More information about the SciPy-User mailing list