How to evaluate single integrals of multidimensional functions with Python scipy.integrate.quad?

There is a function that I am trying to integrate into Python with scipy.integrate.quad. This particular function takes two arguments. There is only one argument that I want to integrate. An example is shown below.

from scipy import integrate as integrate
def f(x,a):  #a is a parameter, x is the variable I want to integrate over
    return a*x

result = integrate.quad(f,0,1)

This example does not work (you think it is clear), because Python reminds me when I try it:

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

I am wondering how to use a integrate.quad()variable to integrate into a single value when this function is, generally speaking, a function with several variables, with additional variables providing the parameters of the function.

+4
source share
2 answers

.

:

from scipy import integrate as integrate
def f(x,a):  #a is a parameter, x is the variable I want to integrate over
    return a*x

result = integrate.quad(f,0,1,args=(1,))

args=(1,) quad a=1 .

:

from scipy import integrate as integrate
def f(x,a,b,c):  #a is a parameter, x is the variable I want to integrate over
    return a*x + b + c

result = integrate.quad(f,0,1,args=(1,2,3))

a=1, b=2, c=3 .

, , , .

+5

args (. scipy documentation):

result = integrate.quad(f,0,1, args=(a,))

args=(a,) , .

+3

All Articles