clear
echo on
%This script demonstrates the use
%of qr factorization with pivoting.
%First, we consider the matrix A:
a=[1 2 3;4 5 6;7 8 9;10 11 12]
pause
%If we do a qr factorization on this
%matrix, we obtain:
[q r]=qr(a)
pause
%Note that the matrix a was not of full
%rank because the last diagonal element
%of r is zero.  Also note that it is
%not -quite- zero due to round off error.
%We may look at its value:
format short e
s=r(3,3)
format short
%which is about what we would expect for
%single precision.  Now let's look at the
%matrix discussed in class.
pause
%We have:
n=7;
a=eye(n);
for i=1:(n-1)
 for j=i+1:n
  a(i,j)=-1;
echo off
 end
end
echo on
%which produces:
a
pause
%We can do a qr factorization of the matrix:
[q r]=qr(a)
%Note that the factorization didn't really do
%anything this time, because r was already in
%upper triangular form.
pause
%We can learn alot about the matrix (and how
%close to singularity the matrix is) if we
%also do pivoting, however:
[q r pt]=qr(a);
%This time we get:
r
pause
%Note that the last diagonal element of r is
%now a couple of orders of magnitude smaller
%than the other diagonal elements.  This is because
%The matrix A is close to singularity.  This
%decomposition is a Q R P decomposition, although
%matlab returns P' rather than P.
pause
%We can show this:
q*r*pt'
%which is the original matrix a, at least to machine
%precision.
echo off