A Double Pendulum In 100 Lines Of JavaScript

### New Book Reviews!

 A Double Pendulum In 100 Lines Of JavaScript
Written by Mike James
Sunday, 13 July 2014

Sometimes it is better to go back to basics. Here we have a simulation of a double pendulum without a trace of a physics engine. And it all fits into 100 lines of JavaScript.

The double pendulum is interesting because it is a simple physical system with surprisingly complex behavior. It shows sensitivity to initial conditions and hence can be considered chaotic for some starting configurations.

Micael Oliveira has started physics-sandbox with the idea of creating some physics demos and the first is the double pendulum which you can see, in a simplified version, below.

From the chaos point of view, what is interesting is how often the bottom pendulum does a complete loop. It is very difficult to predict if or when a loop will occur - so as you watch lookout for a loop about to happen.

If you want to know the theory, go to Micael's web page explaining it. He takes the Lagrangian approach and, after working out the Lagrangian and putting it into the equations of motion, ends up with something that looks horrible. The equations give the angular acceleration as a function of the angular position and velocity.

The power of programming it is that even when you cannot find a closed form for position as a function of time, you can simulate the system. In this case nothing more than the basic Euler method is needed; the new position is the old position plus the relevant velocity multiplied by the time step and the new velocity is the old velocity multiplied by the relevant acceleration multiplied by the time step. This is a direct physical simulation but it seems to be stable enough if the step size is small.

Source:George Ioannidis

A double pendulum showing chaotic dynamics - the end of the pendulum is tracked by an LED.

This isn't ground breaking, but it is fun and a reminder that you can do things without having to resort to learning a complex framework.

Anyone want to try it in Box2D?

You can see a better version of the program at Micael's web site where you can try out different masses and starting configurations.

The code is on GitHub.

#### More Information

Double Pendulum

Physics Sandbox on GitHub

#### Related Articles

PhysicsJS - Physics In Pure JavaScript

JavaScript Physics Playground

The Magic Of N-Body Solutions

Tearable Cloth In JavaScript

Getting Started with Box2D in JavaScript

To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, FacebookGoogle+ or Linkedin,  or sign up for our weekly newsletter.

 Quantum Computing From FutureLearn18/04/2018A new course on quantum computing for which the only prerequisite is high school math and a rough idea of what atoms, electrons and photons are, started this week on the FutureLearn platform. I&n [ ... ] + Full Story CockroachDB Adds GDPR Compliance06/04/2018CockroachDB 2.0 has been released with geo-partitioning features that keep data close to the customer, and support for JSON. The developers also say there are new benchmarks showing it can handle ten  [ ... ] + Full Story More News

#### Comments

or email your comment to: comments@i-programmer.info

Last Updated ( Sunday, 13 July 2014 )

 RSS feed of news items only Copyright © 2018 i-programmer.info. All Rights Reserved. Joomla! is Free Software released under the GNU/GPL License.