Double Pendulum in fewer than 100 lines of JavaScript
physicsandbox.com
physicsandbox.com
The problem is that the double pendulum equations (in fact, any equations of motion derived from a Hamiltonian) have symplectic structure, i.e. they have conserved quantities. One of the conserved quantities is the total energy of the system.
When you discretize the equations to simulate them on a computer, you lose this conservation property (due to discretization error - nothing to do with floating point). The structure of the equations means that the total energy becomes an increasing quantity in time, so you tend to see the simulated system "speed up" or become more energetic as the simulation progresses. The error builds up in the same direction over time - exactly what you don't want!
The simplest example is the harmonic oscillator, with second-order equation of motion
x'' = -x
which gives the first order equations in terms of position x and momentum p x' = p
p' = -x
which conserve the total energy 0.5 * (x^2 + p^2). Discretizing these using a first-order forwards Euler scheme x(t+1) = x(t) + p(t) * dt
p(t+1) = p(t) - x(t) * dt
you can see that the total energy changes on each time step to x(t+1)^2 + p(t+1)^2 = x(t)^2 + 2 x(t) p(t) dt + x(t)^2 dt^2 + p(t)^2 - 2 x(t) p(t) dt + p(t)^2 dt^2
= (1 + dt^2) (x(t)^2 + p(t)^2)
so the total energy increases by a factor of (1 + dt^2) each step. Over time, the total energy increases exponentially.The solution is to use a geometric or sympletic integrator which explicitly takes into account the symplectic structure, producing a set of discrete update equations which still conserve a total energy quantity.
See this thesis: http://umu.diva-portal.org/smash/get/diva2:140361/FULLTEXT01...
the basis for this awesome physics simulator: http://www.algodoo.com/
Conservation of the hamiltonian with leapfrog integration is also the principle behind Hybrid Monte Carlo methods http://www.mcmchandbook.net/HandbookChapter5.pdf
https://www.udacity.com/course/cs222
I wrote a simple N-body simulator game after viewing the course, although I'm still having issues with the energy conservation.
Feature request: mount the pivot on a programmable cart. It'd be fun to write software to balance the double pendulum.
e.g. https://www.youtube.com/watch?v=B6vr1x6KDaY
and, balancing a triple pendulum: https://www.youtube.com/watch?v=cyN-CRNrb3E
My dynamics course teacher actually showed that the double pendulum can be linearized by some approximations (throwing out some negligible terms, small angle approximations etc.), and that way two modes of oscillation could be found: antisymmetric and symmetric oscillation. Just try with small initial angles with OP's script and you'll see.
The lack of friction and the perfectly rigid coupling between the joints might be what makes it seem lacking reality. If we'd extend the chain with more links, the motion of the outermost mass would become more and more unpredictable due to extreme acceleration.
Of course I might have made a mistake somewhere, but I checked that energy is conserved between the frames of the animation (excluding minor energy drift due to the numerical integration).
The first five chapters are about physical systems like this pendulum while the remaining chapters are mostly AI related.
The book use Processing but the concepts are language agnostic: http://natureofcode.com/book/
It barely exceeds 100 lines of js though...
I have a whole bunch of similar physics simulators here: http://www.dllu.net/programming/physics/
In any case, when you max out the first scrollbar, then m_1/M_1 approaches zero. Then the denominators of the Lagrange equation will be close to zero when \theta_1 is close to \theta_2. Thus things become unstable.
Anyway, a symplectic integrator [1] would be more suitable for this problem, since it has the beautiful property of conserving energy.
[0] https://en.wikipedia.org/wiki/RK4#The_Runge.E2.80.93Kutta_me... [1] https://en.wikipedia.org/wiki/Symplectic_integrator
Or maybe I just don't understand how double pendulums work...
(Note: code was never intended to be visible to the public, and this is old and bad. But hey, it works!)
http://www.abebooks.com/9780201657029/Classical-Mechanics-Go...
I learned from a less established text by Taylor: http://www.amazon.com/Classical-Mechanics-John-R-Taylor/dp/1...
Looks unavailable, but a PDF is very clearly the second result on Google for "Classical Mechanics Taylor" so it may be a good option for the tight budget :)
1. Max Mass 1
2. Min Mass 2
3. Max Phi 1
4. Min Phi 2
I assume you can plot the y-axis position of the pendulums over time easily enough, and detect cross-overs... How would you discover the set of inputs where cross-overs will happen, but most infrequently?
[0]: http://rbaron.net/blog/2014/01/02/Simulating-dynamic-systems...
I hacked together a WebGL version here:
https://dl.dropboxusercontent.com/u/1109/dp/index.html
Lots of fun - thank you.
Hint: a big Mass1 and Phi1 makes for a nice wobbly configuration.
Undetermenistic fpu?
It would be cool if you could turn on and off a trail of one mass or the other.
I bet it can produce some nice spirographic(?) shapes!
Chaotic systems are very sensitive to input conditions, where a tiny change yields a completely different result.
With a non chaotic system a small change in the input yields are small change in the output, so it's possible to calculate approximately and get useful results, not so with Chaotic systems.
That's why the "butterfly effect" is used as an explanation of chaotic systems (albeit in my opinion a not a very good one).
Edit: changed contradict to conflict
Another way of looking at real systems, is that the system is deterministic and the errors are random.