Académique Documents
Professionnel Documents
Culture Documents
2
NB! These notes are used by myself in the course. They are provided to students as a
supplement to the textbook. They can not substitute the textbook.
Textbook:
Numerical Mathematics and Computing,
by Ward Cheney and David Kincaid, published by Brooks/Cole publishing Company,
2000. ISBN 0-534-35184-0
Contents
1 Computer arithmetic
1.1
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.2
10
1.3
12
1.4
Loss of significance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
13
1.5
14
2 Polynomial interpolation
19
2.1
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
19
2.2
Lagrange interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . .
21
2.3
22
2.4
25
2.5
Numerical differentiations . . . . . . . . . . . . . . . . . . . . . . . . . .
29
3 Splines
31
3.1
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
31
3.2
32
3.3
33
4 Numerical integration
39
4.1
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
39
4.2
Trapezoid rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
39
4.3
Simpsons rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
42
4.4
44
4.5
Romberg Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
45
4.6
47
. . . . . . . . . . . . . . . . . .
CONTENTS
4.7
49
51
5.1
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
51
5.2
Bisection method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
52
5.3
52
5.4
Newtons method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
56
5.5
Secant method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
58
5.6
59
61
6.1
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
61
6.2
62
6.3
63
6.4
LU-Factorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
66
6.5
67
6.6
69
73
7.1
General introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
73
7.2
Jacobi iterations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
74
7.3
Gauss-Seidal iterations . . . . . . . . . . . . . . . . . . . . . . . . . . . .
75
7.4
SOR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
76
7.5
77
7.6
79
8 Least Squares
81
8.1
Problem description . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
81
8.2
81
8.3
84
8.4
84
8.5
85
8.6
Non-linear LSM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
87
9 ODEs
89
9.1
89
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
CONTENTS
9.2
90
9.3
93
9.4
94
9.5
Multi-step methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
96
9.6
98
9.7
. . . . . . . . . . . . . . . . . . . .
99
9.8
Stiff systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
100
CONTENTS
List of Figures
1.1
1.2
12
1.3
16
2.1
29
3.1
Linear splines . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
33
4.1
40
4.2
42
4.3
43
4.4
45
4.5
Romberg triangle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
47
5.1
56
7.1
Splitting of A. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
78
8.1
Linear regression. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
82
LIST OF FIGURES
Chapter 1
Computer arithmetic
1.1
Introduction
What are numeric methods? They are algorithms that compute approximations to
solutions of equations or similar things.
Such algorithms should be implemented (programmed) on a computer.
physical model
verification
physical explanation of the results
mathematical model
presentation of results
* visualization
computer
programming
mathematical theorems
numerical analysis
10
1.2
integer part
fractional part
z
}|
{ z }| {
an an1 a1 a0 . b1 b2 b3
= an + an1
+b1
n1
+ b2
+ + a1 + a0
+ b3
(integer part)
(fractonal part)
Then,
(5034)8 = (|{z}
101 |{z}
000 |{z}
011 |{z}
100 )2
5
0
3
4
and
= (|{z}
6
110
2
|{z}
010
7
|{z}
111
1 )8
|{z}
001
12
6
3
1
0
0
1
1
(12)10 = (1100)2
fractional part
0.45
0.9
1.8
1.6
1.2
0.4
0.8
1.6
2
2
2
2
2
2
2
2
(0.45)10 = (0.01110011001100 )2 .
Put together:
(10.45)10 = (1100.01110011001100 )2
11
12
1.3
octal: x = r
10n ,
8n ,
21
81
r: normalized mantissa
r<1
d1 6= 0)
r<1
n: exponent
Computers represent numbers with finite length. These are called machine numbers.
In a 32-bit computer, with single-precision:
1 byte
8 bytes
radix point
23 bytes
sign of
mantissa
biased exponent
mantissa
fl(x) x
=
x
13
|| , where is called machine epsilon, which represents the smallest positive number
detectable by the computer, such that fl(1 + ) > 1.
In a 32-bit computer:
= 223 .
fl(y) = y(1 + y )
Then
fl(z) = fl (fl(x) + fl(y))
= (x(1 + x ) + y(1 + y )) (1 + z )
= (x + y) + x (x + z ) + y (y + z ) + (xx z + yy z )
(x + y) + x (x + z ) + y (y + z )
relative error =
1.4
(x + y) z
| {z }
round off err
xx + yy
fl(z) (x + y)
=
+
z
|{z}
x+y
x+y
| {z }
propagated err
round off err
Loss of significance
This typically happens when one gets too few significant digits in subtraction.
For example, in a 8-digit number:
x = 0.d1 d2 d3 d8 10a
d1 is the most significant digit, and d8 is the least significant digit.
Let y = 0.b1 b2 b3 b8 10a . We want to compute x y.
14
If b1 = d1 , b2 = d2 , b3 = d3 , then
x y = 0.000c4 c5 c6 c7 c8 10a
We lose 3 significant digits.
Example 1. Find the roots of x2 40x + 2 = 0. Use 4 significant digits in the
computation.
Answer. The roots for the equation ax2 + bx + c = 0 are
r1,2 =
In our case, we have
p
1
b b2 4ac
2a
x1,2 = 20
398 20 19.95
so
x1 20 + 19.95 = 39.95,
x2 20 19.95 = 0.05,
(OK)
2
c
=
0.05006
ax1
1 39.95
1.5
1
1
f (c)(x c)2 + f (c)(x c)3 +
2!
3!
X
1 (k)
f (x) =
f (c)(x c)k .
k!
k=0
X 1
1
1
f (0)x2 + f (0)x3 + =
f (k) (0)xk .
2!
3!
k!
k=0
15
X
xk
k=0
sin x =
k!
= 1+x+
x2 x3
+
+ ,
2!
3!
|x| <
X
x3 x5 x7
x2k+1
= x
+
+ ,
(1)k
(2k + 1)!
3!
5!
7!
|x| <
k=0
X
x2k
x2 x4 x6
cos x =
(1)k
= 1
+
+ ,
(2k)!
2!
4!
6!
k=0
1
1x
etc.
X
k=0
xk = 1 + x + x2 + x3 + x4 + ,
|x| <
|x| < 1
N
X
xk
k=0
k!
for some large integer N such that the error is sufficiently small.
Example 1. Compute e to 6 digit accuracy.
Answer. We have
e = e1 = 1 + 1 +
1
1
1
1
+ + + +
2! 3! 4! 5!
And
1
2!
1
3!
1
4!
1
9!
= 0.5
= 0.166667
= 0.041667
= 0.0000027
so
e 1+1+
1
1
1
1
1
+ + + + = 2.71828
2! 3! 4! 5!
9!
Error and convergence: Assume f (k) (x) (0 k n) are continuous functions. Call
fn (x) =
n
X
1 (k)
f (c)(x c)k
k!
k=0
16
X
1 (k)
1
= f (x) fn (x) =
f (c)(x c)k =
f (n+1) ()(x c)n+1
k!
(n + 1)!
k=n+1
- x
b
Figure 1.3: Mean Value Theorem
This implies
f () =
f (b) f (a)
ba
So, if a, b are close to each other, this can be used as an approximation for f .
Given h > 0 very small, we have
f (x)
f (x + h) f (x)
h
f (x)
f (x) f (x h)
h
f (x)
f (x + h) f (x h)
2h
17
n
X
X
1 (k)
1 (k)
f (x)hk =
f (x)hk + En+1
k!
k!
k=0
where
En+1 =
k=0
X
1
1 (k)
f (x)hk =
f (n+1) ()hn+1
k!
(n + 1)!
k=n+1
18
Chapter 2
Polynomial interpolation
2.1
Introduction
Problem description:
Given (n + 1) points, (xi , yi ), i = 0, 1, 2, , n, with distinct xi such that
x0 < x1 < x2 < < xn ,
find a polynomial of degree n
Pn (x) = a0 + a1 x + a2 x2 + + an xn
such that it interpolates these points:
Pn (xi ) = yi ,
i = 0, 1, 2, , n
20
Then
x = 0, y = 1 : P2 (0) = a0 = 1
x = 1, y = 0 : P2 (1) = a0 + a1 + a2 = 0
x = 2/3, y = 0.5 : P2 (2/3) = a0 + (2/3)a1 + (4/9)a2 = 0.5
In matrix-vector form
1 0
1 1
1 23
0
a0
1
1 a1 = 0
4
a2
0.5
9
a1 = 1/4,
a2 = 3/4.
3
1
P2 (x) = 1 x x2 .
4
4
i = 0, 1, 2, , n
or
1 x0 x20
1 x1 x2
1
.. ..
.. . .
. .
.
.
2
1 xn xn
a0
y0
xn0
n
x1 a1 y1
.. .. = ..
. . .
xnn
an
yn
X~a = ~y
where
x :
~a :
~y :
21
2.2
Lagrange interpolation
Given points: x0 , x1 , , xn
n
X
i=0
li (x) yi .
n
X
i=0
li (xj ) yi = yj ,
j.
n
Y
x xj
xi xj
j=0,j6=i
x x0 x x1
x xi1 x xi+1
x xn
xi x0 xi x1
xi xi1 xi xi+1
xi xn
One can easily check that li (xi ) = 1 and li (xj ) = 0 for i 6= j, i.e., li (xj ) = ij .
Example 2. Consider again (same as in Example 1)
xi 0 1 2/3
yi 1 0 0.5
Write the Lagrange polynomial.
22
Answer. We have
l0 (x) =
l1 (x) =
l2 (x) =
x 2/3 x 1
3
2
= (x )(x 1)
0 2/3 0 1
2
3
x1
9
x0
= x(x 1)
2/3 0 2/3 1
2
2
x 0 x 2/3
= 3x(x )
1 0 1 2/3
3
so
P2 (x) = l0 (x)y0 + l1 (x)y1 + l2 (x)y2
3
2
9
=
(x )(x 1) x(x 1)(0.5) + 0
2
3
2
3 2 1
= x x+1
4
4
This is the same as in Example 1.
Pros and cons
of Lagrange polynomial:
2.3
xi
yi
x0 x1
y0 y1
n=0
P0 (x) = y0
n=1
P1 (x) = P0 (x) + a1 (x x0 )
n=2
xn
yn
23
Assume that Pn1 (x) interpolates (xi , yi ) for i = 0, 1, , n 1. We will find Pn (x) that
interpolates (xi , yi ) for i = 0, 1, , n, in the form
Pn (x) = Pn1 (x) + an (x x0 )(x x1 ) (x xn1 )
where
an =
yn Pn1 (xn )
(xn x0 )(xn x1 ) (xn xn1 )
pn (x) = a0 + a1 (x x0 ) + a2 (x x0 )(x x1 ) +
+an (x x0 )(x x1 ) (x xn1 )
a1 = f [x0 , x1 ]
ai = f [x0 , x1 , , xi ]
f [x0 ] = y0
x1
f [x1 ] = y1
f [x0 , x1 ] =
f [x1 ]f [x0 ]
x1 x0
x2
f [x2 ] = y2
f [x1 , x2 ] =
f [x2 ]f [x1 ]
x2 x1
..
.
..
.
..
.
xn
f [xn ] = yn
f [xn1 , xn ] =
f [xn ]f [xn1 ]
xn xn1
f [x0 , x1 , x2 ] =
..
.
..
f [xn2 , xn1 , xn ] =
...
.
f [x0 , x1 , , xn ]
Example : Use Newtons divided difference to write the polynomial that interpolates
the data
xi 0 1 2/3 1/3
yi 1 0 1/2 0.866
Answer. Set up the trianglar table for computation
0
1
2/3
1/3
1
0
0.5
0.8660
-1
-1.5
-1.0981
-0.75
-0.6029
0.4413
24
So
P3 (x) = 1 + -1 x + -0.75 x(x 1) + 0.4413 x(x 1)(x 2/3).
Flexibility of Newtons form: easy to add additional points to interpolate.
Nested form:
q(xi ) = yi ,
i = 0, 1, , n
Now, let g(x) = p(x) q(x), which will be a polynomial of degree n. Furthermore,
we have
g(xi ) = p(xi ) q(xi ) = yi yi = 0,
i = 0, 1, , n
So g(x) has n + 1 zeros. We must have g(x) 0, therefore p(x) q(x).
25
2.4
i = 0, 1, , n
Y
1
e(x) =
f (n+1) () (x xi ),
(n + 1)!
i=0
Now assume f
/ Pn . For x = xi , we have e(xi ) = f (xi ) Pn (xi ) = 0, OK.
Now fix an a such that a 6= xi for any i. We define
n
Y
(x xi )
W (x) =
i=0
and a constant
c=
Pn+1
f (a) Pn (a)
,
W (a)
i = 0, 1, , n
and
(a) = f (a) Pn (a) cW (a) = 0
So, has at least (n + 2) zeros.
Here goes our deduction:
(x) has at least
(x) has at least
(x) has at least
(n+1) (x) has at least
n+2
n+1
n
..
.
zeros.
zeros.
zeros.
zero.
Call it .
26
So we have
(n+1) () = f (n+1) () 0 cW (n+1) () = 0.
Use
W (n+1) = (n + 1)!
we get
f (n+1) () = cW (n+1) () =
f (a) Pn (a)
(n + 1)!.
W (a)
Y
1
1
f (n+1) ()W (x) =
f (n+1) () (x xi ).
(n + 1)!
(n + 1)!
i=0
Example n = 1, x0 = a, x1 = b, b > a.
We have an upper bound for the error, for x [a, b],
|e(x)| =
1
1
(b a)2
1
f () |(x a)(x b)|
f
=
f
(b a)2 .
2
2
4
8
n
Y
i=0
h=
ba
,
n
|x xi |
i = 0, 1, , n.
1 n+1
h
n!
4
1
Mn+1 n+1
(n+1) n+1
h
(x) h
f
4(n + 1)
4(n + 1)
Mn+1 = max f (n+1) (x) .
x[a,b]
Example Consider interpolating f (x) = sin(x) with polynomial on the interval [1, 1]
with uniform nodes. Give an upper bound for error, and show how it is related with
total number of nodes with some numerical simulations.
27
(n+1)
(x)
f
n+1
n+1
|e(x)| = |f (x) Pn (x)|
4(n + 1)
n+1
2
.
n
error bound
4.8 101
3.2 103
1.8 109
measured error
1.8 101
1.2 103
6.6 1010
Problem with uniform nodes: peak of errors near the boundaries. See plots.
Chebychev nodes:
x
i = cos( ni ),
x
i = 12 (a + b) + 12 (b a) cos( ni ),
i = 0, 1, , n
k=0
|x x
k | = 2n
n
Y
k=0
i = 0, 1, , n
|x xk |
1
(n+1) n
(x) 2 .
f
(n + 1)!
Example Consider the same example with uniform nodes, f (x) = sin x. With Chebyshev nodes, we have
1
n+1 2n .
|e(x)|
(n + 1)!
The corresponding table for errors:
n
4
8
16
error bound
1.6 101
3.2 104
1.2 1011
measured error
1.15 101
2.6 104
1.1 1011
28
x
i =
i = 0, 1, , n
x 6= xi .
n
Y
(x xi ).
i=0
n
Y
(a xi ).
i=0
1 (n)
f (),
n!
[a, b].
Proof. Let Pn1 (x) interpolate f (x) at x0 , , xn1 . The error formula gives
n
1 (n) Y
f () (xn xi ),
n!
i=0
(a, b).
n
Y
(xn xi )
i=0
29
:
:
f [x0 , x1 , x2 ] =
2.5
1
1
[f (x + h) 2f (x) + f (x + h)] = f (),
2
2h
2
[x h, x + h].
Numerical differentiations
Finite difference:
(1)
f (x)
(2)
f (x)
(3)
f (x)
f (x)
1
(f (x + h) f (x))
h
1
(f (x) f (x h))
h
1
(f (x + h) f (x h))
(central difference)
2h
1
(f (x + h) 2f (x) + f (x h))
h2
f (x)
6
(1) (3)
f (x)
(2)
- x
xh
x+h
30
1
f (x + h) = f (x) + hf (x) + h2 f (x) +
2
1 2
1 3
h f (x) + O(h4 )
6
1 3
h f (x) + O(h4 )
6
Then,
1
f (x + h) f (x)
= f (x) + hf (x) + O(h2 ) = f (x) + O(h),
h
2
(1st order)
similarly
f (x) f (x h)
1
= f (x) hf (x) + O(h2 ) = f (x) + O(h),
h
2
(1st order)
and
1
f (x + h) f (x h)
= f (x) h2 f (x) + O(h2 ) = f (x) + O(h2 ),
2h
6
(2nd order)
finally
f (x + h) 2f (x) + f (x h)
1
= f (x)+ h2 f (4) (x)+O(h4 ) = f (x)+O(h2 ),
2
h
12
Richardson Extrapolation
Rombergs algorithm
(2nd order)
Chapter 3
Piece-wise polynomial
interpolation. Splines
3.1
Introduction
Usage:
visualization of discrete data
graphic design VW car design
Requirement:
interpolation
certain degree of smoothness
Disadvantages of polynomial interpolation Pn (x)
n-time differentiable. We do not need such high smoothness;
big error in certain intervals (esp. near the ends);
no convergence result;
Heavy to compute for large n
Suggestion: use piecewise polynomial interpolation.
Problem setting : Given a set of data
x t0 t1
y y0 y1
31
tn
yn
32
CHAPTER 3. SPLINES
S0 (x),
S1 (x),
S(x)=
..
Sn1 (x),
t0 x t1
t1 x t2
tn1 x tn
(ti ) = Si (ti ),
Si1
..
.
(n1)
(n1)
Si1 (ti ) = Si
(ti ),
3.2
i = 0, 1, , n 1
Requirements:
S0 (t0 ) = y0
Sn1 (tn ) = yn .
i = 1, 2, , n 1
Easy to find: write the equation for a line through two points: (ti , yi ) and (ti+1 , yi+1 ),
Si (x) = yi +
yi+1 yi
(x ti ),
ti+1 ti
i = 0, 1, , n 1.
33
S(x) 6
y1
y2
y0
y3
t0
t1
t2
t3
h = max(ti+1 ti )
i
Let f (x) be a given function, and let S(x) be a linear spline that interpolates f (x) s.t.
S(ti ) = f (ti ),
i = 0, 1, , n
1
h max f (x) .
x
2
1
|f (x) S(x)| h2 max f (x) .
x
8
Quadratics splines.
3.3
Given t0 < t1 < < tn , we define the cubic spline S(x) = Si (x) for ti x ti+1 . We
(t ) = 0,
require that S, S , S are all continuous. If in addition we require S0 (t0 ) = Sn1
n
then it is called natural cubic spline.
34
CHAPTER 3. SPLINES
Write
Si (x) = ai x3 + bi x2 + ci x + di ,
i = 0, 1, , n 1
Equations we have
equation
(1) Si (ti ) = yi ,
i = 0, 1, , n 1
i = 0, 1, , n 1
(3)
i = 0, 1, , n 2
Si (ti+1 )
(t ),
Si+1
i
(t ),
(4) Si (ti+1 ) = Si+1
i
i = 0, 1, , n 2
(5) S0 (t0 ) = 0,
(t ) = 0,
(6) Sn1
n
:
:
:
number
n1
n1
1.
total = 4n.
polynomial of degree 3
polynomial of degree 2
polynomial of degree 1
procedure:
Start with Si (x), they are all linear, one can use Lagrange form,
Integrate Si (x) twice to get Si (x), you will get 2 integration constant
Determine these constants by (2) and (1). Various tricks on the way...
Details: Define zi as
zi = S (ti ),
i = 1, 2, , n 1,
z0 = zn = 0
Si (x) =
zi+1
zi
(x ti ) (x ti+1 ).
hi
hi
Then
Si (x) =
Si (x) =
zi+1
(x ti )2
2hi
zi+1
(x ti )3
6hi
zi
(x ti+1 )2 + Ci Di
2hi
zi
(x ti+1 )3 + Ci (x ti ) Di (x ti+1 ).
6hi
35
zi
1
(hi )3 Di (hi ) = zi h2i + Di hi
6hi
6
zi+1 3
h + Ci hi ,
6hi i
Ci =
Di =
yi
hi
zi
hi
6
yi+1 hi
zi+1 .
hi
6
We see that, once zi s are known, then (Ci , Di )s are known, and so Si , Si are known.
zi+1
yi+1 hi
zi
3
3
Si (x) =
(x ti )
(x ti+1 ) +
zi+1 (x ti )
6hi
6hi
hi
6
yi
hi
zi (x ti+1 ).
hi
6
yi+1 yi zi+1 zi
zi
z
i+1
(x ti )2
(x ti+1 )2
hi .
Si (x) =
2hi
2hi
hi
6
How to compute zi s? Last condition thats not used yet: continuity of S (x), i.e.,
Si1
(ti ) = Si (ti ),
i = 1, 2, , n 1
We have
yi+1 yi zi+1 zi
zi
(hi )2 +
hi
2hi
hi
6
| {z }
bi
1
1
= hi zi+1 hi zi + bi
6
3
1
1
Si1
(ti ) =
zi1 hi1 + zi hi1 + bi1
6
3
Set them equal to each other, we get
hi1 zi1 + 2(hi1 + hi )zi + hi zi+1 = 6(bi bi1 ),
i = 1, 2, , n 1
z0 = zn = 0.
Si (ti ) =
In matrix-vector form:
where
H ~z = ~b
2(h0 + h1 )
h
h1
2(h1 + h2 )
h2
h2
2(h2 + h3 )
h3
H=
..
..
..
.
.
.
hn3
2(hn3 + hn2 )
hn2
hn2
2(hn2 + hn1 )
36
CHAPTER 3. SPLINES
and
z1
z2
z3
..
.
~z =
,
zn2
zn1
6(b1 b0 )
6(b2 b1 )
6(b3 b2 )
..
.
~b =
.
6(bn2 bn3 )
6(bn1 bn2 )
Note that
b
2
S (x) dx
b
2
f (x) dx.
Cubic spline gives the least curvature, most smooth, so best choice.
Proof. Let
g(x) = f (x) cS(x)
Then
g(ti ) = 0,
i = 0, 1, , n
and f = S + g , so
(f )2 = (S )2 + (g )2 + 2S g
Z b
Z b
Z b
Z b
2
2
2
2S g dx
(g ) dx +
(S ) dx +
(f ) dx =
a
Claim that
b
a
S g dx = 0
(f )2 dx
b
a
(S )2 dx
37
b Z b
S g dx
S g dx = S g
a
Since g(a) = g(b) = 0, so the first term is 0. For the second term, since S is piecewise
constant. Call
ci = S (x), for x [ti , ti+1 ].
Then
b
a
S g dx =
n1
X
i=0
ci
ti+1
g (x) dx =
ti
n1
X
i=0
ci [g(ti+1 ) g(ti )] = 0,
38
CHAPTER 3. SPLINES
Chapter 4
Numerical integration
4.1
Introduction
Given a function f (x) on interval [a, b], find an approximation to the inte-
Problem:
gral
I(f ) =
f (x) dx
a
Main idea:
Cut up [a, b] into smaller sub-intervals
In each sub-interval, find a polynomial pi (x) f (x)
Integrate pi (x) on each sub-interval, and sum up
4.2
Trapezoid rule
cut up [a, b] into n sub-intervals:
The grid:
x0 = a,
xi < xi+1 ,
xn = b
xi+1
xi
f (x) dx
xi+1
pi (x) dx =
xi
39
1
(f (xi+1 ) + f (xi )) (xi+1 xi )
2
40
pi (x)
f (x)
x0 = a
xi
xi+1
xn = b
f (x) dx =
n1
X Z xi+1
i=0 xi
n1
X Z xi+1
ba
n , xi+1
1
(f (xi+1 ) + f (xi )) (xi+1 xi )
2
so we can write
xi = h,
f (x) dx =
a
pi (x) dx
i=0 xi
n1
X
i=0
Uniform grid: h =
f (x) dx
n1
X
i=0
h
(f (xi+1 ) + f (xi ))
2
"
#
n1
X
1
1
= h f (x0 ) +
f (xi ) + f (xn )
2
2
i=1
{z
}
|
T (f ; h)
Z
b
a
f (x) T (f ; h)
Error estimates.
ET (f ; h) = I(f ) T (f ; h) =
n1
X Z xi+1
i=0
xi
f (x) pi (x) dx
41
Total error is
#
"n1
1 3
1 ba
1 3 X
h f (i ) = h
ET,i (f ; h) =
f (i )
12
12
n | {z
h }
i=0
i=0
| i=0 {z
}
= f ()
=n
n1
X
n1
X
ET (f ; h) =
b a 2
h f (),
12
Error bound
ET (f ; h)
(a, b).
ba 2
h max f (x) .
12
x(a,b)
Require error 0.5 104 . How many points should be used in the Trapezoid rule?
Answer. We have
f (x) = ex ,
so
f (x) = ex ,
a = 0,
b=2
max f (x) = e2 .
x(a,b)
1
|ET (f ; h)| h2 e2 0.5 104
6
h2 0.5 104 6 e2 4.06 105
p
2
= h 4.06 105 = 0.0064
n
2
n
313.8
0.0064
We need at least 314 points.
42
4.3
Simpsons rule
x0 = a,
x2n = b,
h=
ba
,
2n
xi+1 xi = h
Consider the interval [x2i , x2i+2 ]. Find a 2nd order polynomial that interpolates f (x) at
the points
x2i , x2i+1 , x2i+2
pi (x)
f (x)
x2i
x2i+1 x2i+2
Figure 4.2: Simpsons rule: quadratic polynomial approximation (thick line) in each
sub-interval.
Lagrange form gives
pi (x) = f (x2i )
43
x2i+2
f (x) dx
x2i
x2i+2
pi (x) dx
x2i
1
f (x2i )f (x2i )
2h2
1
f (x2i+1 )
h2
|
1
+ 2 f (x2i+2 )
2h
x2i+2
x2i
x2i+2
x2i
Z
|
x2i+2
x2i
h
[f (x2i ) + 4f (x2i+1 ) + f (x2i+2 )]
3
Putting together
Z
b
a
f (x) dx S(f ; h)
=
n1
X Z x2i+2
pi (x) dx
x2i
i=0
n1
X
h
3
i=0
1
=2
1
x2i2
x2i1
x2i
x2i+1
x2i+2
i=1
44
1 5 (4)
h f (i ),
90
i (x2i , x2i+2 )
so
n1
X
1 ba
1
b a 4 (4)
f (4) (i )
ES (f ; h) = I(f ) S(f ; h) = h5
=
h f (),
90
n 2h
180
(a, b)
i=0
Error bound
|ES (f ; h)|
ba 4
h max f (4) (x) .
180
x(a,b)
Example With f (x) = ex in [0, 2], now use Simpsons rule, to achieve an error
0.5 104 , how many points must one take?
Answer. We have
2 4 2
h e 0.5 104
180
h4 0.54 180/e2 = 1.218 103
|ES (f ; h)|
h 0.18682
ba
= 5.3 6
n=
2h
We need at least 2n + 1 = 13 points.
4.4
ba
,
2n
hn+1 =
1
h
2
So
#
n 1
2X
1
1
f (a + ihn )
f (a) + f (b) +
T (f ; hn ) = hn
2
2
i=1
2n+1
1
X
1
1
T (f ; hn+1 ) = hn+1 f (a) + f (b) +
f (a + ihn+1 )
2
2
"
i=1
45
n 1
n 1
2X
2X
1
hn 1
f (a + (2j + 1)hn+1 )
f (a + ihn ) +
f (a) + f (b) +
T (f ; hn+1 ) =
2 2
2
i=1
j=1
2n 1
X
1
f (a + (2j + 1)hn+1 )
T (f ; hn ) + hn+1
2
j=1
Advantage: One case keep the computation for a level n. If this turns out to be not
accurate enough, then add one more level to get better approximation. flexibility.
4.5
Romberg Algorithm
If f (n) exists and is bounded, then we have the Euler MacLaurins formula for error
E(f ; h) = I(f ) T (f ; h) = a2 h2 + a4 h4 + a6 h6 + + an hn
h
h
h
h
h
h
E(f ; ) = I(f ) T (f ; ) = a2 ( )2 + a4 ( )4 + a6 ( )6 + + an ( )n
2
2
2
2
2
2
Here an depends on the derivatives f (n) .
We have
(1)
(2)
I(f ) = T (f ; h) + a2 h2 + a4 h4 + a6 h6 +
h
h
h
h
h
I(f ) = T (f ; ) + a2 ( )2 + a4 ( )4 + a6 ( )6 + + an ( )n
2
2
2
2
2
The goal is to use the 2 approximations T (f ; h) and T (f ; h2 ) to get one thats more
accurate, i.e., we wish to cancel the leading error term, the one with h2 .
Multiply (2) by 4 and subtract (1), gives
3 I(f ) = 4 T (f ; h/2) T (f ; h) + a4 h4 + a6 h6 +
4
1
a4 h4 + a
6 h6 +
I(f ) =
T (f ; h/2) T (f ; h) +
3
3
{z
}
|
U (h)
46
T (f ; h/2) T (f ; h)
22 1
I(f ) = U (h) + a
4 h4 + a
6 h6 +
I(f ) = U (h/2) + a
4 (h/2)4 + a
6 (h/2)6 +
24 U (h/2) U (h)
U (h/2) U (h)
= U (h/2) +
.
4
2 1
24 1
I(f ) = V (h) + a
6 h6 +
H
(f (a) + f (b))
2
R(1, 0) = T (f ; H/2)
R(2, 0) = T (f ; H/(22 ))
..
.
R(n, 0) = T (f ; H/(2n ))
Here R(n, 0)s are computed by the recursive trapezoid formula.
Romberg triangle: See Figure 4.5.
The entry R(n, m) is computed as
R(n, m) = R(n, m 1) +
R(n, m 1) R(n 1, m 1)
22m1
Accuracy:
I(f ) = R(n, m) + O(h2(m+1) ),
h=
H
.
2n
47
R(1, 0)
s
-
R(2, 0)
s
-
R(3, 0)
R(n, 0)
R(1, 1)
s
-
R(2, 1)
s
-
R(3, 1)
R(n, 1)
R(2, 2)
s
-
R(3, 2)
R(n, 3)
R(n, n)
for i = 1 to n 1 do
end
for j = 2 to n do
%2 to $n$ column
for i = j to n do
R(i, j) = R(i, j 1) +
end
1
4j 1
end
4.6
ba
2 ,
ba
a+b
S1 [a, b] =
) + f (b)
f (a) + 4f (
6
2
Error form:
E1 [a, b] =
1 5 (4)
h f (),
90
(a, b)
48
Then
I(f ) = S1 [a, b] + E1 [a, b]
Divide [a, b] up in the middle, let c =
a+b
2 .
h
i
1
(h/2)5 f (4) (1 ) + f (4) (2 )
90
Assume f (4) does NOT change much, then E1 [a, c] E1 [c, b], and
E2 [a, b] 2E1 [a, c] = 2
1
1
E1 [a, b] = 4 E1 [a, b]
25
2
This gives
S2 [a, b] S1 [a, b] = (I E2 [a, b]) (I E1 [a, b]) = E1 E2 = 24 E2 E2 = 15E2
This means, we can compute the error E2 :
E2 =
1
(S2 S1 )
15
24 1
This gives the idea of an adaptive recursive formula:
(A)
I = S1 + E1
(B)
I = S2 + E2 = S2 +
1
E1
24
(24 1)I = 24 S2 S1
I=
S2 S1
24 S2 S1
= S2 +
24 1
15
Note that this gives the best approximation when f (4) const.
Pseudocode: f : function, [a, b] interval, : tolerance for error
49
In Matlab, one can use quad to compute numerical integration. Try help quad, it will
give you info on it. One can call the program by using:
a=quad(fun,a,b,tol)
It uses adaptive Simpsons formula.
See also quad8, higher order method.
4.7
i = 0, 1, , n
q0 (x) = 1
q1 (x) = x
3 2
q2 (x) =
x
2
5 3
x
q3 (x) =
2
1
2
3
x
2
50
q2 : 1/ 3
p
q3 : 0, 3/5
For general interval [a, b], use the transformation:
t=
1
1
x = (b a)t + (a + b)
2
2
2x (a + b)
,
ba
so for 1 t 1 we have a x b.
Weights Ai : Recall li (x), the Cardinal form in Lagrange polynomial:
n
Y
li (x) =
j=0,j6=i
Then
Ai =
x xj
xi xj
li (x) dx.
a
There are tables of such nodes and weights (Table 5.1 in textbook).
We skip the proof. See textbook if interested.
Advantage: Since all nodes are in the interior of the interval, these formulas can handle integrals of function that tends to infinite value at one end of the interval (provided
that the integral is defined). Examples:
Z
dx,
1
0
(x2 1)1/3
sin(ex 1) dx.
Chapter 5
Introduction
Problem: f (x) given function, real-valued, possibly non-linear. Find a root r of f (x)
such that f (r) = 0.
Example 1. Quadratic polynomials: f (x) = x2 + 5x + 6.
f (x) = (x + 2)(x + 3) = 0,
r1 = 2,
r2 = 3.
52
5.2
Bisection method
a+b
2 ,
mid-point.
bn a n
b0 a0
e0
n+1 = n .
2
2
2
2n+1
log(b a) log(2)
,
log 2
(# of steps)
5.3
53
stop here
x = g(x) = e2x (x 1) + x
Choose an initial guess x0 = 0.99, very close to the real root. Iterations:
x1 = cos x0 = 0.9886
x2 = cos x1 = 0.9870
x3 = cos x2 = 0.9852
..
.
x27 = cos x26 = 0.1655
x28 = cos x27 = 0.4338
54
Convergence analysis.
= g ()ek
Observation:
|ek+1 | g () |ek |
OK, convergence.
In Example 2, we have
g(x) = e2x (x 1) + x,
g (r = e2 + 1 > 1
Pseudo code:
r=fixedpoint(g, x,tol,nmax}
r=g(r);
nit=1;
while (abs(r-g(r))>tol and nit < nmax) do
r=g(r);
nit=nit+1;
end
55
|e1 | m |e0 | ,
|ek | mk |e0 |
We also have
|e0 | = |r x0 | = |r x1 + x1 x0 | |e1 | + |x1 x0 | m |e0 | + |x1 x0 |
then
|e0 |
1
|x1 x0 | ,
1m
(can be computed)
Put together
|ek |
mk
|x1 x0 | .
1m
mk
(1 m)
|x1 x0 |
Example cos x x = 0, so
x = g(x) = cos x,
g (x) = sin x
And x1 = cos x0 = cos 1 = 0.5403. Then, to achieve an error = 105 , the maximum
# iterations needed is
k
Of course that is the worst situation. Give it a try and you will find that k = 25 is
enough.
56
f (x) 6
f (xk )
xk+1
xk
5.4
Goal:
Newtons method
Given f (x), find a root r s.t. f (r) = 0.
f (xk )
= f (xk )
xk xk+1
which gives
xk+1 = xk
f (xk )
= xk xk ,
f (xk )
xk =
f (xk )
.
f (xk )
b(x)f (x) = 0,
Here the function b(x) is chosen in such a way, to get fastest possible convergence. We
have
g (x) = 1 b (x)f (x) b(x)f (x)
Let r be the root, such that f (r) = 0, and r = g(r). We have
g (r) = 1 b(r)f (r),
smallest possible: g (r) = 0.
Choose now
1 b(x)f (x) = 0,
b(x) =
f (x)
.
f (x)
1
f (x)
57
Continue like this, we conclude that ek+1 < ek for all k, i.e., error is strictly decreasing
after each iteration. convergence.
xk+1 = xk
x2k a
xk
a
f (xk )
=
x
=
+
k
f (xk )
2xk
2
2xk
= 1.7
= 1.7324
= 1.7321
= 1.7321
error
7.2 102
3.0 104
2.6 108
4.4 1016
Note the extremely fast convergence. Usually, if the initial guess is good (i.e., close to
r), usually a couple of iterations are enough to get an very accurate approximation.
58
Stop criteria:
|xk xk1 |
|f (xk )|
max number of iterations reached.
Sample Code:
r=newton(f,df,x,nmax,tol)
n=0; dx=f(x)/df(x);
while (dx > tol) and (f(x) > tol) and (n<nmax) do
n=n+1;
x=x-dx;
dx=f(x)/df(x);
end
r=x-dx;
5.5
Secant method
f (xk ) f (xk1 )
xk xk1
xk xk1
f (xk )
f (xk ) f (xk1 )
Advantages include
No computation of f ;
One f (x) computation each step;
Also rapid convergence.
A bit on convergence: One can show that
ek+1 Cek ,
1
= (1 + 5) 1.62
2
59
a.
k
xk + xk+1
(x2k a) (x2k1 a)
x1
x2
x3
x4
x5
= 1.7
= 1.7328
= 1.7320
= 1.7321
= 1.7321
error
7.2 102
7.9 104
7.3 106
1.7 109
3.6 1015
5.6
F = (f1 , f2 , , fn )t ,
~x = (x1 , x2 , , xn )t
f1 (x1 , x2 , , xn ) = 0
f2 (x1 , x2 , , xn ) = 0
..
fn (x1 , x2 , , xn ) = 0
60
x1
f2
x
1
Df =
..
.
f
n
x1
f1
x2
f2
x2
..
.
fn
x2
..
f1
xn
f2
xn
..
.
fn
xn
Chapter 6
Introduction
The problem:
(A) :
= b1
= b2
..
.
(1)
(2)
(n)
aij xj = bi ,
i = 1, , n.
j=1
Or in matrix-vector form:
A~x = ~b,
~x IRn , ~b IRn
A = {aij } = .
..
.. ,
..
..
.
.
.
an1 an2 ann
where A IRnn ,
x1
x2
~x = . ,
..
xn
b1
b2
~b =
.. .
.
bn
62
6.2
Consider system (A). The basic Gaussian elimination takes two steps:
Step 1: Make an upper triangular system forward elimination.
for k = 1, 2, 3, , n 1
(j) (j) (k)
ajk
akk ,
j = k + 1, k + 2, , n
a22 x2 + + a2n xn
(B) :
ann xn
= b1
= b2
..
.
(1)
(2)
= bn
(n)
Note, here the aij and bi are different from those in (A).
Step 2: Backward substitution you get the solutions.
xn =
xi =
bn
ann
n
X
1
aij xj ,
bi
aii
j=i+1
i = n 1, n 2, , 1.
Potential problem: In step 1, if some akk is very close to or equal to 0, then you are in
trouble.
Forward elimination:
x + x2 + x3
= 1
1
2x1 + 4x2 + 4x3
= 2
(1)
(2)
(3)
Backward substitution:
(2 )
(3 )
x + x2 + x3 = 1
1
2x2 + 2x3 = 0
3x3 = 3
x3 = 1
1
x2 =
(0 2x3 ) = 1
2
x1 = 1 x2 x3 = 1
6.3
(1)
(2)
x2 = 1.00
(1)
(2)
63
64
1
1
(1.00 + 1.00x2 ) =
0 0
0.001
0.001
.
Note that x1 is wrong!!
What is the problem? We see that 0.001 is a very small number!
One way around this difficulty: Change the order of two equations.
1.00x1
2.00x2 = 3.00
(1)
0.00100x1 1.00x2 = 1.00
(2)
Now run the whole procedure again: (1) (0.001) + (2) will give us x2 = 1.00.
Set it back in (1):
a11 x1 + a12 x2 = b1
a21 x1 + a22 x2 = b2
x
1 =
Note that 1 =
a12
a11 2 .
a12
a11 .
1jn
ak1 ai1
sk si ,
i = 1, , n
(1)
a
x
+
+
a
x
=
b
(2)
22
2
2n
n
2
..
.
a
x
+
+
a
x
=
b1
(k)
12 2
1n n
..
an2 x2 + + ann xn = bn
(n)
x1 + 2x2 + x3 =
pivoting.
3
(1)
(2)
10
(3)
~s = [2, 4, 10]
2. We have
a21
a31
1
3
2
a11
= ,
= ,
= , k=2
s1
2
s2
4
s3
10
Exchange eq (1) and (2), and do one step of elimination
2
(1 ) = (1) + (2) ( 31 )
3 x2 + x3 = 2
22
8
(3 ) = (3) + (2) ( 23 )
3 x2 + 4x3 =
a
12
2/3
1
=
= ,
s1
2
3
Exchange (3) and (1) and
22
3 x2 + 4x3
7
11 x3
a
32
22/3
22
=
=
s3
10
30
k = 3.
=
=
8
14
11
(2)
(3 )
1
(1 ) = (1 ) + (3 ) ( 11
)
65
66
x2 = 0,
x1 = 1.
6.4
LU-Factorization
0
1
0
0
1
..
.
l21
L = l31 l32
..
..
.
.
ln1 ln2 ln3
0
0
0
,
..
.
u11 u12
0 u22
U = ...
0
0
0
0
..
.
u1,(n1)
u2,(n1)
..
.
u(n1),(n1)
0
u1n
u2n
..
.
u(n1),n
unn
Ux = y
Ly = b
Two triangular system. We first solve y (by forward substitution), then solve x (by
backward substitution).
With pivoting:
LU = P A
where P is the pivoting matrix.
Used in Matlab:
67
> [L,U]=lu(A);
> y = L \ b;
> x = U \ y;
+ transparences.
Work amount for direct solvers for A IRnn : operation count
1
3
3 (n
n) flops
Backward substitution:
1
2
2 (n
n) flops
1 3
3n
for large n.
This is very slow for large n. We will need something more efficient.
6.5
n
X
j=1,j6=i
|aij | ,
i = 1, 2, , n
then A is called strictly diagonal dominant. And A has the following properties:
A is regular, invertible, A1 exists, and Ax = b has a unique solution.
Ax = b can be solved by Gaussian Elimination without pivoting.
One such example: the system from natural cubic spline.
Vector and matrix norms:
A norm: measures the size of the vector and matrix.
General norm properties:
a: is a constant;
68
2. kxk2 =
n
X
i=1
|xi |,
n
X
x2i
i=1
l1 -norm
!1/2
l2 -norm
l -norm
1in
kAxk
kxk
Obviously we have
kAk
In addition we have
kAxk
kxk
kIk = 1,
1jn
n
X
i=1
|aij |
i : eigenvalues of A
1in
n
X
j=1
|aij |
Eigenvalues i for A:
Av = v,
(A I)v = 0,
: eigenvalue,
det(A I) = 0 :
v : eigenvector
polynomial of degree n
1
i (A)
This implies
1
A
= max i (A1 ) = max
2
i
1
1
=
1
|i (A )|
mini |i (A1 )|
69
kpk
kbk
k
x xk
kxk
We wish to find a relation between ex and eb . We have
ex =
A(
x x) = p,
so
x
x = A1 p
1
1
A p
A
kpk
k
x xk
ex =
=
.
kxk
kxk
kxk
kAXk = kbk
we get
Here
1
kAk
kxk
kbk
kAk
= kAk
A1
eb = (A)eb ,
ex
A1
kpk
kbk
maxi |i |
(A) = kAk
A1
=
mini |i |
is called the condition number of A. Error in b propagates with a factor of (A) into the
solution.
If (A) is very large, Ax = b is very sensitive to perturbation, therefore difficult to solve.
We call this ill-conditioned system.
In Matlab: cond(A) gives the condition number of A.
6.6
d1 c1 0
0
0
0
a1 d2 c2
0
0
0
0 a2 d3 . . .
0
0
0
.
..
.. ..
..
A=
..
.
.
.
.
0 0 0 dn2 cn2
0
70
or we can write
A = tridiag(ai , di , ci ).
This can be solved very efficiently:
Elimination (without pivoting)
for i = 2, 3, , n
di di adi1
ci1
i1
end
bi bi
ai1
di1 bi1
d1
0
.
A=
..
0
0
c1
d2
0
0
0
0
0
c2
..
.
d3
..
..
.
.
0
0
0
0
0
0
0
0
0
..
.
dn2 cn2
0
dn1
0
0
Backward substitution
0
0
..
.
cn1
dn
xn bn /dn
for i = n 1, n 2, , 1
xi d1i (bi ci xi+1 )
end
Amount of work: O(Cn) where C is a fixed small constant.
We can write
Penta-diagonal system.
A = pentadiag(ei , ai , di , ci , fi )
which means
d1
a1
e1
A=
0
0
c1
d2
a2
..
.
0
0
0
f1
c2
d3
..
.
..
.
0
0
0
0
f2
c3
..
.
..
.
..
.
0
0
0
0
f3
.. ..
.
.
.. ..
.
.
.. ..
.
.
0
0
0
..
..
0
0
0
..
.
..
0
0
0
..
.
.
71
..
.
d
1
.
.
. d2
. . . . . .
.
.
A =
.. ..
..
description:
..
..
..
..
..
..
..
..
..
..
..
..
..
..
..
d3
..
.
..
.
..
.
d4
..
.
..
.
..
.
..
|k|
..
.
..
.
..
.
dn
diagonal matrix: k = 0,
tridiagonal matrix: k = 1,
pentadiagonal matrix: k = 2.
Gaussian elimination is efficient if k << n.
Some Matlab commands:
[L,U] = lu(A);
norm(x);
eig(A);
cond(A);
%
%
%
%
LU-factorization
vector norm
eigenvalue/eigen vector of a matrix
condition number of A
72
Chapter 7
General introduction
Gauss-Seidal
SOR
74
7.2
Jacobi iterations
x1 =
x2 =
1
a11
1
a22
xn =
1
ann
..
.
or in a compact for:
n
X
1
aij xj ,
bi
xi =
aii
j=1,j6=i
i = 1, 2, , n
n
X
1
xk+1
=
bi
aij xkj
i
aii
j=1,j6=i
end
end
75
=
2x1 x2
x1 + 2x2 x3 =
x2 + 2x3 =
iterations.
0
1
2
k+1
x1
=
xk+1
=
2
k+1
x3
=
1 k
2 x2
1
2 (1
+ xk1 + xk3 )
1
2 (2
+ xk2 )
x1 = (0.25, 1, 1.25)t
x2 = (0.5, 1.25, 1.5)t
x3 = (0.625, 1.5, 1.625)t
Observations:
Looks like it is converging. Need to run more steps to be sure.
Rather slow convergence rate.
7.3
Gauss-Seidal iterations
n
X
1
bi
=
xk+1
aij xkj
i
aii
j=1,j6=i
n
i1
X
X
1
aij xkj
aij xkj
bi
=
aii
j=1
j=i+1
In the first summation term, all xkj are already computed for step k + 1.
We will replace all these xkj with xk+1
. This gives:
j
76
Gauss-Seidal iterations:
n
i1
X
X
1
bi
xk+1
=
aij xkj
aij xk+1
i
j
aii
j=1
j=i+1
end
end
+ xk3 )
xk+1
= 12 (1 + xk+1
1
2
k+1
x3
= 12 (2 + xk+1
2 )
We run a couple of iterations:
7.4
SOR
n
i1
X
X
1
aij xkj
aij xk+1
bi
xk+1
= (1 w)xki + w
j
j
aii
j=1
j=i+1
w: relaxation parameter.
Usual value: 0 < w < 2 (for convergence reason)
x1
= 0.2xk1 + 0.6xk2
+ xk3 )
xk+1
= 0.2xk2 + 0.6 (1 + xk+1
1
2
k+1
x3
= 0.2xk3 + 0.6 (2 + xk+1
2 )
7.5
Want to solve Ax = b.
Splitting of the matrix A:
A=L+D+U
where
L is the lower triangular part of A:
L = {lij },
lij =
aij ,
0
i>j
ij
dij =
aij = aii ,
0
i=j
i 6= j
uij =
aij ,
0
i<j
ij
77
78
D
L
MJ = D 1 (L + U ).
Gauss-Seidal:
Dxk+1 + Lxk+1 = b U xk
so
xk+1 = (D + L)1 b (D + L)1 U xk = yGS + MGS xk
where
yGS = (D + L)1 b,
MGS = (D + L)1 U.
SOR:
xk+1 = (1 w)xk + wD 1 (b Lxk+1 U xk )
so
xk+1 = (D + wL)1 b + (D + wL)1 [(1 w)D wU ]xk = ySOR + MSOR xk
where
ySOR = (D + wL)1 b,
7.6
79
k
e
M k
e0
,
e0 = x0 s.
Theorem If kM k < 1 for some norm kk, then the iterations converge.
NB! Convergence only depends on the iteration matrix M .
Check our methods: A = D + L + U .
Jacobi: M = D 1 (L + U ), given by A;
G-S: M = (D + L)1 U , given by A;
SOR: M = (D + wL)1 [(1 w)D wU ], can adjust w to get a smallest posible
kM k. More flexible.
Example Lets check the same example we have been using. We have
2 1 0
A = 1 2 1 ,
0 1 2
80
so
0
0 0
L = 1 0 0 ,
0 1 0
2 0 0
D = 0 2 0 ,
0 0 2
0 0.5 0
0 0.5
0
MJ = 0.5 0 0.5 ,
MGS = 0 0.25 0.5 ,
0 0.5 0
0 0.125 0.25
0 1 0
U = 0 0 1 .
0 0
0
MSOR
0.2
0.6
0
= 0.12 0.16 0.6
0.072 0.096 0.16
l1 norm
1
0.875
0.856
l2 norm
0.707
0.5
0.2
l norm
1
0.75
0.88
The l2 norm is the most significant one. We see now why SOR converges fastest.
Convergence Theorem.
|aii | >
n
X
j=1,j6=i
|aij | ,
i = 1, 2, , n.
Then, all three iteration methods converge for all initial choice of x0 .
NB! If A is not diagonal dominant, it might still converge, but there is no guarantee.
Chapter 8
Problem description
x0
y0
x1
y1
x2
y2
xm
yM
8.2
Example 1. If y = ax + b, this is called linear regression. Your lab data would not lie
exact on a straight line (or your lab instructor will be very suspicious!). Our job now is
to find a straight line that best fit our data. See Figure 8.1 for an illustration.
A more specific way of saying the same thing: Find a, b, such that when we use y = ax+b,
the error becomes smallest possible.
How to measure error?
1. max |y(xk ) yk | l norm
k
2.
m
X
k=0
|y(xk ) yk | l1 norm
81
82
3.
m
X
[y(xk ) yk ]2 l2 norm, used in Least Square Method. (LSM)
k=0
m
X
(axk + b yk )2
k=0
is minimized.
How to find a and b?
At the minimum of a function, we have
=
=0
a
b
In detail:
m
X
2(axk + b yk )xk = 0,
=0 :
a
=0 :
b
k=0
m
X
k=0
2(axk + b yk ) = 0,
(I)
(II)
!
!
m
m
m
X
X
X
xk a +
xk b =
xk y k
k=0
k=0
k=0
!
m
m
X
X
a
+
(m
+
1)
b
=
yk
k
k=0
k=0
Tk
Sk
where
k=0
7
X
k=0
7
X
k=0
Tk Sk = = 22685
7
X
k=0
Sk = = 514.5
Solve it
a = 0.079930,
b = 67.9593.
So
S(T ) = 0.079930 T + 67.9593.
83
84
8.3
2
Example 3. Given m + 1 data (xk , yk )m
k=0 . Find y(x) = ax + bx + c that best fit our
data.
m
X
k=0
At minimum, we have
ax2k + bxk + c yk
2
=
=
=0
a
b
c
In detail:
m
X
=0 :
a
k=0
m
X
=0 :
b
k=0
m
X
=0 :
c
k=0
m
X
xk a +
k=0
m
X
x3k a +
k=0
xk a +
k=0
m
X
2 ax2k + bxk + c yk x2k = 0
2 ax2k + bxk + c yk xk = 0
2 ax2k + bxk + c yk = 0
k=0
m
X
k=0
xk
x2k
b+
b + (m + 1) c
k=0
m
X
x2k
x3k
xk
b+
m
X
k=0
m
X
k=0
c =
c =
=
m
X
x2k yk
k=0
m
X
k=0
m
X
xk y k
yk
k=0
8.4
x2k ,
xk ,
x2k yk ,
xk y k ,
yk
85
g(x) = ln(x),
h(x) = cos x,
k=0
At minimum, we have
=0 :
a
=0 :
b
=0 :
c
m h
i
X
2 a f (xk ) + b g(xk ) + c h(xk ) yk f (xk ) = 0
k=0
m h
i
X
2 a f (xk ) + b g(xk ) + c h(xk ) yk g(xk ) = 0
k=0
m h
i
X
2 a f (xk ) + b g(xk ) + c h(xk ) yk h(xk ) = 0
k=0
m
m
m
m
X
X
X
X
f (xk )yk
f (xk ) a +
f (xk )h(xk ) c =
f (xk )g(xk ) b +
k=0
k=0
k=0
k=0
!
!
!
m
m
m
m
X
X
X
X
2
f (xk )g(xk ) a +
g(xk ) b +
h(xk )g(xk ) c =
g(xk )yk
k=0
k=0
k=0
k=0
!
!
!
m
m
m
m
X
X
X
X
f
(x
)h(x
)
a
+
f
(x
)h(x
)
b
+
c
=
h(xk )yk
h(x
)
k
k
k
k
k
k=0
k=0
k=0
k=0
We note that the system of normal equations is always symmetric. We only need to
compute half of the entries.
8.5
y(x) =
n
X
i=0
ci gi (x)
86
How to choose the basis functions? They are chosen such that the system of the
normal equations is regular (invertible) and well-conditioned.
ci gi (x) = 0,
if and only if c0 = c1 = c2 = = cn = 0.
i=0
m h
X
k=0
y(xk ) yk
i2
" n
m
X
X
k=0
i=0
ci gi (x) yk
#2
At minimum, we have
= 0,
cj
j = 0, 1, , n.
This gives:
m
X
k=0
"
n
X
i=0
ci gi (xk ) yk gj (xk ) = 0
k=0
m
X
gj (xk )yk ,
k=0
j = 0, 1, , n.
aij =
~b = {bj },
bj =
m
X
k=0
m
X
gj (xk )yk .
k=0
87
8.6
Non-linear LSM
a
= ln a,
b = ln b.
a = exp{
a},
Example 6. Non-linear LSM. For example,
y(x) = ax sin(bx).
We can not find a variable change that can change this problem into a linear one. So
we will now deal with it as a non-linear problem.
Define error
(a, b) =
m h
X
k=0
y(xk ) yk
i2
m h
X
k=0
i2
axk sin(bxk ) yk .
At minimum:
=0:
a
=0:
a
m h
i
X
2 axk sin(bxk ) yk [xk sin(bxk )] = 0
k=0
m h
i
X
2 axk sin(bxk ) yk [axk cos(bxk )xk ] = 0
k=0
May have several solutions, including all the maximum, minimum and saddle points.
(see slides)
88
Chapter 9
Introduction
x + x x + 4 = 0,
etc.
x(0) = 0.
solution:
x = 2,
x(0) = 0.
solution:
x(t) = et 1
x(t) = 2t.
tk = t0 + kh
Overview:
89
90
CHAPTER 9. ODES
Taylor series method, and error estimates
Runge-Kutta methods
Multi-step methods
System of ODE
High order equations and systems
Stiff systems
Matlab solvers
9.2
Given
x (t) = f (t, x(t)),
x(t0 ) = x0 .
X
1 2
1 m (m)
x(t0 + h) = x(t0 ) + hx (t0 ) + h x (t0 ) + =
h x (t0 )
2
m!
m=0
1
1 m (m)
x(t0 + h) = x(t0 ) + hx (t0 ) + h2 x (t0 ) + +
h x (t0 ).
2
m!
Error in each step:
x(t0 + h) x1 =
k=m+1
1 k (k)
1
h x (t0 ) =
hm+1 x(m+1) ()
k!
(m + 1)!
k = 0, 1, 2,
For m = 2, we have
1
x1 = x0 + hx (t0 ) + h2 x (t0 )
2
91
d
f (t0 , x(t0 )) = ft (t0 , x0 ) + fx (t0 , x0 ) x (t0 ) = ft (t0 , x0 ) + fx (t0 , x0 ) f (t0 , x0 )
dt
we get
1
x1 = x0 + hf (t0 , x0 ) + h2 [ft (t0 , x0 ) + fx (t0 , x0 ) f (t0 , x0 )]
2
For general step k, we have
1
xk+1 = xk + hf (tk , xk ) + h2 [ft (tk , xk ) + fx (tk , xk ) f (tk , xk )]
2
Example 2. Set up Taylor series methods with m = 1, 2 for
x = x + et ,
x(0) = 0.
x(0) = 1.
x = x = x,
x(m) = x
92
CHAPTER 9. ODES
We have
m=1:
m=2:
m=3:
m=4:
See slides.
Given ODE
Error analysis.
x = f (t, x),
x(t0 ) = x0 .
Local truncation error (error in each time step) for Taylor series method of order m is
m+1 m
m+1
h
h
d f ()
(k)
(m+1)
, (tk , tk+1 ).
x
() =
eL = |xk+1 x(tk + h)| =
(m + 1)!
(m + 1)! dtm
x(m+1) =
We have
(k)
eL
dm f
dtm
m
d f
dtm M.
M
hm+1 = O(hm+1 ).
(m + 1)!
k=1
M
hm+1
(m + 1)!
M
MT
M
hm+1 = (N h)
hm =
hm = O(hm )
= N
(m + 1)!
(m + 1)!
(m + 1)!
93
9.3
K1 = h f (tk , xk )
K2 = h f (tk+1 , xk + K1 )
xk+1 = xk +
94
CHAPTER 9. ODES
K1
K2
K3
K
m
= h f (tk , xk )
= h f (tk + a2 h, x + b2 K1 )
= h f (tk + a3 h, x + b3 K1 + c3 K2 )
..
.
= h f (tk + am h, x +
Pm1
i=1
i Ki )
where
9.4
In general, we have
1. smaller time step h gives smaller error;
2. The error varies at each step, depending on f .
Optimal situation: h varies each step to get uniform error at each step.
95
But this is rather wasteful of computing time. Although the idea is good.
A better method, by Fehlberg, building upon R-K methods. He has a 4th order method:
x(t + h) = x(t) +
1408
2197
1
25
K1 +
K3 +
K4 K5 ,
216
2565
4104
5
where
K1 = h f (t, x)
1
1
K2 = h f (t + h, x + K1 )
4
4
3
3
9
K3 = h f (t + h, x + K1 + K2 )
8
32
32
12
1932
7200
7296
K4 = h f (t + h, x +
K1
K2 +
K3 )
13
2197
2197
2197
439
3680
845
K5 = h f (t + h, x +
K1 8K2 +
K3
K4 )
216
513
4104
Adding an additional term:
8
3544
1859
11
1
K3 +
K4 K5 )
K6 = h f (t + h, x K1 + 2K2
2
27
2565
4104
40
We obtain a 5th order method:
x
(t + h) = x(t) +
6656
28561
9
2
16
K1 +
K3 +
K4 K5 + K6 .
135
12825
56430
50
55
96
CHAPTER 9. ODES
Given t0 , x0 , h0 , nmax , emin , emax , hmin , hmax
set h = h0 , x = x0 , k = 0,
while k < nmax do
if h < hmin then h = hmin
else if h > hmax then h = hmax
end
Compute RK4, RK5, and e = |RK4 RK5|
if e > emax , then h = h/2;
else
k=k+1; t=t+h; x=RK5;
If e < emin , the h = 2 h; end
end
end (while)
9.5
Multi-step methods
Given
x = f (t, x),
x(t0 ) = x0 ,
Let tn = t0 + nh. If x(tn ) is given, the exact value for x(tn=1 ) would be
x(tn+1 ) = x(tn ) +
tn+1
x (s) ds
tn
Assume we know xn , xn1 , xn2 , , xnk one can approximate the integrand x (s) by
using interpolating polynomial.
Example Consider k = 1. Given xn , xn1 , we can compute fn , fn1 as
fn = f (tn , xn ),
fn fn1
(s tn1 ).
h
tn+1
P1 (s) ds = xn +
tn
h
(3fn fn1 ).
2
97
and
fn , fn1 , , , fnk
Disadvantage: Here we use interpolating polynomial to approximate a function outside the interval of interpolating points. This gives bigger error.
Improved version.
(fn , tn ),
and use
xn+1 = xn +
which will always be in the form
(fnk , tnk ),
tn+1
Pk+1 (s) ds
tn
xn+1 = xn + h fn+1 ,
(implicit backward Eulers method)
h
xn+1 = xn + (fn + fn+1 ),
(trapezoid rule)
2
3
1
5
fn+1 + fn fn1
xn+1 = xn + h
12
12
12
98
CHAPTER 9. ODES
Given xn , xn1 , fn , fn1 , compute AB solution with k = 1:
3
1
xn+1 = xn + h
fn fn1
(P )
2 2
fn=1 = f tn+1 , xn+1 .
h
fn+1 + fn
xn+1 = xn +
(C)
2
f
=
f
(t
n+1
n+1 , xn+1 )
Here step (P ) is called the predictor, and step (C) is the corrector.
This is called predictor-correctors method.
9.6
We consider
~x = F (t, ~x),
~x(t0 ) = ~x0
=
x
1
x2 =
xn =
f1 (t, x1 , x2 , , xn )
f2 (t, x1 , x2 , , xn )
fn (t, x1 , x2 , , xn )
Remark: All methods for scalar equation can be used for systems!
Taylor series methods:
1 m (m)
1
h ~x
~x(t + h) = ~x + h~x + h2 ~x + +
2
m!
Example Consider
x1 = x1 x2 + 2t t2 t3
x2 = x1 + x2 4t2 + t3
99
and so on...
x
= x1 x2 2 6t
1
x2 = x1 + x2 8 + 6t
9.7
given.
x2 = x ,
x3 = x ,
xn = x(n1)
x1 = x = x2
x2 = x = x3
x3 = x = x4
..
.
x
= x(n1) = xn
n1
xn = x(n) = f (t, x1 , x2 , , xn )
100
CHAPTER 9. ODES
9.8
Stiff systems
A system is called stiff if the solution consists of components that vary with very
different speed/frequency.
Example Consider
x = ax,
x(0) = 1
x 0 as
t +.
xn = (1 ah)n x0 = (1 ah)h
as
n +,
We must have
2
a
which gives a restriction to the time step size: it has to be sufficiently small.
|1 ah| < 1,
h<
x(0) = 2
y(0) = 0
x(t) = e39t + et
y(t) = e39t et
Observations:
The solution tends to 0, i.e., x 0, y 0 as t +.
Two components in the solution, e39t and et ;
They decay at a very different rate. The term e39t tends to 0 much faster than
the term et ;
For large values of t, the term et dominate.
Therefore, e39t is called the transient term.
101
x0 = 2
y0 = 0
yn 0
as
n +.
and
|1 h| < 1
2
39
and
(2) : h < 2
which implies
(1) : h <
We see that condition (1) is much stronger than condition (2), therefore it must be
satisfied.
Condition (1) corresponds to the term e39t , which is the transient term and it tends
to 0 very quickly as t grows. Unfortunately, time step size is restricted by this transient
term.
A more stable method: Backward Euler, implicit method
(
xn+1 = xn + h (20xn+1 19yn+1 )
yn+1 = yn + h (19xn+1 20yn+1 )
Let
A=
20 19
,
19 20
~x =
x
,
y
~xn =
xn
.
yn
We can write
102
CHAPTER 9. ODES
We see that if
(I hA)1
< 1, then ~xn 0 as n +.
(I hA)1
= max i (I hA)1 = max
i
We have
1 (A) = 1,
1
|(1 h i (A))|
2 (A) = 39
Disadvantage:
Longer computing time for each step. Not recommended if the system is not stiff.