matlab练习程序(拟合优度)

拟合优度可以确定回归曲线对原始数据的拟合程度,用$R^{2}$表示,最大值为1,值越大拟合程度越好。

公式如下:

其中:

回归平方和SSR(regression sum of squares)公式:

总体平方和SST(total sum of squares)公式:

残差平方和SSE(error sum of squares)公式:

公式中$y_{i}$表示待拟合数据, $\bar{y}$表示待拟合数据均值,$\widehat{y}$表示拟合数据。

matlab代码如下:

clear all;close all;clc;

x = (0:0.01:2*pi)';
y = sin(x);

B = y;
A = [ones(length(x),1) x x.^2 x.^3];
X = inv(A'*A)*A'*B;
newy = A*X;

SST = sum((y-mean(y)).^2);
SSR = sum((newy-mean(y)).^2);
SSE = sum((y-newy).^2);

R1 = SSR/SST
R2 = 1-(SSE/SST)

plot(x,y,'b');
hold on;
plot(x,newy,'r');
axis equal;

结果如下:

拟合优度为0.9911。

posted @ 2023-03-04 12:58  Dsp Tian  阅读(771)  评论(0编辑  收藏  举报