Numerical Methods 1
Numerical Methods 1
Z. S. Makumbe, V. Chikwasha
Contents
1 Introduction 2
2 Errors in Numerical Computation 2
2.1 Absolute and Relative Errors . . . . . . . . . . . . . . . . . . . . 2
2.2 Basic Sources of Error . . . . . . . . . . . . . . . . . . . . . . . . 4
4 Polynomial Interpolation 10
4.0.2 For interpolation, the following assumptions are made: . . 10
4.0.3 Existence of interpolating polynomial . . . . . . . . . . . 10
4.1 Finite Dierences . . . . . . . . . . . . . . . . . . . . . . . . . . . 12
4.1.1 Dierences Tables . . . . . . . . . . . . . . . . . . . . . . 13
4.2 Newton's Forward Dierence (for equally spaced points) . . . . . 13
4.3 Newton's Divided Dierence (for unequally spaced points) . . . . 15
1
4.4 Lagrange Interpolating Polynomial . . . . . . . . . . . . . . . . . 16
4.5 Approximation by Spline Functions . . . . . . . . . . . . . . . . . 18
4.5.1 Properties of S . . . . . . . . . . . . . . . . . . . . . . . . 19
4.5.2 Second Degree Splines . . . . . . . . . . . . . . . . . . . . 20
5 Numerical Dierentiation 21
5.1 Two Point Forward Dierence Formula . . . . . . . . . . . . . . . 21
5.2 Two Point Backward Dierence Formula . . . . . . . . . . . . . . 21
5.3 Three Point Central Dierence Formula . . . . . . . . . . . . . . 22
5.4 Three Point Formula for First Order Derivative by the Lagrange
Polynomial . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
5.5 Second Order Derivatives . . . . . . . . . . . . . . . . . . . . . . 23
6 Numerical Integration 24
6.1 The Trapezoidal Rule . . . . . . . . . . . . . . . . . . . . . . . . 24
1 Introduction
2 Errors in Numerical Computation
2.1 Absolute and Relative Errors
Denition
A number a∗ is said to be an approximate number to the exact number a,
if it diers slightly from a. The approximate number, a∗ is called a minor
approximation if it is less than a, and a major approximation if it is greater
than a. √
For example, 2.6 is a minor approximation of 7 and 2.7 is a mjor approxi-
mation.
If a∗ is an approximate value of a, we write a∗ ≈ a.
Denition
The dierence between the exact number a and the approximate number a∗ is
called the error, sometimes denoted by ∆a, that is,
∆a = a − a∗ ,
2
Denition
If a∗ is an approximation to a and ∆ the absolute error, then
∆ = |a − a∗ | ≤ ∆a
Example
Determine the limiting absolute error of the approximation number a = 2.7 to
the number e.
Solution:
∆ = |e − 2.7| ≤ ∆a
2.7 − ∆a ≤ e ≤ ∆a + 2.7
Since e > 2.7 and e < 2.72, |e − 2.7| < 2.72 − 2.7 = 0.02 ⇒ ∆a = 0.02.
Denition
If a∗ is an approximation to a, then
∆
δ=
|a|
Denition
The limiting relative error δa of the approximation a∗ to a is any number not
less than the relative error of that number. For example,
δ ≤ δa
or
∆
< δa
|a|
3
Thus, the limiting absolute error of a number a∗ can be written as
∆a = |a|δa ≈ |a∗ |δa
So, a∗ − ∆a ≤ a ≤ a∗ + ∆a can be written as
a∗ − a∗ δa ≤ a ≤ a∗ + a∗ δa
⇒ a (1 − δa ) ≤ a ≤ a∗ (1 + δa )
∗
or a = a∗ (1 ± δa ).
Example
The weight of 1dm3 of water at 0o C is given as p = 999.847gf ± 0.001gf
(gf = gram f orce). Determine the error of the result of weighing the water.
Solution:
The limiting absolute error ∆p = 0.001gf and p ≤ 999.847 + 0.001 = 999.848gf .
0.001
δp = · 100 %
999.848
= 10−4 %
3. Initial error - Some errors arise due to the use of approximate numerical
parameters whose values are determined either experimentally or through
innite processes. When performing computations with machines these
errors are carried into the nal data.
4
3 Solution of Non-linear Equations
Consider the equation
f (x) = 0 (1)
where f (x) is dened and continuous on some nite or innite interval a < x < b.
If f (ξ) = 0, then ξ ∈ [a, b] is called a root of the equation.
2. Improve the values of the approximate roots, that is, rene the roots to
the required degree of accuracy
• if f (ξ) = 0, then the root ξ will be unique if f 0 (x) exists and retains the
same sign throughout [a, b]. E.g. f (x) > 0 (or f 0 (x) < 0) for α < ξ < β .
0
y
6
y = f (x)
f (β)
-
> x
α ξ β
f (α)
a0 xn + a1 xn−1 + . . . + an = 0, (a0 6= 0) ai ∈ R, i = 1, 2 . . . , n
has at most n real roots. n + 1 sign changes on [α, β] assures us of the isolation.
5
Example
Isolate the roots of f (x) ≡ x3 − 6x + 2 = 0 on (−∞, ∞).
x −∞ -3 -1 0 1 3 +∞
f (x) - - + + - + +
Example:
Solve 3x2 − ex = 0
Solution:
Consider 3x2 = ex . y = ex
y = 3x2
6
1 p
-
ξ
ξ ≈ 0.9
6
3.2.1 Procedure
To nd the root of f (x)
= 0 in the interval [a, b], divide the interval into two
subintervals. If f a+b2 = 0, then ξ =
a+b
2 is a root. If not, search for the root
in the interval [a, a+b a+b
2 ] or [ 2 , b]. Let this new subinterval be [a1 , b1 ].
Bisect the new interval again, and make the same investigation of nding a
root, and so on.
This leads to either an exact root or an innite sequence of nested intervals
[a1 , b1 ], [a2 , b2 ], . . . , [an , bn ] that contain ξ and is such that
and
b−a
bn − an =
2n
that is, if the interval [a, b] is subinterval into two, four, eight, etc, equal subin-
tervals, then the length of each subinterval is
b−a
= bn − an
2n
The end points a1 , a2 , . . . , an form a monotonic non-decreasing bounded se-
quence, and b1 , b2 , . . . , bn form a monotonic non-increasing bounded sequence.
Thus, limn→∞ (bn − an ) = limn→∞ b−a 2n = 0. Hence,
ξ = lim bn = lim an
n→∞ n→∞
.
Asn → ∞, f (x) is continuous on [a, b], |f (ξ)|2 ≤ 0. This is possible only if
f (ξ) = 0, which means ξ is a root of f (x) = 0.
Example:
Find the root of f (x) = x4 + 2x3 − x − 1 = 0 in the interval [0,1] using the
bisection method.
Solution:
f (0) = −1 and f (1) = 1. ∴ f (x) has a root in [0,1].
ξ1 = 0+1
2 = 0.05 and f (0.5) = −1.1900, ∴ f (x) has a root in [0.5,1]. Contin-
uing with the iterations we obtain the following table
7
n an bn ξn f (ξn )
1 0.0000 1.0000 0.5000 -1.1900
2 0.05000 1.0000 0.7500 -0.5900
3 0.7500 1.0000 0.8750 0.0500
4 0.7500 0.8750 0.8125 -0.3040
5 0.81250 0.8750 0.8438 -0.1350
6 0.8438 0.8750 0.8594 -0.0430
7 0.8594 0.8750 0.8672 -0.0027
8 0.8672 0.8750 0.8711 -0.0267083
Error = |ξ − ξ7 |
≤ |b7 − a7 |
= 0.8750 − 0.8594
= 0.0156
|b7 − a7 |
Absoluteerror = ≈ 0.0181 = 1.8 × 10−2
|a7 |
Excercise:
Use the bisection method to nd the smallest positive root of x3 − 2x + 1 = 0.
f (x)
r
6
(x0 , f (x0 ))
- x
a ξ x1 b = x0
y = `(x)
8
`(x) = f 0 (x0 )(x − x0 ) + f (x0 )(equation of tangent line). At b = x0 , f and `
agree. Therefore , we take the zero of ` as an approximation to the zero of f .
The zero of `(x) is given by
f (x0 )
x1 = x0 − .
f 0 (x0 )
f (x1 )
x2 = x1 −
f 0 (x0 )
f (x2 )
x3 = x2 − 0
f (x2 )
.
.
.
h2 00
f (x0 + h) = f (x0 ) + hf 0 (x0 ) + f (x0 ) + . . . = 0
2!
.
We can determine h by taking the rst two terms.
f (x0 ) + hf 0 (x0 ) = 0
f (x0 )
⇒h = −
f 0 (x0 )
x1 = x0 + h
f (x0 )
= x0 − 0
f )x0 )
f (xn )
xn+1 = xn − , n = 0, 1, 2, . . . ,unexpected00 inmathunexpected00 inmath
f 0 (xn )
9
Excercise:
1. Use Newton's method to nd the negative root for x4 −3x2 +75x−10000 =
−3
0 within 10 within the interval [-15;-10].
2. Two of the zeros of x4 + 2x3 − 7x2 + 3 are positive. Find them by Newton's
method, correct to two signicant gures.
4 Polynomial Interpolation
Interpolation is to connect discrete data points in a plausible way so that one
can get reasonable estimates of data points between the given points. The inter-
polation curve goes through all data points. Numerically, we can say that inter-
polation is the technique of obtaining a value of a function for any intermediate
value of the independent variable if the values of the function corresponding to
that variable are given in tabular form. On the other hand, the technique of
computing the value of the function outside the range of given values is called
extrapolation.
Example Given the table
x 0 4 8 12 16
f (x)5 7 10 13 17
The technique of nding f (1), f (2), f (7), f (15) etc, is called interpolation,
while nding f (−1), f (17), etc is extrapolation.
p(xi ) = yi , 0 ≤ i ≤ n
p(x) = y0 .
10
When n = 1, we have two points. A linear function can be tted
y1 − y0
p(x) = y0 + (x − x0 ).
x1 − x0
Thus p(x0 ) = y0 and p(x1 ) = y1 are easily veried.
Example:
Find the polynomial of least degree that interpolates the following table.
x 2 5
y 6 18
Solution:
18 − 6
p(x) = 6+ (x − 2)
5−2
= 76 + 4(x − 2)
= 4x − 2
To produce one more entry in the table we add another term p4. Assuming
p(xi ) = yi , 0 ≤ i ≤ k . We consider
Theorem
Given (n + 1) points x1 , x2 , . . . , x(n+1) , there is a unique polynomial of degree
m≤n that will interpolate through the
y (n + 1) points.
y3 y4 y5 y6 yn yn+1
y1 y2
-
x
x1 x2 x3 x4 x5 x6 ... xn xn+1
2 n
P (x) = a0 + a1 x + a2 x + . . . + an x
11
Dierence tables are another way to write the nth order polynomial that
passes through (n + 1) points of the curve, (xi , f (xi )), i = 0, 1, 2, . . . , n.
where ∆x and ∆y are increments of x and y respectively, is called the rst nite
dierence of f .
Finite dierences of higher order are denoted by
∆n y = ∆(∆n−1 y), n = 1, 2, 3 . . . .
1. ∆(u + v) = ∆u + ∆v .
2. ∆(cu) = c∆u. (where c is a constant)
3. ∆m (∆n y) = ∆m+n y .
for given functions u and v, and m and n being nonnegative integers.
Example:
Construct the nite dierence for the function P (x) = x2 , taking the interval
increment to be ∆x = 1.
Solutio
12
In general if Pn (x) = a0 xn + a1 xn−1 + . . . + an is an nth order polynomial, then
∆n Pn (x) = n!a0 hn
= constant, where h = ∆x
In general,
n(n − 1)
∆n yi = yi+n − nyi+n−1 + yi+n−2 − . . . + (−1)k n Ck y(i+n−k) + . . . + (−1)n yi unexpected00 inmathunex
2
We can construct a forward nite dierences table as follows:
x y ∆y ∆2 y ∆3 y ∆4 y
x0 y0
∆y0
x1 y1 ∆2 y 0
∆y1 ∆3 y 0
2
x2 y2 ∆ y1 ∆4 y0
3
∆y2 ∆ y1
.
.
x3 y3 ∆2 y 2 .
.
.
∆y3 .
. .
. .
x4 y4 . .
. . . . . .
. . . . . .
. . . . . .
Pn (x) = a0 + a1 (x − x0 ) + a2 (x − x0 )(x − x1 ) + . . .
+ an (x − x0 )(x − x1 ) . . . (x − xn−1 ) (2)
13
for appropriate constants a0 , a1 , . . . , an .
Since the x0i s are given, the problem is to determine coecients ai .
Pn (x0 ) = y0 = a0
Pn (x1 ) = y1 = a0 + a(x1 − x0 ) = a0 + a1 h
y1 − y0 ∆y0
⇒ a1 = =
h h
Pn (x2 ) = y2 = a0 + a1 (x2 − x0 ) + a2 (x2 − x0 )(x2 − x1 )
⇒ y2 − y1 − 2(y1 − y0 ) = 2a2 h2
y2 − 2y1 + y0 ∆2 y0
∴ a2 = 2
=
2h 2h2
.
.
.
∆n y 0
an =
n!hn
Substituting for ai (i = 0, 1, 2, . . . , n) in (2),
∆y0 ∆2 y 0
Pn (x) = y0 + (x − x0 ) + (x − x0 )(x − x1 ) + . . . +
h 2!h2
∆n y 0
+ (x − x0 )(x − x1 ) . . . (x − xn−1 ).
n!hn
Example:
Construct Newton's interpolation polynomial on [3.5;3.7] using the spacing h=
0.05 for f (x) = ex given in the following table.
SOLUTION:
We rst form a horizontal dierences table:
x y ∆y ∆2 y ∆3 y ∆4 y
3.5 33.115 1.698 0.087 0.005 -0.002
3.55 34.813 1.785 0.092 0.003
3.6 36.598 1.887 0.95
3.65 38.475 1.972
3.7 40.447
1.698 0.087
P4 (x) = 33.115 + (x − 3.5) + (x − 3.5)(x − 3.55)
0.05 2 × 0.052
0.005
+ (x − 3.5)(x − 3.55)(x − 3.6)
6 × 0.053
0.002
− (x − 3.5)(x − 3.55)(x − 3.6)(x − 3.65).
24 × 0.054
14
Excercise:
Construct an empirical formula for the function represented by the following
table.
x 0 1 2 3 4 5
y 5.2 8.0 10.4 12.4 14.0 15.2
y0 y1 y2 y3
- x
x0 x1 x2 x3 xn
As for Newton's forward dierence, the nth degree polynomial may be writ-
ten as
y0 = a0
y1 − y0 = a1 (x1 − x0 )
y1 − y0
⇒ a1 = , denoted y[x1 , x0 ]
x1 − x0
y2 − y0 − (x2 − x0 )(y1 − y0 )
a2 =
(x2 − x0 )(x2 − x1 )
y[x2 , x1 ] − y[x1 , x0 ]
= denoted y[x2 , x1 , x0 ]
x2 − x0
.
.
.
ak = y[xk , xk−1 , . . . , x0 ]
y[xk , xk−1 , . . . , x1 ] − y[xk−1 , xk−2 , . . . , x0 ]
=
xk − x0
15
The Newton's divided dierence polynomial can be written as
y = Ln (x)
- x
x0 x1 xn
1, i=j
`i (x) = δij =
0, i 6= j
16
The polynomial vanishes at n+1 points: x0 , x1 , . . . , xi−1 , xi+1 , . . . , xn and
thus has the form
Ln (xi ) = yi
Xn
Ln (x) = `i (x)yi
i=0
n
X
Ln (xj ) = `i (xj )yi
i=0
= `j (xj )yj
= yj (j = 0, 1, 2, . . . , n).
Thus,
n
X (x − x0 )(x − x1 )(x − x2 ) . . . (x − xi−1 )(x − xi+1 ) . . . (x − xn )
Ln (x) = f (xi )
i=0
(xi − x0 )(xi − x1 ) . . . (xi − xi−1 )(xi − xi+1 ) . . . (xi − xn )
i = 0, 1, 2, . . . , n
The nth Lagrange interpolating polynomial.
Excercise:
Consider the table of values of y = f (x).
x 321.0 322.8 324.2 325.0
y 2.50651 2.50893 2.51081 2.51188
Estimate f (324.7).
17
Solution:
3
x − xj
with `i (x) = Π3j=0,j6=i
X
L3 (x) = f (xi )`i (x) ,
i=0
xi − xj
⇒ f (324.7) ≈ 2.52397
Excercise:
Given the Gaussian function
2
g(x) = e−x /5
x -5 -4 -3 -2 -1 0
f (x) 0.0067379 0.0183156 0.0497871 0.1353353 0.3678794 1
18
pr
s0 s2 s4 r h. . .
ph sn−2
s1 r r s3 h hr
h pq
r r
- x
t0 t1 t2 t3 t4 t5 ... tn−1
4.5.1 Properties of S
1. The domain of S is an interval [a, b].
Excercise:
Show that G(x) is not a spline function of rst degree.
x, x ∈ [−1, 0]
G(x) = 1 − x, x ∈ [0, 1]
2x − 2 , x ∈ [1, 2]
Solution:
lim x = lim (1 − x) = 1
x→0+ x→0
lim x = lim x = 0
x→0− x→0
Thus lim G(x) 6= lim G(x)
x→0+ x→0−
19
4.5.2 Second Degree Splines
A function S(x) is spline of degree 2 if S is a piecewise quadratic polynomial
such that S and S0 are continuous.
Example:
Given that
x, x≤0
G(x) = −x2 , 0 ≤ x ≤ 1
1 − 2x , x ≥ 1
Solution:
lim G(x) = lim x2 = 0
x→0− x→0−
Zi+1 − Zi
Qi (x) = (x − ti )2 + Zi (x − ti ) + yi
2(ti+1 − ti )
where Zi = Q0 (ti )
20
Excercise:
Determine whether the following are quadratic splines.
0.1x2 , 0≤x≤1
1. S(x) =
9.3x2 − 18.4x + 9.2 , 1 ≤ x ≤ 1.3
x , −∞ < x < 1
2. S(x) = x2 , 1 ≤ x ≤ 2
4, 2≤x≤∞
5 Numerical Dierentiation
Numerical dierentiation is the process of calculating the value of the derivative
of a function y = f (x), at some assigned value of x from a given set of values
(xi , yi ).
The derivation of formulae for numerical dierentiation hinges on the Taylor
series expansion of f (x + h) and f (x − h), h > 0.
h2 00 h3
f (x + h) = f (x) + hf 0 (x) + f (x) + f 000 (x) + . . . (4)
2! 3!
and
h2 00 h3
f (x − h) = f (x) − hf 0 (x) + f (x) − f 000 (x) + . . . (5)
2! 3!
h2 00
f (x + h) = f (x) + hf 0 (x) + f (c1 )
2!
for some c1 ∈ [x, x + h].
f (x + h) − f (x)
⇒ f 0 (x) = + ET (h)
h
where ET (h) is the truncation error and is given by
h
ET (h) = − f 00 (c1 )
2
21
where
h
ET (h) = − f 00 (c2 )
2
for some c2 ∈ [x − h, x].
2h3 000
f (x + h) − f (x − h) = 2hf 0 (x) + f (x) + . . .
3!
f (x + h) − f (x − h)
⇒ f 0 (x) = + ET (h)
2h
where
h2 000
ET (h) = f (c)
6
for some c ∈ [x − h, x + h]
Example:
Let f (x) be given by the table below
Solution:
f (x + h) − f (x)
f 0 (x) =
h
f (1.50 + 0.05) − f (1.50)
⇒ f 0 (1.50) ≈
0.05
f (1.55) − f (1.50)
=
0.05
7.30278 − 6.72253
=
0.05
= 11.6050
Excercise:
Use the three point central dierence formula with h = 0.1 to estimate f 0 (1.50)
using the table above.
22
5.4 Three Point Formula for First Order Derivative by the
Lagrange Polynomial
Consider an approximation to f (x) through x0 , x1 = x0 + h, x2 = x0 + 2h by
the Lagrange polynomial P2 (x).
(x − x1 )(x − x2 ) (x − x0 )(x − x2 ) (x − x0 )(x − x1 )
P2 (x) = f0 + f1 + f2
(x0 − x1 )(x0 − x2 ) (x1 − x0 )(x1 − x2 ) (x2 − x0 )(x2 − x1 )
where fk = f (xk ).
2x − x1 − x2 2x − x0 − x2 2x − x0 − x1
f 0 (x) = P 0 (x) = f0 + f1 + f2 .
2h2 h2 2h2
Estimating at x0 , x1 , x2 we obtain three point formulae:
1
f 0 (x) ≈ (−3f0 + 4f1 − f2 ) − forward dierence formula
2h
1
f 0 (x1 ) ≈ (−f0 + f2 ) − central dierence formula
2h
1
f 0 (x2 ) ≈ (f0 − 4f1 + 3f2 ) − backward dierence formula
2h
f (x + h) − 2f (x) + f (x − h)
f 00 (x) = + ET (f, h) − central dierence formula
h2
where
h2 (iv)
ET (f, h) = − f (c)
12
where c ∈ [x − h, x + h].
Example:
Given f (x) = xex , use the central dierence formula to approximate f 00 (0.5)
with h = 0.1, 0.01, 0.001. Compare the calculated value with the true value.
23
Solution:
f (x + h) − 2f (x) + f (x − h)
f 00 (x) =
h2
for h = 0.01
f (0.51) − 2f (0.5) + f (0.49)
f 00 (0.5) =
0.0001
≈ 4.121864950
6 Numerical Integration
The process of evaluating denite integrals from a set of tabulated values of the
integrand f (x) is called numerical integration.
The general form of numerical integration of a function f (x) over some in-
terval [a, b] is a weighted sum of the function values at a nite number (N + 1)
of sample points (nodes), referred to as quadrature
P
Q
y0 y1 y2 yi yi+1 yn−1 yn
M N
- x
a = x0 x1 x2 xi xi+1 xn−1 b = xn
To derive the Trapezoidal rule, divide the area represented by I into a num-
ber of thin strips.
Let
b−a
h=
n
24
n number of strips.
, be the interval length,
h is small, area due to the ith
Provided strip is approximated by area of
trapezium M P QN .
Let xr = a + rh, fr = f (xr ) = yr
Total area under the curve is approximated the sum of areas of the n trapezia,
i.e.
h h h
I ≈ (f0 + f1 ) + (f1 + f2 ) + . . . + (fn−1 + fn )
2 2 2
1 1
= h( (f0 + f1 + f2 + . . . + fn−1 + fn )
2 2
Xn
00
= h fr , where means rst and last term are halved.
r=0
xk+1 = xk + h, k = 0, 1, 2, . . . , n − 1
where
b−a
h= , x0 = a , xn = b
n
.
Assuming y has (n+1) continuous derivatives in [xk , xk+1 ], k = 0, 1, 2, . . . , n−
1, by Taylor's theorem
y(xk+1 ) = y(xk + h)
h2 00 hn (n) h(n+1) (n+1)
= y(xk ) + hy 0 (xk ) + y (xk ) + . . . + y (xk ) + y (ck )
2 n! (n + 1)!
for some ck ∈ [xk , xk+1 ].
For h very small
h2 00 hn (n)
y(xk + h) = y(xk ) + hy 0 (xk ) + y (xk ) + . . . + y (xk )
2! n!
Example:
Use Taylor's method of order 4 to solve
y 0 = y + ex on [0, 2] with y(0) = 1, and h=1
25
Solution:
y0 = y + y + ex
y 00 = y 0 + ex
= y + 2ex
000
y = y + 3ex
y (iv) = y + 4ex
y = (1 + x)ex
∴ y1 = 2e2
= 5.43656
|e| = 0.06156
26