You are on page 1of 10

ECM6 Computational Methods :

Slide 1 of 5

Lecture 5
Analysis of Iterates
Brian G. Higgins
Department of Chemical Engineering & Materials Science
University of California, Davis
April 2014, Hanoi, Vietnam

ECM6Lecture5Vietnam_2014.nb

Topics for Lecture 5


In this lecture we give a brief over how to analyze iterates by examining several examples. We will use
NestList to generate the iterates
Iterates of a recursive algorithm
Iterates that may or may not converge.
This lecture makes use of the following Functions
NestList (see Lecture 4)
Solve, Partition, ReplaceAll, Take, Flatten (see Lecture 2)

ECM6Lecture5Vietnam_2014.nb

A Recursive Algorithm
Remove@g, iteratesD

Background
To begin our discussion let us look at the following general recursive algorithm
xn+1 = g Hp, b, xn L =

1
p

Hp - 1L xn +

b
xn

We saw this formula in Lecture 3.


It is used to compute the square root of a real number b.
The parameter p in the recursive algorithm influences the order of convergence.
When xn+1 = xn we say that the iteration has converged to a stable fixed point.

Example
To begin our discussion let us look at the following general recursive algorithm
xn+1 = g Hp, b, xn L =

1
p

Hp - 1L xn +

b
xn

We saw this formula in Lecture 3. It is used to compute the square root of a real number b.
The parameter p in the recursive algorithm influences the order of convergence.
When xn+1 = xn we say that the iteration has converged to a stable fixed point.
We define in Mathematica the function gHp, b, xn L
g@p_, b_, x_D :=

1
p

Hp - 1L x +

b
x

Note: note all fixed points are stable!

Fixed Points of g[p,b,x]


Let us determine the fixed points of the following function. That is we want to find the value of x such
that
x = g@p, b, xD
Thus we want to solve the following polynomial equation
x=

1
p

Hp - 1L x +

b
x

The value of x that satisfies the above equation is called a fixed point
We can use Mathematica's Solve function

ECM6Lecture5Vietnam_2014.nb

Solve@g@p, b, xD x, xD
99x -

b =, 9x

b ==

There are two fixed points. Note that the fixed points do not depend on the parameter p.

Application of NestList
How do we program Mathematica to produce the iterates?
In Lecture 4 we discussed the use of NestList to generate iterates:
NestList[f, expr, n]
gives a list of the results of applying
f to expr 0 through n times.

Since NestList takes a function with a single argument, we use NestList with a pure function:
iterates = NestList@g@1.5, 78.8, D &, 2, 20D
82, 26.9333, 10.9283, 8.44986, 9.03369, 8.8265, 8.89394,
8.87129, 8.87882, 8.87631, 8.87715, 8.87687, 8.87696, 8.87693,
8.87694, 8.87694, 8.87694, 8.87694, 8.87694, 8.87694, 8.87694<

It is clear the iterates have stabilized (converged) to a value of 8.87694.


Note that the

b =

78.8 = 8.87693640846886

ECM6Lecture5Vietnam_2014.nb

Convergence of Iteration Sequence


The next question we can ask is how fast did the iteration sequence take to converge to the fixed point
and how is the rate dependent on the parameter p.
To do this analysis we need to look at consecutive iterates.
Thus we partition the list of iterative values into sublists of 2 with an overlap of 1:
88x0 , x1 <, 8x1 , x2 <, 8x2 , x3 <, , 8x19 , x20 <<

Partition Function
We can accomplish this structure using the Partition function, with the appropriate arguments:
Partition@iterates, 2, 1D
882, 26.9333<, 826.9333, 10.9283<, 810.9283, 8.44986<, 88.44986, 9.03369<,
89.03369, 8.8265<, 88.8265, 8.89394<, 88.89394, 8.87129<, 88.87129, 8.87882<,
88.87882, 8.87631<, 88.87631, 8.87715<, 88.87715, 8.87687<, 88.87687, 8.87696<,
88.87696, 8.87693<, 88.87693, 8.87694<, 88.87694, 8.87694<, 88.87694, 8.87694<,
88.87694, 8.87694<, 88.87694, 8.87694<, 88.87694, 8.87694<, 88.87694, 8.87694<<

