Integrate custom function with sympy

1.5k views Asked by At

Is it possible to integrate custom function using sympy?

I want something like this:

def func(x, y):
    return something_with(x, y)

res = integrate(func, (y, x-2, 3), (x, 1, 2))

Is it possible?

1

There are 1 answers

3
camz On

You can integrate some functions with sympy's integrate. Suppose you had the function (one variable to make it simpler)

def my_func(x):
    return x**2 + x + 1

Then you could integrate it with

x = Symbol('x')
integrate(my_func(x), (x, a, b))

Although only limited mathematical functions are likely to work.

If you only care about the numerical answer, you could try scipy's integrate.quad() (or scipy.integrate.dblquad()) which will do numerical integration, and can tackle a broader set of functions within a small error.

scipy.integrate.quad(my_func, a, b)