Main Content

quad

(Not recommended) Numerically evaluate integral, adaptive Simpson quadrature

quad is not recommended. Use integral instead.

Description

example

q = quad(fun,a,b) approximates the integral of function fun from a to b using recursive adaptive Simpson quadrature:

q=abf(x) dx

q = quad(fun,a,b,tol) specifies an absolute error tolerance tol for each subinterval, instead of the default value of 1e-6.

q = quad(fun,a,b,tol,trace) optionally turns on the display of diagnostic information. When trace is nonzero, quad shows the vector of values [fcnEvals, a, b-a, Q] during the recursion.

[q,fcnEvals] = quad(___) additionally returns the number of function evaluations fcnEvals. You can specify any of the previous input argument combinations.

Examples

collapse all

Compute the integral

021x3-2x-5dx.

First, create an anonymous function myfun that computes the integrand.

myfun = @(x) 1./(x.^3-2*x-5);

Now use quad to compute the integral. Specify the limits of integration as the second and third input arguments.

q = quad(myfun,0,2)
q = -0.4605

Alternatively, you can pass the integrand to quad by creating a function file:

function y = myfun(x) 
  y = 1./(x.^3-2*x-5);
end

With this method, the call to quad becomes quad(@myfun,0,2).

Input Arguments

collapse all

Integrand, specified as a function handle that defines the function to be integrated from a to b.

For scalar-valued problems, the function y = fun(x) must accept a vector argument x and return a vector result y, where y is the integrand evaluated at each element of x. This requirement generally means that fun must use array operators (.^, .*, …) instead of matrix operators (^, *, …).

Parameterizing Functions explains how to provide additional parameters to the function fun, if necessary.

Example: q = quad(@(x) exp(1-x.^2),a,b) integrates an anonymous function handle.

Example: q = quad(@myFun,a,b) integrates the function myFun, which is saved as a file.

Data Types: function_handle

Integration limits, specified as separate scalar arguments. The limits a and b must be finite.

Example: quad(fun,0,1) integrates fun from 0 to 1.

Data Types: single | double

Absolute error tolerance, specified as a scalar. quad uses the absolute error tolerance on each subinterval in the integration. As the magnitude of tol increases, quad performs fewer function evaluations and completes the calculation faster, but produces less accurate results.

Example: quad(fun,a,b,1e-12) sets the absolute error tolerance to 1e-12.

Data Types: single | double

Toggle for diagnostic information, specified as a nonzero scalar. When trace is nonzero, quad displays the vector of values [fcnEvals, a, b-a, Q] for each subinterval in the recursion:

  • fcnEvals gives the number of function evaluations

  • a and b are the limits of integration

  • Q is the computed area of the subinterval

Example: quad(fun,a,b,1e-8,1) integrates fun from a to b with a tolerance of 1e-8 and diagnostic information turned on.

Output Arguments

collapse all

Value of integral, returned as a scalar.

Number of function evaluations, returned as a scalar.

Algorithms

quad implements a low order quadrature method using an adaptive recursive Simpson's rule .

References

[1] Gander, W., and W. Gautschi. “Adaptive Quadrature—Revisited.” BIT Numerical Mathematics 40 (2000): 84–101. https://doi.org/10.1023/A:1022318402393

Introduced before R2006a