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

M = [ones(length(x),1) x'];
pred = (M*a)';
J = [J 0.5 * meansq(pred - y)];
%while (abs(J(length(J)) - J(length(J) - 1)) / (abs(J(length(J) - 1)) +
%        abs(J(length(J)))))> threshold
for i = 1:1000
    grada1 = (1/length(x)) * sum(pred - y);
    grada2 = (1/length(x)) * sum((pred - y).*x);
    a = [a(1) - grada1 * lrate; a(2) - grada2 * lrate];
    pred = (M*a)';
    J = [J 0.5 * meansq(pred - y)];
end
a
