integral — Approximate finite scalar definite integrals in MATLAB and RunMat.
q = integral(fun, a, b) approximates the definite integral of fun from a to b using adaptive quadrature. Current support targets scalar-valued integrands over finite real bounds, following MATLAB-style call semantics.
Syntax
q = integral(fun, xmin, xmax)
q = integral(fun, xmin, xmax, options)
q = integral(fun, xmin, xmax, name, value, ...)Inputs
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
fun | Any | Yes | — | Scalar integrand callback. |
xmin | Any | Yes | — | Lower integration bound. |
xmax | Any | Yes | — | Upper integration bound. |
options | Any | No | — | Options struct for AbsTol/RelTol/MaxFunEvals. |
name | PropertyName | No | — | Option name. |
value | PropertyValue | Variadic | — | Option value and additional name/value pairs. |
Returns
| Name | Type | Description |
|---|---|---|
q | NumericScalar | Numerical integral estimate. |
Errors
| Identifier | When | Message |
|---|---|---|
RunMat:integral:InvalidArgument | Option grammar/name-value parsing is invalid. | integral: invalid argument |
RunMat:integral:InvalidInput | Bounds/integrand/adaptive solver semantics are invalid. | integral: invalid input |
How integral works
- The function may be a named function handle such as
@sin, an anonymous function such as@(x) x.^2, or a function-handle string. - The integration loop evaluates the integrand at scalar sample points and adapts subintervals using Simpson error estimates.
- The integrand must return a finite real scalar. Array-valued and complex-valued integrands are not supported yet.
- Finite reversed bounds are accepted and negate the result. Equal bounds return zero.
AbsTol,RelTol, andMaxFunEvalsname/value options are accepted for the initial scalar implementation.
Examples
Integrate a polynomial anonymous function
q = integral(@(x) x.^2, 0, 1)Expected output:
q =
0.3333Integrate sine over one half-period
q = integral(@sin, 0, pi)Expected output:
q =
2.0000Set tighter tolerances
q = integral(@sin, 0, pi, 'AbsTol', 1e-12, 'RelTol', 1e-8)Expected output:
q =
2.0000Using integral with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how integral changes the result.
Run a small integral example, explain the result, then change one input and compare the output.
FAQ
Is this the same as trapz?⌄
No. integral evaluates a function handle adaptively over continuous bounds. trapz integrates already-sampled discrete data.
Does integral support vector-valued functions?⌄
Not yet. The initial implementation supports finite real scalar-valued integrands.
Does integral run on the GPU?⌄
The adaptive solver runs on the host because it repeatedly invokes user code through function-handle dispatch. The callback itself may call GPU-aware builtins.
Related Math functions
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
Reduction
all · any · cummax · cummin · cumprod · cumsum · cumtrapz · diff · gradient · max · mean · median · min · nnz · prod · std · sum · trapz · var
Structure
Open-source implementation
Unlike proprietary runtimes, every RunMat function is open-source. Read exactly how integral is executed, line by line, in Rust.
- View the source for integral 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.