lunes, 16 de noviembre de 2020

Eigenvalue Computation in MATLAB with QR algorithm


http://faculty.salina.k-state.edu/tim/DAT/linAlg/appendixLA/matlabEig.html

Eigenvalue Computation in MATLAB


function eigs = myQReig(A)
% MYQREIG - iterative QR algorithm to find eigenvalues
%
% NOTE: The purpose of this code is to illustrate the algorithm.
%   It converges reasonably quick for most matrices, but not all matrices.
%   So use MATLAB's eig function when you just want the eigenvalues of
%   a matrix.

    A1 = A;
    count = 0;   % just to track the performance
    % limit the precision and test convergence to upper triangular
    while ~istriu(round(A1, 7))
        count = count+1;
        [Q, R] = qr(A1);
        A1 = R*Q;
    end
    eigs = diag(A1);
    fprintf('iterations = %g\n', count);
end


Eigenvalue Computation in MATLAB

The eigenvalue computation algorithm in MATLAB’s eig() function is quite complex. It looks at the data and performs pre-conditoning transformations to make the computation quicker. It uses different algorithms to compute the eigenvalues depending on the size and relationships of the values in the matrix.

It does not usually (maybe never, but I’m not certain about that) find the eigenvalues by finding the roots of the characteristic polynomial, i.e., find the \lambdas that satisfy the equation det(A - \lambda\,I) = 0.

6.11.6.1. QR Factorization

One of the more well known algorithms for finding eigenvalues without computing determinants is an iterative method based on QR factorization. MATLAB’s eig() will sometimes use an advanced implementation of this algorithm. What is described here is just the basics of the algorithm and does not have good performance for all matrices.

QR factorization factors the matrix into sub-matrices \bm{Q} of orthonormal column vectors and an upper triangular matrix, \bm{R} (see Gram Schmidt and Orthogonal (QR) Factoring). After \bm{A} = \bm{Q\,R} is computed, a new matrix \bm{A_1} = \bm{R\,Q} is found. Then QR factorization is done again on \bm{A_1} to find \bm{A_2 = \bm{R_1\,Q_1}} continuing until \bm{A_n} is upper triangular and additional iterations will not change the result. These \bm{A_{1 \ldots n}} are Similar Matrices to the original \bm{A} matrix, which means that they have the same eigenvalues. When a matrix is upper triangular, the eigenvalues are on the diagonal.

The algorithm for finding the QR factorization is called Gram-Schmidt. See Gram Schmidt and Orthogonal (QR) Factoring for a quick description of the Gram-Schmidt algorithm and why \bm{R\,Q} and \bm{A} are Similar Matrices. It is described in many Linear Algebra textbooks and online videos such as Professor Strang’s Linear Algebra course (18.06) on MIT Open Courseware. [STRANG99]

A more complex question about this algorithm is why does it converge to a stable upper triangular matrix. The complete explanation of convergence is quite advanced and beyond the scope of this course. For the brave, an explanation of the convergence is found at http://pi.math.cornell.edu/~web6140/TopTenAlgorithms/QRalgorithm.html.

What happens after the loop has been repeated many (k) times is that the orthogonal \bm{Q} matrix becomes the identity matrix and then \bm{A_}k = \bm{R}_{k+1}.

Since \bm{R}_k = \bm{Q}_k^T\,\bm{A}_{k-1}, then \bm{A}_k =
\bm{R}_k\,\bm{Q}_k = \bm{Q}_k^T\,\bm{A}_{k-1}\,\bm{Q}_k. We can see that if \bm{Q}_k \to \bm{I} as k \to \infty, then \bm{A}_k
\to \bm{A}_{k-1} as k \to \infty, so \bm{A}_k converges to final values as an upper triangular matrix with the eigenvalues on the diagonal.

No hay comentarios:

Publicar un comentario