Assignment One, Question Four

Assignment One, Question Four
(a) We have
f (x) =
1
cos(x)
sin(x)
with f (0) = 0 and x 2 [ ⇡/2, +⇡/2]. To show that one can expect to be able to write a
computer program that uses floating-point arithmetic to accurately evaluate f (x), we need to
compute the condition number of f (x) and show that it is not too large (in absolute value).
The condition number may be computed as follows:
(f (x)) =
=
=
=
=
=
x · f 0 (x)
f✓
(x)
sin(x) ⇤ sin(x)
x·
(1 cos(x))(cos(x))
sin2 (x)
f (x)
✓ 2
◆
2
sin (x) + cos (x) cos(x)
x·
sin2 (x)
f◆
(x)
✓
1 cos(x)
x·
sin2 (x)
✓ f (x) ◆
f (x)
x·
sin(x)
f (x)
x
sin(x)
Now let us examine the function (f (x)) =
◆
x
because we need to know its global extrema
sin(x)
on the interval [ ⇡/2, +⇡/2]. We have
d
sin(x) x cos(x)
=
.
dx
sin2 (x)
d
Local extrema will occur where
= 0 or is undefined. For x in [ ⇡/2, 0)[(0, +⇡/2], sin2 (x) >
dx
d
0. Hence
= 0 only where sin(x) x cos(x) = 0, or equivalently where x = tan(x). (The
dx
derivative will not be undefined on the interval, since the denominator won’t be zero in the
interval and the numerator is just the di↵erence of continuous functions.) Also | tan(x)| > |x|
for [ ⇡/2, 0) [ (0, +⇡/2]. Hence (f (x)) does not have a critical point in [ ⇡/2, 0) [ (0, +⇡/2].
d
sin(x) x cos(x)
0
Evaluating
=
at x = 0 leads to the indeterminate form , however,
2
dx
0
sin (x)
d
applying l’Hôpital’s rule twice shows that
(0) = 0, and therefore x = 0 is a critical point.
dx
The global extrema for (f (x)) will occur at
point (x = 0) or at the endpoints
✓ the critical
◆
x
x = ±⇡/2. But (f (0)) = 1 (because limx!0
= 1) and (f (±⇡/2)) = ⇡/2. Therefore,
sin(x)
it follows that |(f (x))|  ⇡/2 for x 2 [ ⇡/2, +⇡/2].
1
Hence, the significance of small relative errors in an argument to f (x) can be magnified by
a factor of at most ⇡/2 (a small number), and we can conclude that evaluating f (x) is wellconditioned for x 2 [ ⇡/2, +⇡/2]. We can expect to be able to evaluate f (x) accurately using
floating-point arithmetic.
(b) The formula f = ( 1 - cos(x) ) / sin( x ) does not lead to a numerically stable algorithm,
as it would su↵er from catastrophic cancellation in the numerator calculation when x is near
0, because there cos(x) is almost 1. There would also be trouble evaluating the formula at x
= 0 as both the numerator and the denominator would be calculated to be 0, leading to the
indeterminate form 0/0.
Rearranging, we have
1
cos(x)
1 cos(x) 1 + cos(x)
=
sin(x)
sin(x) 1 + cos(x)
1 cos2 (x)
=
sin(x)(1 + cos(x))
sin2 (x)
=
sin(x)(1 + cos(x))
sin(x)
=
1 + cos(x)
This suggests that the algorithm f = sin( x ) / ( 1 + cos(x) ) be used. This form can be
evaluated accurately for x near zero because then the terms 1 and cos(x) are positive and hence
no catastrophic cancellation can occur. Since the resulting operations are the stable operations
of addition and division, we conclude that the given algorithm is numerically stable. When
x = 0, the result f = 0 is computed, which is correct, as an application of l’Hôpital’s rule to
the original representation for f (x) at x = 0 will show. We assume that sin(x) and cos(x)
are accurately computed by standard math library functions.
Note: it is possible that there are other equivalent representations for f (x) that lead to numerically stable algorithms.
2