当前位置:网站首页>[matlab] curve fitting
[matlab] curve fitting
2022-06-25 17:30:00 【No card, no change】
Curve fitting
The difference between interpolation and fitting :
- Implementation method : Interpolation requires the curve to pass through the sample points , The fitting does not need to pass through the sample points , Only the minimum overall error is required .
- The form of the result : Interpolation is a piecewise approximation of sample points , There is no same approximation function ; Function fitting uses a function to approximate , There is a complete expression .
- Focus on : Interpolation can be used to estimate the function values corresponding to some points in the interval ; Fitting can not only estimate the points in the interval , You can also predict points outside the interval .
- applications : Interpolation is often used for precise data sets ; Fitting is often used in statistical data sets .
polyfit Function for Univariate multiple curve fitting ( Also polynomial fitting ), Form like : y = a x 5 + b x 4 + c x 3 + d x 2 + e x + f y=ax^5+bx^4+cx^3+dx^2+ex+f y=ax5+bx4+cx3+dx2+ex+f, We know x x x、 y y y Sample data , Use this function to solve the parameter ( coefficient ) a a a、 b b b、 c c c、 d d d、 e e e、 f f f.
regress Function for Univariate or multivariate linear regression ( fitting ), Form like : y = a x 1 + b x 2 + c x 3 + d x 4 + e y=ax_1+bx_2+cx_3+dx_4+e y=ax1+bx2+cx3+dx4+e, We know x 1 x_1 x1、 x 2 x_2 x2、 x 3 x_3 x3、 x 4 x_4 x4、 y y y Sample data , Use this function to solve the parameter ( coefficient ) a a a、 b b b、 c c c、 d d d、 e e e.
polyfit function
Invocation format :
[P,S,mu]=polyfit(X,Y,m)
[P,S]=polyfit(X,Y,m)
P=polyfit(X,Y,m)
Parameter interpretation :
According to the sample data X and Y, Produce a m Polynomial coefficient vector P And its error data at the sampling point S,mu It's a binary vector ,mu(1) yes mean(X), and mu(2) yes std(X).
x=0: 0.1: 1;
y=[-0.447, 1.978, 3.11, 5.25, 5.02, 4.66, 4.01, 4.58, 3.45, 5, 35];
p = polyfit(x, y, 3) % Cubic polynomial fitting
xx = 0: 0.01 : 1;
yy = polyval(p, xx) ; % According to the coefficient vector p Calculated at xx The function value at point
plot(xx, yy, '-b', x, y, 'markersize', 20)
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-HfdDYeiK-1655004670298)(C:\Users\23343\AppData\Roaming\Typora\typora-user-images\image-20210905121325482.png)]](/img/3f/b0116ceeeb02c64b5aa6adcce12b3a.png)
When you still need to Linear regression analysis when , Can be reused corrcoef Function to obtain the correlation coefficient .
regress function
stay matlab Of regress Confidence interval in function bint、rint as well as stats The last three values are all infinite , This shows that the data does not obey the linear relationship , Nonlinear fitting function shall be considered for fitting .
Invocation format :
- [B,BINT,R,RINT,STATS] = regress(Y,X)
- [B,BINT,R,RINT] = regress(Y,X)
- [B,BINT,R] = regress(Y,X)
- [B,BINT] = regress(Y,X)
- B = regress(Y,X)
Parameter interpretation :
Call this function to ensure that the form of the object function is known , For example, the objective function is : y = a x 1 + b x 2 + c x 3 + d x 4 + e y=ax_1+bx_2+cx_3+dx_4+e y=ax1+bx2+cx3+dx4+e, Or is it y = a x 1 2 + b x 2 2 + c x 1 + d x 2 + e x 1 × x 2 + f y = ax_1^2+bx_2^2+cx_1+dx_2+ex_1×x_2+f y=ax12+bx22+cx1+dx2+ex1×x2+f Such multinomial polynomials . Pass in the parameter , Call this function to get the fitting coefficient value .
- x It's a matrix . Every sample of the matrix , That is, the number of rows equals the number of samples , Each column of each sample represents the value of each term of the sample in the polynomial excluding the coefficient .
- y It's a column vector . The vector dimension is consistent with the number of rows of the matrix , Same as the number of samples , Each value represents the function value corresponding to each sample .
This function is based on the... Of multiple samples x i x_i xi and y y y Fit the coefficients of the polynomial .
Return value interpretation :
- B: Regression coefficient , I.e. unknown parameter ,B(1) Constant term ,B(2~……) In turn X Each column ( From the second column ) The coefficient of the corresponding term ;
- BINT: Confidence intervals for regression coefficients .( confidence interval : When an estimate is given 95% The confidence interval is [a, b] when , It can be understood that we have 95% It can be said that the average value of the sample is between a To b Between , The probability of error is 5%)
- R: residual ( Residual refers to the observed value and predicted value ( Fit value ) The difference between , That is, the difference between the actual observed value and the regression estimated value )
- RINT: The confidence interval of the residuals .
- STATS: Statistics used to test the regression model . Yes 4 A numerical : Determination factor R 2 R^2 R2( Statistics to measure goodness of fit ,R² The closer the value of 1, It shows that the better the fitting degree of the regression line to the observed value ), F F F Statistics Observations , Tested p p p Value ( p < 0.05 p<0.05 p<0.05 The regression model is established ), Estimation of error variance .
x1=[3.91 6.67 5.33 5.56 6.12 7.92 5.82 5.5 5.59 6.12 6.68 6.93]';
x2=[9.43 14.5 15.8 19.8 17.4 23.8 31.6 37.1 36.4 32.2 36.6 41.3]';
X=[ones(12,1), x1, x2];
Y=[280 338 405 432 452 582 596 602 606 621 629 656]';
[b,bint,r,rint,stats] = regress(Y,X)
rcoplot(r,rint) % Draw a residual diagram
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-1Y5opgw0-1655004670300)(C:\Users\23343\AppData\Roaming\Typora\typora-user-images\image-20210905133501687.png)]](/img/b8/b7104706942a1d69b1d80fe787ee9b.png)
Residual diagram : The circle in the residual graph is the actual residual of each data point , The horizontal interval is the residual confidence interval , The confidence interval crosses the origin, indicating that the equation fits well ; Does not pass through the origin , It can be regarded as an outlier , For example, the figure above shows that the second group of data does not pass through the origin , Therefore, the fitting results of the second group of data are poor .
After that, the second group of data can be removed and fitted again , More accurate results .
%% Objective function :y=Ax1^2+Bx2^2+Cx1+Dx2+Ex1*x2+F ( This is a quadratic function , Two variables , Capital letters are constants )
format long;
y=[7613.51 7850.91 8381.86 9142.81 10813.6 8631.43 8124.94 9429.79 10230.81 10163.61 9737.56 8561.06 7781.82 7110.97]';
x1=[7666 7704 8148 8571 8679 7704 6471 5870 5289 3815 3335 2927 2758 2591]';
x2=[16.22 16.85 17.93 17.28 17.23 17 19 18.22 16.3 13.37 11.62 10.36 9.83 9.25]';
X=[ones(size(y)) x1.^2 x2.^2 x1 x2 x1.*x2]; % structure X matrix !!!
[b,bint,r,rint,stats] = regress(y,X)
give the result as follows :
b =
1.0e+04 *
-1.353935450267780
0.000000089381408
-0.005811190715467
-0.000605427789545
0.479983626458515
-0.000037869040292
bint =
1.0e+04 *
-2.621944842897225 -0.085926057638335
0.000000034253753 0.000000144509063
-0.027588831662544 0.015966450231609
-0.001309493882546 0.000098638303455
0.119564693553897 0.840402559363132
-0.000105954336341 0.000030216255756
r =
1.0e+02 *
-4.397667358984126
-2.361417286008764
-1.434643909138249
-5.904203974279353
7.511701773844997
5.570806699070599
-2.447861341816779
0.494622057474844
6.376995507987613
-6.789520765534544
2.744335484633611
1.578124015815701
-0.803533566911865
-0.137737336155596
rint =
1.0e+03 *
-1.219619853471144 0.340086381674319
-1.426253867770768 0.953970410569015
-0.919089416302223 0.632160634474573
-1.568776909577359 0.387936114721488
0.111430783412043 1.390909571356956
-0.533006860832905 1.647168200647025
-1.168898277755904 0.679326009392548
-0.977546779130818 1.076471190625786
-0.398546999643731 1.673946101241254
-1.439044988776064 0.081140835669156
-0.919162439561023 1.468029536487746
-1.088788822632291 1.404413625795431
-1.271236485719865 1.110529772337492
-1.017369947314269 0.989822480083149
stats =
1.0e+05 *
0.000008444011951 0.000086828553270 0.000000043344434 3.162249735298930
Parameters X The first column of is all 1 Column vector , Starting from the second column, it is the column vector composed of the sample data corresponding to each coefficient corresponding term , The objective function of this problem is a bivariate quadratic , because regress Functions can only solve linear problems , So we will x 1 2 x_1^2 x12 As the first item , x 2 2 x_2^2 x22 As the second item , x 1 x_1 x1 As the third item , x 2 x_2 x2 As the fourth item , x 1 x 2 x_1x_2 x1x2 As the fifth item , This is equivalent to a five variable objective function .
b Is the corresponding parameter b(1) by F( The last constant term ) ,b(2) by A( The first parameter ),b(3) by B,b(4) by C,b(4) by D,b(5) by E.
bint by b Of 95% confidence interval .
stats The third parameter of is F Tested p value ,p Small values (p<0.05), It shows that the fitting model is effective .
application
The relationship between the known randomness parameter and the diversity measure and the convergence measure is shown in the following table , Diversity is as important as convergence , Q. choose the equilibrium point of random parameters .
surface 1 The relationship between the randomness parameter and the diversity measure
| x | 0.03 | 0.06 | 0.09 | 0.12 | 0.15 | 0.18 | 0.21 | 0.24 | 0.27 | 0.3 |
| y1 | 0.01 | 0.01 | 0.02 | 0.03 | 0.06 | 0.07 | 0.13 | 0.17 | 0.25 | 0.37 |
surface 2 The relationship between the randomness parameter and the convergence measure
| x | 0.03 | 0.06 | 0.09 | 0.12 | 0.15 | 0.18 | 0.21 | 0.24 | 0.27 | 0.3 |
| y1 | 0.85 | 0.76 | 0.68 | 0.62 | 0.54 | 0.52 | 0.5 | 0.49 | 0.48 | 0.47 |
x=0.03:0.03:0.3;
y1=[0.01,0.01,0.02,0.03,0.06,0.07,0.13,0.17,0.25,0.37];
y2=[0.85,0.76,0.68,0.62,0.56,0.52,0.49,0.46,0.43,0.39];
plot(x,y1,'*',x,y2,'o');
legend(' diversity ',' The convergence ')
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-aJvIw8H0-1655004670300)(C:\Users\23343\AppData\Roaming\Typora\typora-user-images\image-20210905151252383.png)]](/img/f1/8c0a110f154792764be9727112ec5b.png)
Problem analysis :
The increase of random parameter leads to the increase of diversity , Reduced convergence ; Both are equally important , Then take the equilibrium point ; The best position of the equilibrium point is where the diversity and convergence are equal .
Solution :
First step : The diversity and convergence are fitted respectively , Get the fitting curve .
The second step : Find the intersection of the two curves .
p1=polyfit(x,y1,2);
p2=polyfit(x,y2,2);
p=p1-p2;
xi=roots(p); % -1.14148334023966 0.316172995412073
xj=0:0.03:0.36;
yj1=polyval(p1,xj);
yj2=polyval(p2,xj);
yi=polyval(p1,xi(2))
plot(x,y1,'*',x,y2,'o',xj,yj1,xj,yj2,xi(2),yi,'rp');
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-XHuzPPTI-1655004670301)(C:\Users\23343\AppData\Roaming\Typora\typora-user-images\image-20210905170755125.png)]](/img/f0/d88b550f7ecdd82ca326eed66d0382.png)
lsqlin function
Because this function is used less , There is less relevant information on the Internet , Therefore, the following ideas are subjective .
Linear least squares solver with boundary or linear constraints .
Solve the least squares curve fitting problem in the following form :

