Summary and usage of interpolation functions in MATLAB

Source: Internet
Author: User

The interpolation function in MATLAB is interp1. The Calling format is: Yi = interp1 (X, Y, XI, 'method ')

Where X and Y are interpolation points, Yi is the result of interpolation at the interpolation point XI; X and Y are vectors, and 'method' indicates the interpolation method used, there are several kinds of interpolation methods provided by MATLAB: 'method' is the nearest interpolation, 'linear 'linear interpolation, 'spline' cubic spline interpolation, and 'cubic'CubeInterpolation. Lack of time to represent linear interpolation

Note: All interpolation methods require that X is monotonous, and XI cannot exceed the range of X.

For example:In the 24 hours of a day, the environmental temperature data measured every two hours from is

12, 9, 9, 10, 18, 24, 28, 27, 25, 20, 18, 15, 13,

Estimate the temperature at noon.

X =;
Y = [12 9 9 10 18 24 28 27 25 20 18 15 13];

A = 13;
Y1 = interp1 (X, Y, A, 'spline ')

Result:27.8725

To get a 24-hour temperature curve:

Xi =/3600: 24;

Yi = interp1 (X, Y, XI, 'spline ');

Plot (X, Y, 'O', XI, Yi)

Command 1 interp1
One-dimensional functions Data Interpolation (Table search ). This command computes inner interpolation between data points. It finds the value of The unary function f (x) at the center point. The f (x) function is determined by the data given.
X: original data point
Y: original data point
XI: Interpolation Point
Yi: Interpolation Point
Format
(1) YI = interp1 (X, Y, xi)
Returns the interpolation vector Yi. Each element corresponds to the parameter XI, which is also determined by the inner interpolation of the vectors x and y. Parameter x specifies the point of data y.
If y is a matrix, it is calculated based on each column of Y. Yi is the output matrix of the order number length (xi) * size (Y, 2.
(2) YI = interp1 (Y, xi)
Assume that x = 1: n, where N is the length of vector y or the number of rows in matrix Y.
(3) YI = interp1 (X, Y, XI, method)
Use the specified Algorithm Calculated interpolation:
'Nearest ': interpolation of the nearest neighbor to directly complete the computation;
'Linear ': linear interpolation (default method), which directly completes computation;
'Spline': Cubic Spline function interpolation. For this method, the interp1 command calls the spline, ppval, mkpp, and umkpp functions. These commands generate a series of functions for piecewise polynomial operations. Run the spline command to execute cubic spline function interpolation;
'Pchip ': piecewise cubic Hermite interpolation. For this method, the interp1 command calls The PChIP function to perform piecewise inner Interpolation on the vectors x and y. This method retains the monotonicity and data shape;
'Cubic ': Same as 'pchip;
'V5cubic ': cubic Interpolation in MATLAB 5.0.
For the XI component that exceeds the X range, use the interpolation algorithms of 'nearest ', 'linear', and 'v5cubic 'to return Nan accordingly. For other methods, interp1 executes the external Interpolation Algorithm for the excess components.
(4) YI = interp1 (X, Y, XI, method, 'extrap ')
For components in Xi that are out of the range of X, special external interpolation extrap will be executed.
(5) YI = interp1 (X, Y, XI, method, extrapval)
Determine the extrapval of the component in Xi that is out of the X range. The value is generally Nan or 0.
Example 1

    1. > X = 0: 10; y = x. * sin (X );
    2. > Xx = 0:. 25:10; YY = interp1 (X, Y, XX );
    3. > Plot (X, Y, 'kd ', XX, YY)

CopyCode

Example 2

    1. > Year = 1900: 10: 2010;
    2. > Product = [75.995 91.972 105.711 123.203 131.669 150.697 179.323 203.212
    3. 249.633 256.344 267.893];
    4. > P1995 = interp1 (year, product, 1995)
    5. >> X = 1900: 1: 2010;
    6. > Y = interp1 (year, product, X, 'pchip ');
    7. > Plot (year, product, 'O', x, y)

Copy code

Interpolation result:

    1. P1995 =
    2. 252.9885

Copy code

Command 2 interp2
Function two-dimensional data interpolation (Table search)
Format
(1) Zi = interp2 (X, Y, Z, XI, Yi)
Returns the matrix Zi, which contains the elements corresponding to the parameters XI and Yi (which can be a vector or a matrix of the same type), that is, Zi (I, j) semi [XI (I, j), Yi (I, j)]. You can input row vectors and column vectors XI and Yi. In this case, the output vector Zi is the same as the matrix meshgrid (XI, Yi. It also depends on the two-dimensional functions z = f (x, y) determined by the input matrix X, Y, and Z ). Parameters X and Y must be monotonous and in the same format, just as they are generated by the command meshgrid. If Xi and Yi have vertices out of the range of X and Y, Nan (not a number) is returned accordingly ).
(2) Zi = interp2 (z, XI, Yi)
By default, x = 1: N, y = 1: m, where [M, N] = size (z ). The calculation is based on the first case.
(3) Zi = interp2 (z, n)
Perform n recursive calculations and insert two-dimensional interpolation between every two elements of Z. In this way, the order of Z will continue.Add.Interp2 (z) is equivalent to interp2 (z, 1 ).
(4) Zi = interp2 (X, Y, Z, XI, Yi, method)
Use the specified algorithm method to calculate two-dimensional interpolation:
'Linear ': bilinear interpolation algorithm (default algorithm );
'Nearest ': near interpolation;
'Spline': Cubic Spline Interpolation;
'Cubic ': Double cubic Interpolation.

