Page 407 - Applied Numerical Methods Using MATLAB
P. 407

396    MATRICES AND EIGENVALUES
           8.6 QR Factorization of Hessenberg Form Using the Givens Rotation
               We can make use of the Givens rotation to get the QR factorization of Hessen-
               berg form by the procedure implemented in the following routine
               “qr_Hessenberg()”, where each element on the lower subdiagonal is zeroed
               out at each iteration. Generate a 4 × 4 random matrix A by the MATLAB com-
               mand rand(4), transform it into a Hessenberg form Hs by using the routine
               “Hessenberg()” and try this routine “qr_Hessenberg()”for the matrix of
               Hessenberg form. Check the validity by seeing if norm(Hs-Q*R) ≈ 0 or not.



               function [Q,R] = qr_Hessenberg(Hs)
               %QR factorization of Hessenberg form by Givens rotation
               N = size(Hs,1);
               Q = eye(N); R = Hs;
               for k = 1:N - 1
                  x = R(k,k); y = R(k+1,k); r = sqrt(x*x + y*y);
                  c = x/r; s = -y/r;
                  R0=R;Q0 = Q;
                  R(k,:) = c*R0(k,:) - s*R0(k + 1,:);
                  R(k + 1,:) = s*R0(k,:) + c*R0(k + 1,:);
                  Q(:,k) = c*Q0(:,k) - s*Q0(:,k + 1);
                  Q(:,k + 1) = s*Q0(:,k) + c*Q0(:,k + 1);
               end




           8.7 Diagonalization by Using QR Factorization to Find Eigenvalues
               You will see that a real symmetric matrix A can be diagonalized into a
               diagonal matrix having the eigenvalues on its diagonal if we repeat the
               similarity transformation by using the orthogonal matrix Q obtained from the
               QR factorization. For this purpose, take the following steps.


                function [eigs,A] = eig_QR(A,kmax)
                %Find eigenvalues by using QR factorization
                if nargin < 2, kmax = 200; end
                for k = 1:kmax
                   [Q,R] = qr(A); %A = Q*R; R =Q’*A =Q^-1*A
                   A = R*Q; %A = Q^ - 1*A*Q
                end
                eigs = diag(A);
                function [eigs,A] = eig_QR_Hs(A,kmax)
                %Find eigenvalues by using QR factorization via Hesenberg
                if nargin < 2, kmax = 200; end
                Hs = hessenberg(A);
                for k = 1:kmax
                   [Q,R] = qr_hessenberg(Hs); %Hs = Q*R; R = Q’*Hs = Q^ - 1*Hs
                   Hs = R*Q; %Hs = Q^ - 1*Hs*Q
                end
                eigs = diag(Hs);
   402   403   404   405   406   407   408   409   410   411   412