Interpolation and Fitting - Fitting

1. The introduction of fitting

Fitting: given the measurement data (xi, yi), it is required to construct the function y=φ(x), so that the distance between y=φ(x) and these data points is the smallest in a certain sense.
Usually, assuming that the function structure of φ(x) is known, in order to obtain a specific function expression, only a few parameters need to be determined.
Least squares fitting: When determining parameters, the sum of squared errors is required to be the smallest, that is
insert image description here

Second, the solution of the fitting function

least squares fitting problem

In scientific research and engineering practice, it is often necessary to find the functional relationship y=f(x) between the dependent variable y and the independent variable x from a set of experimental (or observation) data (xi,yi). Since the observed data is often not accurate enough, it is not required that y=f(x) go through all points (xi,yi), but only that the error ei=f(xi)-yi be minimized according to a certain criterion. Usually, with
∑ i = 1 n \sum_{i=1}^ni=1nei e_iei2 = ∑ i = 1 n \sum_{i=1}^n i=1n[ f( x i x_i xi) - y i y_i yi] 2
as a measure of error, this is the so-called method of least squares.

Example
insert image description here
As you can see, we need to obtain the fitting curve of the above data, and we have two ways to obtain it.
Method 1:
We can solve the contradictory equations
xi x_ixi2 c(1) + x i x_i xi c(2) + c(3) = y i y_i yi, (i = 1,...,6)
can be written in the form of A c = b (similar to interpolation)
and solve the least squares solution of the equation system to get the coefficient c of polynomial fitting.
Method 2:
Use polynomial fitting function—polyfit

Use the format: a=polyfit(x,y,n)

Function: Use n degree polynomial to perform least squares fitting on given data points.
Parameter description: x, y are the coordinate vectors of the given data points, n is the degree of the polynomial, a is the coefficient vector of the polynomial (arranged in descending power, the length is n+1)

code show as below:

x=[0.5:0.5:3]';
y=[1.75, 2.45, 3.81, 4.80, 8.00, 8.60]';

%% 方法一:通过求解矛盾方程组
% A=zeros(6,3);
% for i =1:6
%     A(i,1)=x(i)^2;
%     A(i,2)=x(i);
%     A(i,3)=1;
% end
% b=y;
% c=A\b;
%% 方法二:利用ployfit
c=polyfit(x,y,2);
%% 画多项式曲线
%方法一:用plot
%x0=0.5:0.05:3;
%y0=c(1)*x0.^2+c(2)*x0+c(3);
%plot(x0,y0,'b-',x0,y0,'r+');
%legend('拟合曲线','数据点','Location','NorthWest')
%方法二:用fplot
f=@(x)polyval(c,x);
fplot(f,[0.5, 3]);
hold on
plot(x,y,'r+');

The fitted curve:
insert image description here
insert image description here
the method is similar to the example above, so I won’t say more.
Method 1:

x=[19 25 31 38 44]';
y=[19.0 32.3 49.0 73.3 98.8]';

%构造系数矩阵
% A=zeros(5, 2);
% for i = 1:5
%     A(i, 1)=1;
%     A(i, 2)=x(i)^2;
% end
%或者
A=[ones(5,1), x.^2];
 b=y;
 c=A\b;
 a=c(1);
 b=c(2);
 
 %画拟合曲线
 f=@(x)a+b*x.^2;
fplot(f,[19, 44]);
hold on
plot(x, y ,'r+');

e=f(x)-y;
MSE=mean(e.^2)
MAE=mean(abs(e))

Method Two:

x=[19 25 31 38 44]';
X=x.^2;
y=[19.0 32.3 49.0 73.3 98.8]';

%用y=a+b*x^2 对数据点进行最小二乘法拟合
%令X=x^2
%则 y=a+b*X

c=polyfit(X, y, 1);
%y=c(1)*X+c(2)
a=c(2);
b=c(1);

%画拟合曲线
 f=@(x)a+b*x.^2;
fplot(f,[19, 44]);
hold on
plot(x, y ,'r+');

The curves fitted by the two methods are similar, and the fitting effect is better
insert image description here

3. General nonlinear least squares fitting

Some nonlinear least squares problems can be transformed into linear least squares problems for solution

For example

x=[-5 -4 -3 -2 -1 0 1 2 3 4 5];
y=[0.11 0.34 0.73 1.27 1.81 2.00 1.79 1.30 0.73 0.35 0.14];
用y=a*exp(-x2/b2)

