Matlab Meshgrid, Interp, griddata usage and examples (RPM)

Source: Internet
Author: User

Http://blog.sina.com.cn/s/blog_67f37e760101bu4e.html

Instance result http://wenku.baidu.com/link?url= Sigsfzixus1e1vzwtixqxdjg5y9sy4tu1w8txgk147hdollcgpffjx8ywdmih1pykqsi5rp8gkmnmpslhh-iuagjml8hsrhzqsstpmnbjdy

About Matlab interpolation (interpolation) http://blog.sina.com.cn/s/blog_4c7482f101009vm2.html

Matlab Two-dimensional interpolation--interp2 and Griddata fromhttp://3y.uu456.com/bp_32jw03xolo8iiwn479ge_1.html

1. Meshgrid

The Meshgrid is used to generate meshes from arrays A and B. The resulting mesh matrix A and B sizes are the same. It can also be a higher dimension.

[A,b]=meshgrid (A, B)
Generates matrices A and B of size (b) xsize (a). It is equivalent to a repeating increment from row to size (b), the B to a column, and then to the size (a) column. The command is therefore equivalent to:

A=ones (size (b)) *a;
B=b ' *ones (Size (a))

As shown below:

>> A=[1:2]

A =

1 2

>> B=[3:5]

b =

3 4 5

>> [A,b]=meshgrid (A, B)

A =

1 2
1 2
1 2


B =

3 3
4 4
5 5

>> [B,a]=meshgrid (B,a)

B =

3 4 5
3 4 5


A =

1 1 1
2 2 2

2. Interp

interp1--one-dimensional data interpolation function

One-dimensional data interpolation. This function calculates interpolation between data points, which finds the value of the unary function f (x) at the midpoint, where the function expression is determined by the given data. YI=INTERP1 (X,Y,XI): Returns the interpolation vector yi, each element corresponds to the parameter XI, and is determined by the interpolation of the vector x and Y. Parameter x Specifies the point of the data Y. If Y is a matrix, then each column of y is calculated. Yi is the output matrix of the order Length (xi) *size (y,2). YI=INTERP1 (Y,XI): Assumes X=1:n, where N is the length of the vector y, or the number of rows of matrix Y. YI=INTERP1 (X,y,xi,method): Calculates the interpolation with the specified algorithm. Nearest is the nearest neighbor interpolation, the calculation is done directly, the linear is the linear interpolation (the default), and the spline is interpolated for three spline functions. YI=INTERP1 (X,y,xi,method, ' Extrap '): A special extrapolation method Extrap is performed for components that exceed the X range of the XI. YI=INTERP1 (X,y,xi,method,extrapval): Determines the extrapolation value extrapval of the component in the XI of the X range, which usually takes Nan or 0. interp2 Function--interpolation in two-D dataComplete the two-dimensional data interpolation. Zi=interp2 (X,y,z,xi,yi): Returns the Matrix ZI whose elements contain elements corresponding to the parameter XI and YI (which can be a vector, or a matrix of the same type). The user can enter the row vector and the column Vector Xi and Yi, at this time, the output vector zi and matrix Meshgrid (Xi,yi) is the same type. It also depends on the two-dimensional function z=f (x, y) determined by the input matrix ×, Y, and Z. Zi=interp2 (z,xi,yi): By default, X=1:n, Y=1:m, where [M,n]=size (Z). Then the first case is calculated. Zi=interp2 (z,n): For n recursive calculations, insert their two-dimensional interpolation between each of the two elements in Z, so that the order of Z will continue to increase. INTERP2 (Z) is equivalent to Interp2 (z,1). Zi=interp2 (X,y,z,xi,yi,method): Calculates two-dimensional interpolation with the specified algorithm method. Linear is a bilinear interpolation algorithm (the default algorithm), nearest is the nearest interpolation, spline is three spline interpolation, Cubic is a double three-time interpolation. INTERP3 Function--three-dimensional data interpolationComplete the three-dimensional data interpolation. VI=INTERP3 (X,y,z,v,xi,yi,zi): The value of the ternary function v=v (x, Y, z) at the point (Xi,yi,zi) determined by the parameters x, Y, Z. The parametric Xi,yi,zi is the same pattern or vector. If the vector parameter xi,yi,zi is a vector of different lengths and directions (rows or columns), then the output parameter VI and Y1,Y2,Y3 are the same type matrix. The y1,y2,y3 is the same pattern generated with the function Meshgrid (Xi,yi,zi). If there are points in the interpolation point (Xi,yi,zi) that are outside the point (x, Y, z), the special variable value nan is returned accordingly. VI=INTERP3 (V,xi,yi,zi): By default, X=1:n,y=1:m,z=1:p, where, [M,n,p]=size (V), is calculated as above. VI=INTERP3 (V,n): N-times recursive calculation, inserting their three-dimensional interpolation between each of the two elements of V. In this way, the order of V will continue to increase. INTERP3 (V) is equivalent to INTERP3 (v,1). VI=INTERP3 (..., method): Interpolation is calculated using the specified algorithm method. The linear is a linear interpolation (the default algorithm), the cubic is three interpolation, the spline is three spline interpolation, and nearest is the nearest neighbor interpolation. INTERPN function--n Dimension data interpolationComplete the n-dimensional data interpolation. VI=INTERPN (x1,x2,..., xn,v,y1,y2,.., Yn): Returns the interpolated value of the N-ary function x1,x2 (xn,v,.., v=v) determined by the parameter x1,x2,.., Xn at the point (y1,y2,..., Yn). Parametric y1,y2,..., yn are matrices or vectors of the same type. If the Y1,y2,..., yn is a vector, it can be a vector of different lengths, in different directions (rows or columns). VI=INTERPN (V,y1,y2,..., Yn): By default, X1=1:size (v,1), X2=1:size (v,2),..., xn=1:size (v,n), and then calculated as above. VI=INTERPN (v,ntimes): For ntimes recursive calculation, insert their n-dimensional interpolation between each of the two elements of V. In this way, the order of V will continue to increase. INTERPN (V) is equivalent to INTERPN (v,1). 3. Griddata
function Data grid point
Format
(1) ZI = Griddata (x,y,z,xi,yi)
The z=f (x, y) surface of the two-tuple function is fitted with an irregular data vector x, Y, Z. Griddata returns the interpolation at the point (xi,yi) of the surface Z. Surfaces are always passing through these data points (x, y, z). The input parameter (xi,yi) is usually the Gerther of the rule (as generated by the command Meshgrid). Xi can be a line vector, and Xi specifies a matrix with a constant sequence of vectors. Similarly, Yi can be a column of vectors that specifies a matrix with a constant line vector.
(2) [Xi,yi,zi] = Griddata (x,y,z,xi,yi)
The returned matrix Zi meaning ibid, and the returned matrix Xi,yi is generated by the line vector XI and the column vector YI with the command Meshgrid.
(3) [Xi,yi,zi] = Griddata (..., method)
Calculated using the specified algorithm method:
' Linear ': linear interpolation based on triangles (default algorithm);
' Cubic ': three times interpolation based on triangles;
' Nearest ': Nearest neighbor interpolation method;
' V4 ': The Griddata algorithm in MATLAB 4. 4.matlab Two-dimensional interpolation--interp2 and Griddata

