Independently re-derived in Python/sympy, following the physical setup described in
singlePendulum/ of
Kelly’s Lagrange_Mechanics_Derivations
(there: MATLAB Symbolic Math Toolbox + Newton-Euler cross-check; here: sympy + Lagrange).
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¶
A point mass on a massless rigid rod of length , pivoted at the origin, angle from the downward vertical:
t = sp.symbols("t")
m, l, g = sp.symbols("m l g", positive=True)
th = sp.Function("theta")(t)
x = l * sp.sin(th)
y = -l * sp.cos(th)
T = sp.Rational(1, 2) * m * (sp.diff(x, t) ** 2 + sp.diff(y, t) ** 2)
V = m * g * y
L = sp.simplify(T - V)
print("T =", sp.simplify(T), " V =", V)
eqs = lagrange_equations(L, [th], t)
eq = sp.simplify(eqs[0])
print("Euler-Lagrange residual (=0):", eq)T = l**2*m*Derivative(theta(t), t)**2/2 V = -g*l*m*cos(theta(t))
Euler-Lagrange residual (=0): l*m*(g*sin(theta(t)) + l*Derivative(theta(t), (t, 2)))
This simplifies to , i.e. — the exact nonlinear pendulum equation.
M, f, qdd_sol = solve_for_accelerations([eq], [th], t)
print("theta_ddot =", qdd_sol[0])theta_ddot = -g*sin(theta(t))/l
Nonlinear vs. linearized period¶
For small angles , the linear period is , independent of amplitude. Let’s integrate the exact nonlinear equation for a few amplitudes and see the (well-known) amplitude-dependent period grow beyond .
m_val, l_val, g_val = 1.0, 1.0, 9.81
rhs = sp.lambdify((th, m, l, g), qdd_sol[0], "numpy")
T0 = 2 * np.pi * np.sqrt(l_val / g_val)
def deriv(_t, y):
theta, thd = y
return [thd, rhs(theta, m_val, l_val, g_val)]
fig, ax = plt.subplots()
for theta0_deg in [10, 60, 120, 170]:
theta0 = np.deg2rad(theta0_deg)
sol = solve_ivp(deriv, [0, 3 * T0], [theta0, 0.0], max_step=0.005, dense_output=True)
tt = np.linspace(0, 3 * T0, 1500)
ax.plot(tt / T0, sol.sol(tt)[0] / theta0, label=f"$\\theta_0$={theta0_deg}°")
ax.set_xlabel(r"$t/T_0$ (linear periods)"); ax.set_ylabel(r"$\theta/\theta_0$")
ax.set_title("Single pendulum: large-amplitude period lengthens beyond the linear $T_0$")
ax.legend()
plt.show()