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 .
Python1import numpy as np 2 3# Define the function to integrate 4def f(x): 5 return np.sin(x)
Here, we use NumPy to define our sine function.
Next, import the quad
function from SciPy's integrate module.
Python1from scipy.integrate import quad
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 .
Python1# Calculate the definite integral from 0 to π 2integral, error = quad(f, 0, np.pi)
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.
Python1print(f"Integral of function from 0 to π: {integral} with an error estimate of {error}")
Expected Output:
Plain text1Integral of function from 0 to π: 2.0 with an error estimate of 2.220446049250313e-14
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.
Python1import matplotlib.pyplot as plt 2 3# Plot the function 4x = np.linspace(0, np.pi, 100) 5y = f(x) 6 7plt.plot(x, y, label='f(x)') 8plt.fill_between(x, y, where=[(xi >= 0 and xi <= np.pi) for xi in x], color='lightblue', alpha=0.5, label='Integral region') 9plt.xlabel('x') 10plt.ylabel('f(x)') 11plt.title('Definite Integration using SciPy') 12plt.legend() 13plt.show()
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:
Python1# Define the function to integrate 2def f(x): 3 return np.exp(-x)
Here is its plot:
We'll compute the integral of the exponential decay function from 0 to positive infinity.
Python1# Calculate the definite integral from 0 to infinity 2integral_inf, error_inf = quad(f, 0, np.inf)
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.
Python1print(f"Integral of function from 0 to infinity: {integral_inf} with an error estimate of {error_inf}")
Expected Output:
Plain text1Integral of function from 0 to infinity: 1.0 with an error estimate of 5.842606742906004e-11
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!