# LogisticBifn.py:
#	Plot a bifurcation diagram for the Logistic map.

# Import modules
# SciPy for the arange function
from scipy import arange
# Plotting: Use this for Linux/Unix/OS X
from pylab import *
# OR
# Plotting: Use this for Windows
#from matplotlib.matlab import *

# Define the Logistic map's function 
def f(r,x):
	return r * x * (1.0 - x)

# Setup parameter range
rlow  = 2.9
rhigh = 4.0

# Setup the plot
# It's numbered 1 and is 8 x 6 inches.
figure(1,(8,6))
# Stuff parameter range into a string via the string formating commands.
TitleString = 'Logistic map, r x (1 - x), bifurcation diagram for r in [%g,%g]' % (rlow,rhigh)
title(TitleString)
# Label axes
xlabel('Control parameter r')
ylabel('{X_n}')

# Set the initial condition used across the different parameters
ic = 0.2
# Establish the arrays to hold the set of iterates at each parameter value
rsweep = [ ] # The parameter value
x = [ ] # The iterates
# The iterates we'll throw away
nTransients = 10
# This sets how much the attractor is filled in
nIterates = 250
# This sets how dense the bifurcation diagram will be
nSteps = 200.0
# Sweep the control parameter over the desired range
for r in arange(rlow,rhigh,(rhigh-rlow)/nSteps):
	# Set the initial condition to the reference value
	state = ic
	# Throw away the transient iterations
	for i in range(nTransients):
		state = f(r,state)
	# Now store the next batch of iterates
	for i in range(nIterates):
		state = f(r,state)
		rsweep.append(r)
		x.append( state )

# Plot the list of (r,x) pairs as pixels
plot(rsweep, x, 'k,')

# Use this to save figure as a bitmap png file
#savefig('LogisticCobweb', dpi=600)

# Display plot in window
show()