See , In fact, the official ones are not detailed , Poor sample
Here I will give the simplest example :
%% y=ax^2+bxsinx+cx^3
xdata = [3.6,7.7,9.3,4.1,8.6,2.8,1.3,7.9,10.0,5.4];
ydata = [16,150.5,260.1,22.5,206.5,9.9,2.7,165.5,325.0,54.5];
C =[ xdata'.^2, xdata'.*sin(xdata'),xdata'.^3]; d = ydata';
[x, resnorm, residual] = lsqlin(C, d);
%% mapping
xi = 0:0.1:11;
f = @(c, x) c(1).*x.^2 + c(2).*x.*sin(x) + c(3).*x.^3;
plot(xdata, ydata, '*', xi, f(x, xi))
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-iTZuCS8E-1655004670301)(C:\Users\23343\AppData\Roaming\Typora\typora-user-images\image-20210905193249034.png)]](/img/d1/67609abc2a42993baae77339415601.png)
The disadvantages of functions : When the upper boundary (ub) Or the lower boundary (lb) or Aeq or beq The restriction in is when the non polynomial term is , These restriction matrices are difficult to construct . such as , The code above will x 2 x^2 x2、 x s i n x xsinx xsinx、 x 3 x^3 x3 Regard each as the first 、 Two 、 Three items , Therefore, the constraint matrix must also be the constraint on these three terms , But if the title only gives the right x x x What about the limitations of ? If the title only gives the right x e x l g x xe^xlgx xexlgx What about the limitations of ? It is difficult to translate into constraints for each term .
I don't know how to deal with ” Some terms have upper and lower bounds , Other terms have no upper and lower bounds “ The situation of .
Little is known about this function , And there is too little relevant content on the Internet .
lsqcurvefit function
The least square method is used to solve the nonlinear fitting problem !!!
Invocation format :
x = lsqcurvefit(fun,x0,xdata,ydata)
x = lsqcurvefit(fun,x0,xdata,ydata,lb,ub)
x = lsqcurvefit(fun,x0,xdata,ydata,lb,ub,options)
[x,resnorm,residual,exitflag] = lsqcurvefit(…)
Parameter interpretation :
- x0 Is the parameter to be found ( Initial solution vector ), The selection of initial variables affects the final solution
- xdata,ydata Is the data used for fitting
- lb、ub Is the lower and upper bounds of the solution vector lb≤x≤ub, If no boundary is specified , be lb=[ ],ub=[ ]
- fun Is the function to be fitted , Calculation x Fitting function value at , There are generally two types of parameters , One is the parameter of the objective function , One is the unknown of the objective function
Return value interpretation :
- x Is the parameter vector to be solved
- resnorm=sum ((fun(x,xdata)-ydata).^2), That is to say x Sum of squares of residuals at
- residual=fun(x,xdata)-ydata, That is to say x Residual at
- exitflag Conditions for terminating iterations
%% y=tcos(kx)e^(wx)
x=[0,0.4,1.2,2,2.8,3.6,4.4,5.2,6,7.2,8,9.2,10.4,11.6,12.4,13.6,14.4,15]';
y=[1,0.85,0.29,-0.27,-0.53,-0.4,-0.12,0.17,0.28,0.15,-0.03,-0.15,-0.07,0.059,0.08,0.032,-0.015,-0.02]';
f= @(c,x) c(1)*cos(c(2)*x).*exp(c(3)*x);
c0= [0 0 0];
[c, fval]= lsqcurvefit(f, c0, x, y);
xx=0:0.1:20;
yy=f(c, xx);
plot(x, y, 'r*', xx, yy, 'b-');
disp(c);
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-Qb3dbz25-1655004670301)(C:\Users\23343\AppData\Roaming\Typora\typora-user-images\image-20210905200611657.png)]](/img/8d/c8105bf52421ff0b65382f6094d8e7.png)
Be careful : The functions defined here are quite special , The parameters to be fitted should also be transferred to the function as parameters of the function , The purpose of treating the parameter to be fitted as a vector input function is to make the form of the function more concise .
xdata = [3.6,7.7,9.3,4.1,8.6,2.8,1.3,7.9,10.0,5.4];
ydata = [16,150.5,260.1,22.5,206.5,9.9,2.7,165.5,325.0,54.5];
c0=[ 0 0 0];
[email protected](c, x) c(1)*x.^2 + c(2)*x.*sin(x) + c(3)*x.^3;
[c, resnorm, r]=lsqcurvefit(f_h, c0, xdata, ydata);
%% mapping
xx=0:0.1:11;
yy=f_h(c, xx);
plot(xdata, ydata, 'r*', xx, yy, 'b-');
disp(c);
![[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-EYS2XXhz-1655004670302)(C:\Users\23343\AppData\Roaming\Typora\typora-user-images\image-20210905201055326.png)]](/img/f1/b3e011faaf69062ab23f9721d425bb.png)
fittype Functions and fit function
Self study , Use less , Baidu can be used directly when necessary ( For those who play games ).
边栏推荐
- 软考中级和高级选哪个比较好?
- 启牛蜻蜓点金下载是可以开户吗?开户安全吗
- How does social e-commerce operate and promote?
- DDD concept is complex and difficult to understand. How to design code implementation model in practice?
- Babbitt yuan universe daily recommendation: three players holding "tens of millions" of collections have revealed the "three routines" of the digital collection market
- 组件通讯的方式有哪些
- TCP聊天+传输文件服务器服务器套接字v2.8 - 修复已知程序4个问题
- WPF development essays Collection - ECG curve drawing
- Snakeyaml profile parser
- Redis 的PSYNC命令
猜你喜欢

