News for hackers. Stuff that matters.
Re:Double Pendulum in fewer than 100 lines of JavaScript by throwaway283719 on Wednesday July 09, 2014 @06:00AM (#8008844)
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 by murbard2 on Wednesday July 09, 2014 @08:18AM (#8009219)
Leapfrog integration will also get you conservation of energy.

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

Re:Double Pendulum in fewer than 100 lines of JavaScript by vanderZwan on Wednesday July 09, 2014 @08:44AM (#8009317)
That thesis looks insteresting, but is also 472 pages. Is there a TL;DR version?
Re:Double Pendulum in fewer than 100 lines of JavaScript by madengr on Wednesday July 09, 2014 @05:55PM (#8012464)
The Udacity course on numerical diffy-Q covers some of this.

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.

https://github.com/madengr/nbody

Re:Double Pendulum in fewer than 100 lines of JavaScript by fjarlq on Tuesday July 08, 2014 @09:02PM (#8007595)
Cool.

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

Re:Double Pendulum in fewer than 100 lines of JavaScript by pavel_lishin on Wednesday July 09, 2014 @01:02PM (#8010757)
I feel like we ought to be burning someone as a witch after watching that.
Re:Double Pendulum in fewer than 100 lines of JavaScript by tfb on Thursday July 10, 2014 @11:28AM (#8015280)
Whoa. This is blowing my mind. It is seriously amazing that this exists.
Re:Double Pendulum in fewer than 100 lines of JavaScript by flannelhead on Wednesday July 09, 2014 @03:00AM (#8008412)
Very nice simulation you've got there! I also did a triple pendulum in JS a while ago. Quite a lot more than 100 lines of code, though. The equations of motion alone take up lots and again lots of space. (Imagine typing them in! I don't even dare to think about adding a fourth degree of freedom) http://users.jyu.fi/~samakapa/triplependulum/

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.

Re:Double Pendulum in fewer than 100 lines of JavaScript by cyphar on Thursday July 10, 2014 @04:12AM (#8014117)
... that pendulum doesn't mirror reality at all. It's as though the first mass is constantly being accelerated by something other than gravity.
Re:Double Pendulum in fewer than 100 lines of JavaScript by flannelhead on Thursday July 10, 2014 @05:40AM (#8014361)
First mass counting from which end? The masses are accelerated by each other as there are rigid rods between them.

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).

Re:Double Pendulum in fewer than 100 lines of JavaScript by ApiM on Wednesday July 09, 2014 @07:23AM (#8009051)
The Nature of Code by Daniel Shiffman is a free (pay what you want) open source ebook about simulating natural systems.

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/

Re:Double Pendulum in fewer than 100 lines of JavaScript by dllu on Wednesday July 09, 2014 @12:12AM (#8008021)
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/

Re:Double Pendulum in fewer than 100 lines of JavaScript by rullelito on Wednesday July 09, 2014 @02:38AM (#8008366)
Well, your code does not seem to work like the real world. I maxed the first scrollbar, and it does not look good.
Re:Double Pendulum in fewer than 100 lines of JavaScript by dllu on Wednesday July 09, 2014 @03:47AM (#8008520)
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 by simias on Wednesday July 09, 2014 @05:19AM (#8008741)
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 by pbhjpbhj on Tuesday July 08, 2014 @09:04PM (#8007601)
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.
Re:Double Pendulum in fewer than 100 lines of JavaScript by Bakkot on Tuesday July 08, 2014 @09:44PM (#8007686)
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

Re:Double Pendulum in fewer than 100 lines of JavaScript by leni536 on Wednesday July 09, 2014 @04:21AM (#8008590)
Your simulation has a "Daming factor"
Re:Double Pendulum in fewer than 100 lines of JavaScript by Bakkot on Wednesday July 09, 2014 @11:26AM (#8010129)
... Well that's an ancient typo. Corrected, thanks.
Re:Double Pendulum in fewer than 100 lines of JavaScript by lessnonymous on Tuesday July 08, 2014 @11:12PM (#8007894)
I thought the same .. though there's no decay but it's nice to see the traced path: http://jsfiddle.net/m5MZe/1/
Re:Double Pendulum in fewer than 100 lines of JavaScript by codezero on Tuesday July 08, 2014 @08:43PM (#8007546)
I remember when I first learned about the Lagrangian, and my mind was blown that really complicated problems could be wrangled so easily. Thanks for sharing this :)
Re:Double Pendulum in fewer than 100 lines of JavaScript by randlet on Tuesday July 08, 2014 @09:06PM (#8007611)
Me too. Lagrangian mechanics are almost the only thing about my physics undergrad where I still recall thinking "wow this is awesome!"
Re:Double Pendulum in fewer than 100 lines of JavaScript by nileshtrivedi on Tuesday July 08, 2014 @11:24PM (#8007915)
Where can I find a good introduction to Lagrangian mechanics?
Re:Double Pendulum in fewer than 100 lines of JavaScript by ivan_ah on Wednesday July 09, 2014 @12:30AM (#8008072)
I highly recommend Classical Mechanics by Goldstein et al.

http://www.abebooks.com/9780201657029/Classical-Mechanics-Go...

Re:Double Pendulum in fewer than 100 lines of JavaScript by sigterm on Wednesday July 09, 2014 @12:29AM (#8008070)
Any undergraduate classical mechanics text book such as this one should cover it: http://www.amazon.com/Classical-Dynamics-Particles-Systems-T...
Re:Double Pendulum in fewer than 100 lines of JavaScript by codezero on Wednesday July 09, 2014 @11:56AM (#8010312)
I love how everyone has their favorite Classical Mechanics text and how they are almost all very different, though Goldstein and Marion almost always top the lists.

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 :)

Re:Double Pendulum in fewer than 100 lines of JavaScript by msds on Wednesday July 09, 2014 @03:34AM (#8008481)
Landau and Lifshitz's Classical Mechanics - dense, high level, the classic text.
Re:Double Pendulum in fewer than 100 lines of JavaScript by randlet on Wednesday July 09, 2014 @08:34AM (#8009278)
We used a combination of Goldstein (a classic as another poster pointed out) and Marion & Thornton's Classical Dynamics of Particles and Systems (which was good but not great).
Re:Double Pendulum in fewer than 100 lines of JavaScript by thrownaway2424 on Tuesday July 08, 2014 @11:41PM (#8007949)
College.
Re:Double Pendulum in fewer than 100 lines of JavaScript by ShardPhoenix on Tuesday July 08, 2014 @09:21PM (#8007639)
You can get a vigorous chaotic motion going with a high top mass, low bottom mass, and high angles.
Re:Double Pendulum in fewer than 100 lines of JavaScript by grimtrigger on Tuesday July 08, 2014 @10:09PM (#8007749)
That was certainly my goal =)

1. Max Mass 1

2. Min Mass 2

3. Max Phi 1

4. Min Phi 2

Re:Double Pendulum in fewer than 100 lines of JavaScript by aceperry on Tuesday July 08, 2014 @10:59PM (#8007873)
Just the opposite, and you get a "hinky" kind of motion.
Re:Double Pendulum in fewer than 100 lines of JavaScript by zaroth on Wednesday July 09, 2014 @02:24AM (#8008333)
I was playing with the gauges in the simulator, and noticed certain values will create a system where it's possible for the lower pendulum to flip full-circle around the upper. Other parameters will create a system where that is clearly impossible. But there are some where it seems like it might be just barely possible for the lower to complete the loop.

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?

Re:Double Pendulum in fewer than 100 lines of JavaScript by aceperry on Tuesday July 08, 2014 @11:09PM (#8007889)
I remember about 3-4 years ago, a couple of programmers talking. One said to the other, "you know, javascript is getting respectable now." I'm blown away by how such a complicated system can be described and implemented in so few lines of code. When I went to school, seriously complicated programs took a lot of time to write and render. The easy way involved Matlab because it had a lot of graphics libraries. It looks like javascript really has turned the corner into becoming a language with serious possibilities.
Re:Double Pendulum in fewer than 100 lines of JavaScript by r4pha on Thursday July 10, 2014 @01:03PM (#8015767)
If you're interested in simulations in javascript, I wrote a small blog post and a toy project [0] about it. I would love some feedback!

[0]: http://rbaron.net/blog/2014/01/02/Simulating-dynamic-systems...

Re:Double Pendulum in fewer than 100 lines of JavaScript by callumprentice on Wednesday July 09, 2014 @02:11PM (#8011288)
I love these short JavaScript examples that even idiots like me can experiment with. Thank you for sharing.

I hacked together a WebGL version here:

https://dl.dropboxusercontent.com/u/1109/dp/index.html

Lots of fun - thank you.

Re:Double Pendulum in fewer than 100 lines of JavaScript by epidemian on Tuesday July 08, 2014 @10:50PM (#8007862)
I really like the simplicity of the demo page: http://www.physicsandbox.com/projects/double-pendulum-sim.ht...

Hint: a big Mass1 and Phi1 makes for a nice wobbly configuration.

Re:Double Pendulum in fewer than 100 lines of JavaScript by wiso on Wednesday July 09, 2014 @04:49AM (#8008674)
Very nice simulation, based on this code I have made double Pendulum in fewer than 100 lines of C#: https://github.com/wieslawsoltes/DoublePendulumSim
Re:Double Pendulum in fewer than 100 lines of JavaScript by chaffneue on Wednesday July 09, 2014 @12:05AM (#8008007)
Anyone reminded of those pandemonium toys in the 90s? like a pendulum stick man sort of thing...

https://www.youtube.com/watch?v=87HvVqa4t7k&t=30s

Re:Double Pendulum in fewer than 100 lines of JavaScript by callesgg on Wednesday July 09, 2014 @05:28AM (#8008767)
What is that causes the difference between 2 runs.ww In reality there is lots of variables but that is not the case here.

Undetermenistic fpu?

Re:Double Pendulum in fewer than 100 lines of JavaScript by saganus on Wednesday July 09, 2014 @01:49AM (#8008248)
Very nice.

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!

Re:Double Pendulum in fewer than 100 lines of JavaScript by olaf on Tuesday July 08, 2014 @09:24PM (#8007652)
Ain't that a chaotic system which is not deterministic?
Re:Double Pendulum in fewer than 100 lines of JavaScript by gibybo on Tuesday July 08, 2014 @09:40PM (#8007678)
Chaotic systems are deterministic (as shown by a deterministic computer algorithm simulating one). They just aren't predictable except by simulating them (which constrains us by time and accuracy).
Re:Double Pendulum in fewer than 100 lines of JavaScript by ars on Wednesday July 09, 2014 @12:16AM (#8008031)
No, that's not it.

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.

Re:Double Pendulum in fewer than 100 lines of JavaScript by gibybo on Wednesday July 09, 2014 @01:31AM (#8008213)
I don't think that conflicts with what I said?
Re:Double Pendulum in fewer than 100 lines of JavaScript by saganus on Wednesday July 09, 2014 @01:54AM (#8008260)
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

Re:Double Pendulum in fewer than 100 lines of JavaScript by papaf on Wednesday July 09, 2014 @02:11AM (#8008304)
For me, the most interesting part of Chaos is that it is deterministic.
Re:Double Pendulum in fewer than 100 lines of JavaScript by throwaway283719 on Wednesday July 09, 2014 @06:18AM (#8008893)
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.
Re:Double Pendulum in fewer than 100 lines of JavaScript by papaf on Wednesday July 09, 2014 @03:58PM (#8011897)
I would disagree with the phrase "effectively random" because its too general. Chaotic attractors survive the presence of noise and Chaotic systems look nothing like random data when viewed in a certain way.

Another way of looking at real systems, is that the system is deterministic and the errors are random.

Re:Double Pendulum in fewer than 100 lines of JavaScript by olaf on Wednesday July 09, 2014 @04:39AM (#8008637)
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 by lessnonymous on Tuesday July 08, 2014 @10:56PM (#8007870)
Want to play with it? Here's a fiddle: http://jsfiddle.net/GBQ44/
Re:Double Pendulum in fewer than 100 lines of JavaScript by skiplecariboo on Wednesday July 09, 2014 @04:54AM (#8008693)
Nice ! Can you add friction ? :)
Re:Double Pendulum in fewer than 100 lines of JavaScript by SimeVidas on Wednesday July 09, 2014 @07:59AM (#8009157)
Polluting the browser's global environment with variables like "time", "init", "run", "animate". What can go wrong? :-P
Re:Double Pendulum in fewer than 100 lines of JavaScript by CmonDev on Wednesday July 09, 2014 @04:16AM (#8008579)
Wow, using a high-level scripting language results in short programs?! Who would've guessed.