Contenido principal

integral2

Numerically evaluate double integral

Description

q = integral2(fun,xmin,xmax,ymin,ymax) approximates the integral of the function z = fun(x,y) over the planar region xmin ≤ x ≤ xmax and ymin(x) ≤ y ≤ ymax(x).

example

q = integral2(fun,xmin,xmax,ymin,ymax,Name=Value) specifies options using one or more name-value arguments. For example, you can specify the absolute and relative error tolerances and the integration method.

example

Examples

collapse all

The following function is undefined when x and y are zero. integral2 performs best when singularities are on the integration boundary.

f(x,y)=1(x+y)(1+x+y)2

Create the function.

fun = @(x,y) 1./(sqrt(x+y).*(1+x+y).^2);

Integrate over the triangular region bounded by 0x1 and 0y1-x.

ymax = @(x) 1 - x;
q = integral2(fun,0,1,0,ymax)
q = 
0.2854

Create this function.

f(θ,r)=rrcosθ+rsinθ(1+rcosθ+rsinθ)2

fun = @(x,y) 1./(sqrt(x+y).*(1+x+y).^2);
polarfun = @(theta,r) fun(r.*cos(theta),r.*sin(theta)).*r;

Create a function for the upper limit of r.

rmax = @(theta) 1./(sin(theta)+cos(theta));

Integrate over the region bounded by 0θπ/2 and 0rrmax.

q = integral2(polarfun,0,pi/2,0,rmax)
q = 
0.2854

Create the parameterized function f(x,y)=ax2+by2 with parameters a=3 and b=5.

a = 3; 
b = 5;
fun = @(x,y) a*x.^2+b*y.^2;

Evaluate the integral over the region bounded by 0x5 and -5y0. Specify the "iterated" method and approximately 10 significant digits of accuracy.

format long
q = integral2(fun,0,5,-5,0,Method="iterated",AbsTol=0,RelTol=1e-10)
q = 
     1.666666666666667e+03

For more information on this technique, see Parameterizing Functions.

Since R2026a

Create the function f(x,y)=ln(x2+y2) assuming scalar inputs.

fun = @(x,y) log(x^2+y^2);

Integrate over the region bounded by 0x2 and 0y2. Specify the Vectorized name-value argument as false to compute the double integral without using vectorization.

q = integral2(fun,0,2,0,2,Vectorized=false)
q = 
2.6010

Input Arguments

collapse all

Integrand, specified as a function handle that defines the function to be integrated. The function z = fun(x,y) must accept two arrays of the same size and return an array of corresponding values. The function must perform element-wise operations.

Lower limit of x, specified as a real number that is either finite or infinite.

Data Types: double | single

Upper limit of x, specified as a real number that is either finite or infinite.

Data Types: double | single

Lower limit of y, specified as a real number that is either finite or infinite, or a function handle. You can specify ymin as a function handle (a function of x) when integrating over a nonrectangular region.

Data Types: double | single | function_handle

Upper limit of y, specified as a real number that is either finite or infinite, or a function handle. You can specify ymax as a function handle (a function of x) when integrating over a nonrectangular region.

Data Types: double | single | function_handle

Name-Value Arguments

collapse all

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: q = integral2(fun,xmin,xmax,ymin,ymax,AbsTol=1e-12) sets the absolute error tolerance to approximately 12 decimal places of accuracy.

Absolute error tolerance, specified as a nonnegative real number. integral2 uses the absolute error tolerance to limit an estimate of the absolute error, |qQ|, where q is the computed value of the integral and Q is the (unknown) exact value. integral2 might provide more decimal places of precision if you decrease the absolute error tolerance.

Note

AbsTol and RelTol work together. integral2 might satisfy the absolute error tolerance or the relative error tolerance, but not necessarily both. For more information on using these tolerances, see the Tips section.

Example: q = integral2(fun,xmin,xmax,ymin,ymax,AbsTol=1e-12)

Data Types: double | single

Relative error tolerance, specified as a nonnegative real number. integral2 uses the relative error tolerance to limit an estimate of the relative error, |qQ|/|Q|, where q is the computed value of the integral and Q is the (unknown) exact value. integral2 might provide more significant digits of precision if you decrease the relative error tolerance.

Note

RelTol and AbsTol work together. integral2 might satisfy the relative error tolerance or the absolute error tolerance, but not necessarily both. For more information on using these tolerances, see the Tips section.

Example: q = integral2(fun,xmin,xmax,ymin,ymax,RelTol=1e-9)

Data Types: double | single

Integration method, specified as one of the values in this table.

Integration MethodDescription
"auto"integral2 chooses whether to use the "tiled" or "iterated" method. For most cases, integral2 uses the "tiled" method. integral2 uses the "iterated" method when any of the integration limits are infinite.
"tiled"integral2 transforms the region of integration to a rectangular shape and subdivides it into smaller rectangular regions as needed. The integration limits must be finite.
"iterated"integral2 calls integral to perform an iterated integral. The outer integral is evaluated over xmin ≤ x ≤ xmax. The inner integral is evaluated over ymin(x) ≤ y ≤ ymax(x). The integration limits can be infinite.

Since R2026a

Perform vectorized computation, specified as a numeric or logical 1 (true) or 0 (false). By default, Vectorized is true and the computation of the integral is vectorized to run faster. The integrand function fun must accept two arrays of the same size and operates element-wise, returning an array of corresponding values. If the lower or upper limit of y is a function handle, ymin or ymax must accept a vector argument and return a vector of corresponding values.

If you specify Vectorized as false, then fun, ymin, and ymax accept only scalar arguments and return scalar values.

Output Arguments

collapse all

Computed value of the double integral, returned as a numeric scalar or array.

Tips

  • The integral2 function attempts to satisfy the following expression, where q is the computed value of the integral and Q is the (unknown) exact value.

    abs(q - Q) <= max(AbsTol,RelTol*abs(q))
    The absolute and relative tolerances provide a way of trading off accuracy and computation time. Usually, the relative tolerance determines the accuracy of the integration. However, if abs(q) is sufficiently small, the absolute tolerance determines the accuracy of the integration. It is a best practice to specify both absolute and relative tolerances together.

  • The "iterated" method can be more effective when your function has discontinuities within the integration region. However, the best performance and accuracy occurs when you split the integral at the points of discontinuity and sum the results of multiple integrations.

  • When you integrate over nonrectangular regions, the best performance and accuracy occurs when ymin, ymax, or both are function handles. Avoid setting integrand function values to zero to integrate over a nonrectangular region. If you must do so, specify the "iterated" method.

  • Use the "iterated" method when ymin, ymax, or both are unbounded functions.

  • When you parameterize anonymous functions, parameter values persist for the life of the function handle. For example, the function fun = @(x,y) x + y + a uses the value of a at the time fun was created. If you later decide to change the value of a, you must redefine the anonymous function with the new value.

  • If you specify single-precision limits of integration, or if fun returns single-precision results, you might need to specify larger absolute and relative error tolerances.

References

[1] Shampine, L.F. “Vectorized Adaptive Quadrature in MATLAB®.” Journal of Computational and Applied Mathematics 211, no. 2 (February 2008): 131–40. https://doi.org/10.1016/j.cam.2006.11.021.

[2] Shampine, L.F. "MATLAB Program for Quadrature in 2D." Applied Mathematics and Computation 202, no. 1 (August 2008): 266–74. https://doi.org/10.1016/j.amc.2008.02.012.

Extended Capabilities

expand all

Version History

Introduced in R2012a

expand all