How to evaluate single integrals of multivariate functions with Python's scipy.integrate.quad?

前端 未结 2 864
名媛妹妹
名媛妹妹 2020-12-08 17:38

There is a function I am trying to integrate in Python using scipy.integrate.quad. This particular function takes two arguments. There is only one argument I

相关标签:
2条回答
  • 2020-12-08 17:58

    Found the answer in the scipy documentation.

    You can do the following:

    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,))
    

    The args=(1,) argument in the quad method will make a=1 for the integral evalution.

    This can also be carried to functions with more than two variables:

    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))
    

    This will make a=1, b=2, c=3 for the integral evaluation.

    The important thing to remember for the function you want to integrate this way is to make the variable you want to integrate over the first argument to the function.

    0 讨论(0)
  • 2020-12-08 18:08

    Use the args argument (see the scipy documentation):

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

    The comma in args=(a,) is required because a tuple must be passed.

    0 讨论(0)
提交回复
热议问题