{
"title": "integral",
"category": "math/optim",
"keywords": [
"integral",
"numerical integration",
"adaptive quadrature",
"quadrature",
"function handle"
],
"summary": "Approximate a finite scalar definite integral using adaptive quadrature.",
"references": ["https://www.mathworks.com/help/matlab/ref/integral.html"],
"gpu_support": {
"elementwise": false,
"reduction": false,
"precisions": [],
"broadcasting": "none",
"notes": "The adaptive solver runs on the host. Callback functions may still call GPU-aware builtins."
},
"fusion": {
"elementwise": false,
"reduction": false,
"max_inputs": 3,
"constants": "inline"
},
"requires_feature": null,
"tested": {
"unit": "builtins::math::optim::integral::tests",
"integration": "runmat-vm/tests/closures.rs::integral_accepts_anonymous_function"
},
"description": "`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.",
"behaviors": [
"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`, and `MaxFunEvals` name/value options are accepted for the initial scalar implementation."
],
"examples": [
{
"description": "Integrate a polynomial anonymous function",
"input": "q = integral(@(x) x.^2, 0, 1)",
"output": "q =\n 0.3333"
},
{
"description": "Integrate sine over one half-period",
"input": "q = integral(@sin, 0, pi)",
"output": "q =\n 2.0000"
},
{
"description": "Set tighter tolerances",
"input": "q = integral(@sin, 0, pi, 'AbsTol', 1e-12, 'RelTol', 1e-8)",
"output": "q =\n 2.0000"
}
],
"faqs": [
{
"question": "Is this the same as trapz?",
"answer": "No. `integral` evaluates a function handle adaptively over continuous bounds. `trapz` integrates already-sampled discrete data."
},
{
"question": "Does integral support vector-valued functions?",
"answer": "Not yet. The initial implementation supports finite real scalar-valued integrands."
},
{
"question": "Does integral run on the GPU?",
"answer": "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."
}
],
"links": [
{ "label": "trapz", "url": "./trapz" },
{ "label": "cumtrapz", "url": "./cumtrapz" },
{ "label": "fzero", "url": "./fzero" }
],
"source": {
"label": "`crates/runmat-runtime/src/builtins/math/optim/integral.rs`",
"url": "https://github.com/runmat-org/runmat/blob/main/crates/runmat-runtime/src/builtins/math/optim/integral.rs"
}
}