%%%%%%%%%%%%%%%%%%%%%%
% experiment1 (p.65) %
%%%%%%%%%%%%%%%%%%%%%%

format long g;
[U,X] = qr(randn(40));
[V,X] = qr(randn(40));


S = diag(2.^(-1:-1:-40));

A = U*S*V;

[QC, RC] = classicgs(A);
[QM, RM] = modifiedgs(A);
[Q,R] = qr(A);

dia_rc = diag(RC);
dia_rm = diag(RM);
dia_r = diag(R);

j = 1:1:40;

figure(1)
semilogy(j',2.^-j','-',j',dia_rc,'o',j',dia_rm,'x',...
    j',dia_r,'g*',j',1e-8,'--',j',1e-16,'--');
grid off;
legend('2^{-j}','Classical GS','Modified GS','Matlab QR function',3);
xlabel('j')
ylabel('r_{jj}')

checkQ = norm(Q'*Q-eye(40))
checkQM = norm(QM'*QM-eye(40))
checkQC = norm(QC'*QC-eye(40))