load('lin_regression.mat')
ms = -1:0.5:5;
ns = -10:1:10;

error_surf = zeros(length(ms), length(ns));
gradient_m = zeros(size(error_surf));
gradient_n = zeros(size(error_surf));

for i = 1:length(ms)
    for j = 1:length(ns)
        error_surf(i,j) = lsqError([ms(i), ns(j)], x, y);
        grad = lsqGradient([ms(i), ns(j)], x, y);
        gradient_m(i,j) = grad(1);
        gradient_n(i,j) = grad(2);
    end
end

figure()
hold on
[N, M] = meshgrid(ns, ms);
%surface(M,N, error_surf, 'FaceAlpha', 0.5);
contour(M,N, error_surf, 50);
quiver(M,N, gradient_m, gradient_n)
xlabel('Slope m')
ylabel('Intercept b')
zlabel('Mean squared error')
set(gcf, 'paperunits', 'centimeters', 'papersize', [15, 10.5], ... 
    'paperposition', [0., 0., 15, 10.5])
saveas(gcf, 'error_gradient', 'pdf')
