Numerical Integration with SciPy

Introduction to Numerical Integration

SciPy's integrate module provides several functions for numerical integration of functions and solving ordinary differential equations. This is essential when analytical solutions are difficult or impossible to obtain.

Key Functions:
  • quad - Single integration (adaptive quadrature)
  • dblquad - Double integration
  • tplquad - Triple integration
  • nquad - N-dimensional integration
  • fixed_quad - Fixed-order Gaussian quadrature
  • quadrature - Adaptive Gaussian quadrature
  • odeint / solve_ivp - ODE solvers

1. Basic Definite Integration

The quad function is the workhorse for single-variable integration. It uses adaptive quadrature and returns both the result and an error estimate.

Loading code editor...

2. Trigonometric Functions

SciPy can handle various function types, including trigonometric functions with proper handling of periods and singularities.

Loading code editor...

3. Improper Integrals

SciPy can handle integrals with infinite limits using appropriate transformations and numerical techniques.

Loading code editor...

4. Multiple Integration

For multi-dimensional integrals, SciPy provides functions likedblquad (double) and tplquad (triple).

Loading code editor...

5. Ordinary Differential Equations

SciPy also includes powerful ODE solvers for systems of ordinary differential equations.

Loading code editor...
Best Practices
  • Always check the error estimate returned by quad
  • For oscillatory functions, consider using quad with appropriate weight functions
  • Break up integrals at singular points
  • Use vectorized functions for better performance
  • For ODEs, choose the appropriate solver based on stiffness and accuracy requirements