>
 

Documentation 

This is the cobweb program for Closer and Closer to help students produce the diagrams.  It is 

a modification of a program written by Professor Steve Slack. 

Last Modified by Carol Schumacher:  February, 2007. 

 

>
 

Cobweb Diagrams 

> with(plots); -1
 

First input the function you wish to iterate. 

> `:=`(f, proc (x) options operator, arrow; `+`(`*`(2, `*`(x, `*`(`+`(1, `-`(x)))))) end proc); -1
 

Next, input the starting value for x[0]. 

> `:=`(x[0], .95); -1
 

Input the desired number of steps. 

> `:=`(numsteps, 15); -1
 

Finally, input the interval you want the iteration function to be defined over, with left endpoint 

a and right endpoint b. 

> `:=`(a, 0); -1; `:=`(b, 1); -1
 

> plot({x, f(x)}, x = a .. b)
 

Plot_2d
 

The next five lines draw the first line of the 

"cobweb graph," the graph of the function f(x) and the diagonal line. 

> `:=`(v, [[x[0], 0], [x[0], f(x[0])]]); -1
 

> `:=`(diagonal, plot(x, x = a .. b, color = red)); -1
 

> `:=`(graph, plot(f(x), x = a .. b, color = aquamarine)); -1
 

> `:=`(step0, plot(v, x = a .. b, color = black)); -1
 

> `:=`(frame0, display([diagonal, graph, step0])); -1
 

The following loop will calculate x[1] = f(x[0]), x[2] = f(x[1]), etc.  It will also create the graphics 

for the cobweb diagram. 

> for k from 1 to numsteps
 

> do
 

> x[k] := evalf(f(x[k-1]),20):
 

> if x[k]<a or x[k]>b then v:=[op(v),[x[k-1],f(x[k-1])]] else v:= [ op(v), [x[k], x[k]], [ x[k], f(x[k]) ] ] fi:
 

> if x[k]<a or x[k]>b then numsteps:=k-1 else step||k:=plot(v, x = a..b, color=black) fi:
 

> if x[k]<a or x[k]>b then numsteps:=k-1 else frame||k:=display([step||k, diagonal, graph]) fi:
 

> if x[k]<a or x[k]>b then break fi:
 

> od:
 

The following line puts the various stages into film so you can view an animated version  

of the procedure. 

> `:=`(film, [seq(frame || k, k = 0 .. numsteps)]); -1
 

Finally, to view an animation of the cobweb graph, execute the following line, then click  

on the graph.  Play the film using the playback toolbar that appears at the top of the screen. 

> display(film, insequence = true)
 

Plot_2d
 

>
 

>