x=[-5  -4  -3 -2 -1 0 1 2 3 4 5];
y=[0.11    0.34    0.73   1.27  1.81    2.00    1.79    1.30    0.73    0.35  0.14];
figure
plot(x,y,'r+'); %在图一中画出数据点的分布

%y=a*exp(-x^2/b^2)
%log(y)=a-x^2/b^2
%令X=x^2, Y=log(y)
%则,Y=c(1)*X+c(2),
%其中,c(1)=-1/b^2, c(2)=log(a)
X=x.^2;
Y=log(y);
figure(2)
plot(X,Y,'m+');  %图二中画出X,Y的分布

c=polyfit(X,Y,1);
a=exp(c(2));
bb=-1/c(1);
b=sqrt(bb);

figure(1)
hold on
f=@(x)a*exp(-x.^2/b^2); %拟合得到的曲线
fplot(f,[-5, 5]);

The resulting image is as shown below
insert image description here
insert image description here

For those cases that cannot be transformed into a linear least squares problem,
the functions can be used to solve it - lsqnonlin and lsqcurvefit

lsqnonlin
is used to solve the nonlinear least squares problem
sum {FUN(a).^2}
where a is the parameter (vector) to be found, and FUN(a) is the residual vector.
Use format:
a) a= LSQNONLIN(FUN,a0);
b) a = LSQNONLIN(FUN,a0,LB,UB);

lsqcurvefit
is used to solve the nonlinear least squares fitting problem
sum {[FUN(a,xdata)-ydata].^2}
where a is the parameter (vector) to be sought, and xdata and ydata are the data points to be fitted (observed values ​​of the independent variable x and the dependent variable y), FUN(a,x) is a function that calculates the value of the dependent variable y according to the fitting formula when the parameter a and the independent variable x are given.
Format:
1) X = LSQCURVEFIT(FUN,X0,XDATA,YDATA)
2) X = LSQCURVEFIT(FUN,X0,XDATA,YDATA,LB,UB)

By the way, there is another function fit

Commonly used format:
FO = fit(X,Y,fittype)
FO = fit(X,Y,fittype, 'StartPoint', P0 )
function: use the model specified by fittype to fit the data X, Y to a curve or surface, and get is a fit object.
X must be a matrix with 1 column (curve fitting) or 2 columns (surface fitting), and Y must be a column vector with the same number of rows in X and Y. P0 is the initial value (vector) of the parameter to be sought.
Fittype is used to specify the type or form of the fitting function.

Just look at the examples
insert image description here

%产生数据点
x0=0:0.2:4;
y0=3*exp(-0.4*x0)+12*exp(-3.2*x0);
y0=y+randn(size(y0))*0.3;
figure(1)
plot(x0,y0,'r+');
hold on
%对这些数据点用y=a*exp(b*x)+c*exp(d*x)
%进行最小二乘拟合

%% 方法一.利用lsqnonlin
%首先定义计算在各个数据点处误差构成的向量
err_fun = @(c) c(1)*exp(c(2)*x0) + c(3)*exp(c(4)*x0)-y0;
c0=[2.5 0 0 0];
c=lsqnonlin( err_fun, c0);

f=@(x) c(1)*exp(c(2)*x) + c(3)*exp(c(4)*x);
fplot(f,[0, 10])

e=err_fun(c);
MSE=mean(e.^2)

%% 方法二. 用lsqcurvefit
fun=@(c, x) c(1)*exp(c(2)*x) + c(3)*exp(c(4)*x);
c0=[2.5 0 0 0];
c=lsqcurvefit( fun, c0, x0, y0);

figure(2)
plot(x0, y0, 'r+');
hold on
f=@(x) fun(c, x);
fplot(f, [0, 4])

%% 方法三. 利用fit
f_type=fittype('a*exp(b*x)+c*exp(d*x)',...
    'independent', 'x', ...
    'coefficients', {
    
    'a', 'b', 'c', 'd'} );

fo=fit(x0', y0', f_type, 'StartPoint',[2,0,0,0]);

figure(3)
fun=@(x)f(x)
fplot(fun,[0,4])
hold on
plot(x0, y0,'r+')


Images obtained by three methods:
insert image description here

insert image description here
insert image description here

4. The simplest fitting method

After reading the above examples and function solving methods, we were surprised to find that what is this, we can’t understand it at all.
However, we will also be surprised to find that there is a shortcut to curve fitting. Enter the cftool command
in the console or click APP—Curve Fitting to enter the curve fitting tool in Matlab, select the x and y variables to be fitted, and select the type of function to be fitted to perform the fitting analysis directly. ginkgo.

insert image description here

Guess you like

Origin blog.csdn.net/qq_49288154/article/details/122370239