Chaotic movement with Double Pendulum

Inspired by Think Twice

What is it?

It is a simulation of a double pendulum where two mass are attached with each other in swing motion. It is interesting because when there is only one mass (a single pendulum), the motion of the system is well-predicted as it is just swing back and forth. However, when another mass is attached, it exhibits chaotic motions where a tiny change in the initial setting will dramatically change the overall motion.

Please see Double Pendulum by Erik Neumann for more detail.

What I learn

It is my first time implementing a physics simulation. At first, I thought it should be straightforward as I already have formulas describing position, velocity and acceleration. Here is my simple pseudocode
    for each step t: 
        v = v + at
        x = x + vt
I found out later that the way I implemented is called `Euler's Method`. It is a numerical method to determine values in a differential equation system.

After playing around for a while, I observed strange behaviour as it was also reported here. It seems that the Euler's method works just fine when `t` is very small and the equations are quite simple. Because it "does not take into account the curvature of the solution, so it tends to give different results depending on the step size" [1]. A better solution to this issue is to implement Runge-Kutta Method. Aim of the method is similar to the Euler's but it takes into account the curvature so it results in a significantly accurate motion.

Even though I didn't fully understand how Runge-Kutta Method works, but my pendulum seems to run smoothly without the strange behaviour.

Open world to the Physics Simulation.