代码托管在github
https://github.com/sofiathefirst/matlabdemo/tree/master/02Linear_regressin_demo
x=[-2:0.1:2];
y = 4*x;
z=normrnd(0,0.6,1,41);
y = y+z;
w = -5;
plot(x,y,'.')
%hold on
for i =[1:1000]
d=w*x*x' - y*x';
if(abs(d)<1)
break;
end
w = w-0.004*d
plot(x,y,'.',x,w*x)
pause(0.1)
end