<>Matlab How to fit the curve

Matlab There are many ways to fit a curve , Cubic spline interpolation , linear interpolation , Polynomial fitting and so on . Polynomial fitting because the function is derived from f ( x ) = a n x n + a n − 1
x n − 1 + . . + a 1 x + a 0 f(x)=a_nx^n+a_{n-1}x^{n-1}+...+a_1x+a_0f(x)=an​xn+
an−1​xn−1+...+a1​x+a0​ form , If the least square method is used to fit , For parameters a n , a n − 1 , . . , a 1 , a 0
a_n,a_{n-1},...a_1,a_0an​,an−1​,...a1​,a0​ Come on , Equations f n ( x ) = 0 f_n(x)=0 fn​(x)
=0
It's a system of linear equations , It can be used Matlab The method of finding inverse matrix , The least square solution of the equation is obtained . But if the system of equations formed by parameters is not a linear system , It can not be obtained by matrix method . It is possible to use spline interpolation and linear interpolation , But we can't get the expression we need , At this time, the nonlinear fitting method is the most appropriate .

usually , We all have a hypothesis about the model before the experiment , For example, this is an exponential decay curve , Or the curve of exponential decay oscillation , Or a periodic oscillation signal composed of trigonometric functions of several frequencies . In this case, we only need to specify the parameters to be estimated , It can be solved by substituting data . Here is a typical example .

<> Step interpretation

The data in this example is a step input given to an inertial system , The output of the system is collected , And identify the system .
(xdata,ydata) It is the acquisition data of step response of a first-order system ,ydata Is the output value ,xdata It's a timestamp . Because the system is step response , We assume that the transfer function of the system is K T
p s + 1 \frac{K}{T_ps+1}Tp​s+1K​, Obviously, the two parameters that need to be identified are K and T p T_p Tp​.
The expression of the starting point of the system under step response input is c ( t ) = K ( 1 − e t T P ) c(t)=K(1-\rm e^\frac{t}{T_P}) c
(t)=K(1−eTP​t​), Therefore, we need to establish the function fun as follows
fun=@(xdata,ydata)(x(1)*(1-exp(-xdata/x(2))))

Is a function with specified parameters , The parameters we need to solve are x(1) and x(2), among x The return value is a binary parameter vector , It can be called directly fun Function evaluation y The calculated value of identification system generated by time stamp . And compared with the experimental value ydata Draw on a picture for comparison .
clc close all plot(xdata,ydata);xlim([0,1]);hold on;% Actual curve drawing fun=@(x,xdata)(x(1)
*(1-exp(-xdata/x(2))));% Estimation function x0=[1500,0.025];% Initial estimate [x(1),x(2)] x=lsqcurvefit(fun
,x0,xdata,ydata);% Nonlinear function fitting y=fun(x,xdata);% Substitute the estimated value , The function points are obtained plot(xdata,y);xlim([0,1]
);% Draw estimated curve title(['[K,Tp]=',num2str(x)]);% Label estimated parameters
The estimated curve drawn is as follows ：( The blue one is the experimental data , The red one is the fitting curve )

We can find out , If you follow the general trend of the experimental curve , The fitted exponential approximation curve is shown by the red line , It can be seen that the parameters identified are more accurate .

Technology