% Linear regression with arbitrary points, universal program, easy to add extra points % Both theta0 and theta1 are updated simultaneously. clear clc x = [1 2 3]; y = [1 2 3]; myData = [x; y]'; theta0 = -2 : 0.1 : 5; theta1 = -2 : 0.1 : 5; Jtheta01 = zeros(length(theta1)); for i1 = 1 : length(theta0) for i2 = 1 : length(theta1) temp = 0; for j = 1 : length(x) temp = temp + (theta0(i1) + theta1(i2) * myData(j,1) - myData(j,2))^2; end Jtheta01(i1,i2) = (1/(2*length(x))) * temp; end end % [X, Y] = meshgrid(theta0, theta1); % mesh(X,Y,Jtheta01) % q=inf; % % 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