lrate = 0.01;
J = 1;
threshold = 0.00001;
a = [0; 0];

% Data
x = 0:10;
y = 1 + 0.5*x + (-1).^x * 0.5; % Regress y = 1 + 0.5x

xcount = 1;
pred = a(1) + a(2)*x(xcount);
J = [J 0.5 * (pred - y(xcount))^2];
%while (abs(J(length(J)) - J(length(J) - 1)) / (abs(J(length(J) - 1)) +
%        abs(J(length(J)))))> threshold
for i = 1:500
    grada1 = pred - y(xcount);
    grada2 = (pred - y(xcount))*x(xcount);
    a = [a(1) - grada1 * lrate; a(2) - grada2 * lrate];
    xcount = rem(xcount + 1, length(x));
    if (xcount == 0)
        xcount = 1;
    end
    pred = a(1) + a(2)*x(xcount);
    J = [J 0.5 * (pred - y(xcount))^2];
end
a
