Independently re-derived in Python/sympy, following the physical setup described in
rollingCartPole/ of
Kelly’s Lagrange_Mechanics_Derivations:
a disk of mass , radius and moment of inertia rolls without slipping on the ground,
and a point-mass pendulum bob on a rod of length hangs from the disk’s center. The
original derives this with Newton-Euler equations and manually eliminates the internal
constraint (rod tension) force; here Lagrange’s equations skip that elimination step
entirely — no internal constraint force ever appears, which is exactly
Chapter 9’s point about energy methods.
import numpy as np
import matplotlib.pyplot as plt
import sympy as sp
from scipy.integrate import solve_ivp
from scipy.linalg import eigh
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¶
Rolling without slipping ties the disk’s horizontal position to its rotation angle : (one coordinate, not two, thanks to the constraint). The pendulum bob hangs from the disk’s (moving) center: .
t = sp.symbols("t")
m1, m2, I, r, l, g = sp.symbols("m1 m2 I r l g", positive=True)
phi = sp.Function("phi")(t)
th = sp.Function("theta")(t)
x1 = -r * phi
x2 = x1 + l * sp.sin(th)
y2 = -l * sp.cos(th)
T = (sp.Rational(1, 2) * m1 * sp.diff(x1, t) ** 2 + sp.Rational(1, 2) * I * sp.diff(phi, t) ** 2
+ sp.Rational(1, 2) * m2 * (sp.diff(x2, t) ** 2 + sp.diff(y2, t) ** 2))
V = m2 * g * y2
L = sp.simplify(T - V)
print("T =", sp.expand(T))
eqs = [sp.simplify(e) for e in lagrange_equations(L, [phi, th], t)]
M_sym, f_sym, qdd_sol = solve_for_accelerations(eqs, [phi, th], t)
print("Mass matrix M(theta) =\n", M_sym)T = I*Derivative(phi(t), t)**2/2 + l**2*m2*sin(theta(t))**2*Derivative(theta(t), t)**2/2 + l**2*m2*cos(theta(t))**2*Derivative(theta(t), t)**2/2 - l*m2*r*cos(theta(t))*Derivative(phi(t), t)*Derivative(theta(t), t) + m1*r**2*Derivative(phi(t), t)**2/2 + m2*r**2*Derivative(phi(t), t)**2/2
Mass matrix M(theta) =
Matrix([[I + m1*r**2 + m2*r**2, -l*m2*r*cos(theta(t))], [-l*m2*r*cos(theta(t)), l**2*m2]])
The off-diagonal term is the disk/pendulum coupling — it vanishes at (pendulum horizontal) and is maximal at (pendulum hanging straight down, same configuration used for linearization below).
Linearized natural frequency about the hanging equilibrium¶
Small oscillations about (disk free to roll, no restoring force on itself — this system has a rigid-body/zero-frequency mode, exactly like a free-free system):
# Standard small-oscillation recipe: evaluate the mass matrix at the equilibrium configuration
# (theta=0) and take the Hessian of V there for the stiffness matrix (Chapter 9's technique).
M_lin = sp.simplify(M_sym.subs(th, 0))
K_full = sp.hessian(V, [phi, th]).subs(th, 0)
print("M (theta=0) =\n", M_lin)
print("K (theta=0) =\n", K_full)
subs_num = {m1: 2.0, m2: 0.5, I: 1.0, r: 0.3, l: 0.8, g: 9.81}
M_num = np.array(M_lin.subs(subs_num)).astype(float)
K_num = np.array(K_full.subs(subs_num)).astype(float)
lam, _ = eigh(K_num, M_num)
wn = np.sqrt(np.abs(lam))
print("Natural frequencies (rad/s):", np.sort(wn))M (theta=0) =
Matrix([[I + m1*r**2 + m2*r**2, -l*m2*r], [-l*m2*r, l**2*m2]])
K (theta=0) =
Matrix([[0, 0], [0, g*l*m2]])
Natural frequencies (rad/s): [0. 3.56793185]
As expected for a system where is a free (unconstrained, non-restored) rigid-body coordinate, one frequency is (numerically) zero — the disk-plus-pendulum can roll off indefinitely at constant average speed — and the other is the pendulum’s oscillation frequency, slightly modified by the inertia it must drag along with the rolling disk.
Nonlinear simulation¶
Release the pendulum from a moderate angle with the disk initially at rest, and watch the disk roll back and forth in reaction to the swinging bob (conservation of the system’s total angular momentum about the contact line, analogous to a “reaction wheel” pendulum).
args = (phi, th, sp.diff(phi, t), sp.diff(th, t), m1, m2, I, r, l, g)
phidd_f = sp.lambdify(args, qdd_sol[0], "numpy")
thdd_f = sp.lambdify(args, qdd_sol[1], "numpy")
p = subs_num
def deriv(_t, y):
a1, a2, w1, w2 = y
return [w1, w2,
phidd_f(a1, a2, w1, w2, p[m1], p[m2], p[I], p[r], p[l], p[g]),
thdd_f(a1, a2, w1, w2, p[m1], p[m2], p[I], p[r], p[l], p[g])]
tt = np.linspace(0, 12, 2400)
sol = solve_ivp(deriv, [tt[0], tt[-1]], [0.0, np.deg2rad(60), 0.0, 0.0], t_eval=tt,
max_step=0.005, rtol=1e-9, atol=1e-9)
fig, ax = plt.subplots()
ax.plot(tt, sol.y[0], label=r"$\phi$ (disk rotation, rad)")
ax.plot(tt, np.rad2deg(sol.y[1]), label=r"$\theta$ (pendulum angle, deg)")
ax.set_xlabel("t [s]"); ax.legend()
ax.set_title("Rolling disk + pendulum: reaction rolling driven by the swinging bob")
plt.show()