Chapter 5
Chapter 5
Gerald W. Recktenwald
Department of Mechanical Engineering
Portland State University
gerry@[Link]
These slides are a supplement to the book Numerical Methods with Matlab:
Implementations and Applications, by Gerald W. Recktenwald,
c 2000–2006, Prentice-Hall,
c 2000–2006 Gerald W. Recktenwald.
Upper Saddle River, NJ. These slides are copyright
The PDF version of these slides may be downloaded or stored or printed for noncommercial,
educational use. The repackaging or sale of these slides in any form, without written
consent of the author, is prohibited.
The latest version of this PDF file, along with other supplemental material for the book,
can be found at [Link]/recktenwald or [Link]/~gerry/nmm/.
• Truncation error
⊲ Demonstrate with Taylor series
⊲ Order Notation
Expert’s Note: The built-in int8, int16, int32, uint8, uint16, and
uint32 classes are used to reduce memory usage for
very large data sets.
(bbbb)2 ⇐⇒ |23|22|21|20|
Example:
(1001)2 = 1 × 23 + 0 × 22 + 0 × 21 + 1 × 20
=8+0+0+1=9
Limitations:
• Limiting the number of bits limits the size of integer that can be
represented
Numeric values with non-zero fractional parts are stored as floating point
numbers.
Example:
1
The IEEE Standard on Floating Point arithmetic defines a normalized binary format. Here we use a
simplified decimal (base ten) format that, while abusing the standard notation, expresses the essential ideas
behind the decimal to binary conversion.
Floating point values have a fixed number of bits allocated for storage of
the mantissa and a fixed number of bits allocated for storage of the
exponent.
The finite number of bits in the exponent limits the magnitude or range of
the floating point numbers.
The finite number of bits in the mantissa limits the number of significant
digits or the precision of the floating point numbers.
1
The floating point mantissa is expressed in powers of
2
0
1
= 1 is not used
2
1 2 3
1 1 1
= 0.5 = 0.25 = 0.125 ···
2 2 2
Most real numbers cannot be stored exactly (they do not exist on the
floating point number line)
• Numbers with 15 (decimal) digit mantissas that are the exact sum of
powers of (1/2) can be stored exactly.
In other words: The floating point number line is a subset of the real
number line.
denormal
under- under-
flow flow
overflow usable range usable range overflow
zoom-in view
• DeriveTM
• MACSYMATM
• MapleTM
• MathematicaTM
f is close to, but not exactly equal to zero because of roundoff. Also note
that f is a single value, not a formula.
Operation Result
2+2=4 integer
9 × 7 = 63 integer
12
=4 integer
3
29
=2 exact result is not an integer
13
29
=0 exact result is not an integer
1300
>> v = 13*u
v =
29
>> v-29
ans =
0
Two rounding errors are made: (1) during computation and storage of u,
and (2) during computation and storage of v. Fortuitously, the
combination of rounding errors produces the exact result.
>> x = 29/1300
x =
2.230769230769231e-02
>> y = 29 - 1300*x
y =
3.552713678800501e-015
x1 = 54.3218158995, x2 = 0.0018410049576.
p q
2
b2 − 4ac = (−54.32) − 0.4000
√
= 2951 − 0.4000
√
= 2951
= 54.32
Use x1,4 to designate the first root computed with four-digit arithmetic:
√
−b + b2 − 4ac
x1,4 =
2a
+54.32 + 54.32
=
2.000
108.6
=
2.000
= 54.30
A solution: rationalize the numerators of the expressions for the two roots:
√ √ !
−b + b2 − 4ac −b − b2 − 4ac 2c
x1 = √ = √ , (4)
2a 2
−b − b − 4ac 2
−b − b − 4ac
√ √ !
−b − b2 − 4ac −b + b2 − 4ac 2c
x2 = √ = √ (5)
2a 2
−b + b − 4ac −b + b2 − 4ac
Now use Equation (5) to compute the troublesome second root with four
digit arithmetic
2c 0.2000 0.2000
x2,4 = √ = = = 0.001842.
2
−b + b − 4ac +54.32 + 54.32 108.6
2c
x1,4 = √
−b − b2 − 4ac
0.2000
= (i)
+54.32 − 54.32
0.2000
= (ii)
0
= ∞.
√
Limited precision in the calculation of b2 + 4ac leads to a catastrophic
cancellation error in step (i)
A robust solution uses a formula that takes the sign of b into account in a
way that prevents catastrophic cancellation.
The ultimate quadratic formula:
1h p i
q ≡ − b + sign(b) b2 − 4ac
2
where (
1 if b ≥ 0,
sign(b) =
−1 otherwise
Then roots to quadratic equation are
q c
x1 = x2 =
a q
Summary
The errors in
c=a+b and c=a−b
will be large when a ≫ b or a ≪ b.
Consider c = a + b with
a = [Link] . . . × 100
b = [Link] . . . × 10−8
available precision
z }| {
[Link] xxxx xxxx xxxx
+ 0.000 0000 yyyy yyyy yyyy yyyy
= [Link] xxxx zzzz zzzz yyyy yyyy
| {z }
lost digits
The most significant digits of a are retained, but the least significant digits
of b are lost because of the mismatch in magnitude of a and b.
c=a−b
Consider c = a − b with
a = x.xxxxxxxxxxx1ssssss
b = x.xxxxxxxxxxx0tttttt
where x, y, s and t are decimal digits. The digits sss . . . and ttt . . . are
lost when a and b are stored in double-precision, floating point format.
Summary
1+δ =1
epsilon = 1;
it = 0;
maxit = 100;
while it < maxit
epsilon = epsilon/2;
b = 1 + epsilon;
if b == 1
break;
end
it = it + 1;
end
Let
Absolute error
Eabs(b
α) = αb − α
Relative error
αb − α
α) =
Erel(b
αref
Since
x3 x5
sin(x) = x − + − ...
3! 5!
we can approximate sin(x) with
sin(x) ≈ x
x3 x5
Eabs = x − sin(x) = − + ...
3! 5!
x − sin(x) x
Eabs = = −1
sin(x) sin(x)
quickly.
10
The relative error grows
Error
quickly because the absolute 5
value of sin(x) is small near
x = 0. 0
−5
−0.4 −0.3 −0.2 −0.1 0 0.1 0.2 0.3
x (radians)
where δ is a small.
In Matlab:
x = ... % initialize
xold = ...
In Matlab:
x = ... % initialize
xold = ...
1.6
y = cos(x)
1
0.8
and
0.6
y=x
0.4
0.2
0
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6
x (radians)
1. Guess x0
2. Set xold = x0
3. Update guess
xnew = cos(xold)
4. If xnew ≈ xold stop; otherwise set xold = xnew and return to step 3
MATLAB implementation
Bad test # 1
This test will be true unless xnew and xold are exactly equal. In other
words, xnew and xold are equal only when their bit patterns are identical.
This is bad because
• Even if test is eventually met, the iterations will probably do more work
than needed
Bad test # 2
These values of xnew and xold are not close, but the erroneous
convergence criterion would cause the iterations to stop.
The user supplies appropriate value of xref. For this particular iteration
we could use xref = xold.
x3 x5
sin(x) = x − + − ···
3! 5!
For small x, only a few terms are needed to get a good approximation to
sin(x). The · · · terms are “truncated”
The size of the truncation error depends on x and the number of terms
included in fsum.
>> s = sinser(pi/6,5e-9,15);
Series approximation to sin(0.523599)
k term ssum
1 5.236e-001 0.52359878
3 -2.392e-002 0.49967418
5 3.280e-004 0.50000213
7 -2.141e-006 0.49999999
9 8.151e-009 0.50000000
11 -2.032e-011 0.50000000
The truncation error in the series is small relative to the true value of
sin(π/6)
>> s = sinser(pi/6,5e-9,15);
.
.
>> err = (s-sin(pi/6))/sin(pi/6)
err =
-7.1276e-014
k term ssum
1 7.854e+000 7.85398163
3 -8.075e+001 -72.89153055
5 2.490e+002 176.14792646
.
. .
. .
.
. . .
25 1.537e-003 1.00012542
27 -1.350e-004 0.99999038
29 1.026e-005 1.00000064
Increasing the number of terms will allow the series to converge with the
tolerance of 5 × 10−9. A better solution to the slow convergence of the
series are explored in Exercise 23.
where
(n+1) (n+1)
(x − x0) d f
Rn(x) =
(n + 1)! dx(n+1) x=ξ
1 x − x0
P1(x) = +
1 − x0 (1 − x0)2
1 x − x0 (x − x0)2
P2(x) = + +
1 − x0 (1 − x0)2 (1 − x0)3
1 x − x0 (x − x0)2 (x − x0)3
P3(x) = + + +
1 − x0 (1 − x0)2 (1 − x0)3 (1 − x0)4
−0.5
−1
−1.5
Approximations to f(x) = 1/(1−x)
−2
−2.5
−3
−3.5
−4 exact
P1(x)
P (x)
−4.5 2
P (x)
3
−5
1.3 1.4 1.5 1.6 1.7 1.8 1.9 2
x
′ f (x + h) − f (x) h ′′
f (x) = − f (x) + . . .
h 2
This approximation is said to be first order because the leading term in the
truncation error is linear in h. Dropping the truncation error terms we
obtain
′ f (x + h) − f (x) ′
ffd (x) = or ffd (x) = f ′(x) + O(h)
h
To study the roles of roundoff and truncation errors, compute the finite
difference2 approximation to f ′(x) when f (x) = ex.
′ d x
The relative error in the ffd (x) approximation to e is
dx
′
ffd (x) − f ′(x) ffd
′
(x) − ex
Erel = =
f ′(x) ex
2
The finite difference approximation is used to obtain numerical solutions to ordinary and partial
differentials equations where f (x) and hence f ′ (x) is unknown.
dominates at large h. 10
−1
−5
10
−6
10
−7
10
−8
10 −12 −10 −8 −6 −4 −2 0
10 10 10 10 10 10 10
Stepsize, h