Learn Tai Chi Maker - mqtt (III) connect to mqtt server

College Students' hot summer exchange, Rog star product phantom 16 flipped version / phantom 13 / phantom x appointment

【编译原理】词法分析

旧手机变废为宝,充当服务器使用

Babbitt yuan universe daily recommendation: three players holding "tens of millions" of collections have revealed the "three routines" of the digital collection market

What are the steps for launching the mobile ERP system? It's important to keep it tight

ddia数据密集型应用系统设计 整理

学习太极创客 — MQTT(一)MQTT 是什么

Design and arrangement of DDIA data intensive application system

杰理之唤醒口使用注意事项【篇】
随机推荐
杰理之定时器捕获(timer_cap.c)使用注意事项【篇】
Jerry's ADC_ get_ Incorrect voltage value obtained by voltage function [chapter]
Sword finger offer II 012 The sum of left and right subarrays is equal
The role of the project manager in the project
Design and arrangement of DDIA data intensive application system
杰理之如何给外界输出一个时钟源使用【篇】
SnakeYAML配置文件解析器
2022云的世界会更好吗
How high does UART baud rate require for clock accuracy?
【编译原理】概述
Best practices for data relocation: using CDM to relocate offline Mysql to DWS
剑指 Offer II 035. 最小时间差
软考中的嵌入式系统设计师为什么考的人少?
【UVM实战 ===> Episode_2 】~ VIP、VIP的开发、VIP的发布
STM32 hardware error hardfault_ Handler processing method
远程终端控制神器——MobaXterm
中断操作:AbortController学习笔记
[UVM practice== > episode_1] ~ MCDF design update, AMBA standard interface, UVM verification environment update
Why does MySQL limit affect performance?
服务器四通道内存性能提升,四通道内存性能怎么样