Python 是否可以使用scipy.integrate.fixed_quad计算二重积分?
我试图计算一个二重积分(在一个节点位于(0,0)、(0,1)、(1,0)的三角形上),使用阶数为Python 是否可以使用scipy.integrate.fixed_quad计算二重积分?,python,scipy,numerical-integration,Python,Scipy,Numerical Integration,我试图计算一个二重积分(在一个节点位于(0,0)、(0,1)、(1,0)的三角形上),使用阶数为n的高斯求积。然而,运行 import scipy.integrate as integrate f = lambda x,y: x+y inside = lambda x: integrate.fixed_quad(f, 0, 1-x, args=(x,), n=5)[0] outside = integrate.fixed_quad(inside, 0, 1, n=5) 给予 回溯(最近一次呼叫
n的高斯求积。然而,运行
import scipy.integrate as integrate
f = lambda x,y: x+y
inside = lambda x: integrate.fixed_quad(f, 0, 1-x, args=(x,), n=5)[0]
outside = integrate.fixed_quad(inside, 0, 1, n=5)
给予
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/Users/username/anaconda/lib/python3.5/site packages/scipy/integrate/quadrature.py”,第82行,固定格式
返回值(b-a)/2.0*np.和(w*func(y,*args),轴=0),无
文件“”,第1行,在
文件“/Users/username/anaconda/lib/python3.5/site packages/scipy/integrate/quadrature.py”,第78行,固定格式
如果np.isinf(a)或np.isinf(b):
ValueError:包含多个元素的数组的真值不明确。使用a.any()或a.all()
这是问题的第二部分。在某些条件下,您的问题的答案是肯定的
出于演示目的,我首先选择与您不同的边界(11
,而不是1-x
)
通常,可以使用dblquad
求解这些类型的积分:
area_dblquad = integrate.dblquad(lambda x, y: x + y, 0, 1, lambda x: 0, lambda x: 11)[0]
def integrand(x, y):
return x + y
def fint_quad(x):
return integrate.quad(integrand, 0, 11, args=(x, ))[0]
def fint_fixed_quad(x):
return integrate.fixed_quad(integrand, 0, 11, args=(x, ), n=5)[0]
res_quad = integrate.quad(lambda x: fint_quad(x), 0, 1)
res_fixed_quad = integrate.fixed_quad(lambda x: fint_fixed_quad(x), 0, 1, n=5)
area_dblquad = integrate.dblquad(lambda x, y: x + y, 0, 1, lambda x: 0, lambda x: 1 - x)[0]
res_quad = integrate.quad(lambda x: fint_quad(x), 0, 1)
在这里返回66
。这不是你在评论中提到的选项
现在可以逐步进行此集成,它可以很好地用于quad
以及fixed\u quad
:
area_dblquad = integrate.dblquad(lambda x, y: x + y, 0, 1, lambda x: 0, lambda x: 11)[0]
def integrand(x, y):
return x + y
def fint_quad(x):
return integrate.quad(integrand, 0, 11, args=(x, ))[0]
def fint_fixed_quad(x):
return integrate.fixed_quad(integrand, 0, 11, args=(x, ), n=5)[0]
res_quad = integrate.quad(lambda x: fint_quad(x), 0, 1)
res_fixed_quad = integrate.fixed_quad(lambda x: fint_fixed_quad(x), 0, 1, n=5)
area_dblquad = integrate.dblquad(lambda x, y: x + y, 0, 1, lambda x: 0, lambda x: 1 - x)[0]
res_quad = integrate.quad(lambda x: fint_quad(x), 0, 1)
正如预期的那样,它们都返回66
。这表明它可以使用scipy.integrate.fixed\u quad
计算二重积分
但是,当您现在将上限更改回原来的上限时(从11
更改为1-x
),它仍然适用于quad
,但在fixed\u quad
时崩溃:
area_dblquad = integrate.dblquad(lambda x, y: x + y, 0, 1, lambda x: 0, lambda x: 11)[0]
def integrand(x, y):
return x + y
def fint_quad(x):
return integrate.quad(integrand, 0, 11, args=(x, ))[0]
def fint_fixed_quad(x):
return integrate.fixed_quad(integrand, 0, 11, args=(x, ), n=5)[0]
res_quad = integrate.quad(lambda x: fint_quad(x), 0, 1)
res_fixed_quad = integrate.fixed_quad(lambda x: fint_fixed_quad(x), 0, 1, n=5)
area_dblquad = integrate.dblquad(lambda x, y: x + y, 0, 1, lambda x: 0, lambda x: 1 - x)[0]
res_quad = integrate.quad(lambda x: fint_quad(x), 0, 1)
两者都返回0.333333…
,使用fixed\u quad
的调用将导致您收到的错误。通过查看源代码可以了解错误:
x, w = _cached_roots_legendre(n)
x = np.real(x)
if np.isinf(a) or np.isinf(b):
raise ValueError("Gaussian quadrature is only available for "
"finite limits.")
y = (b-a)*(x+1)/2.0 + a
return (b-a)/2.0 * np.sum(w*func(y, *args), axis=-1), None
当打印a
和b
时,会得到:
a: 0
b: 1
a: 0
b: [ 0.95308992 0.76923466 0.5 0.23076534 0.04691008]
因此,对于使用1-x
的调用,b
实际上是一个包含n
元素的numpy数组,不能将数组与无穷大进行比较,这就是它崩溃的原因。无论这是一个有意的行为还是一个bug,我都无法回答;可能值得在github上打开一个问题。fixed\u quad
需要f
才能接受向量输入。结果应该是输入的映射值(例如map(f,xs)
)。记住这一点,只需确保inside
函数返回映射值,就可以开始了
导入scipy.integrate作为integrate
f=λy,x:x+y
inside=lambda xs,n:np.array([integrate.fixed_quad(f,0,1-x,args=(x,),n=n)[0]
对于np.数组中的x(xs)])
订单=5
外部=集成。固定四元组(内部,0,1,n=顺序,args=(顺序,)
另外,要注意被积函数的参数顺序。从代码arg=(x,)
判断,似乎希望沿y维进行内部积分。被积函数的第一个参数是它被积分的维数。所以它应该是λy,x(注意,这也是被积函数的形状,被积函数是dblquad所期望的)。你在寻找吗?@kazemakase我正在尝试用参数n实现二重积分,它被称为高斯正交积分阶。可以使用scipy.integrate.dblquad()
计算二重积分,但这不是我想要的。你能写下你试图求解的积分吗?@Cleb int_0^1 int_0^(x-1)f(x,y)dy dx(函数f(x,y)在三角形上与节点(0,0)、(0,1)、(1,0)的积分)使用dblquad:result=integrate.dblquad(f,0,1,lambda x:0,lambda x:1-x)[0]
这是一个NumPy数组,而不是一个列表。至于它是否是一个bug,不。修复了您集成的函数“必须接受向量输入”,而内部的没有。@user2357112:很可能是;但它是否会被打印为数组(…)
?但无论它是一个numpy数组还是一个列表,与infinity相比,它仍然会导致相同的问题。啊,非常好的发现,谢谢!我在文档中忽略了这一部分。它不会被打印为数组(…)
,只需打印就可以了。你必须执行打印(repr(…)
获取repr
而不是str
输出。