Numerical Analysis Lecture Ch.01 06
Numerical Analysis Lecture Ch.01 06
Lectures on YouTube:
https://www.youtube.com/channel/UCmRbK4vlGDht-joOQ5g0J2Q
Seongjai Kim
Currently the lecture note is not fully grown up; other useful techniques and interesting examples
would be soon incorporated. Any questions, suggestions, comments will be deeply appreciated.
Seongjai Kim
June 26, 2021
iii
iv
Contents
Title ii
Prologue iii
1 Mathematical Preliminaries 1
1.1. Review of Calculus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.1.1. Continuity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.1.2. Differentiability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.1.3. Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.1.4. Taylor’s Theorem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.2. Review of Linear Algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.2.1. Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
1.2.2. System of linear equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
1.2.3. Invertible (nonsingular) matrices . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.2.4. Determinants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.2.5. Eigenvectors and eigenvalues . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.2.6. Vector and matrix norms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.3. Computer Arithmetic and Convergence . . . . . . . . . . . . . . . . . . . . . . . . . . 25
1.3.1. Computational algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
1.3.2. Big O and little o notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
1.4. Programming with Matlab/Octave . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
Exercises for Chapter 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
v
vi Contents
Bibliography 227
viii Contents
Index 229
Chapter 1
Mathematical Preliminaries
1
2 Chapter 1. Mathematical Preliminaries
In other words, if for every ε > 0, there exists a δ > 0 such that
• f is continuous at x
• If {xn }∞
n=1 is any sequence in X converging to x , then
1.1.2. Differentiability
Example 1.12. Let f (x) = x +sin x be defined on [0,2]. Find c which assigns
the average slope.
Figure 1.2: The resulting figure, from the implementation in Figure 1.1.
Example 1.14. Find the absolute minimum and absolute maximum val-
ues of f (x) = 5 ∗ cos(2 ∗ x) − 2 ∗ x ∗ sin(2 ∗ x) on the interval [1, 2].
Maple-code
1 a := 1: b := 2:
2 f := x -> 5*cos(2*x) - 2*x*sin(2*x):
3 fa := f(a);
4 = 5 cos(2) - 2 sin(2)
5 fb := f(b);
6 = 5 cos(4) - 4 sin(4)
7
13 fsolve(fp(x), x, a..b);
14 1.358229874
15 fc := f(%);
16 -5.675301338
17 Maximum := evalf(max(fa, fb, fc));
18 = -0.241008123
19 Minimum := evalf(min(fa, fb, fc));
20 = -5.675301338
6 Chapter 1. Mathematical Preliminaries
21
22 with(plots);
23 plot([f(x), fp(x)], x = a..b, thickness = [2, 2],
24 linestyle = [solid, dash], color = black,
25 legend = ["f(x)", "f'(x)"],
26 legendstyle = [font = ["HELVETICA", 10], location = right]);
1.1.3. Integration
Remark 1.19. When g(x) ≡ 1, the WMVT becomes the usual Mean
Value Theorem on Integral, which gives the average value of f ∈
C[a, b] over the interval [a, b]:
ˆ b
1
f (c) = f (x)dx. (1.9)
b −a a
8 Chapter 1. Mathematical Preliminaries
Example 1.21. Let f (x) = cos(x) and x0 = 0. Determine the second and
third Taylor polynomials for f about x0 .
Maple-code
1 f := x -> cos(x):
2 fp := x -> -sin(x):
3 fpp := x -> -cos(x):
4 fp3 := x -> sin(x):
5 fp4 := x -> cos(x):
6
18 # On the other hand, you can find the Taylor polynomials easily
19 # using built-in functions in Maple:
20 s3 := taylor(f(x), x = 0, 4);
21 = 1 - 1/2 x^2 + O(x^4)
22 convert(s3, polynom);
23 = 1 - 1/2 x^2
1.1. Review of Calculus 9
Figure 1.4: f (x) = cos x and its third Taylor polynomial P3 (x).
where ˆ x
1
En (x) = f (n+1) (t) · (x − t)n dt.
n! x0
10 Chapter 1. Mathematical Preliminaries
where
1 ∂ ∂ n+1
Rn (h, k ) = h +k f (x + θh, y + θk ),
(n + 1)! ∂ x ∂y
in which θ ∈ [0, 1].
where
R1 (h, k ) = O(h 2 + k 2 ).
Equation (1.16), as a linear approximation or tangent plane ap-
proximation, will be used for various applications.
1.1. Review of Calculus 11
2x + 3
f (x, y) = at (0, 0).
4y + 1
Maple-code
1 f := (2*x + 3)/(4*y + 1):
2 f0 := eval(%, {x = 0, y = 0});
3 = 3
4 fx := diff(f, x);
5 = 2/(4*y + 1)
6 fx0 := eval(%, {x = 0, y = 0});
7 = 2
8 fy := diff(f, y);
9 = 4*(2*x + 3)/(4*y + 1)^2
10 fy0 := eval(%, {x = 0, y = 0});
11 = -12
12
The above algebraic system can be solved by the elementary row operations
applied to the augmented matrix, augmented system:
a11 a12 · · · a1n b1
21 a22 · · · a2n b2
a
[A b] = . .. . . . .. .. , (1.23)
.. . . .
am1 am2 · · · amn bm
Example 1.34. Solve the following system of linear equations, using the
3 EROs. Then, determine if the system is consistent.
4x2 + 2x3 = 6
x1 − 4x2 + 2x3 = −1
4x1 − 8x2 + 12x3 = 8
Solution.
Answer: y = 2 − 2x + x 2
16 Chapter 1. Mathematical Preliminaries
Theorem 1.40.
" #
a b
a. (Inverse of a 2 × 2 matrix) Let A = . If ad − bc 6= 0, then
c d
A is invertible and
" #
1 d −b
A −1 = (1.24)
ad − bc −c a
1.2.4. Determinants
Definition 1.42. Let A be an n × n square matrix. Then determinant
is a scalar value denoted by det A or |A |.
1) Let A = [a] ∈ R1 × 1 . Then det A = a .
" #
a b
2) Let A = ∈ R2 × 2 . Then det A = ad − bc .
c d
" #
2 1
Example 1.43. Let A = . Consider a linear transformation T : R2 → R2
0 3
defined by T (x) = A x.
Solution.
Answer: c. 12
Note: The determinant can be viewed as the volume scaling factor.
1.2. Review of Linear Algebra 19
Answer: −2
20 Chapter 1. Mathematical Preliminaries
Proof.
c1 v1 + c2 v2 + · · · + cp vp = vp+1 (1.31)
c1 A v1 + c2 A v2 + · · · + cp A vp = A vp+1
• Multiplying both sides of (1.31) by λp+1 and subtracting the result from
(1.32), we have
c1 (λ1 − λp+1 )v1 + c2 (λ2 − λp+1 )v2 + · · · + cp (λp − λp+1 )vp = 0. (1.33)
c1 = c2 = · · · = cp = 0 ⇒ vp+1 = 0,
which is a contradiction.
22 Chapter 1. Mathematical Preliminaries
Definition 1.57. Once a vector norm || · || has been specified, the in-
duced matrix norm is defined by
kA xk
kA k = max . (1.39)
x6=0 kxk
It is also called an operator norm or subordinate norm.
Theorem 1.58.
a. For all operator norms and the Frobenius norm,
kAx k ≤ kA k kx k, kA B k ≤ kA k kB k. (1.40)
kA xk1 X
b. kA k1 ≡ max = max |aij |
x6=0 kxk1 j
i
kA xk∞ X
c. kA k∞ ≡ max = max |aij |
x6=0 kxk∞ i
j
kA xk2 p
d. kA k2 ≡ max = λmax (A T A ),
x6=0 kxk2
where λmax denotes the largest eigenvalue.
e. kA k2 = kA T k2 .
f. kA k2 = max |λi (A )|, when A T A = AA T (normal matrix).
i
24 Chapter 1. Mathematical Preliminaries
κ(A ) ≡ kA k kA −1 k
a. Find kA k1 , kA k∞ , and kA k2 .
b. Compute the `1 -condition number κ1 (A ).
Solution.
1.3. Computer Arithmetic and Convergence 25
Algorithms consist of various steps for inputs, outputs, and functional op-
erations, which can be described effectively by a so-called pseudocode.
Definition 1.68.
• A sequence {αn }∞ ∞
n=1 is said to be in O (big Oh) of {βn }n=1 if a positive
number K exists for which
|αn |
|αn | ≤ K |βn |, for large n or equivalently, ≤K . (1.46)
|βn |
In this case, we say “αn is in O (βn )" and denote αn ∈ O (βn ) or αn =
O (βn ).
• A sequence {αn } is said to be in o (little oh) of {βn } if there exists a
sequence εn tending to 0 such that
|αn |
|αn | ≤ εn |βn |, for large n or equivalently, lim = 0 . (1.47)
n→∞ |βn |
Solution.
Answer: k = 3.
1
Self-study 1.74. Let f (h) = (1 + h − e h ). What are the limit and the rate
h
of convergence of f (h) as h → 0?
Solution.
1.3. Computer Arithmetic and Convergence 31
a. e x − 1 = O(x 2 ), as x → 0
b. x = O(tan−1 x), as x → 0
c. sin x cos x = o(1), as x → 0
Solution.
The most basic thing you will need Vectors and Matrices
to do is to enter vectors and matri- 1 >> u = [1; 2; 3] % column vector
2 u=
ces. You would enter commands to 3 1
Matlab or Octave at a prompt that 4 2
looks like >>. 5 3
6 >> v = [4; 5; 6];
7 >> u + 2*v
• Rows are separated by semi- 8 ans =
colons (;) or Enter . 9 9
10 12
• Entries in a row are separated 11 15
by commas (,) or space Space . 12 >> w = [5, 6, 7, 8] % row vector
13 w=
For example, 14 5 6 7 8
15 >> A = [2 1; 1 2]; % matrix
16 >> B = [-2, 5
17 1, 2]
18 B=
19 -2 5
20 1 2
21 >> C = A*B % matrix multiplication
22 C=
23 -3 12
24 0 9
1.4. Programming with Matlab/Octave 33
You can save the commands in a file to run and get the same results.
tutorial1_vectors.m
1 u =
[1; 2; 3]
2 v =
[4; 5; 6];
3 u +
2*v
4 w =
[5, 6, 7, 8]
5 A =
[2 1; 1 2];
6 B =
[-2, 5
7 1, 2]
8 C = A*B
Solving equations
1 −4 2 3
Let A = 0 3 5 and b = −7 . Then A x = b can be numerically
2 8 −4 −3
solved by implementing a code as follows.
tutorial2_solve.m Result
1 A = [1 -4 2; 0 3 5; 2 8 -4]; 1 x =
2 b = [3; -7; -3]; 2 0.75000
3 x = A\b 3 -0.97115
4 -0.81731
tutorial3_plot.m
1 close all
2
3 %% a curve
4 X1 = linspace(0,2*pi,10); % n=10
5 Y1 = cos(X1);
6
7 %% another curve
8 X2=linspace(0,2*pi,20); Y2=sin(X2);
9
10 %% plot together
11 plot(X1,Y1,'-or',X2,Y2,'--b','linewidth',3);
12 legend({'y=cos(x)','y=sin(x)'},'location','best',...
13 'FontSize',16,'textcolor','blue')
14 print -dpng 'fig_cos_sin.png'
while loop
The syntax of a while loop in Matlab is as follows.
while <expression>
<statements>
end
An expression is true when the result is nonempty and contains all nonzero
elements, logical or real numeric; otherwise the expression is false. Here
is an example for the while loop.
n1=11; n2=20;
sum=n1;
while n1<n2
n1 = n1+1; sum = sum+n1;
end
fprintf('while loop: sum=%d\n',sum);
When the code above is executed, the result will be:
while loop: sum=155
36 Chapter 1. Mathematical Preliminaries
for loop
A for loop is a repetition control structure that allows you to efficiently
write a loop that needs to execute a specific number of times. The syntax
of a for loop in Matlab is as following:
for index = values
<program statements>
end
Here is an example for the for loop.
n1=11; n2=20;
sum=0;
for i=n1:n2
sum = sum+i;
end
fprintf('for loop: sum=%d\n',sum);
When the code above is executed, the result will be:
for loop: sum=155
4 s=0;
5 for i=n1:n2
6 s = s+i;
7 end
1.1. Prove that the following equations have at least one solution in the given intervals.
(a) Find the third Taylor polynomial of f about x = x0 , p3 (x), and use it to approxi-
mate f (0.2).
(b) Use the Taylor’s Theorem (Theorem 1.20) to find an upper bound for the error
|f (x) − p3 (x)| at x = 0.2. Compare it with the actual error.
(c) Find the fifth Taylor polynomial of f about x = x0 , p5 (x), and use it to approximate
f (0.2).
(d) Use the Taylor’s Theorem to find an upper bound for the error |f (x) − p5 (x)| at
x = 0.2. Compare it with the actual error.
1
The mark C indicates that you should solve the problem via computer programming. Attach hard
copies of your code and results. For other problems, if you like and doable, you may try to solve them with
computer programming.
38 Chapter 1. Mathematical Preliminaries
Through the chapter, the objective is to find solutions for equations of the
form
f (x) = 0. (2.1)
Various numerical methods will be considered for the solutions of (2.1).
Although the methods will be derived for a simple form of equations, they
will be applicable for various general problems.
39
40 Chapter 2. Solutions of Equations in One Variable
f (x) = 0. (2.2)
Theorem 2.3. Suppose that f ∈ C[a, b] and f (a) · f (b) < 0. Then, the
Bisection method generates a sequence pn approximating a zero p of f
with
b −a
|p − pn | ≤ n
, n ≥ 1. (2.3)
2
Proof. For n ≥ 1,
1
bn − an = (b − a) and p ∈ (an , bn ). (2.4)
2 n −1
It follows from pn = (an + bn )/2 that
1 1
|p − pn | ≤ (bn − an ) = n (b − a), (2.5)
2 2
which completes the proof.
Note: The right-side of (2.3) is the upper bound of the error in the n-th
iteration.
Example 2.6. Suppose that the bisection method begins with the interval
[45, 60]. How many steps should be taken to compute a root with a relative
error not larger than 10−8 ?
Solution.
9 fa=feval(f,a);
10 fb=feval(f,b);
11 if fa*fb > 0,return,end
12 max1=1+round((log(b-a)-log(TOL))/log(2));
13
14 for k=1:max1
15 c=(a+b)/2;
16 fc=feval(f,c);
17 if fc==0
18 a=c; b=c;
19 elseif fa*fc<0
20 b=c; fb=fc;
21 else
22 a=c; fa=fc;
23 end
24 if b-a < TOL, break,end
25 end
26
Example 2.7. You can call the above algorithm with varying function, by
|p − pn+1 |
Example 2.8. In the bisection method, does lim exist?
n→∞ |p − pn |
Solution.
Answer: no
2.2. Fixed-Point Iteration 47
for some h(x). Then, since g(p) = p − h(p) · f (p) = p − 0 = p , Equation (2.10)
defines a fixed-point problem.
Proof.
Example 2.13. Show that g(x) = (x 2 − 2)/3 has a unique fixed point on
[−1, 1].
Solution.
2.2. Fixed-Point Iteration 49
This implies that the limit p is a fixed point of g , i.e., the iteration
converges to a fixed point.
pn = g(pn−1 ), n ≥ 1, (2.14)
Proof.
• It follows from Theorem 2.12 that there exists a unique fixed point p ∈
[a, b], i.e., p = g(p) ∈ [a, b].
• Since g(x) ∈ [a, b] for all x ∈ [a, b], we have pn ∈ [a, b] for all n ≥ 1. It
follows from the MVT that
which converges to 0 as n → ∞.
52 Chapter 2. Solutions of Equations in One Variable
• For m > n ≥ 1,
|pm − pn | = |pm − pm−1 + pm−1 − · · · − pn+1 + pn+1 − pn |
≤ |pm − pm−1 | + |pm−1 − pm−2 | + · · · + |pn+1 − pn |
≤ K m−1 |p1 − p0 | + K m−2 |p1 − p0 | + · · · + K n |p1 − p0 |
= K n |p1 − p0 |(1 + K + K 2 + · · · + K m−1−n ).
(Here we have used the MVT, for the last inequality.) Thus,
∞
n
X
i Kn
|p − pn | = lim |pm − pn | ≤ K |p1 − p0 | K = |p1 − p0 |.
m→∞ 1−K
i=0
That is,
Kn
|p − pn | ≤ |p1 − p0 |. (2.17)
1−K
• The Fixed-Point Theorem holds for any contractive mapping g de-
fined on any closed subset C ⊂ R. By a contractive mapping, we
mean a function g that satisfies for some 0 < K < 1,
|g(x) − g(y)| ≤ K |x − y | for all x, y ∈ C. (2.18)
and therefore
1 K
|p − pn | ≤ |pn+1 − pn | ≤ |pn − pn−1 |, (2.19)
1−K 1−K
2 − ex + x 2 5
(a) x = (b) x = +2
3 x2
Answer: (a): (1) [0, 1], (2) K = 1/3 ⇒ n ≥ 5 ln(10)/ ln(3) ≈ 10.48.
Example 2.20. Prove that the sequence xn defined recursively as follows
is convergent.
x0 = −15
1
xn+1 = 3 − |xn | (n ≥ 0)
2
f (p) = 0. (2.20)
0 (p − p0 )2 00
0 = f (p) = f (p0 + h) = f (p0 ) + (p − p0 )f (p0 ) + f (ξ ), (2.21)
2
where ξ lies between p and p0 .
• If |p − p0 | is small, it is reasonable to ignore the last term of (2.21) and
solve for h = p − p0 :
f (p0 )
h = p − p0 ≈ − . (2.22)
f 0 (p0 )
• Define
f (p0 )
p1 = p0 − ; (2.23)
f 0 (p0 )
then p1 may be a better approximation of p than p0 .
• The above can be repeated.
Graphical interpretation
• Let p0 be the initial approximation close to p . Then, the tangent line
at (p0 , f (p0 )) reads
L (x) = f 0 (p0 )(x − p0 ) + f (p0 ). (2.25)
• To find the x -intercept of y = L (x), let
0 = f 0 (p0 )(x − p0 ) + f (p0 ).
An Example of Divergence
1 f := arctan(x);
2 Newton(f, x = Pi/2, output = plot, maxiterations = 3);
Remark 2.23.
• The Newton’s method may diverge, unless the initialization is ac-
curate.
• The Newton’s method can be interpreted as a fixed-point itera-
tion:
f (pn−1 )
pn = g(pn−1 ) := pn−1 − 0 . (2.27)
f (pn−1 )
• It cannot be continued if f 0 (pn−1 ) = 0 for some n. As a matter of fact,
the Newton’s method is most effective when f 0 (x) is bounded away
from zero near p .
Since p = 0, en = pn and
|en | ≤ 0.67|en−1 |3 , (2.32)
which is an occasional super-convergence.
Example 2.27. Use the Newton’s method to find the square root of a
positive number Q .
√
Solution. Let x = Q . Then x is a root of x 2 − Q = 0. Define f (x) = x 2 − Q ;
set f 0 (x) = 2x . The Newton’s method reads
f (pn−1 ) pn2−1 − Q 1 Q
pn = pn−1 − 0 = pn−1 − = pn−1 + . (2.33)
f (pn−1 ) 2pn−1 2 pn−1
(Compare the above with (1.45), p. 27.)
NR.mw
1 NR := proc(Q, p0, itmax)
2 local p, n;
3 p := p0;
4 for n to itmax do
5 p := (p+Q/p)/2;
6 print(n, evalf[14](p));
7 end do;
8 end proc:
58 Chapter 2. Solutions of Equations in One Variable
An illustration:
• We begin with a pair of equations involving two variables:
(
f1 (x1 , x2 ) = 0
(2.34)
f2 (x1 , x2 ) = 0
Then, the Newton’s method for two nonlinear equations in two variables
reads " # " # " #
x1n x1n−1 h1n−1
= n −1 + n −1 , (2.37)
x2n x2 h2
fi (x1 , x2 , · · · , xm ) = 0, 1 ≤ i ≤ m,
can be expressed as
F(X ) = 0, (2.39)
where X = (x1 , x2 , · · · , xm )T and F = (f1 , f2 , · · · , fm )T . Then
Example 2.29. Starting with (1, 1, 1)T , carry out 6 iterations of the New-
ton’s method to find a root of the nonlinear system
xy = z2 + 1
xyz + y 2 = x 2 + 2
ex + z = ey + 3
Solution.
Procedure NewtonRaphsonSYS.mw
1 NewtonRaphsonSYS := proc(X, F, X0, TOL, itmax)
2 local Xn, H, FX, J, i, m, n, Err;
3 m := LinearAlgebra[Dimension](Vector(X));
4 Xn := Vector(m);
5 H := Vector(m);
6 FX := Vector(m);
7 J := Matrix(m, m);
8 Xn := X0;
9 for n to itmax do
10 FX := eval(F, [seq(X[i] = Xn[i], i = 1..m)]);
11 J := evalf[15](VectorCalculus[Jacobian](F, X=convert(Xn,list)));
12 H := -MatrixInverse(J).Vector(FX);
13 Xn := Xn + H;
14 printf(" %3d %.8f ", n, Xn[1]);
15 for i from 2 to m do; printf(" %.8f ", Xn[i]); end do;
16 for i to m do; printf(" %.3g ", H[i]); end do;
17 printf("\n");
18 if (LinearAlgebra[VectorNorm](H, 2) < TOL) then break endif:
19 end do;
20 end proc:
Result
1 F := [x*y-z^2-1, x*y*z-x^2+y^2-2, exp(x)+z-exp(y)-3]:
2 X := [x, y, z]:
3 X0 := <1, 1, 1>:
4 TOL := 10^-8: itmax := 10:
5 NewtonRaphsonSYS(X, F, X0, TOL, itmax):
6 1 2.18932610 1.59847516 1.39390063 1.19 0.598 0.394
7 2 1.85058965 1.44425142 1.27822400 -0.339 -0.154 -0.116
8 3 1.78016120 1.42443598 1.23929244 -0.0704 -0.0198 -0.0389
9 4 1.77767471 1.42396093 1.23747382 -0.00249 -0.000475 -0.00182
10 5 1.77767192 1.42396060 1.23747112 -2.79e-006 -3.28e-007 -2.7e-006
11 6 1.77767192 1.42396060 1.23747112 -3.14e-012 -4.22e-014 -4.41e-012
2.3. Newton’s Method and Its Variants 61
n −1 − p n −2
h p i
pn = pn−1 − f (pn−1 ) , n ≥ 2. (2.45)
f (pn−1 ) − f (pn−2 )
Note:
• Two initial values (p0 , p1 ) must be given, which however is not a
drawback.
• It requires only one new evaluation of f per step.
• The graphical interpretation of the secant method is similar to that
of Newton’s method.
• Convergence:
f 00 (p) f 00 (p) 0.62 √
(1+ 5)/2
|en | ≈ en−1 en−2 ≈ |en−1 | . (2.46)
2f 0 (p) 2f 0 (p)
Here evalf((1+sqrt(5))/2) = 1.618033988.
62 Chapter 2. Solutions of Equations in One Variable
Graphical interpretation
with(Student[NumericalAnalysis]):
f := x b 3 - 1:
Secant(f, x = [1.5, 0.5], maxiterations = 3, output = sequence);
1.5, 0.5, 0.7692307692, 1.213510253, 0.9509757891
Secant(f, x = [1.5, 0.5], maxiterations = 3, output = plot);
Solution.
Answer: p2 = 5.0
2.3. Newton’s Method and Its Variants 63
• If (f (p1 ) · f (p2 ) < 0), set (p1 and p2 bracket the root)
p3 = the x -intercept of the line joining (p2 , f (p2 )) and (p1 , f (p1 )).
else, set
p3 = the x -intercept of the line joining (p2 , f (p2 )) and (p0 , f (p0 )).
endif
Graphical interpretation
with(Student[NumericalAnalysis]):
f := x b 3 - 1:
FalsePosition(f,x=[1.5,0.5], maxiterations=3, output=plot);
Convergence Speed:
Find a root for x = cos x , starting with π/4 or [0.5, π/4].
Comparison
1 with(Student[NumericalAnalysis]):
2 f := cos(x) - x:
3
8 S := Secant(f,x=[0.5,Pi/4],tolerance=10^-8,maxiterations=10,
9 output=sequence);
10 0.5, 0.7853981635, 0.7363841388, 0.7390581392, 0.7390851493,
11 0.7390851332, 0.7390851332
12
13 F := FalsePosition(f,x=[0.5,Pi/4],tolerance=10^-8,maxiterations=10,
14 output=sequence);
15 [0.5, 0.7853981635], [0.7363841388, 0.7853981635],
16 [0.7390581392, 0.7853981635], [0.7390848638, 0.7853981635],
17 [0.7390851305, 0.7853981635], [0.7390851332, 0.7853981635],
18 [0.7390851332, 0.7853981635], [0.7390851332, 0.7853981635],
19 [0.7390851332, 0.7853981635], [0.7390851332, 0.7853981635],
20 [0.7390851332, 0.7853981635]
# print out
n Newton Secant False Position
0 0.7853981635 0.5000000000 0.5000000000
1 0.7395361335 0.7853981635 0.7363841388
2 0.7390851781 0.7363841388 0.7390581392
3 0.7390851332 0.7390581392 0.7390848638
4 0.7390851332 0.7390851493 0.7390851305
5 0.7390851332 0.7390851332 0.7390851332
6 0.7390851332 0.7390851332 0.7390851332
7 0.7390851332 0.7390851332 0.7390851332
8 0.7390851332 0.7390851332 0.7390851332
2.4. Zeros of Polynomials 65
• Substituting the above into (2.50), utilizing (2.47), and setting equal
the coefficients of like powers of x on the two sides of the resulting
equation, we have
bn = an
b n −1 = an−1 + x0 bn
.. (2.52)
.
b1 = a1 + x0 b2
P(x0 ) = a0 + x0 b1
bn+1 = 0; bk = ak + x0 bk +1 , n ≥ k ≥ 0. (2.53)
P(x) = x 4 − 4x 3 + 7x 2 − 5x − 2. (2.54)
How to evaluate P 0 (x): The derivative P 0 (x) can be evaluated by using the
Horner’s method with the same efficiency. Indeed, differentiating (2.50)
reads
P 0 (x) = Q(x) + (x − x0 )Q 0 (x). (2.56)
Thus
P 0 (x0 ) = Q(x0 ). (2.57)
That is, the evaluation of Q at x0 becomes the desired quantity P 0 (x0 ).
68 Chapter 2. Solutions of Equations in One Variable
Example 2.37. Evaluate P 0 (3) for P(x) considered in Example 2.36, the
previous example.
Solution. As in the previous example, we arrange the calculation and
carry out the synthetic division one more time:
6 n = size(A(:),1);
7 p = A(n); d=0;
8
9 for i = n-1:-1:1
10 d = p + x0*d;
11 p = A(i) +x0*p;
12 end
Call_horner.m
1 a = [-2 -5 7 -4 1];
2 x0=3;
3 [p,d] = horner(a,x0);
4 fprintf(" P(%g)=%g; P'(%g)=%g\n",x0,p,x0,d)
5 P(3)=19; P'(3)=37
2.4. Zeros of Polynomials 69
6 x = x0;
7 for it=1:itmax
8 [p,d] = horner(A,x);
9 h = -p/d;
10 x = x + h;
11 if(abs(h)<tol), break; end
12 end
Call_newton_horner.m
1 a = [-2 -5 7 -4 1];
2 x0=3;
3 tol = 10^-12; itmax=1000;
4 [x,it] = newton_horner(a,x0,tol,itmax);
5 fprintf(" newton_horner: x0=%g; x=%g, in %d iterations\n",x0,x,it)
6 newton_horner: x0=3; x=2, in 7 iterations
Figure 2.10: Polynomial P(x) = x 4 − 4x 3 + 7x 2 − 5x − 2. Its two zeros are −0.275682 and 2.
70 Chapter 2. Solutions of Equations in One Variable
bn+1 = bn+2 = 0
(2.60)
bk = ak + ubk +1 + vbk +2 , n ≥ k ≥ 0.
2.4. Zeros of Polynomials 71
The quantities b0 and b1 must be functions of (u, v), which is clear from
(2.59) and (2.60).
∂ bk ∂ bk −1
ck = , dk = (0 ≤ k ≤ n). (2.65)
∂u ∂v
• Differentiating the recurrence relation, (2.60), results in the fol-
lowing pair of additional recurrences:
Note that these recurrence relations obviously generate the same two
sequences (ck = dk ); we need only the first.
• The Jacobian explicitly reads
" #
∂ (b0 , b1 ) c0 c1
J= = , (2.67)
∂ (u, v) c1 c2
and therefore
" # " # " #
δu b0 1 b1 c1 − b0 c2
= −J −1 = 2
. (2.68)
δv b1 c0 c2 − c1 b0 c1 − b1 c0
Run Bairstow
1 P := x -> x^4 - 4*x^3 + 7*x^2 - 5*x - 2:
2 n := degree(P(x)):
3 a := Array(0..n):
4 for i from 0 to n do
5 a[i] := coeff(P(x), x, i);
6 end do:
7 itmax := 10: TOL := 10^-10:
8
9 u := 3:
10 v := -4:
11 Bairstow(n, a, u, v, itmax, TOL);
12 1 2.2000000 -2.7000000 -0.8 1.3
13 2 2.2727075 -3.9509822 0.07271 -1.251
14 3 2.2720737 -3.6475280 -0.0006338 0.3035
15 4 2.2756100 -3.6274260 0.003536 0.0201
16 5 2.2756822 -3.6273651 7.215e-05 6.090e-05
17 6 2.2756822 -3.6273651 6.316e-09 -9.138e-09
18 7 2.2756822 -3.6273651 -1.083e-17 -5.260e-17
19 Q(x) = (1)x^2 + (-1.72432)x^1 + (-0.551364)
20 Remainder: -2.66446e-18 (x - (2.27568)) + (-2.47514e-16)
21 Quadratic Factor: x^2 - (2.27568)x - (-3.62737)
22 Zeros: 1.137841102 +- (1.527312251) i
2.4. Zeros of Polynomials 75
Deflation
• Given a polynomial of degree n, P(x), if the Newton’s method finds a
zero (say, b
x1 ), it will be written as
P(x) ≈ (x − b
x1 )Q1 (x). (2.69)
Q1 (x) ≈ (x − b
x2 )Q2 (x). (2.70)
Remark 2.43.
• The deflation process introduces an accuracy issue, due to the fact
that when we obtain the approximate zeros of P(x), the Newton’s
method is applied to the reduced polynomials Qk (x).
• An approximate zero b xk +1 of Qk (x) will generally not approximate a
root of P(x) = 0; inaccuracy increases as k increases.
• One way to overcome the difficulty is to improve the approximate
zeros; starting with these zeros, apply the Newton’s method with the
original polynomial P(x).
76 Chapter 2. Solutions of Equations in One Variable
2.1. Let the bisection method be applied to a continuous function, resulting in intervals
[a1 , b1 ], [a2 , b2 ], · · · . Let pn = (an + bn )/2 and p = lim pn . Which of these statements can
n→∞
be false?
(a) a1 ≤ a2 ≤ · · ·
b1 − a1
(b) |p − pn | ≤ , n≥1
2n
(c) |p − pn+1 | ≤ |p − pn |, n ≥ 1
(d) [an+1 , bn+1 ] ⊂ [an , bn ]
1
(e) |p − pn | = O n as n → ∞
2
2.2. C Modify the Matlab code used in Example 2.7 for the bisection method to incorpo-
rate
Inputs : f, a, b, TOL, itmax
Stopping criterion : Relative error ≤ TOL or k ≤ itmax
Consider the following equations defined on the given intervals:
I. 3x − e x = 0, [0, 1]
II. 2x cos(2x) − (x + 1)2 = 0, [−1, 0]
(a) Use Maple or Matlab (or something else) to find a very accurate solution in the
interval.
(b) Find the approximate root by using your Matlab with TOL=10−6 and itmax=10.
(c) Report pn , |p − pn |, and |p − pn−1 |, for n ≥ 1, in a table format.
2.3. C Let us try to find 51/3 by the fixed-point method. Use the fact that the result must
be the positive solution of f (x) = x 3 − 5 = 0 to solve the following:
(a) Introduce two different fixed-point forms which are convergent for x ∈ [1, 2].
(b) Perform five iterations for each of the iterations with p0 = 1.5, and measure
|p − p5 |.
(c) Rank the associated iterations based on their apparent speed of convergence
with p0 = 1.5. Discuss why one is better than the other.
(a) Show that for each y ∈ [0, π ], there exists an x satisfying the equation.
(b) Interpret this as a fixed-point problem.
2.4. Zeros of Polynomials 77
(c) C Find x ’s for y = 1, π/2, 2, using the fixed-point iteration. Set ε = 1/2.
Hint : For (a), you may have to use the IVT for x − ε ∗ sin(x) defined on [0, π ], while
for (b) you should rearrange the equation in the form of x = g(x). For (c), you may
use any source of program which utilizes the fixed-point iteration.
2.5. Consider a variation of Newton’s method in which only one derivative is needed;
that is,
f (pn−1 )
pn = pn−1 − , n ≥ 1. (2.72)
f 0 (p0 )
Find C and s such that
en ≈ Cens−1 . (2.73)
1
Hint : You may have to use f (pn−1 ) = en−1 f 0 (pn−1 ) − en2−1 f 00 (ξn−1 ).
2
2.6. (Note: Do not use programming for this problem.) Starting with x0 = (0, 1)T , carry
out two iterations of the Newton’s method on the system:
2
4x − y 2 = 0
4xy 2 − x = 1
P(x) = 3x 5 − 7x 4 − 5x 3 + x 2 − 8x + 2.
Topics Applications/Properties
Polynomial interpolation The first step toward approximation theory
Newton form
Lagrange form Basis functions for various applications in-
cluding visualization and FEMs
Chebyshev polynomial Optimized interpolation
Divided differences
Neville’s method Evaluation of interpolating polynomials
Hermite interpolation It incorporates f (xi ) and f 0 (xi )
Spline interpolation Less oscillatory interpolation
B-splines
Parametric curves Curves in the plane or the space
Rational interpolation Interpolation of rough data with minimum
oscillation
79
80 Chapter 3. Interpolation and Polynomial Approximation
pn ≡ qn .
for some ck .
m
Y
(Here the convention has been adopted that (x − xj ) = 1 when m < 0.)
j=0
The first few cases of (3.12) are
p0 (x) = c0 ,
p1 (x) = c0 + c1 (x − x0 ), (3.13)
p2 (x) = c0 + c1 (x − x0 ) + c2 (x − x0 )(x − x1 ).
These polynomials are called the interpolating polynomials in Newton
form, or Newton form of interpolating polynomials.
3.1. Polynomial Interpolation 83
5 xk := 0:
6 xy := [[xk, f(xk)]]:
7 P0 := PolynomialInterpolation(xy, independentvar = x,
8 method = newton, function = f);
9 p0 := x -> Interpolant(P0):
10 p0(x)
11 1
12
13 xk := 0.5:
14 P1 := AddPoint(P0, [xk, f(xk)]):
15 p1 := x -> Interpolant(P1):
16 p1(x)
17 1. - 0.4948079186 x
18
19 xk := 1.0:
20 P2 := AddPoint(P1, [xk, f(xk)]):
21 p2 := x -> Interpolant(P2):
22 p2(x)
23 1. - 0.4948079186 x + 0.9896158372 x (x - 0.5)
24
25 xk := 2.0:
26 P3 := AddPoint(P2, [xk, f(xk)]):
27 p3 := x -> Interpolant(P3):
28 p3(x)
29 1. - 0.4948079186 x + 0.9896158372 x (x - 0.5)
30 - 0.3566447492 x (x - 0.5) (x - 1.0)
31
84 Chapter 3. Interpolation and Polynomial Approximation
32 xk := 1.5:
33 P4 := AddPoint(P3, [xk, f(xk)]):
34 p4 := x -> Interpolant(P4):
35 p4(x)
36 1. - 0.4948079186 x + 0.9896158372 x (x - 0.5)
37 - 0.3566447492 x (x - 0.5) (x - 1.0)
38 - 0.5517611839 x (x - 0.5) (x - 1.0) (x - 2.0)
Figure 3.2: Illustration of Newton’s interpolating polynomials, with f (x) = sin(x · (x − 1)) + 1,
at [0, 0.5, 1.0, 2.0, 1.5].
u = c0 + c1 d0 + c2 d0 d1 + · · · + ck −1 d0 d1 · · · dk −2 + ck d0 d1 · · · dk −1
= ck d0 d1 · · · dk −1 + ck −1 d0 d1 · · · dk −2 + · · · + c2 d0 d1 + c1 d0 + c0
= (ck d1 · · · dk −1 + ck −1 d1 · · · dk −2 + · · · + c2 d1 + c1 )d0 + c0
(3.15)
= ((ck d2 · · · dk −1 + ck −1 d2 · · · dk −2 + · · · + c2 )d1 + c1 )d0 + c0
...
= (· · · (((ck )dk −1 + ck −1 )dk −2 + ck −2 )dk −3 + · · · + c1 )d0 + c0
3.1. Polynomial Interpolation 85
xi 5 −7 −6 0
yi 1 −23 −54 −954
DividedDifferenceTable(N);
1 0 0 0
−23 2 0 0
−54 −31
3 0
−954 −150 −17 4
Solution.
where Ln,k (x) are polynomials that depend on the nodes x0 , x1 , · · · , xn , but
not on the ordinates y0 , y1 , · · · , yn .
How to determine the basis {Ln,k (x)}
Observation 3.9. Let all the ordinates be 0 except for a 1 occupying
i -th position, that is, yi = 1 and other ordinates are all zero.
• Then,
n
X
pn (xj ) = yk Ln,k (xj ) = Ln,i (xj ). (3.17)
k =0
and therefore
1
c= . (3.21)
(x0 − x1 )(x0 − x2 ) · · · (x0 − xn )
Hence, we have
n
Y (x − xj )
(x − x1 )(x − x2 ) · · · (x − xn )
Ln,0 (x) = = . (3.22)
(x0 − x1 )(x0 − x2 ) · · · (x0 − xn ) (x0 − xj )
j=1
Solution.
90 Chapter 3. Interpolation and Polynomial Approximation
(a) Use the points to find the second Lagrange interpolating polynomial
p2 for f (x) = 1/x .
(b) Use p2 to approximate f (3) = 1/3.
Solution.
3.1. Polynomial Interpolation 91
Maple-code
1 with(Student[NumericalAnalysis]);
2 f := x -> 1/x:
3 unassign('xy'):
4 xy := [[2, 1/2], [4, 1/4], [5, 1/5]]:
5
6 L2 := PolynomialInterpolation(xy, independentvar = x,
7 method = lagrange, function = f(x)):
8 Interpolant(L2);
9 1 1 1
10 -- (x - 4) (x - 5) - - (x - 2) (x - 5) + -- (x - 2) (x - 4)
11 12 8 15
12 RemainderTerm(L2);
13 / (x - 2) (x - 4) (x - 5)\
14 |- -----------------------| &where {2 <= xi_var and xi_var <= 5}
15 | 4 |
16 \ xi_var /
17 p2 := x -> expand(Interpolant(L2));
18 1 2 11 19
19 -- x - -- x + --
20 40 40 20
21 evalf(p2(3));
22 0.3500000000
Example 3.16. For Example 3.14, determine the error bound in [2, 5].
Solution.
Maple-code
1 p2 := x -> interp([2, 4, 5], [1/2, 1/4, 1/5], x):
2 p2(x):
3 f := x -> 1/x:
4 fd := x -> diff(f(x), x, x, x):
5 fd(xi)
6 6
7 - ---
8 4
9 xi
10 fdmax := maximize(abs(fd(x)), x = 2..5)
11 3
12 -
13 8
14 r := x -> (x - 2)*(x - 4)*(x - 5):
15 rmax := maximize(abs(r(x)), x = 2..5);
16 /5 1 (1/2)\ /4 1 (1/2)\ /1 1 (1/2)\
17 |- - - 7 | |- + - 7 | |- + - 7 |
18 \3 3 / \3 3 / \3 3 /
19 #Thus, "|f(x)-p2(x)|<=(max)|R[2](x)|="
20 evalf(fdmax*rmax/3!)
21 0.1320382370
3.1. Polynomial Interpolation 93
g := x -> (x+1)*(x+0.6)*(x+0.2)*(x-0.2)*(x-0.6)*(x-1):
gmax := maximize(abs(g(x)), x = -1..1)
0.06922606316
Thus,
5
f (6) (ξ ) Y sin(1)
| sin(x) − P5 (x)| = (x − xi ) ≤ gmax
6! 6! (3.26)
i=0
= 0.00008090517158
h n+1
|f (x) − Pn (x)| ≤ M, (3.27)
4(n + 1)
where
M = max |f (n+1) (ξ )|.
ξ∈[a,b]
Start by picking an x . We can assume that x is not one of the nodes, be-
94 Chapter 3. Interpolation and Polynomial Approximation
cause otherwise the product in question is zero. Let x ∈ (xj , xj+1 ), for some
j . Then we have
h2
|x − xj | · |x − xj+1 | ≤ . (3.28)
4
Now note that
(
(j + 1 − i)h for i < j
|x − xi | ≤ (3.29)
(i − j)h for j + 1 < i.
Thus
n
Y h2
|x − xi | ≤ [(j + 1)! h j ] [(n − j)! h n−j −1 ]. (3.30)
4
j=1
(2/n)n+1 √
2 ≤ 10−8 .
4(n + 1)
Answer: n = 10
3.1. Polynomial Interpolation 95
(b) It has been verified that if the nodes x0 , x1 , · · · , xn ∈ [−1, 1], then
n
Y
max (x − xi ) ≥ 2−n , n ≥ 0, (3.34)
|x |≤1
i=0
(c) The nodes then must be the roots of Tn+1 , which are
(2i + 1)π
xi = cos , i = 0, 1, · · · , n. (3.36)
2n + 2
3.1. Polynomial Interpolation 97
Thus
sin(1)
|f (x) − P5 (x)| ≤ = 0.00003652217816. (3.38)
2n (n + 1)!
It is an optimal upper bound of the error and smaller than the one in
Equation (3.26), 0.00008090517158.
Accuracy comparison between uniform nodes and Chebyshev nodes:
Maple-code
1 with(Student[NumericalAnalysis]):
2 n := 5:
3 f := x -> sin(2*x*Pi):
4 xd := Array(0..n):
5
6 for i from 0 to n do
7 xd[i] := evalf[15](-1 + (2*i)/n);
8 end do:
9 xyU := [[xd[0],f(xd[0])], [xd[1],f(xd[1])], [xd[2],f(xd[2])],
10 [xd[3],f(xd[3])], [xd[4],f(xd[4])], [xd[5],f(xd[5])]]:
11 U := PolynomialInterpolation(xyU, independentvar = x,
12 method = lagrange, function = f(x)):
13 pU := x -> Interpolant(U):
98 Chapter 3. Interpolation and Polynomial Approximation
14
15 for i from 0 to n do
16 xd[i] := evalf[15](cos((2*i + 1)*Pi/(2*n + 2)));
17 end do:
18 xyC := [[xd[0],f(xd[0])], [xd[1],f(xd[1])], [xd[2],f(xd[2])],
19 [xd[3],f(xd[3])], [xd[4],f(xd[4])], [xd[5],f(xd[5])]]:
20 C := PolynomialInterpolation(xyC, independentvar = x,
21 method = lagrange, function = f(x)):
22 pC := x -> Interpolant(C):
23
Figure 3.4: Accuracy comparison between uniform nodes and Chebyshev nodes.
3.2. Divided Differences 99
p0 (x) = c0 = y0 ,
p1 (x) = c0 + c1 (x − x0 ), (3.40)
p2 (x) = c0 + c1 (x − x0 ) + c2 (x − x0 )(x − x1 ).
y1 − y0 = c1 (x1 − x0 ) (3.42)
and therefore
y1 − y0
c1 = . (3.43)
x1 − x0
(b) Now, since
Note: It follows from Remark 3.24 that the coefficients of the Newton
interpolating polynomials read
In general,
ck = f [x0 , x1 , · · · , xk ]. (3.50)
3.2. Divided Differences 101
Solution.
Remark 3.30.
• We have studied how to construct interpolating polynomials. A fre-
quent use of these polynomials involves the interpolation of tabulated
data.
• However, in in many applications, an explicit representation of the
polynomial is not needed, but only the values of the polynomial at
specified points.
• In this situation, the function underlying the data might be unknown
so the explicit form of the error cannot be used to assure the accuracy
of the interpolation.
• Neville’s Method provides an adaptive mechanism for the evalua-
tion of accurate interpolating values.
Note: The above theorem implies that the interpolating polynomial can
be generated recursively. For example,
(x − x0 )P1 (x) − (x − x1 )P0 (x)
P0,1 (x) =
x1 − x0
(x − x1 )P2 (x) − (x − x2 )P1 (x)
P1,2 (x) = (3.61)
x2 − x1
(x − x0 )P1,2 (x) − (x − x2 )P0,1 (x)
P0,1,2 (x) =
x2 − x0
and so on. They are generated in the manner shown in the following ta-
ble, where each row is completed before the succeeding rows are begun.
x0 y0 = P0
x1 y1 = P1 P0,1
(3.62)
x2 y2 = P2 P1,2 P0,1,2
x3 y3 = P3 P2,3 P1,2,3 P0,1,2,3
x0 P0 = Q0,0
x1 P1 = Q1,0 P0,1 = Q1,1
(3.63)
x2 P2 = Q2,0 P1,2 = Q2,1 P0,1,2 = Q2,2
x3 P3 = Q3,0 P2,3 = Q3,1 P1,2,3 = Q3,2 P0,1,2,3 = Q3,3
106 Chapter 3. Interpolation and Polynomial Approximation
10 Q := NevilleTable(P, 2.1)
11 [[0.6931471806, 0, 0, 0, 0 ],
12 [0.7884573604, 0.7408022680, 0, 0, 0 ],
13 [0.8329091229, 0.7440056025, 0.7418700461, 0, 0 ],
14 [0.6418538862, 0.7373815030, 0.7417975693, 0.7419425227, 0 ],
15 [0.7654678421, 0.7407450500, 0.7418662324, 0.7419348958, 0.7419374382]]
Note that
|Q3,3 − Q2,2 | = |0.7419425227 − 0.7418700461| = 0.0000724766
|Q4,4 − Q3,3 | = |0.7419374382 − 0.7419425227| = 0.0000050845
Thus Q3,3 = 0.7419425227 is already in a four-digit accuracy.
Check: The real value is ln(2.1) = 0.7419373447. The absolute error:
| ln(2.1) − Q3,3 | == 0.0000051780.
Pseudocode 3.35.
(
the nodes x0 , x1 , · · · , xn ; the evaluation point x; the tolerance ε;
Input:
and values y0 , y1 , · · · , yn in the 1st column of Q ∈ R(n+1)×(n+1)
Output: Q
Step 1: For i = 1, 2, · · · , n
For j = 1, 2, · · · , i
(x − xi −j )Qi,j −1 − (x − xi )Qi −1,j −1
Qi,j =
xi − xi −j
if (|Qi,i − Qi −1,i −1 | < ε) {i0 = i; break; }
Step 2: Return (Q, i0 )
3.3. Data Approximation and Neville’s Method 107
x0 =0 Q0,0 = 1
x1 = 0.25 Q1,0 = 2 Q1,1 = 2.2
(3.64)
x2 = 0.5 Q2,0 Q2,1 Q2,2
x3 = 0.75 Q3,0 = 5 Q3,1 Q3,2 = 2.12 Q3,3 = 2.168
• Since there are four conditions, it seems reasonable to look for a so-
lution in P3 , the space of all polynomials of degree at most 3. Rather
than writing p(x) in terms of 1, x, x 2 , x 3 , let us write it as
f (x0 ) = a
f 0 (x0 ) = b
(3.68)
f (x1 ) = a + bh + ch 2 (h = x1 − x0 )
f 0 (x1 ) = b + 2ch + dh 2
where 0 2
Hn,i (x) = [1 − 2(x − xi )Ln,i (xi )]Ln,i (x),
2
b n,i (x) = (x − xi )Ln,i (x).
H
Here Ln,i (x) is the i th Lagrange polynomial of degree n. Moreover, if
f ∈ C 2n+2 [a, b], then
n
f (2n+2) (ξ ) Y
f (x) − H2n+1 (x) = (x − xi )2 . (3.70)
(2n + 2)!
i=0
with
f [xi ] − f [xi ]
f [z2i , z2i+1 ] = f [xi , xi ] = replaced by f 0 (xi ). (3.74)
xi − xi
110 Chapter 3. Interpolation and Polynomial Approximation
x f (x) f 0 (x)
0 2 −9
1 −4 4
3.4. Hermite Interpolation 111
Solution.
Theorem 3.46. To find the error bound, we will consider the error on
a single subinterval of the partition, and apply a little calculus. Let p(x)
be the linear polynomial interpolating f (x) at the endpoints of [xi −1 , xi ].
Then,
f 00 (ξ )
f (x) − p(x) = (x − xi −1 )(x − xi ), (3.82)
2!
for some ξ ∈ (xi −1 , xi ). Thus
M2
|f (x) − p(x)| ≤ max (xi − xi −1 )2 , x ∈ [a, b], (3.83)
8 1≤i ≤n
where
M2 = max |f 00 (x)|.
x ∈(a,b)
3.5. Spline Interpolation 115
we have
zi − zi −1
Qi0 (x) = zi −1 + (x − xi −1 ), x ∈ [xi −1 , xi ]. (3.90)
xi − xi −1
Note:
a. You should first decide zi using (3.93) and then finalize Qi from
(3.91).
b. When zn is specified, Equation (3.93) can be replaced by
y i − y i −1
zi −1 = 2 − zi , i = n, n − 1, · · · , 1. (3.94)
xi − xi −1
Example 3.49. Find the quadratic spline for the same dataset used in
Example 3.45, p. 114:
Figure 3.7: The graph of Q(x) is superposed over the graph of the linear spline L (x).
118 Chapter 3. Interpolation and Polynomial Approximation
Si (x) = ai x 3 + bi x 2 + ci x + di , i = 1, 2, · · · , n. (3.95)
zi = S 00 (xi ), i = 0, 1, · · · , n. (3.97)
zi −1 (xi − x)3 zi (x − xi −1 )3
Si (x) = +
y 6h i 6hi (3.102)
i 1 y
i −1 1
+ − zi hi (x − xi −1 ) + − zi −1 hi (xi − x).
hi 6 hi 6
for i = 1, 2, · · · , n − 1.
(4) Two additional user-chosen conditions are required to deter-
mine (n + 1) unknowns, z0 , z1 , · · · , zn . There are two popular ap-
proaches for the choice of the two additional conditions.
where
2(h1 + h2 ) h2
h2 2(h2 + h3 ) h3
... ... ...
6
A = and bi = (yi − yi −1 ).
hi
hn−2 2(hn−2 + hn−1 ) hn−1
hn − 1 2(hn−1 + hn )
3.5. Spline Interpolation 121
Clamped Cubic Splines: Let f 0 (a) and f 0 (b) be prescribed. Then the two
extra conditions read
Equation (3.106) and the above two equations clearly make (n + 1) condi-
tions for (n + 1) unknowns, z0 , z1 , · · · , zn . It is a good exercise to compose an
algebraic system for the computation of clamped cubic splines.
Example 3.52. Find the natural cubic spline for the same dataset
Solution.
Figure 3.8: The graph of S(x) is superposed over the graphs of the linear spline L (x) and
the quadratic spline Q(x).
122 Chapter 3. Interpolation and Polynomial Approximation
Example 3.53. Find the natural cubic spline that interpolates the data
x 0 1 3
y 4 2 7
Solution.
Maple-code
1 with(CurveFitting):
2 xy := [[0, 4], [1, 2], [3, 7]]:
3 n := 2:
4 L := x -> Spline(xy, x, degree = 1, endpoints = 'natural'):
5 Q := x -> Spline(xy, x, degree = 2, endpoints = 'natural'):
6 S := x -> Spline(xy, x, degree = 3, endpoints = 'natural'):
7 S(x)
8 / 11 3 3 41 49 27 2 3 3\
9 piecewise|x < 1, 4 - -- x + - x , otherwise, -- - -- x + -- x - - x |
10 \ 4 4 8 8 8 8 /
Figure 3.9
3.5. Spline Interpolation 123
Figure 3.10
Example 3.57. For data {(xi , f (xi ), f 0 (xi )}, i = 0, 1, · · · , n, the piecewise cu-
bic Hermite polynomial can be generated independently in each portion
[xi −1 , xi ]. Why?
Solution.
126 Chapter 3. Interpolation and Polynomial Approximation
Thus
dy (y0 + β0 ) − y0 β0 y 0 (0)
(t = 0) = = = 0 ,
dx (x0 + α0 ) − x0 α0 x (0)
(3.114)
dy y1 − (y1 − β1 ) β1 y 0 (1)
(t = 1) = = = 0 .
dx x1 − (x1 − α1 ) α1 x (1)
can be constructed as
x(t) = [2(x0 − x1 ) + (α0 + α1 )] t 3 + [3(x1 − x0 ) − (2α0 + α1 )] t 2
(3.116)
+α0 t + x0 .
can be constructed as
y(t) = [2(y0 − y1 ) + (β0 + β1 )] t 3 + [3(y1 − y0 ) − (2β0 + β1 )] t 2
(3.117)
+β0 t + y0 .
Solution.
3.1. C For the given functions f (x), let x0 = 0, x1 = 0.5, x2 = 1. Construct interpolation
polynomials of degree at most one and at most two to approximate f (0.4), and find
the absolute error.
3.2. Use the Polynomial Interpolation Error Theorem to find an error bound for the
approximations in Problem 1 above.
3.3. The polynomial p(x) = 1 − x + x(x + 1) − 2x(x + 1)(x − 1) interpolates the first four points
in the table:
x −1 0 1 2 3
y 2 1 2 −7 10
By adding one additional term to p , find a polynomial that interpolates the whole
table. (Do not try to find the polynomial from the scratch.)
3.4. Determine the Newton interpolating polynomial for the data:
x 4 2 0 3
y 63 11 7 28
3.5. Neville’s method is used to approximate f (0.4), giving the following table.
x0 = 0 Q0,0
x1 = 0.5 Q1,0 = 1.5 Q1,1 = 1.4
x2 = 0.8 Q2,0 Q2,1 Q2,2 = 1.2
x −1 0 1
f (x) 5 7 9
(a) Find the quadratic spline that interpolates the data (with z0 = f 0 (0)).
(b) Find the clamped cubic spline that interpolates the data.
(c) Plot the splines and display them superposed.
x f (x) f 0 (x)
0 2 −9
1 −4 4
2 4 12
3.10. C Let C be the unit circle of radius 1: x 2 + y 2 = 1. Find a piecewise cubic para-
metric curve that interpolates the circle at (1, 0), (0, 1), (−1, 0), (1, 0). Try to make the
parametric curve as circular as possible.
Now, (1) you can make it better, (2) you should find parametric curves for the other two
portions, and (3) combine them for a piece.
Chapter 4
In this chapter:
Topics Applications/Properties
Numerical Differentiation f (x) ≈ Pn (x) locally
⇒ f 0 (x) ≈ Pn0 (x)
Three-point rules
Five-point rules
Richardson extrapolation Combination of low-order differences,
to get higher-order accuracy
Numerical Integration f (x) ≈ Pn (x) piecewisely
ˆ b ˆ b
⇒ f (x) ≈ Pn (x)
a a
Trapezoid rule
Simpson’s rule Newton-Cotes formulas
Simpson’s Three-Eights rule
Romberg integration
Gaussian Quadrature Method of undetermined coefficients &
orthogonal polynomials
Legendre polynomials
131
132 Chapter 4. Numerical Differentiation and Integration
f (xi + h) − f (xi )
f 0 (xi ) ≈ Dx+ f (xi ) = , (forward-difference)
h (4.6)
f (xi ) − f (xi − h)
f 0 (xi ) ≈ Dx− f (xi ) = . (backward-difference)
h
4.1. Numerical Differentiation 133
3 h := 0.1:
4 (f(x0 + h) - f(x0))/h
5 3.310000000
6 h := 0.05:
7 (f(x0 + h) - f(x0))/h
8 3.152500000
9 h := 0.025:
10 (f(x0 + h) - f(x0))/h
11 3.075625000
Hence,
n n
0
X
0 f (n+1) (ξ ) Y
f (xi ) = f (xk )Ln,k (xi ) + (xi − xk ). (4.9)
(n + 1)!
k =0 k =0,k 6=i
134 Chapter 4. Numerical Differentiation and Integration
Solution.
136 Chapter 4. Numerical Differentiation and Integration
Solution.
M = N(h) + K2 h 2 + K4 h 4 + K6 h 6 + · · · , (4.15)
1 1
M = [16N2 (h /2) − N2 (h)] + K6 h 6 + · · · . (4.21)
15 64
4.2. Richardson Extrapolation 139
for i = 1, 2, · · · , n do
for j = 1, 2, · · · , i do
1 j
D(i, j) = j 4 · D(i, j − 1) − D(i − 1, j − 1) (4.23)
4 −1
end do
end do
Note:
(a) One can prove that
(b) The second step in the algorithm can be rewritten for a column-wise
computation:
for j = 1, 2, · · · , i do
for i = j, j + 1, · · · , n do
1 j
D(i, j) = j 4 · D(i, j − 1) − D(i − 1, j − 1) (4.25)
4 −1
end do
end do
140 Chapter 4. Numerical Differentiation and Integration
Let {x0 , x1 , · · · , xn } be distinct points (nodes) in [a, b]. Then the Lagrange
interpolating polynomial reads
n
X
Pn (x) = f (xi )Ln,i (x), (4.26)
i=0
where ˆ b
Ai = Ln,i (x) dx. (4.29)
a
The formula of the form in (4.28) is called a Newton-Cotes formula
when the nodes are equally spaced.
4.3. Numerical Integration 143
and
ˆ x1 ˆ
f 00 (ξx ) f 00 (ξ ) x1
(x − x0 )(x − x1 ) dx = (x − x0 )(x − x1 ) dx
x0 2! 2! x0
(4.33)
f 00 (ξ )
= − (x1 − x0 )3 .
12
(Here we could use the Weighted Mean Value Theorem on Integral be-
cause (x − x0 )(x − x1 ) ≤ 0 does not change the sign over [x0 , x1 ].)
Graphical interpretation:
with(Student[Calculus1]):
f := x^3 + 2 + sin(2*Pi*x):
ApproximateInt(f, 0..1, output = animation, partition = 1,
method = trapezoid, refinement = halve,
boxoptions = [filled = [color=pink,transparency=0.5]]);
Then the trapezoid rule can be applied to each subinterval. Here the nodes
are not necessarily uniformly spaced. Thus, we obtain the composite
trapezoid rule reads
ˆ b n ˆ xi n
X X hi
f (x) dx = f (x) dx ≈ (f (xi −1 ) + f (xi )) , hi = xi − xi −1 . (4.35)
a x i −1 2
i=1 i=1
b −a
b − a
where we have used h = ⇒n= .
n h
Example 4.16.
with(Student[Calculus1]):
f := x^3 + 2 + sin(2*Pi*x):
ApproximateInt(f, 0..1, output = animation, partition = 8,
method = trapezoid, refinement = halve,
boxoptions = [filled = [color=pink,transparency=0.5]]);
which is reduced to
ˆ x2
2h
f (x) dx ≈ f (x0 ) + 4f (x1 ) + f (x2 ) . (4.39)
x0 6
Graphical interpretation:
with(Student[Calculus1]):
f := x^3 + 2 + sin(2*Pi*x):
ApproximateInt(f, 0..1, output = animation, partition = 1,
method = simpson, refinement = halve,
boxoptions = [filled = [color=pink,transparency=0.5]]);
Figure 4.3: The elementary Simpson’s rule, which is exact for the given problem.
4.3. Numerical Integration 147
• The error for the elementary Simpson’s rule can be analyzed from
ˆ x2 000
f (ξ )
(x − x0 )(x − x1 )(x − x2 ) dx, (4.40)
x0 3!
which must be in O (h 4 ).
• However, by approximating the problem in another way, one can show
the error is in O (h 5 ). (See Example 4.5, p.135.)
• It follows from the Taylor’s Theorem that for each x ∈ [x0 , x2 ], there is a
number ξ ∈ (x0 , x2 ) such that
f 00 (x1 ) f 000 (x1 ) f (4) (ξ )
f (x) = f (x1 )+f 0 (x1 )(x −x1 )+(x −x1 )2 + (x −x1 )3 + (x −x1 )4 . (4.41)
2! 3! 4!
By integrating the terms over [x0 , x2 ], we have
ˆ x2
f 0 (x1 ) 2 f 00 (x1 )
f (x) dx = f (x1 )(x − x1 ) + (x − x1 ) + (x − x1 )3
2 3!
x0
000 ˆ x (4) (4.42)
f (x1 ) x2 2
f (ξ )
+ (x − x1 )4 + (x − x1 )4 dx.
4! x0 x0 4!
The last term can be easily computed by using the Weighted Mean Value
Theorem on Integral:
ˆ x2 (4) ˆ
f (ξ ) 4 f (4) (ξ1 ) x2 4 f (4) (ξ1 ) 5
(x − x1 ) dx = (x − x1 ) dx = h . (4.43)
x0 4! 4! x0 60
Thus, Equation (4.42) reads
ˆ x2
h 3 00 f (4) (ξ1 ) 5
f (x) dx = 2h f (x1 ) + f (x1 ) + h . (4.44)
x0 3 60
See (4.14), p.137, to recall that
00 f (x0 ) − 2f (x1 ) + f (x2 ) h 2 (4)
f (x1 ) = − f (ξ2 ). (4.45)
h2 12
Plugging this to (4.44) reads
ˆ x2 3
h f (x0 ) − 2f (x1 ) + f (x2 ) h 3 h 2 (4) f (4) (ξ )
1
f (x) dx = 2h f (x1 ) + 2
− f (ξ2 ) + h 5,
x0 3 h 3 12 60
(4.46)
148 Chapter 4. Numerical Differentiation and Integration
and therefore
ˆ x2
2h h 5 (4)
f (x) dx = f (x0 ) + 4f (x1 ) + f (x2 ) − f (ξ3 ). (4.47)
x0 6 90
Example 4.18. Show that the error term for the composite Simpson’s
rule becomes
(b − a)h 4 (4)
− f (ξ ). (4.49)
180
Solution.
4.3. Numerical Integration 149
Theorem 4.19. When three equal subintervals are combined, the re-
sulting integration formula is called the Simpson’s three-eights rule:
ˆ x3
3h 3h 5 (4)
f (x) dx = f (x0 ) + 3f (x1 ) + 3f (x2 ) + f (x3 ) − f (ξ ). (4.50)
x0 8 80
b −a
xi = a + i h, h= , (0 ≤ i ≤ n)
n
derive the error term for the composite Simpson’s three-eights rule.
Solution.
Solution.
(a) 0.7895495781.
(so, the error = 0.0151693779)
´ 0.8 ´2
(b) 0 f (x)dx + 0.8 f (x)dx ≈ 0.2474264468 + 0.5567293981 = 0.8041558449.
(so, the error = 0.0005631111)
4.4. Romberg Integration 151
h1 n −1
X 1 i
T (n) = h f (a) + f (xk ) + f (b) . (4.52)
2 2
k =1
2i −1
i 1 i −1
X
T (2 ) = T (2 ) + hi f (a + (2k − 1)hi ) , (4.57)
2
k =1
where
1
h0 = b − a, hi = h i −1 , i ≥ 1.
2
4.4. Romberg Integration 153
for i = 1, 2, · · · , n do
for j = 1, 2, · · · , i do
1 j
R(i, j) = j 4 · R(i, j − 1) − R(i − 1, j − 1) (4.62)
4 −1
end do
end do
154 Chapter 4. Numerical Differentiation and Integration
6 # Trapezoid estimates
7 #----------------------------------
8 R[0, 0] := (b - a)/2*(f(a) + f(b));
9 0
10 for i to n do
11 hi := (b-a)/2^i;
12 R[i,0] := R[i-1,0]/2 +hi*add(f(a+(2*k-1)*hi), k=1..2^(i-1));
13 end do:
14
Recall:
• In Section 4.3, we saw how to create quadrature formulas of the type
ˆ b n
X
f (x) dx ≈ wi f (xi ), (4.64)
a i=0
that are exact for polynomials of degree ≤ n, which is the case if and
only if
ˆ b ˆ b Y
n
x − xj
wi = Ln,i (x) dx = dx. (4.65)
a a xi − xj
j=0, j 6=i
Key Idea 4.28. Gaussian quadrature chooses the points for evalu-
ation in an optimal way, rather than equally-spaced points. The nodes
x1 , x2 , · · · , xn in the interval [a, b] and the weights w1 , w2 , · · · , wn are
chosen to minimize the expected error obtained in the approximation
ˆ b n
X
f (x) dx ≈ wi f (xi ). (4.70)
a i=1
Then,
ˆ 1 n
X
f (x) dx ≈ wi f (xi ) is exact, ∀ f ∈ P2n−1 . (4.77)
−1 i=1
Note: Once the nodes are determined, the weights {w1 , w2 , · · · , wn } can
also be found by using the method of undetermined coefficients. That is,
the weights are the solution of the linear system
X n ˆ 1
(xj )i wj = x i dx, i = 0, 1, · · · , n − 1. (4.78)
j=1 −1
4.5. Gaussian Quadrature 161
Gauss-integration
1 with(LinearAlgebra):
2 with(Statistics):
3 WeightSystem := proc(n, A, b, X)
4 local i, j;
5 for j to n do
6 A[1, j] := 1;
7 for i from 2 to n do
8 A[i, j] := A[i - 1, j]*X[j];
9 end do:
10 end do:
11 for i to n do
12 b[i] := int(x^(i - 1), x = -1..1);
13 end do:
14 end proc
15
16 nmax := 5:
17 for k to nmax do
18 Legendre[k] := sort(orthopoly[P](k, x));
19 end do;
20 Legendre[1] := x
21 3 2 1
22 Legendre[2] := - x - -
23 2 2
24 5 3 3
25 Legendre[3] := - x - - x
26 2 2
27 35 4 15 2 3
28 Legendre[4] := -- x - -- x + -
29 8 4 8
30 63 5 35 3 15
31 Legendre[5] := -- x - -- x + -- x
32 8 4 8
33
34 Node := Array(0..nmax):
35 Weight := Array(0..nmax):
36
37 for k to nmax do
38 solve(Legendre[k] = 0, x):
39 Node[k] := Sort(Vector([%])):
40 n := Dimensions(Node[k]):
41 A := Matrix(n, n):
42 b := Vector(n):
43 WeightSystem(n, A, b, Node[k]):
162 Chapter 4. Numerical Differentiation and Integration
44 Weight[k] := A^(-1).b:
45 end do:
46
47 for k to nmax do
48 printf(" k=%d\n", k);
49 print(Nodek = evalf(Node[k]));
50 print(Weightk = evalf(Weight[k]));
51 end do;
52 k=1
53 Nodek = [0.]
54 Weightk = [2.]
55 k=2
56 [ -0.5773502693 ]
57 Nodek = [ ]
58 [ 0.5773502693 ]
59
60 [1.]
61 Weightk = [ ]
62 [1.]
63 k=3
64 [ -0.7745966692 ]
65 [ ]
66 Nodek = [ 0. ]
67 [ ]
68 [ 0.7745966692 ]
69
70 [0.5555555556]
71 [ ]
72 Weightk = [0.8888888889]
73 [ ]
74 [0.5555555556]
75 k=4
76 [ -0.8611363114 ]
77 [ ]
78 [ -0.3399810437 ]
79 Nodek = [ ]
80 [ 0.3399810437 ]
81 [ ]
82 [ 0.8611363114 ]
83
84 [0.3478548456]
85 [ ]
86 [0.6521451560]
87 Weightk = [ ]
88 [0.6521451563]
4.5. Gaussian Quadrature 163
89 [ ]
90 [0.3478548450]
91 k=5
92 [ -0.9061798457 ]
93 [ ]
94 [ -0.5384693100 ]
95 [ ]
96 Nodek = [ 0. ]
97 [ ]
98 [ 0.5384693100 ]
99 [ ]
100 [ 0.9061798457 ]
101
102 [0.2427962711]
103 [ ]
104 [0.472491159 ]
105 [ ]
106 Weightk = [0.568220204 ]
107 [ ]
108 [0.478558682 ]
109 [ ]
110 [0.2369268937]
Then, ˆ n
1 X
f (x) dx ≈ wi f (xi ) is exact, ∀ f ∈ P2n−1 . (4.82)
−1 i=0
Then, ˆ n
1 X
f (x) dx ≈ wi f (xi ) is exact, ∀ f ∈ P2n−1 . (4.84)
−1 i=1
Remark 4.37.
• The Gauss-Lobatto integration is a closed formula for numerical
integrations, which is more popular in real-world applications than
open formulas such as the Gauss integration.
• Once the nodes are determined, the weights {w0 , w1 , w2 , · · · , wn } an
also be found by using the method of undetermined coefficients, as for
Gauss integration; the weights are the solution of the linear system
Xn ˆ 1
i
(xj ) wj = x i dx, i = 0, 1, · · · , n. (4.85)
j=0 −1
166 Chapter 4. Numerical Differentiation and Integration
ˆ π
Self-study 4.38. Find the Gauss-Lobatto Quadrature for sin(x) dx ,
0
with n = 2, 3, 4.
4.5. Gaussian Quadrature 167
4.1. Use the most accurate three-point formulas to determine the missing entries.
4.2. Use your results in the above table to approximate f 0 (1.6) and f 00 (1.6) with O(h 4 )-
accuracy. Make a conclusion by comparing all results (obtained here and from Prob-
lem 1) with the exact values:
M = N(h) + K1 h + K2 h 2 + K3 h 3 + · · · (4.86)
Explain how Richardson extrapolation will work in this case. (Try to introduce a
formula described as in (4.23), page 139.)
ˆ 2
4.4. In order to approximate x ln(x 2 + 1) dx with h = 0.4, use
0
4.5. A car laps a race track in 65 seconds. The speed of the car at each 5 second interval
is determined by using a radar gun and is given from the beginning of the lap, in
feet/second, by the entries in the following table:
Time 0 5 10 15 20 25 30 35 40 25 50 55 60 65
Speed 0 90 124 142 156 147 133 121 109 99 95 78 89 98
(a) For each of the integrals, use the Romberg extrapolation to find R[3, 3].
(b) Determine the number of subintervals required when the Composite Trapezoid
rule is used to find approximations within the same accuracy as R[3, 3].
ˆ π/2
4.7. C Find the Gaussian Quadrature for cos2 x dx , with n = 2, 3, 4.
0
Chapter 5
In this chapter:
Topics Applications/Properties
Elementary Theory of IVPs Existence and uniqueness of so-
lution
Taylor-series Methods
Euler’s method
Higher-Order Taylor methods
Runge-Kutta (RK) Methods
Second-order RK (Heun’s method) Modified Euler’s method
Fourth-order RK
Runge-Kutta-Fehlberg method Variable step-size (adaptive
method)
Multi-step Methods
Adams-Bashforth-Moulton method
Higher-Order Equations &
Systems of Differential Equations
169
170 Chapter 5. Numerical Solution of Ordinary Differential Equations
then the IVP (5.2) has a unique solution y(x) in an interval I, where
x0 ∈ I ⊂ (a, b).
Example 5.5. Show that each of the initial-value problems has a unique
solution and find the solution.
(a) y 0 = e x −y , 0 ≤ x ≤ 1, y(0) = 1
(b) y 0 = (1 + x 2 )y, 3 ≤ x ≤ 5, y(3) = 1
(Find the solution): Here, we will find the solution for (b), using Maple.
Maple-code
1 DE := diff(y(x), x) = y(x)*(x^2 + 1);
2 d / 2 \
3 --- y(x) = y(x) \x + 1/
4 dx
5 IC := y(3) = 1;
6 y(3) = 1
7 dsolve({DE, IC}, y(x));
8 /1 / 2 \\
9 exp|- x \x + 3/|
10 \3 /
11 y(x) = -----------------
12 exp(12)
0 h 2 00
y(x + h) = y(x) + hy (x) + y (x) + · · · . (5.6)
2
• Letting x = x0 and utilizing y(x0 ) = y0 and y 0 (x0 ) = f (x0 , y0 ), the value
y(x1 ) can be approximated by
y1 = y0 + h f (x0 , y0 ), (5.7)
y2 = y1 + h f (x1 , y1 ), (5.8)
Theorem 5.9. Let f satisfy the Lipschitz condition in its second vari-
able, i.e., there is λ > 0 such that
Here we will prove (5.11) by using (5.13) and induction. It holds trivially
when n = 0. Suppose it holds for n. Then,
|en+1 | ≤ (1 + λh)|en | + Ch 2
C
≤ (1 + λh) · h[(1 + λh)n − 1] + Ch 2
λ
C
= h[(1 + λh)n+1 − (1 + λh)] + Ch 2
λ
C
= h[(1 + λh)n+1 − 1],
λ
which completes the proof.
176 Chapter 5. Numerical Solution of Ordinary Differential Equations
y0 = y − x 3 + x + 1, 0 ≤ x ≤ 3,
(5.14)
y(0) = 0.5.
1 1
As the step lengths become smaller, h = 1 → → , the numerical solu-
4 16
tions represent the exact solution better, as shown in the following figures:
1 1
Figure 5.2: The Euler method, with h = 1 → → .
4 16
1
y 0 = y − x 3 + x + 1, 0 ≤ x ≤ 3, y(0) = 0.5, with h = .
16
5.2. Taylor-Series Methods 177
Solution.
Euler.mw
1 Euler := proc(f, x0, b, nx, y0, Y)
2 local h, t, w, n:
3 h := (b - x0)/nx:
4 t := x0; w := y0:
5 Y[0] := w:
6 for n to nx do
7 w := w + h*eval(f, [x = t, y = w]);
8 Y[n] := w;
9 t := t + h;
10 end do:
11 end proc:
12
17 nx := 48:
18 YEuler := Array(0..nx):
19 Euler(f, x0, b, nx, y0, YEuler):
20
and generally,
y (k ) (x) = f (k −1) (x, y(x)). (5.16)
• Thus, we have
h2 0 h m (m−1)
y(xn+1 ) = y(xn ) + hf (xn , y(xn )) + f (xn , y(xn )) + · · · + f (xn , y(xn ))
2! m!
h m+1 (m)
+ f (ξn , y(ξn ))
(m + 1)!
(5.17)
where
h 0 h m−1 (m−1)
Tm (xn , yn ) = f (xn , yn ) + f (xn , yn ) + · · · + f (xn , yn ). (5.19)
2! m!
5.2. Taylor-Series Methods 179
Remark 5.14.
• m = 1 ⇒ yn+1 = yn + hf (xn , yn )
which is the Euler method.
h h 0 i
• m = 2 ⇒ yn+1 = yn + h f (xn , yn ) + f (xn , yn )
2
• As m increases, the method achieves higher-order accuracy; how-
ever, it requires to compute derivatives of f (x, y(x)).
f 0 (x, y) = y 0 − 3x 2 + 1
= (y − x 3 + x + 1) − 3x 2 + 1
= y − x 3 − 3x 2 + x + 2
and
f 00 (x, y) = y 0 − 3x 2 − 6x + 1
= (y − x 3 + x + 1) − 3x 2 − 6x + 1
= y − x 3 − 3x 2 − 5x + 2
Thus
h 0 h 2 00
T3 (x, y) = f (x, y) + f (x, y) + f (x, y)
2 6
h
= y − x 3 + x + 1 + (y − x 3 − 3x 2 + x + 2) (5.21)
2
h2
+ (y − x 3 − 3x 2 − 5x + 2)
6
For m large, the computation of Tm is time-consuming and cumbersome.
180 Chapter 5. Numerical Solution of Ordinary Differential Equations
Part (b):
Maple-code
1 T3 := y - x^3 + x + 1 + 1/2*h*(-x^3 - 3*x^2 + x + y + 2)
2 + 1/6*h^2*(-x^3 - 3*x^2 - 5*x + y + 2):
3 h := 1/2:
4 x0 := 0: y0 := 1/2:
5 y1 := y0 + h*eval(T3, [x = x0, y = y0])
6 155
7 ---
8 96
9 y2 := y1 + h*eval(T3, [x = x0 + h, y = y1])
10 16217
11 -----
12 4608
13 evalf(%)
14 3.519314236
15
where
(a) wj ≥ 0 and w1 + w2 + · · · + wm = 1
(b) Kj are recursive evaluations of the slope f (x, y)
(c) Need to determine wj and other parameters to satisfy
Formulation:
yn+1 = yn + h (w1 K1 + w2 K2 ) (5.25)
where
K1 = f (xn , yn )
K2 = f (xn + αh, yn + β hK1 )
Requirement: Determine w1 , w2 , α, β such that
h 0
w1 K1 + w2 K2 = T2 (xn , yn ) + O(h 2 ) = f (xn , yn ) + f (xn , yn ) + O(h 2 ). (5.26)
2
Derivation: For the left-hand side of (5.25), the Taylor series reads
0 h 2 00
y(x + h) = y(x) + h y (x) + y (x) + O(h 3 ).
2
Since y 0 = f and y 00 = fx + fy y 0 = fx + fy f ,
h2
y(x + h) = y(x) + h f + (fx + fy f ) + O(h 3 ). (5.27)
2
y + h(w1 K1 + w2 K2 )
= y + w1 h f (x, y) + w2 h f (x + αh, y + β h K1 )
= y + w1 h f + w2 h (f + αh fx + β h fy f ) + O(h 3 ),
which reads
The comparison of (5.27) and (5.28) drives the following result, for the
second-order Runge-Kutta methods.
Results:
1 1
w1 + w2 = 1, w2 α = , w2 β = . (5.29)
2 2
5.3. Runge-Kutta Methods 183
Common Choices:
Algorithm 5.17.
1
I. w1 = w2 = , α = β = 1:
2
Then, the algorithm (5.25) becomes
h
yn+1 = yn + (K1 + K2 ), (5.30)
2
where
K1 = f (xn , yn )
K2 = f (xn + h, yn + h K1 )
It follows from (5.27) and (5.28) that the local truncation error for the
above Runge-Kutta methods are O(h 3 ). Thus the global error becomes
O(h 2 ). (5.32)
184 Chapter 5. Numerical Solution of Ordinary Differential Equations
Formulation:
where
K1 = f (xn , yn )
K2 = f (xn + α1 h, yn + β1 h K1 )
K3 = f (xn + α2 h, yn + β2 h K1 + β3 h K2 )
K4 = f (xn + α3 h, yn + β4 h K1 + β5 h K2 + β6 h K3 )
Requirement: Determine wj , αj , βj such that
w1 K1 + w2 K2 + w3 K3 + w4 K4 = T4 (xn , yn ) + O(h 4 )
The local truncation error for the above RK4 can be derived as
h 5 (5)
y (ξn ), (5.35)
5!
for some ξn ∈ [xn , xn+1 ]. Thus the global error reads, for some ξ ∈ [x0 , T ],
(T − x0 )h 4 (5)
y (ξ ). (5.36)
5!
5.3. Runge-Kutta Methods 185
RK4.mw
1 f := proc(x,w)
2 w-x^3+x+1
3 end proc:
4 RK4 := proc(x0,xt,nt,y0,y)
5 local h,x,w,n,K1,K2,K3,K4;
6 h:=(xt-x0)/nt:
7 x:=x0: w:=y0:
8 y[0]:=w;
9 for n from 1 by 1 to nt do
10 K1:=f(x,w);
11 K2:=f(x+h/2,w+(h/2)*K1);
12 K3:=f(x+h/2,w+(h/2)*K2);
13 x:=x+h;
14 K4:=f(x,w+h*K3);
15 w:=w+(h/6.)*(K1+2*K2+2*K3+K4);
16 y[n]:=w;
17 end do
18 end proc:
19
20 x0 := 0: xt := 3: nt := 48: y0 := 0.5:
21 yRK4 := Array(0..nt);
22 RK4(x0,xt,nt,y0,yRK4):
23
h Max-error Error-ratio
1 /4 4.61 · 10−4
−5 4.61 · 10−4
1 /8 2.93 · 10 = 15.73378840
2.93 · 10−5
−6 2.93 · 10−5
1/16 1.85 · 10 = 15.83783784
1.85 · 10−6
−7 1.85 · 10−6
1/32 1.01 · 10 = 18.31683168
1.01 · 10−7
Remark 5.20.
• Accuracy of numerical methods can be improved by decreasing
the step size.
• Decreasing the step size ≈ Increasing the computational cost
• There may be subintervals where a relatively large step size suffices
and other subintervals where a small step is necessary to keep the
truncation error within a desired limit.
• An adaptive method is a numerical method which uses a variable
step size.
• Example: Runge-Kutta-Fehlberg method (RKF45), which uses
RK5 to estimate local truncation error of RK4.
5.4. One-Step Methods: Accuracy Comparison 187
κ F0
y 00 (t) + y= cos(µt),
m m (5.38)
y(0) = c0 , y 0 (0) = 0,
where m is the mass attached at the end of a spring of the spring constant
κ, the term F0 cos(µt) is a periodic driving force of frequency µ, and c0 is the
initial displacement from the equilibrium position.
• It is not difficult to find the analytic solution of (5.38):
F0
y(t) = A cos(ω t) + cos(µt), (5.39)
m(ω 2 − µ2 )
p
where ω = κ/m is the angular frequency and the coefficient A is
determined corresponding to c0 .
• Let y1 = y and y2 = −y10 /ω. Then, we can reformulate (5.38) as
y10 = −ω y2 , y0 (0) = c0 ,
F0 (5.40)
y20 = ω y1 − cos(µt), y2 (0) = 0.
mω
We will deal with details of High-Order Equations & Systems of Dif-
ferential Equations in § 5.6 on page 194.
• The motion is periodic only if µ/ω is a rational number. We choose
Accuracy comparison
Table 5.1: The `2 -error at t = 1 for various time step sizes.
• Table 5.1 presents the `2 -error at t = 1 for various time step sizes h ,
defined as
h
h
2 h 2 1/2
|yNt − y(1)| = y1,Nt − y1 (1) + y2,Nt − y2 (1) , (5.42)
where yhNt denotes the computed solution at the Nt -th time step with
h = 1 / Nt .
• The numbers in parenthesis indicate the order of convergence α,
defined as
ln(E(2h)/E(h))
α := , (5.43)
ln 2
where E(h) and E(2h) denote the errors obtained with the grid spacing
to be h and 2h , respectively.
5.4. One-Step Methods: Accuracy Comparison 189
• As one can see from the table, the one-step methods exhibit the ex-
pected accuracy.
• RK4 shows a much better accuracy than the lower-order methods,
which explains its popularity.
Numerical Methods:
• Single-step/Starting methods: Euler’s method, Modified Eu-
ler’s, Runge-Kutta methods
• Multi-step/Continuing methods: Adams-Bashforth-Moulton
Remark 5.24.
• y1 , y2 , y3 can be computed by RK4.
• Multi-step methods may save evaluations of f (x, y) such that in each
step, they require only one or two new evaluations of f (x, y) to fulfill
the step.
• RK methods are accurate enough and easy to implement, so that
multi-step methods are rarely applied in practice.
• ABM shows a strong stability for special cases, occasionally but not
often [1].
192 Chapter 5. Numerical Solution of Ordinary Differential Equations
ABM.mw
1 ## Maple code: Adams-Bashforth-Moulton (ABM) Method
2 ## Model Problem: y'= y- x^3 + x + 1, y(0) = 0.5, 0 <= x <= 3
3
4 f := proc(x,w)
5 w-x^3+x+1
6 end proc:
7 RK4 := proc(x0,xt,nt,y0,y)
8 local h,x,w,n,K1,K2,K3,K4;
9 h:=(xt-x0)/nt:
10 x:=x0: w:=y0:
11 y[0]:=w;
12 for n from 1 by 1 to nt do
13 K1:=f(x,w);
14 K2:=f(x+h/2,w+(h/2)*K1);
15 K3:=f(x+h/2,w+(h/2)*K2);
16 x:=x+h;
17 K4:=f(x,w+h*K3);
18 w:=w+(h/6.)*(K1+2*K2+2*K3+K4);
19 y[n]:=w;
20 end do
21 end proc:
22
23 ABM:= proc(x0,xt,nt,y0,y)
24 local h,x,w,n,ystar;
25 h:=(xt-x0)/nt:
26 ### Initialization with RK4
27 RK4(x0,x0+3*h,3,y0,y);
28 w:=y[3];
29 ### Now, ABM steps
30 for n from 4 by 1 to nt do
31 x:=x0+n*h;
32 ystar:=w +(h/24)*(55*f(x-h,y[n-1])-59*f(x-2*h,y[n-2])
33 +37*f(x-3*h,y[n-3])-9*f(x-4*h,y[n-4]));
34 w:=w +(h/24)*(9*f(x,ystar)+19*f(x-h,y[n-1])
35 -5*f(x-2*h,y[n-2])+f(x-3*h,y[n-3]));
5.5. Multi-step Methods 193
36 y[n]:=w;
37 end do;
38 end proc:
39
40 x0 := 0: xt := 3: nt := 48: y0 := 0.5:
41 yABM := Array(0..nt);
42 ABM(x0,xt,nt,y0,yABM):
43
u0 = y 00 = f (x, y, y 0 ) = f (x, y, u)
Thus, the above 2nd-order IVP can be equivalently written as the fol-
lowing system of first-order DEs:
(
y 0 = u, y(x0 ) = y0 ,
x ∈ [x0 , T ] (5.54)
u0 = f (x, y, u), u(x0 ) = u0 ,
Remark 5.25.
• The right-side of the DEs involves no derivatives.
• The system (5.54) can be solved by one of the numerical methods (we
have studied), after modifying it for vector functions.
5.6. High-Order Equations & Systems of Differential Equations 195
Hint : For (b), you should first rewrite the first equation as x 00 = F(t, x, x 0 ) and introduce x 0 = u
and y 0 = v .
Solution.
196 Chapter 5. Numerical Solution of Ordinary Differential Equations
RK4SYS.mw
1 ## Ex) IVP of 2 equations:
2 ## x' = 2x+4y, x(0)=-1
3 ## y' = -x+6y, y(0)= 6, 0 <= t <= 1
4
5 ef := proc(t,w,f)
6 f(1):=2*w(1)+4*w(2);
7 f(2):=-w(1)+6*w(2);
8 end proc:
9
10 RK4SYS := proc(t0,tt,nt,m,x0,x)
11 local h,t,w,n,j,K1,K2,K3,K4;
12 #### initial setting
13 w:=Vector(m):
14 K1:=Vector(m):
15 K2:=Vector(m):
16 K3:=Vector(m):
17 K4:=Vector(m):
18 h:=(tt-t0)/nt:
19 t:=t0;
20 w:=x0;
21 for j from 1 by 1 to m do
22 x[0,j]:=x0(j);
23 end do;
24 #### RK4 marching
25 for n from 1 by 1 to nt do
26 ef(t,w,K1);
27 ef(t+h/2,w+(h/2)*K1,K2);
28 ef(t+h/2,w+(h/2)*K2,K3);
29 ef(t+h,w+h*K3,K4);
30 w:=w+(h/6.)*(K1+2*K2+2*K3+K4);
31 for j from 1 by 1 to m do
32 x[n,j]:=w(j);
33 end do
34 end do
35 end proc:
5.6. High-Order Equations & Systems of Differential Equations 197
36
37 m := 2:
38 x0 := Vector(m):
39
40 t0 := 0: tt := 1.: nt := 40:
41 x0(1) := -1:
42 x0(2) := 6:
43
58 # Check error
59 #--------------------------------
60 h := (tt - t0)/nt:
61 printf(" n x(n) y(n) error(x) error(y)\n");
62 printf(" -----------------------------------------------\n");
63 for n from 0 by 2 to nt do
64 printf(" \t %5d %10.3f %10.3f %-10.3g %-10.3g\n",
65 n, xRK4[n,1], xRK4[n,2], abs(xRK4[n,1]-ex(n*h)),
66 abs(xRK4[n,2]-ey(n*h)) );
67 end do;
198 Chapter 5. Numerical Solution of Ordinary Differential Equations
Result
1 n x(n) y(n) error(x) error(y)
2 -----------------------------------------------------
3 0 -1.000 6.000 0 0
4 2 0.366 8.122 6.04e-06 4.24e-06
5 4 2.387 10.890 1.54e-05 1.07e-05
6 6 5.284 14.486 2.92e-05 2.01e-05
7 8 9.347 19.140 4.94e-05 3.35e-05
8 10 14.950 25.144 7.81e-05 5.26e-05
9 12 22.577 32.869 0.000118 7.91e-05
10 14 32.847 42.782 0.000174 0.000115
11 16 46.558 55.474 0.000251 0.000165
12 18 64.731 71.688 0.000356 0.000232
13 20 88.668 92.363 0.000498 0.000323
14 22 120.032 118.678 0.000689 0.000443
15 24 160.937 152.119 0.000944 0.000604
16 26 214.072 194.550 0.00128 0.000817
17 28 282.846 248.313 0.00174 0.0011
18 30 371.580 316.346 0.00233 0.00147
19 32 485.741 402.332 0.00312 0.00195
20 34 632.238 510.885 0.00414 0.00258
21 36 819.795 647.785 0.00549 0.0034
22 38 1059.411 820.262 0.00725 0.00447
23 40 1364.944 1037.359 0.00954 0.00586
5.6. High-Order Equations & Systems of Differential Equations 199
RK4SYSTEM.mw
1 ## Ex) y''-2*y'+2*y = exp(2*x)*sin(x), 0 <= x <= 1,
2 ## y(0) = -0.4, y'(0) = -0.6
3 Digits := 20:
4 RK4SYSTEM := proc(a,b,nt,X,F,x0,xn)
5 local h,hh,t,m,n,j,w,K1,K2,K3,K4;
6 #### initial setting
7 with(LinearAlgebra):
8 m := Dimension(Vector(F));
9 w :=Vector(m);
10 K1:=Vector(m);
11 K2:=Vector(m);
12 K3:=Vector(m);
13 K4:=Vector(m);
14 h:=(b-a)/nt; hh:=h/2;
15 t :=a;
16 w:=x0;
17 for j from 1 by 1 to m do
18 xn[0,j]:=x0[j];
19 end do;
20 #### RK4 marching
21 for n from 1 by 1 to nt do
22 K1:=Vector(eval(F,[x=t,seq(X[i+1]=xn[n-1,i], i = 1..m)]));
23 K2:=Vector(eval(F,[x=t+hh,seq(X[i+1]=xn[n-1,i]+hh*K1[i], i = 1..m)]));
24 K3:=Vector(eval(F,[x=t+hh,seq(X[i+1]=xn[n-1,i]+hh*K2[i], i = 1..m)]));
25 t:=t+h;
26 K4:=Vector(eval(F,[x=t,seq(X[i+1]=xn[n-1,i]+h*K3[i], i = 1..m)]));
27 w:=w+(h/6)*(K1+2*K2+2*K3+K4);
28 for j from 1 by 1 to m do
29 xn[n,j]:=evalf(w[j]);
30 end do
31 end do
32 end proc:
33
34 # Call RK4SYSTEM.mw
35 #--------------------------------
36 with(LinearAlgebra):
37 m := 2:
38 F := [yp, exp(2*x)*sin(x) - 2*y + 2*yp]:
39 X := [x, y, yp]:
40 X0 := <-0.4, -0.6>:
41 a := 0: b := 1: nt := 10:
42 Xn := Array(0..nt, 1..m):
43 RK4SYSTEM(a, b, nt, X, F, X0, Xn):
200 Chapter 5. Numerical Solution of Ordinary Differential Equations
44
60 # Check error
61 #--------------------------------
62 printf(" n y_n y(x_n) y'_n y'(x_n) err(y) err(y')\n");
63 printf("--------------------------------------------------------\n");
64 for n from 0 to nt do
65 xp := h*n + a;
66 printf(" %2d %12.8f %12.8f %12.8f %12.8f %.3g %.3g\n",
67 n, Xn[n, 1], ey(xp), Xn[n, 2], eyp(xp),
68 abs(Xn[n, 1] - ey(xp)), abs(Xn[n, 2] - eyp(xp)));
69 end do:
Result
1 n y_n y(x_n) y'_n y'(x_n) err(y) err(y')
2 ---------------------------------------------------------------------------------------
3 0 -0.40000000 -0.40000000 -0.60000000 -0.60000000 0 0
4 1 -0.46173334 -0.46173297 -0.63163124 -0.63163105 3.72e-07 1.91e-07
5 2 -0.52555988 -0.52555905 -0.64014895 -0.64014866 8.36e-07 2.84e-07
6 3 -0.58860144 -0.58860005 -0.61366381 -0.61366361 1.39e-06 1.99e-07
7 4 -0.64661231 -0.64661028 -0.53658203 -0.53658220 2.02e-06 1.68e-07
8 5 -0.69356666 -0.69356395 -0.38873810 -0.38873905 2.71e-06 9.58e-07
9 6 -0.72115190 -0.72114849 -0.14438087 -0.14438322 3.41e-06 2.35e-06
10 7 -0.71815295 -0.71814890 0.22899702 0.22899243 4.06e-06 4.59e-06
11 8 -0.66971133 -0.66970677 0.77199180 0.77198383 4.55e-06 7.97e-06
12 9 -0.55644290 -0.55643814 1.53478148 1.53476862 4.77e-06 1.29e-05
13 10 -0.35339886 -0.35339436 2.57876634 2.57874662 4.50e-06 1.97e-05
5.6. High-Order Equations & Systems of Differential Equations 201
has a unique solution in the interval |t | ≤ π/4. Can you find the solution, by guess-
ing?
5.2. C Use Taylor’s method of order m to approximate the solution of the following
initial-value problems.
(a) y 0 = e x −y , 0 ≤ x ≤ 1; y(0) = 1, with h = 0.25 and m = 2.
(b) y 0 = e x −y , 0 ≤ x ≤ 1; y(0) = 1, with h = 0.5 and m = 3.
sin x − 2xy
(c) y 0 = 2
1 ≤ x ≤ 2; y(1) = 2, with h = 0.5 and m = 4.
x
5.3. (Do not use computer programming for this problem.) Consider the initial-value
problem:
= 1 + (x − y)2 , 2 ≤ x ≤ 3,
0
y
(5.55)
y(2) = 1,
of which the actual solution is y(x) = x + 1/(1 − x). Use h = 1/2 and a calculator to get
the approximate solution at x = 3 by applying
(a) Euler’s method
(b) RK2
(c) Modified Euler method
(d) RK4
Then, compare their results with the actual value y(3) = 2.5.
5.4. C Now, solve the problem in the preceding exercise, (5.55), by implementing
(a) RK4
(b) Adams-Bashforth-Moulton method
Use h = 0.05 and compare the accuracy.
Use RK4SYSTEM to approximate the solution with h = 0.2, 0.1, 0.05, and compare
the errors to see if you can conclude that the algorithm is a fourth-order method for
systems of differential equations.
Chapter 6
203
204 Chapter 6. Gauss Elimination and Its Variants
Given the coefficients aij and the source bi , we wish to find [x1 , x2 , · · · , xn ]
which satisfy the equations.
• Since it is tedious to write (6.1) again and again, we generally prefer
to write it as a single matrix equation
A x = b, (6.2)
where
a11 a12 · · · a1n x1 b1
a a · · · a x b
21 22 2n 2 2
A = . .. .. , x=. , and b = .. .
. . . . .. .
an1 an2 · · · ann xn bn
where
c ≥ 0 and β ≥ 0 (c + β > 0).
Numerical discretization:
• However, we will meet ghost grid values at the end points. For example,
at the point ax = x0 , the formula becomes
Here the value u−1 is not defined and we call it a ghost grid value.
6.1. Systems of Linear Equations 207
• Now, let’s replace the ghost grid value u−1 by using the boundary con-
dition (6.4.b). The central FD scheme for ux at x0 can be formulated as
u 1 − u −1 uxxx (x0 ) 2
ux (x0 ) ≈ , Trunc.Err = − hx + · · · . (6.8)
2hx 6
Thus the equation (6.4.b), −ux + β u = g , can be approximated (at x0 )
The same can be considered for the algebraic equation at the point xn .
and
hx2 f0 2hx g0
hx2 f1
0
b= ..
+ ..
.
. .
2
hx fnx −1 0
hx2 fnx 2hx gnx
A 0 u = b0 , (6.13)
and
hx2 f0 2hx g0
hx2 f1
0
b0 =
..
+ ..
.
. .
2
hx fnx −1 0
ud 0
6.2. Triangular Systems 209
Definition 6.7.
The first equation involves only the unknown y1 , which can be found as
y1 = b1 /`11 . (6.16)
Now with y2 known, we can solve the third equation for y3 , and so on.
210 Chapter 6. Gauss Elimination and Its Variants
for i=1:n
for j=1:i-1
b(i) = b(i)-L(i,j)*b(j)
end (6.19)
if L(i,i)==0, set error flag, exit
b(i) = b(i)/L(i,i)
end
The result is y .
Computational complexity: For each i , the forward substitution re-
quires 2(i − 1) + 1 flops. Thus the total number of flops becomes
n
X n
X
{2(i − 1) + 1} = {2i − 1} = n(n + 1) − n = n2 . (6.20)
i=1 i=1
6.2. Triangular Systems 211
for i=n:-1:1
if(U(i,i)==0), error(’U: singular!’); end
x(i)=b(i)/U(i,i); (6.23)
b(1:i-1)=b(1:i-1)-U(1:i-1,i)*x(i);
end
The algorithms developed here produce (in the absence of rounding errors)
the unique solution of A x = b, whenever A ∈ Rn×n is nonsingular.
Replacement: Ri ← Ri + αRj (i 6= j)
Interchange: Ri ↔ Rj (6.24)
Scaling: Ri ← β Ri (β 6= 0)
Proposition 6.14.
A x = b1 , A x = b2 , · · · , A x = bp . (6.25)
Figure 6.1
1 0 0 0 2 −1
3 1 0 0 0 8
L = , U= .
−5 − 14
1 0 0 0
1
6 2
0 1 4×4
0 0 4×2
Note: U has the same size as A , that is, its size is 4 × 2. L is a square
matrix and is a unit lower triangular matrix.
∗ ∗ ∗ 1 0 0 0 0 0
| {z } | {z }
L U
P −1 = P T .
Solution.
Example 6.30. Let A ∈ Rn×n , and let Ab be a matrix obtained from scram-
bling the rows of A . Show that there is a unique permutation matrix
P ∈ Rn×n such that A
b = PA .
Hint: Consider the row indices in the scrambled matrix Ab , say {k1 , k2 , · · · , kn }.
(This means that for example, the first row of A
b is the same as the k1 -th
row of A .) Use the index set to define a permutation matrix P .
Proof. (Self-study)
6.3. Gauss Elimination 221
A
b = PA = LU (6.33)
or, equivalently,
A = P T LU (6.34)
2. Solve P T LUx = b
(a) LUx = Pb (permuting b)
(b) Ux = L −1 (Pb) (forward substitution)
(c) x = U −1 (L −1 Pb) (back substitution)
In practice:
) (
A x = b ⇐⇒ P T (LU)x = b L y = Pb
⇐⇒
⇐⇒ L (Ux) = Pb Ux = y
222 Chapter 6. Gauss Elimination and Its Variants
P1 AP2 = LU.
• We can choose P20 = I and P10 so that a11 is the largest entry in absolute
value in its column, which implies L21 = Aa1121 has entries bounded by 1
in modulus.
• More generally, at step k of Gaussian elimination, where we are com-
puting the k th column of L , we reorder the rows so that the largest
entry in the column is on the pivot. This is called Gaussian elimina-
tion with partial pivoting, or GEPP for short. GEPP guarantees
that all entries of L are bounded by one in modulus.
PA = LU
9
0 1 0 1 0 0 1 2
17
− 11
P = 0 0 1 , L = − 23 1 0 , U= 0
3 3
1 4
1 0 0 3
− 17 1 0 0 9
− 17
224 Chapter 6. Gauss Elimination and Its Variants
6.3.4. Calculating A −1
AX = I. (6.36)
A xi = ei , i = 1, 2, · · · , n. (6.38)
Computational complexity
• A naive flop count:
2 3
LU -factorization of A : n + O(n2 )
3
Solve for n equations in (6.38): n · 2n2 = 2n3
8 3
Total cost: n + O(n2 )
3
• A modification: The forward-substitution phase requires the solu-
tion of
L yi = ei , i = 1, 2, · · · , n. (6.39)
Some operations can be saved by exploiting the leading zeros in ei .
(For each i , the portion of L to be accessed is triangular.) With these
savings, one can conclude that A −1 can be computed in 2n3 + O(n2 )
flops.
6.3. Gauss Elimination 225
3 −5
4 2
A = −4 −5 7 and b = −4.
8 6 −8 6
Thus the product of two unit lower-triangular matrices is unit lower triangular.
(a) Implement a function to construct algebraic systems in the full matrix form, for
general nx ≥ 1.
(b) Use a direct method (e.g., A \b ) to find approximate solutions for nx = 25, 50, 100.
(c) The actual solution for (6.40) is u(x) = cos(π x). Measure the maximum errors for
the approximate solutions.
(This problem is optional for undergraduate students; you will get an extra credit
when you solve it.)
226 Chapter 6. Gauss Elimination and Its Variants
Bibliography
[1] G. D AHLQUIST, A special stability problem for linear multistep methods, BIT, 3 (1963),
pp. 27–43.
[2] F. G OLUB AND C. V. L OAN, Matrix Computations, 3rd Ed., The Johns Hopkins Uni-
versity Press, Baltimore, 1996.
227
228 BIBLIOGRAPHY
Index
229
230 INDEX