Col726 2302 Ass1 Solutions
Col726 2302 Ass1 Solutions
Use single precision (24 bits) unless specified otherwise. Whenever you are asked to explain
some results, you should explain the observation in a quantitative manner. For example, if
a curve has a bend at say n = 1000, you need to explain why you would expect the bend to
happen roughly around this value of n.
Assume without loss of generality that |x| > |y|. Then |hy+xk+hk|
|h|+|k|
can be made as large
as |x| by setting h to 0 and moving k towards 0. It is also easy to check that this ratio
is at most |x|. Therefore, the condition number is equal to
|x| + |y| |x|
=1+ .
|y| |y|
1
By definition of condition number,
|f (xb) − f (x)| |xb − x|
= O(kf (x)) · = O(kf (x) · εmach ).
|f (x)| |x|
Therefore the above can be written as
|y − f (xb)| |f (xb)|
· + O(kf (x) · εmach ) ≤ O(εmach ) · (1 + O(kf (x) · εmach )) + O(kf (x) · εmach )
|f (xb)| |f (x)|
= O((kf (x) + 1) · εmach )).
(ii) (5 marks) Consider the function f (x) = 1 + x. Show that if we compute this
function using this expression, then the algorithm may not be backward stable,
but is always stable.
Soln: The number x gets rounded to x′ , where x′ = x(1 + ε1 ) for some ε1 , |ε1 | ≤
εmach . And the final output is
2
This can be easily shown to be stable. Indeed, consider xb = x. Then the computed
value y = sin(x)(1 + ε), and
|y − f (x)|
≤ εmach .
|f (x)|
ex −1
3. Consider the function f (x) = x
.
of the exponential and the logarithm function have relative error at most εmach .
Prove that this algorithm is backward stable or stable.
Soln: Again (all epsilons have absolute value at most εmach ). The computed value
of ex is ex (1 + ε1 ). Therefore, the computed value of f (x) is
ex (1 + ε1 ) − 1
· (1 + ε3 ),
ln(ex (1 + ε1 ))(1 + ε2 )
where ε2 appears because of ln and ε3 appears because of division.
Now we set xb = x and let y be the expression above. Let z denote the intermediate
ex (1+ε1 )−1 |y−f (x)|
expression ln(e x (1+ε )) . Now, we want to show that
1 |f (x)|
= O(εmach ), i.e., y =
u−1
f (x)(1 + ε) for some ε, |ε| ≤ εmach . Define a function g(u) = ln(u) . Then f (x) =
g(e ) and z = g(e (1 + ε1 )). Now we want to argue that g(e ) and g(ex (1 + ε1 ))
x x x
3
are close. For this we find the condition number of g at u ≈ 1 (since ex ≈ 1 when
x ≈ 0). By definition of condition number,
kg (y) = |g ′ (y)y|/|g(y)|.
ex (1 + ε1 ) − 1 z(1 + ε3 )
y = · (1 + ε 3 ) = = z(1 + O(εmach ))
ln(ex (1 + ε1 ))(1 + ε2 ) 1 + ε2
= g(a(1 + ε1 ))(1 + O(εmach )) = g(a)(1 + O(εmach ))(1 + O(εmach ))
= f (x)(1 + O(εmach )),
1 + x2
kf (x) = .
1 − x2
Therefore the problem is ill-conditioned when x ≈ ±1.
(b) (3 marks) Suppose we compute this function using the expression above. Show
that the algorithm is unstable for values close to 0.
Soln: Note that the function is well-condition for x ≈ 0. We now show that the
computation has high relative error, and hence, must be unstable. First observe
that f (x) ≈ 0 for x ≈ 0. The computed value can be written as (assume that x
can be represented exactly, even then this computation is unstable)
1 + ε1 1 + ε2 ε1 1 + ε2
y= − = f (x) + − .
1−x 1+x 1−x 1+x
For x ≈ 0,
ε1 1 + ε2
− ≈ ε1 (1 + x) − ε2 (1 + x).
1−x 1+x
The above can be close to εmach . Thus, y could be f (x) + εmach . Since f (x) can be
very close to 0, this is high relative error.
4
2x
(c) (5 marks) Suppose we compute the function as f (x) = 1−x 2 . Show that this
2x(1 + ε1 )
y= .
1 − x2 (1 + ε2 )
√
Define xb = x 1 + ε2 ≈ x(1 + ε2 /2). Clearly |bx|x| −x|
= O(εmach ). Now we need to
show that y and f (xb) are close (i.e., y = f (xb)(1 + O(εmach ))). Observe that
2xb(1 + ε3 )
y= = z(1 + ε3 ),
1 − xb2
and so this shows the desired result.
for any integer k > 1. A small perturbation of them, the pib numbers, pk , are defined
by p0 = 1, p1 = 1 and
(a) (5 marks) Plot the numbers fn and pn together in one log scale plot. On the
plot, mark 1/ϵmach for single and double precision arithmetic.
Plots are shown. The interesting thing to note is that both grow exponentially at
about the same rate.
(b) (4 marks) Rewrite (1) to express fk−1 in terms of fk and fk+1 . Use the computed
fn and fn−1 to recompute fk for k = n−2, n−3, . . . , 0. Make a plot of the difference
between the original f0 = 1 and the recomputed f0 as a function of n. What n
5
values result in low accuracy for the recomputed f0 ? How do the results in single
and double precision differ?
In single precision, machine precision can store up to 16 digits. The f (n) values
reach 1016 around n = 35. Since computation involves integer addition, this
means that there would not be any error in computation till n = 35. After that
digits will start getting chopped and errors will grow exponentially. As the plot
shows, till n = 35, there is no cancellation error. Similarly, for double precision,
there are roughly 35 digits, and f (n) reaches 1035 around n = 75. Hence, till
f (75) or so, there are no errors.
(c) (6 marks) Repeat part (b) for the pib numbers. Comment on the striking dif-
ference in the way precision is lost in the two cases. Explain the results.
In the computation of p(n), we have rounding errors from the very beginning.
This is because the value c is not stored precisely. Since errors double at every
step, it roughly means that one would lose one digit of accuracy after every step.
Since single precision corresponds to about 15 digits, we see high error around
n = 15. Similarly for double precision, one has about 35 digits of accuracy, and
so one starts seeing high errors around n = 35.
6. Write a program to generate the first n terms in the sequence given by the difference
equation:
xk+1 = 111 − (1130 − 3000/xk−1 )/xk ,
6
with starting values x1 = 11/2, x2 = 61/11 (2 marks). Use n = 10 for single precision.
(i) (2 marks) The exact solution is monotonically converging to 6, but what do you
observe?
After about n = 8, the values start deviating from 5. Roughly the reason is as
follows: each computation of xn requires two divisions by xk values, which may
lead to a loss of about 2 digits in precision. Since single precision is about 15
digits, it implies accuracy till about n = 7 only.
(ii) In order to explain the results, consider the function F : ℜ2 → ℜ2 as
" #! " #
x 111 − (1130 − 3000/y)/x
F = .
y x
" # " #!
xk+1 xk
The recurrence above can be expressed as = F . A vector
xk xk−1
z is a fixed point of F if F (z) = z. What are the possible fixed points of this
function? (3 marks)
We see that if (z, u) is a fixed point then u = z and so, u2 = 111u − (1130u −
3000/u). Solving this, we see that the solutions are u = 5, 6, 100.
(iii) (5 marks) To understand
" # the behaviour of the above recurrence, give " 2D# plots
xk 5
of how the vector evolves as we start from points close to . Use
xk−1 5
high precision here so that rounding errors do not affect your conclusion.
As can be seen from the figure, starting from points close to (5, 5), the recurrence
leads to a trajectory which always goes away from (5, 5).