An example of chaos using an jupyter notebook and numpy (numerical python).
Adapted from chapter 1 of Zelle's "Python: an Intro to CS" book.
I launched this notebook with
$ jupyter notebook chaos_notebook.ipynb
and converted it into an html file with
$ jupyter nbconvert chaos_notebook.ipynb
Tested with python 3.something.
Jim Mahoney | mahoney@marlboro.edu | Aug 2018
# set up the graphics and math libraries
% matplotlib inline
from numpy import *
from matplotlib.pyplot import plot
import matplotlib.pyplot as plt
import numpy as np
# calculate the numbers ...
n = 50 # number of points
x = zeros(n) # a numpy function to make an array of zeros
alpha = 3.2 # 0 to 4 ... change this to rerun. Big is chaos; small isn't.
x[0] = 0.314159 # Shouldn't matter particularly what this is.
for i in range(1, n):
x[i] = alpha * x[i-1] * (1.0 - x[i-1])
print(i, x[i])
# ... or look at only first and last few points.
# count = 5
# print("x first {} : {}".format(count, x[:5]))
# print("x last {} : {}".format(count, x[-5:]))
# ... and plot them.
plt.figure(dpi=200, figsize=(8,8)) # inches
plt.title(r"chaos with $\alpha$ = {}".format(alpha), fontsize=16)
plt.ylabel(r"$x_i$", fontsize=16)
plt.xlabel(r"$i$", fontsize=16)
plot(x)
plt.show()