%Demonstrate least squares fit
%Custom function for points:
% (1,0) (3,1) (4,2)
%

x = [1 3 4];
y = [0 1 2];

plot(x,y,'*','LineWidth',3);
axis([0 5 -1 3]);
grid on;

[X,Y] = meshgrid(-1:0.1:1);
%The explicit form of the error function that needs to be minimized
ff = @(a,b)( 26.*a.^2+3.*b.^2-22.*a-6.*b+16.*a.*b+5);
%the cellarray of the partial derivative functions
df = {@(a)(52.*a(1)-22+16.*a(2)), @(a)(6.*a(2)-6+16.*a(1))};

[opt,seq] = gradDesc2([-1,1],df);

v = zeros(1,size(seq,2));
for i=1:size(seq,2)
    v(i) = ff(seq(1,i),seq(2,i));
end

Z = ff(X,Y);
figure;
surf(X,Y,Z);
hold on;
plot3(seq(1,:),seq(2,:),v,'r');
hold off;

figure;
plot(x,y,'*','LineWidth',3);
hold on;
base = 0:0.1:5;
plot(base,polyval(opt,base),'r');
plot(base,polyval(polyfit(x,y,1),base),'g');
hold off;