💻 Module: 09-SymPy-Symbolic • Unabridged Executed Lesson
📑 Download Module Slides (PDF) 🐍 Download Notebook (.ipynb)

Lesson 23: SymPy Symbolic Calculus & Exact Analytical ODEs (diff, integrate, dsolve)

The “Boring Stuff” Problem: Integrating Beams by Parts 4 Times & Guessing Homogeneous Solutions

Look at Euler-Bernoulli beam bending (Calculus I Week 5 & Solids): \[E I \frac{d^4 w}{dx^4} = q(x)\] Integrating a triangular distributed load \(q(x) = q_0 (x / L)\) four consecutive times by hand, finding integration constants \(C_1, C_2, C_3, C_4\) from boundary conditions (w(0)=0, w'(0)=0, M(L)=0, V(L)=0), and checking your signs takes two clean pages—and one algebra slip ruins the deflection formula!

SymPy automates exact engineering calculus:

import sympy as sp

# Example 1 (Statics & Solid Mechanics): Automated 4th-Order Euler-Bernoulli Cantilever Beam Derivation
x, L, q0, E, I = sp.symbols("x L q0 E I", positive=True)
C1, C2, C3, C4 = sp.symbols("C1 C2 C3 C4")

# Linearly increasing load q(x) = q0 * (x / L) from root (x=0) to free tip (x=L)
q_x = q0 * x / L
V_x = sp.integrate(q_x, x) + C1               # dV/dx = q (or -q depending on sign; using EIw'''' = -q(x) downward)
EI_w4 = -q_x
V_expr = sp.integrate(EI_w4, x) + C1          # E I w''' = V
M_expr = sp.integrate(V_expr, x) + C2         # E I w''  = M
theta_expr = sp.integrate(M_expr, x) + C3     # E I w'
w_expr = (sp.integrate(theta_expr, x) + C4) / (E * I)

# Apply Cantilever BCs: fixed at x=0 (w=0, theta=0), free at x=L (M=0, V=0)
bc_eqs = [
    sp.Eq(w_expr.subs(x, 0), 0),
    sp.Eq(theta_expr.subs(x, 0), 0),
    sp.Eq(M_expr.subs(x, L), 0),
    sp.Eq(V_expr.subs(x, L), 0),
]
consts = sp.solve(bc_eqs, [C1, C2, C3, C4])
w_exact = sp.factor(w_expr.subs(consts))
w_tip = sp.simplify(w_exact.subs(x, L))

print("=== Automated Exact Cantilever Beam Derivation (Downward Load q(x)=q0*x/L) ===")
print("Deflection w(x)   =", w_exact)
print("Max Tip w(L)      =", w_tip, "  (Matches classic handbook -11*q0*L^4 / (120*E*I)!)")
=== Automated Exact Cantilever Beam Derivation (Downward Load q(x)=q0*x/L) ===
Deflection w(x)   = -q0*x**2*(20*L**3 - 10*L**2*x + x**3)/(120*E*I*L)
Max Tip w(L)      = -11*L**4*q0/(120*E*I)   (Matches classic handbook -11*q0*L^4 / (120*E*I)!)
# Example 2 (Heat & Mass Transfer Module 1): Exact dsolve() of 1D Extended Fin ODE!
m, L_fin, theta_b = sp.symbols("m L_fin theta_b", positive=True)
x_f = sp.Symbol("x", real=True, nonnegative=True)
theta = sp.Function("theta")

# Governing Fin ODE: theta''(x) - m^2 * theta(x) = 0; BCs: theta(0)=theta_b, theta'(L)=0 (insulated tip)
fin_ode = sp.Eq(theta(x_f).diff(x_f, 2) - (m**2) * theta(x_f), 0)
sol_fin = sp.dsolve(fin_ode, theta(x_f), ics={theta(0): theta_b, theta(x_f).diff(x_f).subs(x_f, L_fin): 0})
theta_exact = sp.simplify(sol_fin.rhs.rewrite(sp.cosh))
print("=== Exact Fin Temperature Profile via SymPy dsolve ===")
print("theta(x) =", theta_exact)

# Taylor Series check: Verify tanh(u)/u -> 1 - u^2/3 as fin parameter u = m*L -> 0
u = sp.Symbol("u", positive=True)
print("Fin Efficiency Series tanh(u)/u =", sp.series(sp.tanh(u) / u, u, 0, 6))
=== Exact Fin Temperature Profile via SymPy dsolve ===
theta(x) = theta_b*(exp(2*L_fin*m) + exp(2*m*x))*exp(-m*x)/(exp(2*L_fin*m) + 1)
Fin Efficiency Series tanh(u)/u = 1 - u**2/3 + 2*u**4/15 + O(u**6)

LOOK AT THAT OUTPUT IN EXAMPLE 1: SymPy derived w(L) = -11*q0*L^4 / (120*E*I) from scratch in 0.2 seconds with zero integration slips! Notice also .rewrite(sp.cosh) in Example 2 neatly folded raw exponentials e^(mx) + e^(-mx) straight back into textbook hyperbolic cosines cosh(m*(L - x))!


Practice Questions (Concept Check)

  1. Why do we pass theta = sp.Function("theta") instead of theta = sp.Symbol("theta") when calling sp.dsolve?
  2. What does sp.integrate(f, (x, 0, L)) compute vs. sp.integrate(f, x)?
  3. How does .series(u, 0, 5) help engineers create rapid desktop approximations from bulky transcendental formulas?
  4. When solving an ODE with ics={...}, why do derivative boundary conditions require .subs(x, L) cleanly?

Guided Practice Problem 1 (Worked Self-Check): Parabolic Submerged Dam Hydrostatic Integral (Fluid Mechanics Module 1)

Using sp.integrate, find the exact symbolic area \(A = \int_0^H w(y)\,dy\), centroid depth \(\bar{y}_c = \frac{1}{A}\int_0^H y\,w(y)\,dy\), and total hydrostatic force \(F_R = \rho g \int_0^H y\,w(y)\,dy\) on a vertical parabolic spillway gate of width \(w(y) = b_0 \left(1 - \frac{y^2}{H^2}\right)\) extending from surface \(y=0\) to \(y=H\) (Module 1 Lecture).

Hint: Area = (2/3) * b0 * H, Centroid y_bar = (3/8) * H

Notice hydrostatic pressure is \(P(y) = \rho g y\), so \(dF = \rho g y\, w(y)\,dy\)!

# Worked Practice Problem 1 Solution: Exact Parabolic Spillway Hydrostatic Integrals
y, H, b0, rho, g = sp.symbols("y H b0 rho g", positive=True)
w_y = b0 * (1 - (y / H)**2)
A_gate = sp.integrate(w_y, (y, 0, H))
F_R = rho * g * sp.integrate(y * w_y, (y, 0, H))
y_bar = sp.simplify(sp.integrate(y * w_y, (y, 0, H)) / A_gate)
y_cp = sp.simplify(sp.integrate(y**2 * w_y, (y, 0, H)) / sp.integrate(y * w_y, (y, 0, H)))
print(f"Area A        = {A_gate}")
print(f"Centroid y_c  = {y_bar} | Center of Pressure y_cp = {y_cp}")
print(f"Force F_R     = {F_R}")
Area A        = 2*H*b0/3
Centroid y_c  = 3*H/8 | Center of Pressure y_cp = 8*H/15
Force F_R     = H**2*b0*g*rho/4

End-of-Chapter Practice Problems (Try Yourself!)

  1. Plane Wall Conduction with Uniform Heat Generation (HMT): Solve \(d^2 T / dx^2 + \dot{q}/k = 0\) via sp.dsolve with symmetric wall boundary conditions \(T(-L) = T_s\) and \(T(L) = T_s\), and verify peak centerline temperature \(T(0) = T_s + \frac{\dot{q} L^2}{2k}\).
  2. Area Moment of Inertia Proof: Prove \(I_{xx,c} = \frac{b h^3}{12}\) and \(I_{\text{circle}} = \frac{\pi R^4}{4}\) using exact sp.integrate!
  3. Optimization via Exact Calculus (Calculus I): Derive the radius-to-height ratio \(R/H\) minimizing surface area of a closed cylindrical storage tank of fixed volume \(V_0 = \pi R^2 H\) (D = H!).

🎬 Concept & Engineering Animation

🎬 Euler Bernoulli Four Integrals