</>
Skip to content
SciPy lessons (7/25)

SciPy — Integration

Quad (single integral)

from scipy.integrate import quad

def f(x):
    return x**2

result, error = quad(f, 0, 1)
print(f"Integral = {result:.4f}")
print(f"Error = {error:.2e}")

Definite integral

import numpy as np

# Integrate sin(x) from 0 to pi
result, _ = quad(np.sin, 0, np.pi)
print(f"Integral of sin(x) from 0 to pi = {result:.4f}")

Double integral

from scipy.integrate import dblquad

def f(y, x):
    return x * y

result, error = dblquad(f, 0, 1, 0, 1)
print(f"Double integral = {result:.4f}")

Triple integral

from scipy.integrate import tplquad

def f(z, y, x):
    return x * y * z

result, error = tplquad(f, 0, 1, 0, 1, 0, 1)
print(f"Triple integral = {result:.4f}")

Numerical integration (fixed-sample)

from scipy.integrate import simps
import numpy as np

x = np.array([0, 1, 2, 3, 4])
y = np.array([0, 1, 4, 9, 16])

result = simps(y, x)
print(f"Simpson's rule = {result:.4f}")

ODE integration

from scipy.integrate import odeint

def model(y, t):
    return -2 * y

y0 = 1
t = np.linspace(0, 5, 100)
solution = odeint(model, y0, t)

Mini Practice

  1. Compute a definite integral
  2. Perform double integration
  3. Use Simpson's rule
  4. Solve a simple ODE

Up Next

Continue with Differentiation - Numerical derivatives.

Related Topics

Frequently Asked Questions about Integration

What is Integration in SciPy?

Integration is a fundamental concept in SciPy. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Integration?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Integration.

Why is Integration important in SciPy?

Integration is essential for SciPy development. Understanding this concept will help you write better code and solve real-world problems more effectively.