Lesson
11: NumPy Linear Algebra β Trusses, Thermal Networks &
Eigenvalues (Ku = f)
The βBoring Stuffβ Problem: Solving 6-Equation Equilibrium & Heat Networks by Hand
In Statics, Finite Element Analysis (FEA), Heat Conduction Nodal Networks, and Vibrations, problems rarely produce just one equation with one unknown. A 4-node 2D truss produces a \(4 \times 4\) (or \(1000 \times 1000\)) stiffness matrix \(\mathbf{K}\mathbf{u} = \mathbf{f}\). Eliminating variables on paper takes 40 minutes and one sign slip wrecks the whole answer.
In NumPy, once you write the coefficient matrix \(\mathbf{A}\) and right-hand side vector
\(\mathbf{b}\),
np.linalg.solve(A, b) solves the system in
microseconds with full IEEE-64 precision.
Core np.linalg
Matrix Operations
| Engineering Task | Math Expression | NumPy Code |
Watch Out! |
|---|---|---|---|
| Matrix multiplication | \(\mathbf{A}\mathbf{x}\) | A @ x |
Never write
A * x (that multiplies element-by-element!) |
| Linear system solve | \(\mathbf{A}\mathbf{x} = \mathbf{b}\) | x = np.linalg.solve(A, b) |
Never invert manually
(inv(A) @ b is slower & less accurate) |
| Matrix determinant | \(\det(\mathbf{A}) = 0\) check | np.linalg.det(A) |
Near-zero determinant means mechanism / rigid-body instability |
| Principal stresses / modes | \(\mathbf{A}\mathbf{v} = \lambda\mathbf{v}\) | vals, vecs = np.linalg.eig(A) |
Or np.linalg.eigh(A) for
symmetric tensors/stiffness! |
import numpy as np
# Example 1 (Statics / Finite Element Direct Stiffness Method): 3-Spring Serial Assembly Ku = f
# Node 0 is fixed wall (u0 = 0). Springs connect 0-1 (k1=120 N/mm), 1-2 (k2=80 N/mm), 2-3 (k3=60 N/mm).
# External forces applied: F1 = 400 N, F2 = 0 N, F3 = 900 N.
k1, k2, k3 = 120.0, 80.0, 60.0 # [N/mm]
K = np.array([
[k1 + k2, -k2, 0.0],
[-k2, k2 + k3, -k3],
[0.0, -k3, k3 ],
])
f_ext = np.array([400.0, 0.0, 900.0]) # [N]
u_mm = np.linalg.solve(K, f_ext)
residual = np.linalg.norm(K @ u_mm - f_ext)
print("=== 3-DOF Stiffness Solve (Ku = f) ===")
for i, val in enumerate(u_mm, 1):
print(f" Node {i} displacement u_{i} = {val:8.4f} mm")
print(f"Wall reaction R_wall = -k1*u1 = {-k1 * u_mm[0]:.1f} N (Sum of F_ext = {f_ext.sum():.1f} N)")
print(f"Matrix Residual ||Ku - f|| = {residual:.2e} N")=== 3-DOF Stiffness Solve (Ku = f) ===
Node 1 displacement u_1 = 10.8333 mm
Node 2 displacement u_2 = 22.0833 mm
Node 3 displacement u_3 = 37.0833 mm
Wall reaction R_wall = -k1*u1 = -1300.0 N (Sum of F_ext = 1300.0 N)
Matrix Residual ||Ku - f|| = 0.00e+00 N
# Example 2 (Heat Transfer Module 2): Steady 3-Node 1D Insulated Fin Finite-Difference System
# Interior nodes T1, T2, T3 along an aluminum fin; base T0 = 120 C, adiabatic tip at node 3.
# Discretized balance: -(1 + m^2 dx^2 / 2) or standard tridiagonal -(2 + beta) T_i + T_{i-1} + T_{i+1} = -beta T_inf
T0 = 120.0 # Base [C]
T_inf = 25.0 # Ambient air [C]
beta = 0.08 # Dimensionless convection parameter (h*P*dx^2)/(k*Ac)
A = np.array([
[-(2.0 + beta), 1.0, 0.0],
[1.0, -(2.0 + beta), 1.0],
[0.0, 1.0, -(1.0 + 0.5 * beta)], # Insulated tip half-node balance!
])
b = np.array([
-T0 - beta * T_inf,
-beta * T_inf,
-0.5 * beta * T_inf,
])
T_nodes = np.linalg.solve(A, b)
print("=== Steady Fin Nodal Temperatures [degC] ===")
print(" T0 (Base):", T0, "| T1, T2, T3:", np.round(T_nodes, 2))
# Example 3 (Solid Mechanics & Vibrations Eigenvalues): Principal Stresses & 2-DOF Modes
stress_tensor = np.array([
[110.0, 45.0, 0.0],
[45.0, -30.0, 0.0],
[0.0, 0.0, 20.0],
]) # Symmetric Cauchy stress tensor [MPa]
# eigh() exploits symmetry and sorts eigenvalues ascending automatically!
sig_principals, dirs = np.linalg.eigh(stress_tensor)
sig1, sig2, sig3 = sig_principals[::-1]
sig_vm = np.sqrt(0.5 * ((sig1 - sig2)**2 + (sig2 - sig3)**2 + (sig3 - sig1)**2))
print(f"\n=== 3D Principal Cauchy Stresses ===")
print(f" sigma_1, sigma_2, sigma_3 = {sig1:.2f}, {sig2:.2f}, {sig3:.2f} MPa")
print(f" von Mises Equiv. Stress = {sig_vm:.2f} MPa")=== Steady Fin Nodal Temperatures [degC] ===
T0 (Base): 120.0 | T1, T2, T3: [105.11 96.62 93.87]
=== 3D Principal Cauchy Stresses ===
sigma_1, sigma_2, sigma_3 = 123.22, 20.00, -43.22 MPa
von Mises Equiv. Stress = 145.52 MPa
Practice Questions (Concept Check)
- Why does
A * Bfail to perform matrix multiplication in NumPy, and what operator MUST you use instead? - Why is
np.linalg.solve(A, b)strongly preferred overnp.linalg.inv(A) @ bin finite-element and CFD solvers? - What exception does
np.linalg.solveraise if a truss is statically unstable (zero-determinant / singular \(\mathbf{K}\))? - Why is
np.linalg.eigh()better thannp.linalg.eig()for real symmetric stress tensors and stiffness/mass matrices?
Guided
Practice Problem 1 (Worked Self-Check): 2-DOF Car Suspension Natural
Frequencies (Vibrations)
Consider a 2-DOF quarter-car suspension (m1 = 350 kg
sprung body, m2 = 45 kg wheel unsprung mass,
k1 = 22,000 N/m suspension spring,
k2 = 180,000 N/m tire stiffness). Assemble the symmetric
stiffness matrix \(\mathbf{K}\) and
diagonal mass matrix \(\mathbf{M}\),
solve the generalized eigenvalue problem \(\mathbf{M}^{-1}\mathbf{K}\mathbf{v} = \omega^2
\mathbf{v}\), and print both natural frequencies in Hertz
(\(f = \omega / (2\pi)\)).
Hint: Standard Form M^{-1} K
\[K = \begin{bmatrix} k_1 & -k_1 \\
-k_1 & k_1 + k_2 \end{bmatrix}, \qquad M = \begin{bmatrix} m_1 &
0 \\ 0 & m_2 \end{bmatrix}\] Eigenvalues \(\lambda\) of
np.linalg.solve(M, K) equal \(\omega^2\), so
f_hz = np.sqrt(np.real(vals)) / (2 * np.pi)!
# Worked Practice Problem 1 Solution: Quarter-Car 2-DOF Modal Frequencies
m1, m2 = 350.0, 45.0
k1, k2 = 22_000.0, 180_000.0
M = np.diag([m1, m2])
K = np.array([[k1, -k1], [-k1, k1 + k2]])
omega_sq, modes = np.linalg.eig(np.linalg.solve(M, K))
freqs_hz = np.sort(np.sqrt(np.real(omega_sq)) / (2.0 * np.pi))
print(f"Body Bounce Mode 1 : {freqs_hz[0]:.2f} Hz (typical ride comfort target 1.0-1.5 Hz)")
print(f"Wheel Hop Mode 2 : {freqs_hz[1]:.2f} Hz (typical tire hop 10-12 Hz)")Body Bounce Mode 1 : 1.19 Hz (typical ride comfort target 1.0-1.5 Hz)
Wheel Hop Mode 2 : 10.67 Hz (typical tire hop 10-12 Hz)
End-of-Chapter Practice Problems (Try Yourself!)
- Statically Determinate Planar Truss Member Forces: Solve \(\mathbf{A}\mathbf{F} = \mathbf{P}\) for three reaction/member forces \([R_{Ax}, R_{Ay}, F_{BC}]\) supporting a crane boom holding \(P_y = -12\text{ kN}\).
- 2D 4-Node Square Plate Conduction Solver
(
Heat Transfer): Assemble and solve the \(4 \times 4\) interior nodal temperature equations for a square slab with Top=\(100^\circ\text{C}\), Left=\(75^\circ\text{C}\), Bottom=\(25^\circ\text{C}\), Right=\(25^\circ\text{C}\) (\(4T_{i,j} - T_{i-1,j} - T_{i+1,j} - T_{i,j-1} - T_{i,j+1} = 0\)). - Mass Conservation Stoichiometry Matrix
(
ME 300 Combustion): Balance \(\text{C}_3\text{H}_8 + a(\text{O}_2 + 3.76\text{N}_2) \to b\text{CO}_2 + c\text{H}_2\text{O} + d\text{N}_2\) by putting the atomic balances (C, H, O, N) into a \(4 \times 4\) NumPy matrix!