[SciPy-User] interp1d results vs. MatLab interp1

745 views
Skip to first unread message

Lynn Oliver

unread,
Nov 21, 2011, 11:29:36 AM11/21/11
to SciPy...@scipy.org
I'm converting a MatLab program to Python, and I'm having problems understanding why scipy.interpolate.interp1d is giving different results than MatLab interp1.

In MatLab the usage is slightly different:
yi = interp1(x,Y,xi,'cubic')

While  in SciPy it's like this:
f = interp1d(x,Y,kind='cubic')
yi = f(xi)

For a trivial example the results are the same:
MatLab:
interp1([0 1 2 3 4], [0 1 2 3 4],[1.5 2.5 3.5],'cubic')
  1.5000 2.5000 3.5000

Python:
interp1d([1,2,3,4],[1,2,3,4],kind='cubic')([1.5,2.5,3.5])
array([ 1.5,  2.5,  3.5])

But for a real-world example they are not the same:
x =   0.0000e+000  2.1333e+001  3.2000e+001  1.6000e+004  2.1333e+004  2.3994e+004
Y =   -6   -6   20   20   -6   -6
xi =  0.00000 11.72161 23.44322 35.16484

Matlab:    -6.0000   -12.3303    -3.7384    22.7127

Python:    -6.  -15.63041012  -2.04908267  30.43054192

Any thoughts as to how I can get results that are consistent with MatLab?

Thanks-
Lynn

Pauli Virtanen

unread,
Nov 21, 2011, 11:53:09 AM11/21/11
to scipy...@scipy.org
21.11.2011 17:29, Lynn Oliver kirjoitti:
> I'm converting a MatLab program to Python, and I'm having problems
> understanding why scipy.interpolate.interp1d is giving different results
> than MatLab interp1.

With cubic splines, there is freedom in choosing the interpolants, so
there are many different "cubic" spline interpolation schemes.

Matlab's interp1's 'cubic' mode apparently produces a C1 continuous
spline that is monotonicity-preserving. I don't think such a mode is
currently implemented in Scipy.

--
Pauli Virtanen

_______________________________________________
SciPy-User mailing list
SciPy...@scipy.org
http://mail.scipy.org/mailman/listinfo/scipy-user

Charles R Harris

unread,
Nov 21, 2011, 1:13:58 PM11/21/11
to SciPy Users List
On Mon, Nov 21, 2011 at 9:53 AM, Pauli Virtanen <p...@iki.fi> wrote:
21.11.2011 17:29, Lynn Oliver kirjoitti:
> I'm converting a MatLab program to Python, and I'm having problems
> understanding why scipy.interpolate.interp1d is giving different results
> than MatLab interp1.

With cubic splines, there is freedom in choosing the interpolants, so
there are many different "cubic" spline interpolation schemes.

Matlab's interp1's 'cubic' mode apparently produces a C1 continuous
spline that is monotonicity-preserving. I don't think such a mode is
currently implemented in Scipy.


The boundary conditions can make a difference. I expect, given De Boor's participation, that the Matlab spline uses not-a-knot boundary conditions when no other boundary conditions are specified. I'm not sure what interp1d does.

Chuck

Pauli Virtanen

unread,
Nov 21, 2011, 1:28:37 PM11/21/11
to scipy...@scipy.org
21.11.2011 19:13, Charles R Harris kirjoitti:
[clip]

> The boundary conditions can make a difference. I expect, given De Boor's
> participation, that the Matlab spline uses not-a-knot boundary
> conditions when no other boundary conditions are specified. I'm not sure
> what interp1d does.

It's not only the boundary conditions: you can also make a choice
whether you want C2 contiguity, or if you stick with C1 which gives you
more freedom to play around with other things such as monotonicity.

Charles R Harris

unread,
Nov 21, 2011, 2:39:58 PM11/21/11
to SciPy Users List
On Mon, Nov 21, 2011 at 11:28 AM, Pauli Virtanen <p...@iki.fi> wrote:
21.11.2011 19:13, Charles R Harris kirjoitti:
[clip]
> The boundary conditions can make a difference. I expect, given De Boor's
> participation, that the Matlab spline uses not-a-knot boundary
> conditions when no other boundary conditions are specified. I'm not sure
> what interp1d does.

It's not only the boundary conditions: you can also make a choice
whether you want C2 contiguity, or if you stick with C1 which gives you
more freedom to play around with other things such as monotonicity.


Is that an option in interp1d? That is usually done for b-splines by using repeated knot points. When the knot points are isolated then the spline and all derivatives except the last non-zero one are continuous. Each repeat of the knot point drops the number of continuity conditions  by one, so that in the cubic spline case a knot point repeated four times allows the spline to be discontinuous at that point, whereas zero knot points, i.e., between knot points, requires continuity to all orders.

Chuck

Reply all
Reply to author
Forward
0 new messages