% Primer za linearnu regresiju sa opadanjem gradijenta % Postavljanje linije preko 3 tacke clear clc % podaci x = [0.5 2.3 2.9 1.5 1.8]; y = [1.4 1.9 3.2 2.1 1.7]; % inicijalna pretpostavka theta_0 = 1; theta_1 = -1; alpha = 0.25; m = size(x ,2); % broj primeraka u skupu podataka % crtanje inicijalne linije u = 0 : 0.1 : 3; v = theta_0 + theta_1 * u; plot(u,v,'LineWidth',2) axis([0 3 0 4]) hold on plot(x,y,'rs', 'MarkerFaceColor','r') text(0.5, 1.7, 'A', 'FontSize', 20) text(2.3, 2.2, 'B', 'FontSize', 20) text(2.8, 3.5, 'C', 'FontSize', 20) text(1.55, 2.2, 'D', 'FontSize', 20) text(1.8, 1.9, 'E', 'FontSize', 20) dJ_theta_0 = 1; dJ_theta_1 = 1; iterationCounter = 0; while (abs(dJ_theta_0)>0.001 || abs(dJ_theta_0)>0.001) SSR = 0; for i = 1 : m SSR = SSR + (theta_0 + theta_1 * x(i) - y(i))^2; endfor J = (1/(2*m)) * SSR; dJ_theta_0 = 0; for i = 1 : m dJ_theta_0 = dJ_theta_0 + (theta_0 + theta_1 * x(i) - y(i)); endfor dJ_theta_0 = dJ_theta_0 / m; dJ_theta_1 = 0; for i = 1 : m dJ_theta_1 = dJ_theta_1 + (theta_0 + theta_1 * x(i) - y(i))*x(i); endfor dJ_theta_1 = dJ_theta_1 / m; theta_0 = theta_0 - alpha * dJ_theta_0; theta_1 = theta_1 - alpha * dJ_theta_1; v = theta_0 + theta_1 * u; %clf %plot(x,y,'rs', 'MarkerFaceColor','r') %axis([0 3 0 4] %text(0.5, 1.7, 'A', 'FontSize', 20) %text(2.3, 2.2, 'B', 'FontSize', 20) %text(2.8, 3.5, 'C', 'FontSize', 20) %hold on plot(x,y,'rs', 'MarkerFaceColor','r') plot(u,v,'LineWidth',2) drawnow iterationCounter = iterationCounter + 1; endwhile