Both are common two-dimensional difference methods, the difference is that the INTERP2 interpolation data must be a rectangular field, that is, the known quantity point (x, y) composition of the rules of the matrix, or called a raster, can be generated using Meshgid. The known quantity point (X, y) of the Griddata function does not require a regular arrangement, especially the interpolation of data that is randomly not regularly taken in the experiment. Griddata (X,y,xi,yi, ' v4 ') v4 is an interpolation algorithm, no specific name, the original is called "MATLAB 4 Griddata Method", is a very smooth difference algorithm, the effect is good. X and y provide the known quantity stronghold, Xi and Yi are the data points that need interpolation, generally use meshgrid generation, of course, but also other data, but that is more troublesome when drawing, can not use mesh, etc., can only use Trimesh.

Examples are as follows:

a=[
3 3 1.5300
3 27 0.4210
5 17 0.5980
9 9 0.5900
13 25 0.4470
15 15 1
17 5 0.3830
21 21 0.3100
25 13 0.2830
27 3 0.2820
27 27 0.1200
];
X=a (:, 1);
Y=a (:, 2);
Z=a (:, 3);
Xtemp=linspace (min (x), Max (x), 100);
Ytemp=linspace (min (y), Max (y), 100);
[X,y]=meshgrid (Xtemp,ytemp);
Z=griddata (x,y,z,x,y, ' V4 ');
Surf (x, y, z)
Shading Interp

————————————————————————————————————————————————————————————————————————————————————————————

INTERP2 Two-dimensional data interpolation function in MATLAB

Interp2
Functional two-dimensional data interpolation (table lookup)
Format
(1) ZI = Interp2 (x,y,z,xi,yi)
Returns the Matrix Zi, whose elements contain elements corresponding to the parameter Xi and Yi (which can be a vector, or a matrix of the same type), namely Zi (i,j) ←[xi (i,j), Yi (i,j)]. The user can enter the row vector and the column Vector Xi and Yi, at this time, the output vector zi and matrix Meshgrid (Xi,yi) is the same type. It also depends on the two-dimensional function z=f (x, y) determined by the input matrix ×, Y, and Z. The parameters x and Y must be monotonous, and the same partitioning format, as generated by the command Meshgrid. If Xi and Yi have points outside the X and Y ranges, then the Nan (not a number) is returned accordingly.
matlab Chinese forum


(2) ZI = Interp2 (z,xi,yi)
By default, X=1:n, Y=1:m, where [M,n]=size (Z). Then the first case is calculated.
(3) ZI = Interp2 (z,n)
For n recursive calculations, insert their two-dimensional interpolation between each of the two elements in Z, so that the order of Z will continue to increase. INTERP2 (Z) is equivalent to Interp2 (z,1).

matlab Chinese Forum


(4) ZI = Interp2 (X,y,z,xi,yi,method)
Computes two-dimensional interpolation with the specified algorithm method:
' Linear ': bilinear interpolation algorithm (default algorithm);
' Nearest ': the nearest interpolation;
' Spline ': three times spline interpolation;
' Cubic ': double three times interpolation. book.iLoveMatlab.cn
Example 1
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);
6. >>SURFL (XI,YI,ZZ+15)
7. >>axis ([-3 3-3 3-5]) shading flat "simulink and signal Processing"
8. >>hold off
Copy Code
Example 2
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.730www.ilovematlab.cn
7.249.633 120.281 598.243];
8. >>w = Interp2 (service,years,wage,15,1975)
Copy Code
The interpolation results are:
1. W =
2.190.6288

Matlab Meshgrid, Interp, griddata usage and examples (RPM)

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.