9951 explained code solutions for 126 technologies


python-scipyHow do I use Python and SciPy to calculate an integral?


To calculate an integral using Python and SciPy, you can use the scipy.integrate.quad() function. This function takes two parameters, the function to be integrated and the integration limits. Here is an example:

from scipy.integrate import quad
def f(x):
    return x**4 - 2*x + 1

result = quad(f, 0, 2)
print(result)

The output of this example is: (4.0, 4.440892098500626e-14), where the first value is the integration result and the second value is the estimated error.

The code can be broken down as follows:

  • from scipy.integrate import quad: imports the quad() function from the scipy.integrate module
  • def f(x): return x**4 - 2*x + 1: defines the function to be integrated
  • result = quad(f, 0, 2): calls the quad() function with the function f and the integration limits of 0 and 2
  • print(result): prints the integration result and the estimated error

For more information, please see the documentation for the quad() function.

Edit this code on GitHub