Evaluating erf(x) using Taylor’s series

calculuserror functiontaylor expansion

I tried to evaluate error function using Taylor series by using its definition
$$ erf(z) = \frac{2}{\sqrt{\pi}}\int_0^ze^{-t^2}dt$$
I've used Taylor expansion to evaluate this integration and i got this
$$ \frac{2}{\sqrt{\pi}}\int_0^ze^{-t^2}dt = \frac{2}{\sqrt{\pi}}\int_0^z\sum_{k=0}^{\infty}\frac{(-1)^kt^{2k}}{k!}$$
$$ \frac{2}{\sqrt{\pi}}\sum_{k=0}^{\infty}\frac{(-1)^kt^{2k+1}}{(2k+1)k!}\Bigg|_{t=0}^{t=z} $$
I've used wolfram alpha to evaluate this summation on certain values but the series diverges when getting partial sum the value becomes bigger while calculating more terms but when evaluating infinity series wolfram automatically uses built-in error function to get value why do I get huge values when calculating partial sums but it converges when calculating infinity series ?

Best Answer

The ratio test or similar proves the series converges for all $x$. However a brief inspection of each term should convince you that for a large $x$ the terms begin by increasing and only for larger $n$ will they start approaching zero.

If you define $a_n$

$$ a_n = \dfrac{2}{\sqrt \pi} \dfrac{(-1)^{n}}{(2n+1) \cdot n!} $$

so that the series becomes $ \text {erf}(z) = \sum_n a_n z^{2n+1} $, you can calculate

$$ \frac{a_{n+1}}{a_n} = -\frac{(2n+1) z^2 }{(2n+3) (n+1) } $$

which plainly has limit zero as $n \to \infty$ but does not start to decrease in magnitude until $ (2n+3)(n+1)/(2n+1) > |z|^2 $.

This makes the series particularly inappropriate for calculating $\text{erf}(z)$ for large $z$. In computer floating point arithmetic the large terms will dominate the sum early on and fail to cancel so that all accuracy is lost. The sum will eventually converge (if individual terms do not exceed the large number limit and overflow) but the apparent limit will be useless.

If you consult Wikipedia or similar references you can find alternative formulae, particularly those for the complementary error function $\text{erfc} (x) = 1 - \text{erf} (x) $, that converge effectively for large arguments and allow accurate calculation of the function.