# Evaluating erf(x) using Taylor's series

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 ?

• See this post: math.stackexchange.com/questions/125328/… May 28, 2020 at 6:38
• @AntonVrdoljak I got already the expansion I'm asking about why the summation seems to diverge when the number of terms not infinity, but converges when terms are infinity May 28, 2020 at 6:46

## 2 Answers

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.

Since @WA Don already gave a good answer, let us see what happens from a numerical point view.

Since it is an alternating series, you want to know $$k$$ such that

$$\frac{z^{2k+1}}{(2k+1)\,k!} < 10^{-n}\implies (2k+1)\, k!>z^{2k+1}\,10^n$$ Approximating $$(2k+1)\, k!$$ by $$(k+1)!$$ we then face the problem of finding the zero of $$(k+1)! = (z^2)^{k+1}\,\frac{10^n} z$$

In this question of mine, @robjohn proposed a magnificent approximation of $$n$$ solution of $$n!=a^n\, 10^k$$. Adapted to your problem, this would give

$$k \sim e z^2 \exp\left(W\left(\frac{\log \left(\frac{10^{2 n}}{2 \pi }\right)}{2 e z^2}\right) \right) -\frac 32$$

Using $$n=6$$ and $$z=10$$ would give $$k=282.937$$ that is to say $$k=283$$ ! In fact the solution of the very first equation is $$k=277.826$$.

I hope that this explains at least part of the problem.

Edit

Using the approximation I had in the question, the solution would have been $$k= \frac t {2 W(t)} \qquad \text{where} \qquad t=\log \left(\frac{z^2\, 10^{2 n}}{8 \pi }\right)$$ Applied to the working case, this would give $$k=285.972$$.