clear 
close all

load('lin_regression.mat')

ms = -1:0.5:5;
ns = -10:1:10;

position = [-2. 10.];
gradient = [];
error = [];
eps = 0.01;

% claculate error surface
error_surf = zeros(length(ms), length(ns));
for i = 1:length(ms)
    for j = 1:length(ns)
        error_surf(i,j) = lsqError([ms(i), ns(j)], x, y);
    end
end
figure()
hold on
[N, M] = meshgrid(ns, ms);
surface(M,N, error_surf, 'FaceAlpha', 0.5);
view(3)
xlabel('slope')
ylabel('intersection')
zlabel('error')

% do the descent

while isempty(gradient) || norm(gradient) > 0.1
    gradient = lsqGradient(position, x,y);
    error = lsqError(position, x, y);
    plot3(position(1), position(2), error, '.', 'color', 'red', 'markersize', 20)
    position = position - eps .* gradient; 
    pause(0.05)
end

grid on
set(gcf, 'paperunits', 'centimeters', 'papersize', [15, 15], ... 
    'paperposition', [0., 0., 15, 15])
saveas(gcf, 'gradient_descent', 'pdf')
