% Linear regression with arbitrary points, universal program, easy to add extra % points clear clc x = [1.5 1 2.5 3 4.5 5 2 6]; y = [2 1 2 3 3.2 5 2 4 ]; myData = [x; y]'; theta0 = 0; theta1 = -1 : 0.1 : 3; Jtheta1 = zeros(1, length(theta1)); % Jtheta1=0; for i = 1 : length(theta1) % for theta1 = -1 : 0.1 : 3 temp = 0; for j = 1 : length(x) % temp = temp + (theta0 + theta1*myData(j,1)-myData(j,2))^2; temp = temp + (theta0 + theta1(i) * myData(j,1) - myData(j,2))^2; end Jtheta1(i) = (1/(2*length(x))) * temp; % Jtheta1(end+1) = (1/(2*length(x))) * temp; end % find line [p q] = min(Jtheta1); k = theta1(q); x1 = min(x) : 0.1 : max(x); y1 = k * x1; subplot(1,2,1), plot(x,y,'s', 'markerSize',12, 'markerFaceColor','b'), ... xlabel('x'), ylabel('y'), axis([min(x)-0.2 max(x)+0.2 min(y)-0.2 max(y)+0.2]), grid on, hold on, ... plot(x1,y1,'LineWidth',3) subplot(1,2,2), plot(theta1, Jtheta1,'LineWidth',3), xlabel('theta 1'), ylabel('J(theta 1)'), grid on