This chapter is not in Prof. Morlier’s original notes, which derive every equation of motion from Newton’s second law. Chapter 5’s handwritten exercise box previews the energy-method idea (“write and in compact form...”) without carrying it through — this chapter completes that thought, in the spirit of PEC00025’s Lagrange/fundamental-principles class. Energy methods are indispensable once a system’s geometry makes Newton’s law painful to apply directly (rotations, constraints, multiple coupled coordinates).
New here: Chapter 9 (the prerequisite chapter just before this one) works out the matrix
calculus behind and the general
matrix form , and shows how sympy’s hessian() can extract and
automatically from and instead of the manual coefficient-matching used below. Read
it first if the manual extraction in §10.2 feels like it’s missing a shortcut — it is,
and Chapter 9 names it.
import numpy as np
import matplotlib.pyplot as plt
import sympy as sp
from scipy.linalg import eigh
plt.rcParams["figure.figsize"] = (7, 4.2)
plt.rcParams["axes.grid"] = True
np.set_printoptions(precision=4, suppress=True)10.1 Lagrange’s equations¶
For a conservative system with generalized coordinates , define the Lagrangian (kinetic minus potential energy). The Euler-Lagrange equations
(where collects any non-conservative generalized forces, e.g. damping or external excitation) give the equations of motion directly from two scalar energy expressions — no free body diagrams, no bookkeeping of internal constraint forces. This is especially powerful once coordinates are not simple Cartesian displacements (angles, arc lengths, etc.).
10.2 Warm-up: re-deriving Chapter 5’s 2-DOF system¶
As a sanity check, let’s re-derive the mass-spring chain from Chapter 5
( connected to a wall and to each other by ) using Lagrange’s equations
with sympy, and confirm we recover the same matrices used there.
t = sp.symbols("t")
m1, m2, k1, k2, k3 = sp.symbols("m1 m2 k1 k2 k3", positive=True)
x1 = sp.Function("x1")(t)
x2 = sp.Function("x2")(t)
T = sp.Rational(1, 2) * m1 * sp.diff(x1, t) ** 2 + sp.Rational(1, 2) * m2 * sp.diff(x2, t) ** 2
V = (sp.Rational(1, 2) * k1 * x1**2 + sp.Rational(1, 2) * k2 * (x2 - x1) ** 2
+ sp.Rational(1, 2) * k3 * x2**2)
L = T - V
def lagrange_eq(L, q):
qd = sp.diff(q, t)
return sp.expand(sp.diff(sp.diff(L, qd), t) - sp.diff(L, q))
eq1 = lagrange_eq(L, x1)
eq2 = lagrange_eq(L, x2)
print("Equation 1 (=0):", eq1)
print("Equation 2 (=0):", eq2)Equation 1 (=0): k1*x1(t) + k2*x1(t) - k2*x2(t) + m1*Derivative(x1(t), (t, 2))
Equation 2 (=0): -k2*x1(t) + k2*x2(t) + k3*x2(t) + m2*Derivative(x2(t), (t, 2))
Reading off the coefficients of gives exactly , — matching Chapter 5’s equation \eqref{MDOF2} derived from Newton’s law, as it must (Lagrange’s and Newton’s formulations always agree; Lagrange is just bookkeeping through energy instead of forces).
Msym = sp.Matrix([[sp.diff(eq1, sp.diff(x1, t, 2)), sp.diff(eq1, sp.diff(x2, t, 2))],
[sp.diff(eq2, sp.diff(x1, t, 2)), sp.diff(eq2, sp.diff(x2, t, 2))]])
Ksym = sp.Matrix([[sp.diff(eq1, x1), sp.diff(eq1, x2)],
[sp.diff(eq2, x1), sp.diff(eq2, x2)]])
print("M =\n", Msym)
print("K =\n", Ksym)M =
Matrix([[m1, 0], [0, m2]])
K =
Matrix([[k1 + k2, -k2], [-k2, k2 + k3]])
10.3 A system Newton’s law makes painful: the double pendulum¶

