I have make a program in Matlab that calculate $e^{-5}$ using Taylor's series. I have calculated it with two ways first way using the first 10 terms for $x = -5$
$$
e^{-5} = 1 - 5/1! + 5^2/2! - 5^3/3! + ...
$$
and second way using the first 10 terms for $x = 5$
$$
e^5 = 1 + 5/1! + 5^2/2! + 5^3/3! + ...
$$
and then divide the result with $1$ to get $e^{-5}$. I noticed that second way gives more accurate answer than first one. So I would like to ask is it because of the truncation error added from Taylor's series because I use only the first $10$ terms or is it because of the subtraction error added from the floating point system?
Taylor's series error or floating point error?
-
0alternating series can be numerically instabil ! – 2017-01-14
-
3The first term you truncated, $5^{11}/11!$, is still $>1$. So it is a bit early to truncate the series at that point, if you expect an error $<1$. But, the size of that term is already relatively small in comparison to $e^5\approx 148$. However, it is HUUUUUGE in comparison to $e^{-5}=1/e^5\approx0.0067$. I tend to think that the inaccuracies of the floating point arithmetic are irrelevant at this point of the calculation. – 2017-01-14
-
0@JyrkiLahtonen thank you for explaining that, so basically there is no need to think about floating point errors unless I have enough terms let's say something like 100 terms? – 2017-01-14
-
0That depends on the size of those terms (in comparison to the sum and to each other). You can probably get floating point errors with a handful of terms and a bit of bad luck. – 2017-01-14
-
3If you really wanted to compute some exact value from the first 10 terms of the exponential series, you would use something like $5-7·\ln(2)=0.1479697360803831$ or $5/32=0.15625$ to get small arguments to evaluate the series in, and then reconstruct the wanted result from them as $e^5=2^7·e^{0.1479697360803831}$ resp. $e^5=(e^{0.15625})^{2^5}$. – 2017-01-14
1 Answers
The largest term in the series for $e^x$ has index $n$ or $n+1$ where $n≤|x|≤n+1$ with value about $\frac{n^n}{n!}∼e^n$ (by Stirling's approximation). For negative arguments, these alternating large numbers have to cancel to get a very small result, you would need many digits of precision in the internal number format to capture both the large and the small numbers with enough remaining digits to get the result in the desired output precision.
Or put another way, if $μ$ ($=2^{-23}$ or $=2^{-53}$) is the machine constant of the floating point number type, the series evaluation of $e^x$ (up to some kind of numerical convergence at a number of $4(1+|x|)+|\log_4(\mu)|$ or so terms) has random floating point error contributions up to size $e^{|x|}μ$, which can be bad for medium to large negative arguments $x$, as the relative error would be of size up to $e^{2|x|}μ$.
Miraculously the errors cancel largely in the computation of $e^{-5}$ giving only an error of -8.29e-16 in the 40 term partial sum. Computation of $1/e^5$ by the same number of terms gives no difference to the FPU result.