# Return to Revisions

2 of 2 slight optimization

In the interest of showing that there's more than one way to skin a cat, I present a method suitable for large positive arguments, due to Chiarella and Reichel. The method uses the approximation

$$\exp(z^2)\mathrm{erfc}(z)\approx\frac{hz}{\pi}\left(z^{-2}+2\sum_{k \geq 1}\frac{\exp(-h^2 k^2)}{z^2+h^2 k^2}\right)$$

where $$h$$ is a suitably chosen parameter, based on the precision needed.

f[z_?InexactNumberQ] :=
Module[{prec = Precision[z], y = z^2, e, h, j, k, s, t},
h = Pi/Sqrt[(Round[prec] + 1) Log[10]]; e = h^2;
s = 0; j = k = 1;
While[True,
t = Exp[-e k]/(y + e k);
s += t;
If[Abs[t] <= Abs[s] 10^-prec, Break[]];
j += 2; k += j];
h z (1/y + 2 s)/Pi] /; TrueQ[Quiet[z > 0]]


Again, this works best for large positive $$z$$, which seems to be the arguments of interest for the OP anyway. If evaluation for small $$z$$ is needed, a correction term has to be added to the Chiarella-Reichel approximation; see their paper for details.

 answered May 11 '12 at 19:59 J. M. will be back soon♦ 104k1111 gold badges322322 silver badges485485 bronze badges