Independently re-derived in Python/sympy, inspired by the physical setup described in
simpleRocket/ of
Kelly’s Lagrange_Mechanics_Derivations:
a rocket near a planet, actuated by two thrusters, moving under inverse-square gravity. The
original repo models a slender rigid body with its own attitude angle and two offset
thrusters (producing both net force and torque). To keep this a clean two-generalized-coordinate
vibration/mechanics demo, we simplify to the point-mass translational core of that problem —
radial and tangential thrust components acting directly on the center of mass — and note the
simplification explicitly here; the full rigid-body attitude dynamics is a natural (but
significantly longer) extension left to the original repository.
import numpy as np
import matplotlib.pyplot as plt
import sympy as sp
from scipy.integrate import solve_ivp
plt.rcParams["figure.figsize"] = (6.5, 6.5)
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¶
Polar coordinates about the planet’s center, inverse-square gravitational potential (), and two thruster force components: radial and tangential . A physical force acting tangentially at radius does virtual work , so its generalized force conjugate to is (moment arm ); the radial thruster maps directly, .
t = sp.symbols("t")
m, mu = sp.symbols("m mu", positive=True)
Fr = sp.Function("F_r")(t)
Ft = sp.Function("F_t")(t)
r = sp.Function("r")(t)
phi = sp.Function("phi")(t)
T = sp.Rational(1, 2) * m * (sp.diff(r, t) ** 2 + r**2 * sp.diff(phi, t) ** 2)
V = -mu * m / r
L = T - V
eqs = [sp.expand(e) for e in lagrange_equations(L, [r, phi], t, Q=[Fr, r * Ft])]
print("Radial equation (=0): ", eqs[0])
print("Angular equation (=0): ", eqs[1])
M_sym, f_sym, qdd_sol = solve_for_accelerations(eqs, [r, phi], t)
print("r_ddot =", qdd_sol[0])
print("phi_ddot =", qdd_sol[1])Radial equation (=0): m*mu/r(t)**2 - m*r(t)*Derivative(phi(t), t)**2 + m*Derivative(r(t), (t, 2)) - F_r(t)
Angular equation (=0): m*r(t)**2*Derivative(phi(t), (t, 2)) + 2*m*r(t)*Derivative(phi(t), t)*Derivative(r(t), t) - F_t(t)*r(t)
r_ddot = (-m*mu + m*r(t)**3*Derivative(phi(t), t)**2 + F_r(t)*r(t)**2)/(m*r(t)**2)
phi_ddot = (-2*m*Derivative(phi(t), t)*Derivative(r(t), t) + F_t(t))/(m*r(t))
Sanity check: an unpowered circular orbit stays circular¶
With and the circular-orbit condition , should stay exactly at for all time (Kepler’s first law, circular special case).
mu_val, m_val = 3.986e5, 1.0 # mu in km^3/s^2 (roughly Earth's), arbitrary unit mass
args = (r, phi, sp.diff(r, t), sp.diff(phi, t), Fr, Ft, m, mu)
rdd_f = sp.lambdify(args, qdd_sol[0].subs({Fr: 0, Ft: 0}), "numpy")
phidd_f = sp.lambdify(args, qdd_sol[1].subs({Fr: 0, Ft: 0}), "numpy")
r0 = 7000.0 # km, roughly a low-Earth-orbit radius
phid0 = np.sqrt(mu_val / r0**3)
def deriv_free(_t, y):
rr, pp, rd, pd = y
return [rd, pd,
rdd_f(rr, pp, rd, pd, 0, 0, m_val, mu_val),
phidd_f(rr, pp, rd, pd, 0, 0, m_val, mu_val)]
period = 2 * np.pi / phid0
tt = np.linspace(0, 2 * period, 2000)
sol0 = solve_ivp(deriv_free, [tt[0], tt[-1]], [r0, 0.0, 0.0, phid0], t_eval=tt,
max_step=1.0, rtol=1e-10, atol=1e-10)
print(f"Circular-orbit check: r ranges over [{sol0.y[0].min():.6f}, {sol0.y[0].max():.6f}] km "
f"(should stay at r0={r0})")Circular-orbit check: r ranges over [7000.000000, 7000.000000] km (should stay at r0=7000.0)
A radial thrust burn perturbs the orbit into an ellipse¶
Starting from the same circular orbit, fire a short radial (outward) thrust burn and watch the
orbit become elliptical — a first-principles version of the classic orbit-raising manoeuvre,
driven entirely by the sympy-derived above with now a
nonzero pulse.
rdd_ctrl = sp.lambdify(args, qdd_sol[0], "numpy")
phidd_ctrl = sp.lambdify(args, qdd_sol[1], "numpy")
burn_accel = 0.05 # km/s^2-equivalent specific radial force, applied briefly at t=0..30s
def deriv_burn(_t, y):
rr, pp, rd, pd = y
fr = burn_accel * m_val if _t < 30 else 0.0
return [rd, pd,
rdd_ctrl(rr, pp, rd, pd, fr, 0, m_val, mu_val),
phidd_ctrl(rr, pp, rd, pd, fr, 0, m_val, mu_val)]
tt2 = np.linspace(0, 2 * period, 4000)
sol1 = solve_ivp(deriv_burn, [tt2[0], tt2[-1]], [r0, 0.0, 0.0, phid0], t_eval=tt2,
max_step=1.0, rtol=1e-10, atol=1e-10)
fig, ax = plt.subplots()
for sol, label, style in [(sol0, "unpowered (circular)", "--"), (sol1, "after radial burn", "-")]:
xx = sol.y[0] * np.cos(sol.y[1])
yy = sol.y[0] * np.sin(sol.y[1])
ax.plot(xx, yy, style, label=label)
ax.plot(0, 0, "yo", markersize=12, label="planet")
ax.set_aspect("equal"); ax.legend()
ax.set_title("Point-mass rocket: radial thrust burn turns a circular orbit elliptical")
plt.show()