Exercise Session1
Exercise Session1
Exercise session 1
Topics: finite arithmetic, cancellation error
1. Given the function f (x) = sin(x), approximate f ′ (x) using the following difference quotient
sin(x + h) − sin(x)
f ′ (x) ≈ r(x) = ,
h
with h = 2−k , k = 1, 2, ..., 50.
Fix for instance x = π/4; graphically represent the relative error |f ′ (x) − r(x)|/|f ′ (x)| and comment
the results. After that, reformulate r(x) in order to avoid cancellation errors and graphically represent
the relative error.
2. QUIZ: Consider a floating point arithmetic with basis N = 10 with t digits of mantissa. Let y = f (x)
an assigned numerical problem. Is known the condition number K = 2 × 109 of the problem. Let x̄ a
|y − ȳ|
datum affected by rounding error and let ȳ = f (x̄). If = 10−5 , how many are (at maximum)
|y|
the digits of mantissa t?
Answers:
a) 14
b) 15
c) 13
d) 16
Additional exercises
1. Consider a computer which uses floating-point arithmetic with base N = 10, t = 5 digits for mantissa
and rounding rule. Introduce the following numbers:
a = 1.483593 ,
b = 1.484111.
Determinate the result of s̄ = ā ⊖ b̄, where x̄ stands for machine-number corresponding to x in the
used floating-point arithmetic and ⊖ stands for the machine-subtraction. Compare s̄ with c = a − b
and then compute the relative error of s̄.
x1 = 2 r q
n−1/2
xn = 2 1 − 1 − 41−n x2n−1 , n≥2
which gives a π approximation. Compute the relative errors |π − xn |/π for n = 1, ..., 40 and plot the
relative errors behavior using logarithmic scale. After that, determine an expression x̃n equivalent to
xn which avoids cancellation error. Graphically represent relative errors |π − x̃n |/π for n = 1, ..., 40
using logarithmic scale. Compare this plot with the previous one and comment the results.
1
3. Evaluate the functions:
1 − cos(x) ex − 1
f1 (x) = , f2 (x) = ,
x2 x2
p (x + 1) − 1
f3 (x) = 1 − 1 − x2 , f4 (x) =
x
in x = 10−n , n = 1, 2, .., 16. After that, reformulate the functions in order to avoid cancellation error.
Assuming the values obtained using reformulated functions as exact values, compute relative errors
and compare them with machine precision. Print the relative error for every value of x and graphically
represent it.