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.

10 (advanced). Lagrange’s Equations & Fundamental Principles

ISAE-SUPAERO

This chapter is not in Prof. Morlier’s original notes, which derive every equation of motion from Newton’s second law. Chapter 5’s handwritten exercise box previews the energy-method idea (“write 2T2T and 2V2V in compact form...”) without carrying it through — this chapter completes that thought, in the spirit of PEC00025’s Lagrange/fundamental-principles class. Energy methods are indispensable once a system’s geometry makes Newton’s law painful to apply directly (rotations, constraints, multiple coupled coordinates).

New here: Chapter 9 (the prerequisite chapter just before this one) works out the matrix calculus behind ∂(q˙TMq˙)/∂q˙=2Mq˙\partial(\dot q^\mathsf{T}M\dot q)/\partial\dot q=2M\dot q and the general matrix form Mq¨+Cq˙+Kq=FM\ddot q+C\dot q+Kq=F, and shows how sympy’s hessian() can extract MM and KK automatically from TT and VV instead of the manual coefficient-matching used below. Read it first if the manual M,KM,K extraction in §10.2 feels like it’s missing a shortcut — it is, and Chapter 9 names it.

import numpy as np
import matplotlib.pyplot as plt
import sympy as sp
from scipy.linalg import eigh

plt.rcParams["figure.figsize"] = (7, 4.2)
plt.rcParams["axes.grid"] = True
np.set_printoptions(precision=4, suppress=True)

10.1 Lagrange’s equations

For a conservative system with generalized coordinates qiq_i, define the Lagrangian L=T−VL=T-V (kinetic minus potential energy). The Euler-Lagrange equations

ddt(∂L∂q˙i)−∂L∂qi=Qi\frac{d}{dt}\left(\frac{\partial L}{\partial\dot q_i}\right)-\frac{\partial L}{\partial q_i} = Q_i

(where QiQ_i collects any non-conservative generalized forces, e.g. damping or external excitation) give the equations of motion directly from two scalar energy expressions — no free body diagrams, no bookkeeping of internal constraint forces. This is especially powerful once coordinates are not simple Cartesian displacements (angles, arc lengths, etc.).

10.2 Warm-up: re-deriving Chapter 5’s 2-DOF system

As a sanity check, let’s re-derive the mass-spring chain from Chapter 5 (m1,m2m_1,m_2 connected to a wall and to each other by k1,k2,k3k_1,k_2,k_3) using Lagrange’s equations with sympy, and confirm we recover the same [M],[K][M],[K] matrices used there.

t = sp.symbols("t")
m1, m2, k1, k2, k3 = sp.symbols("m1 m2 k1 k2 k3", positive=True)
x1 = sp.Function("x1")(t)
x2 = sp.Function("x2")(t)

T = sp.Rational(1, 2) * m1 * sp.diff(x1, t) ** 2 + sp.Rational(1, 2) * m2 * sp.diff(x2, t) ** 2
V = (sp.Rational(1, 2) * k1 * x1**2 + sp.Rational(1, 2) * k2 * (x2 - x1) ** 2
     + sp.Rational(1, 2) * k3 * x2**2)
L = T - V


def lagrange_eq(L, q):
    qd = sp.diff(q, t)
    return sp.expand(sp.diff(sp.diff(L, qd), t) - sp.diff(L, q))


eq1 = lagrange_eq(L, x1)
eq2 = lagrange_eq(L, x2)
print("Equation 1 (=0):", eq1)
print("Equation 2 (=0):", eq2)
Equation 1 (=0): k1*x1(t) + k2*x1(t) - k2*x2(t) + m1*Derivative(x1(t), (t, 2))
Equation 2 (=0): -k2*x1(t) + k2*x2(t) + k3*x2(t) + m2*Derivative(x2(t), (t, 2))

Reading off the coefficients of x¨1,x¨2,x1,x2\ddot x_1,\ddot x_2,x_1,x_2 gives exactly M=diag(m1,m2)M=\text{diag}(m_1,m_2), K=[k1+k2−k2−k2k2+k3]K=\begin{bmatrix}k_1+k_2&-k_2\\-k_2&k_2+k_3\end{bmatrix} — matching Chapter 5’s equation \eqref{MDOF2} derived from Newton’s law, as it must (Lagrange’s and Newton’s formulations always agree; Lagrange is just bookkeeping through energy instead of forces).

