Imaginary error function, as it is defined in Mathematica erfi(z)==erf(iz)/i (z could be complex) using the incomplete gamma function in matlab: gammainc
Note that an alternate way of computing erfi, which works for complex z, is to use the Faddeeva function (http://www.mathworks.com/matlabcentral/fileexchange/38787-faddeeva-function-scaled-complex-error-function), via erfi = @(z) -i * (exp(z.^2) .* Faddeeva_w(z) - 1) ... this seems to avoid the below-mentioned problems for large arguments, and is faster.
The program does not work as stated. When using complex z (i.e., not pure real, or pure imaginary), I get the following error:
Error using ==> gammainc
Inputs must be real, full, and double or single.
Error in ==> erfi at 19
ans=~isreal(x).*(-(sqrt(-x.^2)./(x+isreal(x))).*gammainc(-x.^2,1/2))+...
This also occurs for Example 2 provided with the code. I'm assuming it's a trivial problem since the figure above seems to correspond to what I would expect (look at the erfi article on Mathworld ). In case it's relevant, I'm using Matlab 7.10.0.499 (R2010a) (64-bit).
-Good work with this function Per!!
-While preforming an optimisation using this function. A bug in MATLAB 7.10 (R2010a) was reported. The function enters an infinite loop when calling gamminc(x,a) with x<0 and abs(x)>>a. They are working to fix this in future releases.
kind regards,
Ian Gregory, Sydney.