Fitting Algorithms of Mathematical Modeling Algorithms

 Select Linear Fit

The reason is simple, and after making a sketch, it is found that the linear correlation is high

clear;clc

load data.mat

n = size(year,2);
k = (n*sum(year.*po)-sum(po)*sum(year))/(n*sum(year.*year)-sum(year)*sum(year));
b = (sum(year.*year)*sum(po)-sum(year)*sum(year.*po))/(n*sum(year.*year)-sum(year)*sum(year));

f = @(year) k*year + b;
fplot(f,[2009,2018]);

hold on % 继续在之前的图形上来画图形

plot(year,po,'o');

po_hat = k*year+b; % y的拟合值
SSR = sum((po_hat-mean(po)).^2);  % 回归平方和
SSE = sum((po_hat-po).^2); % 误差平方和
SST = sum((po-mean(po)).^2); % 总体平方和
SST-SSE-SSR;  % 5.6843e-14  =   5.6843*10^-14   matlab浮点数计算的一个误差
R_2 = SSR / SST;

 The result of the code implementation is as shown in the figure below

Using matlab fitting toolbox to get

 

Guess you like

Origin blog.csdn.net/zkx0214/article/details/127776449