Avoid catastrophic cancellation? but I can't see any?

2.6k Views Asked by At

enter image description here


As you can see here, the question is about part b. By using Matlab, the answer to the part a is -2.4, but by using "format long" to compute directly, the answer is -2.401923018799901, which I don't think the cancellation is catastrophic.

Bty, I tried to put rewrite it to 1/x - sqrt(1/x^2 + 5/x), while the answer is -2.4 as well.

I am also not confident with my rewriting, please show me if you have any better rewritings.

Thanks a lot.

3

There are 3 best solutions below

0
On

One way to rewrite the expression is to multiply by the conjugate:

$$\frac{1 - \sqrt{1 + 5x}}{x} \cdot \frac{1 + \sqrt{1 + 5x}}{1 + \sqrt{1 + 5x}} = \frac{1 - (1 + 5x)}{x(1 + \sqrt{1 + 5x})} = -\frac{5}{1 + \sqrt{1 + 5x}}$$

For this second expression, when $x \sim 0$ the denominator is $\sim 2$, and you get a number close to $-5/2$. In the original expression when $x \sim 0$ both the numerator and denominator are close to zero so you could get problems.

But it doesn't actually seem like you got any problems in your computation, so...?

8
On

The trick here is to avoid the subtraction cancellation through multiplying numerator and denominator by the conjugate radical:

$$ \frac{1 - \sqrt{1+5x}}{x} = \frac{1 - (1+5x)}{x(1+\sqrt{1+5x})} = \frac{-5}{1+\sqrt{1+5x}} $$

Give that a try.


Here's what happens if you evaluate the original expression in 2 decimal digit floating point precision:

$$ x = 0.034 $$

$$ 5x \approx 0.17 $$

$$ 1 + 5x \approx 1.2 $$

$$ \sqrt{1+5x} \approx 1.1 $$

$$ 1 - \sqrt{1+5x} \approx -0.1 \; \text{ NB: subtractive cancellation }$$

$$ \frac{1 - \sqrt{1+5x}}{x} \approx -2.9 $$

So there's a significant loss of precision in doing it this way.

1
On

I think the right rewriting is

$-\frac{5}{1+\sqrt{1+5x}} $

obtained by multiplication for $1+\sqrt{1+5x}$.

You usually have a cancellation with the sign minus when the result is very close to zero, so it's convenient use a rewriting where it doesn't appear