# HenonMapLine.py: Plot out Henon map iterating a line
#
# The Henon 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 ) = y_n + 1 - a * x_n^2
#     g( x_n , y_n ) = b * x_n
#
# The state space is R^2
# and the control parameters range in
#       a in [0,2]
#       b in [0,1]
# Henon's original parameters for a chaotic attractor: (a,b) = (1.4,0.3)
# 

def HenonMap(a,b,x,y):
	return y + 1.0 - a *x*x, b * x

# Import plotting routines
from pylab import *

# Simulation parameters
#
# Control parameters:
a = 1.4
b = 0.3
# The number of iterations to generate
nIterates = 7

# Setup the plot
xlabel('x(n)') # set x-axis label
ylabel('y(n)') # set y-axis label
title('Henon with (a,b) = (' + str(a) + ',' + str(b) + ')') # set plot title
axis([-2.0, 2.0, -1.0, 1.0])

# An array of initial conditions on a straight line
nICs = 50
xmin = -0.4
xmax =  0.4
xinc = (xmax - xmin) / nICs
ymin =  0.0
ymax =  0.0
yinc = (ymax - ymin) / nICs
x = []
y = []
for i in xrange(0,nICs):
	x.append(xmin + (xmax - xmin) * xinc * i)
	y.append(ymin + (ymax - ymin) * yinc * i)
# Colors and line styles for the lines
c = ['r-', 'g-', 'b-', 'c-', 'y-', 'k-', 'm-']
nColors = len(c)
plot(x,y, c[nColors-1], label = '0 Its')
for n in xrange(0,nIterates):
  # We iterate over the entire set of ICs
  for i in xrange(0,nICs):
  	x[i], y[i] = HenonMap(a,b,x[i],y[i])
  # Plot the sequence of iterates that define the current curve
  plot(x,y, c[n % nColors], label = str(n+1) + ' Its')

legend()

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

# Display the plot in a window
show()
