# [SciPy-user] spline interpolation

Christian Kristukat ckkart at hoc.net
Fri Nov 10 07:39:05 CST 2006

```Nils Wagner wrote:
> Christian Kristukat wrote:
>> Nils Wagner wrote:
>>
>>> Christian Kristukat wrote:
>>>
>>>> Robert Kern wrote:
>>>>
>>>>
>>>>> Jordan Dawe wrote:
>>>>>
>>>>>
>>>>>> I've been looking at scipy's interpolation routines and I can't make
>>>>>> heads or tails of them.  I just want to do a spline interp1d like matlab
>>>>>> does.  Is there any way to do this?
>>>>>>
>>>>>>
>>>>> I don't know exactly what features you want from Matlab's interp1d, but you
>>>>> probably want scipy.interpolate.UnivariateSpline.
>>>>>
>>>>>
>>>> I just noticed that UnivariateSpline.derivatives() seems to be broken:
>>>>
>>>> import numpy as N
>>>> from scipy.interpolate import UnivariateSpline as spline
>>>> x=N.arange(10,dtype=float)
>>>> y=x**2
>>>> a=N.linspace(2,5,100)
>>>> sp=spline(x,y)
>>>> b=sp(a)
>>>> der=sp.derivatives(a)
>>>>
>>>> fails with:
>>>>
>>>> 0-th dimension must be fixed to 8 but got 4
>>>> ---------------------------------------------------------------------------
>>>> dfitpack.error                                     Traceback (most recent call last)
>>>>
>>>> /mnt/home/ck/<console>
>>>>
>>>> /usr/local/lib/python2.4/site-packages/scipy/interpolate/fitpack2.py in
>>>> derivatives(self, x)
>>>>     179     def derivatives(self, x):
>>>>     180         """ Return all derivatives of the spline at the point x."""
>>>> --> 181         d,ier = dfitpack.spalde(*(self._eval_args+(x,)))
>>>>     182         assert ier==0,`ier`
>>>>     183         return d
>>>>
>>>> error: failed in converting 2nd argument `c' of dfitpack.spalde to C/Fortran array
>>>>
>>>> with numpy 1.0rc2, scipy 0.5.1 on linux
>>>>
>>>> Christian
>>>>
>>>>
>>>> _______________________________________________
>>>> SciPy-user mailing list
>>>> SciPy-user at scipy.org
>>>> http://projects.scipy.org/mailman/listinfo/scipy-user
>>>>
>>>>
>>> Works fine for me.
>>>
>>>
>>>>>> der
>>>>>>
>>> array([  4.00000000e+00,   4.00000000e+00,   2.00000000e+00,
>>>         -1.92438658e-15])
>>>
>>>>>> N.__version__
>>>>>>
>>> '1.0.1.dev3432'
>>>
>>>>>> scipy.__version__
>>>>>>
>>> '0.5.2.dev2314'
>>>
>> Thanks for trying. But I don't understand the result. Why does it have 4
>> elements, when b had 100? What is meant by 'return all derivatives'? Is that
>> 'all derivatives until the derivative is zero'?
>>
>> Christian
>> _______________________________________________
>> SciPy-user mailing list
>> SciPy-user at scipy.org
>> http://projects.scipy.org/mailman/listinfo/scipy-user
>>
>
>
>  If you use sp=spline(x,y,k=2)
> you obtain
>>>> der
> array([ 4.,  4.,  2.])

So this seems to be the 0th, 1st and 2nd derivative. Not very intuitive, nor the
fact, that it silently considers only the first element of the array I passed to
derivatives() and ignores the rest.

Christian

```