# [SciPy-user] 2d interpolation

Michael Hearne mhearne@usgs....
Wed Aug 1 16:15:22 CDT 2007

```All:  I'm trying to use interp2d to replicate behavior in Matlab.

The Matlab script:
x = reshape(1:16,4,4)';
xi = 1:0.5:4;
yi = [1:0.5:4]';
z = interp2(x,xi,yi,'linear')

which results in the matrix:
z =

1.0000    1.5000    2.0000    2.5000    3.0000    3.5000    4.0000
3.0000    3.5000    4.0000    4.5000    5.0000    5.5000    6.0000
5.0000    5.5000    6.0000    6.5000    7.0000    7.5000    8.0000
7.0000    7.5000    8.0000    8.5000    9.0000    9.5000   10.0000
9.0000    9.5000   10.0000   10.5000   11.0000   11.5000   12.0000
11.0000   11.5000   12.0000   12.5000   13.0000   13.5000   14.0000
13.0000   13.5000   14.0000   14.5000   15.0000   15.5000   16.0000

I had thought the following Python/numpy script would be equivalent,
but it is not:
from scipy.interpolate import interpolate
from numpy.random import randn
from numpy import *

data = arange(16)
data = data+1
data = data.reshape(4,4)
xrange = arange(4)
yrange = arange(4)
X,Y = meshgrid(xrange,yrange)

outgrid = interpolate.interp2d(X,Y,data,kind='linear')
xi = array([0,0.5,1,1.5,2,2.5,3])
yi = xi

z = outgrid(xi,yi)

This results in the matrix:
[[  1.           1.10731213   2.           2.89268787   3.
3.25045605
4.        ]
[  3.           2.57118448   4.           5.42881552   5.
4.90975947
6.        ]
[  5.           4.03505682   6.           7.96494318   7.
6.56906289
8.        ]
[  7.           5.49892917   8.          10.50107083   9.
8.22836631
10.        ]
[  9.           6.96280152  10.          13.03719848  11.
9.88766973
12.        ]
[ 11.           8.42667386  12.          15.57332614  13.
11.54697315
14.        ]
[ 13.           9.89054621  14.          18.10945379  15.
13.20627657
16.        ]]

(Incidentally, is there a way to pretty-print arrays in numpy?  The
above is kind of ugly and hard to read)

Is this some kind of spline interpolation that I don't understand?

Thanks,

Mike Hearne

------------------------------------------------------
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/20070801/5ae71590/attachment-0001.html
```