Function Reference
— Function File: zi= interp2 (x, y, z, xi, yi)
— Function File: zi= interp2 (Z, xi, yi)
— Function File: zi= interp2 (Z, n)
— Function File: zi= interp2 (..., method)
— Function File: zi= interp2 (..., method, extrapval)

Two-dimensional interpolation. x, y and z describe a surface function. If x and y are vectors their length must correspondent to the size of z. x and Yy must be monotonic. If they are matrices they must have the meshgrid format.

interp2 (x, y, Z, xi, yi, ...)
Returns a matrix corresponding to the points described by the matrices XI, YI.

If the last argument is a string, the interpolation method can be specified. The method can be 'linear', 'nearest' or 'cubic'. If it is omitted 'linear' interpolation is assumed.

interp2 (z, xi, yi)
Assumes x = 1:rows (z) and y = 1:columns (z)
interp2 (z, n)
Interleaves the Matrix z n-times. If n is omitted a value of n = 1 is assumed.

The variable method defines the method to use for the interpolation. It can take one of the values

'nearest'
Return the nearest neighbor.
'linear'
Linear interpolation from nearest neighbors.
'pchip'
Piece-wise cubic hermite interpolating polynomial (not implemented yet).
'cubic'
Cubic interpolation from four nearest neighbors.
'spline'
Cubic spline interpolation–smooth first and second derivatives throughout the curve.

If a scalar value extrapval is defined as the final value, then values outside the mesh as set to this value. Note that in this case method must be defined as well. If extrapval is not defined then NA is assumed.

See also: interp1

Demonstration 1

The following code

 A=[13,-1,12;5,4,3;1,6,2];
 x=[0,1,4]; y=[10,11,12];
 xi=linspace(min(x),max(x),17);
 yi=linspace(min(y),max(y),26)';
 mesh(xi,yi,interp2(x,y,A,xi,yi,'linear'));
 [x,y] = meshgrid(x,y); 
 hold on; plot3(x(:),y(:),A(:),"b*"); hold off;

Produces the following figure

Demonstration 2

The following code

 A=[13,-1,12;5,4,3;1,6,2];
 x=[0,1,4]; y=[10,11,12];
 xi=linspace(min(x),max(x),17);
 yi=linspace(min(y),max(y),26)';
 mesh(xi,yi,interp2(x,y,A,xi,yi,'nearest'));
 [x,y] = meshgrid(x,y); 
 hold on; plot3(x(:),y(:),A(:),"b*"); hold off;

Produces the following figure

Demonstration 3

The following code

 A=[13,-1,12;5,4,3;1,6,2];
 x=[0,1,2]; y=[10,11,12];
 xi=linspace(min(x),max(x),17);
 yi=linspace(min(y),max(y),26)';
 mesh(xi,yi,interp2(x,y,A,xi,yi,'cubic'));
 [x,y] = meshgrid(x,y); 
 hold on; plot3(x(:),y(:),A(:),"b*"); hold off;

Produces the following figure

Demonstration 4

The following code

 A=[13,-1,12;5,4,3;1,6,2];
 x=[0,1,2]; y=[10,11,12];
 xi=linspace(min(x),max(x),17);
 yi=linspace(min(y),max(y),26)';
 mesh(xi,yi,interp2(x,y,A,xi,yi,'spline'));
 [x,y] = meshgrid(x,y); 
 hold on; plot3(x(:),y(:),A(:),"b*"); hold off;

Produces the following figure