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).

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.

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.

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.

Expected Output:

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.

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:

Here is its plot:

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!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal