Numerically evaluate integral using adaptive Guass-Konrod quadrature. The formulation is based on a proposal by L.F. Shampine, "Vectorized adaptive quadrature in MATLAB", Journal of Computational and Applied Mathematics, pp131-140, Vol 211, Issue 2, Feb 2008 where all function evalutions at an iteration are calculated with a single call to f. Therefore the function f must be of the form f
(
x)
and accept vector values of x and return a vector of the same length representing the function evalutaions at the given values of x. The function f can be defined in terms of a function handle, inline function or string.The bounds of the quadrature
[
a,
b]
can be finite or infinite and contain weak end singularities. Variable transformation will be used to treat infinite intervals and weaken the singularities. For examplequadgk(@(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 toquadgk
should do the same.The absolute tolerance can be passed as a fourth argument in a manner compatible with
quadv
. Equally the user can request that information on the convergence can be printed is the fifth argument is logicallly true.Alternatively, certain properties of
quadgk
can be passed as pairs prop,
val. Valid properties are
AbsTol
- Defines the absolute error tolerance for the quadrature. The default absolute tolerance is 1e-10.
RelTol
- Defines the relative error tolerance for the quadrature. The default relative tolerance is 1e-5.
MaxIntervalCount
quadgk
initially subdivides the interval on which to perform the quadrature into 10 intervals. Sub-intervals that have an unacceptable error are sub-divided and re-evaluated. If the number of sub-intervals exceeds at any point 650 sub-intervals 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 sub-intervals that can exist before exiting.WayPoints
- If there exists discontinuities in the first derivative of the function to integrate, then these can be flagged with teh 'WayPoints' property. This forces the ends of a sub-interval to fall on the breakpoints of the function and can result in significantly improved estimated 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, then
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 assuemd 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]
If two output arguments are requested, then err returns the approximate bounds on the error in the integral
abs (
q-
i)
, where i is the exact value of the integral.