Numerically evaluate integral, adaptive Gauss-Kronrod quadrature
q = quadgk(fun,a,b)
[q,errbnd] = quadgk(fun,a,b)
[q,errbnd] = quadgk(fun,a,b,param1,val1,param2,val2,...)
q = quadgk(fun,a,b) attempts
to approximate the integral of a scalar-valued function
high-order global adaptive quadrature and default error tolerances.
y = fun(x) should accept a vector
x and return a vector result
y is the integrand evaluated at each element
fun must be a function
b can be
If both are finite, they can be complex. If at least one is complex,
the integral is approximated over a straight line path from
the complex plane.
Parameterizing Functions explains how to provide additional
parameters to the function
fun, if necessary.
[q,errbnd] = quadgk(fun,a,b) returns
an approximate upper bound on the absolute error,
|Q - I|,
I denotes the exact value of the integral.
[q,errbnd] = quadgk(fun,a,b,param1,val1,param2,val2,...) performs
the integration with specified values of optional parameters. The
available parameters are
Absolute error tolerance.
The default value
Relative error tolerance.
The default value
Vector of integration waypoints.
Maximum number of intervals allowed.
default value is
Integrand with a singularity at an integration end point
Write a function
myfun that computes the
function y = myfun(x) y = exp(x).*log(x);
@myfun, a function handle to
quadgk, along with the limits of integration,
q = quadgk(@myfun,0,1) q = -1.3179
Alternatively, you can pass the integrand to
an anonymous function handle
f = (@(x)exp(x).*log(x)); q = quadgk(f,0,1);
Oscillatory integrand on a semi-infinite interval
Integrate over a semi-infinite interval with specified tolerances, and return the approximate error bound:
f = @(x)x.^5.*exp(-x).*sin(x); [q,errbnd] = quadgk(f,0,inf,'RelTol',1e-8,'AbsTol',1e-12) q = -15.0000 errbnd = 9.4386e-009
Contour integration around a pole
Waypoints to integrate around a pole
using a piecewise linear contour:
f = @(z)1./(2*z - 1); q = quadgk(f,-1-i,-1-i,'Waypoints',[1-i,1+i,-1+i]) q = 0.0000 + 3.1416i
quadgk may issue one of the following warnings:
'Minimum step size reached' indicates that
interval subdivision has produced a subinterval whose length is on
the order of roundoff error in the length of the original interval.
A nonintegrable singularity is possible.
'Reached the limit on the maximum number of intervals
in use' indicates that the integration was terminated before
meeting the tolerance requirements and that continuing the integration
would require more than
The integral may not exist, or it may be difficult to approximate
does not help unless the tolerance requirements were nearly met when
the integration was previously terminated.
'Infinite or Not-a-Number function value encountered' indicates
a floating point overflow or division by zero during the evaluation
of the integrand in the interior of the interval.
If the interval is infinite, ,
then for the integral of
fun(x) to exist,
x approaches infinity, and
it to decay rapidly. Special methods should be used for oscillatory
functions on infinite intervals, but
be used if
fun(x) decays fast enough.
quadgk function can integrate
functions that are singular at finite endpoints if the singularities
are not too strong. For example, it can integrate functions that behave
at an endpoint
>= -1/2. If the function is singular at points inside
write the integral as a sum of integrals over subintervals with the
singular points as endpoints, compute them with
and add the results.
 L.F. Shampine "Vectorized Adaptive Quadrature in MATLAB®," Journal of Computational and Applied Mathematics, 211, 2008, pp.131–140.