Lesson 1
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, a and b.
  • 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

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 f(x)=sin(x)f(x) = \sin(x).

Python
1import 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.

Step 2: Import the Necessary Function from SciPy

Next, import the quad function from SciPy's integrate module.

Python
1from scipy.integrate import quad

This import allows us to use quad to perform the definite integration.

Step 3: Perform the Integration

We'll calculate the definite integral of the sine function from 0 to π\pi.

Python
1# 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 π\pi. The result is stored in integral, and error provides an estimate of the error in computation.

Step 4: Print the Result

Finally, let's print the result of our integration along with the error estimate.

Python
1print(f"Integral of function from 0 to π: {integral} with an error estimate of {error}")

Expected Output:

Plain text
1Integral 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, π\pi] is 2, with a very small error estimate, indicating high accuracy.

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.

Python
1import 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.

Example: Integration with Infinity as a Bound

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 f(x)=exf(x) = e^{-x}. Let's define it:

Python
1# Define the function to integrate 2def f(x): 3 return np.exp(-x)

Here is its plot:

Performing the Integration

We'll compute the integral of the exponential decay function from 0 to positive infinity.

Python
1# 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.

Python
1print(f"Integral of function from 0 to infinity: {integral_inf} with an error estimate of {error_inf}")

Expected Output:

Plain text
1Integral 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.

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!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.