integral — Approximate a finite scalar definite integral using adaptive quadrature.
q = integral(fun, a, b) approximates the definite integral of fun from a to b. The first implementation supports scalar-valued integrands over finite real bounds.
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.0000FAQ
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 works, line by line, in Rust.
- View integral.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.