# TentBifn.py:
#	Plot a bifurcation diagram for the tent 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 Tent map's function, a in [0,2]
def TentMap(a,x):
	if x < 0.5:
		return a * x
	else:
		return a - a * x

# Setup parameter range
plow  = 0.1
phigh = 2.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 = 'Tent map bifurcation diagram for a in [%g,%g]' % (plow,phigh)
title(TitleString)
# Label axes
xlabel('Control parameter a')
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
psweep = [ ] # The parameter value
x = [ ] # The iterates
# The iterates we'll throw away
nTransients = 250
# 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 p in arange(plow,phigh,(phigh-plow)/nSteps):
	# Set the initial condition to the reference value
	state = ic
	# Throw away the transient iterations
	for i in range(nTransients):
		state = TentMap(p,state)
	# Now store the next batch of iterates
	for i in range(nIterates):
		state = TentMap(p,state)
		psweep.append(p)
		x.append( state )

# Plot the list of (p,x) pairs as pixels
plot(psweep, x, 'k,')

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

# Display plot in window
show()
