Catastrophic Cancellation in expressions

1.7k Views Asked by At

Each of the expressions below will suffer from catastrophic cancellation error for the given ranges of x. Rewrite the expressions so that catastrophic cancellation does not occur.

a) $\ln(x+1) - \ln(x)$ for x large

b) $\displaystyle{\frac{1-\cos(x)}{x^2}}$ for $x \approx 0.$

c) $\displaystyle{\sqrt[3]{1+x}-1}$ for $x \approx 0.$

2

There are 2 best solutions below

1
On BEST ANSWER

a) $\ln (1+\frac1{x})$ which should be calculated using the Taylor series for $\ln(1+t)$ for $t=\frac1{x}$, starting with the last term which is larger than the machine epsilon and working back toward the first term, which is $t$.

b) Multiply by $\frac{1+\cos x}{1+\cos x}$ to turn this into $\frac{sin^2 x}{x^2}\frac{1}{(1+\cos x)}$ . The second term is well behaved. For the first, you can use $$\frac{\sin x}{x} = 1 - \frac{x^2}{3!} + \frac{x^4}{5!} - \cdots$$ again starting from the last term larger than epsilon. I don't really like this answer, since it relies on the Taylor series for $\frac{\sin x}{x}$ and the other answers don't rely on series as long as you have the transcendental functions avialable.

c) This one is harder (although in practice you can simply use the Taylor series). I might say that $$ \sqrt[3]{1+x}-1 = \sqrt[3]{x+3\sqrt[3]{1+x} -3\sqrt[3]{(1+x)^2}}$$ but the same catastrophic cancellation problem has just been moved under the cube root sign. Instead, write $$ \sqrt[3]{1+x} -1 = \frac{x}{1+\sqrt[3]{1+x}+\sqrt[3]{(1+x)^2}} $$ which is well calculated for small $x$.

0
On

Recall that $\ln\frac ab=\ln a-\ln b$. This allows us to rewrite $$ \ln(x+1)-\ln x=\ln\frac{x+1}x=\ln\left(1+\frac1x\right)$$ For large $x$,we can use Taylor to approximate this as $$\ln\left(1+\frac1x\right)\approx\frac 1x-\frac1{2x^2}\pm\ldots$$

For the other problems, try if the Taylor expansons for $\cos x$ or $\sqrt[3]{1+x}$ can help you.