% CondSquaringEG3p6.m
% Condition squaring effect in linear least squares
% Example 3.6 on p.116 Heath
% K. Ming Leung, 04/15/03

clear all; format short g;
small = 0.1*sqrt(eps);
A = [ 1 1; small -small; 0 0];
E = [ 0 0; 0 0; -small small];
b1 = [ 1; 0; small];
b2 = [ 1; 0; 1];
normA = norm(A,2);
normE = norm(E,2); normE/normA,
condA = cond(A),    % cond(A) is large
[m,n] = size(A);

A1 = A+E.*rand(m,n);
AT1 = A1.';
ATA1 = AT1*A1;
ATb1 = AT1*b1;
L1 = Cholesky(ATA1); 
y1 = forwardSubstitution(L1,ATb1,n);
x1 = backSubstitution(L1.',y1,n),
r1 = A1*x1-b1, 
r12 = r1.'*r1; 
normr1=sqrt(r12),
normAx1 = norm(A1*x1); 
normb1 = norm(b1); 
cosTheta1 = normAx1/normb1,


A2 = A+E.*rand(m,n);
AT2 = A2.';
ATA2 = AT2*A2;
ATb2 = AT2*b2;
L2 = Cholesky(ATA2);
y2 = forwardSubstitution(L2,ATb2,n);
x2 = backSubstitution(L2.',y2,n),
r2 = A2*x2-b2, 
r22 = r2.'*r2;
normr2 = sqrt(r22),
normAx2 = norm(A2*x2); 
normb2 = norm(b2); 
cosTheta2 = normAx2/normb2,



