Professional Documents
Culture Documents
Syntax
Vq = interp2(X,Y,V,Xq,Yq)
Vq = interp2(V,Xq,Yq)
Vq = interp2(V)
Vq = interp2(V,k)
Vq = interp2(___,method)
Vq = interp2(___,method,extrapval)
Description
example
Vq = interp2(X,Y,V,Xq,Yq) returns interpolated values of a function of two variables at
specific query points using linear interpolation. The results always pass through the original
sampling of the function. X and Y contain the coordinates of the sample points. V contains the
corresponding function values at each sample point. Xq and Yq contain the coordinates of the
query points.
Vq = interp2(V,Xq,Yq) assumes a default grid of sample points. The default grid points
cover the rectangular region, X=1:n and Y=1:m, where [m,n] = size(V). Use this syntax
when you want to conserve memory and are not concerned about the absolute distances
between points.
Vq = interp2(V) returns the interpolated values on a refined grid formed by dividing the
interval between sample values once in each dimension.
example
Vq = interp2(V,k) returns the interpolated values on a refined grid formed by repeatedly
halving the intervals k times in each dimension. This results in 2^k-1 interpolated points
between sample values.
example
Vq = interp2(___,method) specifies an alternative interpolation
method: 'linear', 'nearest', 'cubic', 'makima', or 'spline'. The default method
is 'linear'.
example
Vq = interp2(___,method,extrapval) also specifies extrapval, a scalar value that is
assigned to all queries that lie outside the domain of the sample points.
If you omit the extrapval argument for queries outside the domain of the sample points, then
based on the method argument interp2 returns one of the following:
• Extrapolated values for the 'spline' and 'makima' methods
• NaN values for other interpolation methods
Examples
collapse all
Interpolate Over a Grid Using Default Method
Try This Example
Coarsely sample the peaks function.
[X,Y] = meshgrid(-3:3);
V = peaks(X,Y);
Plot the coarse sampling.
figure
surf(X,Y,V)
title('Original Sampling');
• If X and Y are matrices representing a full grid (in meshgrid format), then V must be the same
size as X and Y.
• If X and Y are grid vectors, then V must be a matrix containing length(Y) rows
and length(X) columns.
If V contains complex numbers, then interp2 interpolates the real and imaginary parts
separately.
Example: rand(10,10)
Data Types: single | double
Complex Number Support: Yes
Xq,Yq — Query points
scalars | vectors | matrices | arrays
Query points, specified as a real scalars, vectors, matrices, or arrays.
• If Xq and Yq are scalars, then they are the coordinates of a single query point.
• If Xq and Yq are vectors of different orientations, then Xq and Yq are treated as grid vectors.
• If Xq and Yq are vectors of the same size and orientation, then Xq and Yq are treated
as scattered points in 2-D space.
• If Xq and Yq are matrices, then they represent either a full grid of query points
(in meshgrid format) or scattered points.
• If Xq and Yq are N-D arrays, then they represent scattered points in 2-D space.
Note
In a future release, interp2 will not accept mixed combinations of row and column vectors for
the sample and query grids. Instead, you must construct the full grid using meshgrid.
Alternatively, if you have a large data set, you can use griddedInterpolant instead
of interp2.
Example: interp2(V,2)
Data Types: single | double
method — Interpolation method
'linear' (default) | 'nearest' | 'cubic' | 'spline' | 'makima'
Interpolation method, specified as one of the options in this table.
'linear' The interpolated value at a query point is based on C0 • Requires at least two grid
linear interpolation of the values at neighboring points in each dimension
grid points in each respective dimension. This is
the default interpolation method. • Requires more memory
than'nearest'
'nearest' The interpolated value at a query point is the value • Requires two grid points in
Discontinuous
at the nearest sample grid point. each dimension.
• Fastest computation with
modest memory requirements
'cubic' The interpolated value at a query point is based on C1 • Grid must have uniform
a cubic interpolation of the values at neighboring spacing in each dimension, but
grid points in each respective dimension. The the spacing does not have to be
interpolation is based on a cubic convolution. the same for all dimensions
• Requires at least four points in
each dimension
• Requires more memory and
computation time
than 'linear'
'makima' Modified Akima cubic Hermite interpolation. The C1 • Requires at least 2 points in
interpolated value at a query point is based on a each dimension
piecewise function of polynomials with degree at
most three evaluated using the values of • Produces fewer undulations
neighboring grid points in each respective than'spline'
dimension. The Akima formula is modified to • Computation time is typically
avoid overshoots. less than 'spline', but the
memory requirements are
similar
'spline' The interpolated value at a query point is based on C2 • Requires four points in each
a cubic interpolation of the values at neighboring dimension
grid points in each respective dimension. The
interpolation is based on a cubic spline using not-a- • Requires more memory and
knot end conditions. computation time
than 'cubic'
Output Arguments
collapse all
Vq — Interpolated values
scalar | vector | matrix
Interpolated values, returned as a real or complex scalar, vector, or matrix. The size and shape
of Vq depends on the syntax you use and, in some cases, the size and value of the input
arguments.
Special
Syntaxes Size of Vq Example
Conditions
Same as above Xq, Yq are Matrix or array of the same If size(Xq) = [50
matrices or size as Xq and Yq 25]
arrays of the and size(Yq) =
same size [50 25],
then size(Vq) =
[50 25].
Strictly Monotonic
A set of values that are always increasing or decreasing, without reversals. For example, the
sequence, a = [2 4 6 8] is strictly monotonic and increasing. The sequence, b = [2 4 4 6
8] is not strictly monotonic because there is no change in value between b(2) and b(3). The
sequence, c = [2 4 6 8 6] contains a reversal between c(4)and c(5), so it is not monotonic
at all.
-1 0 1 2 3
-1 0 1 2 3
-1 0 1 2 3
-1 0 1 2 3
Y =
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
4 4 4 4 4
Grid Vectors
For interp2, grid vectors consist of a pair of vectors that define the x- and y-coordinates in a
grid.
For example, the following code creates the grid vectors that specify the region, –1 ≤ x ≤ 3 and 1
≤ y ≤ 4:
x = -1:3;
y = (1:4)';
Scattered Points
For interp2, scattered points consist of a pair of arrays that define a collection of points
scattered in 2-D space. One array contains the x-coordinates, and the other contains the y-
coordinates.
For example, the following code specifies the points, (2,7), (5,3), (4,1), and (10,9):
x = [2 5; 4 10];
y = [7 3; 1 9];
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.