Learn the principles and applications of mathematical modeling [Basic principles and programming implementation of interpolation and fitting]

main content

Interpolation and fitting is a basic data analysis method in mathematical modeling. It is recognized as one of the commonly used algorithms in modeling. Learned
interpolation problems, interpolation principles, Runge phenomenon of high-order interpolation, one-dimensional and two-dimensional in Matlab Interpolation command
, fitting problem, fitting principle and steps, and then introduces the fitting calculation of Matlab.

interpolation

Insert image description here
Insert image description here
Insert image description here
The above problem can be reduced to "knowing the value of the function at several points in a certain interval (domain), find the value of the function at other points in the interval (domain)", this kind of problem is suitable to be solved by interpolation method.
The one-dimensional interpolation problem can be described as: Knowing the values ​​Yo, Y1,..., Yn of the function at Xo, x1,···,xn, finding a simple function p(x), making p(xi) = yi usually Taking p(x) as a polynomial
can be proved by Vandermonde determinant and Cramer's rule:
the polynomial with values ​​y0, y1, yn at x0, x1, xn exists and is unique, that is, the solution to the interpolation problem only exists.
The commonly used interpolation methods are Lagrange interpolation method and Newton interpolation method.

Lagrangian interpolation

The Lagrange interpolation formula (the foreign name Lagrange interpolation formula refers to an interpolation polynomial in which the node basis functions are given on the nodes, and then the linear combination of the basis functions is made, and the combination coefficient is the node function value.
Insert image description here

Runge phenomenon of high-order interpolation (Runge phenomenon)

The higher the degree of the interpolation polynomial, the higher the interpolation accuracy. The conclusion is only valid when the degree of the interpolation polynomial does not exceed seven; when the degree of the interpolation polynomial exceeds seven, the interpolation polynomial will have a serious oscillation phenomenon, which is called the Runge phenomenon.
Insert image description here
In practice more than seven interpolations should not be used.
A common method to avoid the Runge phenomenon is: divide the interpolation interval into several small intervals, and use low-order (quadratic, cubic) interpolation in the small intervals, that is, segmented low-order interpolation, such as spline function interpolation.
Insert image description here

MATLAB interpolation

1. One-dimensional interpolation

The one-dimensional interpolation command is interp1, and its basic format is yi= interp1(x,y,xi, 'method').
x,y are known points, xi,yi are interpolated points and interpolation results, x,y and xi ,yi is usually a vector;
method' indicates the interpolation method:
nearest'-nearest neighbor interpolation
'linear'-linear interpolation
spline'-cubic spline interpolation,
'cubic' a cubic interpolation,
the default is linear interpolation.

Cubic spline interpolation programming application

x=0:2:24;
y=[12 9 9 10 18 24 28 27 25 20 18 15 13];
x1=13;%插值点在13处
yl=interp1(x,y,x1,'spline')%求出在该点的值
xi=0:1/3600:24;
yi=interp1(x,y,xi, 'spline');
plot(x,y,'*',xi,yi)


Insert image description here
The graph drawn for the value at x=13
Insert image description here

Comparison of Three Interpolation Methods

First create a Lagrange interpolation function and name it lagrange.m The
function code is as follows

function y=lagrange(x0,y0,x)
n=length(x0);
m=length(x);
for i=1: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;
en

Create a program% Lagrangian interpolation, comparison of linear interpolation and cubic spline interpolation

x0=[0 3 5 7 9 11 12 13 14 15 ];
y0=[0 1.2 1.7 2.0 2.1 2.0 1.8 1.2 1.0 1.6];
x=0:0.1:15;
y1=lagrange(x0,y0,x);%使用拉格朗日插值法
y2=interp1(x0,y0,x);%使用一维插值命令的线性插值法
y3=interp1(x0,y0,x,'spline');%使用一维插值命令的三次样条插值法
subplot(3,1,1);
plot(x0,y0,'k+',x,y1,'r');
grid;%生成网格点
title('lagrange');
subplot(3,1,2);
plot(x0,y0,'k+',x,y2,'r');
grid;
title('piecewise linear');
subplot(3,1,3);
plot(x0,y0,'k+',x,y3,'r');
grid;
title('spline');

Running results
Insert image description here
The results show that the cubic spline interpolation method is better here

  • The program uses three interpolation methods of Lagrange, piecewise linear and cubic spline in turn to calculate, and the Lagrange high-order interpolation obviously shows the Runge phenomenon;
  • Because Matlab does not have the Lagrange high-order interpolation function, the high-order interpolation function lagrange is written separately in the program, and then called.

2. Two-dimensional interpolation

The two-dimensional interpolation command is interp2, and the basic format is zi=interp2(x,y,z,xi,yi,'method'). The use of two-dimensional interpolation commands is more complicated. x,y,z are known interpolation points, and z can
be It is understood as the value of the interpolated function at (x, y); xi, yi are the interpolated points, and zi is the output interpolation result, which can be understood as the value of the interpolated function at (xi, yi); x, y is a vector , xi, yi are vectors or matrices, while z and zi are matrices.
'method' indicates the interpolation method:
'nearest' - nearest neighbor interpolation,
'linear' - bilinear interpolation,
'spline' - bicubic spline interpolation,
'cubic' bicubic interpolation,
default bilinear interpolation.
program practice

x=1:5;
y=1:3;%表示三行五列的数据,x在此表示列数,y在次表示行数
temps=[82 81 80 82 84;79 63 61 65 81;84 84 82 85 86];
figure(1);
mesh(x,y,temps);
xi=1:0.2:5;
yi=1:0.2:3;
zi=interp2(x,y,temps,xi,yi','cubic');
figure(2);
mesh(xi,yi,zi);
figure(3);
contour(xi,yi,zi,20,'r');%画出20条等高线
[i,j]=find(zi==min(min(zi)));
%==表示判断或者寻找位置,
% min(zi)寻找每一列最小值,min(min(zi))在得到的最小值中继续寻找最小值
x=xi(j),y=yi(i),zmin=zi(i,j)
[i,j]=find(zi==max(max(zi)));
x=xi(j),y=yi(i),zmax=zi(i,j)

Running results
Insert image description here
Insert image description here
Insert image description here
Insert image description here
(1) xi in interp2 is a row vector, and yi is a column vector. In fact, the rows and columns of xi and yi can be different.
(2) plot3 (space curve),
mesh (space surface)
surf (space surface),
contour (contour) are commonly used commands in 3D drawing.
The difference between mesh and surf is that mesh draws a surface grid map, which is simply connected with a grid. Surf draws a curved surface map.
The function of contour(x,y,z,n) is to make n contour lines of the surface interpolated from the points (x,y,z).
Use meshc and surfc to draw contour lines under the surface.
Meshz and surfz draw vertical curtains
. If the figure is removed, only the last image will be drawn. The function is somewhat similar to the hold on
Insert image description here
Insert image description here
program.

x=0:400:5600;
y=0:400:4800;
z=[370 470 550 600 670 690 670 620 580 450 400 300 100 150 250;...
    510 620 730 800 850 870 850 780 720 650 500 200 300 350 320;...
    650 760 880 970 1020 1050 1020 830 900 700 300 500 550 480 350;...
    740 880 1080 1130 1250 1280 1230 1040 900 500 700 780 750 650 550;...
    830 980 1180 1320 1450 1420 1400 1300 700 900 850 840 380 780 750;...
    880 1060 1230 1390 1500 1500 1400 900 1100 1060 950 870 900 930 950;...
    910 1090 1270 1500 1200 1100 1350 1450 1200 1150 1010 880 1000 1050 1100;...
    950 1190 1370 1500 1200 1100 1550 1600 1550 1380 1070 900 1050 1150 1200;...
    1430 1430 1460 1500 1550 1600 1550 1600 1600 1600 1550 1500 1500 1550 1550;...
    1420 1430 1450 1480 1500 1550 1510 1430 1300 1200 980 850 750 550 500;...
    1380 1410 1430 1450 1470 1320 1280 1200 1080 940 780 620 460 370 350;...
    1370 1390 1410 1430 1440 1140 1110 1050 950 820 690 540 380 300 210;...
    1350 1370 1390 1400 1410 960 940 880 800 690 570 430 290 210 1501];
figure(1);
meshz(x,y,z);%带帷幕的网格曲面图
xlabel('X'),ylabel('Y'),zlabel('Z');
[xi,yi]=meshgrid(0:50:5600,0:50:4800);%基于向量 x 和 y 中包含的坐标返回二维网格坐标
figure(2);
z1i=interp2(x,y,z,xi,yi,'nearest');%最邻近插值法
surfc(xi,yi,z1i);%画垂帘图
xlabel('X'),ylabel('Y'),zlabel('Z');
figure(3);
z2i=interp2(x,y,z,xi,yi);%默认双线性插值法
surfc(xi,yi,z2i);
xlabel('X'),ylabel('Y'),zlabel('Z');
figure(4);
z3i=interp2(x,y,z,xi,yi,'cubic');%双立方插值法
surfc(xi,yi,z3i);
xlabel('X'),ylabel('Y'),zlabel('Z');
figure(5);
subplot(1,3,1),contour(xi,yi,z1i,10,'r');
subplot(1,3,2),contour(xi,yi,z2i,10,'r');
subplot(1,3,3),contour(xi,yi,z3i,10,'r');%画出三个图的等高线

The program uses "[xi,yi]=meshgrid(0:50:5600,0:50:4800);" to generate grid points (xi,yi), which is equivalent to "xi=0:50:5600;yi=0 :50:4800;" But the xi and yi generated by meshgrid(x,y) are same-dimensional matrices. The rows of xi are all x, and the columns of yi are all y.
Running results:
Insert image description here
nearest neighbor interpolation,
Insert image description here
bilinear interpolation,
Insert image description here
bicubic interpolation
Insert image description here
Insert image description here

Scattered point interpolation

The interpolation points (x, y) of the interpolation problem discussed earlier are all grid points. When (x,y) are scattered points, the griddata(x,y,z,xi,yi,'method') command can be used to perform two-dimensional interpolation.

Example

Insert image description here
Insert image description here
program

x=[129 140 103.5 88 185.5 195 105 157.5 107.5 77 81 162 162 117.5];
y=[7.5 141.5 23 147 22.5 137.5 85.5 -6.5 -81 3 56.5 -66.5 84 -33.5];
z=[-4 -8 -6 -8 -6 -8 -8 -9 -9 -8 -8 -9 -4 -9];
[xi,yi]=meshgrid(75:0.5:200,-70:0.5:150);%基于向量 x 和 y 中包含的坐标返回二维网格坐标
zi=griddata(x,y,z,xi,yi,'cubic');%对二维或三维散点数据插值,双立方插值法
figure(1);
meshz(xi,yi,zi);%带帷幕的网格曲面图
xlabel('X'),ylabel('Y'),zlabel('Z');
figure(2);
contour(xi,yi,zi,[-5 -5], 'b');%画出高度-5的等高线
grid;%显示或隐藏坐标区网格线
hold on;
plot(x,y,'+');
xlabel('X'),ylabel('Y');

operation result
Insert image description here
Insert image description here

fitting problem

example

Insert image description here
Insert image description here
The first question in Example 1 is a typical interpolation problem, but the second question is not suitable to use the interpolation method, because 13.5 has exceeded the given data range, and using the interpolation function to extrapolate the data outside the interpolation interval will produce a large error. Use Matlab to calculate the
height Results of subinterpolation, spline interpolation and extrapolation

Runge phenomenon occurs in high-order interpolation, and huge errors occur in the extrapolated data when t>12.
The first graph of spline interpolation shows that spline interpolation can solve the first problem well, but the second graph shows that using spline interpolation to extrapolate the data when t>12 will also produce large errors.
In summary, the interpolation method is not suitable for the second question.
A common method to solve the second question is to find the approximate functional relationship f(t) between temperature and time based on the temperature data between 1 and 12 points, and infer the temperature at t =13.5 from f(t). This is based on discrete The problem of finding an approximate functional relationship between data is called a curve fitting problem.
The difference between a fitting problem and an interpolation problem is:
(1) The interpolation function passes through a known point , but the fitting function does not necessarily pass through a known point;
(2) Interpolation It is mainly used to find function values , and the main purpose of fitting is to find functional relationships for further analysis such as prediction.
Of course, some specific problems can use both interpolation and fitting.
Example 2 is also a fitting problem

2. Calculation of fit

(1) Selection of line type:
(2) Calculation of parameters in line type. The choice of line type is the key and difficult point in fitting calculations. The line type is usually determined mainly based on professional knowledge and scatter plots. The least squares method can be used to calculate parameters in linear fitting, while the Gauss-Newton iteration method should be used to calculate parameters in nonlinear fitting.

3. Matlab fitting

Matlab polynomial fitting command format is:
[a,S]=polyfit(x,y,n) where x and y are the independent variable and dependent variable of the data to be fitted; n is the degree of the fitted polynomial; a is the fitted A vector composed of polynomial coefficients; S is the indicator (can be omitted) required to analyze the fitting effect.
The code displays the fitting of a set of data.

%拟合
x=1:12;
y=[5, 8, 9, 15,25, 29, 31, 30, 22, 25, 27, 24];
a=polyfit(x,y,9);%进行9次多项式拟合
xp=1:0.1:12;
yp=polyval(a,xp);%计算多项式 a 在 xp 的每个点处的值。
% 参数 a 是长度为 n+1 的向量,其元素是 n 次多项式的系数(降幂排序)
plot(x,y,'.k',xp,yp,'r');

The results are as follows
Insert image description here
(2) Nonlinear fitting
Matlab nonlinear fitting command format is: [b,r]=polyfit(x,y,fun,b0,option)
where x and y are the independent variables of the data to be fitted and Dependent variable; fun is the fitting function; b0 is the initial iteration value of the fitting parameter; option is the fitting option; b is the fitting parameter; r is the fitting residual.
Use Example 2 for MATLAB practice

x=1:16;
y=[4.00 6.40 8.00 8.80 9.22 9.50 9.70 9.86 10.00 10.20 10.32 10.42 10.50 10.55 10.58 10.60];
y1=@(b,t)b(1)*exp(-t/b(2))+b(3)*exp(-t/b(4))+b(5);%此关系式可以写的更简单一些,作用只是确定拟合函数的类型
b0=[-1 1 -1 1 1];%初值
a=nlinfit(x,y,y1,b0);%nlinfit非线性回归拟合,此处等效polyfit。
xp=1:0.1:16;
yp=y1(a,xp);%带插点的坐标
plot(x,y,'.k',xp,yp,'r');

Fitting using MIatlab programming has many inconveniences
(1) Programming is required. For example, anonymous functions are used in the program in Example 2, which is not easy for beginners.
(2) The fitting results are incomplete. Matlab fitting commands generally only provide basic results such as fitting coefficients. Sometimes additional calculations are needed to obtain statistics that represent the goodness of fit.
(3) Iterative convergence of nonlinear fitting cannot be guaranteed. Since the initial iteration parameters of nonlinear fitting need to be determined manually, there is no guarantee that the iteration will converge.
So you can use the MATLAB toolbox cftool (curve fitter)

cftool (curve fitter)

Insert image description here
Insert image description here
Use the cftool toolbox to fit the data in Example 2.
Insert image description here
Insert image description here
Output under seventh-order polynomial
Insert image description here
fitting. After the fitting is completed, the fitting results will be evaluated.
SSE-root mean square error/residual sum of squares error
R-square-correlation coefficient
RMSE -Residual standard deviation
generally looks at SSE and RMSE, which should not exceed 0.1.
Select Export in the upper right corner to export the code.
This toolbox works great.

Guess you like

Origin blog.csdn.net/Luohuasheng_/article/details/128589612
Recommended