Visualizing the Butterfly Effect with Python
Tags: python, physics, science
Published: 2020-10-26 05:05:09

> _"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 :snake:. 

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 :smirk:.

# :question: Why a double pendulum? 

As the all knowing gods of [Wikipedia](https://en.wikipedia.org/wiki/Double_pendulum#:~:text=In%20physics%20and%20mathematics%2C%20in,strong%20sensitivity%20to%20initial%20conditions.) 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 :butterfly:. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/q6x51wf03yuw7dkkaiz9.png)

---

# :ghost: 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](#simulate). 

To model the double pendulum, we are going to use what is known as [Lagrangian mechanics](https://phys.libretexts.org/Bookshelves/Classical_Mechanics/Book%3A_Classical_Mechanics_(Tatum)/13%3A_Lagrangian_Mechanics/13.01%3A_Introduction_to_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](https://phys.libretexts.org/Bookshelves/Classical_Mechanics/Book%3A_Variational_Principles_in_Classical_Mechanics_(Cline)/03%3A_Review_of_Newtonian_Mechanics) just can't easily do. 

An important part of Lagrangian mechanics is the [Euler-Lagrange equation](https://en.wikipedia.org/wiki/Euler%E2%80%93Lagrange_equation) which is the second-order partial differential equation: 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/2w5puk3k3fvjs5h65p5b.png)

where L is the [Lagrangian](https://scholar.harvard.edu/files/david-morin/files/cmchap6.pdf) 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](https://en.wikipedia.org/wiki/Ordinary_differential_equation) which we can then plug into Python and solve numerically!

Check out [this](https://scienceworld.wolfram.com/physics/DoublePendulum.html) 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. 

---

# :computer: Simulating the double pendulum <a name="simulate"></a>

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](https://github.com/chris-greening/double-pendula/tree/master) for you to experiment with.

Essentially, we instantiate our [`DoublePendulum`](https://github.com/chris-greening/double-pendula/blob/master/double_pendula.py#L23) objects with parameters such as the length `L` of the rods, the mass `m` of the bobs, the initial conditions `y0`, etc as so:

```python
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](https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.odeint.html) 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 :movie_camera:.

Now that we have solved our system numerically, we can use [matploblib.animation.FuncAnimation](https://matplotlib.org/3.3.2/api/_as_gen/matplotlib.animation.FuncAnimation.html) to animate our double pendulum :raised_hands:

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/plg1m1rx0nq3nr4pzwun.gif)

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 :smiling_imp:! 

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.

# :butterfly: The Butterfly Effect

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/5r399hcyrhwu7tm7iukk.gif)

And there it is: The Butterfly Effect :butterfly::heart:. 
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 :boom:. 

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 :snowflake:. 

Weather is influenced by an infinite amount of interconnected variables :dizzy_face:. 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 :smile: 

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

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/gzhpxbhmmxzfl9y3b050.gif)

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

{% github chris-greening/double-pendula no-readme %}

or leave a comment below and let me know what you thought :speech_balloon: