用matlab的右除实现最小二乘拟合

clear
clc
x=0:1/10:100;
y=x.^2+2*x+1;
y=y/max(y);
ye=y+0.2*rand(size(y));
ye=ye-0.1;
p=polyfit(x,ye,2);
yp=polyval(p,x);
plot(x,y);
hold on;
plot(x,ye,'r.');
plot(x,yp,'k');
grid on;
figure
plot(x,y-yp);
%%%---用matlab的右除实现
xr=ones(length(x),3);
xr(:,1)=x.^2;
xr(:,2)=x;
yl=ye';
p2=xr\yl;
yp2=polyval(p2,x);
figure
plot(x,y);
hold on;
plot(x,yp2,'r');
grid on;
figure
plot(x,y-yp2);

---------------------------------

右除可以摆脱1对1的变量关系,可以实现多个变量对一个变量,例如  y = a1*f1(x1) + a2*f2(x2) + a3*f3(x3) + a3。

你可能感兴趣的:(matlab,plot)