Gradient Descent in Linear Regression in MATLAB | m-file
%Code:
clc
clear all
close all
figure;
pause(4);
x=[1,2,4,3,5];
y=[1,3,3,2,5];
format long
m=0;
c=0;
plot(x,y,'bo','linewidth',3);
axis([0 6 0 8]);
hold on;
pause(1);
for
h=1:4
for
ua=1:length(x)
predicted=m*x+c;
s=y-predicted;
w=((sum(-2*x.*s))/length(x))*0.01;
m=m-w;
w=((sum(-2*s))/length(x))*0.01;
c=c-w;
e=m*x+c;
rs=plot(x,e,'k','linewidth',3);
axis([0 6 0 8]);
pause(0.3);
if(h~=4 || ua~=length(x))
delete(rs);
end
end
end
Join us on Telegram: https://t.me/matlabirawen Join us on Facebook Group: https://www.facebook.com/groups/matlabcodes
No comments