Msym = sp.Matrix([[sp.diff(eq1, sp.diff(x1, t, 2)), sp.diff(eq1, sp.diff(x2, t, 2))],
                   [sp.diff(eq2, sp.diff(x1, t, 2)), sp.diff(eq2, sp.diff(x2, t, 2))]])
Ksym = sp.Matrix([[sp.diff(eq1, x1), sp.diff(eq1, x2)],
                   [sp.diff(eq2, x1), sp.diff(eq2, x2)]])
print("M =\n", Msym)
print("K =\n", Ksym)
M =
 Matrix([[m1, 0], [0, m2]])
K =
 Matrix([[k1 + k2, -k2], [-k2, k2 + k3]])

10.3 A system Newton’s law makes painful: the double pendulum

Two independent SDOF systems

Now for a system where free-body diagrams get messy fast: two point masses mm on massless rigid rods of length ll, pivoted in series (a double pendulum). Generalized coordinates: θ1,θ2\theta_1,\theta_2, the angles of each rod from vertical.

x1=lsin⁡θ1, y1=−lcos⁡θ1,x2=x1+lsin⁡θ2, y2=y1−lcos⁡θ2x_1=l\sin\theta_1,\ y_1=-l\cos\theta_1, \qquad x_2=x_1+l\sin\theta_2,\ y_2=y_1-l\cos\theta_2

This is fully nonlinear (there is no simple Newtonian free-body shortcut once the rods swing through large angles — this system is chaotic for large motions). Let’s derive the exact nonlinear equations of motion with sympy, then linearize for small oscillations about the hanging equilibrium and extract the natural frequencies.

m, l, g, eps = sp.symbols("m l g epsilon", positive=True)
th1 = sp.Function("theta1")(t)
th2 = sp.Function("theta2")(t)

# a bookkeeping trick for linearization: scale the angles by a formal small parameter eps,
# expand the exact nonlinear equations in powers of eps, and keep only the leading (linear) order
th1e, th2e = eps * th1, eps * th2
x1 = l * sp.sin(th1e)
y1 = -l * sp.cos(th1e)
x2 = x1 + l * sp.sin(th2e)
y2 = y1 - l * sp.cos(th2e)

T = (sp.Rational(1, 2) * m * (sp.diff(x1, t) ** 2 + sp.diff(y1, t) ** 2)
     + sp.Rational(1, 2) * m * (sp.diff(x2, t) ** 2 + sp.diff(y2, t) ** 2))
V = m * g * y1 + m * g * y2
L = sp.simplify(T - V)

eq1_full = lagrange_eq(L, th1)
eq2_full = lagrange_eq(L, th2)
print("Exact nonlinear eq. 1 (=0):", sp.expand(eq1_full))
Exact nonlinear eq. 1 (=0): epsilon**3*l**2*m*sin(epsilon*theta1(t) - epsilon*theta2(t))*Derivative(theta2(t), t)**2 + epsilon**2*l**2*m*cos(epsilon*theta1(t) - epsilon*theta2(t))*Derivative(theta2(t), (t, 2)) + 2*epsilon**2*l**2*m*Derivative(theta1(t), (t, 2)) + 2*epsilon*g*l*m*sin(epsilon*theta1(t))

The exact equation is nonlinear in θ1,θ2\theta_1,\theta_2 (products of trig functions and squared angular velocities — genuine centripetal/Coriolis-type coupling). To linearize, expand in powers of ε\varepsilon and keep only the lowest surviving order (which turns out to be ε2\varepsilon^2 here, since every term already carries one explicit ε\varepsilon and the leading trig expansion contributes another):

def linearize(eq, order=2):
    s = sp.expand(sp.series(eq, eps, 0, order + 1).removeO())
    return sp.simplify(s.coeff(eps, order))


eq1_lin = linearize(eq1_full)
eq2_lin = linearize(eq2_full)
print("Linearized eq. 1 (=0):", eq1_lin)
print("Linearized eq. 2 (=0):", eq2_lin)
Linearized eq. 1 (=0): l*m*(2*g*theta1(t) + 2*l*Derivative(theta1(t), (t, 2)) + l*Derivative(theta2(t), (t, 2)))
Linearized eq. 2 (=0): l*m*(g*theta2(t) + l*Derivative(theta1(t), (t, 2)) + l*Derivative(theta2(t), (t, 2)))
th1dd, th2dd = sp.diff(th1, t, 2), sp.diff(th2, t, 2)
M_pend = sp.Matrix([[sp.diff(eq1_lin, th1dd), sp.diff(eq1_lin, th2dd)],
                     [sp.diff(eq2_lin, th1dd), sp.diff(eq2_lin, th2dd)]]) / (m * l)
