chaos

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

In [1]:
# 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
In [11]:
# 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:]))
1 0.689481992701
2 0.685109038215
3 0.690350860708
4 0.684052959449
5 0.691598425978
6 0.682528138121
7 0.693387132141
8 0.680324534791
9 0.695945798887
10 0.677136780475
11 0.69959219521
12 0.672521457956
13 0.704756308943
14 0.665839532634
15 0.71199119813
16 0.656191142129
17 0.721933846785
18 0.642385176483
19 0.735124676857
20 0.623092436265
21 0.75151440683
22 0.597569610103
23 0.76953654779
24 0.567520158097
25 0.785411290402
26 0.539329264996
27 0.795050268528
28 0.521425084934
29 0.798531090354
30 0.514813401895
31 0.799297801998
32 0.513346642302
33 0.799429974846
34 0.513093408525
35 0.79945140049
36 0.513052347982
37 0.799454835879
38 0.51304576406
39 0.799455385728
40 0.513044710267
41 0.799455473709
42 0.51304454165
43 0.799455487786
44 0.513044514671
45 0.799455490038
46 0.513044510355
47 0.799455490399
48 0.513044509664
49 0.799455490456
In [12]:
# ... 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()