17

I'm using scipy.integrate.dblquad, and I get this error:

UserWarning: The maximum number of subdivisions (50) has been achieved.
If increasing the limit yields no improvement ...

I want to increase this limit to see if the integral is well-converged. The documentation specifies how to do this for scipy.integrate.quad (that function takes the maximum number of iterations as an argument), but not for scipy.integrate.dblquad. How can I increase the number of subdivisions for dblquad?

Dan
  • 12,157
  • 12
  • 50
  • 84

2 Answers2

15

A simpler way of doing this is to use the nquad function instead of dblquad. Example code:

from scipy.integrate import nquad

options={'limit':100}
integral=nquad(func,[[xmin,xmax],[ymin,ymax]],
          args=(other_arg,),opts=[options,options])

Note that several of the arguments are lists. The elements of these lists apply to each of the coordinates in order. See the documentation for nquad here.

Dan
  • 12,157
  • 12
  • 50
  • 84
7

According to the source code, dblquad calls quad, reading, simply:

return quad(_infunc,a,b,(func,gfun,hfun,args),epsabs=epsabs,epsrel=epsrel)

Therefore, you could implement this directly yourself with the additional maxp1 argument.

from scipy import integrate

def _infunc(x,func,gfun,hfun,more_args):
    a = gfun(x)
    b = hfun(x)
    myargs = (x,) + more_args
    return quad(func,a,b,args=myargs)[0]

def custom_dblquad(func, a, b, gfun, hfun, args=(), epsabs=1.49e-8, 
                   epsrel=1.49e-8, maxp1=50, limit=50):
    return integrate.quad(_infunc, a, b, (func, gfun, hfun, args), 
                          epsabs=epsabs, epsrel=epsrel, maxp1=maxp1, limit=limit)
Dan
  • 12,157
  • 12
  • 50
  • 84
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 1
    Do you know of any ways to do this that don't involve modifying the source code? – Dan Jan 04 '14 at 19:26
  • 2
    I don't mean modify the source code, I mean call `quad` with those arguments, plus `maxpl`, yourself. – jonrsharpe Jan 04 '14 at 19:52
  • 1
    I've added an example implementation – jonrsharpe Jan 04 '14 at 20:37
  • @jonrsharpe, would you be able please to write an implementation of the above for the current ```dblquad``` ? Thank you https://github.com/scipy/scipy/blob/v1.7.0/scipy/integrate/quadpack.py#L530-L602 – velenos14 Jul 06 '21 at 13:14