An approximation for the error function

 The error function, erf(x), is defined as the following function:

\[ \operatorname{erf}( x) = {2 \over \pi} \int _0 ^x { e^{- t^2} \operatorname{d}t } \tag{1}\]

This integral has no analytical solution through any of the conventional functions. Most modern software has a function to evaluate it. However, sometimes it is convenient to have an approximation at hand. It is known that it slowly converges as a series of powers. For this reason, the way to approximate it proposed here is through a geometrically similar function.
The proposed base function is:

\[y(x) = \tanh \left({{a x}\over 2}\right) = { {2 e^{ a x }} \over { 1 + e^{ a x } }} - 1 \tag{2}\]

where \(a\) is a constant to be determined. This particular function is chosen because it satisfies the same function limits; that is to say,

\[ \lim_{x \to \infty }{\operatorname{erf}( x)} = \lim_{x \to \infty } { y (x) } = 1 \tag{3.a}\]

\[ \lim_{x \to -\infty } {\operatorname{erf}( x)} = \lim_{x \to -\infty }{ y (x) } = -1 \tag{3.b}\]

The parameter a takes its value from the slope at 0:

\[ \left . {{ \operatorname{d}[ \operatorname{erf}( x)] } \over { \operatorname{d} x }} \right \vert_{ x=0 } = \left . {{ \operatorname{d}y( x) } \over { \operatorname{d} x }} \right \vert_{ x=0 } \tag{4} \]

and, therefore:

\[ a \equiv {2 \over \sqrt{\pi} } \tag{5}\]

Figures 1 and 2 compare \(\operatorname{erf}( x)\) with \(y(x)\). In Figure 1 it can be seen that, although they have the same limits and the same slope for \(x\) = 0, there is a zone in the interval of x between 1 and 2 that has a notable deviation. This is also seen in Figure 2, which compares both functions in phase space, where a noticeable deviation appears to the naked eye for and between 0.6 and 1, although as shown they are coincident at the extremes.

Figure 1. Comparison between \(\operatorname{erf}( x)\) and the base approximation, \(y(x)\).

 

Figure 2. Comparison between \(\operatorname{erf}( x)\) and \(y(x)\) in phase space.

There are several ways to solve this inconvenience. I will explore two of them: a series expansion and an empirical correction:

Series expansion

 It is well known that the series of \(\operatorname{erf}( x)\) converges slowly:

\[\operatorname{erf}( x) = {2 \over \sqrt{\pi}}\sum_k^\infty { {(-1)^k x^{2k+1}} \over { k! (2 k+1)}} \approx {2 \over \sqrt{\pi}} \left(x - {x^3 \over 3} + {x^5 \over 10} - {x^7 \over 21}+ ... \right) \tag{6} \]

Now, what happens if we expand  \(\operatorname{erf}( x)\) as a series of \(y(x)\)? To do so, we first find the inverse of y:

\[X ={2 \over a} \operatorname{arctanh} y = {1 \over a} \ln \left({y + 1}\over{y - 1} \right) \tag{7} \]

The result is a complex series which can't be resumed:

\[\operatorname{erf}(X(y)) \approx y + \left({1 \over 3} - {\pi \over 12} \right) y^3 + \left({1 \over 5} - {{\pi} \over 12} + {\pi^2 \over 160} \right) y^5 + \left({1 \over 7} - {{7 \pi}\over 90} + {\pi^2 \over 96} - {\pi^3 \over 2688}\right) y^7 + ... \tag{8} \]

Moreover, with this series expansion we have lost the limits of \(x \to ±\infty\), as seen in Fig. 3, where we have plotted the terms up to the 11th power. There can be seen that, even if this approach is not capable of diverging, it ends in a wong limit because of the «-1» term in Eq. (2). Therefore, although an atractive alternative, a series expansion in \(y(x)\) is not useful.

Figure 3. Comparison between \(\operatorname{erf}(x)\), a series expansion of order 11 in \(x\), (Eq. 6) and an expansion of the same order in \(y(x)\) (Eq. (8)).

Empirical correction

To solve this error, the base function will be affected by a correction factor of the same base. Firstly, Fig. 4 depicts the difference between Eqs (1) and (2) (blue). There it seems clear that the difference is an odd function with null derivative at \(x = 0\). If we divide this difference by \(y^3\), the result is a function that is quite similar to a even polynomy of degree larger than 2. Moreover, for \(y = 0\), it goes to the second coefficient in Eq. (8).

Figure 4. Difference between \(\operatorname{erf} x\) and \(y\).

Therfore, I define

\[b={1 \over 3}- {\pi \over 12} \tag{9} \]

Now, Fig. 5 shows the search of an even polynome of the type \(1 - y^{2n}\). There it is clear that, with n = 6 we obtain a good approximation with an almost constant value, though it oscilates when approaching \(|y| = 1\).

Figure 5. Search of a last empirical correction for \(y(x)\).

By trial and error, the following approximation has been reached:

\[ {\operatorname{erf}( x)} \approx {y(x)} \left[ 1+ b {y(x)}^2 (1 - {y(x)}^{12} )\right] \tag{10}\]

Eq. (10) makes sure to correct the shape of the approximation satisfying Eqs. (3). The proposed function has a maximum error of 0.0024 for \(|x|\) = 0.94, as can be seen in Figure 3.
The procedure for using the approximate function is not to use a large function of \(x\), but to first evaluate \(y (x)\) from (2) and then to evaluate the approximation from (6). 

Strictly speaking, the correction factor \(1 - y^{12}\) just restores the limiting cases of \(|y| = 1\). Therefore, it may be used for any cut off limit of Eq. (8). In Eq. (9), it has been cut at the 3rd power, with a maximum error of 0.0024. If lower errors are desired, a larger power could be selected.

Figure 6. Error between \(\operatorname{erf}(x)\), the proposed base function (Eq. 2) and the complete approximation function (Eq. 6).

Conclusion

I just provide a rather simple function to evaluate the error fu by hand or using a calculator. Sadly, the function is not analyticaly solvable for the inverse. Nonetheless, it is the root of a polynomy, which is not difficult to evaluate.


Comentarios

Entradas populares de este blog

Antoine constant's conversion

An empiric profile for a turbulent flow in a circular pipe