Now for a system where free-body diagrams get messy fast: two point masses on massless rigid rods of length , pivoted in series (a double pendulum). Generalized coordinates: , the angles of each rod from vertical.
This is fully nonlinear (there is no simple Newtonian free-body shortcut once the rods swing
through large angles — this system is chaotic for large motions). Let’s derive the exact
nonlinear equations of motion with sympy, then linearize for small oscillations about the
hanging equilibrium and extract the natural frequencies.
m, l, g, eps = sp.symbols("m l g epsilon", positive=True)
th1 = sp.Function("theta1")(t)
th2 = sp.Function("theta2")(t)
# a bookkeeping trick for linearization: scale the angles by a formal small parameter eps,
# expand the exact nonlinear equations in powers of eps, and keep only the leading (linear) order
th1e, th2e = eps * th1, eps * th2
x1 = l * sp.sin(th1e)
y1 = -l * sp.cos(th1e)
x2 = x1 + l * sp.sin(th2e)
y2 = y1 - l * sp.cos(th2e)
T = (sp.Rational(1, 2) * m * (sp.diff(x1, t) ** 2 + sp.diff(y1, t) ** 2)
+ sp.Rational(1, 2) * m * (sp.diff(x2, t) ** 2 + sp.diff(y2, t) ** 2))
V = m * g * y1 + m * g * y2
L = sp.simplify(T - V)
eq1_full = lagrange_eq(L, th1)
eq2_full = lagrange_eq(L, th2)
print("Exact nonlinear eq. 1 (=0):", sp.expand(eq1_full))Exact nonlinear eq. 1 (=0): epsilon**3*l**2*m*sin(epsilon*theta1(t) - epsilon*theta2(t))*Derivative(theta2(t), t)**2 + epsilon**2*l**2*m*cos(epsilon*theta1(t) - epsilon*theta2(t))*Derivative(theta2(t), (t, 2)) + 2*epsilon**2*l**2*m*Derivative(theta1(t), (t, 2)) + 2*epsilon*g*l*m*sin(epsilon*theta1(t))
The exact equation is nonlinear in (products of trig functions and squared angular velocities — genuine centripetal/Coriolis-type coupling). To linearize, expand in powers of and keep only the lowest surviving order (which turns out to be here, since every term already carries one explicit and the leading trig expansion contributes another):
def linearize(eq, order=2):
s = sp.expand(sp.series(eq, eps, 0, order + 1).removeO())
return sp.simplify(s.coeff(eps, order))
eq1_lin = linearize(eq1_full)
eq2_lin = linearize(eq2_full)
print("Linearized eq. 1 (=0):", eq1_lin)
print("Linearized eq. 2 (=0):", eq2_lin)Linearized eq. 1 (=0): l*m*(2*g*theta1(t) + 2*l*Derivative(theta1(t), (t, 2)) + l*Derivative(theta2(t), (t, 2)))
Linearized eq. 2 (=0): l*m*(g*theta2(t) + l*Derivative(theta1(t), (t, 2)) + l*Derivative(theta2(t), (t, 2)))
th1dd, th2dd = sp.diff(th1, t, 2), sp.diff(th2, t, 2)
M_pend = sp.Matrix([[sp.diff(eq1_lin, th1dd), sp.diff(eq1_lin, th2dd)],
[sp.diff(eq2_lin, th1dd), sp.diff(eq2_lin, th2dd)]]) / (m * l)
K_pend = sp.Matrix([[sp.diff(eq1_lin, th1), sp.diff(eq1_lin, th2)],
[sp.diff(eq2_lin, th1), sp.diff(eq2_lin, th2)]]) / (m * l)
M_pend = sp.simplify(M_pend)
K_pend = sp.simplify(K_pend)
print("M (linearized, /m*l) =\n", M_pend)
print("K (linearized, /m*l) =\n", K_pend)M (linearized, /m*l) =
Matrix([[2*l, l], [l, l]])
K (linearized, /m*l) =
Matrix([[2*g, 0], [0, g]])
This is now a completely standard generalized eigenvalue problem, exactly like Chapter 5’s
eigsort. Substituting numeric values () and solving:
subs = {m: 1.0, l: 1.0, g: 9.81}
M_num = np.array(M_pend.subs(subs)).astype(float)
K_num = np.array(K_pend.subs(subs)).astype(float)
lam, V = eigh(K_num, M_num)
wn = np.sqrt(lam)
print("Natural frequencies (rad/s):", wn)
wn_exact = np.sqrt(np.array([2 - np.sqrt(2), 2 + np.sqrt(2)]) * 9.81 / 1.0)
print("Exact closed form sqrt((2-/+sqrt2)*g/l):", wn_exact)Natural frequencies (rad/s): [2.3972 5.7874]
Exact closed form sqrt((2-/+sqrt2)*g/l): [2.3972 5.7874]
This matches the well-known closed-form result for the linearized double pendulum, — obtained here purely mechanically (symbolic differentiation
series expansion), without ever having to draw a free-body diagram of the messy internal reaction forces between the two rods.
Visualizing the two linear modes¶
fig, axes = plt.subplots(1, 2, figsize=(9, 4))
for ax, i in zip(axes, range(2)):
shape = V[:, i] / np.max(np.abs(V[:, i]))
ax.plot([0, shape[0]], [0, -1], "o-", label=r"$\theta_1$ contribution")
ax.plot([shape[0], shape[0] + shape[1]], [-1, -2], "o-", label=r"$\theta_2$ contribution")
ax.set_title(rf"Mode {i+1}, $\omega_{i+1}={wn[i]:.3f}$ rad/s")
ax.set_ylim(-2.3, 0.3)
ax.legend(fontsize=7)
plt.tight_layout()
plt.show()
In mode 1 (lower frequency) both pendulums swing in phase; in mode 2 (higher frequency) they swing out of phase — exactly the same qualitative pattern as the in-phase/out-of-phase modes of Chapter 5’s 2-DOF mass-spring chain, even though the physical system (rotational, gravity-driven) looks completely different. This is the real payoff of the modal-analysis machinery built in the basics chapters: it applies unchanged to any linearized multi-DOF system, however it was derived.
10.4 Fundamental principles: virtual work / D’Alembert as an alternative¶
Lagrange’s equations can themselves be derived from D’Alembert’s principle: at every instant, for any virtual displacement consistent with the constraints. Writing the positions in terms of generalized coordinates and using turns this into exactly the Euler-Lagrange equations above. The two viewpoints — energy (Lagrange) and virtual work of forces (D’Alembert) — are equivalent; which one is more convenient depends on the problem (energy bookkeeping is usually easier for chains of rigid bodies; virtual work is often more natural when explicit non-conservative forces, like friction or control inputs, are involved).