Definite integral over one variable in a function with two variables in Scipy

Hannah picture Hannah · Aug 14, 2014 · Viewed 7.8k times · Source

I am trying to calculate the definite integral of a function with multiple variables over just one variable in scipy. This is kind of like what my code looks like-

from scipy.integrate import quad
import numpy as np
def integrand(x,y):
    return x*np.exp(x/y)

quad(integrand, 1,2, args=())

And it returns this type error:

TypeError: integrand() takes exactly 2 arguments (1 given)

However, it works if I put a number into args. But I don't want to, because I want y to remain as y and not a number. Does anyone know how this can be done?

EDIT: Sorry, don't think I was clear. I want the end result to be a function of y, with y still being a symbol.

Answer

Hannah picture Hannah · Aug 18, 2014

Thanks to mdurant, here's what works:

from sympy import integrate, Symbol, exp
from sympy.abc import x
y=Symbol('y')
f=x*exp(x/y)
integrate(f, (x, 1, 2))

Answer:

-(-y**2 + y)*exp(1/y) + (-y**2 + 2*y)*exp(2/y)