clear 
close all
load('lin_regression.mat')

position = [-2. 10.];
gradient = [];
errors = [];
count = 1;
eps = 0.01;

while isempty(gradient) || norm(gradient) > 0.1
    gradient = lsqGradient(position, x,y);
    errors(count) = lsqError(position, x, y);
    position = position - eps .* gradient; 
    count = count + 1;
end
figure()
subplot(2,1,1)
hold on
scatter(x,y, 'displayname', 'data')
xaxis = min(x):0.01:max(x);
f_x = position(1).*xaxis + position(2);
plot(xaxis, f_x, 'displayname', 'fit')
xlabel('Input')
ylabel('Output')
grid on
legend show
subplot(2,1,2)
plot(errors)
xlabel('optimization steps')
ylabel('error')