Independently re-derived in Python/sympy, following the physical setup described in
doublePendulum/ of
Kelly’s Lagrange_Mechanics_Derivations.
Chapter 10 already derives this system’s
linearized natural frequencies; here we keep the exact nonlinear equations and integrate them
numerically for a large initial angle, where the motion becomes genuinely chaotic — exactly the
regime a linear modal analysis cannot reach.
import numpy as np
import matplotlib.pyplot as plt
import sympy as sp
from scipy.integrate import solve_ivp
plt.rcParams["figure.figsize"] = (7, 4.0)
plt.rcParams["axes.grid"] = True
def lagrange_equations(L, qs, t, Q=None):
if Q is None:
Q = [0] * len(qs)
eqs = []
for qi, Qi in zip(qs, Q):
qid = sp.diff(qi, t)
eqs.append(sp.expand(sp.diff(sp.diff(L, qid), t) - sp.diff(L, qi) - Qi))
return eqs
def solve_for_accelerations(eqs, qs, t):
qdd = [sp.diff(qi, t, 2) for qi in qs]
M = sp.Matrix([[sp.diff(eq, a) for a in qdd] for eq in eqs])
f = sp.Matrix([eq.subs({a: 0 for a in qdd}) for eq in eqs])
sol = M.solve(-f)
return M, f, solSetup — exact nonlinear equations¶
Two point masses on massless rods of length , pivoted in series, angles from vertical (no small-angle assumption this time):
t = sp.symbols("t")
m, l, g = sp.symbols("m l g", positive=True)
th1 = sp.Function("theta1")(t)
th2 = sp.Function("theta2")(t)
x1, y1 = l * sp.sin(th1), -l * sp.cos(th1)
x2, y2 = x1 + l * sp.sin(th2), y1 - l * sp.cos(th2)
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)
eqs = [sp.simplify(e) for e in lagrange_equations(L, [th1, th2], t)]
M_sym, f_sym, qdd_sol = solve_for_accelerations(eqs, [th1, th2], t)
print("Mass matrix M(theta1,theta2) =\n", M_sym)Mass matrix M(theta1,theta2) =
Matrix([[2*l**2*m, l**2*m*cos(theta1(t) - theta2(t))], [l**2*m*cos(theta1(t) - theta2(t)), l**2*m]])
M is genuinely configuration-dependent here (it depends on ) — unlike
the simple harmonic oscillator or single pendulum, this is a real multi-body coupled system, and
M.solve(-f) (rather than manual algebra) is doing real work.
th1dd_f = sp.lambdify((th1, th2, sp.diff(th1, t), sp.diff(th2, t), m, l, g), qdd_sol[0], "numpy")
th2dd_f = sp.lambdify((th1, th2, sp.diff(th1, t), sp.diff(th2, t), m, l, g), qdd_sol[1], "numpy")
m_val, l_val, g_val = 1.0, 1.0, 9.81
def deriv(_t, y):
a1, a2, w1, w2 = y
return [w1, w2, th1dd_f(a1, a2, w1, w2, m_val, l_val, g_val),
th2dd_f(a1, a2, w1, w2, m_val, l_val, g_val)]Large-amplitude (chaotic) trajectory¶
Release both rods from a large angle at rest — the small-angle in-phase/out-of-phase modes from Chapter 10 no longer apply; the motion is fully nonlinear and sensitive to initial conditions.
y0 = [np.deg2rad(120), np.deg2rad(-30), 0.0, 0.0]
tt = np.linspace(0, 15, 3000)
sol = solve_ivp(deriv, [tt[0], tt[-1]], y0, t_eval=tt, max_step=0.005, rtol=1e-9, atol=1e-9)
a1, a2 = sol.y[0], sol.y[1]
# Cartesian trace of both bobs (a static "poor man's animation")
X1, Y1 = l_val * np.sin(a1), -l_val * np.cos(a1)
X2, Y2 = X1 + l_val * np.sin(a2), Y1 - l_val * np.cos(a2)
fig, axes = plt.subplots(1, 2, figsize=(11, 4.2))
axes[0].plot(tt, np.rad2deg(a1), label=r"$\theta_1$")
axes[0].plot(tt, np.rad2deg(a2), label=r"$\theta_2$")
axes[0].set_xlabel("t [s]"); axes[0].set_ylabel("angle [deg]"); axes[0].legend()
axes[0].set_title("Angle time histories")
axes[1].plot(X2, Y2, lw=0.6, color="C3")
axes[1].plot(0, 0, "ko")
axes[1].set_aspect("equal")
axes[1].set_title("Trace of bob 2's path (chaotic double-pendulum motion)")
plt.tight_layout()
plt.show()
Energy conservation check¶
Since there is no damping or forcing, total mechanical energy should stay constant along the trajectory — a good numerical-integration sanity check independent of the symbolic derivation above.
w1, w2 = sol.y[2], sol.y[3]
Xd1 = l_val * np.cos(a1) * w1
Yd1 = l_val * np.sin(a1) * w1
Xd2 = Xd1 + l_val * np.cos(a2) * w2
Yd2 = Yd1 + l_val * np.sin(a2) * w2
E = (0.5 * m_val * (Xd1**2 + Yd1**2) + 0.5 * m_val * (Xd2**2 + Yd2**2)
+ m_val * g_val * Y1 + m_val * g_val * Y2)
plt.figure()
plt.plot(tt, E - E[0])
plt.xlabel("t [s]"); plt.ylabel(r"$E(t)-E(0)$ [J]")
plt.title("Energy drift of the numerical integration (should stay near zero)")
plt.show()
print(f"Max energy drift: {np.max(np.abs(E - E[0])):.3e} J (initial E={E[0]:.4f} J)")
Max energy drift: 4.952e-07 J (initial E=1.3143 J)