1 %% Machine Learining----Linear Regression 2 close all 3 clear 4 5 %%data load 6 Year = linspace(0,13,14); 7 Price = [2,2.5,2.9,3.147,4.515,4.903,5.365,5.704,6.853,7.791,8.561,10,11.28,12.9]; 8 9 %%train data 10 alpha = 0.01; 11 theta = [1,8]; 12 obj_old = 1e10; 13 tor = 1e-2; 14 15 tic; 16 for time = 1:1000 17 delta = zeros(1,2); 18 objective = 0; 19 hypothesis = theta(1) + theta(2) * Year; 20 drawnow; 21 subplot(2,1,1); 22 plot(Year,Price,'rx',Year,hypothesis); 23 for i = 1:14 24 delta = (theta(1) + theta(2) * Year(i) - Price(i)) * [1,Year(i)] + delta; 25 objective = (theta(1) + theta(2) * Year(i) - Price(i)) * (theta(1) + theta(2) * Year(i) - Price(i))+ objective; 26 end 27 theta = theta - alpha * delta / 14; 28 29 fprintf('objective is %.4f ', objective); 30 if abs(obj_old - objective) < tor 31 fprintf('torlerance is samller than %.4f ', tor); 32 break; 33 end 34 obj_old = objective; 35 subplot(2,1,2); 36 plot(toc,obj_old); 37 axis([0 20 0 800]) %xmin是x最小,xmax是x最大,ymin,ymax类似 38 hold on; 39 end