A7 · Lesson 40 · Lab: eigenvalues in code
Computing eigenvalues, steady states, modes and e^{At}
No formula gives the eigenvalues of a large matrix: for 5 × 5 and up, the characteristic polynomial has no solution in radicals, and finding its roots numerically is a poor method anyway. Libraries use iterations instead. In this lab you'll write the simplest one, power iteration, and watch it converge at the rate the eigenvalues set. Then you'll put library eigensolvers to work on lessons 33 to 39: a channel's long-run state, the three normal modes of a chain of coupled tanks, and the matrix exponential, computed so that it works even for the defective matrices eigenvectors can't handle.
- Power iteration multiplies a vector by over and over. The part along the top eigenvector grows fastest, and the rest shrinks by each step.
- The curve is the error after steps, , for the ratio you set. The readout counts the steps to reach .
- Push toward 1, two nearly equal eigenvalues, and convergence slows to a crawl.
Predict
Power iteration runs on a symmetric matrix with eigenvalues 4.73, 3 and 1.27. By what factor does the error in the Rayleigh-quotient estimate of the top eigenvalue shrink per step?
About 0.40. The vector's error shrinks by per step, and for a symmetric matrix the Rayleigh quotient is accurate to the square of the vector's error, so it improves by per step. The eigenvalue reaches accuracy in 24 steps.
Name it
Power iteration
Start from almost any and repeat: , then divide by so the numbers stay finite. Writing the start in eigenvectors, , so the direction approaches with error shrinking like . The Rayleigh quotient (with ) estimates . Production eigensolvers use the QR algorithm, a refined relative of this idea.
Library calls
np.linalg.eig(A)returns eigenvalues and eigenvector columns, complex if necessary, in no particular order.np.linalg.eigh(S)is for symmetric matrices: real eigenvalues in increasing order and orthonormal eigenvectors, faster and more accurate.
The matrix exponential by scaling and squaring
fails for defective matrices and loses accuracy when is ill conditioned. The robust method uses the series, made safe by scaling and squaring: divide by until its norm is below about 1, sum a few series terms (they now shrink fast), then square the result times, since . Library versions refine this with Padé approximations.
Worked example
How many steps does power iteration need on ?
Show the solution
- Eigenvalues. They are , 3 and .
- Convergence rate. The Rayleigh quotient's error shrinks per step by
- Track the error. Starting from , the error is 0.105 after one step, 0.0024 after five, and below after 24, where
Answer 24 steps bring the error below .
Worked example
Three identical LC tanks in a row are each coupled to their neighbors with = 0.1, and each has = 1. What are the mode frequencies?
Show the solution
- Stiffness matrix.
- Mode frequencies. The eigenvalues are , and , so the mode frequencies, in rad/s, are
- Mode shapes. The slowest mode swings all three tanks together, ; the middle one has the ends opposed and the center still, ; the fastest has the center against both ends, .
Answer 1, 1.0488 and 1.1402 rad/s.
Check
A power iteration's error shrinks by 0.8 per step. How many steps take it from 1 to below ?
Check
Three tanks in a row have = 1 and coupling = 0.05. What is the highest mode frequency?
Check
Write power(A, steps) returning the Rayleigh-quotient estimate of the largest eigenvalue after steps iterations from . Set lam = power(M, 60) for the matrix in the starter.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
A three-state channel moves between states with the Markov matrix in the starter (its columns sum to 1). Use np.linalg.eig to find the steady state, scaled so its entries sum to 1, and set p.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Build for three tanks in a row with = 1 and coupling = 0.15, and set w to the mode frequencies in increasing order, using np.linalg.eigh.
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Check
Write expm(A) by scaling and squaring: choose so that has norm below 1, sum 20 terms of the series, then square times. Set E1 = expm(np.array([[0, -np.pi], [np.pi, 0]])) and E2 = expm(np.array([[1.0, 1], [0, 1]])).
Python 3.14 with numpy and matplotlib, running in your browser. Tab indents; press Esc and then Tab to move on.
Explain why
Why does power iteration find the largest eigenvalue, and what makes it slow?
Writing the starting vector as a combination of eigenvectors, each multiplication by multiplies each component by its eigenvalue. After steps, the component along the largest eigenvalue has grown by and the next by , so relative to the leader every other component shrinks by at least , and the direction lines up with . The speed is set entirely by that ratio: 0.5 halves the error each step, while 0.99 needs about 2300 steps for ten digits. Two nearly equal eigenvalues are close to indistinguishable to the iteration, which is why practical methods shift the matrix to change the ratio, or use the QR algorithm, which works on all the eigenvalues at once.
Practice
Keep practicing
5 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.