-
Notifications
You must be signed in to change notification settings - Fork 6
/
henon.py
137 lines (113 loc) · 3.88 KB
/
henon.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
###############################################################################
#
# CSCI 4446 - Chaotic Dynamics
#
# File: logistic.py
# Author: Ken Sheedlo
#
# Provides library functions and a main program for plotting the logistic map.
#
###############################################################################
from __future__ import division
import getopt
import matplotlib.pyplot
import numpy
import sys
from mpl_toolkits.mplot3d import Axes3D
from utils import split_dict
def hmap(x_start, y_start, a_ratio, b_ratio, steps, cutoff=0):
'''
Generates points along the Henon map.
Parameters:
x_start A starting value for x0 in the map.
y_start A starting value for y0 in the map.
a_ratio The ratio to use for a.
b_ratio The ratio to use for b.
steps The number of steps to iterate, as an integer.
cutoff The number of initial steps to throw away, as an integer.
Returns a vector of real-valued results.
'''
result = numpy.empty((steps+1, 2), dtype=numpy.float64)
result[0,:] = x_start, y_start
for i in xrange(steps):
x_i, y_i = result[i,:]
result[i+1,:] = (y_i + 1 - a_ratio*(x_i ** 2)), (b_ratio*x_i)
return result[cutoff:,:]
def bifurcate(a_start, a_end, a_delta, steps, cutoff):
'''
Generates a vs. X data for bifurcation plotting and analysis.
'''
ass = numpy.arange(a_start, a_end, a_delta, dtype=numpy.float64)
xs = numpy.array([
hmap(0.1, 0.1, a, 0.3, steps, cutoff=cutoff)[:,0] for a in ass
], dtype=numpy.float64)
return ass, xs
def plot_bifdiag(ass, xs, **kwargs):
'''
Plots a bifurcation diagram of the Henon map.
'''
figure = matplotlib.pyplot.figure()
axes = figure.gca()
opts, plot_args = split_dict(('title', 'filename'), kwargs)
plot_args.update(markersize=0.2)
axes.plot(ass, xs, 'k.', **plot_args)
axes.set_xlabel('a')
axes.set_ylabel('$x_n$')
axes.set_title(opts.get('title', 'Bifurcation Diagram for the Henon Map'))
if opts.get('filename') is not None:
figure.savefig(kwargs['filename'], dpi=220)
else:
figure.show()
def plot_time(ts, xs, **kwargs):
'''
Generates a 3D plot of the Henon map over time.
'''
figure = matplotlib.pyplot.figure()
axes = figure.add_subplot(111, projection='3d')
opts, plot_args = split_dict(('title', 'filename'), kwargs)
axes.plot(ts, xs[:,0], xs[:,1], '-', **plot_args)
figure.show()
def plot_ret1(xs, **kwargs):
'''
Plots the Henon map in the first return map space.
'''
figure = matplotlib.pyplot.figure()
axes = figure.gca()
opts, plot_args = split_dict(('title', 'filename'), kwargs)
plot_args.update(markersize=0.2)
axes.plot(xs[:,0], xs[:,1], '.', **plot_args)
axes.set_xlabel('$x_n$')
axes.set_ylabel('$y_n$')
axes.set_title(opts.get('title', 'Henon Map'))
if opts.get('filename') is not None:
figure.savefig(kwargs['filename'], dpi=220)
else:
figure.show()
def main(argv=None):
if argv is None:
argv = sys.argv
file_prefix = None
try:
options, args = getopt.getopt(argv[1:], 'f:')
for opt, arg in options:
if opt == '-f':
file_prefix = arg
except getopt.GetoptError as err:
print str(err)
return 2
ass, xs = bifurcate(0.0, 1.4, 0.001, 2000, 500)
zs = hmap(0.0, 0.0, 1.4, 0.3, 10000)
if file_prefix is not None:
# import pdb; pdb.set_trace()
plot_bifdiag(ass, xs, filename='{0}_bifurc.png'.format(file_prefix))
plot_ret1(
zs,
filename='{0}_ret1.png'.format(file_prefix),
title='Henon Map, $a=1.4$'
)
else:
plot_bifdiag(ass, xs)
plot_ret1(xs, title='Henon Map, $a=1.4$')
return 0
if __name__ == "__main__":
sys.exit(main())