Independently re-derived in Python/sympy, following the physical setup described in
polarPointMass/ of
Kelly’s Lagrange_Mechanics_Derivations:
a point mass at polar position with an actuator that can push/pull it along the
radial direction. Two versions are shown: (a) the general two-coordinate system with an
explicit radial control force , and (b) the classic playground-swing special case where
is prescribed rather than actuated by an explicit force — “pumping” a swing by
standing up and crouching down twice per oscillation.
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(a) General case: dynamic and , radial thrust ¶
Standard planar-polar kinematics: ... using the convention pointing straight down gives , so
with a radial actuator force entering as the generalized force (no direct actuation of , ).
t = sp.symbols("t")
m, g = sp.symbols("m g", positive=True)
F = sp.Function("F")(t)
r = sp.Function("r")(t)
th = sp.Function("theta")(t)
T = sp.Rational(1, 2) * m * (sp.diff(r, t) ** 2 + r**2 * sp.diff(th, t) ** 2)
V = -m * g * r * sp.cos(th)
L = T - V
eqs = [sp.expand(e) for e in lagrange_equations(L, [r, th], t, Q=[F, 0])]
print("Radial equation (=0): ", eqs[0])
print("Angular equation (=0): ", eqs[1])Radial equation (=0): -g*m*cos(theta(t)) - m*r(t)*Derivative(theta(t), t)**2 + m*Derivative(r(t), (t, 2)) - F(t)
Angular equation (=0): g*m*r(t)*sin(theta(t)) + m*r(t)**2*Derivative(theta(t), (t, 2)) + 2*m*r(t)*Derivative(r(t), t)*Derivative(theta(t), t)
Reading these off: (radial: actuator force balances the centripetal deficit and gravity’s radial component) and (angular: this is exactly — angular momentum changes only due to gravity’s torque, since the actuator is purely radial and exerts no torque about the pivot).
(b) Special case: prescribed — pumping a swing¶
If the “actuator” instead directly prescribes the rod length (think of a child on a swing raising and lowering their center of mass, rather than an external winch force), becomes a known function of time and only remains a true generalized coordinate. The angular equation above still holds with substituted in:
Pumping at (approximately) twice the pendulum’s instantaneous natural frequency — shortening the rod near the bottom of the swing and lengthening it near the extremes — pumps energy into the motion, exactly like a child standing up at the bottom of each swing.
r0, dr = 1.0, 0.15
g_val = 9.81
w0 = np.sqrt(g_val / r0)
def r_smooth(tt, pump):
# smooth length modulation at 2*w0, phase-aligned to shorten near theta=0 crossings
return r0 - pump * dr * np.cos(2 * w0 * tt)
def rdot_smooth(tt, pump):
return pump * dr * 2 * w0 * np.sin(2 * w0 * tt)
def deriv(tt, y, pump):
theta, thd = y
rr = r_smooth(tt, pump)
rd = rdot_smooth(tt, pump)
thdd = -(g_val / rr) * np.sin(theta) - 2 * (rd / rr) * thd
return [thd, thdd]
tt = np.linspace(0, 60, 6000)
fig, ax = plt.subplots()
for pump, label in [(0.0, "constant length (no pumping)"), (1.0, "pumped at $2\\omega_0$")]:
sol = solve_ivp(deriv, [tt[0], tt[-1]], [np.deg2rad(15), 0.0], t_eval=tt, args=(pump,),
max_step=0.01, rtol=1e-9, atol=1e-9)
ax.plot(tt, np.rad2deg(sol.y[0]), label=label)
ax.set_xlabel("t [s]"); ax.set_ylabel(r"$\theta$ [deg]")
ax.set_title("Swing pumping: amplitude grows only when length is modulated at $2\\omega_0$")
ax.legend()
plt.show()