trapz — Approximate integrals of sampled data using the trapezoidal rule with MATLAB-compatible dimension handling.
trapz(Y) approximates integrals of sampled data using trapezoidal segments. By default integration runs along the first non-singleton dimension, with MATLAB-compatible explicit-dimension forms supported.
Syntax
Q = trapz(Y)
Q = trapz(Y, dim)
Q = trapz(X, Y)
Q = trapz(X, Y, dim)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
Y | Any | Yes | — | Sample values. |
dim | Any | Yes | — | Integration dimension. |
X | Any | Yes | — | Sample points or spacing. |
Returns
| Name | Type | Description |
|---|---|---|
Q | NumericArray | Trapezoidal integral output. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:trapz:InvalidArgument | Input argument count, dimension selector, or spacing arguments are invalid. | trapz: invalid argument |
RunMat:trapz:InvalidInput | Input values cannot be converted to supported numeric integration domains. | trapz: invalid input |
RunMat:trapz:Internal | Integration execution fails during gather, allocation, or provider promotion. | trapz: internal integration failure |
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.
Does RunMat run 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 = 4Using trapz with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how trapz changes the result.
Run a small trapz example, explain the result, then change one input and compare the output.
FAQ
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 Math functions
Reduction
all · any · cummax · cummin · cumprod · cumsum · cumtrapz · diff · gradient · max · mean · median · min · nnz · prod · std · sum · var
Elementwise
abs · angle · complex · conj · double · exp · expm1 · factorial · gamma · hypot · imag · ldivide · log · log10 · log1p · log2 · minus · nextpow2 · plus · pow2 · power · rdivide · real · sign · single · sqrt · times
Trigonometry
acos · acosh · asin · asinh · atan · atan2 · atanh · cos · cosd · cosh · deg2rad · rad2deg · sin · sind · sinh · tan · tand · tanh
Structure
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how trapz is executed, line by line, in Rust.
- View the source for trapz in Rust on GitHub
- Learn how the RunMat 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 blazing on any GPU. It is licensed under the Apache 2.0 license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.