Chaos Theory and the Lorenz Attractor (in Python)

Ishaan Singh
6 min readSep 20, 2020

--

Chaos Theory has always been an interesting theory to study. You must’ve heard the phrase “A butterfly flaps its wings in Tokyo and a tornado occurs in Texas” (or a 100 of the other variations of the same) but the basic principle holds true: a minute change may cause an incalculable impact to a system causing it to behave completely differently. Nonlinear objects are the subject of Chaos Theory; objects which are virtually impossible to predict or control (the stock market, weather, brain states etc). Complex mathematical concepts of Calculus, fractals and gravity are included to create the ever-changing equations of Chaos Theory. This article acts as a good entry point for high school students to understand the constantly evolving mathematical theorem and apply it in their research or for a person of any age to learn a fascinating theory that holds great significance in the worlds of mathematics, physics and Computer Science.

The Butterfly Effect Source: Live Science

Historical Significance:
Henri Poincare was a French Mathematician who, in the 1900s, had been studying orbits in the solar system. While adding more elements to Newton’s equations, he found very different results as he changed the starting points of the orbits, and noticed that very small changes to the initially proposed equation resulted in very different outcomes; making it impossible to predicts how orbits might work. Before Poincare there were scientists who noticed these findings but Poincare is unanimously regarded as the ‘father of Chaos Theory.’

Few decades later Ed Lorenz, a scientist working at the Massachusetts Institute of Technology (MIT) in the 1960s, was developing an algorithm to predict weather patterns by generating a series of variables that he believed could predict the weather patterns. After an initial run, he re-entered the data but made a decimal error, resulting in his findings being drastically different. This was the birth of the concept that added complexity to systems that were considered deterministic and simple.

Applications of Chaos Theory

  • Cryptography: The method of protecting and securing systems for secure communication through the use of codes, to be processed for only the intended reader to read and process. The use of chaotic maps are prominent in cryptographic systems and the root cause of the design of chaos based cryptographic algorithms.
  • Robotics: used to build predictive models rather than the typical trial and error methods used.
  • Biology: used to create chaotic models in varying populations, used in models in the field of hydrology and in cardiotocography.
  • Celestial Mechanics: used for the observation of asteroids
  • Electrical engineering and quantum physics: study of large arrays of Josephson junctions
  • Physics: The Double Pendulum

Chaotic Dynamics

Now the fun stuff. If you’d like to go deeper into chaos theory I’ll be putting a list of great sites to visit that explain the topic and the math in great detail.

I, on the other hand, will be showing the graphing procedure in Python and briefly explaining the maths behind it.

Lorenz Attractor

The Lorenz Attractor is the paradigm for chaos. It not only *helps* scientists attempt to understand deterministic chaos but it’s also really pretty and very satisfying to watch. (You’ll see)

We will be using Python to graph a simple Lorenz Attractor

Make sure you have the following packages:
– matplotlib
– numpy
– scipy

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

There are certain constants we must define before hand for simplicity

prandtl = 10 
rho = 28
beta = 8/3

Those constants may seem familiar. ‘prandtl’ is the ratio of momentum diffusivity (kinematic viscosity) to thermal diffusivity. It is used in fluids and flow rates to signify the rate of heat transfer. ‘rho’ is typically associated with density. However, here the ‘rho’ value is the Rayleigh number, a measure of the instability of a layer of fluid (due to differences of temperature and density at the top and bottom). We use it to describe natural convection and heat transfer by natural convection. Lastly, beta is the measure of compressibility which is the measure of relative volume change of a fluid.
Beta is a general term commonly used in multiple fields (stock markets, radiation etc.) but the equations and values differ amongst different disciplines.
If you’ve noticed, the Lorenz Attractor equations relate the properties of a 2-D fluid layer that is cooled from below and warmed from above, uniformly. (This is why we can observe the flow on the Python simulator and when drawn)

We must now define the actual function which will contain the logic of the code.

def lorenz_attr(x, y, z):
x_dot = prandtl*(y - x)
y_dot = rho*x - y - x*z
z_dot = x*y - beta*z
return x_dot, y_dot, z_dot

Set the initial values

dt = 0.01
num_steps = 10000

xs = np.empty(num_steps + 1)
ys = np.empty(num_steps + 1)
zs = np.empty(num_steps + 1)
xs[0], ys[0], zs[0] = (0., 1., 1.05)

We then step through ‘time’ while calculating the partial derivatives at the point we are currently at and using them to estimate the next point.

for i in range(num_steps):
x_dot, y_dot, z_dot = lorenz(xs[i], ys[i], zs[i])
xs[i + 1] = xs[i] + (x_dot * dt)
ys[i + 1] = ys[i] + (y_dot * dt)
zs[i + 1] = zs[i] + (z_dot * dt)

Now to plot

fig = plt.figure()
ax = fig.gca(projection='3d')

ax.plot(xs, ys, zs, lw=0.5)
ax.set_xlabel("X Axis")
ax.set_ylabel("Y Axis")
ax.set_zlabel("Z Axis")
ax.set_title("Lorenz Attractor")

plt.show()
Lorenz attractor generated in Python

So what exactly happened

Essentially, the code uses multivariable calculus and differential equations to come up with the attractor. It’s a system of three first order differential equations with with two non-liner equations (2nd and 3rd) because of the terms xy and xz in them. Using the same mathematical concepts, a python code can be written to express the equation in terms of code.

The slightly complicated part

We first define the constants (sigma, rho, beta)

Constants used in calculation

Sigma is Prandtl number
Rho is the Rayleigh number divided by the critical Rayleigh number
Beta is a geometric factor

Next we define the fixed points of the system

Lorenz equations

Equate all the values to 0 and solve for x, y and z

Lastly, we use basic algebra and solve the equation by inputting the necessary values.

Source: http://www.pyrunner.com/weblog/2015/10/24/lorenz-attractor/#fn:1

Clark University on the process of the Lorenz Attractor

The first image shows the two loops that the flow line runs around. The left loop flows clockwise while the right one flows counterclockwise. They two loops join together in the middle, the left loop coming in from the back and the right loop coming in from the front. As soon as they join they separate again, the left half of the flow going to the left loop, and the right half going to the right loop (https://mathcs.clarku.edu/~djoyce/ma131/lorenz.pdf)

The Lorenz Attractor is just one of millions of mathematical equations that can be graphed on Python and the capability of the programming language from a computational mathematics and computational physics perspective is boundless. At my age I was simply fascinated by the concept of chaos and its applications in our daily lives. Using Python, I better understood the math behind the phenomenon and could write about it with better understanding.

--

--

Ishaan Singh
Ishaan Singh

Written by Ishaan Singh

Student, Co-Founder (Alumni Somnia), Founder (Inaya Connect), AI and Data Science Enthusiast, and social entrepreneur.