Absolute Difference Between Two Iterates


Ideally what we would like to examine is the absolute difference of two successive iterates
Dxk =

xk+1 - xk

We achieve this data structure by making use of a replacement rule (see Lecture 2) with an appropriate
pattern:
Dxk = Partition@iterates, 2, 1D . 8xk1_, xk2_< 8Abs@xk2 - xk1D< Flatten
924.9333, 16.0051, 2.47841, 0.583825, 0.207185, 0.0674407, 0.0226507, 0.00753095,
0.00251245, 0.000837245, 0.000279108, 0.0000930331, 0.0000310113, 0.0000103371,
3.4457 10-6 , 1.14857 10-6 , 3.82855 10-7 , 1.27618 10-7 , 4.25395 10-8 , 1.41798 10-8 =

Note that the absolute difference diminishes with the number of iterates. Thus we can use such metric
to determine if our iteration scheme has converged.

Speed of Convergence
To get an indication of the "speed" of convergence, we again use Partition to interrogate the absolute
difference of and two successive iterates and then examine the ratio of the absolute difference, as
shown below
Partition@Dxk , 2, 1D . 8xk1_, xk2_< 8xk1 xk2<
881.55784<, 86.45779<, 84.24513<, 82.81789<, 83.07211<,
82.97742<, 83.00768<, 82.99746<, 83.00085<, 82.99972<, 83.00009<,
82.99997<, 83.00001<, 83.<, 83.<, 83.<, 83.<, 83.<, 83.<<

ECM6Lecture5Vietnam_2014.nb

This show us that our algorithm converges linearly as


Dxk
Dxk+1

= CL 3.0

Let us put all these steps together in a compound function that depends on the parameter p
convergenceTest@p_D :=
Take@Partition@Flatten@HPartition@NestList@g@p, 78.8, D &, 2, 10D, 2, 1D .
8xk1_, xk2_< 8Abs@xk1 - xk2D<LD, 2, 1D . 8xk1_, xk2_< 8xk1 xk2<, - 1D
res = Table@Flatten@8p, convergenceTest@pD<D, 8p, 0.5, 5, .11<D
880.5, 1.04619<, 80.61, 2.53484<, 80.72, 0.71611<, 80.83, 0.829812<, 80.94, 0.910497<,
81.05, 1.07508<, 81.16, 1.36332<, 81.27, 1.73881<, 81.38, 2.22755<,
81.49, 2.9225<, 81.6, 4.00025<, 81.71, 5.89657<, 81.82, 10.1111<, 81.93, 27.5715<,
82.04, 51.0007<, 82.15, 14.3333<, 82.26, 8.69221<, 82.37, 6.40516<,
82.48, 5.16619<, 82.59, 4.38904<, 82.7, 3.85598<, 82.81, 3.46757<, 82.92, 3.17192<,
83.03, 2.93933<, 83.14, 2.75157<, 83.25, 2.59683<, 83.36, 2.4671<, 83.47, 2.3568<,
83.58, 2.26188<, 83.69, 2.17935<, 83.8, 2.10695<, 83.91, 2.04294<, 84.02, 1.98596<,
84.13, 1.93493<, 84.24, 1.88896<, 84.35, 1.84737<, 84.46, 1.80955<,
84.57, 1.77503<, 84.68, 1.74341<, 84.79, 1.71433<, 84.9, 1.68751<<

Plotting Data
We would like to plot the descrete data points shown above. To do so we make use of the function
? ListPlot
ListPlot@8y1 , y2 , <D plots points corresponding to a list of values, assumed to correspond to x coordinates 1, 2, .
ListPlot@88x1 , y1 <, 8x2 , y2 <, <D plots a list of points with specified x and y coordinates.
ListPlot@8list1 , list2 , <D plots several lists of points.

Here is a plot of the convergence rate CL as function of p


ListPlot@res, PlotRange All, Frame True, Axes False,
FrameLabel 8Style@"p", 16D, Style@"CL ", 16D<, PlotStyle PointSize@0.015DD
50

40

30

CL

20

10

0
1

p
Clearly we have rapid convergence near p=2. Let see what is happening at p=2

