File Exchange

## Error function of complex numbers

version 1.0 (59.4 KB) by

Extend the error function to the complex plane.

Updated

This package contains two MATLAB functions e=ERF(r) and e=ERFZ(z)} as MEX-files for Windows. ERF overloads the default MATLAB error function of real-valued numbers with a much faster implementation. ERFZ enhances ERF to evaluate the error function of complex numbers too. If called with real numbers, it is identical to ERF and equally fast. ERFZ can replace ERF if no error message is required when called with complex numbers. For compatibility with operating systems other than Windows on x86 processors, ERFZ is egally implemented as a normal M-file, which relies upon the default ERF by MATLAB.

Implementation details are found in the attached PDF manual.

junye li

### junye li (view profile)

While double(erf(sym(1+1i))) and erfz(1+1i) give the same result, Per's function, erfi(1+1i), gives a different answer. Why?

Karan Gill

### Karan Gill (view profile)

An alternative is to use the Symbolic Math Toolbox if you have it. Ex:

>> double(erf(sym(1+1i)))
ans =
1.3162 + 0.1905i

You could define an anonymous function to make it easier:

>> erfCmplx = @(x) double(erf(sym(x)))
erfCmplx =
@(x)double(erf(sym(x)))
>> erfCmplx(1+1i)
ans =
1.3162 + 0.1905i

Steven G. Johnson

### Steven G. Johnson (view profile)

Although this implementation is very good (and is competitive with Per's code in performance on my machine), note that it computes real(erf(z)) inaccurately near the imaginary z axis. e.g. real(erfz(1e-8 + 1i)) gives 3.1023...e-8, but the correct answer is 3.067...e-8 according to Mathematica.

See http://ab-initio.mit.edu/Faddeeva for an alternative (free/open-source) function that is a compiled MEX plugin (hence running several times faster than this code) which achieves around 13 digits of accuracy or more in both the real and imaginary parts in my tests.

Zeqë

### Zeqë (view profile)

How could I use the code if I have the R2010b version?

Felix

### Felix (view profile)

Works Great, Thank you

Bora Ung

Works well. Speedwise, this implementation seems to be on par with Godfrey's (file ID: 3574) which I currently use for small to medium range of the complex argument. It would be interesting to compare the accuracy and range of both implementations.

A Asheim

Works fine.
I use this for a wide range of parameters, then Per's implemetation is no good(NaN for large values). I have not checked why. When speed is not an issue I would recommend this(Marcels) implementation.

Per Sundqvist

Well I don't know about the speed of your routines but I guess its ok. As you maby have seen I submitted "erfi" using matlabs internal function gammainc (which runs in fortran speed). By a simple substitution one could alternatively get:

erf(z)=i*erfi(-i*z)

since my function covers the general complex case. The intermediate case for asymptotic and medium value of z, has perhaps to be improved I admitt.

##### MATLAB Release
MATLAB 6.1 (R12.1)