Do the results agree with theoretical expectations? Explain why.

123 Views Asked by At

Considering the function $$f(x) = \frac{(e^x -1)}{x}$$

Using L'Hospital's rule, it is shown that,$$lim_{x→0} f(x) = lim_{x→0}\frac{(e^x -1)}{x} =1$$

When checking the results, by writing a program to calculate f(x) for $x=10.0^{-k}, k =1,...15$,

Program L'HospitalApprox implicit none

integer :: k
real :: x, f, abs_error

    x = 10.0**(-k)
    f = (exp(x)-1.0)/x
    abs_error = abs(1.0 - f)

    write(*,*) k, x, f, &
                             abs_error

end do

End Program L'HospitalApprox

Results: Picture describing results

Could anyone please provide me with an in-detail explanation as to whether the results agree with theoretical expectations? While, also, explaining why?

Here are some points that I would like further clarifications on:

  1. What is seen in terms of behaviour of the error as k increases?
  2. By what rate does the error change?
  3. What do you think might be causing that error change?
2

There are 2 best solutions below

3
On BEST ANSWER

In single precision you will have $\exp(x)=1.0$ for $|x|\ll10^{-8}$. This is because, by the very same limit, you may see that $\exp(x)\approx1+x$, which rounds to $1$ if $x$ is too small.

In fact you can try this, replacing $\exp(x)$ with $1+x$ and get the same issue. See here.

This issue is due catastrophic cancellation, where subtracting close floats leads to a loss of significant figures. This happens often when dealing with difference quotients.

To avoid this issue, you should either use more precision, stop before you lose too many figures, or estimate the limit a different way (for example, symmetric difference quotients converge faster).

In your particular case, you can look at the ratio of errors you got. For the first few steps, the errors decreased by a factor of almost exactly $0.1$ and then this started increasing, eventually leading to a completely wrong answer.

3
On

Accuracy problem, I suppose.

I give you below results computed with unlimited precision $$\left( \begin{array}{cc} k & f \\ 1 & 1.0517091807564762481 \\ 2 & 1.0050167084168057542 \\ 3 & 1.0005001667083416681 \\ 4 & 1.0000500016667083342 \\ 5 & 1.0000050000166667083 \\ 6 & 1.0000005000001666667 \\ 7 & 1.0000000500000016667 \\ 8 & 1.0000000050000000167 \\ 9 & 1.0000000005000000002 \\ 10 & 1.0000000000500000000 \\ 11 & 1.0000000000050000000 \\ 12 & 1.0000000000005000000 \\ 13 & 1.0000000000000500000 \\ 14 & 1.0000000000000050000 \\ 15 & 1.0000000000000005000 \\ 16 & 1.0000000000000000500 \\ 17 & 1.0000000000000000050 \\ 18 & 1.0000000000000000005 \\ 19 & 1.0000000000000000001 \\ 20 & 1.0000000000000000000 \end{array} \right)$$ where you could notice nice patterns.