Octave supports three different algorithms for computing the integral of a function f over the interval from a to b. These are
quad
quadl
quadgk
quadv
quadcc
trapz
Besides these functions Octave also allows you to perform cumulative
numerical integration using the trapezoidal method through the
cumtrapz
function.
Integrate a nonlinear function of one variable using quadpack. The first argument is the name of the function, the function handle, or the inline function to call to compute the value of the integrand. It must have the form
y = f (x)where y and x are scalars.
The second and third arguments are limits of integration. Either or both may be infinite.
The optional argument tol is a vector that specifies the desired accuracy of the result. The first element of the vector is the desired absolute tolerance, and the second element is the desired relative tolerance. To choose a relative test only, set the absolute tolerance to zero. To choose an absolute test only, set the relative tolerance to zero.
The optional argument sing is a vector of values at which the integrand is known to be singular.
The result of the integration is returned in v. ier contains an integer error code (0 indicates a successful integration). nfun indicates the number of function evaluations that were made, and err contains an estimate of the error in the solution.
The function
quad_options
can set other optional parameters forquad
.Note: because
quad
is written in Fortran it cannot be called recursively.See also: quad_options, quadv, quadl, quadgk, quadcc, trapz, dblquad, triplequad.
When called with two arguments, this function allows you set options parameters for the function
quad
. Given one argument,quad_options
returns the value of the corresponding option. If no arguments are supplied, the names of all the available options and their current values are displayed.Options include
"absolute tolerance"
- Absolute tolerance; may be zero for pure relative error test.
"relative tolerance"
- Non-negative relative tolerance. If the absolute tolerance is zero, the relative tolerance must be greater than or equal to
max (50*eps, 0.5e-28)
."single precision absolute tolerance"
- Absolute tolerance for single precision; may be zero for pure relative error test.
"single precision relative tolerance"
- Non-negative relative tolerance for single precision. If the absolute tolerance is zero, the relative tolerance must be greater than or equal to
max (50*eps, 0.5e-28)
.
Here is an example of using quad
to integrate the function
f(x) = x * sin (1/x) * sqrt (abs (1 - x))
from x = 0 to x = 3.
This is a fairly difficult integration (plot the function over the range of integration to see why).
The first step is to define the function:
function y = f (x) y = x .* sin (1 ./ x) .* sqrt (abs (1 - x)); endfunction
Note the use of the `dot' forms of the operators. This is not necessary
for the call to quad
, but it makes it much easier to generate a
set of points for plotting (because it makes it possible to call the
function with a vector argument to produce a vector result).
Then we simply call quad:
[v, ier, nfun, err] = quad ("f", 0, 3) ⇒ 1.9819 ⇒ 1 ⇒ 5061 ⇒ 1.1522e-07
Although quad
returns a nonzero value for ier, the result
is reasonably accurate (to see why, examine what happens to the result
if you move the lower bound to 0.1, then 0.01, then 0.001, etc.).
Numerically evaluate integral using adaptive Lobatto rule.
quadl (
f,
a,
b)
approximates the integral of f(
x)
to machine precision. f is either a function handle, inline function or string containing the name of the function to evaluate. The function f must return a vector of output values if given a vector of input values.If defined, tol defines the relative tolerance to which to which to integrate f
(
x)
. While if trace is defined, displays the left end point of the current interval, the interval length, and the partial integral.Additional arguments p1, etc., are passed directly to f. To use default values for tol and trace, one may pass empty matrices.
Reference: W. Gander and W. Gautschi, Adaptive Quadrature - Revisited, BIT Vol. 40, No. 1, March 2000, pp. 84–101. http://www.inf.ethz.ch/personal/gander/
See also: quad, quadv, quadgk, quadcc, trapz, dblquad, triplequad.
Numerically evaluate integral using adaptive Gauss-Konrod quadrature. The formulation is based on a proposal by L.F. Shampine, "Vectorized adaptive quadrature in matlab", Journal of Computational and Applied Mathematics, pp131-140, Vol 211, Issue 2, Feb 2008 where all function evaluations at an iteration are calculated with a single call to f. Therefore the function f must be of the form f
(
x)
and accept vector values of x and return a vector of the same length representing the function evaluations at the given values of x. The function f can be defined in terms of a function handle, inline function or string.The bounds of the quadrature
[
a,
b]
can be finite or infinite and contain weak end singularities. Variable transformation will be used to treat infinite intervals and weaken the singularities. For example:quadgk(@(x) 1 ./ (sqrt (x) .* (x + 1)), 0, Inf)Note that the formulation of the integrand uses the element-by-element operator
./
and all user functions toquadgk
should do the same.The absolute tolerance can be passed as a fourth argument in a manner compatible with
quadv
. Equally the user can request that information on the convergence can be printed is the fifth argument is logically true.Alternatively, certain properties of
quadgk
can be passed as pairs prop,
val. Valid properties are
AbsTol
- Defines the absolute error tolerance for the quadrature. The default absolute tolerance is 1e-10.
RelTol
- Defines the relative error tolerance for the quadrature. The default relative tolerance is 1e-5.
MaxIntervalCount
quadgk
initially subdivides the interval on which to perform the quadrature into 10 intervals. Sub-intervals that have an unacceptable error are sub-divided and re-evaluated. If the number of sub-intervals exceeds at any point 650 sub-intervals then a poor convergence is signaled and the current estimate of the integral is returned. The property 'MaxIntervalCount' can be used to alter the number of sub-intervals that can exist before exiting.WayPoints
- If there exists discontinuities in the first derivative of the function to integrate, then these can be flagged with the
"WayPoints"
property. This forces the ends of a sub-interval to fall on the breakpoints of the function and can result in significantly improved estimation of the error in the integral, faster computation or both. For example,quadgk (@(x) abs (1 - x .^ 2), 0, 2, 'Waypoints', 1)signals the breakpoint in the integrand at x
= 1
.Trace
- If logically true, then
quadgk
prints information on the convergence of the quadrature at each iteration.If any of a, b or waypoints is complex, then the quadrature is treated as a contour integral along a piecewise continuous path defined by the above. In this case the integral is assumed to have no edge singularities. For example,
quadgk (@(z) log (z), 1+1i, 1+1i, "WayPoints", [1-1i, -1,-1i, -1+1i])integrates
log (z)
along the square defined by[1+1i, 1-1i, -1-1i, -1+1i]
If two output arguments are requested, then err returns the approximate bounds on the error in the integral
abs (
q-
i)
, where i is the exact value of the integral.See also: quad, quadv, quadl, quadcc, trapz, dblquad, triplequad.
Numerically evaluate the integral of f from a to b using adaptive Simpson's rule. f is either a function handle, inline function or string containing the name of the function to evaluate. The function defined by f may be a scalar, vector or array-valued.
If a value for tol is given, it defines the tolerance used to stop the adaptation procedure, otherwise the default value of 1e-6 is used.
The algorithm used by
quadv
, involves recursively subdividing the integration interval and applying Simpson's rule on each sub-interval. If trace is true, after computing each of these partial integrals, display the total number of function evaluations, the left end of the sub-interval, the length of the sub-interval and the approximation of the integral over the sub-interval.Additional arguments p1, etc., are passed directly to f. To use default values for tol and trace, one may pass empty matrices.
See also: quad, quadl, quadgk, quadcc, trapz, dblquad, triplequad.
Numerically evaluates an integral using the doubly-adaptive quadrature described by P. Gonnet in Increasing the Reliability of Adaptive Quadrature Using Explicit Interpolants, ACM Transactions on Mathematical Software, in Press, 2010. The algorithm uses Clenshaw-Curtis quadrature rules of increasing degree in each interval and bisects the interval if either the function does not appear to be smooth or a rule of maximum degree has been reached. The error estimate is computed from the L2-norm of the difference between two successive interpolations of the integrand over the nodes of the respective quadrature rules.
For example,
int = quadcc (f, a, b, 1.0e-6);computes the integral of a function f in the interval [a, b] to the relative precision of six decimal digits. The integrand f should accept a vector argument and return a vector result containing the integrand evaluated at each element of the argument, for example:
f = @(x) x .* sin (1 ./ x) .* sqrt (abs (1 - x));If the integrand has known singularities or discontinuities in any of its derivatives inside the interval, as does the above example at x=1, these can be specified in the additional argument sing as follows
int = quadcc (f, a, b, 1.0e-6, [ 1 ]);The two additional output variables err and nr_points return an estimate of the absolute integration error and the number of points at which the integrand was evaluated respectively. If the adaptive integration did not converge, the value of err will be larger than the requested tolerance. It is therefore recommended to verify this value for difficult integrands.
If either a or b are
+/-Inf
,quadcc
integrates f by substituting the variable of integration withx=tan(pi/2*u)
.
quadcc
is capable of dealing with non-numeric values of the integrand such asNaN
,Inf
or-Inf
, as in the above example at x=0. If the integral diverges andquadcc
detects this, a warning is issued andInf
or-Inf
is returned.Note that
quadcc
is a general purpose quadrature algorithm and as such may be less efficient for smooth or otherwise well-behaved integrand than other methods such asquadgk
ortrapz
.See also: quad, quadv, quadl, quadgk, trapz, dblquad, triplequad.
Numerical integration using trapezoidal method.
trapz (
y)
computes the integral of the y along the first non-singleton dimension. If the argument x is omitted a equally spaced vector is assumed.trapz (
x,
y)
evaluates the integral with respect to x.See also: cumtrapz.
Cumulative numerical integration using trapezoidal method.
cumtrapz (
y)
computes the cumulative integral of the y along the first non-singleton dimension. If the argument x is omitted an equally spaced vector is assumed.cumtrapz (
x,
y)
evaluates the cumulative integral with respect to x.