# BakersMap.py: Plot out iterates of the Dissipative Baker's Map
#
# The Dissipative Baker's Map is given by
#     x_n+1 = f( x_n , y_n )
#     y_n+1 = g( x_n , y_n )
# with
#     f( x_n , y_n ) = 2 * x_n (mod 1)
#     g( x_n , y_n ) = a * y_n,       if x_n <= 1/2
#                    = 1/2 + a * y_n, if x_n > 1/2
#
# The state space is the unit square:
#       (x,y) in [0,1] x [0,1]
# and the control parameter ranges in
#       a in [0,1/2]
# 

def BakersMap(a,x,y):
	# We'll assume, for now, that (x,y) is in [0,1] x [0,1]
	# Calculate next y first, since it depends on current x
	y = a * y
	if x > 0.5:
		y = y + 0.5
	# Try using 2.0 instead of the number below. What happens?
	x = 1.9999999999999 * x
	# Here's one way to implement (mod 1)
	while x > 1.0:
		x = x - 1.0
	return x,y

# Import plotting routines
from pylab import *

# Simulation parameters
#
# Control parameter of the map:
a = 0.3
# Set up arrays of iterates (x_n,y_n) and set the initital condition
x = [0.1]
y = [0.3]
# The number of iterations to generate
N = 1000000

# The main loop that generates iterates and stores them
for n in xrange(0,N):
  # at each iteration calculate (x_n+1,y_n+1)
  xtemp, ytemp = BakersMap(a,x[n],y[n])
  # and append to lists x and y
  x.append( xtemp )
  y.append( ytemp )

# Setup the plot
xlabel('x(n)') # set x-axis label
ylabel('y(n)') # set y-axis label
title("Dissipative Baker's Map with a =" + str(a)) # set plot title
# Plot the time series
plot(x,y, 'r,')
# Make sure the iterates appear in a unit square
axis('equal')
axis([0.0, 1.0, 0.0, 1.0])

# Use command below to save figure
#savefig('BakersMapIterates', dpi=600)

# Display the plot in a window
show()
