使用scipy.integrate.dblquad的二重积分

-1 投票
1 回答
4062 浏览
提问于 2025-04-18 05:09

我正在尝试使用scipy.integrate.dblquad来进行双重积分。下面是我的代码:

    from scipy.integrate import dblquad
    import numpy as np
    def integrand(x, y, a, b):
    return a*x**2 + b*y**3
    def low_y(x):
        0
    def up_y(x):
        1-2*x
    a = 1.0
    b = 1.0
    area = dblquad(integrand, 0, np.Inf, low_y, up_y, args=(a,b), epsabs=1.49e-08, epsrel=1.49e-08)
    print area

但是我遇到了一个错误,错误信息是TypeError: a float is required,这个错误指向了代码的倒数第二行。请问有什么解决办法吗?

1 个回答

1

当我们遇到程序问题时,应该总是测试程序的每一部分。在这个例子中,运行 low_y(1.0)up_y(1.0) 会发生什么呢?应该发生什么?我们能看到错误吗?

对于这种简单的边界函数,你可能会想用 lambda 函数来代替,不过这主要是个人喜好。在这里,你可以把你的积分写成

area = dblquad(integrand, 0, np.Inf, lambda x : 0, lambda x : 1-2*x, args=(a,b), epsabs=1.49e-08, epsrel=1.49e-08)

注意:这可能只是一个测试案例,但这个函数在积分到无穷大时并没有有限的体积!

撰写回答