Example 3:

    1. > [X, y] = meshgrid (-3:. 25:3 );
    2. > Z = peaks (x, y );
    3. > [XI, Yi] = meshgrid (-3:. 125:3 );
    4. > ZZ = interp2 (X, Y, Z, XI, Yi );
    5. > Surfl (x, y, z); Hold on;
    6. > Surfl (XI, Yi, ZZ + 15)
    7. > Axis ([-3 3-3 3-5 20]); shading flat
    8. > Hold off

Copy code

Example 4:

    1. > Years = 1950: 10: 1990;
    2. > Service = 10:10:30;
    3. > Wage = [150.697 199.592 187.625
    4. 179.323 195.072 250.287
    5. 203.212 179.092 322.767
    6. 226.505 153.706 426.730
    7. 249.633 120.281 598.243];
    8. > W = interp2 (Service, years, wage, 15,1975)

Copy code

Interpolation result:

    1. W =
    2. 190.6288

Copy code

Command 3 interp3
Function 3D data interpolation (look-up table)
Format
(1) Vi = interp3 (X, Y, Z, V, XI, Yi, zi)
Find the value of the ternary function v = V (x, y, z) at the vertex (XI, Yi, zi) determined by the X, Y, and Z parameters. The Xi, Yi, and Zi parameters are the same type arrays or vectors. If the vector parameters Xi, Yi, and Zi are vectors of different lengths and directions (rows or columns), the output parameters VI and Y1, Y2, and Y3 are the same type matrices. Y1, Y2, and Y3 are the same type arrays generated by the command meshgrid (XI, Yi, zi. If the interpolation point (XI, Yi, zi) contains a vertex other than the vertex (x, y, z), the special variable value Nan is returned accordingly.
(2) Vi = interp3 (v, XI, Yi, zi)
By default, x = 1: N, y = 1: M, Z = 1: P, where, [M, N, P] = size (V ), the calculation is based on the above situation.
(3) Vi = interp3 (v, n)
Perform n recursive calculations and insert three-dimensional interpolation between every two elements of V. In this way, the order of V will increase constantly. Interp3 (v) is equivalent to interp3 (V, 1 ).
(4) Vi = interp3 (..., method) % use the specified algorithm method for interpolation calculation:
'Linear ': linear interpolation (default algorithm );
'Cubic ': cubic Interpolation;
'Spline': Cubic Spline Interpolation;
'Nearest ': the nearest interpolation.
All algorithms require that x, y, and z be monotonous and have the same lattice form. When x, y, and z are same distance and monotonous, you can use algorithms like '* linear', '* cubic', and '* nearest' to obtain fast interpolation.

Example 5

    1. > [X, y, z, V] = flow (20 );
    2. > [XX, YY, zz] = meshgrid (. 1:. 25:10,-3:. 25:3,-3:. 25:3 );
    3. > VV = interp3 (X, Y, Z, V, XX, YY, ZZ );
    4. > Slice (XX, YY, ZZ, VV, [6 9.5], [1 2], [-2. 2]); shading interp; colormap cool

Copy code

Command 4 interpft
Function uses the fast Fourier algorithm for one-dimensional Interpolation
Format
(1) y = interpft (x, n)
Returns the interpolation y that contains the cyclic Function x at N points of the re-sample. If length (x) = M and X has a sampling interval dx, The New Y's sampling interval DY = DX * M/N. Note:N must be greater than or equal to M. If X is a matrix, it is calculated by column X. The returned matrix Y has the same number of columns as X, but n rows.
(2) y = interpft (x, N, dim)
Dim calculation in the specified direction

Command 5 griddata
Function data points
Format
(1) Zi = griddata (X, Y, Z, XI, Yi)
Use the binary function z = f (x, y) to fit an irregular data vector X, Y, Z. Griddata returns the interpolation of surface Z at points (XI, Yi. The surface always goes through these data points (x, y, z. The input parameter (XI, Yi) is usually the point of the rule (as generated using the meshgrid command ). Xi can be a single vector. In this case, Xi specifies a matrix with a constant column vector. Similarly, Yi can be a column vector, which specifies a matrix with a constant row vector.
(2) [XI, Yi, Zi] = griddata (X, Y, Z, XI, Yi)
The meaning of the returned matrix Zi is the same as that of the preceding one. At the same time, the returned matrix XI and Yi are generated by the line vector XI and the column vector Yi using the command meshgrid.
(3) [XI, Yi, Zi] = griddata (......, method)
Calculated using the specified algorithm method:
'Linear ': Linear triangle-based interpolation (default algorithm );
'Cubic ': triangle-based cubic Interpolation;
'Nearest ': the nearest interpolation method;
'V4 ': griddata Algorithm in MATLAB 4.

Command 6 Spline
Function cubic spline Data Interpolation
Format
(1) YY = spline (X, Y, XX)
For the given discrete measurement data X, Y (called breakpoint ), To find 1 Polynomial y = p (x) to approximate the curve between each pair of data (x, y) points. After two points (XI, Yi) and (xi + 1, yi + 1), only one straight line can be identified, and the cubic polynomial curves passing through one point have infinite rows. In order to make the cubic polynomial curve passing through the intermediate breakpoint unique, two conditions need to be added (because the cubic polynomial has four coefficients ):
A. cubic polynomials at points (XI, Yi) include: P & cent; I (xi) = P & cent; I (XI );
B. cubic polynomials at points (XI + 1, yi + 1) include: P & cent; I (XI + 1) = PI & cent; (xi + 1 );
The slope of c. p (x) At the point (XI, Yi) is continuous (in order to make the cubic polynomials have good attention, the conditions are added );
D. P (x) the curvature at the vertex (XI, Yi) is continuous;
For the first and last polynomials, the following conditions are artificially specified:
①. P & cent; 1 & cent; (x) = P & cent; 2 & cent; (x)
②. P & cent; N & cent; (x) = P & cent; N & cent;-1 (x)
The preceding two conditions are called non-a-knot conditions. Based on the above content, we can see that the cubic spline function p (x) for data fitting is a piecewise cubic polynomial:
& Iuml; & icirc;
& Iuml; í
Bytes
& Pound;
& Pound;
& Pound;
=
N + 1
2 2 3
1 1 2
P (x) x
P (x) x
P (x) x
P (x)
L
Each Pi (X) is a cubic polynomial.
This command uses cubic spline interpolation to calculate the value of The unary Function Y = f (x) determined by the vectors x and y at the Point xx. If the parameter Y is a matrix, each column of Y is paired with X, and then the values of the functions determined by them at point XX are calculated respectively. YY is a matrix with the first order of length (XX) * size (Y, 2.
(2) pp = spline (x, y)
Returns the coefficient matrix PP of the piecewise splines determined by the vectors x and y. It can be used for ppval and unmkpp calculations.

Example 6
Perform Spline Interpolation on the data points discretely distributed on the Y = exp (x) sin (x) Function Curve:

    1. > X = [0 2 4 5 8 12 12.8 17.2 19.9 20]; y = exp (x). * sin (X );
    2. > Xx = 0:. 25:20;
    3. > YY = spline (X, Y, XX );
    4. > Plot (X, Y, 'O', XX, YY)

Copy code

Command 7 interpn
Function n-dimensional data interpolation (look-up table)
Format
(1) Vi = interpn (x1, x2, xn, V, Y1, Y2, clerk, yn) % returns the result of parameters x1, x2 ,..., N-element functions determined by XN and V v = V (x1, x2 ,..., XN) at (Y1, Y2 ,..., YN. Parameter Y1, Y2 ,..., YN is a matrix or vector of the same type. If Y1, Y2 ,..., If YN is a vector, you can
Is a vector of different lengths and directions (rows or columns. They will generate a matrix of the same type by running the ndgrid command before computation. If you click (Y1, Y2 ,..., YN) has a vertex (x1, x2 ,..., Point other than XN, the special variable Nan is returned accordingly.
Vi = interpn (v, Y1, Y2, clerk, yn) % by default, X1 = 1: size (V, 1), X2 = 1: size (v, 2 ),... ,
Xn = 1: size (v, n.
Vi = interpn (v, ntimes) % for ntimes recursive computation, insert their n-dimensional interpolation between each two elements of v. In this way, the order of V will increase constantly. Interpn (V)
It is equivalent to interpn (V, 1 ).
Vi = interpn (scheme, method) % is calculated using the specified algorithm method:
'Linear ': linear interpolation (default algorithm );
'Cubic ': cubic Interpolation;
'Spline': Cubic Spline Interpolation;
'Ignore': the nearest interpolation algorithm.

Command 8 meshgrid
Function to generate matrix data used to draw 3D images.
The format [x, y] = meshgrid (X, Y) is determined by the Regions specified by the vectors x, y (which can be in different directions) [min (x), max (x ), min (Y), max (y)] uses a straight line x = x (I), Y = Y (j) (I = 1, 2 ,..., Length (x), j = 1, 2 ,..., Length (y. In this way, the length (x) * length (y) points are obtained,
The X coordinate of these points is represented by matrix X, and each row vector of X is the same as that of vector X. The Y coordinate of these points is represented by matrix Y, and each column vector of Y is the same as that of vector y. X and Y can be used to calculate the division of the binary function z = f (x, y) and the XY plane rectangular definite domain in a three-dimensional image or
Plot the surface.
[X, y] = meshgrid (x) % is equivalent to [x, y] = meshgrid (x, x ).
[X, y, z] = meshgrid (x, y, z) % to generate a three-dimensional array X, Y, Z, used to calculate the ternary function v = f (x, y, z) or a three-dimensional volume chart.

Example 7

    1. [X, y] = meshgrid)

Copy code

The calculation result is:

    1. X =
    2. 1 2 3
    3. 1 2 3
    4. 1 2 3
    5. 1 2 3
    6. 1 2 3
    7. Y =
    8. 10 10 10
    9. 11 11 11 11
    10. 12 12 12
    11. 13 13 13
    12. 14 14 14

Copy code

Command 9 ndgridFunction generation array for multi-dimensional function compute or multi-dimensional Interpolation
Format: [x1, x2 ,..., XN] = ndgrid (x1, x2 ,..., XN) % pass the vector x1, x2, X3 ..., XN Specified RegionConversionArray x1, x2, X3 ,..., XN. In this way, we get the length (X1) * length (X2 )*... * Length (Xn) points. The first-dimensional coordinates of these points are in the matrix X1 table.
Shows that each of the First-dimensional vectors of X1 is the same as that of vector X1. The second-dimensional coordinates of these points are represented by matrix X2, and each second-dimensional vector of X2 is the same as that of vector X2.
X1, x2 ,..., XN can be used to calculate the multivariate Function Y = f (x1, x2 ,..., XN) and the array used by the multi-dimensional interpolation command.
[X1, x2 ,..., XN] = ndgrid (x) % is equivalent to [x1, x2 ,..., XN] = ndgrid (x, x ,..., X)

Command 10 Table1
One-dimensional function Lookup
The format y = Table1 (tab, X0) % returns the result y obtained by linear interpolation of x0 (search for x0 in the first column of the tab) using the row linear interpolation element in the table matrix tab. The matrix tab is the first column containing
Key value, while other columns contain a matrix of data. Each element in x0 returns the first-line interpolation row vector accordingly. The first column of the Matrix tab must be monotonous.

Example 8

    1. > Tab = [() 'hilb (4)]
    2. > Y = Table1 (tab, [1 2.3 3.6 4])

Copy code

The query results are as follows:

    1. > Tab = [() 'hilb (4)]
    2. > Y = Table1 (tab, [1 2.3 3.6 4])

copy the Code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.