Welcome! In this lesson, we will explore the concept of definite integration using the SciPy library in Python. Integration is a fundamental part of calculus. It is used to find the area under a curve, among other applications. This lesson will show you how to implement integration in Python using SciPy, allowing you to solve practical, real-world problems.
But before we start, it is important to note: this course doesn't cover differentiation with SciPy, because SciPy has deprecated its derivative
function and doesn't plan to provide any numerical differentiation tools in the future.
The primary function in SciPy for performing definite integration is quad
. This function takes a mathematical function and computes its integral over a specified interval. Here's a brief overview of how quad
works:
- Inputs: A function
f(x)
to integrate and the limits of integration,a
andb
. - Outputs: The computed integral value and an estimate of the error in computation.
The error estimate is important because numerical integration methods can introduce small inaccuracies due to the way they approximate the area under the curve. The quad
function provides this error estimate to give you an idea of how close the computed integral is to the true value.
Let's dive deeper into this through a code example.
First, we'll set up our function and compute its integral over a specified interval.
To start, let's define the function to integrate. For this lesson, we will use a simple example – the sine function .
Here, we use NumPy to define our sine function.
Next, import the quad
function from SciPy's integrate module.
This import allows us to use quad
to perform the definite integration.
We'll calculate the definite integral of the sine function from 0 to .
Here, quad(f, 0, np.pi)
computes the integral of f(x)
from 0 to . The result is stored in integral
, and error
provides an estimate of the error in computation.
Finally, let's print the result of our integration along with the error estimate.
Expected Output:
This output shows that the integral of the sine function over the interval [0, ] is 2, with a very small error estimate, indicating high accuracy.
Now that we've computed the integral, let's visualize it. Visualizations help in understanding the area under the curve that corresponds to your integral.
First, import Matplotlib's plotting functions, then plot our function and the area under the curve.
Here, we use plt.plot
to draw the sine curve and plt.fill_between
to shade the area between the curve and the x-axis over the integration limits (0 to π). This shaded area represents the definite integral we calculated.
SciPy's quad
function can also handle integration where one or both of the bounds are infinite. This can be useful in various applications where the integration is performed over an infinite interval. Let's take a look at an example with the exponential decay function . Let's define it:
Here is its plot:
We'll compute the integral of the exponential decay function from 0 to positive infinity.
Here, quad(f, 0, np.inf)
computes the integral of f(x)
from 0 to infinity. The result is stored in integral_inf
, and error_inf
provides an estimate of the error in computation. Let's print the result of our integration with an infinite bound.
Expected Output:
This result shows that the integral of the exponential decay function over the interval [0, +∞) is 1, with a small error estimate, which aligns with the theoretical solution for this integral.
To wrap up, we've explored how to perform definite integration using SciPy's quad
function and visualize it with Matplotlib. You've gained practical skills in calculating integrals and plotting them to see the area represented visually.
Congratulations on reaching the end of this course! Now that you have the foundational tools for differentiation and integration with SciPy, you're well-prepared for tackling complex scientific problems in Python. Don't forget to apply these skills in the practical exercises provided. Well done, and keep up the great work!
