clear all load fisheriris NumObs = size(meas,1); NameObs = strcat({'Obs'},num2str((1:NumObs)','%-d')); iris = dataset({nominal(species),'species'},... {meas,'SL','SW','PL','PW'},... 'ObsNames',NameObs); x=iris.SL; y=iris.PL; [x Ind]=sortrows(x); y=y(Ind); m=length(x); subplot(3,1,1) plot(x,y,'.') hold on grid on epochMax=1000; alpha=0.05; iterCount=0; % Parameters: theta0(1)=rand; theta1(1)=rand; % Hypothesis: h=theta0(1)+theta1(1).*x; plot(x,h,'r') for thetaX=0:100 for thetaY=0:100 hContour=((thetaX-50)./50)+((thetaY-50)./50).*x; JContour(thetaX+1,thetaY+1)=(1/m).*sum((hContour-y).^2); end end subplot(3,1,3) contour([-1:0.02:1],[-1:0.02:1],JContour) hold on axis([-1 1 -1 1]) % Cost Function: J(1)=(1/(2*m))*sum((h-y).^2); % Minimize Cost Function (Gradient Descent): for iterCount=1:epochMax theta0(iterCount+1)=theta0(iterCount)-alpha*(1/m)*sum(h-y); theta1(iterCount+1)=theta1(iterCount)-alpha*(1/m)*sum((h-y).*x); h=theta0(iterCount+1)+theta1(iterCount+1).*x; J(iterCount+1)=(1/(2*m))*sum((h-y).^2); subplot(3,1,1) plot(x,y,'.') hold on grid on plot(sort(x),h,'r') hold off subplot(3,1,2) plot(0:iterCount,J) axis([0 iterCount+1 0.5*mean(J) J(1)+0.2]) hold on grid on subplot(3,1,3) plot(theta0,theta1) hold on pause(0.1) end