Beruflich Dokumente
Kultur Dokumente
Methods
Jeffrey R. Chasnov
Copyright
c 2012 by Jeffrey Robert Chasnov
This work is licensed under the Creative Commons Attribution 3.0 Hong Kong License.
To view a copy of this license, visit http://creativecommons.org/licenses/by/3.0/hk/
or send a letter to Creative Commons, 171 Second Street, Suite 300, San Francisco,
California, 94105, USA.
Preface
What follows are my lecture notes for Math 3311: Introduction to Numerical
Methods, taught at the Hong Kong University of Science and Technology. Math
3311, with two lecture hours per week, is primarily for non-mathematics majors
and is required by several engineering departments.
All web surfers are welcome to download these notes at
http://www.math.ust.hk/~machas/numerical-methods.pdf
and to use the notes freely for teaching and learning. I welcome any comments,
suggestions or corrections sent by email to jeffrey.chasnov@ust.hk.
iii
Contents
1 IEEE Arithmetic 1
1.1 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Numbers with a decimal or binary point . . . . . . . . . . . . . . 1
1.3 Examples of binary numbers . . . . . . . . . . . . . . . . . . . . . 1
1.4 Hex numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.5 4-bit unsigned integers as hex numbers . . . . . . . . . . . . . . . 2
1.6 IEEE single precision format: . . . . . . . . . . . . . . . . . . . . 2
1.7 Special numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.8 Examples of computer numbers . . . . . . . . . . . . . . . . . . . 3
1.9 Inexact numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.9.1 Find smallest positive integer that is not exact in single
precision . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.10 Machine epsilon . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.11 IEEE double precision format . . . . . . . . . . . . . . . . . . . . 5
1.12 Roundoff error example . . . . . . . . . . . . . . . . . . . . . . . 6
2 Root Finding 7
2.1 Bisection Method . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.2 Newtons Method . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.3 Secant Method .. . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.3.1 Estimate 2 = 1.41421356 using Newtons Method . . . . 8
2.3.2 Example of fractals using Newtons Method . . . . . . . . 8
2.4 Order of convergence . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.4.1 Newtons Method . . . . . . . . . . . . . . . . . . . . . . . 9
2.4.2 Secant Method . . . . . . . . . . . . . . . . . . . . . . . . 10
3 Systems of equations 13
3.1 Gaussian Elimination . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.2 decomposition . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.3 Partial pivoting . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
3.4 Operation counts . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.5 System of nonlinear equations . . . . . . . . . . . . . . . . . . . . 21
4 Least-squares approximation 23
4.1 Fitting a straight line . . . . . . . . . . . . . . . . . . . . . . . . 23
4.2 Fitting to a linear combination of functions . . . . . . . . . . . . 24
v
vi CONTENTS
5 Interpolation 27
5.1 Polynomial interpolation . . . . . . . . . . . . . . . . . . . . . . . 27
5.1.1 Vandermonde polynomial . . . . . . . . . . . . . . . . . . 27
5.1.2 Lagrange polynomial . . . . . . . . . . . . . . . . . . . . . 28
5.1.3 Newton polynomial . . . . . . . . . . . . . . . . . . . . . . 28
5.2 Piecewise linear interpolation . . . . . . . . . . . . . . . . . . . . 29
5.3 Cubic spline interpolation . . . . . . . . . . . . . . . . . . . . . . 30
5.4 Multidimensional interpolation . . . . . . . . . . . . . . . . . . . 33
6 Integration 35
6.1 Elementary formulas . . . . . . . . . . . . . . . . . . . . . . . . . 35
6.1.1 Midpoint rule . . . . . . . . . . . . . . . . . . . . . . . . . 35
6.1.2 Trapezoidal rule . . . . . . . . . . . . . . . . . . . . . . . 36
6.1.3 Simpsons rule . . . . . . . . . . . . . . . . . . . . . . . . 37
6.2 Composite rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
6.2.1 Trapezoidal rule . . . . . . . . . . . . . . . . . . . . . . . 37
6.2.2 Simpsons rule . . . . . . . . . . . . . . . . . . . . . . . . 38
6.3 Local versus global error . . . . . . . . . . . . . . . . . . . . . . . 38
6.4 Adaptive integration . . . . . . . . . . . . . . . . . . . . . . . . . 39
IEEE Arithmetic
1.1 Definitions
Bit = 0 or 1
Byte = 8 bits
Word = Reals: 4 bytes (single precision)
8 bytes (double precision)
= Integers: 1, 2, 4, or 8 byte signed
1, 2, 4, or 8 byte unsigned
Decimal: 103 102 101 100 101 102 103 104
Binary: 23 22 21 20 21 22 23 24
{0, 1, 2, 3, . . . , 9, 10, 11, 12, 13, 14, 15} = {0, 1, 2, 3.......9, a,b,c,d,e,f}
1
2 CHAPTER 1. IEEE ARITHMETIC
0 1 2 3 4 5 6 7 8 9 31
where s = sign
e = biased exponent
p=e-127 = exponent
1.f = significand (use binary point)
1.7. SPECIAL NUMBERS 3
Subnormal numbers
Allow 1.f 0.f (in software)
Smallest positive number = 0.0000 0000 0001 2126
= 223 2126 1.4 1045
0.333 . . . 0.
0.666 . . . 0.0
1.333 . . . 0.01
0.666 . . . 0.010
1.333 . . . 0.0101
etc.
= 01010101010101010101011,
where we have rounded to the nearest binary number (here, rounded up). The
machine number 1/3 is then represented as
00111110101010101010101010101011
or in hex
3.
2 = 223 1.11 . . . 1,
and
1 = 224 1.00 . . . 0.
The integer would then require a one-bit in the 224 position, which is not
available. Therefore, the smallest positive integer that is not exact is 224 + 1 =
16 777 217. In MATLAB, single(224 ) has the same value as single(224 +1). Since
single(224 + 1) is exactly halfway between the two consecutive machine numbers
224 and 224 + 2, MATLAB rounds to the number with a final zero-bit in f, which
is 224 .
1.10. MACHINE EPSILON 5
Find mach
The number 1 in the IEEE format is written as
1 = 20 1.000 . . . 0,
with 23 0s following the binary point. The number just larger than 1 has a 1
in the 23rd position after the decimal point. Therefore,
mach = 223 1.192 107 .
What is the distance between 1 and the number just smaller than 1? Here,
the number just smaller than one can be written as
21 1.111 . . . 1 = 21 (1 + (1 223 )) = 1 224
Therefore, this distance is 224 = mach /2.
The spacing between numbers is uniform between powers of 2, with logarith-
mic spacing of the powers of 2. That is, the spacing of numbers between 1 and
2 is 223 , between 2 and 4 is 222 , between 4 and 8 is 221 , etc. This spacing
changes for denormal numbers, where the spacing is uniform all the way down
to zero.
0 1 2 3 4 5 6 7 8 9 10 11 12 63
6 CHAPTER 1. IEEE ARITHMETIC
where s = sign
e = biased exponent
p=e-1023 = exponent
1.f = significand (use binary point)
2 + 2 1 = 0,
Consider the solution with > 0 and > 0 (the + solution) given by
= + 2 + 1. (1.1)
As ,
= + 2 + 1
= + 1 + 1/2
= ( 1 + 1/2 1)
( )
1
1+ 2 1
2
1
= .
2
Now in double precision, realmin 2.2 10308 and we would like to be
accurate to this value before it goes to 0 via denormal numbers. Therefore,
should be computed accurately to 1/(2 realmin) 2 10307 . What
2 2
happens if we compute (1.1) directly? Then = 0 when + 1 = , or
1 + 1/2 = 1. That is 1/2 = mach /2, or = 2/ mach 108 .
For a subroutine written to compute the solution of a quadratic for a general
user, this is not good enough. The way for a software designer to solve this
problem is to compute the solution for as
1
= .
+ 2 + 1
Root Finding
1 0
2 = 0 + .
2
The sign of (2 ) can then be determined. The value of 3 is then chosen as
either the midpoint of 0 and 2 or as the midpoint of 2 and 1 , depending on
whether 0 and 2 bracket the root, or 2 and 1 bracket the root. The root,
therefore, stays bracketed at all times. The algorithm proceeds in this fashion
and is typically stopped when the increment to the left side of the bracket
(above, given by (1 0 )/2) is smaller than some required precision.
(+1 ) = ( ) + (+1 ) ( ) + . . . .
7
8 CHAPTER 2. ROOT FINDING
2 2
+1 = .
2
We take as our initial guess 0 = 1. Then
1 3
1 = 1 = = 1.5,
2 2
9
3 4 2 17
2 = = = 1.416667,
2 3 12
172
17 122 2 577
3 = 17 = = 1.41426.
12 6
408
() = 3 1.
2 = 1, = 0, 1, 2, . . .
1, 2/3 , 4/3 .
With
= cos + sin ,
2.4. ORDER OF CONVERGENCE 9
and cos (2/3) = 1/2, sin (2/3) = 3/2, the three cubic roots of unity are
1 3 1 3
1 = 1, 2 = + , 3 = .
2 2 2 2
The interesting idea here is to determine which initial values of 0 in the complex
plane converge to which of the three cubic roots of unity.
Newtons method is
3 1
+1 = 2 .
3
If the iteration converges to 1 , we color 0 red; 2 , blue; 3 , green. The result
will be shown in lecture.
|+1 | = | | ,
( )
+1 = .
( )
( )
+1 = + ,
( )
or
( )
+1 = + . (2.1)
( )
10 CHAPTER 2. ROOT FINDING
We use Taylor series to expand the functions ( ) and ( ) about the root
, using () = 0. We have
1
( ) = () + ( ) () + ( )2 () + . . . ,
2
1 2
= () + () + . . . ;
2 (2.2)
1
( ) = () + ( ) () + ( )2 () + . . . ,
2
1 2
= () () + () + . . . .
2
To make further progress, we will make use of the following standard Taylor
series:
1
= 1 + + 2 + . . . , (2.3)
1
which converges for || < 1. Substituting (2.2) into (2.1), and using (2.3) yields
( )
+1 = +
( )
() + 12 2 () + . . .
= +
() () + 12 2 () + . . .
+ 12 2 ()
()
+ ...
= +
1 ()
()
+ ...
1 () ()
( ) ( )
= + + 2 + ... 1 + + ...
2 () ()
( (
) )
1 () ()
= + + 2 + . . .
2 () ()
1 () 2
= + ...
2 ()
Therefore, we have shown that
|+1 | = | |2
as , with
1 ()
= ,
2 ()
provided () = 0. Newtons method is thus of order 2 at simple roots.
We therefore have
() + 12 2 () + . . .
+1 = +
() 12 (1 + ) () + . . .
1 12 ()
()
+ ...
=
1 12 (1 + ) ()
()
+ ...
()
( ) ( )
1 () 1
= 1 + ... 1 + (1 + ) + ...
2 () 2 ()
1 ()
= 1 + . . . ,
2 ()
or to leading order
1 ()
|+1 | = |1 || |. (2.4)
2 ()
The order of convergence is not yet obvious from this equation, and to determine
the scaling law we look for a solution of the form
|+1 | = | | .
| | = |1 | ,
and therefore 2
|+1 | = +1 |1 | .
Substitution into (2.4) results in
2 ()
+1 |1 | = +1
() |1 | .
2
1 ()
= ,
2 ()
and
2 = + 1.
12 CHAPTER 2. ROOT FINDING
Systems of equations
13
14 CHAPTER 3. SYSTEMS OF EQUATIONS
interchange the order of any rows. The goal is to convert the original matrix
into an upper-triangular matrix.
We start with the first row of the matrix and work our way down as follows.
First we multiply the first row by 2 and add it to the second row, and add the
first row to the third row:
3 2 1 1
0 2 5 9 .
0 2 3 7
We then go to the second row. We multiply this row by 1 and add it to the
third row:
3 2 1 1
0 2 5 9 .
0 0 2 2
The resulting equations can be determined from the matrix and are given by
31 + 22 3 = 1
22 + 53 = 9
23 = 2.
These equations can be solved by backward substitution, starting from the last
equation and working backwards. We have
23 = 2 3 = 1
22 = 9 53 = 4 2 = 2,
31 = 1 22 + 3 = 6 1 = 2.
Therefore,
1 2
2 = 2 .
3 1
3.2 decomposition
The process of Gaussian Elimination also results in the factoring of the matrix
A to
A = LU,
where L is a lower triangular matrix and U is an upper triangular matrix.
Using the same matrix A as in the last section, we show how this factorization
is realized. We have
3 2 1 3 2 1
6 6 7 0 2 5 = M1 A,
3 4 4 0 2 3
where
1 0 0 3 2 1 3 2 1
M1 A = 2 1 0 6 6 7 = 0 2 5 .
1 0 1 3 4 4 0 2 3
3.2. DECOMPOSITION 15
Note that the matrix M1 performs row elimination on the first column. Two
times the first row is added to the second row and one times the first row is
added to the third row. The entries of the column of M1 come from 2 = (6/3)
and 1 = (3/ 3) as required for row elimination. The number 3 is called the
pivot.
The next step is
3 2 1 3 2 1
0 2 5 0 2 5 = M2 (M1 A),
0 2 3 0 0 2
where
1 0 0 3 2 1 3 2 1
M2 (M1 A) = 0 1 0 0 2 5 = 0 2 5 .
0 1 1 0 2 3 0 0 2
Here, M2 multiplies the second row by 1 = (2/ 2) and adds it to the
third row. The pivot is 2.
We now have
M2 M1 A = U
or
A = M1 1
1 M2 U.
The inverse matrices are easy to find. The matrix M1 multiples the first row by
2 and adds it to the second row, and multiplies the first row by 1 and adds it
to the third row. To invert these operations, we need to multiply the first row
by 2 and add it to the second row, and multiply the first row by 1 and add
it to the third row. To check, with
M1 M1
1 = I,
we have
1 0 0 1 0 0 1 0 0
2 1 0 2 1 0 = 0 1 0 .
1 0 1 1 0 1 0 0 1
Similarly,
1 0 0
M1
2 = 0 1 0
0 1 1
Therefore,
L = M1 1
1 M2
is given by
1 0 0 1 0 0 1 0 0
L = 2 1 0 0 1 0 = 2 1 0 ,
1 0 1 0 1 1 1 1 1
We interchange rows to place the largest element (in absolute value) in the pivot,
or 11 , position. That is,
6 6 7
A 2 2 1 = P12 A,
3 8 4
where
0 1 0
P12 = 1 0 0
0 0 1
is a permutation matrix that when multiplied on the left interchanges the first
and second rows of a matrix. Note that P112 = P12 . The elimination step is
then
6 6 7
P12 A 0 0 4/3 = M1 P12 A,
0 5 1/2
where
1 0 0
M1 = 1/3 1 0 .
1/2 0 1
Since the permutation matrices given by P are their own inverses, we can write
our result as
(P23 M1 P23 )P23 P12 A = U.
18 CHAPTER 3. SYSTEMS OF EQUATIONS
which we write as
PA = LU.
Instead of L, MATLAB will write this as
A = (P1 L)U.
[LU] = lu(A);
y = L b;
x = U ;
x = U (L b);
where the parenthesis are required. In lecture, I will demonstrate these solutions
in MATLAB using the matrix A = [2, 2, 1; 6, 6, 7; 3, 8, 4]; which is the
example in the notes.
the size of the problem. For example, suppose one wants to multiply two full
matrices. The calculation of each element requires multiplications and
1 additions, or say 2 1 operations. There are 2 elements to compute
so that the total operation count is 2 (2 1). If is large, we might want to
know what will happen to the computational time if is doubled. What matters
most is the fastest-growing, leading-order term in the operation count. In this
matrix multiplication example, the operation count is 2 (2 1) = 23 2 ,
and the leading-order term is 23 . The factor of 2 is unimportant for the scaling,
and we say that the algorithm scales like O(3 ), which is read as big Oh of n
cubed. When using the big-Oh notation, we will drop both lower-order terms
and constant multipliers.
The big-Oh notation tells us how the computational time of an algorithm
scales. For example, suppose that the multiplication of two large matrices
took a computational time of seconds. With the known operation count
going like O(3 ), we can write
= 3
2 = (2)3
= 83
= 8 ,
so that doubling the size of the matrix is expected to increase the computational
time by a factor of 23 = 8.
Running MATLAB on my computer, the multiplication of two 2048 2048
matrices took about 0.75 sec. The multiplication of two 4096 4096 matrices
took about 6 sec, which is 8 times longer. Timing of code in MATLAB can be
found using the built-in stopwatch functions tic and toc.
What is the operation count and therefore the scaling of Gaussian elimina-
tion? Consider an elimination step with the pivot in the th row and th column.
There are both rows below the pivot and columns to the right of the
pivot. To perform elimination of one row, each matrix element to the right of
the pivot must be multiplied by a factor and added to the row underneath. This
must be done for all the rows. There are therefore ( )( ) multiplication-
additions to be done for this pivot. Since we are interested in only the scaling
of the algorithm, I will just count a multiplication-addition as one operation.
To find the total operation count, we need to perform elimination using 1
pivots, so that
1
op. counts = ( )2
=1
= ( 1)2 + ( 2)2 + . . . (1)2
1
= 2 .
=1
20 CHAPTER 3. SYSTEMS OF EQUATIONS
The solution for is found after solving for with > . The explicit solution
for is given by
1
= , .
, =+1
(+1 , +1 ) = ( , ) + (+1 ) ( , )
+ (+1 ) ( , ) + . . .
(+1 , +1 ) = ( , ) + (+1 ) ( , )
+ (+1 ) ( , ) + . . . .
To obtain Newtons method, we take (+1 , +1 ) = 0, (+1 , +1 ) = 0 and
drop higher-order terms above linear. Although one can then find a system of
linear equations for +1 and +1 , it is more convenient to define the variables
= +1 , = +1 .
The iteration equations will then be given by
+1 = + , +1 = + ;
and the linear equations to be solved for and are given by
( ) ( ) ( )
= ,
where , , , , , and are all evaluated at the point ( , ). The two-
dimensional case is easily generalized to dimensions. The matrix of partial
derivatives is called the Jacobian Matrix.
We illustrate Newtons Method by finding the steady state solution of the
Lorenz equations, given by
( ) = 0,
= 0,
= 0,
22 CHAPTER 3. SYSTEMS OF EQUATIONS
where , , and are the unknown variables and , , and are the known
parameters. Here, we have a three-dimensional homogeneous system = 0,
= 0, and = 0, with
(, , ) = ( ),
(, , ) = ,
(, , ) = .
= , = , = 0,
= , = 1, = ,
= , = , = .
with
+1 = + ,
+1 = + ,
+1 = + .
The MATLAB program that solves this system is contained in newton system.m.
Chapter 4
Least-squares
approximation
() = + .
The distance from the data point ( , ) and the fitting curve is given by
= ( )
= ( + ).
A least-squares fit minimizes the sum of the squares of the s. This minimum
can be shown to result in the most probable values of and .
We define
= 2
=1
( )2
= ( + ) .
=1
= 0, = 0.
23
24 CHAPTER 4. LEAST-SQUARES APPROXIMATION
These equations form a system of two linear equations in the two unknowns
and , which is evident when rewritten in the form
2 + = ,
=1 =1 =1
+ = .
=1 =1
r = y Ac
= r r
= (y Ac) (y Ac)
= y y c A y y Ac + c A Ac.
Since is a scalar, each term in the above expression must be a scalar, and since
the transpose of a scalar is equal to the scalar, we have
)
c A y = c A y = y Ac.
(
Therefore,
= y y 2y Ac + c A Ac.
To find the minimum of , we will need to solve / = 0 for = 1, . . . , .
To take the derivative of , we switch to a tensor notation, using the Einstein
summation convention, where repeated indices are summed over their allowable
range. We can write
= 2 A + A A .
= 2 A + A A + A A .
Now, {
1, if = ;
=
0, otherwise.
Therefore,
= 2 A + A A + A A .
Now,
A A = A A
= A A
= A A
= A A .
26 CHAPTER 4. LEAST-SQUARES APPROXIMATION
Therefore,
= 2 A + 2A A .
With the partials set equal to zero, we have
A A = A ,
or
A A = A ,
In vector notation, we have
A Ac = A y. (4.2)
Equation (4.2) is the so-called normal equation, and can be solved for c by
Gaussian elimination using the MATLAB backslash operator. After construct-
ing the matrix A given by (4.1), and the vector y from the data, one can code
in MATLAB
= ( )( );
But in fact the MATLAB back slash operator will automatically solve the normal
equations when the matrix A is not square, so that the MATLAB code
= ;
Interpolation
() = 0 + 1 1 + + .
27
28 CHAPTER 5. INTERPOLATION
0 = 0 0 + 1 1
0 + + 1 0 +
2 = 0 1 + 1 1
1 + + 1 1 +
.. .. ..
. . .
= 0 + 1 1
+ + 1 + .
The matrix is called the Vandermonde matrix, and can be constructed using
the MATLAB function vander.m. The system of linear equations can be solved
in MATLAB using the operator, and the MATLAB function polyval.m can
used to interpolate using the coefficients. I will illustrate this in class and
place the code on the website.
( 1 )( 2 ) ( )0 ( 0 )( 2 ) ( )1
() = +
(0 1 )(0 2 ) (0 ) (1 0 )(1 2 ) (1 )
( 0 )( 1 ) ( 1 )
+ + .
( 0 )( 1 ) ( 1 )
It can be clearly seen that the first term is equal to zero when = 1 , 2 , . . . ,
and equal to 0 when = 0 ; the second term is equal to zero when =
0 , 2 , . . . and equal to 1 when = 1 ; and the last term is equal to zero
when = 0 , 1 , . . . 1 and equal to when = . The uniqueness of
the interpolating polynomial implies that the Lagrange polynomial must be the
interpolating polynomial.
() = 0 + 1 ( 0 ) + 2 ( 0 )( 1 ) + + ( 0 ) ( 1 ),
5.2. PIECEWISE LINEAR INTERPOLATION 29
0 = 0 ,
1 = 0 + 1 (1 0 ),
2 = 0 + 1 (2 0 ) + 2 (2 0 )(2 1 ),
.. .. ..
. . .
= 0 + 1 ( 0 ) + 2 ( 0 )( 1 ) + + ( 0 ) ( 1 ).
This system of linear equations is lower triangular as can be seen from the
matrix form
1 0 0 0 0
1
(1 0 ) 0 0 1
.. .. .. .. .. ..
. . . . . .
1 ( 0 ) ( 0 )( 1 ) ( 0 ) ( 1 )
0
1
= . ,
..
() = (), for +1 ,
where
() = ( ) + ,
and = 0, 1, . . . , 1.
We now require = () to pass through the endpoints ( , ) and (+1 , +1 ).
We have
= ,
+1 = (+1 ) + .
30 CHAPTER 5. INTERPOLATION
(0 , 0 ), (1 , 1 ), . . . ( , ).
() = ( )3 + ( )2 + ( ) + , = 0, 1, . . . , 1,
() = (), for +1 .
( ) = , = 0 to 1, (5.1)
(+1 ) = +1 , = 0 to 1. (5.2)
The requirement that () is continuous results in
(+1 ) = +1
(+1 ), = 0 to 2. (5.3)
(+1 ) = +1
(+1 ), = 0 to 2. (5.4)
There are cubic polynomials () and each cubic polynomial has four free
coefficients; there are therefore a total of 4 unknown coefficients. The number
of constraining equations from (5.1)-(5.4) is 2 + 2( 1) = 4 2. With 4 2
constraints and 4 unknowns, two more conditions are required for a unique
solution. These are usually chosen to be extra conditions on the first 0 () and
last 1 () polynomials. We will discuss these extra conditions later.
We now proceed to determine equations for the unknown coefficients of the
cubic polynomials. The polynomials and their first two derivatives are given by
() = ( )3 + ( )2 + ( ) + , (5.5)
() 2
= 3 ( ) + 2 ( ) + , (5.6)
() = 6 ( ) + 2 . (5.7)
5.3. CUBIC SPLINE INTERPOLATION 31
We will consider the four conditions (5.1)-(5.4) in turn. From (5.1) and (5.5),
we have
= , = 0 to 1, (5.8)
which directly solves for all of the -coefficients.
To satisfy (5.2), we first define
= +1 ,
and
= +1 .
Now, from (5.2) and (5.5), using (5.8), we obtain the equations
3 + 2 + = , = 0 to 1. (5.9)
3 2 + 2 + = +1 , = 0 to 2. (5.10)
3 + = +1 = 0 to 2. (5.11)
31 1 + 1 = , (5.12)
which simplifies to
1 2 1 +1
+ ( + +1 )+1 + +1 +2 = , (5.15)
3 3 3 +1
Once the missing first and last equations are specified, the matrix equation
for the -coefficients can be solved by Gaussian elimination. And once the -
coefficients are determined, the - and -coefficients can also be determined from
(5.13) and (5.14), with the -coefficients already known from (5.8). The piece-
wise cubic polynomials, then, are known and () can be used for interpolation
to any value satisfying 0 .
The missing first and last equations can be specified in several ways, and
here we show the two ways that are allowed by the MATLAB function spline.m.
The first way should be used when the derivative () is known at the endpoints
0 and ; that is, suppose we know the values of and such that
0 (0 ) = ,
1 ( ) = .
From the known value of , and using (5.6) and (5.14), we have
= 0
0 1
= 0 (1 + 20 ).
0 3
2 1 0
0 0 + 0 1 = . (5.16)
3 3 0
From the known value of , and using (5.6), (5.13), and (5.14), we have
= 31 21 + 21 1 + 1
( ) ( )
1 1 1
=3 ( 1 ) 21 + 21 1 + 1 ( + 21 ) ,
31 1 3
5.4. MULTIDIMENSIONAL INTERPOLATION 33
which simplifies to
1 2 1
1 1 + 1 = , (5.17)
3 3 1
to be used as the missing last equation.
The second way of specifying the missing first and last equations is called
the not-a-knot condition, which assumes that
0 () = 1 (), 2 () = 1 ().
0 ( 0 )3 + 0 ( 0 )2 + 0 ( 0 ) + 0
= 1 ( 1 )3 + 1 ( 1 )2 + 1 ( 1 ) + 1 .
0 = 1 .
1 0 (0 + 1 )1 + 0 2 = 0, (5.18)
1 2 (2 + 1 )1 + 2 = 0. (5.19)
The MATLAB subroutines spline.m and ppval.m can be used for cubic spline
interpolation (see also interp1.m). I will illustrate these routines in class and
post sample code on the course web site.
= (, ).
= ( , ),
Integration
( /2)2
() = (/2) + ( /2) (/2) + (/2)
2
( /2)3 ( /2)4
+ (/2) + (/2) + . . .
6 24
( /2)2
(
= (/2) + ( /2) (/2) + (/2)
0 2
( /2)3 ( /2)4
)
+ (/2) + (/2) + . . . .
6 24
35
36 CHAPTER 6. INTEGRATION
= (/2)
/2 (
2 3 4
)
+ (/2) + (/2) + (/2) + (/2) + . . .
/2 2 6 24
/2 ( 4
)
= (/2) + 2 (/2) + (/2) + . . . .
0 12
Therefore,
3 5
= (/2) + (/2) + (/2) + . . . . (6.3)
24 1920
2 3 4
(0) = (/2) (/2) + (/2) (/2) + (/2) + . . . ,
2 8 48 384
and
2 3 4
() = (/2) + (/2) + (/2) + (/2) + (/2) + . . . .
2 8 48 384
( 3 5
(0) + () = (/2) + (/2) +
)
(/2) + . . . .
2 8 384
We now substitute for the first term on the right-hand-side using the midpoint
rule formula:
3 5
( )
(
(/2)
)
(0) + () = (/2)
2 24 1920
3 5
+ (/2) + (/2) + . . . ,
8 384
( ) 3 5
= (0) + () (/2) (/2) + . . . . (6.4)
2 12 480
6.2. COMPOSITE RULES 37
( ) 5
2 = (0) + 4 () + (2) () + . . . . (6.5)
3 90
= ( ), = +1 .
If the points are not evenly spaced, say because the data are experimental values,
then the may differ for each value of and (6.6) is to be used directly.
However, if the points are evenly spaced, say because () can be computed,
we have = , independent of . We can then define
= + , = 0, 1, . . . , ;
38 CHAPTER 6. INTEGRATION
= .
The composite trapezoidal rule for evenly space points then becomes
1
() = ( + +1 )
2 =0
= (0 + 21 + + 21 + ) . (6.7)
2
The first and last terms have a multiple of one; all other terms have a multiple
of two; and the entire sum is multiplied by /2.
The first and last terms have a multiple of one; the even indexed terms have a
multiple of 2; the odd indexed terms have a multiple of 4; and the entire sum is
multiplied by /3.
More important is the Global Error which is obtained from the composite
formula (6.7) for the trapezoidal rule. Putting in the remainder terms, we have
1
3
() = (0 + 21 + + 21 + ) ( ),
2 12 =0
= ,
so that the error term becomes
3 ( )2
( ) = ( )
12 12
= O(2 ).
Therefore, the global error is O(2 ). That is, a halving of the grid spacing only
decreases the global error by a factor of four.
Similarly, Simpsons rule has a local error of O(5 ) and a global error of
O(4 ).
= .
2
40 CHAPTER 6. INTEGRATION
( ) 5
= () + 4 () + () (),
3 90
where is some value satisfying .
Integration using Simpsons rule twice with grid size /2 yields
( ) (/2)5 (/2)5
= () + 4 () + 2 () + 4 () + () ( ) ( ),
6 90 90
with and some values satisfying and .
We now define
( )
1 = () + 4 () + () ,
3
( )
2 = () + 4 () + 2 () + 4 () + () ,
6
5
1 = (),
90
5 (
( ) + ( ) .
)
2 = 5
2 90
Now we ask whether 2 is accurate enough, or must we further refine the cal-
culation and go to Level 2? To answer this question, we make the simplifying
approximation that all of the fourth-order derivatives of () in the error terms
are equal; that is,
() = ( ) = ( ) = .
Then
5
1 = ,
90
5 1
2 = 4 = 1 .
2 90 16
Then since
1 + 1 = 2 + 2 ,
and
1 = 162 ,
we have for our estimate for the error term 2 ,
1
2 = (2 1 ).
15
Therefore, given some specific value of the tolerance tol, if
1
(2 1 ) < tol,
15
162 1
= + O(7 ),
15
where the error terms of O(5 ) approximately cancel. This free lunch, so to
speak, is called Richardsons extrapolation.
42 CHAPTER 6. INTEGRATION
Chapter 7
Ordinary differential
equations
43
44 CHAPTER 7. ORDINARY DIFFERENTIAL EQUATIONS
() = ,
1
2 + + = 0.
If the two solutions for are distinct and real, then the two found exponential
solutions can be multiplied by constants and added to form a general solution.
The constants can then be determined by requiring the general solution to satisfy
the two initial conditions. If the roots of the characteristic equation are complex
or degenerate, a general solution to the differential equation can also be found.
2
= (), (7.2)
2
with 0 1. Boundary conditions are usually prescribed at the end points of
the rod, and here we assume that the temperature at both ends are maintained
at zero so that
(0) = 0, (1) = 0.
so that the maximum of the heat source occurs in the center of the rod, and
goes to zero at the ends.
The differential equation can then be written as
2
= (1 ).
2
The first integration results in
= (2 )
3 2
= + 1 ,
3 2
7.1. EXAMPLES OF ANALYTICAL SOLUTIONS 45
0 = 2 ,
and at = 1, we have
1 1
0= + 1 ,
12 6
so that 1 = 1/12. Our solution is therefore
4 3
() = +
12 6 12
1
= (1 )(1 + 2 ).
12
The temperature of the rod is maximum at = 1/2 and goes smoothly to zero
at the ends.
2
+ 2 = 0,
2
with the two-point boundary conditions (0) = 0 and (1) = 0. Notice that
() = 0 satisfies both the differential equation and the boundary conditions.
Other nonzero solutions for = () are possible only for certain discrete values
of . These values of are called the eigenvalues of the differential equation.
We proceed by first finding the general solution to the differential equation.
It is easy to see that this solution is
() = cos + sin .
= 0.
sin = 0.
The corresponding negative values of are also solutions, but their inclusion
only changes the corresponding values of the unknown constant. A linear
superposition of all the solutions results in the general solution
() = sin .
=1
The obvious problem with this formula is that the unknown value +1 appears
on the right-hand-side. We can, however, estimate this value, in what is called
the predictor step. For the predictor step, we use the Euler method to find
+1 = + ( , ).
1 = ( , ),
2 = ( + , + 1 ), (7.4)
1
+1 = + (1 + 2 ).
2
1 = ( , ),
2 = ( + , + 1 ), (7.5)
+1 = + 1 + 2 ,
+1 = ( + )
1
) + ()2
= ( ) + ( ( ) + O(3 ).
2
Now,
(
) = ( , ).
The second derivative is more complicated and requires partial derivatives. We
have
]
( ) = (, ())
=
= ( , ) + (
) ( , )
= ( , ) + ( , ) ( , ).
48 CHAPTER 7. ORDINARY DIFFERENTIAL EQUATIONS
Therefore,
1 2 ( )
+1 = + ( , ) + () ( , ) + ( , ) ( , ) . (7.6)
2
Second, we compute +1 from the Runge-Kutta method given by (7.5).
Substituting in 1 and 2 , we have
( )
+1 = + ( , ) + + , + ( , ) .
+1 = + ( + ) ( , )
+ ()2 ( , ) + ( , ) ( , ) + O(3 ). (7.7)
( )
+ = 1,
= 1/2,
= 1/2.
There are three equations for four parameters, and there exists a family of
second-order Runge-Kutta methods.
The Modified Euler Method given by (7.4) corresponds to = = 1 and
= = 1/2. Another second-order Runge-Kutta method, called the Midpoint
Method, corresponds to = = 1/2, = 0 and = 1. This method is written
as
1 = ( , ),
( )
1 1
2 = + , + 1 ,
2 2
+1 = + 2 .
1 = ( , ),
2 = ( + , + 1 ),
3 = ( + , + 1 + 2 ),
+1 = + 1 + 2 + 3 .
order 2 3 4 5 6 7 8
minimum # stages 2 3 4 6 7 9 11
Because of the jump in the number of stages required between the fourth-
order and fifth-order method, the fourth-order Runge-Kutta method has some
appeal. The general fourth-order method starts with 13 constants, and one then
finds 11 constraints. A particularly simple fourth-order method that has been
widely used is given by
1 = ( , ),
( )
1 1
2 = + , + 1 ,
2 2
( )
1 1
3 = + , + 2 ,
2 2
4 = ( + , + 3 ) ,
1
+1 = + (1 + 22 + 23 + 4 ) .
6
= |+1 +1 |.
Now is of O(5 ), where is the step size taken. Let be the estimated
step size required to get the desired error . Then we have
/ = ()5 /( )5 ,
or solving for ,
( )1/5
= .
On the one hand, if < , then we accept +1 and do the next time step
using the larger value of . On the other hand, if > , then we reject
the integration step and redo the time step using the smaller value of . In
practice, one usually increases the time step slightly less and decreases the time
step slightly more to prevent the waste of too many failed time steps.
50 CHAPTER 7. ORDINARY DIFFERENTIAL EQUATIONS
= (, , ).
= ,
= (, , ).
This trick also works for higher-order equation. For another example, the third-
order equation
...
= (, , ,
),
can be written as
= ,
= ,
= (, , , ).
= (, , ),
= (, , ),
with the initial conditions (0) = 0 and (0) = 0 . The generalization of the
7.3. NUMERICAL METHODS: BOUNDARY VALUE PROBLEM 51
1 = ( , , ),
1 = ( , , ),
( )
1 1 1
2 = + , + 1 , + 1 ,
2 2 2
( )
1 1 1
2 = + , + 1 , + 1 ,
2 2 2
( )
1 1 1
3 = + , + 2 , + 2 ,
2 2 2
( )
1 1 1
3 = + , + 2 , + 2 ,
2 2 2
4 = ( + , + 3 , + 3 ) ,
4 = ( + , + 3 , + 3 ) ,
1
+1 = + (1 + 22 + 23 + 4 ) ,
6
1
+1 = + (1 + 22 + 23 + 4 ) .
6
2
= (), 0 1, (7.8)
2
with two-point boundary conditions
(0) = , (1) = .
1 1 3 1 4
( + ) = () + () + 2 () + () + () + . . . ,
2 6 24
1 2 1 3 1 4
( ) = () () + () () + () + . . . .
2 6 24
52 CHAPTER 7. ORDINARY DIFFERENTIAL EQUATIONS
1 + 22 3 = 2 2 ,
2 + 23 4 = 2 3 ,
...
M=diag(-ones(n-1,1),-1)+diag(2*ones(n,1),0)+diag(-ones(n-1,1),1); .
y=Mb;
2
= (, , /),
2
with (0) = and (1) = , we use a shooting method. First, we formulate
the ode as an initial value problem. We have
= ,
= (, , ).
54 CHAPTER 7. ORDINARY DIFFERENTIAL EQUATIONS
The initial condition (0) = is known, but the second initial condition (0) =
is unknown. Our goal is to determine such that (1) = .
In fact, this is a root-finding problem for an appropriately defined function.
We define the function = () such that
() = (1) .
In other words, () is the difference between the value of (1) obtained from
integrating the differential equations using the initial condition (0) = , and
. Our root-finding routine will want to solve () = 0. (The method is called
shooting because the slope of the solution curve for = () at = 0 is given
by , and the solution hits the value (1) at = 1. This looks like pointing a
gun and trying to shoot the target, which is .)
To determine the value of that solves () = 0, we iterate using the Secant
method, given by
1
+1 = ( ) .
( ) (1 )
We need to start with two initial guesses for , solving the ode for the two
corresponding values of (1). Then the Secant Method will give us the next value
of to try, and we iterate until |(1) | < tol, where tol is some specified
tolerance for the error.
1 2 + +1 = 2 2 . (7.10)
21 + 2 = 2 2 1 ,
1 2 = 2 2 .
7.4. NUMERICAL METHODS: EIGENVALUE PROBLEM 55
(2 + )2 1 = 0.
The solutions are = 1, 3, and since = 3 , we have = 3, 3 3 =
5.1962. These two eigenvalues serve as rough approximations to the first two
eigenvalues and 2.
With A an -by- matrix, the MATLAB variable mu=eig(A) is a vector
containing the eigenvalues of the matrix A. The built-in function eig.m can
therefore be used to find the eigenvalues. With grid points, the smaller eigen-
values will converge more rapidly than the larger ones.
We can also consider boundary conditions on the derivative, or mixed bound-
ary conditions. For example, consider the mixed boundary conditions given by
(0) = 0 and (1) = 0. The eigenvalues of (7.9) can then be determined
analytically to be = ( 1/2), with a natural number.
The difficulty we now face is how to implement a boundary condition on
the derivative. Our computation of uses a second-order method, and we
would like the computation of the first derivative to also be second order. The
condition (1) = 0 occurs on the right-most boundary, and we can make use of
the second-order backward-difference approximation to the derivative that we
have previously derived. This finite-difference approximation for (1) can be
written as
3+1 4 + 1
+1 = . (7.11)
2
Now, the th finite-difference equation was given by
1 2 + +1 = 2 ,
1 (
)
+1 = 2+1 + 4 1 .
3
56 CHAPTER 7. ORDINARY DIFFERENTIAL EQUATIONS
Implementing the boundary condition +1 = 0, we have
4 1
+1 = 1 .
3 3
Therefore, the th finite-difference equation becomes
2 2
1 = 2 2 .
3 3
For example, when = 2, the finite difference equations become
( ) ( ) ( )
2 1 1 1 2 1
2 = .
3 32 2 9 2
= ,
= 2 ,
() = 0, (7.12)
sin ()
() = .
The function () = (1) is therefore given by
sin
() = ,
and the roots occur when = , 2, . . . .
If the boundary conditions were (0) = 0 and (1) = 0, for example, then
we would simply redefine () = (1). We would then have
cos
() = ,
and the roots occur when = /2, 3/2, . . . .