ECM6Lecture3Vietnam 2014
ECM6Lecture3Vietnam 2014
ECM6Lecture3Vietnam 2014
Slide 1 of 12
Lecture 3
Introduction to Algorithms
Brian G. Higgins
Department of Chemical Engineering & Materials Science
University of California, Davis
April 2014, Hanoi, Vietnam
ECM6Lecture3Vietnam_2014.nb
1. Concept of an algorithm
2. Example involving summations
3. Algorithm for approximating an integral (trapezoidal rule) using Table
4. Algorithm for approximating integral using For loop
ECM6Lecture3Vietnam_2014.nb
Introduction to Algorithms
An algorithm is a step-by-step procedure that produces a solution in a finite number of steps. The basic
components of an algorithm are
(i) List of the input parameters that must be supplied
(ii) List of operations that must be performed and in what order
(iii) Output of the algorithm.
When the output is a numerical result we call the procedure a numerical method. Numerical methods by
their nature do not produce exact solutions. There is always some error associated with the method.
Thus one goal of a numerical method is to assess how close the "solution" is to the exact solution.
Matters are complicated because the exact solution is often not known. If it were there would be no
reason to use a numerical method.
Here are some simple example of an algorithm
Example
Evaluate the mean and standard deviation of a given set of data (measurements). Suppose the data is
in the form of a sequence
data = 8x1 , x2 , x3 , , xn <
The quantities that we want to evaluate are given by the following formulas
mean :
Xx\ =
1
n
xi
i=1
Standard Deviation : s =
1
n Hn - 1L
x2i
i=1
- xi
i=1
ECM6Lecture3Vietnam_2014.nb
Algorithm
A word description of the steps in an algorithm
Given Inputs: xi
Step 1:
Calculate n (number of data points)
Step 2:
Initialize variables for algorithm:
xsum=0, x2sum=0
Step 3:
for i from 1 to n
add xi to xsum
add xi2 to x2sum
end
Step 4:
calculate xbar= xsum/n
Step 5:
calculate s=Sqrt(n(x2sum)- HxsumL2 )/(n(n-1)
Output:
xbar and s
ECM6Lecture3Vietnam_2014.nb
n = Length@xD
15
xsum = 0;
x2sum = 0;
? For
For@start, test, incr, bodyD executes start, then repeatedly evaluates body and incr until test fails to give True.
In[6]:=
xbar = xsum n
9.83147
Out[8]=
s=
1
n Hn - 1L
0.232434
n x2sum - xsum2
ECM6Lecture3Vietnam_2014.nb
Output: xbar, s
In[9]:=
Out[9]=
Out[10]=
xbar
s
9.83147
0.232434
Final Comments
This is not the only way to do the calculation in Mathematica. Here are some alternative methods that
make use of Mathematica's built-in functions
ECM6Lecture3Vietnam_2014.nb
Example 1:
In[11]:=
Out[15]=
9.83147
Out[16]=
0.232434
Example 2:
Here is another method that makes use of functional programming concepts. In this example we use
the Apply function.
In[17]:=
? Apply
Apply@ f , exprD or f expr replaces the head of expr by f .
Apply@ f , expr, 81<D or f expr replaces heads at level 1 of expr by f .
Apply@ f , expr, levelspecD replaces heads in parts of expr specified by levelspec.
In[18]:=
Out[20]=
9.83147
Out[21]=
0.232434
Example 3:
Here is another method that makes use of functional programming concepts. In this example we use
the Fold function.
ECM6Lecture3Vietnam_2014.nb
In[22]:=
? Fold
Fold@ f , x, listD gives the last element of FoldList@ f , x, listD.
In[23]:=
Out[25]=
9.83147
Out[26]=
0.232434
ECM6Lecture3Vietnam_2014.nb
Int = f HxL x
a
Tn = f HxL x
a
h
2
n-1
Algorithm
Given Inputs: Limits of integration a,b, integrand f(x)
and number of sub intervals
Step 1:
compute h; set sum=0
Step 2:
Step 3:
Step 4:
Output:
10
ECM6Lecture3Vietnam_2014.nb
Mathematica Code
Let us evaluate the following definite integral
5
Cos HxL x
0
Remove@x, a, b, h, n, i, mysum, f, T, eD
In[28]:=
Plot@Cos@xD, 8x, 0, 5<, PlotStyle Blue, Frame True, FrameLabel 8"x", "fHxL"<D
1.0
fHxL
0.5
0.0
Out[28]=
-0.5
-1.0
0
Out[32]=
ECM6Lecture3Vietnam_2014.nb
Let us see how the solution varies with the value of n. We will define a function for the error
In[34]:=
Remove@TD;
a = 0; b = 5; f@x_D := Cos@xD;
Hb - aL
T@n_D :=
Hf@aD + 2 Sum@f@a + i Hb - aL nD, 8i, 1, n - 1<D + f@bDL N
2n
b
In[37]:=
e@100D
0.000199784
? TableForm
TableForm@listD prints with the elements of list arranged in an array of rectangular cells.
In[40]:=
Out[40]//TableForm=
Tn
- 0.398281
- 0.830687
- 0.902778
- 0.927504
- 0.938863
- 0.945011
- 0.94871
- 0.951108
- 0.95275
- 0.953925
- 0.954793
- 0.955453
- 0.955967
- 0.956375
- 0.956704
- 0.956973
n
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
en
0.560643
0.128237
0.0561464
0.0314201
0.0200613
0.0139136
0.0102144
0.00781648
0.00617387
0.00499961
0.00413116
0.00347084
0.00295709
0.00254952
0.00222076
0.00195173
en en2
0.134509
0.228733
0.240992
0.245015
0.246834
0.24781
0.248395
0.248773
0.249032
0.249216
0.249353
0.249456
0.249537
0.249601
0.249652
0.249694
? Grid
Grid@88expr11 , expr12 , <, 8expr21 , expr22 , <, <D is
an object that formats with the exprij arranged in a two-dimensional grid.
11
12
ECM6Lecture3Vietnam_2014.nb
In[42]:=
In[43]:=
Out[43]=
mytable = Table@8n, T@nD, Abs@e@nDD, Abs@e@nDD Abs@e@n 2DD<, 8n, 2, 32, 2<D;
mytable2 = Prepend@mytable, 8"n", "Tn ", "en ", "en en2 "<D;
Grid@mytable2, Frame AllD
n
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
Tn
- 0.398281
- 0.830687
- 0.902778
- 0.927504
- 0.938863
- 0.945011
- 0.94871
- 0.951108
- 0.95275
- 0.953925
- 0.954793
- 0.955453
- 0.955967
- 0.956375
- 0.956704
- 0.956973
en
en en2
0.560643
0.134509
0.128237
0.228733
0.0561464
0.240992
0.0314201
0.245015
0.0200613
0.246834
0.0139136
0.24781
0.0102144
0.248395
0.00781648 0.248773
0.00617387 0.249032
0.00499961 0.249216
0.00413116 0.249353
0.00347084 0.249456
0.00295709 0.249537
0.00254952 0.249601
0.00222076 0.249652
0.00195173 0.249694
The function Grid takes on many options for embellishing the output of the table.
In[44]:=
Out[44]=
Options@GridD
8Alignment 8Center, Baseline<, AllowedDimensions Automatic,
AllowScriptLevelChange True, AutoDelete False, Background None,
BaselinePosition Automatic, BaseStyle 8<, DefaultBaseStyle Grid,
DefaultElement , DeleteWithContents True, Dividers None,
Editable Automatic, Frame None, FrameStyle Automatic, ItemSize Automatic,
ItemStyle None, Selectable Automatic, Spacings Automatic<
ECM6Lecture3Vietnam_2014.nb
In[45]:=
en en2
-0.398281 0.560643
0.134509
-0.830687 0.128237
0.228733
-0.902778 0.0561464
0.240992
-0.927504 0.0314201
0.245015
10 -0.938863 0.0200613
0.246834
12 -0.945011 0.0139136
0.24781
14
Out[45]=
Tn en
-0.94871 0.0102144
0.248395
13
14
ECM6Lecture3Vietnam_2014.nb
1
a
Ha - 1L x +
b
x
The essential idea is as follows. We are given a function g(x), and we want to iterate based on the
following algorithm
xn+1 = g Hxn L
starting with the value x0 .
We say the function g(x) has a fixed point if we can find a value x = xp such that
xp = g Ixp M
where xp is called the fixed point of g(x).
In[46]:=
ECM6Lecture3Vietnam_2014.nb
15
1
a
Ha - 1L x +
b
x
This function is used to compute the square root of a real number a. The parameter a 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.
Let us determine the fixed points of this function for b=78.4, a=5
Plot@g@2, 78.4, xD, 8x, - 15, 15<, Frame True, PlotStyle 8Thick, Blue<,
FrameLabel 8Style@"x", 16D, Style@"gHx<", 16D<, AspectRatio 1D
40
gHx<
20
Out[48]=
-20
-40
-15
-10
-5
10
15
16
ECM6Lecture3Vietnam_2014.nb
gHx<
20
Out[49]=
-20
-40
-15
-10
-5
10
15
x
Let us determine the fixed points of this function for b=78.4, a=5 with
In[50]:=
Solve@g@a, 78.4, xD x, xD
Solve::ratnz : Solve was unable to solve the system with inexact coefficients. The
answer was obtained by solving a corresponding exact system and numericizing the result.
Out[50]=
Note in this example the fixed points do not depend on the parameter a. Further
Thus the fixed points are the values
78.4 = 8.85438.
ECM6Lecture3Vietnam_2014.nb
17
The For loop executes start, then repeatedly evaluates body and incr until test fails to give True.
Here is an example where the arguments of the For function are defined as
start block:
test block:
incr block:
body block:
i = 1; x0 = 0.2
i<20
i++
xp=g[1.5,x0 ];Print[xp];x0=xp
Note that the final value returned by For is Null. For this reason we use the Print function to determine
the values of x during the iteration.
Mathematica Code
In[51]:=
Rather than print the results of the iteration at each step we can store the intermediate results in a list
In[52]:=
18
ECM6Lecture3Vietnam_2014.nb
In[53]:=
Out[53]=
resultList
80.2, 261.4, 87.3333, 29.7096, 11.6624, 8.3691, 9.03489, 8.79661,
8.87389, 8.8479, 8.85654, 8.85366, 8.85462, 8.8543, 8.8544, 8.85437,
8.85438, 8.85438, 8.85438, 8.85438, 8.85438, 8.85438, 8.85438, 8.85438<
It is clear the iterates in this example have stabilized (converged) to a value of 8.85438 =
78.4 .
If we give a negative value for the initial guess, we converge to the other root!
In[54]:=
Out[55]=
ECM6Lecture3Vietnam_2014.nb
Final Comments
Questions we would like to know are:
Does the algorithm always converge?
(What is the order of convergence, i.e what is the change in the error at each iteration?
These topics will be more fully discussed in future Lectures.
19