MATLAB: How to solve an equation with term beyond realmax

large numbers calculation

I run some equation which include exponent term multiplied by erfc term:
A=exp(B)*erfc(C)
For certain values the exponent term returns Inf and I can't solve the equation. Since the erfc term tend to zero I can't do log for the equation because the log(erfc) term will result in -Inf.
How can I overcome this problem and solve the equation?
Thanks,

Best Answer

The following is a somewhat improved version of my previous answer. It guarantees that you will not get a NaN for the product of exp(B)*erfc(C), (though of course you can get an infinity or a zero if the actual product is overflowed or underflowed.) For C > 25.54 a seven term asymptotic expansion is used in place of erfc(C) except that the exp(-C^2) part is omitted. Otherwise erfc(C) is computed. Then the needed corrective multiplicative factor, either exp(B) or exp(B-C^2), is broken up into n factors if necessary.
Let B and C be defined.
if C <= 26.54 % Use erfc
P = erfc(C);
B2 = B;
else % Use asymptotic expansion
P = 1;
for k = 11:-2:1 % This generates seven terms
P = 1-k/2/C^2*P;
end
P = P/sqrt(pi)/C;
B2 = B-C^2;
end
n = max(ceil(B2/log(.99*realmax)),1);
for k = 1:n % If necessary make correction in n separate factors
P = P*exp(B2/n);
end
The value of P is the required product of exp(B)*erfc(C).
Related Question