% Example of a Least Squares method
% Peak height of mountains
% Solve the normal equations using Cholesky factorization
% Example 3.1 on p.106 of Heath
% K. Ming Leung, 04/07/03

clear all; format long g;
A = [    1 0 0
            0 1 0
            0 0 1
           -1 1 0
           -1 0 1
            0 -1 1];
b = [ 1237
        1941
        2417
        711
        1177
        475];
n=min(size(A));
AT = A.';
ATA = AT*A;
ATb = AT*b;
L=Cholesky(ATA); %L=chol(ATA);
y=forwardSubstitution(L,ATb,n);
x=backSubstitution(L.',y,n),
%x = ATA\ATb,
r = A*x-b, r2 = r.'*r, normr=sqrt(r2)
normAx=norm(A*x), normb=norm(b), cosTheta=normAx/normb,
normA=norm(A),
Ap=inv(ATA)*AT,
normAp=norm(Ap),