# How to Find the Integral of a Function in Python with Scipy

In this article, we show how to find the integral of a function in Python with the scipy module.

Integration is a fundamental of calcuculus that adds the area underneath the curve of a given function.

There's 2 types of integration. There's indefinite integration and there's definite integration.

Indefinite integration is finding the area underneath a curve of a function
without specifying a start and end point. This is why it is called indefinite.
It just specifies the area under the curve for a function without having any range from
where to compute the function to in terms of a start and end value. An example of indefinite
integration is finding the integral of x^{2}, which is 1/3 x^{3} + C.

Definite integration is integration that specifies a start and end point.
We integrate a function from a specified start and end value. An example of this is integrating
the function, x, from 0 to 3. Doing this gives us the value of, x^{2}/2= 4.5.

So you see the difference between indefinite integration and definite integration.

With indefinite integration, we get a generic function value returned that contains a constant.

With definite integration, we get an actual numerical value returned.

As an example, we will show an example of definite integration in Python using the scipy module.

We will find the definite integration of the function, x, over the range of 0 to 3.

If you know the math involved, you know that this gives us an answer of 4.5.

You should see this value returned as output in the Python code.

The code to find the integral of this mathematical example is shown below.

So let's now go over the code.

The first thing we must do is we import the scipy module using the statement, import scipy.integrate as integrate.

This line imports the scipy module and the integrate function of the scipy module all in one line of code.

This integrate() function gives us the ability to perform integration with the scipy module. < pid="para1">So we then create a variable named result and set it equal to, integrate.quad(lambda x: x,0,3)

This performs integration for the function, x, over the range of 0 to 3.

Doing the math for this calculation gives us an integral of
x^{2}/2. Plugging in the values gives us an answer of 4.5.

Let's do another slightly more complicated value.

Let's perform the integration of x^{2} over the range of 0 to 4.

The code to perform the definite integration for the function, x^{2},
over the values of 0 to 4 is shown below.

So let's do the math for this computation.

The integral of x^{2} is 1/3 x^{3}.

Plugging 4 into this equation gives us the value of 1/3 4^{3}= 64/3=
21.333333333333336.

One more example.

Let's peform the integration of e^{3x} over the range of values of
1 to 5.

This is shown in the code below.

So this code is a little more involved but still simple.

We import the scipy module and the integrate() function from scipy with the line, import scipy.integrate as integrate.

We then import the math module.

We then create a function called result and set it equal to, integrate.quad(lambda x: math.e**3*x,1,5)

This integrates the function e^{3x}

The integral of e^{3x} is, 3e^{3x}.

Plugging in the values into the equations gives us 241.02644307825196

So now you just have a pretty good idea of how to perform integration in Python with
the scipy module.

Related Resources

How to Randomly Select From or Shuffle a List in Python