quadgk
quadgk(f, a,b,c...; reltol=sqrt(eps), abstol=0, maxevals=10^7, order=7, norm=vecnorm)
Numerically integrate the function f(x)
from a
to b
, and optionally over additional intervals b
to c
and so on. Keyword options include a relative error tolerance reltol
(defaults to sqrt(eps)
in the precision of the endpoints), an absolute error tolerance abstol
(defaults to 0), a maximum number of function evaluations maxevals
(defaults to 10^7
), and the order
of the integration rule (defaults to 7).
Returns a pair (I,E)
of the estimated integral I
and an estimated upper bound on the absolute error E
. If maxevals
is not exceeded then E <= max(abstol, reltol*norm(I))
will hold. (Note that it is useful to specify a positive abstol
in cases where norm(I)
may be zero.)
The endpoints a
etcetera can also be complex (in which case the integral is performed over straightline segments in the complex plane). If the endpoints are BigFloat
, then the integration will be performed in BigFloat
precision as well (note: it is advisable to increase the integration order
in rough proportion to the precision, for smooth integrands). More generally, the precision is set by the precision of the integration endpoints (promoted to floatingpoint types).
The integrand f(x)
can return any numeric scalar, vector, or matrix type, or in fact any type supporting +
, 
, multiplication by real values, and a norm
(i.e., any normed vector space). Alternatively, a different norm can be specified by passing a norm
like function as the norm
keyword argument (which defaults to vecnorm
).
[Only onedimensional integrals are provided by this function. For multidimensional integration (cubature), there are many different algorithms (often much better than simple nested 1d integrals) and the optimal choice tends to be very problemdependent. See the Julia externalpackage listing for available algorithms for multidimensional integration or other specialized tasks (such as integrals of highly oscillatory or singular functions).]
The algorithm is an adaptive GaussKronrod integration technique: the integral in each interval is estimated using a Kronrod rule (2*order+1
points) and the error is estimated using an embedded Gauss rule (order
points). The interval with the largest error is then subdivided into two intervals and the process is repeated until the desired error tolerance is achieved.
These quadrature rules work best for smooth functions within each interval, so if your function has a known discontinuity or other singularity, it is best to subdivide your interval to put the singularity at an endpoint. For example, if f
has a discontinuity at x=0.7
and you want to integrate from 0 to 1, you should use quadgk(f, 0,0.7,1)
to subdivide the interval at the point of discontinuity. The integrand is never evaluated exactly at the endpoints of the intervals, so it is possible to integrate functions that diverge at the endpoints as long as the singularity is integrable (for example, a log(x)
or 1/sqrt(x)
singularity).
For realvalued endpoints, the starting and/or ending points may be infinite. (A coordinate transformation is performed internally to map the infinite interval to a finite one.)
Examples
In the Julia programming language, the function quadgk(f, a, b, c...; reltol=sqrt(eps), abstol=0, maxevals=10^7, order=7, norm=vecnorm)
Numerically integrate the function f(x)
from a
to b
, and optionally over additional intervals b
to c
and so on. Keyword options include a relative error tolerance reltol
(defaults to sqrt(eps)
in the precision of the endpoints), an absolute error tolerance abstol
(defaults to 0), a maximum number of function evaluations maxevals
(defaults to 10^7
), and the order
of the integration rule (defaults to 7).
Returns a pair (I, E)
of the estimated integral I
and an estimated upper bound on the absolute error E
. If maxevals
is not exceeded, then E <= max(abstol, reltol * norm(I))
will hold. (Note that it is useful to specify a positive abstol
in cases where norm(I)
may be zero.)
The endpoints a
, b
, etc., can also be complex (in which case the integral is performed over straightline segments in the complex plane). If the endpoints are BigFloat
, then the integration will be performed in BigFloat
precision as well (note: it is advisable to increase the integration order
in rough proportion to the precision, for smooth integrands). More generally, the precision is set by the precision of the integration endpoints (promoted to floatingpoint types).
The integrand f(x)
can return any numeric scalar, vector, or matrix type, or in fact any type supporting +
, 
, multiplication by real values, and a norm
(i.e., any normed vector space). Alternatively, a different norm can be specified by passing a norm
like function as the norm
keyword argument (which defaults to vecnorm
).
The algorithm is an adaptive GaussKronrod integration technique: the integral in each interval is estimated using a Kronrod rule (2 * order + 1
points) and the error is estimated using an embedded Gauss rule (order
points). The interval with the largest error is then subdivided into two intervals, and the process is repeated until the desired error tolerance is achieved.
These quadrature rules work best for smooth functions within each interval, so if your function has a known discontinuity or other singularity, it is best to subdivide your interval to put the singularity at an endpoint. For example, if f
has a discontinuity at x=0.7
and you want to integrate from 0 to 1, you should use quadgk(f, 0, 0.7, 1)
to subdivide the interval at the point of discontinuity. The integrand is never evaluated exactly at the endpoints of the intervals, so it is possible to integrate functions that diverge at the endpoints as long as the singularity is integrable (for example, a log(x)
or 1/sqrt(x)
singularity).
For realvalued endpoints, the starting and/or ending points may be infinite. (A coordinate transformation is performed internally to map the infinite interval to a finite one.)
Note: Only onedimensional integrals are provided by this function. For multidimensional integration (cubature), there are many different algorithms (often much better than simple nested 1d integrals), and the optimal choice tends to be very problemdependent. See the Julia externalpackage listing for available algorithms for multidimensional integration or other specialized tasks (such as integrals of highly oscillatory or singular functions).
Now, let's see some common examples of using quadgk
:

Integrate a simple function:
julia> f(x) = x^2 + 2x  1; julia> quadgk(f, 0, 2) (9.333333333333332, 1.1102230246251565e15)
This example integrates the function
f(x) = x^2 + 2x  1
over the interval [0, 2]. 
Integrate a complex function:
julia> using ComplexNumbers julia> g(z) = cos(z); julia> quadgk(g, 0, 1im) (0.30116867893975674 + 0.5403023058681398im, 1.2212453270876722e16)
Here, we integrate the complex function
g(z) = cos(z)
over the interval [0, i].  Specify custom error tolerance and maximum number of evaluations:
julia> h(x) = exp(x) / sqrt(x); julia> quadgk(h, 0, Inf, abstol=1e6, maxevals=10^6) (0.8862269254527579, 9.905583889082726e7)
In this example, we integrate the function
h(x) = exp(x) / sqrt(x)
over the interval [0, Inf], specifying a custom absolute error tolerance and a maximum of 10^6 function evaluations.
These examples should give you a good starting point for using the quadgk
function in Julia. Experiment with different functions and options to suit your specific integration needs.
See Also
User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.