take the integral of a function in python

To integrate a function in Python, you can use the quad function from the scipy library. Here is an example code:

main.py
from scipy.integrate import quad

# define your function
def f(x):
    return x**2

# integrate the function over the range [0, 1]
result, error = quad(f, 0, 1)

# print the result
print("Integral value:", result)
214 chars
12 lines

Output:

main.py
Integral value: 0.33333333333333337
36 chars
2 lines

Here, the quad function takes the function to be integrated (f), and the lower and upper limits of integration (0 and 1, in this case) as inputs, and returns the result of the integration as well as an estimate of the error.

gistlibby LogSnag