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", ee https://en.wikipedia.org/wiki/Logistic_map .
Jim Mahoney | cs.bennington.college | Sep 2020
First, here is the graphics program as it is in Zelle's book, which is online at https://mcsp.wartburg.edu/zelle/python/ppics3/code/chapter01/ . It asks for a number (I typed 0.3) then prints out ten values.
# File: chaos.py
# A simple program illustrating chaotic behavior.
def main():
print("This program illustrates a chaotic function")
x = eval(input("Enter a number between 0 and 1: "))
for i in range(10):
x = 3.9 * x * (1 - x)
print(x)
main()
This program illustrates a chaotic function Enter a number between 0 and 1: 0.8 0.6239999999999999 0.9150336 0.303213732397056 0.8239731430433197 0.5656614700878675 0.9581854282490104 0.1562578420270566 0.5141811824452056 0.9742156868513775 0.09796598114189749
Notice how this notebook is made up of "cells", and that each cell can be one of several types. The ones we'll use most often are markdown cells, with formattable text (like this one), and python code cells (like the cell above this).
One of the things that these notebooks are often used for is numerical work, including making plots. After storing the x values in a list (which we'll be talking more about soon), we can use some graphics libraries to make plots.
And just to keep things very simple, I'll just put the values into the cells explicitly rather than asking for them with "input". Then to see a different plot, I edit the cell and evaluate it again.
Here we go.
# First I load some numerical utilities,
# defining the variables "np" and "plt"
# and letting us get to the functions they contain.
import numpy as np
import matplotlib.pyplot as plt
# Next we calculate the numeric values and remember them.
n = 50 # The number of points
xs = [] # "xs" is how I'm writing the plural of x; just an empty list for now.
alpha = 3.5 # A number between 0 and 4. Big gives chaos; small doesn't.
x = 0.314159 # The initial x value.
for i in range(n):
xs.append(x) # Store the last value of x at the end of the list.
x = alpha * x * (1.0 - x) # Calculate the next value of x.
# And let's see what we put into the list xs.
print(xs)
[0.314159, 0.7541209295164999, 0.6489789361358453, 0.7973184680574109, 0.5656060499319802, 0.8599354617431288, 0.42156262182916215, 0.8534665219698974, 0.4377149624627642, 0.861422009346448, 0.41780945905991623, 0.8513565024299174, 0.442920128700754, 0.8635966090236152, 0.412291270672849, 0.8480751257993578, 0.4509529737991642, 0.866580362272991, 0.40466593298530945, 0.8431899548325347, 0.4627722921571499, 0.870149342190686, 0.39546312566530867, 0.8367521466651653, 0.47809297100741055, 0.8733202872825124, 0.3872118708615584, 0.8304759327390757, 0.4927498025808326, 0.8748160212308409, 0.3832953258003864, 0.8273300665698672, 0.49999259631778215, 0.8749999998081492, 0.38281250050360827, 0.8269348148662412, 0.500897693899318, 0.8749971795098209, 0.38281990375887703, 0.826940887657224, 0.5008837959226726, 0.8749972661666847, 0.38281967628629415, 0.826940701070229, 0.5008842229429751, 0.8749972635242549, 0.38281968322262183, 0.8269407067598366, 0.5008842099218249, 0.8749972636048495]
Now that we have calculated a list of values, let's plot them.
(The specifics of all this aren't really the point right now. We'll be learning about these sorts of interfaces as the course goes on. Once you understand the syntax, it's mostly a matter of reading the documentation to see what's possible, and asking the software nicely to do something. Googling for example "jupyter plots" would lead you to the docs.)
# Use plt (i.e. the matplotlib library plotting routines) to graph the values.
plt.figure(dpi=200, figsize=(6, 3)) # dpi=dots_per_inches; figsize is in inches
plt.title(r"chaos with $\alpha$ = {}".format(alpha), fontsize=16)
plt.ylabel(r"$x_i$", fontsize=16)
plt.xlabel(r"$i$", fontsize=16)
plt.plot(xs)
plt.show()
Now edit the value of $\alpha$, re-evaluate the python cells, and see the new plot. 🙂