This worksheet develops several ways to compute eigenvalues and eigenvectors for a real matrix. All methods for computing eigenvalues and eigenvectors are iterative in nature, except for very small matrices. We begin with a short discussion of eigenvalues and eigenvectors, and then go on to the power method and inverse power methods. These are methods for computing a single eigenpair, but they can be modified to find several. We then look at shifting, which is an approach for computing one eigenpair whose eigenvalue is close to a specified value. We then look at the QR method, the most efficient method for computing all the eigenvalues at once, and also its shifted variant.
Given an $n\times n$ matrix $A$, a scalar $\lambda$ and nonzero vector $\vec{x}$ are called eigenvalue and eigenvector for $A$ if $A\vec{x}=\lambda\vec{x}$. The set of eigenvectors for some eigenvalue $\lambda$, together with the zero vector, for the $\lambda$-eigenspace of $A$.
The degree $n$ polynomial (in the variable $\lambda$) $\det(A-\lambda I)$ is called the characteristic polynomial, and the equation $\det(A-\lambda I)=0$ is the characteristic equation.
The roots (zeros) of the characteristic equation are the eigenvalues of $A$. The $\lambda$-eigenspace of $A$ is the nullspace of $A-\lambda I$.
Often, textbooks show examples where the characteristic equation can be completely factored and so the eigenvalues easily read off -- this is very rare in real applications.
Here are some interesting questions about eigenvalues:
In some cases, the eigenvectors can be chosen to be orthonormal. Here are some iteresting questions about eigenvectors:
If a unit vector $\vec{u}$ is an exact eigenvector of a matrix $A$, then is easy to determine the value of the corresponding eigenvalue: $$\vec{u}\cdot(A\vec{u})=\vec{u}\cdot(\lambda\vec{u})=\lambda(\vec{u}\cdot\vec{u})=\lambda||\vec{u}||^2=\lambda$$ This still works pretty well with any eigenvector $\vec{x}$, even if it is not a unit vector, just apply the above to the vector $\vec{u}=\frac{1}{||\vec{x}||^2}\vec{x}$.
This motivates the definition of the Rayleigh quotient of a matrix $A$ and a vector $\vec{x}$ as $$R(A,\vec{x})=\frac{\vec{x}\cdot(A\vec{x})}{\vec{x}\cdot\vec{x}}$$
FACT: If the matrix $A$ is symmetric, then the Rayleigh quotient satisfies $$\lambda_{\mbox{min}} \le R(A,\vec{x}) \le \lambda_{\mbox{max}}$$
function r = rayleigh ( A, x )Be sure to include comments describing the input and output parameters and the purpose of the function.
x R(A,x)
[ 1; 2; 3] -0.78571
[ 1; 0; 1] __________ (is an eigenvector)
[ 0; 1; 1] __________ (is an eigenvector)
[ 1;-2; 0] __________ (is an eigenvector)
[ 1; 1; 1] __________
[ 0; 0; 1] __________
x R(A,x)
[3; 2; 1] __________
A*[3; 2; 1] __________
A^2*[3; 2; 1] __________
A^3*[3; 2; 1] __________
A^4*[3; 2; 1] __________
A^5*[3; 2; 1] __________
A^10*[3; 2; 1] __________
A^15*[3; 2; 1] __________
A^20*[3; 2; 1] __________
A^25*[3; 2; 1] __________
The Rayleigh quotient $R(A,\vec{x})$ provides a way to approximate eigenvalues from an approximate eigenvector. Now we have to figure out a way to come up with an approximate eigenvector.
In many physical and engineering applications, the largest or the smallest eigenvalue associated with a system represents the dominant and most interesting mode of behavior. For a bridge or support column, the largest eigenvalue might reveal the maximum load, and the eigenvector the shape of the object as it begins to fail under this load. For a concert hall, the smallest eigenvalue of the acoustic equation reveals the lowest resonating frequency. For nuclear reactors, the largest eigenvalue determines whether the reactor is subcritical, critical, or supercritical. Hence, a simple means of approximating just one extreme eigenvalue might be enough for some cases.
The ``power method'' tries to determine the largest magnitude eigenvalue, and the corresponding eigenvector, of a matrix, by computing (scaled) vectors in the sequence:
$$\vec{x}^{(k+1)} = A \vec{x}^{(k)}$$
Note: The superscript in the above equation does not refer to a power of $x$, or to a component. The use of superscript distinguishes it from the $k^{\mbox{th}}$ component, $\vec{x}_k$, and the parentheses distinguish it from an exponent. It is simply the $k^{\mbox{th}}$ vector in a sequence. When you write Octave code, you will typically denote both $\vec{x}^{(k)}$ and $\vec{x}^{(k+1)}$ with the same Octave name x, and overwrite it each iterate. In the signature lines specified below, xold is used to denote $\vec{x}^{(k)}$ and x is used to denote $\vec{x}^{(k+1)}$ and all subsequent iterates.
If we include the scaling, and an estimate of the eigenvalue, the power method can be described in the following way.
function [r, x, R, X] = power_method ( A, xold ,nsteps ) % [r, x, R, X] = power_method ( A, xold ,nsteps ) % comments % your name and the date . . . for k=1:nsteps . . . r= ??? %Rayleigh quotient R(k)=r; % save Rayleigh quotient on each step X(k)=x(1); % save one eigenvector component on each step endThe history variables R and X will be used to illustrate the progress that the power method is making and have no mathematical importance.
Note: It is usually the case that when writing code such as this that requires the Rayleigh quotient, it would make sense for you to use rayleigh.m, but in this case the Rayleigh quotient is so simple that it is probably better to just copy the code for it into power_method.m.
Using your power method code, try to determine the largest eigenvalue of the matrix test_matrices(1), starting from the vector [1;1;1]. Be sure to make this starting vector into a unit vector if you don't use rayleigh.m. (I have included the result for step 1 to help you check your code out.)
Step Rayleigh q. x(1) 0 3 1.0 1 0.72353 0.0 2 __________ _____________________________ 3 __________ _____________________________ 4 __________ _____________________________ 10 __________ _____________________________ 15 __________ _____________________________ 20 __________ _____________________________In addition, plot the progress of the method with the following commands
plot(R); title 'Rayleigh quotient vs. k' plot(X); title 'Eigenvector component vs. k'Please send me these two plots with your summary.
function [r, x, R, X] = power_method1 ( A, xold , tol ) % [r, x, R, X] = power_method1 ( A, xold , tol ) % comments % your name and the date nsteps=10000; % maximum allowable number of stepsTo determine when to stop iterating, add code to effect
Matrix Rayleigh q. x(1) no. iterations 1 __________ __________ ____________ 2 __________ __________ ____________ 3 __________ __________ ____________ 4 __________ __________ ____________ 5 __________ __________ ____________ 6 __________ __________ ____________ 7 __________ __________ ____________These matrices were chosen to illustrate different iterative behavior. Some converge quickly and some slowly, some oscillate and some don't. One does not converge. Generally speaking, the eigenvalue converges more rapidly than the eigenvector. You can check the eigenvalues of these matrices using the eig function. Plot the histories of Rayleigh quotient and first eigenvector component for all the cases. Send me the plots for cases 3, 5, and 7.
Octave hint: The eig command will show you the eigenvectors as well as the eigenvalues of a matrix A, if you use the command in the form:
[ V, D ] = eig ( A )The quantities V and D are matrices, storing the n eigenvectors as columns in V and the eigenvalues along the diagonal of D.
The inverse power method reverses the iteration step of the power method. We write:$$A x^{(k+1)} = x^{(k)}$$ or, equivalently, $$x^{(k+1)} = A^{-1} x^{(k)}$$ In other words, this looks like we are just doing a power method iteration, but now for the matrix $A^{-1}$. You can immediately conclude that this process will often converge to the eigenvector associated with the largest eigenvalue of $A^{-1}$. The eigenvectors of $A$ and $A^{-1}$ are the same, and if $\vec{v}$ is an eigenvector of $A$ for the eigenvalue $\lambda$, then it is also an eigenvector of $A^{-1}$ for $1/\lambda$ and vice versa.
This means that we can freely choose to study the eigenvalue problem of either $A$ or $A^{-1}$, and easily convert information from one problem to the other. The difference is that the inverse power iteration will find us the biggest eigenvalue of $A^{-1}$, and that's the eigenvalue of $A$ that's smallest in magnitude, while the plain power method finds the eigenvalue of $A$ that is largest in magnitude.
The inverse power method iteration is given in the following algorithm.
Warning: The convergence on $x$ is different, and better, than the one we used in power_method1. You will have to keep track of the previous iterate to implement it.
Your file should have the signature:
function [r, x, R, X] = inverse_power ( A, xold, tol) % [r, x, R, X] = inverse_power ( A, xold, tol) % comments % your name and the dateBe sure to include a check on the maximum number of iterations (10000 is a good choice) so that a non-convergent problem will not go on forever.
[r x R X]=inverse_power(A,[1;1;1],1.e-8);Please include r and x in your summary. How many steps did it take?
[rp xp Rp Xp]=power_method(inv(A),[1;1;1],nsteps);where nsteps is the number of steps you just took using inverse_power. You should find that r is close to 1/rp, that x and xp are close, and that X and Xp are close. If these relations are not true, you have an error somewhere. Fix it before continuing. Warning: the convergence criteria between the two methods is different, so they may take slightly different numbers of iterations. Compare X and Xp only for the common iterations.
% find location of entry with largest magnitude and % multiply all of x so its entry with largest % magnitude is a positive number. This code only % works correctly when x is a real vector. [absx,location]=max(abs(x)); factor=sign(x(location)); x=factor*x; % makes x(location)>=0
Matrix eigenvalue x(1) no. iterations 1 __________ __________ ____________ 2 __________ __________ ____________ 3 __________ __________ ____________ 4 __________ __________ ____________ 5 __________ __________ ____________ 6 __________ __________ ____________ 7 __________ __________ ____________Be careful! One of these still may not converge.
It is common to need more than one eigenpair, but not all eigenpairs. For example, finite element models of structures often have upwards of a million degrees of freedom (unknowns) and just as many eigenvalues. Only the lowest few are of practical interest. Fortunately, matrices arising from finite element structural models are typically symmetric and negative definite, so the eigenvalues are real and negative and the eigenvectors are orthogonal to one another. For the remainder of this section, and this section only, we will be assuming that the matrix whose eigenpairs are being sought is symmetric.
If you try to find two eigenvectors by using the power method (or the inverse power method) twice, on two different vectors, you will discover that you get two copies of the same dominant eigenvector. (The eigenvector with eigenvalue of largest magnitude is called ``dominant.'') Working smarter helps, though. Because the matrix is assumed symmetric, you know that the dominant eigenvector is orthogonal to the others, so you could use the power method (or the inverse power method) twice, forcing the two vectors to be orthogonal at each step of the way. This will guarantee they do not converge to the same vector. If this approach converges to anything, it probably converges to the dominant and ``next-dominant'' eigenvectors and their associated eigenvalues.
Here is a useful function called gram_schmidt.m which regards some vectors as columns of a matrix: the first vector is the first column of a matrix X, the second is the second column, etc. (Be warned that this function will sometimes return a matrix with fewer columns than the one it is given. This is because the original columns were not a linearly independent set.)
The power method applied to several vectors can be described in the following algorithm:
Note: There is no guarantee that the eigenvectors that you find have the largest eigenvalues in magnitude! It is possible that your starting vectors were themselves orthogonal to one or more eigenvectors, and you will never recover eigenvectors that are not spanned by the initial ones. In applications, there are consistency checks that serve to mitigate this problem.
In the following exercise, you will write a Octave function to carry out this procedure to find several eigenpairs for a symmetric matrix. The initial set of eigenvectors will be represented as columns of a Octave matrix V.
function [R, V, k] = power_several ( A, Vold , tol ) % [R, V, k] = power_several ( A, Vold , tol ) % comments % your name and the dateto implement the algorithm described above.
[absV,indices]=max(abs(V)); % indices of largest in columns d=diag(sign(V(indices,:))); % pick out their signs as +-1 D=diag(d); % diagonal matrix with +-1 V=V*D; % Transform V
V = [ 1 1
1 1
1 1
1 -1
1 -1
1 -1]
You should immediately recognize that these vectors are
not only linearly independent, but also orthogonal. Is
the largest eigenvalue this time the same as before?
V=[1 1 1 1 1 1 1 1 1 1 1 -1 1 1 1 1 -1 -1 1 1 1 -1 -1 -1 1 1 -1 -1 -1 -1 1 -1 -1 -1 -1 -1];What are the six eigenvalues?
A major limitation of the power method was that it can only find the eigenvalue of largest magnitude. Similarly, the inverse power iteration seems only able to find the eigenvalue of smallest magnitude. Suppose, however, that you want one (or a few) eigenpairs with eigenvalues that are away from the extremes? Instead of working with the matrix $A$, let's work with the matrix $A+\sigma I$, where we have shifted the matrix by adding $\sigma$ to every diagonal entry. This shift makes a world of difference.
We already said the inverse power method finds the eigenvalue of smallest magnitude. Denote the smallest eigenvalue of $A+\sigma I$ by $\mu$ If $\mu$ is an eigenvalue of $A+\sigma I$ then $A+\sigma I-\mu I=A-(\mu-\sigma )I$ is singular. In other words, $\lambda=\mu-\sigma$ is an eigenvalue of $A$, and is the eigenvalue of $A$ that is closest to $\sigma$.
In general, the idea of shifting allows us to focus the attention of the inverse power method on seeking the eigenvalue closest to any particular value we care to name. This also means that if we have an estimated eigenvalue, we can speed up convergence by using this estimate in the shift. Although various strategies for adjusting the shift during the iteration are known, and can result in very rapid convergence, we will be using a constant shift.
We are not going to worry here about the details or the expense of shifting and refactoring the matrix. (A real algorithm for a large problem would be very concerned about this!) Our simple-minded method algorithm for the shifted inverse power method looks like this:
function [r, x, nsteps] = shifted_inverse ( A, xold, shift, tol) % [r, x, nsteps] = shifted_inverse ( A, xold, shift, tol) % comments % your name and the date
In the previous exercise, we used the backslash division operator. This is another example where the matrix could be factored first and then solved many times, with a possible time savings.
The value of the shift is at the user's disposal, and there is no good reason to stick with the initial (guessed) shift. If it is not too expensive to re-factor the matrix $A-\sigma I$, then the shift can be reset after several iterations. One possible choice for $\sigma$ would be to set it equal to the current eigenvalue estimate, and do so after every, say, third iteration. The closer the shift to the eigenvalue, the faster the convergence.
So far, the methods we have discussed seem suitable for finding one or a few eigenvalues and eigenvectors at a time. In order to find more eigenvalues, we'd have to do a great deal of special programming. Moreover, these methods are surely going to have trouble if the matrix has repeated eigenvalues, distinct eigenvalues of the same magnitude, or complex eigenvalues. The ``QR method'' is a method that handles these sorts of problems in a uniform way, and computes all the eigenvalues, but not the eigenvectors, at one time.
The QR method can be described in the following way.
The sequence of matrices $A^{(k)}$ is orthogonally similar to the original matrix $A$, and hence has the same eigenvalues. This is because $A^k=QR$, and $A^{k+1}=RQ=Q^TQRQ=Q^TA^{k}Q$ Under fairly general conditions, the sequence of matrices $A^{(k)}$ converges to
The Octave command
[ Q, R ] = qr ( A )
computes the QR factorization.
function [E,k] = qr_method ( A, tol) % [E,k] = qr_method ( A, tol) % comments % your name and the dateE is a diagonal matrix of eigenvalues of A. Since the matrix $A^{(k)}$ is supposed to converge, terminate the iteration when $$||A^{(k+1)}-A^{(k)}||_{\text{fro}}\le \epsilon ||A^{(k+1)}||_{\text{fro}}$$ where $\epsilon$ is the relative tolerance. Include a check so the number of iterations never exceeds some maximum (10000 is a good choice).
[v,indices]=max(abs(Q)); % indices of largest in columns d=diag(sign(Q(indices,:))); % pick out their signs as +-1 D=diag(d); % diagonal matrix with +-1 Q=Q*D; % Transform Q R=D*R; % Transform R so still have A=Q*RAdd this code just after doing the QR decomposition in your code and try your revised code on A=test_matrices(1). It should converge. How many steps does it take?
Matrix 3 largest Eigenvalues Number of steps 1 _________ _________ _________ __________ 2 _________ _________ _________ __________ 3 _________ _________ _________ __________ 4 _________ _________ _________ __________ skip no. 5 6 _________ _________ _________ __________ 7 _________ _________ _________ __________
Serious implementations of the QR method save information about each step in order to construct the eigenvectors. The algorithm also uses shifts to try to speed convergence. Some eigenvalues can be determined early in the iteration, which speeds up the process even more. In the following section we consider the question of convergence of the algorithm.
Throughout this lab we have used a very simple convergence criterion. In general, convergence is a difficult subject and frought with special cases and exceptions to rules. To give you a flavor of how convergence might be addressed, we consider the case of the QR algorithm applied to a real, symmetric matrix, $A$, with disctinct eigenvalues, no two of which are the same in magnitude. For such matrices, the following theorem can be shown.
Theorem Let $A$ be a real symmetric matrix of order $n$ and
let its eigenvalues satisfy
$$|\lambda_1|>|\lambda_2|>\dots>|\lambda_n|>0.$$
Then the iterates $\{A^{(k)}\}$ of the QR method will converge to a diagonal
matrix $D$ whose entries are the eigenvalues of $A$. Furthermore,
$$\|D-A^{(k)}\|\leq C \max_k \left|\frac{\lambda_{k+1}}{\lambda_k}\right|$$
The theorem points out a method for detecting convergence. Supposing you start out with a matrix $A$ satisfying the above conditions, and you call the matrix at the $k^{\mbox{th}}$ iteration $A^{(k)}$. If $D^{(k)}$ denotes the matrix consisting only of the diagonal entries of $A^{(k)}$, then the Frobenius norm of $A^{(k)}-D^{(k)}$ must go to zero as a power of $\rho=\max|\lambda_{k}|/|\lambda_{k+1}|$, and if $D$ is the diagonal matrix with the true eigenvalues on its diagonal, then $D^{(k)}$ must converge to $D$ as a geometric series with ratio $\rho$. In the following algorithm, the diagonal entries of $A^{(k)}$ (these are the eigenvalues) will be the only entries considered. The off-diagonal entries converge to zero, but there is no need to make them small so long as the eigenvalues are converged.
The following algorithm includes a convergence estimate as part of the QR algorithm.
You may wonder where the denominator $(1-\rho)$ came from. Consider the geometric series $$S=1+\rho+\rho^2+\rho^3+\dots+\rho^k+\rho^{k+1}+\dots=\frac{1}{(1-\rho)}.$$ Truncating this series at the $k^{\text{th}}$ term and calling the truncated sum $S_k$, we have $$S-S_k=\rho^{k+1}(1+\rho+\rho^2+\dots)=\frac{\rho^{k+1}}{1-\rho}.$$ But, $\rho^{k+1}=S_{k+2}-S_{k+1}$, so $$S-S_k=\frac{S_{k+2}-S_{k+1}}{1-\rho}.$$ Thus, the error made in truncating a geometric series is the difference of the two next terms divided by $(1-\rho)$.
function [E, nsteps]=qr_convergence(A,tol) % [E, nsteps]=qr_convergence(A,tol) % comments % your name and the dateModify the code to implement the above algorithm.
norm(sort(e)-sort(ee))/norm(e)where e=diag(E) is the vector of eigenvalues from qr_convergence?
A = [ .01 1
-1 .01];
It should fail.