Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

7 (exercises). Engineer’s Toolbox — Symmetric Eigenproblems, Gershgorin & Rayleigh-Ritz

ISAE-SUPAERO

🟩 Complete every # TODO. Solution: 07_EngineeringTips_Gershgorin_RayleighRitz.ipynb.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Circle
from scipy.linalg import eigh, eig

plt.rcParams["figure.figsize"] = (7, 4.2)
plt.rcParams["axes.grid"] = True
np.set_printoptions(precision=4, suppress=True)

Exercise (handwritten) — symmetrizing the eigenproblem

🟥 For M=[9001]M=\begin{bmatrix}9&0\\0&1\end{bmatrix} and the (correct!) stiffness K=[27−3−33]K=\begin{bmatrix}27&-3\\-3&3\end{bmatrix}, compute K~=M−1/2KM−1/2\tilde K=M^{-1/2}KM^{-1/2} by hand, then check with code that it equals [3−1−13]\begin{bmatrix}3&-1\\-1&3\end{bmatrix} with eigenvalues 2, 4.

M = np.array([[9.0, 0.0], [0.0, 1.0]])
K = np.array([[27.0, -3.0], [-3.0, 3.0]])

M_inv_sqrt = ...  # TODO
Ktilde = ...  # TODO
print(Ktilde)

lam_sym, v_sym = ...  # TODO: eigh(...)
print(lam_sym)

Exercise — Gershgorin discs

🟩 For the 3-DOF cantilever-like system K=[2−10−12−10−11]K=\begin{bmatrix}2&-1&0\\-1&2&-1\\0&-1&1\end{bmatrix}, M=IM=I: build K~\tilde K, get its eigenvalues (compare to 0.1981, 1.5550, 3.2470), then complete gershgorin_discs to draw a matplotlib.patches.Circle for each row (center = diagonal entry, radius = sum of the absolute off-diagonal entries in that row) and overlay the true eigenvalues.

K3 = np.array([[2.0, -1.0, 0.0], [-1.0, 2.0, -1.0], [0.0, -1.0, 1.0]])
M3 = np.eye(3)

Ktilde3 = ...  # TODO
lam3, V3 = ...  # TODO
print(lam3)


def gershgorin_discs(A, eigenvalues=None, ax=None):
    n = A.shape[0]
    if ax is None:
        fig, ax = plt.subplots(figsize=(5.5, 5.5))
    for i in range(n):
        center = ...  # TODO
        radius = ...  # TODO
        circ = Circle((center, 0), radius, fill=False, edgecolor=f"C{i}", lw=1.5)
        ax.add_patch(circ)
    if eigenvalues is not None:
        ax.scatter(eigenvalues, np.zeros_like(eigenvalues), color="k", zorder=5, marker="x")
    ax.relim(); ax.autoscale_view()
    ax.set_aspect("equal")
    return ax


gershgorin_discs(Ktilde3, eigenvalues=lam3)
plt.show()

Exercise — Rayleigh quotient with three trial vectors

🟩 Complete rayleigh_quotient(X, K, M) = (X^T K X)/(X^T M X) and evaluate it for X=[1,2,3]TX=[1,2,3]^T, X=[1,1,1]TX=[1,1,1]^T, and X=[−0.32,−0.59,−0.73]TX=[-0.32,-0.59,-0.73]^T (close to the true fundamental mode). Compare each to the true ω12\omega_1^2 and compute the percentage error; plot as a bar chart. Which trial vector is the worst? Which is the best, and why?

def rayleigh_quotient(X, K, M):
    return ...  # TODO


D1 = lam3[0]
trials = {
    "X = [1, 2, 3]": np.array([1.0, 2.0, 3.0]),
    "X = [1, 1, 1]": np.array([1.0, 1.0, 1.0]),
    "X close to true mode": np.array([-0.32, -0.59, -0.73]),
}

# TODO: loop, compute RR and % error for each trial, print, and bar-plot the errors

Bonus exercise

🟩 Evaluate the Rayleigh quotient using the exact second eigenvector (V3[:, 1]) as the trial vector. How small is the error against ω22\omega_2^2? What does this tell you about where the Rayleigh quotient is stationary?

X_mode2 = ...  # TODO
RR2 = ...  # TODO
# TODO: compare RR2 to lam3[1] and print the error