英文:
nquad giving different result than tplquad for a triple integral
问题
使用nquad
函数时,您的代码似乎没有正确地指定积分变量的范围。以下是修正后的代码:
from scipy.integrate import nquad
def inte(x, y, z):
return 2 * z
def range_1(x):
return [0, 2 * x]
def range_2(x):
return [x**3, x]
def range_3():
return [0, 1]
volume, error = nquad(inte, [range_1, range_2, range_3])
volume
在这个修正后的版本中,积分变量的顺序和范围已经正确指定,应该得到正确的积分结果。不过,请注意,nquad
的结果可能会与tplquad
的结果略有不同,因为nquad
是数值积分方法,而tplquad
是精确积分方法。
英文:
I'm trying to do this triple integral in scipy:
from scipy.integrate import dblquad, tplquad, nquad
def inte(z, y, x):
return 2*z
with tplquad
I get the right result:
volume = tplquad(inte, 0, 1, lambda x: x**3, lambda x: x, lambda x, y: 0, lambda x, y: 2*x)
volume[0]
0.33333333333333337
While this is what happens when I use nquad
:
def range_3():
return [0, 1]
def range_2(x):
return [x**3, x]
def range_1(x, y):
return [0, 2*x]
volume, error = nquad(inte, [range_1, range_2, range_3])
volume
0.2
What is wrong with my nquad
call?
答案1
得分: 1
我盯着这个问题看了一会儿,试图弄清楚,但最终我明白了。当你将函数作为`tplquad`的积分限制时,函数参数的顺序是`x, y`,但对于`nquad`来说是`y, x`。这在你查看文档时可以看到。
`tplquad`:
> **qfun**:**_函数或浮点数_**<br>
> z的下边界表面。它必须是一个接受两个浮点数(顺序为x, y)并返回一个浮点数或指示常数边界表面的函数。
`nquad`:
> **ranges**:**_可迭代对象_** <br>
> ……如果ranges的一个元素是可调用的,那么它将被调用以使用所有可用的积分参数,以及任何参数化参数。例如,如果`func = f(x0, x1, x2, t0, t1)`,那么`ranges[0]`可以被定义为`(a, b)`或者`(a, b) = range0(x1, x2, t0, t1)`。
```python
from scipy.integrate import tplquad, nquad
def integrand(z, y, x):
return 2*z
res_tplquad, _ = tplquad(integrand,
0, 1,
lambda x: x**3, lambda x: x,
0, lambda x, y: 2*x)
print(res_tplquad) # 0.33333333333333337
def range_3():
return [0, 1]
def range_2(x):
return [x**3, x]
def range_1(y, x):
return [0, 2*x]
res_nquad, _ = nquad(integrand, [range_1, range_2, range_3])
print(res_nquad) # 0.33333333333333337
英文:
I stared at this problem for a while trying to figure it out, but I finally got it. When you pass functions as the integration limits for tplquad
, the function arguments are ordered x, y
, but for nquad
it's y, x
. This can be seen when you look at the documentation.
tplquad
:
> qfun : function or float<br>
> The lower boundary surface in z. It must be a function that takes two floats in the order (x, y) and returns a float or a float indicating a constant boundary surface.
nquad
:
> ranges : iterable object <br>
> ...If an element of ranges is a callable, then it will be called with all of the integration arguments available, as well as any parametric arguments. e.g., if func = f(x0, x1, x2, t0, t1)
, then ranges[0]
may be defined as either (a, b)
or else as (a, b) = range0(x1, x2, t0, t1)
.
from scipy.integrate import tplquad, nquad
def integrand(z, y, x):
return 2*z
res_tplquad, _ = tplquad(integrand,
0, 1,
lambda x: x**3, lambda x: x,
0, lambda x, y: 2*x)
print(res_tplquad) # 0.33333333333333337
def range_3():
return [0, 1]
def range_2(x):
return [x**3, x]
def range_1(y, x):
return [0, 2*x]
res_nquad, _ = nquad(integrand, [range_1, range_2, range_3])
print(res_nquad) # 0.33333333333333337
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论