Tutorial 7
Click the badges above to run this tutorial interactively in your browser without installing anything!
- Binder: Free cloud-based Jupyter environment
- Colab: Google's free Jupyter notebook environment
Topics Covered
This tutorial covers the following topics using Python and SymPy:
- Eigenvalue / eigenvector problems for a 3-DOF mass-spring system
- Characteristic polynomial and the natural frequencies
- Normalised and unscaled eigenvectors
- Diagonalisation: the eigenvector matrix and the spectral matrix
- Powers of a matrix via
- Cayley-Hamilton theorem
Introduction
import sympy as sy
from sympy.abc import x, y, z, t
from sympy import Matrix, Rational, eye, zeros, diag, sqrt, pi, N, simplify, solve
sy.init_printing(use_latex=True)
lam = sy.Symbol('lambda') # the eigenvalue symbol used in the characteristic polynomial
Question 1
The 3-DOF mass-spring system has coefficient matrix
with , and .
A = Matrix([[Rational(100, 3), Rational(-70, 3), 0],
[Rational(-70, 3), Rational(140, 3), Rational(-70, 3)],
[0, Rational(-70, 3), Rational(100, 3)]])
display(A.charpoly(lam).as_expr()) # characteristic polynomial
display(solve(A.charpoly(lam).as_expr(), lam)) # exact eigenvalues
The characteristic equation is , whose roots are and .
ev = sorted(A.eigenvects(), key=lambda item: N(item[0])) # ascending eigenvalue
display([N(item[0], 8) for item in ev]) # [6.3349835, 33.333333, 73.665016]
lam1, _, vecs1 = ev[0] # smallest eigenvalue
v1 = vecs1[0]
display(lam1, N(lam1, 8)) # 40 - 10*sqrt(102)/3 = 6.3350
display(v1.T, [N(c, 6) for c in v1]) # [1, 1.1571, 1]
The smallest eigenvalue is , with unscaled eigenvector .
The printed solution quotes and
. Those are decimal approximations from a numerical
root finder — the exact values are and
. SymPy's solve returns the exact radicals.
Question 2
The second largest eigenvalue and its normalised eigenvector.
lam2, _, vecs2 = ev[1] # middle eigenvalue
v2 = vecs2[0]
display(lam2, N(lam2, 8)) # 100/3 = 33.3333
display(v2.T) # [-1, 0, 1]
display(v2.normalized().T) # [-sqrt(2)/2, 0, sqrt(2)/2]
display([N(c, 6) for c in v2.normalized()]) # [-0.7071, 0, 0.7071]
So with normalised eigenvector .
Question 3
The largest eigenvalue and its unscaled eigenvector.
lam3, _, vecs3 = ev[2] # largest eigenvalue
v3 = vecs3[0]
display(lam3, N(lam3, 8)) # 40 + 10*sqrt(102)/3 = 73.6650
display(v3.T, [N(c, 6) for c in v3]) # [1, -1.7285, 1]
So with unscaled eigenvector .
Question 4
Combine the results to build the eigenvector matrix and diagonalise .
vecs = [item[2][0] for item in ev]
vecs[1] = vecs[1].normalized() # use the normalised middle eigenvector
P = Matrix.hstack(*vecs) # eigenvector (modal) matrix
D = diag(*[item[0] for item in ev]) # spectral matrix
display(N(P, 6))
display(N(D, 6))
# verify P^-1 A P = D
display(simplify(P.inv()*A*P - D) == zeros(3, 3)) # True
The diagonal matrix is the eigenvalue matrix: its diagonal entries are exactly the eigenvalues of , in the same order as the columns of .
Question 5
Compute and comment on the eigenvalues and eigenvectors.
A50 = P * (D**50) * P.inv()
display(N(A50 / 10**93, 6))
The eigenvalues of are (each eigenvalue is raised to the same power), while the eigenvectors of are unchanged from those of .
Question 6
with has an eigenvalue of . Find the remaining eigenvalues and the characteristic equation.
B = Matrix([[1, 2, 3], [0, 1, 1], [0, 0, 2]])
display(B.trace(), B.det()) # 4, 2
display(B.eigenvals()) # {1: 2, 2: 1} -> 1, 1, 2
display(B.charpoly(lam).as_expr()) # lambda**3 - 4*lambda**2 + 5*lambda - 2
Using and with gives and , so . The characteristic equation is — obtained from the trace, the sum of the principal minors and the determinant, without ever forming .
Question 7
Verify the Cayley-Hamilton statements and compute .
display(simplify(Rational(1, 2)*B**2 - 2*B + Rational(5, 2)*eye(3) - B.inv())) # zero matrix
display(B**5)
display(26*B**2 - 47*B + 22*eye(3)) # same matrix -> formula verified
display(B**6)
display(57*B**2 - 108*B + 52*eye(3)) # same matrix -> formula verified
Question 8
Why does fail here?
P8 = Matrix([[1, 1, 5],
[0, 0, 1],
[0, 0, 1]])
display(P8.det()) # 0
display(P8.rank()) # 2 -> only 2 independent eigenvectors
The repeated eigenvalue has algebraic multiplicity but only one independent eigenvector, so the eigenvector matrix has two identical columns and . Since is singular, does not exist and the diagonalisation formula cannot be used. The Cayley-Hamilton theorem has no such restriction — which is exactly why it was used in Q7.
Question 9
Advantage and disadvantage of each method.
| Diagonalisation | Cayley-Hamilton | |
|---|---|---|
| Advantage | Very fast once and are known — a single formula gives any power. | Derived from the characteristic equation alone; no eigenvectors needed. |
| Disadvantage | Needs the complete eigenvector set, and fails when is not invertible (repeated roots, defective matrix). | The recurrence for each new power must be re-derived by hand. |
Question 10
— find all eigenvalues and normalised eigenvectors, then verify .
C = Matrix([[0, 1, 1], [1, 0, 1], [1, 1, 0]])
display(C.charpoly(lam).as_expr()) # lambda**3 - 3*lambda - 2
display(C.eigenvals()) # {-1: 2, 2: 1}
for val, mult, vecs in C.eigenvects():
display(val, mult, [[N(c, 6) for c in v] for v in vecs],
[list(v.normalized()) for v in vecs])
The eigenvalues are (multiplicity 2) and , with normalised eigenvectors
P = Matrix([[-1/sqrt(2), -1/sqrt(2), 1/sqrt(3)],
[ 1/sqrt(2), 0, 1/sqrt(3)],
[ 0, 1/sqrt(2), 1/sqrt(3)]])
D = diag(-1, -1, 2)
display(N(C*P, 6)) # LHS
display(N(P*D, 6)) # RHS
display(simplify(C*P - P*D) == zeros(3, 3)) # True -> verified
Since , the eigenvalue matrix and eigenvector matrix satisfy the eigenvalue/eigenvector problem .
Extra Learning Resources
Key Concepts to Master
- Characteristic equation:
- Trace and determinant checks: and
- Normalisation: divide an eigenvector by its magnitude so that
- Diagonalisation: exists only when (the eigenvector matrix) is invertible
- Cayley-Hamilton: a matrix satisfies its own characteristic equation, so can always be reduced to a combination of
SymPy Resources
- SymPy
charpoly— the characteristic polynomial - SymPy
eigenvects— eigenvalues with multiplicities and eigenvectors - SymPy
diagonalize— returns(P, D)directly
Common Pitfalls
eigenvects()returns a list of tuples(eigenvalue, multiplicity, [vectors])— unpack it before use- A repeated eigenvalue does not guarantee repeated independent eigenvectors; when it does not, the matrix is defective and cannot be diagonalised
- Eigenvectors are only defined up to a scalar multiple — scaled and unscaled answers are both correct
N(expr, n)givesnsignificant figures, not decimal places