def cobweb(g,z,x_0,n,huge):
"""
INPUT:
-"g"-function
-"z"-variable defining g (usually 'x' in the examples)
-"x_0"-initial value of z
-"n" - number of iterations to perform
-"huge" - any value of x with absolute value bigger than this is
so large that we cut short the loop
OUTPUT:
- "P" - plot showing the iterations graphically
RETURN:
- nothing
"""
# set up variable to hold x values and initialize xmin and xmax
x = RDF(x_0)
xmin = x-RDF(0.001)
xmax = x+RDF(0.001)
# dummy line to get plot structure P set up
P = plot(z, z, xmin, xmax, color='gray')
# loop to create cobweb
for i in range(n):
# new value and vertical and horizontals it gives
y = g(x);
P +=line( [ ( x, x ), ( x, y ) ], color='red')
P +=line( [ ( x, y ), ( y, y ) ], color='orange')
x = y
# update xmin and xmax and break if either is too big
if (y > xmax): xmax = y
if (y < xmin): xmin = y
if (xmin < -huge): break
if (xmax > huge): break
# add plot of function g and line y=x with suitable range
# now that we know xmin and xmax
P +=g.plot(z, xmin, xmax, color ='blue')
P +=plot(z, z, xmin, xmax, color='gray')
# show and tell time
P.show(ymin = xmin, ymax = xmax)
return