ECM6Lecture5Vietnam_2014.nb

iterates = NestList@g@2, 78.8, D &, 2, 10D


82, 20.7, 12.2534, 9.34213, 8.88852,
8.87694, 8.87694, 8.87694, 8.87694, 8.87694, 8.87694<

To within machine precision (16 digits) the difference between iterates has gone to zero
Partition@iterates, 2, 1D . 8xk1_, xk2_< 8Abs@xk1 - xk2D<
9818.7<, 88.44662<, 82.91125<, 80.453611<,
80.0115747<, 97.5461 10-6 =, 93.2081 10-12 =, 80.<, 80.<, 80.<=

ECM6Lecture5Vietnam_2014.nb

Temperature Distribution in a Rod


Remove@g2D

Background
The steady state temperature in a rod of material of length L with an embedded heat source is given by
T2 = Cosh J 0.5 L T2 N
Find the all possible steady state temperatures.
Note the steady state temperature is the value of T that satisfies the above equation

Solution
First we make the substitution
u = T2
so that the steady state temperature becomes
u = Cosh J 0.5 L uN
We can get an estimate of the steady state temperatures by plotting the LHS and RHS of the above
equation as a function of T and looking for points were the two curves intersect for a specified rod
length L.

Using Solve
As illustrated below, the fixed points for this iteration scheme cannot be found using the function Solve:
SolveBu CoshB

0.5 0.094 uF, uF

Solve::inex :
Solve was unable to solve the system with inexact coefficients or the system obtained by direct rationalization
of inexact numbers present in the system. Since many of the methods used by Solve
require exact input, providing Solve with an exact version of the system may help.

Solve@u Cosh@0.216795 uD, uD

But we can use a plotting function to examine the roots. That is, we plot the LHS and RHS of the equation for a given value of L
u = Cosh J 0.5 L uN
and the intersection points of the two curves are the desired roots.

Plotting the function


We will take L=0.094 ( Note the use of a replacement rule to set the value of L)

ECM6Lecture5Vietnam_2014.nb

PlotB:u, CoshB

0.5 L uF .L 0.094>, 8u, 0, 20<, Frame True,

FrameLabel 8Style@"u", 16D, Style@"LHS or RHS", 16D<,


Epilog 8Red, PointSize@0.015D, Point@81.02478, 1.02478<D,
Point@815.9743, 15.9743<D<, PlotStyle ThickF
35

LHS or RHS

30
25
20
15
10
5
0
0

10

15

20

u
Thus we have steady solutions near u=1 (1.02478) and u=16 (15.9743).
Let us iterate the function and see if we can find the fixed point (steady solutions)

Fixed Point Iteration


For our iteration scheme we define the following function
g2@L_, u_D := CoshB

0.5 L uF

Again we take L=0.094 and as our initial guess we take u0 = 16


iterates = NestList@g2@0.094, D &, 16, 20D
General::ovfl : Overflow occurred in computation.

916, 16.0634, 16.2851, 17.0855, 20.3179, 40.9284, 3568.59,


4.912464138530 10335 , Overflow@D, Indeterminate, Indeterminate,
Indeterminate, Indeterminate, Indeterminate, Indeterminate, Indeterminate,
Indeterminate, Indeterminate, Indeterminate, Indeterminate, Indeterminate=

It is quite evident that the iterations have diverged!


Let us now give as our initial guess the value u0 = 2. For this value of u0 the iteration sequence converges to the root x = p = 1.02478
iterates = NestList@g2@0.094, D &, 2, 20D
82, 1.09548, 1.02833, 1.02495, 1.02479, 1.02478, 1.02478,
1.02478, 1.02478, 1.02478, 1.02478, 1.02478, 1.02478, 1.02478,
1.02478, 1.02478, 1.02478, 1.02478, 1.02478, 1.02478, 1.02478<

Summary
Thus our iteration method for finding the fixed points of

10

ECM6Lecture5Vietnam_2014.nb

u = Cosh J 0.5 L uN, with L = 0.094


is partially satisfactory. We can find the root near u=1, but cannot find the root near u=16. In the next
lecture we will explain what is happening.

You might also like