What machine computational phenomena is responsible for errors in plots of certain functions at very small scales?

31 Views Asked by At

Consider the function $f(x)=\frac{e^{x^2}-1}{x^2}$. One can use elementary methods (e.g. l'Hospital's rule) to show that $\displaystyle\lim_{x\to 0} f(x)=1$. A plot over $[-1,1]$ is consistent with this.

plot of $f(x)=\frac{e^{x^2}-1}{x^2}$ over $[-1,1]$

However, if one plot this on the interval $[-10^{-3},10^{-3}]$ we get error:

plot of $f(x)=\frac{e^{x^2}-1}{x^2}$ over $[-10^{-3},10^{-3}]$

I have a loose understanding that this is due to floating point arithmetic details, but I am hoping someone can give me some sharper insight into this phenomena.

Thank you in advance!

1

There are 1 best solutions below

2
On BEST ANSWER

Roundoff error with catastrophic cancellation.

To see what's happening, let's suppose your computer did floating-point arithmetic with $10$ decimal digits. Any number that doesn't fit in $10$ digits is rounded to the nearest number that does. Let's see what happens when you compute $$ \frac{e^{x^2}-1}{x^2}$$ with $x = 1/(3 \cdot 10^4)$. In floating-point, $x$ is represented as

3.333333333e-5

(note the $10$ digits before the exponent). It's not exact, but it's the best that can be done with $10$ digits. Computing $x^2$ is no problem: it is

1.111111111e-9

Again, this is the best that can be done with $10$ digits. Next we take the exponential:

 1.000000001

Still the best that can be done with $10$ digits. But notice how close that is to $1$. Next we subtract $1$:

.000000001 = 1.000000000e-9

Those 0's at the end are just "filler": the initial 1 is the only digit that came from our approximation of $x^2$. The actual value of $\exp(x^2)-1$, rounded to $10$ digits precision, would have been $1.111111112e-9$. At this point the difference between the two (the "absolute error") is $1.11111112 \times 10^{-10}$. Not so bad, you might say, considering we're using $10$ digits. But the "relative error", the ratio of this absolute error to the actual value, is not so good: approximately $0.1$. And when we divide our approximation of $\exp(x^2)-1$ by our approximation of $x^2$ (which is a small number), we get

9.000000001e-1

The absolute error now has been magnified to about $10^{-1}$.

If you took an even smaller $x$, things would get even worse: the computer would not be able to tell the difference between $e^{x^2}$ and $1$, resulting in a value of $0$.