bhylu.com

Advertisements

SciPy Sampled Data Integration

Distinguish integrating recorded samples from integrating a known function.

How it works

trapezoid and simpson integrate sampled values. The sample coordinates matter when spacing is uneven. A smooth-looking set of observations does not reveal every feature between samples, so numerical integration cannot recover unmeasured structure automatically. Use the coordinate argument explicitly and keep time and signal units visible in the resulting area.

Run the code

The complete source is checked locally and in the existing SciPy browser editor. Select Try in playground to preload it, then choose Run. Preloading does not execute the program. The browser starts an isolated runtime and does not read your personal PC files.

The reviewed local environment is Python 3.12.14, NumPy 2.5.4 and SciPy 1.18.0. The browser uses Pyodide 0.27.7, NumPy 2.0.2 and SciPy 1.14.1. Use a separate virtual environment for local work, then run python sampled-integration.py. Apache, MySQL and PHP are not required. Environment setup.

Download the complete source

Example

sampled-integration.py

import numpy as np
import scipy

from scipy.integrate import trapezoid, simpson

t = np.array([0., 1., 2.])
y = t * t
trap = trapezoid(y, x=t)
parabolic = simpson(y, x=t)
assert abs(trap - 3.) < 1e-12
assert abs(parabolic - 8. / 3.) < 1e-12
print(f"Trapezoid: {trap:.6f}")
print(f"Simpson: {parabolic:.6f}")

Expected result

Trapezoid: 3.000000
Simpson: 2.666667

The trapezoid rule joins sample points with straight segments and overestimates this convex curve. Simpson is exact for this evenly spaced quadratic fixture. That special exactness is not a guarantee for noisy observations or arbitrary grids.

For precise contracts and version differences use the official SciPy documentation. Synthetic fixtures do not establish real instrument accuracy or population conclusions.