Interpolation: Approximate function of a known finite number of points
Fit: Known finite number of points, approximate function, do not require over known quantity stronghold, only require in some sense at these point error minimum
(i) Interpolation method one, LaGrand day polynomial interpolation 1, interpolation polynomial
is to make a polynomial function, pass the given n nodes, and as close as possible to the original function, the point into the polynomial function to get a linear equation
When the coefficient matrix is full rank, there is a unique solution. And, the determinant of the coefficient matrix is
This is a Vandermonde determinant, as long as each node is not at the same time, the determinant is not 0, so it is possible to solve the coefficient equation
There are some indicators
2. Lagrange interpolation polynomial
3, MATLAB implementation
function y=Lagrange (x0,y0,x)
%n data in an array x0,y0 input, m interpolation points with array x input, output array y is M interpolation. n =length (x0); M=length (x); for i=M; Z =x (i); s =0.0 ; for k=1 :n p =1.0 ; for j=1 :N; if j~=k P =p* (Z-x0 (j))/(x0 (k)-
x0 (j)); End End S =p*y0 (k) +
s; End Y (i) =
s;end
Second, Newton interpolation 1, Difference quotient
2, Newton interpolation formula
A bit is, one more data point, only one more item
Ps:
3. Differential
4, equidistant node interpolation formula
Three, piecewise linear interpolation 1, the oscillation of the interpolation polynomial
That is, if the number of interpolation polynomial is higher, the more prone to oscillation, not good fit.
2. piecewise linear interpolation
3, MATLAB implementation
Four, Hermite interpolation 1, Hermite interpolation polynomial
2, MATLAB implementation
Function yHermite (x0,y0,y1,x);%X0,y0 is the sample point data, Y1 is the derivative, m interpolation points with array x input, output array y is m interpolation n=length (x0); m=length (x); fork=1: M; YY=0.0; forI=1: N H=1.0; A=0.0; forj=i:nifj~=I h=h* ((x (k)-x0 (J))/(x0 (i)-x0 (j)) ^2; A=1/(x0 (i)-x0 (j)) +A; End End YY=yy+h* ((x0 (i)-x0 (k)) * (2*a*y0 (i)-y (i)) +y0 (i)); End Y (k)=Yy;end
Five, spline interpolation 1, concept
In practice, the most commonly used cases are k=2 and k=3.
二、二次 spline function interpolation two times the spline function has n+2 constants, so there are n+2 conditions to have a unique solution
Be sure to have one condition for the first derivative
三、三次 sample function interpolation two times the spline function has n+3 constants, so there are n+3 conditions to have a unique solution
The implementation of 四、三次 interpolation in MATLAB
Partial reprint
1, Y=INTERP1 (x0,y0,x, ' spline '); % (spline changes to linear, then linear interpolation)
2, Y=spline (X0,Y0,XI); This is based on the known x, y data, using the spline function to interpolate the value of XI. That is, the value of the function that corresponds to Xi is computed by x, Y.
3, Pp=spline (x0,y0) , the% is determined by the X, Y data, according to the known, the spline function expression, but the expression is not directly represented by the matrix, requires point X ' value, to use the function y ' =ppval (pp,x ');
4, Pp=csape (x, y, ' variable boundary type ', ' Boundary value conds '); Generates three spline interpolation for various boundary conditions. where (x, y) is a data vector, the boundary type can be:
' Complete ': the first derivative of a given boundary, i.e. the default boundary condition, Lagrange boundary condition
' Not-a-knot ': non-kink condition without giving the boundary value.
' Periodic ': periodic boundary conditions without giving the boundary value.
' Second ': The second derivative of a given boundary.
' variational ': Natural spline (the second derivative of the boundary is [0,0].
Five, demo
% Reprint = =
CLEAR,CLC x0=[0,3,5,7,9, One, A, -, -, the]; Y0=[0,1.2,1.7,2.0,2.1,2.0,1.8,1.2,1.0,1.6]; T=0:0.05: the; %Lagrange interpolation function y1=lagrange (x0,y0,t);Call the Lagrange function written dy1= (Lagrange (x0,y0,0.0001)-lagrange (X0,y0,0))/0.0001%x=0 Slope min1=min (Lagrange (X0,y0, -:0.001: the))%13 to 15 min subplot (2,2,1); Plot (X0,y0,'ro', t,y1);%Draw the curve title ('Lagrange interpolation function'); %piecewise linear interpolation y2=INTERP1 (X0,y0,t,'Spline');%note the distinction between spline and linear Y2=INTERP1 (x0,y0,t);Default Linear Dy2= (INTERP1 (x0,y0,0.0001,'Spline')-interp1 (X0,y0,0,'Spline'))/0.0001%x=0 Slope min2=min (INTERP1 (X0,y0, -:0.001: the,'Spline'))%13 to 15 min subplot (2,2,2); Plot (T,y2,'b', T,y2,'R', X0,y0,'ro');%Draw the curve title ('piecewise linear interpolation'); Legend ('Edge Strips','Linear');%Show graph legend%three-time line interpolation a y3=spline (x0,y0,t); Dy3= (Spline (x0,y0,0.0001)-spline (X0,y0,0))/0.0001%x=0 Slope min3=min (Spline (x0,y0, -:0.001: the))%13 to 15 min subplot (2,2,3); Plot (X0,y0,'ro', t,y3);%Draw the curve title ('three-time line interpolation a'); %three-time line interpolation B pp1=csape (X0,Y0);The default boundary condition, that is, the first derivative of a given boundary pp2=csape (X0,y0,'Second');%second derivative y4 of a given boundary=Ppval (pp1,t); Y4=Ppval (pp2,t); Dy4= (Ppval (PP1,0.0001)-ppval (PP1,0))/0.0001%x=0 Slope min4=min (Ppval (PP1, -:0.001: the))%13 to 15 min subplot (2,2,4); Plot (T,y4,'b', T,y4,'R', X0,y0,'ro');%Draw the curve title ('three-time line interpolation B'); Legend ('First Order','Second Order');
Seven or two-D interpolation
If the node is two-dimensional, the interpolation function is a two-dollar function (surface), we can draw three-dimensional
1. The interpolation node is the network node
MATLAB Encapsulation Program
Z=interp2 (X0,y0,z0,x,y, ' method ')
A, x0,y0 is the node coordinate, z0 is the n*m dimension matrix, which represents the value of the node
B, X0,y0 requires one for the row vector one for the column vector
C, Z is a matrix, n=length (y), m=length (x) because MATLAB is column-First
D
E, x, y are interpolated point coordinates, z is the function value
Then, if it is a three-time spline interpolation, you can use the command
Pp=csape ({x0,y0},z0,conds,valconds), Z=fnval (Pp,{x,y})
A, x0,y0 is the node coordinate, z0 is the n*m dimension matrix, which represents the value of the node
B, X0,y0 requires one for the row vector one for the column vector
C, "Conds" and one-dimensional same, the general default
D, x, y are interpolated point coordinates, Z is function value
2. Demo
clear,clc% Sample Point information x=100:100:500;y=100:100:400;z=[636 697 624 478 698 712 478 420 680 674 598 412 the 662 626 552 334 310];% Input sample Point information Pp=csape ({x,y},z '); % Note the vector xi=100:10:500;yi=100:10:400;cz1=fnval (Pp,{xi,yi}) corresponding to the column of the z Matrix; Cz2=interp2 (X,y,z,xi,yi ', ' spline '); [I,j]=find (Cz1==max (Max (CZ1))) subplot (1,2,1); Surf (XI,YI,CZ1 '); shading interp; % Insert color interpolation axis equal;title (' cz1 '); subplot (1,2,2); surf (xi,yi,cz2); shading Interp;axis equal;title (' cz2 ');
Second, the interpolation node is a scattered node
1. Definition
MATLAB provides a function
Zi=griddata (X,y,z,xi,yi)
A, x, Y, Z are n-dimensional vectors, which are data points
B, Xi,yi is a given grid point horizontal ordinate (interpolation point), return the value of Zi
2. Demo
clear,clc% Sample Point information x=[129,140,103.5,88,185.5 195 157.5 107.5 162 Bayi 162 117.5];y=[7.5 141.5 147 22.5 137.5 85.5 -6.5 -81 3 56.5 -66.5 -33.5];z=-[4 8 6 8 6 8 8 9 9 8 8 9 4 9];xi=75:200;yi=-50:150;zi=griddata (x,y,z,xi,yi ', ' cubic '); subplot (1,2,1);p lot (x, y, ' * '); Title (' xy '); subplot (1,2,2); mesh (xi,yi,zi); shading Interp;axis equal;title (' xyz ');
Modeling Algorithms (eight)-interpolation and fitting