Qu

integral2

Builtin index · Signal processing

result = integral2(f, xa, xb, ya, yb, [tol=1e-8], [max_evals=5000000])

Input: f, xa, xb, ya, yb, tol, max_evals · Output: result

What it is

Adaptive double integral of f(x, y) over the region xa <= x <= xb, ya(x) <= y <= yb(x). f: a function (or its name) of two numbers returning a number; xa, xb: numbers (may be -inf/inf); ya, yb: each a number or a function of x returning a number (a function limit makes a non-rectangular region such as a disc or triangle; they may also be infinite). It is quad nested: the outer integral over x calls an inner quad over y at every x, so the rule, the handling of endpoint singularities and infinite ranges, and the tol meaning (summed error estimate at most tol * max(1, abs(I)); the inner integrals use tol/10) are those of quad. Swapping xa and xb (or a function limit with yb < ya) negates the result. max_evals caps the evaluations of f over the whole call, so an integrand that can never reach tol stops with an error instead of hanging.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = integral2(f, xa, xb, ya, yb, [tol=1e-8], [max_evals=5000000])
f numberf: a function (or its name) of two numbers returning a number.
xa numberxa, xb: numbers (may be -inf/inf).
xb —Adaptive double integral of f(x, y) over the region xa <= x <= xb, ya(x) <= y <= yb(x).
ya numberya, yb: each a number or a function of x returning a number (a function limit makes a non-rectangular region such as a disc or triangle.
yb —Adaptive double integral of f(x, y) over the region xa <= x <= xb, ya(x) <= y <= yb(x).
tol optional (default 1e-8)the inner integrals use tol/10) are those of quad.
max_evals optional (default 5000000)max_evals caps the evaluations of f over the whole call, so an integrand that can never reach tol stops with an error instead of hanging.
f —passed through
Returns the integral as a number. Errors: tol not reached (the message gives the estimate, its error and the evaluation count; loosen tol or split the region at singularities), max_evals exceeded, fewer than 5 arguments, a limit that is neither a number nor a function, a limit function returning NaN, an f returning NaN/Inf (including an exact pole at a node), and any error raised inside f (passed through). A discontinuity along a curve (for example a step inside the region) converges slowly; split the region there.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

# y'' + y = 0, y(0) = 0, y(pi/2) = 1 (y = sin x), as the system y' = [y1, -y0]

function osc(x, y)
    return [y[1], -y[0]]
end function
function ends(ya, yb)
    return [ya[0], yb[0] - 1]
end function
b = bvp(osc, ends, linspace(0, pi/2, 11), zeros(2, 11))
print("{b.status} on {b.nodes} nodes")                  # converged on 41 nodes

print(bvp_eval(b, pi/4)[0] - sin(pi/4))                 # -1.83e-09


c = chebdiff(16)
print(max(abs(c.D * exp(c.x) - exp(c.x))))              # 5.8e-14, spectral accuracy

print(integral2((x, y) := 1, -1, 1, (x) := -sqrt(1 - x^2), (x) := sqrt(1 - x^2)))   # 3.141593, the unit disc

r = fminbnd((x) := cos(x), 3, 4)
print("{r.params} {r.converged}")                       # 3.141593 true

output
converged on 41 nodes
-1.834181e-9
5.817569e-14
3.141593
3.141593 true

from signal-processing