A double pendulum simulator built from scratch: the equations of motion were hand-derived using Lagrangian mechanics, then numerically integrated and visualized in Python. The project also includes a side-by-side chaos demonstration, comparing two pendulums released from nearly identical starting angles to show sensitive dependence on initial conditions.
A double pendulum is a simple-looking mechanical system — two rods, two masses, one pivot — that produces chaotic motion for large swing angles. Small changes in starting conditions lead to wildly different trajectories over time, making it a classic example of deterministic chaos.
This project:
- Derives the system's equations of motion from the Lagrangian by hand
- Converts the resulting coupled, nonlinear, second-order ODEs into a first-order system suitable for numerical integration
- Solves the system using
scipy.integrate.solve_ivp - Animates the result in real time with
matplotlib.animation.FuncAnimation - Demonstrates chaos directly by running two pendulums with a 0.1° difference in initial angle and tracking how fast they diverge
- Live animation of the double pendulum, rendered as connected rod segments with bob markers
- Growing trail behind the second bob, tracing its path through space
- Two phase portraits (θ₁ vs ω₁, θ₂ vs ω₂) — these reveal the qualitative difference between regular and chaotic motion far more clearly than an angle-vs-time plot
- Lyapunov-style divergence panel — plots the angular separation between two near-identical pendulums on a log scale over time, making the exponential divergence characteristic of chaos visible as a straight line
- All four panels (animation, two phase portraits, divergence plot) are laid out together in a single 2×2 figure
Starting from the Lagrangian for two coupled pendulums, applying the Euler-Lagrange equations for
To integrate numerically, the system is rewritten in first-order form by introducing angular velocities
This is the standard method for turning higher-order ODEs into a first-order system that solve_ivp can integrate.
- Python — core implementation, object-oriented design (
Pendulumclass) - NumPy — vectorized trigonometric/array operations, angle unit conversion
- SciPy (
scipy.integrate.solve_ivp,scipy.constants) — numerical ODE integration, physical constants - Matplotlib (
pyplot,animation.FuncAnimation) — real-time animation, multi-panel static plotting, log-scale visualization
double-pendulum/
├── lagrangian.py # Pendulum class, EOMs, simulation, all plotting/animation
├── particle.py # Early single-particle force/motion experiment (unused by lagrangian.py, kept for reference)
└── README.md
pip install numpy scipy matplotlib
python lagrangian.pyI had a venv but I guess pip for normal installs, I don't know how all this works but meh.
A window will open showing the animated double pendulum (with trail), two phase portraits, and the divergence plot, all updating together. Initial conditions (lengths, masses, starting angles) can be changed where the Pendulum objects are constructed near the bottom of lagrangian.py inside the if statement.
MIT — feel free to use or adapt this for your own learning.
