Skip to content
Tutorials Center
My learning

Topics

Lesson 39 of 44 · Projects and Practice

Project: The Double Pendulum and Chaos

Derive the double pendulum's equations with sympy, simulate it, check energy conservation and measure how fast two nearly identical starts drift apart.

Two rods and two masses: it hardly gets simpler as a mechanical system, and yet the double pendulum is the classic example of chaos. Two runs that start almost identically stay together for a while, then separate completely. This project takes you from the Lagrangian to the equations, to a numerical solution, to a measurement of how quickly predictions fail.

The double pendulum and the paths of two nearly identical runs
Caption: left, the setup and the Lagrangian pieces; right, the path of the lower mass for two starts that differ by 0.0001 rad, which separate after a few seconds.

What you will learn

  • How to write the Lagrangian of a two-link system in angle coordinates.
  • How to get and verify the equations of motion with sympy.
  • How to use energy conservation as a correctness check.
  • How to measure sensitive dependence on initial conditions.

Key terms

Chaos: deterministic motion with sensitive dependence on initial conditions.

Lyapunov exponent λ: the average exponential rate at which nearby trajectories separate, d(t) ≈ d₀ e^{λt}.

Prediction horizon: the time after which an initial error becomes as large as the motion itself.

The Lagrangian

Take masses m₁, m₂, rod lengths l₁, l₂, and angles θ₁, θ₂ measured from the downward vertical. The positions are

Text
x₁ = l₁ sinθ₁,           y₁ = −l₁ cosθ₁
x₂ = x₁ + l₂ sinθ₂,      y₂ = y₁ − l₂ cosθ₂

Differentiate, form T = ½m₁(ẋ₁² + ẏ₁²) + ½m₂(ẋ₂² + ẏ₂²) and V = m₁gy₁ + m₂gy₂, and simplify:

Text
T = ½(m₁ + m₂) l₁² θ̇₁² + ½ m₂ l₂² θ̇₂² + m₂ l₁ l₂ θ̇₁ θ̇₂ cos(θ₁ − θ₂)
V = −(m₁ + m₂) g l₁ cosθ₁ − m₂ g l₂ cosθ₂

Notice the cross term in T: the two angles are coupled through their velocities, not only through the potential. That is why the motion is complicated. Since L has no explicit time dependence, the energy E = T + V is conserved. There is no cyclic coordinate and no second conserved quantity, so the system is not integrable: you cannot reduce the problem to quadratures.

The Euler-Lagrange equations give two coupled second-order equations. For the equal-mass, equal-length case they can be written as M(θ) θ̈ = F(θ, θ̇) with

Text
M = m l² [[2, cos(θ₁ − θ₂)], [cos(θ₁ − θ₂), 1]]
F₁ = −m l² θ̇₂² sin(θ₁ − θ₂) − 2 m g l sinθ₁
F₂ =  m l² θ̇₁² sin(θ₁ − θ₂) − m g l sinθ₂

Solving the 2 × 2 system gives the accelerations; the sympy code does this for general masses and lengths.

Common misconceptions

  • "Chaotic means random." The equations are completely deterministic. The unpredictability comes from magnifying tiny differences, not from randomness.
  • "A better computer solves it." The error in a chaotic simulation grows exponentially. Improving precision by a factor of 10⁶ extends the prediction horizon by a fixed amount of time, roughly ln(10⁶)/λ, not indefinitely.
  • "Chaos means no conservation law." Energy is exactly conserved here; chaos limits what you can predict about the configuration, not the energy.

Try it

The first TryIt derives the equations with sympy from the Lagrangian (general masses and lengths), then integrates them for m₁ = m₂ = 1 kg, l₁ = l₂ = 1 m, starting at rest at θ₁ = 2.2 rad, θ₂ = 2.0 rad. It checks the energy and compares the sympy accelerations with a closed-form expression at many points of the trajectory (this closed form is the one used in the interactive demo below). Deriving takes a few seconds, which is normal for symbolic algebra.

Python From L to the double pendulum

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The second TryIt measures the chaos. It integrates two runs whose initial θ₁ differ by 10⁻⁸ rad and plots the distance between them, d = √(Δθ₁² + Δθ₂²), on a logarithmic axis. In the exponential-growth region the slope is an estimate of the Lyapunov exponent λ. This exponent depends on the starting point and energy, and a single trajectory gives only a rough estimate.

Python How fast do nearby starts separate?

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The third TryIt is the double pendulum as a live animation (equal masses and lengths, g = 9.81 m/s², fourth-order Runge-Kutta). Choose the starting angles, press Restart, and switch on the pink twin, a second pendulum whose θ₁ is larger by 0.001 rad. At first you see one pendulum; later the twin goes its own way. It is a simulation of the model equations, not a recording of a real pendulum.

Watch the twin pendulum diverge

Check your understanding

Quick check · 5 questions

of 5 answered You scored / 5 · perfect!

  1. 1. Why is the double pendulum not integrable?

    With two degrees of freedom one needs two independent constants of the motion; only E exists here.
  2. 2. Which cross term couples the two angles in the kinetic energy?

    The velocity of the second mass depends on both angular velocities.
  3. 3. Two runs differ initially by δ and the Lyapunov exponent is λ. After what time does the separation reach about 1?

    From d = δ e^{λt} = 1: t = ln(1/δ)/λ.
  4. 4. You reduce the initial uncertainty by a factor of 1000. The prediction horizon...

    An exponential error growth makes the horizon grow only logarithmically with accuracy.
  5. 5. How can you tell that a simulation of the double pendulum is probably correct?

    Energy conservation is a sharp test; a sign error in an equation destroys it.

Summary

  • The double pendulum's Lagrangian has a velocity cross term; E is conserved but nothing else.
  • sympy can derive the equations; check them by energy conservation and against a closed form.
  • Nearby starts separate as d ≈ δ e^{λt}; the prediction horizon is ln(1/δ)/λ, so more precision helps only logarithmically.

Next: Project: symplectic integrators