Scipy的引用页上提供了integration的以下示例。

from scipy import integrate
N = 5
def f(t, x):
   return np.exp(-x*t) / t**N
integrate.nquad(f, [[1, np.inf],[0, np.inf]])

以下是我从IPython笔记本(在cloud.sagemath.com上)收到的错误:

我猜测cloud.sagemath.com尚未升级到最新版本的Scipy,因此缺少nquad的模块。但是,我需要的是对两个变量进行积分,因此想使用dblquad(已在云上提供)。

因此,我修改了最后一行以适合dblquad语法,如下所示:

但是它仍然弹出错误:TypeError: 'int' object is not callable。我的脚本有什么错误?我粘贴了下面的整个错误消息:
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-5-2d0c5cf05694> in <module>()
      4 def f(t, x):
      5     return np.exp(-x*t) / t**N
----> 6 integrate.dblquad(f,1, np.inf,0, np.inf)

/usr/local/sage/sage-6.2.rc0/local/lib/python2.7/site-packages/scipy/integrate/quadpack.pyc in dblquad(func, a, b, gfun, hfun, args, epsabs, epsrel)
    424
    425     """
--> 426     return quad(_infunc,a,b,(func,gfun,hfun,args),epsabs=epsabs,epsrel=epsrel)
    427
    428 def _infunc2(y,x,func,qfun,rfun,more_args):

/usr/local/sage/sage-6.2.rc0/local/lib/python2.7/site-packages/scipy/integrate/quadpack.pyc in quad(func, a, b, args, full_output, epsabs, epsrel, limit, points, weight, wvar, wopts, maxp1, limlst)
    246     if type(args) != type(()): args = (args,)
    247     if (weight is None):
--> 248         retval = _quad(func,a,b,args,full_output,epsabs,epsrel,limit,points)
    249     else:
    250         retval = _quad_weight(func,a,b,args,full_output,epsabs,epsrel,limlst,limit,maxp1,weight,wvar,wopts)

/usr/local/sage/sage-6.2.rc0/local/lib/python2.7/site-packages/scipy/integrate/quadpack.pyc in _quad(func, a, b, args, full_output, epsabs, epsrel, limit, points)
    313             return _quadpack._qagse(func,a,b,args,full_output,epsabs,epsrel,limit)
    314         else:
--> 315             return _quadpack._qagie(func,bound,infbounds,args,full_output,epsabs,epsrel,limit)
    316     else:
    317         if infbounds !=0:

/usr/local/sage/sage-6.2.rc0/local/lib/python2.7/site-packages/scipy/integrate/quadpack.pyc in _infunc(x, func, gfun, hfun, more_args)
    371
    372 def _infunc(x,func,gfun,hfun,more_args):
--> 373     a = gfun(x)
    374     b = hfun(x)
    375     myargs = (x,) + more_args

TypeError: 'int' object is not callable

编辑1:我使用来自用户WeckesserChen的输入制作了一个工作脚本。出于完整性考虑,任何将来在此问题上绊脚石的人都可以。
import numpy as np
from scipy import integrate
N=5
def f(t, x):
    return np.exp(-x*t) / t**N
R1=integrate.dblquad(f,0, np.inf,lambda x: 1, lambda x: np.inf)
print R1

最佳答案

重读docstring for dblquad 。第四个和第五个参数必须是可调用的(即函数)。您已传递0np.inf。函数是内部积分的上下边界。

an example in the tutorial

09-08 00:04