I am doing a nested integral via quadrature. To give a definite example, lets say:

$$\int_0^2dx \left[x + \int_0^x dy \, 2y\right]$$

So effectively I'm integrating $x + x^2$ from 0 to 2 (although obviously my actual problem can't be analytically solved like this). Its implemented in code something like this:

inner(x) = quad(y->2*y, 0, x, tolerance=t_inner)
quad(x + inner(x), 0, 2, tolerance=t_outer)


EDIT: Also note, in this example $x$ appears only as a limit to the inner integral, but my actual problem it appears in the integrand as well. Generally I just mean that the inner quad call depends on $x$ in some way.

The point of this example is that for small $x$, the first term is bigger than the second, whereas for larger $x$, the second term (the inner integration) dominates.

This would dictate that I could speed up my code by setting the inner quadrature error tolerance (t_inner) large when $x$ is small, since error on the inner integral at small $x$ contributes less to my overall error. Then I can increase t_inner when I get to large $x$ when this inner quadrature really matters.

My question is, is there some general prescription for figuring out the optimal way to do this kind of thing? Is there some way I can just choose my "global" tolerance, t_outer, and have t_inner picked automatically as a function of $x$?