SciPy – Using integrate.dblquad() function (5 examples)

Updated: March 7, 2024 By: Guest Contributor Post a comment

SciPy, a powerful scientific computation library in Python, provides numerous tools for mathematical operations, including integration. This tutorial focuses on the integrate.dblquad() function of SciPy, dedicated to performing double integration. By showcasing five examples that increase in complexity, we aim to demonstrate the versatility and power of this function.

Example 1: Basic Double Integration

The first example demonstrates how to perform a simple double integral. Suppose you want to integrate a function f(x, y) = x*y, over the square region 0 ≤ x ≤ 1, 0 ≤ y ≤ 1.

import scipy.integrate as spi

# Define the integrand function
f = lambda y, x: x*y 

# Define limits of integration
lower_x = 0
upper_x = 1
lower_y = 0
upper_y = 1

# Perform double integration
result, error = spi.dblquad(f, lower_x, upper_x, lambda x: lower_y, lambda x: upper_y)

print(f'Result: {result}, Error estimate: {error}')

This should output:

Result: 0.25, Error estimate: 2.7755575615628914e-17

Example 2: Incorporating Limits as Functions

In Example 2, we explore setting variable limits for integration. Consider the function f(x, y) = x^2 + y^2 integrated over a triangular region defined by 0 ≤ x ≤ 1 and x ≤ y ≤ 1.

import scipy.integrate as spi

# Function definition
f = lambda y, x: x**2 + y**2

# Perform the integration
result, error = spi.dblquad(f, 0, 1, lambda x: x, lambda x: 1)

print(f'Result: {result}, Error estimate: {error}')

This should yield:

Result: 0.3333333333333333, Error estimate: 3.700743415417189e-17

Example 3: Handling Singularities

Handling singularities is an advanced application of integrate.dblquad(). Consider integrating f(x, y) = 1/((x-0.5)^2 + (y-0.5)^2), over the same square region as in Example 1, but excluding the point (0.5, 0.5) to avoid the singularity.

Note: Directly integrating functions with singularities might lead to errors or inaccurate results. In practice, we avoid or work around the singularity.

import scipy.integrate as spi

# Define the integrand
f = lambda y, x: 1/((x-0.5)**2 + (y-0.5)**2)

# Attempt to integrate (might not converge)
try:
    result, error = spi.dblquad(f, 0, 1, lambda x: 0, lambda x: 1)
    print(f'Result: {result}, Error estimate: {error}')
except Exception as e:
    print(f'Integration failed: {str(e)}')

Example 4: Applying Conditions Within the Integrand

This example showcases how conditions can be applied within the integrand to, for instance, define a function piecewise. Let’s integrate f(x, y) = x*y if x > y else x**2, over the region 0 ≤ x ≤ 1, 0 ≤ y ≤ 1.

import scipy.integrate as spi

# Define the conditional integrand
f = lambda y, x: x*y if x > y else x**2

# Execute the double integration
result, error = spi.dblquad(f, 0, 1, lambda x: 0, lambda x: 1)

print(f'Result: {result}, Error estimate: {error}')

This integration explores how to deal with piecewise functions within the context of double integration, producing valid results.

Example 5: Dynamic Integration Limits with External Parameters

The final example demonstrates integrating a function f(x, y, p) = x*y + p, where p is a parameter, over a region defined by 0 ≤ x ≤ 1 and y = p*x to y = 1. This example illustrates the flexibility of dblquad in handling dynamic limits and parameters.

import scipy.integrate as spi

# Integrand that includes an extra parameter
f_param = lambda y, x, p: x*y + p

# Wrap the integrand, fixing the extra parameter
f = lambda y, x: f_param(y, x, p=0.5)

# Integration with dynamic upper limit based on a parameter
result, error = spi.dblquad(f, 0, 1, lambda x: 0.5*x, lambda x: 1)

print(f'Result: {result}, Error estimate: {error}')

Conclusion

Throughout these examples, we’ve observed how integrate.dblquad() can be employed for a variety of double integration challenges, from the basic to the complex. From incorporating dynamic limits and parameters to handling singularities and piecewise functions, dblquad proves to be an invaluable tool in the numerical integration toolkit. As demonstrated, mastering its usage allows for tackling diverse mathematical and real-world problems with precision and efficiency.