从 scipyintegratequad 返回的大数组

问题描述 投票:0回答:1

大家。我试图使用 scipy.quad 计算积分。但是,它返回一个非常大的数组,没有其他错误。任何人都可以看到问题吗?该代码可以很好地使用简单的积分,例如 四边形(lambda x:x**2,0,4) 但对于下面的代码 尝试 a=20,b=20,c=20,j=1 你会得到一个包含重复数字的非常大的结果列表

def Lj(a,b,c,j):
    R1=a
    R2=b
    R3=c
    if j==1:
        Rj=a
    elif j==2:
        Rj=b
    elif j==3:
        Rj=c
    else:
        raise ValueError('j needs to be an integer between 1 and 3')

    result = (R1*R2*R3)*quad(lambda s: 1/((s+Rj**2)*sqrt(((s+R1**2)*(s+R2**2)*(s+R3**2))))/2, 0, inf)
    return result

enter image description here

python scipy numerical-integration
1个回答
3
投票

quad
返回包含积分和误差估计的元组。您将此元组乘以
R1*R2*R3
,这会复制该元组
R1*R2*R3
次。

要解决此问题,请仅使用

quad
返回的第一个值。也就是说,替换这个:

result = (R1*R2*R3)*quad(lambda s: 1/((s+Rj**2)*sqrt(((s+R1**2)*(s+R2**2)*(s+R3**2))))/2, 0, inf)

像这样:

intgrl, err = quad(lambda s: 1/((s+Rj**2)*sqrt(((s+R1**2)*(s+R2**2)*(s+R3**2))))/2, 0, inf)
result = (R1*R2*R3)*intgrl
© www.soinside.com 2019 - 2024. All rights reserved.