nquad得到的结果与tplquad在三重积分中得到的结果不同

huangapple go评论83阅读模式
英文:

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:

nquad得到的结果与tplquad在三重积分中得到的结果不同

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

huangapple
  • 本文由 发表于 2023年8月10日 21:33:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76876235.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定