-
Notifications
You must be signed in to change notification settings - Fork 6
/
logistic.py
187 lines (164 loc) · 5.85 KB
/
logistic.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
###############################################################################
#
# CSCI 4446 - Chaotic Dynamics
#
# File: logistic.py
# Author: Ken Sheedlo
#
# Provides library functions and a main program for plotting the logistic map.
#
###############################################################################
import getopt
import matplotlib.pyplot
import numpy
import sys
from utils import split_dict
'''
Logistic function implementation and plotting.
All function parameters are real numbers (numpy.float64) unless otherwise
specified. If a parameter is specified as an integer, it should be numpy.int32
unless otherwise stated.
'''
def lmap(start, ratio, steps, cutoff=0):
'''
Iterates the Logistic map and produces a vector of results.
Parameters:
start The starting value x_0 for the mapping.
ratio The scaling parameter R.
steps The number of steps to run the mapping, 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, dtype=numpy.float64)
result[0] = start
for i in xrange(steps):
x_i = result[i]
result[i+1] = ratio * x_i * (1-x_i)
return result[cutoff:]
def plot_time(ts, xs, **kwargs):
'''
Plots the specified vector in the time domain.
'''
figure = matplotlib.pyplot.figure()
axes = figure.gca()
opts, plot_args = split_dict(['title', 'filename'], kwargs)
axes.plot(ts, xs, '.', **plot_args)
axes.set_ylim((0.0, 1.0))
axes.set_aspect(ts[-1] - ts[0])
axes.set_xlabel('n')
axes.set_ylabel(r'$x_n$')
axes.set_title(opts.get('title', r'$x_n$'))
if opts.get('filename') is not None:
figure.savefig(kwargs['filename'], dpi=220)
else:
figure.show()
def plot_time2(ts, xs1, xs2, **kwargs):
'''
Plots 2 vectors in the time domain.
'''
figure = matplotlib.pyplot.figure()
axes = figure.gca()
opts, plot_args = split_dict(['title', 'filename'], kwargs)
axes.plot(ts, xs1, 'b-o', **plot_args)
axes.plot(ts, xs2, 'r-o', **plot_args)
axes.set_ylim((0.0, 1.0))
axes.set_xlabel('n')
axes.set_ylabel(r'$x_n$')
axes.legend(('$x_0 = {0}$'.format(xs1[0]), '$x_0 = {0}$'.format(xs2[0])))
axes.set_title(opts.get('title', r'$x_n$'))
if opts.get('filename') is not None:
figure.savefig(kwargs['filename'], dpi=220)
else:
figure.show()
def plot_ret1(xs, **kwargs):
'''
Plots the specified vector in the first return map space.
'''
figure = matplotlib.pyplot.figure()
axes = figure.gca()
opts, plot_args = split_dict(['title', 'filename'], kwargs)
axes.plot(xs[:-1], xs[1:], '.', **plot_args)
axes.set_ylim((0.0, 1.0))
axes.set_xlim((0.0, 1.0))
axes.set_aspect(1.0)
axes.set_xlabel(r'$x_n$')
axes.set_ylabel(r'$x_{n+1}$')
axes.set_title(opts.get('title', 'First Return Map Space'))
if opts.get('filename') is not None:
figure.savefig(kwargs['filename'], dpi=220)
else:
figure.show()
def plot_ret2(xs, **kwargs):
'''
Plots the specified vector in the second return map space.
'''
figure = matplotlib.pyplot.figure()
axes = figure.gca()
opts, plot_args = split_dict(['title', 'filename'], kwargs)
axes.plot(xs[:-2], xs[2:], '.', **plot_args)
axes.set_ylim((0.0, 1.0))
axes.set_xlim((0.0, 1.0))
axes.set_aspect(1.0)
axes.set_xlabel(r'$x_n$')
axes.set_ylabel(r'$x_{n+2}$')
axes.set_title(opts.get('title', 'Second Return Map Space'))
if opts.get('filename') is not None:
figure.savefig(kwargs['filename'], dpi=220)
else:
figure.show()
def main(argv=None):
'''
Main program function.
'''
if argv is None:
argv = sys.argv
start_x = 0.2
ratio = 3.0
steps = 100
cutoff = 0
delta = 0.0001
file_prefix = None
try:
options, args = getopt.getopt(argv[1:], 'x:R:m:k:f:d:')
for opt, arg in options:
if opt == '-x':
start_x = numpy.float64(arg)
elif opt == '-R':
ratio = numpy.float64(arg)
elif opt == '-m':
steps = numpy.int32(arg)
elif opt == '-k':
cutoff = numpy.int32(arg)
elif opt == '-f':
file_prefix = arg
elif opt == '-d':
delta = numpy.float64(arg)
except getopt.GetoptError as err:
print str(err)
return 2
result = lmap(start_x, ratio, steps, cutoff=cutoff)
res2 = lmap(start_x + delta, ratio, steps, cutoff=cutoff)
ts = numpy.array(range(cutoff, steps+1), dtype=numpy.int32)
if file_prefix is not None:
plot_time(ts, result, filename=('{0}_time.png'.format(file_prefix)),
title='$x_n$ (r={0}, $x_0$={1})'.format(ratio, start_x))
plot_time2(ts, result, res2, filename=('{0}_offset.png'.format(file_prefix)),
title='Chaotic Divergence (r={0})'.format(ratio))
plot_ret1(result, filename=('{0}_ret1.png'.format(file_prefix)),
title='First Return Map (r={0}, $x_0$={1})'.format(ratio, start_x))
plot_ret2(result, filename=('{0}_ret2.png'.format(file_prefix)),
title='Second Return Map (r={0}, $x_0$={1})'.format(ratio, start_x))
else:
plot_time(ts, result,
title='$x_n$ (r={0}, $x_0$={1})'.format(ratio, start_x)
)
plot_time2(ts, result, res2, title='Chaotic Divergence (r={0})'.format(ratio))
plot_ret1(result,
title='First Return Map (r={0}, $x_0$={1})'.format(ratio, start_x)
)
plot_ret2(result,
title='Second Return Map (r={0}, $x_0$={1})'.format(ratio, start_x)
)
return 0
if __name__ == "__main__":
sys.exit(main())