MATH 372 -- Numerical Linear Algebra QR-factorization example March 14, 2007 Consider the following 3 x 3 matrix >> A = [2 1 1; 3 2 0; 4 1 -2] A = 2 1 1 3 2 0 4 1 -2 We first perform the QR factorization algorithm "by hand". The first step is to find a reflector that takes the first column of A to a vector with zeroes below the first entry. We use the Corollary we proved in class that for any two unequal vectors x, y with the same 2-norm there is a reflector taking the first to the second. Here x will the be first column of A and y will be the vector [5.3852; 0; 0]. The reflector is determined by the unit vector u in the direction of v = x - y: >> norm([2; 3; 4],2) ans = 5.3852 >> v = [2; 3; 4] - [ 5.3852; 0; 0] v = -3.3852 3.0000 4.0000 >> u = (1/norm(v,2))*v u = -0.5606 0.4968 0.6625 The reflector is the following matrix: >> Q1 = eye(3) - 2*u*u' Q1 = 0.3714 0.5571 0.7428 0.5571 0.5063 -0.6583 0.7428 -0.6583 0.1223 Multiplying Q1 times A we find the following, as expected: >> Ahat = Q1*A Ahat = 5.3852 2.2283 -1.1142 0.0000 0.9114 1.8736 0.0000 -0.4514 0.4982 Now we continue with the same process on the 2 x 2 submatrix on the lower right: >> norm([.9114; -.4514],2) ans = 1.0171 >> v = [.9114; -.4514] - [1.0171; 0] v = -0.1057 -0.4514 >> u = (1/norm(v,2))*v u = -0.2280 -0.9737 >> u2 = [0; -.2280; -.9737] u2 = 0 -0.2280 -0.9737 >> Q2 = eye(3) - 2*u2*u2' The reflector Q2 will produce the final zero below the diagonal: Q2 = 1.0000 0 0 0 0.8960 -0.4440 0 -0.4440 -0.8962 >> Q2*Ahat ans = 5.3852 2.2283 -1.1142 0.0000 1.0171 1.4576 -0.0001 -0.0001 -1.2783 (Note that we don't get exact zeroes because of round-off errors(!)) >> R = Q2*Ahat R = 5.3852 2.2283 -1.1142 0.0000 1.0171 1.4576 -0.0001 -0.0001 -1.2783 The final factorization is given by: >> Q = Q1'*Q2' Q = 0.3714 0.1694 -0.9130 0.5571 0.7459 0.3651 0.7428 -0.6441 0.1827 >> Q*R ans = 2.0000 0.9999 1.0002 3.0000 2.0000 -0.0001 4.0000 1.0000 -2.0000 (close!) MATLAB also has a better built-in QR-factorization routine. It does the same computation as above, except it makes the diagonal entries in R negative rather than positive as above: >> [QM,RM]=qr(A) QM = -0.3714 -0.1695 -0.9129 -0.5571 -0.7459 0.3651 -0.7428 0.6442 0.1826 RM = -5.3852 -2.2283 1.1142 0 -1.0171 -1.4578 0 0 -1.2780