gc5
gc5

Reputation: 9868

Integrate custom function with sympy

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?

Upvotes: 2

Views: 1657

Answers (1)

camz
camz

Reputation: 605

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)

Upvotes: 1

Related Questions