Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use dblquad for double integration?

The following example is provided on Scipy's reference page for 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]])

The following is the error I get from my IPython notebook (on cloud.sagemath.com): enter image description here

It is my guess that cloud.sagemath.com has not upgraded to the latest version of Scipy and therefore it lacks the module for nquad. However, all I need is integration over two variables and therefore wanted to use dblquad which is already available on the cloud.

Therefore, I modified the last line to suit the dblquad syntax as shown below: enter image description here

But it still pops up the error: TypeError: 'int' object is not callable. What is the error in my script? I have pasted the entire error message below:

---------------------------------------------------------------------------
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

Edit 1: I made a working script using the inputs from the users Weckesser and Chen. Just for the sake of completeness for anyone stumbling upon this question in the future.

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
like image 500
Shashank Sawant Avatar asked May 06 '14 19:05

Shashank Sawant


2 Answers

Reread the docstring for dblquad. The fourth and fifth arguments must be callable (i.e. functions). You have passed in 0 and np.inf. The functions are the lower and upper bounds of the inner integral.

There is an example in the tutorial.

like image 113
Warren Weckesser Avatar answered Oct 06 '22 02:10

Warren Weckesser


The signature of dblquad is

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

gfun : callable

The lower boundary curve in y which is a function taking a single floating point argument (x) and returning a floating point result: a lambda function can be useful here.

hfun : callable

The upper boundary curve in y (same requirements as gfun).

So you need to replace gfun and hfun to functions.

like image 40
K.Chen Avatar answered Oct 06 '22 01:10

K.Chen