quad

Numerical integration (quadrature).

Syntax

area=quad(@func,a,b)

area=quad(@func,a,b,reltol)

area=quad(@func,a,b,[reltol,abstol])

[area,count]=quad(...)

Inputs

func
The function to integrate.
A function handle or anonymous function is also supported.
a
Lower integration limit.
Type: double
Dimension: scalar | vector | matrix
b
Upper integration limit.
Type: double
Dimension: scalar | vector | matrix
reltol
Relative tolerance (default: sqrt(eps) or about 1.0e-8).
Type: double
Dimension: scalar
abstol
Absolute tolerance (default: sqrt(eps) or about 1.0e-8).
Type: double
Dimension: scalar

Outputs

area
The estimated area.
Dimension: scalar | matrix
count
Number of function evaluations.
Dimension: scalar | matrix

Examples

Single interval:
function y = Integrand(x)
    y = sqrt(x);
end

[area,count] = quad(@Integrand, 0, 2)
area = 1.88561808
count = 711

Multiple intervals with an anonymous function:

[area,count] = quad(@(x) sqrt(x), [0, 1], [1, 2])
area = [Matrix] 1 x 2
0.66667  1.21895
count = [Matrix] 1 x 2
603  27

Improper integral with an anonymous function:

[area,count] = quad(@(x) exp(-x ^ 2),0,Inf)

area = 0.886226925
count = 198

Comments

quad uses adaptive quadrature with a Gauss-Legendre kernel. The algorithm recursively bisects each interval until the difference between 4 and 5 point Gauss-Legendre rule estimates fall below the relative or absolute tolerance. Either tolerance can be disabled by setting it equal to zero.

The maximum number of function evaluations is 10,000, and the minimum interval is 1.0e-12.

To pass additional parameters to a function argument use an anonymous function.