🟩 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 and the (correct!) stiffness , compute by hand, then check with code that it equals 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 ,
: build , 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
, , and (close to the true fundamental
mode). Compare each to the true 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 errorsBonus exercise¶
🟩 Evaluate the Rayleigh quotient using the exact second eigenvector (V3[:, 1]) as the trial
vector. How small is the error against ? 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