trapz — Trapezoidal numerical integration of sampled data.
trapz(Y) approximates the integral of sampled data in Y using the trapezoidal rule. The integration runs along the first non-singleton dimension unless you specify a different dimension explicitly.
How trapz works
trapz(Y)assumes unit spacing between adjacent samples.trapz(X, Y)accepts a scalar spacing, a vector of coordinates whose length matches the working dimension, or an arrayXwith the same size asY.trapz(..., dim)selects the working dimension explicitly. Dimensions larger thanndims(Y)behave like singleton axes, so the result is zero along those slices.- Logical inputs are promoted to double precision before integration. Complex-valued
Yinputs are integrated component-wise and preserve their complex output type. - When the selected dimension has length 0 or 1,
trapzreturns zeros because there are no intervals to integrate.
How RunMat runs trapz on the GPU
RunMat does not yet expose a native provider hook for trapz. When the input is a gpuArray, the runtime gathers the data to host memory, performs the trapezoidal integration with MATLAB-compatible dimension and spacing rules, and re-uploads real-valued outputs so downstream GPU work stays resident.
GPU memory and residency
Manual gpuArray promotion is optional. If sampled data already lives on the GPU, RunMat preserves real-valued outputs as gpuArrays by re-uploading the trapezoidal result after the host fallback path.
Examples
Integrating sampled sine data with explicit coordinates
x = 0:0.01:pi;
y = sin(x);
area = trapz(x, y)Expected output:
area ≈ 2Using unit spacing for a row vector
y = [1 2 3];
q = trapz(y)Expected output:
q = 4Integrating each row independently
A = [1 2 3; 4 5 6];
q = trapz(A, 2)Expected output:
q =
4
10Running trapz on GPU data
G = gpuArray([1 2 3]);
q = trapz(G);
result = gather(q)Expected output:
result = 4FAQ
What spacing does trapz(Y) assume?
It assumes adjacent samples are one unit apart.
What forms of X are accepted?
A scalar spacing, a vector of coordinates matching the integration dimension, or an array with the same size as Y.
What happens for a scalar or singleton slice?
The integral is zero because there are no trapezoids to sum.
Does trapz support complex inputs?
Yes. RunMat integrates the real and imaginary parts independently and returns a complex result.
Does trapz keep gpuArray residency?
For real-valued results, yes. RunMat currently gathers GPU inputs to the host, computes the trapezoidal integral, and re-uploads the result so downstream GPU code can stay resident.
Related functions to explore
These functions work well alongside trapz. Each page has runnable examples you can try in the browser.
cumtrapz, diff, sum, cumsum, gpuArray, gather
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how trapz works, line by line, in Rust.
- View trapz.rs on GitHub
- Learn how the runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is an open-source runtime that executes MATLAB-syntax code — faster, on any GPU, with no license required.
- Simulations that took hours now take minutes. RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed.
- Start running code in seconds. Open the browser sandbox or download a single binary. No license server, no IT ticket, no setup.
- A full development environment. GPU-accelerated 2D and 3D plotting, automatic versioning on every save, and a browser IDE you can share with a link.