integral

Numerical integral of a function of one variable.

Category: Mathematics > Numerical Integration and Differential Equations

Syntax

q = integral(fun, a, b)

Integrates fun from a to b with adaptive quadrature.

q = integral(fun, a, b, Name, Value)

Sets options such as 'AbsTol', 'RelTol', 'ArrayValued' or 'Waypoints'.

Description

The default method is a global adaptive Gauss-Kronrod rule, which subdivides the interval where the estimated error is largest until the tolerances are met. Infinite limits are handled by a change of variable.

The integrand must be vectorized: write x.^2, not x^2. This is the usual cause of an error when first using integral.

Input arguments

fun
Handle y = fun(x) that accepts a vector x and returns a vector of the same size.
a, b
Limits. They may be -Inf or Inf.
'ArrayValued'
Set true if fun returns an array for each scalar x.
'Waypoints'
Interior points where the integrand has kinks or discontinuities.

Output arguments

q
Approximate value of the integral.

Example

Finite and infinite ranges

integral(@(x) x.^2, 0, 1)
integral(@(x) exp(-x.^2), -Inf, Inf)
sqrt(pi)
Output
ans = 0.3333
ans = 1.7725
ans = 1.7725

See also

integral2 integral3 quadgk trapz ode45