K_pend = sp.Matrix([[sp.diff(eq1_lin, th1), sp.diff(eq1_lin, th2)],
                     [sp.diff(eq2_lin, th1), sp.diff(eq2_lin, th2)]]) / (m * l)
M_pend = sp.simplify(M_pend)
K_pend = sp.simplify(K_pend)
print("M (linearized, /m*l) =\n", M_pend)
print("K (linearized, /m*l) =\n", K_pend)
M (linearized, /m*l) =
 Matrix([[2*l, l], [l, l]])
K (linearized, /m*l) =
 Matrix([[2*g, 0], [0, g]])

This is now a completely standard generalized eigenvalue problem, exactly like Chapter 5’s eigsort. Substituting numeric values (m=1,l=1,g=9.81m=1,l=1,g=9.81) and solving:

subs = {m: 1.0, l: 1.0, g: 9.81}
M_num = np.array(M_pend.subs(subs)).astype(float)
K_num = np.array(K_pend.subs(subs)).astype(float)

lam, V = eigh(K_num, M_num)
wn = np.sqrt(lam)
print("Natural frequencies (rad/s):", wn)

wn_exact = np.sqrt(np.array([2 - np.sqrt(2), 2 + np.sqrt(2)]) * 9.81 / 1.0)
print("Exact closed form sqrt((2-/+sqrt2)*g/l):", wn_exact)
Natural frequencies (rad/s): [2.3972 5.7874]
Exact closed form sqrt((2-/+sqrt2)*g/l): [2.3972 5.7874]

This matches the well-known closed-form result for the linearized double pendulum, ω1,22=(2∓2) g/l\omega_{1,2}^2=(2\mp\sqrt2)\,g/l — obtained here purely mechanically (symbolic differentiation

  • series expansion), without ever having to draw a free-body diagram of the messy internal reaction forces between the two rods.

Visualizing the two linear modes

fig, axes = plt.subplots(1, 2, figsize=(9, 4))
for ax, i in zip(axes, range(2)):
    shape = V[:, i] / np.max(np.abs(V[:, i]))
    ax.plot([0, shape[0]], [0, -1], "o-", label=r"$\theta_1$ contribution")
    ax.plot([shape[0], shape[0] + shape[1]], [-1, -2], "o-", label=r"$\theta_2$ contribution")
    ax.set_title(rf"Mode {i+1}, $\omega_{i+1}={wn[i]:.3f}$ rad/s")
    ax.set_ylim(-2.3, 0.3)
    ax.legend(fontsize=7)
plt.tight_layout()
plt.show()
<Figure size 900x400 with 2 Axes>

In mode 1 (lower frequency) both pendulums swing in phase; in mode 2 (higher frequency) they swing out of phase — exactly the same qualitative pattern as the in-phase/out-of-phase modes of Chapter 5’s 2-DOF mass-spring chain, even though the physical system (rotational, gravity-driven) looks completely different. This is the real payoff of the modal-analysis machinery built in the basics chapters: it applies unchanged to any linearized multi-DOF system, however it was derived.

10.4 Fundamental principles: virtual work / D’Alembert as an alternative

Lagrange’s equations can themselves be derived from D’Alembert’s principle: at every instant, ∑i(Fi−mir¨i)⋅δri=0\sum_i(\mathbf F_i - m_i\ddot{\mathbf r}_i)\cdot\delta\mathbf r_i = 0 for any virtual displacement δri\delta\mathbf r_i consistent with the constraints. Writing the positions ri\mathbf r_i in terms of generalized coordinates qjq_j and using δri=∑j∂ri∂qjδqj\delta\mathbf r_i=\sum_j\frac{\partial\mathbf r_i}{\partial q_j}\delta q_j turns this into exactly the Euler-Lagrange equations above. The two viewpoints — energy (Lagrange) and virtual work of forces (D’Alembert) — are equivalent; which one is more convenient depends on the problem (energy bookkeeping is usually easier for chains of rigid bodies; virtual work is often more natural when explicit non-conservative forces, like friction or control inputs, are involved).