chaos

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

In [1]:
# Load graphics and math libraries.
import numpy as np
from matplotlib.pyplot import plot
import matplotlib.pyplot as plt
%matplotlib inline
In [2]:
# What version of python is this?
import sys
print(sys.version)
3.6.6 | packaged by conda-forge | (default, Oct 12 2018, 14:43:46) 
[GCC 7.3.0]
In [5]:
# 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:]))
first 6: [0.314159   0.7110283  0.67804128 0.7203943  0.66470697 0.73547633]
last 6 : [0.47942702 0.82360328 0.47942702 0.82360328 0.47942702 0.82360328]
In [6]:
# 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()
In [ ]: