TRAPEZOID
Overview
The TRAPEZOID
function numerically integrates sampled data using the composite trapezoidal rule. This method estimates the area under a curve by approximating the region between each pair of points as a trapezoid and summing their areas. It is commonly used for integrating discrete data points or functions sampled at regular or irregular intervals. The y-axis locations of points will be taken from the y
array, and the x-axis sample points must be provided with the x
array. The return value will be equal to the combined area under the red lines in the illustration below.
The composite trapezoidal rule is given by:
where are the function values at points .
This implementation only supports a single column for both y
and x
. This is more restrictive than the underlying scipy.integrate.trapezoid
function, which supports higher-dimensional arrays and more advanced use cases. For more details, see the scipy.integrate.trapezoid documentation and the Wikipedia page .
Image source: Wikipedia - Composite trapezoidal rule illustration
This example function is provided as-is without any representation of accuracy.
Usage
To use the function in Excel:
=TRAPEZOID(y, x)
y
(2D list, required): Single column table of values to integrate. Each row is a sample.x
(2D list, required): Single column table of sample points corresponding toy
. Must have the same number of rows asy
.
The function returns a float. Returns an error message (string) if the input is invalid.
Examples
Example 1: Integrate a non-linear vector
This example integrates the vector [1, 4, 9] (squares) with sample points [0, 1, 2].
Inputs:
y | x |
---|---|
1 | 0 |
4 | 1 |
9 | 2 |
Excel formula:
=TRAPEZOID({1;4;9},{0;1;2})
Expected output:
Result |
---|
9.0 |
This means the area under the curve defined by [1,4,9] is 9.0.
Example 2: Integrate with custom x values
This example integrates [1, 2, 3] with sample points [4, 6, 8].
Inputs:
y | x |
---|---|
1 | 4 |
2 | 6 |
3 | 8 |
Excel formula:
=TRAPEZOID({1;2;3},{4;6;8})
Expected output:
Result |
---|
8.0 |
This means the area under the curve with custom sample points is 8.0.
Python Code
from scipy.integrate import trapezoid as scipy_trapezoid
def trapezoid(y, x):
"""
Integrate sampled data using the composite trapezoidal rule.
Args:
y: 2D list (single column) of values to integrate. Each row is a sample.
x: 2D list (single column) of sample points. Must match number of rows in y.
Returns:
Float with the integral, or error message if input is invalid.
This example function is provided as-is without any representation of accuracy.
"""
# Validate y
if not isinstance(y, list) or len(y) < 2:
return "Invalid input: y must be a 2D list with at least two rows."
try:
if all(isinstance(row, list) and len(row) == 1 for row in y):
y_arr = [float(row[0]) for row in y]
else:
return "Invalid input: y must be a single column 2D list."
except Exception:
return "Invalid input: y must be a 2D list of numbers."
# Handle x
if not isinstance(x, list) or len(x) != len(y):
return "Invalid input: x must be a 2D list with the same number of rows as y."
try:
if all(isinstance(row, list) and len(row) == 1 for row in x):
x_arr = [float(row[0]) for row in x]
else:
return "Invalid input: x must be a single column 2D list."
except Exception:
return "Invalid input: x must be a 2D list of numbers."
# Try integration
try:
val = scipy_trapezoid(y_arr, x=x_arr)
return float(round(val, 6))
except Exception as e:
return f"scipy.trapezoid error: {e}"
Live Notebook
Edit this function in a live notebook .