Search code examples
pythonscipysympyintegral

Double integral with variable boundaries in python Scipy + sympy (?)


The full mathematical problem is here.

Briefly I want to integrate a function with a double integral. The inner integral has boundaries 20 and x-2, while the outer has boundaries 22 and 30.

I know that with Scipy I can compute the double integral with scipy.integrate.nquad. I would like to do something like this:

def f(x, y):
    return (x ** 2 + y ** 2)
res = sp.integrate.nquad(f, [[22, 30], [20, x-2]])

Is it possible? Maybe using also sympy?


Solution

  • I solved with sympy:

    from sympy import *
    
    x, y = symbols("x y")
    f = (x ** 2 + y ** 2)
    res = integrate(f, (y, 20, x-2), (x, 22, 30))
    

    Basically sympy.integrate is able to deal with multiple integrations, also with variable boundaries.