Pendulums that start almost identical and end up worlds apart.
A double pendulum is the classic chaotic system. Several are released from starting angles that differ by a tiny fraction of a degree, each integrated with the fourth-order Runge-Kutta method. For a few seconds they move as one, then their paths diverge completely, painting colorful traces of the lower bob.
Try it. Drag a bob to reposition it, then let go. Press R to restart.
Paste this into Claude Code, Codex or any coding agent to get a simple version running, then take it wherever you like.
Build a double pendulum chaos demo with JavaScript and the HTML canvas element. Put everything in a single index.html file with no libraries or build step, so I can open it directly in a browser.
Start simple:
- Make a canvas that fills the window, stays sharp on high-DPI screens (scale by devicePixelRatio), and resizes with the window. Paint it a near-black background.
- Simulate one double pendulum: two rods of equal length and two equal point masses, with a state of four numbers (both angles and both angular velocities). Use the standard equations of motion derived from the Lagrangian; put the formulas in a small function that returns the derivatives.
- Integrate with the fourth-order Runge-Kutta method (RK4), taking about 10 small sub-steps per animation frame so energy stays steady.
- Draw the pivot, both rods and both bobs, scaled so the full reach fits the screen. Start with both arms raised well above horizontal so it swings wildly.
- Record the lower bob's recent positions and draw them as a trail behind it.
Once that works, show off the chaos:
- Run 7 to 9 pendulums at once from starting angles that differ by about 0.0002 radians, each with its own color from a rainbow of hues.
- Fade each trail from bright and thick at the head to dim and thin at the tail by drawing it in short chunks of falling opacity, and use globalCompositeOperation = "lighter" so the overlapping pendulums glow white while they still agree.
- Let me drag a bob to reposition all the pendulums together, then release them, and press R to restart from new random angles.
Explain the key ideas in short code comments. When you're done, tell me how to open it and suggest three directions I could take it next, such as plotting the divergence over time, a map that colors every starting pair of angles by how fast it flips, or adding damping and a driving force.