MATLAB-based least squares fitting and fitting toolbox tutorial (with complete code and algorithm)

1. Least squares curve fitting

Given a set of data (x_i,y_i),\quad i=1,2,\cdots,Nsatisfying a certain function model \hat y(x)=f(a,x), where a is the undetermined coefficient vector.

Then, the goal of least squares curve fitting is to find the value of a set of undetermined coefficients so that the following expressions are minimized:

J=min\sum_{i=1}^N[y_i-\hat y(x_i)]^2=min\sum_{i=1}^N[y_i-f(a,x_i)]^2

The format in MATLAB is as follows:

[a,jm]=lsqcurvefit(Fun,a0,x,y)

%Fun原型函数的MATLAB表示
%a0为最优化的初值
%x,y为原始输入输出的数据向量
%a为返回的待定系数向量
%jm为此待定系数下的目标函数的值

Example 1

A set of data is generated by the following MATLAB code:

x=0:.1:10;
y=0.12*exp(-0.213*x)+0.54*exp(-0.17*x).*sin(1.23*x);

The set of data satisfies y(x), and the undetermined coefficients are calculated a_ito minimize the value of the objective function.

y(x)=a_1e^{-a_2x}+a_3e^{-a_4x}sin(a_5x)

untie:

The MATLAB code is as follows:


clc;clear;


x=0:0.1:10;
y=0.12*exp(-0.213*x)+0.54*exp(-0.17*x).*sin(1.23*x);
f=inline('a(1)*exp(-a(2)*X)+a(3)*exp(-a(4)*X).*sin(a(5)*X)','a','X');

ff=optimset;
ff.TolFun=1e-20;ff.TolX=1e-15; %修改精度限制

[xx,res]=lsqcurvefit(f,[1 1 1 1 1],x,y,[],[],ff)

%绘制曲线
x1=0:0.01:10;
y1=f(xx,x1); %代入运算
plot(x1,y1,x,y,'o');
legend('拟合曲线','原数据点')

operation result:

xx = 0.120000000000000   0.213000000000000   0.540000000000000   0.170000000000000   1.230000000000000


res =0

Example 2

It is known that the following data may be satisfied y(x)=ax+bx^2e^{-cx}+d. Find the values ​​of the least squares solution a,b,c,d that satisfy the data.

 untie:

Let a_1=a,a_2=b,a_3=c,a_4=dthe original function be written as follows:

y(x)=a_1x+a_2x^2e^{-a_3x}+a_4

The MATLAB code is as follows:

clc;clear;

%输入已知参数
x=0.1:0.1:1;
y=[2.3201,2.6470,2.9707,3.2885,3.6008,3.9090,4.2147,4.5191,4.8232,5.1275];

f=inline('a(1)*X+a(2)*X.^2.*exp(-a(3)*X)+a(4)','a','X');
a=lsqcurvefit(f,[1 2 2 3],x,y)

%绘制曲线
y1=f(a,x);
plot(x,y1,x,y,'o');
legend('拟合曲线','原数据点');

operation result:

a = 3.100076146500888   1.502655931856580   4.004634473891176   2.000013871079962

In MATLAB, the nonlinear fitting command format is as follows:

[beta,r,J]=nlinfit(x,y,fun,beta0)
%x,y为原始数据
%fun是在M文件中定义的函数
%beta0是函数中参数的初始值
%beta为参数的最优值
%r是各点处的拟合残差
%J为雅克比矩阵的数值

The initial parameter beta0 can be obtained by the following method:

First observe how many undetermined coefficients there are, and then put several sets of experimental data into the equation to get the initial value of the parameter.

Example 3

Given the following data, use a nonlinear method to find the parameters of the function f(x).

f(x)=b_1(1-b_2e^{-b_3x})

x 0 47 93 140 186 279 372 465 558 651
y 18.98 27.35 34.86 38.52 38.44 37.73 38.43 43.87 42.77 46.22

untie:

The MATLAB code is as follows:

clc;clear;

x=[0 47 93 140 186 279 372 465 558 651];
y=[18.98 27.35 34.86 38.52 38.44 37.73 38.43 43.87 42.77 46.22];
b0=[43 0.6 0.1]; %初始参数值
fun=inline('b(1)*(1-b(2)*exp(-b(3)*X))','b','X');
[b,r,j]=nlinfit(x,y,fun,b0); %b为最佳参数
b
R=sum(r.^2)  %误差平方和
y1=fun(b,x);
plot(x,y,'*',x,y1,'-or')

operation result:


b =42.664037023023802   0.548346447450711   0.009880232273427


R =46.197464448828995

2. Curve fitting tool

The following will use an example to understand the curve fitting tool in MATLAB.

First type the following code in the newly created script file edit box:

clc;clear;

%产生数据
x=-20:2:20;
y=-20:2:20;
[X,Y]=meshgrid(x,y);
Z=3*X.^3-4*X+2*Y.^4+3*Y^3;
meshgrid(X,Y,Z);

first step

Open the curve fitting tool in the APP option box and select the data source X/Y/Z data

second step

Choose an appropriate fitting method. The surface to be fitted in this example is a polynomial, so it can be selected directly. Other optional fitting types are as follows:

  • Custom Equation: user-defined function type
  • Interpolant: interpolation approximation, there are four types, linear, nearest, neighbor, cubic spline, shape-preserving
  • Lowess: smooth approximation, locally weighted regression
  • Polynomial: polynomial approximation, there are 9 types, linear, quadratic, cubic, 4-9th degree

third step

Choose the appropriate polynomial degree and coefficients

the fourth step

The fitting result information is in the Results text box in the Fitting dialog box, here is the main statistical information of this fitting, including:

  • The sum of squares of error SSE: This parameter calculates the sum of squares of errors between the regression value after fitting the parameters and the corresponding point of the original data, analog variance
  • Coefficient of determination R-square
  • Adjusted exact coefficient Adjusted R-square
  • RMSE: This parameter is the square root of the mean of the sum of the squares of the corresponding point errors of the predicted data and the original data, that is, the mean square error, the analog standard deviation

Two more functions are added:

Generate code : File>>Generate Code>>Automatically generate a creatFit.m file;

Generate a picture: File>>Print to figure

 

 

Guess you like

Origin blog.csdn.net/forest_LL/article/details/124459202