具有可变限制的嵌套数值积分

时间:2020-01-20 10:07:38

标签: python integration

问题的链接: 1

\ int_b ^ a f_1(x)e ^ {\ int_c ^ x f_2(y)dy} dx。

这可以通过Mathematica轻松完成,Matlab中也存在解决方案。但是我不知道如何用scipy.quad或类似函数来评估这个积分。因此,我尝试使用复合Simpson规则来实现此目标,但是在某些情况下错误很大。代码改编自Burden和Faires的数值分析。 例如,f1(x)= cosh(x)和f2(x)= 2x + sinh(x),a = 0,b = 4,c = 4。通过MMA,我得到0.7699,而使用这种数字方案,我得到0.759206。

def compSimpsInt(fun1, fun2, a, b, c):
"""
Numerical integration of the form 
\int_a^b fun1(x,y) e^{\int_c^x fun2(y)dy} dx
"""
n = len(fun1)
m = len(fun2)
h = (b - a) / n
J1 = 0
J2 = 0
J3 = 0
for i in range(n):
    x = a + (i) * h
    HX = (x - c) / m
    K1 = fun2[-1] + fun2[i] # end terms. fun[-1]: at L, fun2[i] at x.
    K2 = 0  # Even terms
    K3 = 0  # Odd terms

    Q = np.interp(np.linspace(c,x,m), np.linspace(a,b,n), fun2)
    K2 = sum(Q[i] for i in range(1, m-1) if i % 2 != 0)
    K3 = sum(Q[i] for i in range(1, m-1) if i % 2 == 0)

    L = (K1 + 2 * K2 + 4 * K3) * HX / 3
    LL = np.exp(L) * fun1[i]
    if i == 0 or i == n:
        J1 += LL
    elif i % 2 == 0:
        J2 += LL
    else:
        J3 += LL
J = (J1 + 2 * J2 + 4 * J3) * h / 3
return J

如果有人可以提供帮助,无论是指出代码中的错误还是使用scipy.integrate(或任何软件包),只要可以近似集成,我将不胜感激。

0 个答案:

没有答案
相关问题