[英]overflow encountered: multipliation of exp() times erfc()
I want to evaluate the following formula:我想评估以下公式:
c = exp{x}*erfc{y}
(see definition of x
and y
in the code below.) (请参阅下面代码中x
和y
定义。)
The problem is that x and y are getting quite large and I get very large values for exp{x}
and very small values for erfc(y)
.问题是 x 和 y 变得非常大,我得到了非常大的exp{x}
值和非常小的erfc(y)
值。
import numpy as np
import scipy as sci
k = 5.7e-3
D = 1.53e-8
R = 1.5e-5
r = 1e-6
t = np.linspace(0.0,12,10)
x = (r/R) + (D/(R*R) - k)*t
y = (r/(2*np.sqrt(D*t))) + np.sqrt(D*t)/R
exp_x = np.exp(x)
erfc_y = sci.special.erfc(y)
print("x = \n{} ".format(x))
print("y = \n{}".format(y))
print("exp(x) = \n{}".format(exp_x))
print("erfc(y) = \n{}".format(erfc_y))
print("exp(x) * erfc(y)= \n{}".format(exp_x*erfc_y))
My Idea was to change the evaluation to我的想法是将评估更改为
log{exp(x)*erfc(y)} = log{exp(x)} + log{erfc(y)} = x + log{erfc(y)}
afterward, I can calculate之后,我可以计算
exp(x + log{erfc(y)})
But here is the problem: When I want to calculate但问题是:当我想计算时
log{erfc(y)} = log{1 - erfc(y)}
I get a similar problem that erfc will be close to 1 and I will get precision problems.我遇到了一个类似的问题,即 erfc 将接近 1,并且我会遇到精度问题。
Any ideas to solve my problem?有什么想法可以解决我的问题吗?
The solution of this problem was given by Cadena, in this article: Cadena给出了这个问题的解决方案,在这篇文章中:
Cadena, F. (1989). Cadena, F. (1989)。 Numerical approach to solution of pollutant transport models using personal computers.使用个人计算机求解污染物迁移模型的数值方法。 Computers in Education, 9(2), 34-6.教育中的计算机, 9(2),34-6。
I could not obtain the article, but the solution is also given by我无法获得该文章,但解决方案也由
Lin, JS, & Hildemann, LM (1995). Lin, JS, & Hildemann, LM (1995)。 A nonsteady-state analytical model to predict gaseous emissions of volatile organic compounds from landfills.一种预测垃圾填埋场挥发性有机化合物气体排放的非稳态分析模型。 Journal of hazardous materials , 40(3), 271-295.危险材料杂志,40(3),271-295。
Cadena proposed to approximate the complementary error function in terms of another exponential function with a negative power term. Cadena 建议根据另一个具有负幂项的指数函数来近似互补误差函数。 It reduces the high power raised by the original exponential function that causes overflow.它减少了导致溢出的原始指数函数提高的高功率。
Suppose W
is the multiplication that needs to be evaluated:假设W
是需要计算的乘法:
W=exp(y)*erfc(x)
W
can be approximated as: W
可以近似为:
W = (a1*t+a2*t^2+a3*t^3+a4*t^4+a5*t^5)*exp(y-x^2), t=1/(1+p*x), if x >= 0
W = 2*exp(y)-(a1*t+a2*t^2+a3*t^3+a4*t^4+a5*t^5)*exp(y-|x|^2), t=1/(1+p*|x|), if x < 0
where在哪里
a1 = 0.254829592
a2 = -0.284496736
a3 = 1.42141741
a4 = -1.453152027
a5 = 1.061405429
p = 0.3275911
Use scipy.special.erfcx()
which gives exp(x**2)*erfc(x)
.使用scipy.special.erfcx()
给出exp(x**2)*erfc(x)
。
Then you just implement erfcx(x)*exp(xx**2)
which again gives erfc(x)*exp(x)
.然后你只需实现erfcx(x)*exp(xx**2)
,它再次给出erfc(x)*exp(x)
。
This of course works best if your arguments x=y
.如果您的参数x=y
这当然效果最好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.