[Application of MATLAB in Mathematical Modeling] [a]

Started "Application of MATLAB in Mathematical Modeling" (Zhuojin Wu) study notes today. Blogger MATLAB is R2016a.

1.1 read and write data
to interact 1.1.1 Excel and MATLAB (slightly)

1.1.2 interactive notepad and MATLAB

  1. Reading the
    load ( 'filename. ***')
    when only the data Notepad, Load may be employed, the variable data stored in a matrix form in the filename.
    [A, B, C, ......] = textread ( 'filename', 'the format', N)
    the ABC variable name for each column of data to be saved; format to read format; N is the number of reads.
  2. Write
    save file obj1 obj2 ...... the variables are stored in the file obj1 obj2 ...... file (.mat format) fid = fopen ( 'out.txt' , 'wt'); fprintf (fid, 'format', obj) ;
    the obj saved out. txt in

1.2 polynomial fitting

  1. polyfit (X, Y, N) ; polynomial fitting, polynomial coefficients in descending return arrangement.
    polyval (P, xi); calcd polynomial;
    X-, the Y data points, P is the coefficient returned. xi is the abscissa of the point calculation required.
  2. Fitting the graphics window
    ① first draw data points
    Tools-Basic Fitting ② Click the graphics window, and set according to the figure.
x=[1,2,3,4,5,6,7,8,9];
y=[9,7,6,3,-1,2,5,7,20];
plot(x,y,'r*')

Here Insert Picture Description

Respectively, using a second order, third order fit the data, a third order polynomial best.

3. Specify function fitting

syms t
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.071;0.059;0.08;0.032;-0.015;-0.02];
plot(x,y,'r*')

Run the above program we can use the image function found
Here Insert Picture Description
fitting.

syms t
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.071;0.059;0.08;0.032;-0.015;-0.02];
f=fittype('a*cos(k*t)*exp(w*t)','independent','t','coefficients',{'a','k','w'});
cfun=fit(x,y,f)%显示拟合函数
xi=0:.1:20;
yi=cfun(xi);
plot(x,y,'r*',xi,yi,'b-')

fittype ( 'custom functions', 'Independent', 'argument', 'coefficients', { 'coefficient' '......'});
Run the above procedures we can obtain custom function coefficients.

Curve Fitting Toolbox
can open Curve Fitting Tool input command window cftool

Here Insert Picture Description
1.3 Data fitting Application examples
1.3.1 Population Forecast

T=1:1:30;
Y=[33815,33981,34004,34165,34212,34327,34344,34458,34498,34476, ...
    34483,34488,34513,34497,34511,34520,34507,34509,34521,34513, ...
  34515,34517,34519,34519,34521,34521,34523,34525,34525,34527];
%30年的人口数据
plot(T,Y,'r*')

As the program runs. Population was found to change with time of a nonlinear process, and there is a parallel to the asymptote abscissa axis. So we were fitted with logistics curve model.

The basic form of logistics curve is
Here Insert Picture Description
replaced by the following equation, it can be converted into a linear model
Here Insert Picture Description

T=1:1:30;
Y=[33815,33981,34004,34165,34212,34327,34344,34458,34498,34476, ...
    34483,34488,34513,34497,34511,34520,34507,34509,34521,34513, ...
  34515,34517,34519,34519,34521,34521,34523,34525,34525,34527];
%30年的人口数据
for t=1:30
    x(t)=exp(-t);
    y(t)=1/Y(t);
end
c=zeros(30,1)+1;
X=[c,x'];
B=inv(X'*X)*X'*y'%回归系数B
for i=1:30
    z(i)=B(1,1)+B(2,1)*x(i);%回归拟合值
    s(i)=y(i)-sum(y)/30;%离差
    w(i)=z(i)-y(i);%误差
end
S=s*s';%离差平方和
Q=w*w';%误差平方和
U=S-Q;%回归平方和
F=28*U/Q%计算并输出F检验值
%回归模型的拟合值
for j=1:10
    Y(j)=1/(B(1,1)+B(2,1)*exp(-j));
end
plot(T,Y)%输出logistics曲线

Published 22 original articles · won praise 3 · Views 1830

Guess you like

Origin blog.csdn.net/qq_42825058/article/details/98661024