scipy.integrate.nquad忽略选项



我需要计算一个数值(三重(积分,但不需要很高的精度,因此在使用nquad时,我希望为速度牺牲一些精度。我想我可以通过增加epsrel和/或epsabs选项来做到这一点,但它们似乎没有效果。例如(注意,这只是一个被积函数的例子——我实际上不需要计算这个特定的积分…(:

import numpy as np
from scipy.integrate import nquad
def integrand(l, b, d, sigma=250):
x = d * np.cos(l) * np.cos(b)
y = d * np.sin(l) * np.cos(b)
z = d * np.sin(b)
return np.exp(-0.5 * z**2 / sigma**2) / np.sqrt(2*np.pi * sigma**2)
ranges = [
(0, 2*np.pi),
(0.5, np.pi/2),
(0, 1000.)
]
# No specification of `opts` - use the default epsrel and epsabs:
result1 = nquad(integrand, ranges=ranges, full_output=True)
# Set some `quad` opts:
result2 = nquad(integrand, ranges=ranges, full_output=True,
opts=dict(epsabs=1e-1, epsrel=0, limit=3))

两个输出相同:

>>> print(result1)
(4.252394424844468, 1.525272379143154e-12, {'neval': 9261})
>>> print(result2)
(4.252394424844468, 1.525272379143154e-12, {'neval': 9261})

此处包含完整的示例:https://gist.github.com/adrn/b9aa92c236df011dbcdc131aa94ed9f9

这不是正确的方法,还是scipy.integrate忽略了我输入的opts

根据scipy.integrate.nquadopts只能传递给quad,如下所示:

https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.nquad.html

应用示例:

import numpy as np
from scipy.integrate import quad
def integrand(a, sigma=250):
x = 2 * np.sin(a) * np.cos(a)
return x

# No specification of `opts` - use the default epsrel and epsabs:
result1 = quad(integrand,0, 2*np.pi)
# Set some `quad` opts:
result2 = quad(integrand,0, 4*np.pi,epsabs=1e-6, epsrel=1e-6, limit=40)

退货:

result1: (-1.3690011097614755e-16, 4.4205541621600365e-14)
result2: (-1.7062635631484713e-15, 9.096805257467047e-14)

nquad之所以不抱怨选项的存在,是因为nquad包括quaddbquadtplquad

相关内容

  • 没有找到相关文章

最新更新