Fixed Point Iteration

Here we are going to illustrate fixed point iteration using mathematica.  Let us set up a function

  g(x) = cos(x).  

We will work in the interval a<=x<=b, where a = 0, and b = 1.  Our starting value is (Subscript[p, 0]) = 0.

Clear[g,a,b]
g[x_]=Cos[x];
a=0;
b=1;
Plot[{g[x],x},{x,a,b},AspectRatio->Automatic]

Now let us do the iteration.

Clear[p,k]
p[0]=0.;
Do[p[k]=N[g[p[k-1]]],{k,1,100}]

Lets write out the values:

TableForm[Table[{k,p[k]},{k,0,11}]]

Now lets do a nice plot.  First of all, we will plot the function.

ppp=Plot[{g[x],x},{x,a,b},AspectRatio->Automatic]

Now lets do a bunch of plots, and then make a movie.  What do you think the mathematica code is doing here?

ListAnimate[Flatten[Table[{
		Show[
			ppp,
			Epilog->{
					Table[Line[{{p[k],p[k]},{p[k],p[k+1]}}],{k,0,n-1}],          	
					Table[Line[{{p[k],p[k+1]},{p[k+1],p[k+1]}}],{k,0,n-1}]}],
		Show[
			ppp,
			Epilog->{
					Table[Line[{{p[k],p[k]},{p[k],p[k+1]}}],{k,0,n}],
					Table[Line[{{p[k],p[k+1]},{p[k+1],p[k+1]}}],{k,0,n-1}]}]},{n,0,10}]]]

Now lets try other functions.  When does the sequence p[k] converge?

Clear[p,g,a,b]
g[x_]=(1/2)+((Sin[x])/2);
a=0;
b=1;
p[0]=0.;
Do[p[k]=N[g[p[k-1]]],{k,1,100}]
TableForm[Table[{k,N[p[k]]},{k,0,11}]]

ppp=Plot[{g[x],x},{x,a,b},AspectRatio->Automatic];
ListAnimate[Flatten[Table[{
		Show[ppp,
			  Epilog->{Table[Line[{{p[k],p[k]},{p[k],p[k+1]}}],{k,0,n-1}],
			                      	Table[Line[{{p[k],p[k+1]},{p[k+1],p[k+1]}}],{k,0,n-1}]}],
		 Show[ppp,
			  Epilog->{Table[Line[{{p[k],p[k]},{p[k],p[k+1]}}],{k,0,n}],
	                      		  Table[Line[{{p[k],p[k+1]},{p[k+1],p[k+1]}}],{k,0,n-1}]}]},
	   {n,0,10}]]]

Clear[p,g,a,b]
g[x_]=1-(x^2);
a=0;
b=1;
p[0]=0.6;
Do[p[k]=N[g[p[k-1]]],{k,1,100}]
TableForm[Table[{k,N[p[k]]},{k,0,11}]]

ppp=Plot[{g[x],x},{x,a,b},AspectRatio->Automatic];
ListAnimate[Flatten[Table[{
		Show[ppp,
			  Epilog->{Table[Line[{{p[k],p[k]},{p[k],p[k+1]}}],{k,0,n-1}],
			                      	Table[Line[{{p[k],p[k+1]},{p[k+1],p[k+1]}}],{k,0,n-1}]}],
		 Show[ppp,
			  Epilog->{Table[Line[{{p[k],p[k]},{p[k],p[k+1]}}],{k,0,n}],
	                      		  Table[Line[{{p[k],p[k+1]},{p[k+1],p[k+1]}}],{k,0,n-1}]}]},
	   {n,0,10}]]]