import numpy as np
#from numpy import cos
import matplotlib.pyplot as plt
%matplotlib inline
# here is the function we want to iterate
# mu is a possible parameter
def func(x,mu):
return x**3 +mu # f(x) = x^3 + mu
# return f^n(x); iterate the function func() n times
def func_n(x,mu,n):
for i in range(0,n):
x = func(x,mu)
return x
# here is "plotting graphical" or "cobweb" for an iterated map
# connect up (x, f^1(x)), (f^1(x),f^1(x)), (f^1(x), f^2(x)), (f^2(x),f^2(x))
# ... (f^i(x), f^(i+1)(x)),(f^(i+1),f^(i+1)) to i=n
# initial condition: x0, mu is a parameter to pass to function
# connect up points n times, this is 2n pairs of points
# ax is plotting axis
def plot_graphical(x0,mu,n,ax):
xv = np.linspace(0.0,1.0,2*n) # create array for points xvalue
yv = np.linspace(0.0,1.0,2*n) # create array for points yvalue
x =x0
for i in range(0,n): #iterate
xv[2*i] = x # first point is (x,f(x))
x = func(x,mu)
yv[2*i] = x
xv[2*i+1] = x #second point is (f(x),f(x))
yv[2*i+1] = x
ax.plot(xv,yv,'b-') # connect up all these points
fig,ax = plt.subplots(1,1,figsize=(3,3))
ax.set_xlabel('x')
ax.set_ylabel('f(x)')
fac=1.01
xmax = 1.3; xmin =-1.0
ymax = 1.5; ymin =-1.0
ax.set_xlim([xmin*fac,xmax*fac])
ax.set_ylim([ymin*fac,ymax*fac])
xcon = np.arange(xmin, xmax, 0.01) # to plot function
ax.plot(xcon,xcon, 'g:',label='y=x') #y=x plotted
mu=0.43
ycon = func(xcon,mu) # function computed for xcon
ax.plot(xcon,ycon, 'r-',label='y=f(x)') # function f(x) plotted
plot_graphical(-0.9,mu,13,ax) # cobweb plot
ax.legend()
<matplotlib.legend.Legend at 0x7fc377e36650>
# this illustrates intermitency where iterates spend a lot of time near a fixed point, behaving as if
# it were at the fixed point
fig,ax = plt.subplots(1,1,figsize=(3,3))
ax.set_xlabel('x')
ax.set_ylabel('f(x)')
fac=1.01
xmax = 1.3; xmin =-1.0
ymax = 1.5; ymin =-1.0
ax.set_xlim([xmin*fac,xmax*fac])
ax.set_ylim([ymin*fac,ymax*fac])
xcon = np.arange(xmin, xmax, 0.01) # to plot function
ax.plot(xcon,xcon, 'g:',label='y=x') #y=x plotted
mu=0.38
ycon = func(xcon,mu) # function computed for xcon
ax.plot(xcon,ycon, 'r-',label='y=f(x)') # function f(x) plotted
plot_graphical(-0.9,mu,13,ax) # cobweb plot
ax.legend()
<matplotlib.legend.Legend at 0x7fc377fc32e0>
# here the function just converges onto a fixed point
# here is another function we want to iterate
# mu is a possible parameter
def func(x,mu):
return mu*np.sin(x*(1.0*np.pi))
xmax = 1.00; xmin =-0.01
ymax = 1.01; ymin =-0.01
fig,ax = plt.subplots(1,1,figsize=(3,3))
ax.set_xlabel('x')
ax.set_ylabel('f(x)')
fac=1.01
xmax = 1.3; xmin =-1.0
ymax = 1.5; ymin =-1.0
ax.set_xlim([xmin*fac,xmax*fac])
ax.set_ylim([ymin*fac,ymax*fac])
xcon = np.arange(xmin, xmax, 0.01) # to plot function
ax.plot(xcon,xcon, 'g:',label='y=x') #y=x plotted
mu=0.84
ycon = func(xcon,mu) # function computed
ax.plot(xcon,ycon, 'r-',label='y=f(x)') # function f(x) plotted
plot_graphical(-0.9,mu,13,ax) # cobweb plot
ax.legend()
<matplotlib.legend.Legend at 0x7fc377fd9210>
# here is an example of converging onto a period 2 orbit