% linear regression with 3 points % Not universal, just to see the method clear clc x = [1 2 3]; y = [1 2 3]; myData = [x; y]'; theta0 = 0; theta1 = -1 : 0.1 : 3; Jtheta1 = zeros(1, 41); for i = 1 : 41 temp = 0; for j = 1 : 3 temp = temp + (theta0 + theta1(i) * myData(j,1) - myData(j,2))^2; end Jtheta1(i) = (1/6) * temp; end subplot(1,2,1), plot(x,y,'s', 'markerSize',12, 'markerFaceColor','b'), ... xlabel('x'), ylabel('y'), axis([0.8 3.2 0.8 3.2]), grid on, hold on, line(x,y,'LineWidth',3) subplot(1,2,2), plot(theta1, Jtheta1,'LineWidth',3), xlabel('theta 1'), ylabel('J(theta 1)'), grid on