function Numstabex()
% Read pgs 65-67, Experiment 2 and 3 from Lecture 9. The code below is
% for Experiment 2. It shows the difference in numerical stability
% between the classical and modified Gram-Schmidt algorithms. 
[U,X] = qr(randn(80));
[V,X] = qr(randn(80));
S = diag(2.^(-1:-1:-80));
A = U*S*V;
[QC,RC] = cgs(A); % Classical Gram-Schmidt.
[QM, RM] = mgs(A); % modified Gram-Schmidt
[QH,RH] = qr(A); % Matlabs qr algorithm which uses Householder
figure;
semilogy(diag(RC),'o'); 
hold on;
semilogy(diag(RM),'x'); 
semilogy(abs(diag(RH)),'+'); 
plot([0 80], [eps eps],'--');
text(10,eps/10,'\epsilon_{machine}','fontsize', 14);
plot([0 80], [sqrt(eps) sqrt(eps)],'--');
text(10,sqrt(eps)/10,'\surd\epsilon_{machine}','fontsize', 14);
plot(2.^(0:-1:-80));
axis([0 80 10^-25 1]);
legend('CGS','MGS','HH','Location','Best')


