pythonarraysscipynumerical-integrationquad

How can I isolate the result of scipy.integrate.quad function, rather than having the result and the error of the calculation?


I'm trying to create an array of integral values that will be used further in calculations. The problem is that integrate.quad returns (answer, error). I can't use that in other calculations because it isn't a float; it is a set of two numbers.


Solution

  • integrate.quad returns a tuple of two values (and possibly more data in some circumstances). You can access the answer value by referring to the zeroth element of the tuple that is returned. For example:

    # import scipy.integrate
    from scipy import integrate
    
    # define the function we wish to integrate
    f = lambda x: x**2
    
    # do the integration on f over the interval [0, 10]
    results = integrate.quad(f, 0, 10)
    
    # print out the integral result, not the error
    print 'the result of the integration is %lf' % results[0]