Navigation

Operators and Keywords

Function List:

C++ API

: q = quadgk (f, a, b)
: q = quadgk (f, a, b, abstol)
: q = quadgk (f, a, b, abstol, trace)
: q = quadgk (f, a, b, prop, val, …)
: [q, err] = quadgk (…)

Numerically evaluate the integral of f from a to b using adaptive Gauss-Konrod quadrature.

f is a function handle, inline function, or string containing the name of the function to evaluate. The function f must be vectorized and return a vector of output values when given a vector of input values.

a and b are the lower and upper limits of integration. Either or both limits may be infinite or contain weak end singularities. Variable transformation will be used to treat any infinite intervals and weaken the singularities. For example:

quadgk (@(x) 1 ./ (sqrt (x) .* (x + 1)), 0, Inf)

Note that the formulation of the integrand uses the element-by-element operator ./ and all user functions to quadgk should do the same.

The optional argument tol defines the absolute tolerance used to stop the integration procedure. The default value is 1e-10.

The algorithm used by quadgk involves subdividing the integration interval and evaluating each subinterval. If trace is true then after computing each of these partial integrals display: (1) the number of subintervals at this step, (2) the current estimate of the error err, (3) the current estimate for the integral q.

Alternatively, properties of quadgk can be passed to the function as pairs "prop", val. Valid properties are

AbsTol

Define the absolute error tolerance for the quadrature. The default absolute tolerance is 1e-10 (1e-5 for single).

RelTol

Define the relative error tolerance for the quadrature. The default relative tolerance is 1e-6 (1e-4 for single).

MaxIntervalCount

quadgk initially subdivides the interval on which to perform the quadrature into 10 intervals. Subintervals that have an unacceptable error are subdivided and re-evaluated. If the number of subintervals exceeds 650 subintervals at any point then a poor convergence is signaled and the current estimate of the integral is returned. The property "MaxIntervalCount" can be used to alter the number of subintervals that can exist before exiting.

WayPoints

Discontinuities in the first derivative of the function to integrate can be flagged with the "WayPoints" property. This forces the ends of a subinterval to fall on the breakpoints of the function and can result in significantly improved estimation of the error in the integral, faster computation, or both. For example,

quadgk (@(x) abs (1 - x.^2), 0, 2, "Waypoints", 1)

signals the breakpoint in the integrand at x = 1.

Trace

If logically true quadgk prints information on the convergence of the quadrature at each iteration.

If any of a, b, or waypoints is complex then the quadrature is treated as a contour integral along a piecewise continuous path defined by the above. In this case the integral is assumed to have no edge singularities. For example,

quadgk (@(z) log (z), 1+1i, 1+1i, "WayPoints",
        [1-1i, -1,-1i, -1+1i])

integrates log (z) along the square defined by [1+1i, 1-1i, -1-1i, -1+1i].

The result of the integration is returned in q.

err is an approximate bound on the error in the integral abs (q - I), where I is the exact value of the integral.

Reference: L.F. Shampine, "Vectorized adaptive quadrature in MATLAB", Journal of Computational and Applied Mathematics, pp. 131–140, Vol 211, Issue 2, Feb 2008.

See also: quad, quadv, quadl, quadcc, trapz, dblquad, triplequad.

Package: octave