An example of chaos in jupyter notebook using numpy (numerical python), adapted from chapter 1 of Zelle's "Python: an Intro to CS" book.
For more information on this "logistic map", see https://en.wikipedia.org/wiki/Logistic_map .
Jim Mahoney | mahoney@marlboro.college | Sep 2019
# Load graphics and math libraries.
import numpy as np
from matplotlib.pyplot import plot
import matplotlib.pyplot as plt
%matplotlib inline
# What version of python is this?
import sys
print(sys.version)
# Calculate the numeric values.
n = 50 # number of points
x = np.zeros(n) # a numpy function to initialize an array of zeros
alpha = 3.3 # between 0 and 4. Big gives chaos; small doesn't.
x[0] = 0.314159 # starting value (anything from 0 to 1 should work.)
for i in range(1, n):
x[i] = alpha * x[i-1] * (1.0 - x[i-1])
# Look at the first few and last few values
count = 6
print("first {}: {}".format(count, x[:count]))
print("last {} : {}".format(count, x[-count:]))
# And plot them. In xkcd.org style of course. 😁
with plt.xkcd():
plt.figure(dpi=200, figsize=(6, 3)) # 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()