Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Lagrange gallery — 2. Single pendulum

ISAE-SUPAERO

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, sol

Setup

A point mass mm on a massless rigid rod of length ll, pivoted at the origin, angle θ(t)\theta(t) from the downward vertical:

x=lsin⁡θ,y=−lcos⁡θ⇒T=12ml2θ˙2,V=−mglcos⁡θx=l\sin\theta,\quad y=-l\cos\theta \qquad\Rightarrow\qquad T=\tfrac12 m l^2\dot\theta^2,\quad V=-mgl\cos\theta
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 ml2θ¨+mglsin⁡θ=0ml^2\ddot\theta+mgl\sin\theta=0, i.e. θ¨=−glsin⁡θ\ddot\theta=-\dfrac{g}{l}\sin\theta — 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 sin⁡θ≈θ\sin\theta\approx\theta, the linear period is T0=2πl/gT_0=2\pi\sqrt{l/g}, independent of amplitude. Let’s integrate the exact nonlinear equation for a few amplitudes and see the (well-known) amplitude-dependent period grow beyond T0T_0.

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()
<Figure size 700x400 with 1 Axes>