"It simply deals with unpredictability in complex systems. The shorthand is the Butterfly Effect. A butterfly can flap its wings in Peking and in Central Park you get rain instead of sunshine."- Dr. Ian Malcolm,Jurassic Park

In this post, I'm going to show you how we can visualize the Butterfly Effect using Python π.

To do so, we're going to use a double pendulum which is exactly as the name implies; a pendulum with a second pendulum attached to it.

Stick to the end to see 300 double pendulums descend into a beautiful cacophony of chaos π.

# β Why a double pendulum?

As the all knowing gods of Wikipedia suggest, a double pendulum is a *"simple physical system that exhibits rich dynamic behavior with a strong sensitivity to initial conditions"*.

The simplicity of the system coupled with its sensitivity to initial conditions will make it relatively easy for us to build our system while simultaneously allowing us to observe the butterfly effect in action π¦.

# π» Mathematical background

DO NOT BE ALARMED. This is not going to be an extensive dive into the math, just a brief introduction so you can get an idea of what it is we're doing. If you aren't interested and want to get to the simulation, then feel free to skip this section.

To model the double pendulum, we are going to use what is known as Lagrangian mechanics. The reason for doing this is to not only flex our fancy math muscles but to also utilize an established and expressive framework for modeling physical systems that Newtonian mechanics just can't easily do.

An important part of Lagrangian mechanics is the Euler-Lagrange equation which is the second-order partial differential equation:

where L is the Lagrangian describing the state of the system in terms of it's kinetic and potential energy.

After getting the Euler-Lagrange equations for our double pendulum, we end up with coupled second-order ordinary differential equations which we can then plug into Python and solve numerically!

Check out this link if you want to take a deeper look at the math behind modeling the double pendulum.

*TL;DR*: Using Lagrangian mechanics, we end up with second-order ordinary differential equations which can be solved numerically for our double pendulum system.

# π» Simulating the double pendulum

And now for the moment we've all been waiting for: Let's model our double pendulum. The full code for this is too much to be shown here explicitly but I published it at this repo for you to experiment with.

Essentially, we instantiate our `DoublePendulum`

objects with parameters such as the length `L`

of the rods, the mass `m`

of the bobs, the initial conditions `y0`

, etc as so:

```
double_pendulum = DoublePendulum(
L1=5,
L2=5,
m1=1,
m2=1,
g=9.81,
y0=[90, 0,-10,0]
)
```

Internally, our object then uses scipy.integrate.odeint and the ordinary differential equations we got to numerically solve for the system at different instants of time. Think of an old movie reel that is composed of a bunch of still images that are played in quick succession giving us a moving picture π₯.

Now that we have solved our system numerically, we can use matploblib.animation.FuncAnimation to animate our double pendulum π

Let's start with just one double pendulum and see how it looks:

Awesome! We can see right away that this is definitely going to be more exciting than just a single pendulum that lulls back.... and forth... and back... and forth... This is gonna be much more chaotic π!

Remember earlier how I said a double pendulum is sensitive to initial conditions? Because of this sensitivity, just the *slightest* change in initial angle or initial angular velocity can lead to *very* different movements, *very* quickly.

To show this, I'm going to create 10 double pendulum's just 1/2 a degree off of one another and see what happens.

# π¦ The Butterfly Effect

And there it is: The Butterfly Effect π¦β€οΈ.

In less than 15 seconds, 10 double pendulum's starting just 1/2 degree apart from one another quickly diverge and explode into absolute chaos π₯.

So how is our double pendulum related to the proverbial butterfly flapping it's wings in Peking and altering the weather in Central Park?

*Think about it:* if our simple system was sensitive to just one slight variation of angle, think of systems that are significantly more complicated such as weather βοΈ.

Weather is influenced by an infinite amount of interconnected variables π΅. That's why the weather can be so unpredictable (so next time, cut your weatherman some slack).

Also, think of historical events! Seemingly infinitesimal events that happened centuries ago may be having dramatic impacts on the time we're living in now. One thing leads to another which leads to another and suddenly, that one time you stubbed your toe might lead to a power outage in Australia twenty years from now!

*Moral of the story*: next time you're experiencing existential dread and down on your luck, just know that the little things you do now are having big impacts on the future π

And as promised: the delightfully chaotic 300 double pendulums (may take a moment to load):

# Final remarks

Check out the repo if you liked this tutorial and want to experiment with it on your own

## chris-greening / double-pendula

### Modeling a double pendulum using Python and Lagrangian mechanics

or leave a comment below and let me know what you thought π¬

## Discussion (2)

Such an incredible tutorial! Thanks!

Hey Isa thanks so much for the positive feedback, I'm glad you enjoyed it π. I will definitely be doing more like this in the future so keep an eye out!