"""
plot_chaos.py
An elaboration of the code in Zelle's
"Python Programming" textbook, chapter 1.
Adapted from Noah Bedford's homework,
which uses matplotlib and some hints from Jim
to graph the logistic map.
It turns out that type of behavior of x[n]
for large n doesn't really depend on x[0],
but does depend on r in an interesting way.
This requires matplotlib, which since it
isn't one of the standard python libraries
of the standard python library
See for example http://en.wikipedia.org/wiki/Logistic_map
Jim M | Sep 2012 | GPL
"""
# The plotting library being used here, matplotlib,
# isn't part of the standard python library,
# so it needs to be installed before running this, i.e.
# Mac (python 2.6 & macports $ sudo port install py26-matplotlib
# Ubuntu : $ sudo apt-get install python-matplotlib
import matplotlib.pyplot as plt
def main():
print "Chaos (logistic map) plot :"
print " x[n+1] = r*x[n]*(1-x[n])"
n = 100
x0 = 0.3
print " N = number of points = ", n
print " x0 = initial x = ", x0
while True:
r = input("Enter 0 < r < 4 (-1 to quit) : ")
if r < 0.0:
print "Bye"
return False
x = x0
result = []
for i in range(n):
x = r * x * (1.0 - x)
result.append(x)
plt.plot(result)
plt.show()
main()
syntax highlighted by Code2HTML, v. 0.93pm6