Iterated Functions

Math 242 Modern Computational Math, spring 2021

Define the Collatz function:

c[n_?OddQ]:=3n+1
c[n_?EvenQ]:=n/2

Create a module that produces the sequence of Collatz iterates, starting with n, until reaching 1:

colSeq[n_]:=Module[{seq={n}},
While[Last[seq]≠ 1,
AppendTo[seq,c[Last[seq]]]
];
seq   (* return the sequence *)
]

colSeq[27]

ListPlot[colSeq[27]]

colSeq[2^20]

Length of Collatz Sequence

lengthList=Table[Length[colSeq[n]],{n,1,100000}]

ListPlot[lengthList,AxesLabel->{"n","length"}]

Maximum of a Collatz Sequence

maxList=Table[Max[colSeq[n]],{n,1,10000}]

ListPlot[maxList,AxesLabel->{"n","max value in sequence"}]

Logistic Map

Define a function to iterate:

g[x_]:=1.3*x*(1-x)

NestList[g,0.2,30]

Define a new function that depends on r and x:

h[r_][x_]:=r*x(1-x)

NestList[h[0.001],0.01,100]

NestList[h[3.3],0.31,100]

ListPlot[NestList[h[3.3],0.31,100]]

NestList[h[3.8],0.5,100]

ListPlot[NestList[h[3.8],0.5,100000]]

The following module produces a “Cobweb Plot”. (Code modified from https://community.wolfram.com/groups/-/m/t/153946)

cobwebPlot[f_,start_,n_,xrange:{xmin_,xmax_}]:=Module[{cob, coor,x,g1,g2},
cob=NestList[f,N[start],n];
coor = Partition[Riffle[cob,cob],2,1];
coor[[1,2]]=0;
g1=Graphics[{Purple,Opacity[0.2],Line[coor]}];
g2=Plot[{x,f[x]},{x,xmin,xmax},PlotStyle->{Blue,Black}];
Show[{g1,g2},Frame->True]
]

Usage is: cobwebPlot[ function, starting value, number of iterates, {xmin, xmax} ], where xmin and xmax specify the domain to be plotted.

cobwebPlot[h[3.8],0.5,100,{0,1}]

cobwebPlot[h[3.2],0.5,1000,{0,1}]