Taylor Series – How to Calculate $\exp(-x)$

approximationexponential functionMATLABtaylor expansion

We know that the Taylor series expansion of $e^x$ is
\begin{equation}
e^x = \sum\limits_{i=1}^{\infty}\frac{x^{i-1}}{(i-1)!}.
\end{equation}

If I have to use this formula to evaluate $e^{-20}$, how should I check for convergence?

MATLAB gives $e^{-20} = 2.0612e-009$. But when I use this formula to calculate it, I get $4.1736e-009$. My answer becomes more inaccurate as I take $x = -30, -40 \cdots$. The reason is obvious. This is because as the number of terms in the Taylor series increases, the factorial becomes more and more inaccurate using MATLAB.

  1. How can I use Taylor series to get accurate values of $e^x$ in MATLAB?
  2. How does MATLAB built in command "exp()" calculate the exponential?

Best Answer

The Problem

The main problem when computing $e^{-20}$ is that the terms of the series grow to $\frac{20^{20}}{20!}\approx43099804$ before getting smaller. Then the sum must cancel to be $\approx2.0611536\times10^{-9}$. In a floating point environment, this means that $16$ digits of accuracy in the sum are being thrown away due to the precision of the large numbers. This is the number of digits of accuracy of a double-precision floating point number ($53$ bits $\sim15.9$ digits).

For example, the RMS error in rounding $\frac{20^{20}}{20!}$, using double precision floating point arithmetic, would be $\sim\frac{20^{20}}{20!}\cdot2^{-53}/\sqrt3\approx3\times10^{-9}$. Since the final answer is $\approx2\times10^{-9}$, we lose all significance in the final answer simply by rounding that one term in the sum.

The problem gets worse with larger exponents. For $e^{-30}$, the terms grow to $\frac{30^{30}}{30!}\approx776207020880$ before getting smaller. Then the sum must cancel to be $\approx9.35762296884\times10^{-14}$. Here we lose $25$ digits of accuracy. For $e^{-40}$, we lose $33$ digits of accuracy.


A Solution

The usual solution is to compute $e^x$ and then use $e^{-x}=1/e^x$. When computing $e^x$, the final sum of the series is close in precision to the largest term of the series. Very little accuracy is lost.

For example, the RMS error in computing $e^{20}$ or $e^{-20}$, using double precision floating point arithmetic, would be $\sim8\times10^{-9}$; the errors are the same because both sums use the same terms, just with different signs. However, this means that using Taylor series, $$ \begin{align} e^{20\hphantom{-}}&=4.85165195409790278\times10^8\pm8\times10^{-9}\\ e^{-20}&=2\times10^{-9}\pm8\times10^{-9} \end{align} $$ Note that the computation of $e^{-20}$ is completely insignificant. On the other hand, taking the reciprocal of $e^{20}$, we get $$ e^{-20}=2.061153622438557828\times10^{-9}\pm3.4\times10^{-26} $$ which has almost $17$ digits of significance.

Related Question