Numerically evaluate integral, adaptive Simpson quadrature
quad will be removed in a future release.
q = quad(fun,a,b)
q = quad(fun,a,b,tol)
q = quad(fun,a,b,tol,trace)
[q,fcnt] = quad(...)
Quadrature is a numerical method used to find the area under the graph of a function, that is, to compute a definite integral.
q = quad(fun,a,b) tries
to approximate the integral of function
within an error of
1e-6 using recursive adaptive
fun is a function handle. Limits
be finite. The function
y = fun(x) should accept
a vector argument
x and return a vector result
the integrand evaluated at each element of
Parameterizing Functions explains how to provide additional
parameters to the function
fun, if necessary.
q = quad(fun,a,b,tol) uses
an absolute error tolerance
tol instead of the
default which is
1.0e-6. Larger values of
in fewer function evaluations and faster computation, but less accurate
results. In MATLAB® version 5.3 and earlier, the
used a less reliable algorithm and a default relative tolerance of
q = quad(fun,a,b,tol,trace) with
trace shows the values of
[fcnt a b-a Q] during
[q,fcnt] = quad(...) returns
the number of function evaluations.
quadl may be more efficient
with high accuracies and smooth integrands.
The list below contains information to help you determine which quadrature function in MATLAB to use:
quad function may be most
efficient for low accuracies with nonsmooth integrands.
quadl function may be more
quad at higher accuracies with
quadgk function may be most
efficient for high accuracies and oscillatory integrands. It supports
infinite intervals and can handle moderate singularities at the endpoints.
It also supports contour integration along piecewise linear paths.
quadv function vectorizes
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 will integrate
functions that are singular at finite endpoints if the singularities
are not too strong. For example, it will 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.
To compute the integral
write a function
myfun that computes the
function y = myfun(x) y = 1./(x.^3-2*x-5);
@myfun, a function handle to
quad, along with the limits of integration,
Q = quad(@myfun,0,2) Q = -0.4605
Alternatively, you can pass the integrand to
an anonymous function handle
F = @(x)1./(x.^3-2*x-5); Q = quad(F,0,2);
quad may issue one of the following warnings:
'Minimum step size reached' indicates that
the recursive 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.
'Maximum function count exceeded' indicates
that the integrand has been evaluated more than 10,000 times. A nonintegrable
singularity is likely.
'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.
 Gander, W. and W. Gautschi, "Adaptive
Quadrature – Revisited," BIT, Vol. 40, 2000, pp. 84-101.
This document is also available at