user1237300
user1237300

Reputation: 253

problems with the erf/erfc function in matlab

I am having problems with the accuracy of Matlab's erf/erfc functions.

As we know, erf(x) is only equal to 1 if x is infinity.

However, in Matlab, I am surprised that erf(6) is already equal to 1 and 6 is not even considerably large!

erfc(x) is a bit better in that erfc(27) is non-zero, whereas erfc(28) is zero.

Is there a way to improve the numerical performance of this function? I.e., increase the range of values for erf(x) to obtain a value that is not exactly 1? (and likewise 0 for erfc?)

Upvotes: 2

Views: 1061

Answers (1)

horchler
horchler

Reputation: 18484

Your dealing with double precision floating point. erfc performs differently because values are more closely spaced around 0 than 1. You need to represent your values with a different number system if you really need more precision (it's not clear to me why you would). Try using variable precision arithmetic if you have the Symbolic Math toolbox. Try

erf(vpa(6))

which returns the symbolic value 0.99999999999999997848026328750109. You'll need to use digits as the argument gets larger. And of course if you convert the results back to floating point with double you'll lose all of the extra precision.

Upvotes: 1

Related Questions