Menu

[r7978]: / trunk / py4science / examples / logistic / exercise02.py  Maximize  Restore  History

Download this file

138 lines (112 with data), 3.9 kB

  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
from maplib import Logistic, Sine
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
def bifurcation_diagram(map_type, params,
ntransients=100, ncycles=200, dotcolor="0.5",
fig=None,
nboundaries=2):
"""Plot a bifurcation diagram for an iterated map object.
Parameters
----------
map_type : functor
An iterated map constructor.
"""
nparam = len(params)
if nboundaries>0:
boundaries = np.zeros((nparam, nboundaries))
bound_rng = range(nboundaries)
xs = []
ys = []
for idx,param in enumerate(params):
m = map_type(param)
y = m.trajectory(m.iterate_from(0.5, ntransients), ncycles)
xs.extend(param*np.ones_like(y))
ys.extend(y)
if nboundaries:
# the boundaries are the iterates of the map's maximum, we assume
# here that it's located at 0.5
boundaries[idx] = m.trajectory(0.5, nboundaries+1)[1:]
# Make final figure
if fig is None:
fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(xs, ys, '.', mfc=dotcolor, mec=dotcolor, ms=1, mew=0)
ax.set_xlim(params[0], params[-1])
if nboundaries>0:
bound_lines = ax.plot(params, boundaries)
def toggle(event):
if event.key!='t': return
for a in bound_lines:
v = a.get_visible()
a.set_visible(not v)
fig.canvas.draw()
fig.canvas.mpl_connect('key_press_event', toggle)
return fig
def cobweb(mu, walkers=10, steps=7):
f = plt.figure()
ax = f.add_subplot(111)
interval = np.linspace(0.0, 1.0, 100)
logmap = Logistic(mu)
logmap.plot(ax, interval, lw=2)
for x0 in np.random.rand(walkers):
logmap.plot_cobweb(ax, x0, steps, lw=2)
ax.set_title('Ex 2A: Random init. cond. for mu=%1.3f'%mu)
return f
def invariant_density(mu, x0,cycles=1000000,ret_all=False, bins=500):
transients = 1000
f = plt.figure()
ax = f.add_subplot(111)
logmap = Logistic(mu)
y0 = logmap.iterate_from(x0, transients)
y = logmap.trajectory(y0, cycles)
n, bins, patches = ax.hist(y, bins, normed=1)
ax.set_xlim(0,1)
if ret_all:
return f,logmap,n
else:
return f
# Exercise solutions
def ex2A():
cobweb(0.2)
cobweb(0.4)
cobweb(0.6)
def ex2B():
def rho(x):
return 1./(np.pi * np.sqrt( x*(1.-x)))
# Don't start from 0.5, which is a fixed point!
f = invariant_density(1.0, 0.567, bins=100)
ax = f.gca()
# avoid the edges: rho(x) is singular at 0 and 1!
x0 = np.linspace(0.001, 0.999, 1000)
l, = ax.plot(x0, rho(x0), 'r-', lw=3, alpha=0.7)
ax.set_title('Ex 2B: invariant density')
ax.legend((ax.patches[0], l), ('empirical', 'analytic'), loc='upper center')
ax.set_xlim(0,1)
ax.set_ylim(0,10)
def ex2CD(mu=0.9,x0=0.64):
fig, logmap, n = invariant_density(mu, x0, ret_all=True)
ax = fig.gca()
ax.set_xticks(np.linspace(0, 1, 10))
ax.grid(True)
# Now, iterate x=1/2 a few times and plot this 'orbit', which corresponds
# to peaks in the invariant density.
x0 = 0.5
pts = logmap.trajectory(x0,10)
pts_y = 0.5*np.linspace(1, max(n), len(pts))
ax.plot(pts,pts_y,'ro-')
ax.set_title('**Ex 2C/D: Analytics of cusps at mu=%0.2g' % mu)
def ex2E():
# Parameter grid to sample each map on
params = np.linspace(0.5, 1, 500)
fig = bifurcation_diagram(Logistic, params, nboundaries=8)
fig.gca().set_title('Ex 2E: Bifurcation diag. with boundaries (press t)')
fig = bifurcation_diagram(Logistic, params, dotcolor='blue')
fig = bifurcation_diagram(Sine, params, dotcolor='red', fig = fig)
fig.gca().set_title('Ex 2E: Bifurcation diag. logistic/sine maps')
if __name__=='__main__':
ex2A()
ex2B()
ex2CD()
ex2E()
plt.show()
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.