2

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.

Hannah
  • 165
  • 2
  • 3
  • 13
  • quad is for numerical integration, so can only be done for a definite value of y (you could use a partial function in this case). Did you want symbolic integration? In that case look at sympy. – mdurant Aug 14 '14 at 18:13
  • I want the answer to be a function of y. Can I use sympy (as in y=Symbol('y') in combination with scipy? – Hannah Aug 14 '14 at 19:01
  • I don't think so, scipy is purely numeric (but try it - maybe the symbol propagates through); you should use the integration within sympy http://docs.sympy.org/dev/modules/integrals/integrals.html – mdurant Aug 14 '14 at 20:01
  • Yeah, I tried using them in combination, it doesn't work. But won't I have the same problem with sympy, just that now it will only do symbolic integration? I still need 'x' to be integrated. – Hannah Aug 14 '14 at 23:25
  • 1
    No: sympy really can do real definite (and indefinite) integration, returning numerical values for coefficients of the remaining unknown symbol, in this instance y. – mdurant Aug 15 '14 at 00:16
  • Thank you, using Sympy only works for this example. – Hannah Aug 18 '14 at 17:38

5 Answers5

4

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)
Hannah
  • 165
  • 2
  • 3
  • 13
  • Hi, in the above code, y is a symbol. If we set `int_f = integrate(f, (x, 1, 2))`, in principle, `int_f` is a function of `y`, how can we get the value of `int_f` if we set different values of `y`? Thanks~ – Huanian Zhang Jul 25 '17 at 05:36
1

You probably just want the result to be a function of y right?:

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

partial_int = lambda y: quad(integrand, 1,2, args=(y,))
print partial_int(5)
#(2.050684698584342, 2.2767173686148355e-14)
CT Zhu
  • 52,648
  • 17
  • 120
  • 133
0

The best you can do is use functools.partial, to bind what arguments you have for the moment. But one fundamentally cannot numerically integrate a definite integral if you havnt got the entire domain specified yet; in that case the resulting expression will necessarily still contain symbolic parts, so the intermediate result isn't numerical.

Eelco Hoogendoorn
  • 10,459
  • 1
  • 44
  • 42
0

(Assuming that you are talking about computing the definite integral over x given a specific, fixed value of y.)

You could use a lambda:

quad(lambda x:integrand(x, 10), 1, 2, args=())

or functools.partial():

quad(functools.partial(integrand, y=10), 1, 2, args=())
NPE
  • 486,780
  • 108
  • 951
  • 1,012
0
from scipy.integrate import quad
import numpy as np
def integrand(x,y):
    return x*np.exp(x/y)

vec_int = np.vectorize(integrand)
y = np.linspace(0, 10, 100)
vec_int(y)