Live data from Hacker News

Double Pendulum in fewer than 100 lines of JavaScript

physicsandbox.com

31–40 of 55 posts

Re: Double Pendulum in fewer than 100 lines of JavaScript

#31
post #19

I did this about a year ago: http://www.dllu.net/dp/ It barely exceeds 100 lines of js though... I have a whole bunch of similar physics simulators here: http://www.dllu.net/programming/physics/

Well, your code does not seem to work like the real world. I maxed the first scrollbar, and it does not look good.

There are some numerical instabilities in my code. Theoretically the integrator I'm using (the RK4 [0]) should be a lot more accurate than the Euler's method that OP is using, but I probably have a poor choice of parameters and/or a bug somewhere.

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

Re: Double Pendulum in fewer than 100 lines of JavaScript

#33
post #9
post #4

Would be even better, IMO, with a line in the simulation showing the path traced by each pendulum over time, preferably the arcs drawn could phase through different colours (and possibly decay in visibility). Nice work though.

I did basically this in my first year of college! See [1]. (Note: code was never intended to be visible to the public, and this is old and bad. But hey, it works!) http://kevingibbons.org/doublependulum.html

Your simulation has a "Daming factor"

Re: Double Pendulum in fewer than 100 lines of JavaScript

#34
post #7

Ain't that a chaotic system which is not deterministic?

I've checked that (source is a text written by physicist Hans-Peter Dürr, who worked with Werner Heisenberg in the past), a real double or triple pendulum is from time to time chaotic and uncalculable (if started with strong impulse).

Re: Double Pendulum in fewer than 100 lines of JavaScript

#37
post #31

Earlier quoted context omitted.

Well, your code does not seem to work like the real world. I maxed the first scrollbar, and it does not look good.

There are some numerical instabilities in my code. Theoretically the integrator I'm using (the RK4 [0]) should be a lot more accurate than the Euler's method that OP is using, but I probably have a poor choice of parameters and/or a bug somewhere. 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…

I agree with the parent, I think you must have a bug somewhere. For many positions of the sliders the movement seems very erratic, the pendulums suddenly bounce back or accelerate without any apparent reason. Especially when you start moving the top slider to the right (though not necessarily all the way to the right).

Or maybe I just don't understand how double pendulums work...

Re: Double Pendulum in fewer than 100 lines of JavaScript

#39
Interesting thing about the double pendulum - it is very difficult to get the numerical integration correct, even using very high order integrators like RK4. The simple Euler integrator used here has no hope of getting it right (not a criticism, just an observation!)

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.

[0] http://en.wikipedia.org/wiki/Symplectic_integrator

Re: Double Pendulum in fewer than 100 lines of JavaScript

#40
post #26
post #25

Earlier quoted context omitted.

I don't think it conflicts with it, but I believe the correct explanation has to do more with tiny changes producing big ramifications than with being deterministic or not. 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

For me, the most interesting part of Chaos is that it is deterministic.

Meh, it's mathematically deterministic. In reality there is always error (modelling error, measurement error, simulation error) which means that chaotic systems are effectively random on long time scales, even though they are technically deterministic.
Post reply on HN