# [SciPy-user] interpolation question

Michael Hearne mhearne@usgs....
Wed Oct 17 13:29:33 CDT 2007

```Is the 'linear' option in interpolate.interp2d() doing 'bilinear'
interpolation, or something else?

A couple of months ago I posted a simple interpolation problem I was
experiencing, which was solved by installing a newer version of scipy.

This time I have a slightly more complicated example.  Following are
examples of a Matlab script and a Python script that should produce
the same result, but do not.  I think it may be due to the fact that
interp2d() is not doing a bilinear interpolation...

I am using scipy version '0.7.0.dev3442' on a Mac OS X.

Matlab code:
------------------------------------------------------------
x = [4.0600    4.0700    4.0800    3.9500    4.1100    3.8400
4.1200    3.8600;
4.0700    4.0800    4.0900    4.1100    4.1600    3.9200
3.9000    3.8700;
4.0700    4.2900    4.3000    4.3100    3.9400    4.0300
4.1700    4.1500;
4.0800    4.0300    4.1100    3.8800    4.0100    4.2000
4.0100    3.8400;
4.0900    3.9600    4.1100    4.1200    4.1800    3.9000
4.2000    4.1700;
4.1000    4.1100    4.0500    4.1300    4.1500    3.8900
3.9000    4.1800;
4.1000    4.1100    4.0600    4.1400    4.1500    4.1000
3.9000    4.1900;
4.1100    4.0600    4.1300    4.1500    4.3700    3.9000
4.1800    4.2000];

xi = [1.77 2.83 3.45 4.66 5.33 6.72 7.11];
yi = xi';

z = interp2(x,xi,yi,'linear')
------------------------------------------------------------

Python code:
------------------------------------------------------------
import numpy
from scipy.interpolate import interpolate

numpy.set_printoptions(precision=4,linewidth=120)

data = numpy.array
(([4.0600,4.0700,4.0800,3.9500,4.1100,3.8400,4.1200,3.8600],

[4.0700,4.0800,4.0900,4.1100,4.1600,3.9200,3.9000,3.8700],

[4.0700,4.2900,4.3000,4.3100,3.9400,4.0300,4.1700,4.1500],

[4.0800,4.0300,4.1100,3.8800,4.0100,4.2000,4.0100,3.8400],

[4.0900,3.9600,4.1100,4.1200,4.1800,3.9000,4.2000,4.1700],

[4.1000,4.1100,4.0500,4.1300,4.1500,3.8900,3.9000,4.1800],

[4.1000,4.1100,4.0600,4.1400,4.1500,4.1000,3.9000,4.1900],

[4.1100,4.0600,4.1300,4.1500,4.3700,3.9000,4.1800,4.2000]))

#indices are different from Matlab because Matlab indices are 1-based
xi = numpy.array(([0.77,1.83,2.45,3.66,4.33,5.72,6.11]))
yi = xi

xrange = numpy.arange(8)
yrange = numpy.arange(8)
X,Y = numpy.meshgrid(xrange,yrange)
outgrid = interpolate.interp2d(X,Y,data,kind='linear')
z = outgrid(xi,yi)
print z
------------------------------------------------------------

Matlab results look like this:
z =
4.0754    4.0860    4.0812    4.1229    4.0670    3.9369    3.9415
4.2119    4.2626    4.2696    4.0789    3.9886    4.0925    4.1217
4.1503    4.2074    4.1704    4.0208    4.0160    4.1004    4.0884
4.0074    4.0885    4.0778    4.0937    4.0825    4.0980    4.1269
4.0288    4.0765    4.1051    4.1542    4.0799    4.0438    4.1090
4.1077    4.0662    4.0932    4.1456    4.1141    3.9395    3.9316
4.1037    4.0740    4.1007    4.1629    4.1425    3.9720    3.9594

Python results look like this:
[[ 4.0233  4.0084  3.9881  4.1503  4.2896  3.8178  3.7936]
[ 4.2695  4.3692  4.469   4.1137  3.7766  4.2214  4.2578]
[ 4.1597  4.1808  4.0441  3.939   3.9272  3.9874  3.9296]
[ 3.9316  4.0521  4.1188  4.1464  4.1767  4.3089  4.3022]
[ 4.0464  4.0654  4.0979  4.1724  4.2222  4.0362  4.0943]
[ 4.1178  4.0647  4.1023  4.105   4.0723  3.7996  3.8618]
[ 4.1037  4.0776  4.1058  4.1629  4.1851  3.9047  3.9594]]

Thanks,

Mike Hearne

On Oct 16, 2007, at 10:57 AM, Michael Hearne wrote:

> I have a question regarding a warning I'm getting trying to do a 2D
> interpolation.  If I see the message below, does that mean I should
> not trust my results?
>
> Warning:     No more knots can be added because the number of B-
> spline coefficients
>     already exceeds the number of data points m. Probably causes:
> either
>     s or m too small. (fp>s)
>
>
>
>
> ------------------------------------------------------
> Michael Hearne
> mhearne@usgs.gov
> (303) 273-8620
> USGS National Earthquake Information Center
> 1711 Illinois St. Golden CO 80401
> Senior Software Engineer
> Synergetics, Inc.
> ------------------------------------------------------
>
>
> _______________________________________________
> SciPy-user mailing list
> SciPy-user@scipy.org
> http://projects.scipy.org/mailman/listinfo/scipy-user

------------------------------------------------------
Michael Hearne
mhearne@usgs.gov
(303) 273-8620
USGS National Earthquake Information Center
1711 Illinois St. Golden CO 80401
Senior Software Engineer
Synergetics, Inc.
------------------------------------------------------

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://projects.scipy.org/pipermail/scipy-user/attachments/20071017/fc56efdb/attachment-0001.html
```