Social Science Xiaobai's Mathematical Modeling Study Notes-Matlab Data Fitting

	 在数学建模中,数据拟合是非常基础的一个操作,在人文社科研究中,所做的实证工作大部分是要得到两个或多个变量之间的变动关系,包括方向和程度。因此,学习数据拟合是简单的,带来的回报却是丰厚的。
	 数据关系可以分为线性的和非线性的,操作方法分为直接借助matlab自带的curve fitting tool和自己编写程序,但无论是哪一种方法,我都建议先绘制散点图看一下数据分布情况,给数据类型下一个初步的判断。
	 matlab自带的curve fitting tool非常便捷,我使用的2010年版功能没有2016年版本的强大,但胜在体积小,不会太占内存。如果对这个工具箱是刚需的话,还是建议安装2016版本的。因此,下面有些图片是我截图2010的,有些是网上copy下来2016的。操作方式都是输入变量矩阵之后,输入cftool回车,调出工具箱页面。

Insert picture description here
Then set x and y in data, and then go to fitting to find a suitable function. Mine is obviously linear regression, so I chose polynomiel, and then plot the image.
Insert picture description here
I saw that the toolbox page of the 2016 version of matlab on the Internet also has options such as result. The interface is very clear. Steal a picture and put it here.
The picture address is MATLAB using scatter points for function curve fitting-Xiaobai_Effort-CSDN Blog https://blog.csdn.net/laobai1015/article/details/77537145
There are many other modes in the toolbox, all of which can be selected from fitting, so let's develop it slowly.
Personally, I prefer to type code, so I don't care what the name of my function is, as long as I know how to express it. The three commonly used functions are polyfit, fittype, and inline. In fact, the coding is similar.
polyfit is suitable for linear least squares fitting, and the scope of application is relatively small. Let's put an example:
>> x=[2,3,5,7,9];

y=[4,6,8,10,12];
a=polyfit(x,y,2)

a =

-0.0436 1.5741 1.2799

y=polyval(a,x)

y =

4.2538    5.6098    8.0603   10.1618   11.9144
第三条里面的2是指x^2,同理你也可以写3进行三阶拟合。2阶拟合的结果是y=a+bx+cx^2,三阶同理。
哎我的字怎么变小了?
fittype和inline的使用范围更加广泛,写法大同小异,放两个例子:

x=[1;1.5;2;2.5;3];y=[0.9;1.7;2.2;2.6;3];
p=fittype(‘poly1’)
f=fit(x,y,p)

p =

 Linear model Poly1:
 p(p1,p2,x) = p1*x + p2

f =

 Linear model Poly1:
 f(x) = p1*x + p2
 Coefficients (with 95% confidence bounds):
   p1 =        1.02  (0.7192, 1.321)
   p2 =        0.04  (-0.5981, 0.6781)	
   下面是inline的
     x=[3.2,3.6,3.8,4,4.2,4.5,4.8,5,5.3,5.4,5.6,5.8,6,6.2,6.4,6.6,6.9,7.1]';

y = [0.38,0.66,1,0.77,0.5,0.33,0.66,0.83,0.33,1,0.33,0.5,0.33,0.71,0.71,1,0.87,0.83] ';

y=[0.38,0.66,1,0.77,0.5,0.33,0.66,0.83,0.33,1,0.33,0.5,0.33,0.71,0.71,1,0.87,0.83]’;
myfunc = inline(‘1./(beta(1)+beta(2).*exp(-x))’,‘beta’,‘x’);

beta0 = [0.2,0.2] ';

beta = nlinfit(x,y,myfunc,beta0)

beta =

1.4591
8.0539
   
   本人是初学者,欢迎指教批评,互相学习,谢谢

Guess you like

Origin blog.csdn.net/vivian233/article/details/90521921