%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%   Least Squares fit of line to data.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  Given: p data points (xi_i,eta_i).
%
%  Required: best line eta = a0 + a1*xi
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Data
xi  = [ 1    2     3     4     5    ]';
eta = [ 4.08 4.95  5.01  5.97  6.04 ]';

stem(xi,eta,'ro')
axis([0,6,3,8])
hold on
pause

y = eta;

H = [   1   1 ;
        1   2 ;
        1   3 ;
        1   4 ;
        1   5   ] ;

%% Solve the normal equations.
x = inv(H'*H) * H'*y;

a0 = x(1);
a1 = x(2);

xi1 = 0;
eta1 = a0 + a1*xi1;
xi2 = 6;
eta2 = a0 + a1*xi2;
plot([xi1,xi2],[eta1,eta2])
hold off

%% Compare to higher order fits.
hold on; pause; clear;
Fitpara2points

hold on; pause; clear;
Fitcubi2points

hold on; pause; clear;
Fitquar2points

return
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
