Definite Integration with SciPy
Introduction to Definite Integration
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.
Using SciPy for Definite Integration
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,aandb. - 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.
Implementing Integration with a Code Example
Step 2: Import the Necessary Function from SciPy
Next, import the quad function from SciPy's integrate module.
This import allows us to use quad to perform the definite integration.
Step 3: Perform the Integration
Step 4: Print the Result
Visualizing Integration with Matplotlib
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.

Example: Integration with Infinity as a Bound
Performing the Integration
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.
Summary and Next Steps
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!

