【发布时间】:2020-12-01 18:06:42
【问题描述】:
我有以下积分(我不知道为什么屏幕截图这么低,所以你需要滚动一下)。 我尝试了以下代码:
from scipy import pi, sqrt
from scipy.integrate import dblquad
func = lambda x,y: ((1-sqrt(x**2+y**2))/(sqrt(x**2+y**2)))*(x**2)
x1,x2 = -1, 1
y1,y2 = lambda x: -sqrt(1-x**2), lambda x: sqrt(1-x**2)
print (dblquad(func, x1, x2, y1, y2))
但我不断得到
The occurrence of roundoff error is detected, which prevents
the requested tolerance from being achieved. The error may be
underestimated.
**opt)
我得到的结果似乎不正确:
(nan, 1.0867783627477699e-07)
【问题讨论】:
-
我怀疑问题来自于 SciPy 在
x = 0、y = 0评估您的函数,它会给出一个nan。尝试重写函数以在这种特殊情况下返回0,例如类似func = lambda x, y: 0.0 if x == y == 0 else ((1-sqrt(x**2+y**2))/(sqrt(x**2+y**2)))*(x**2). -
@MarkDickinson 谢谢,它有效!