如何使用dblquad进行双重集成?

时间:2014-05-06 19:03:07

标签: python numpy scipy ipython quad

以下示例在Scipy的{​​{1}}参考页面上提供。

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]]) 笔记本(在cloud.sagemath.com上)获得的错误: enter image description here

我猜我IPython尚未升级到cloud.sagemath.com的最新版本,因此它缺少Scipy的模块。但是,我只需要集成两个变量,因此希望使用已在云上提供的nquad

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

但它仍会弹出错误:dblquad。我的脚本中有什么错误?我已粘贴下面的整个错误消息:

TypeError: 'int' object is not callable

编辑1:我使用来自用户--------------------------------------------------------------------------- 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 Weckesser的输入制作了一个工作脚本。只是为了对任何在将来绊倒这个问题的人的完整性。

Chen

2 个答案:

答案 0 :(得分:5)

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

an example in the tutorial

答案 1 :(得分:4)

dblquad的签名是

integrate.dblquad(func, a, b, gfun, hfun, args=(), epsabs=1.49e-08, epsrel=1.49e-08)

gfun:callable

y中的下边界曲线,它是一个单一的函数 浮点参数(x)并返回浮点结果:a lambda函数在这里很有用。

hfun:callable

y中的上边界曲线(与gfun相同的要求)。

所以你需要将gfun和hfun替换为函数。