Numerically evaluate double integral
collapse all in page
Syntax
q = integral2(fun,xmin,xmax,ymin,ymax)
q = integral2(fun,xmin,xmax,ymin,ymax,Name,Value)
Description
example
approximatesthe integral of the function q
= integral2(fun,xmin,xmax,ymin,ymax)z = fun(x,y)
overthe planar region xmin
≤x
≤xmax
and ymin(x)
≤y
≤ymax(x)
.
example
specifiesadditional options with one or more q
= integral2(fun,xmin,xmax,ymin,ymax,Name,Value)Name,Value
pairarguments.
Examples
collapse all
Integrate Triangular Region with Singularity at the Boundary
Open Live Script
Consider the function
.
This function is undefined when and are zero. integral2
performs best when singularities are on the integration boundary.
Create the anonymous function.
fun = @(x,y) 1./( sqrt(x + y) .* (1 + x + y).^2 )
fun = function_handle with value: @(x,y)1./(sqrt(x+y).*(1+x+y).^2)
Integrate over the triangular region bounded by and .
ymax = @(x) 1 - x;q = integral2(fun,0,1,0,ymax)
q = 0.2854
Evaluate Double Integral in Polar Coordinates
Define the function
fun = @(x,y) 1./( sqrt(x + y) .* (1 + x + y).^2 );polarfun = @(theta,r) fun(r.*cos(theta),r.*sin(theta)).*r;
Define a function for the upper limit of .
rmax = @(theta) 1./(sin(theta) + cos(theta));
Integrate over the region bounded by and .
q = integral2(polarfun,0,pi/2,0,rmax)
q = 0.2854
Evaluate Double Integral of Parameterized Function with Specific Method and Error Tolerance
Open Live Script
Create the anonymous parameterized function with parameters and .
a = 3; b = 5;fun = @(x,y) a*x.^2 + b*y.^2;
Evaluate the integral over the region and . Specify the 'iterated'
method and approximately 10 significant digits of accuracy.
format longq = integral2(fun,0,5,-5,0,'Method','iterated',...'AbsTol',0,'RelTol',1e-10)
q = 1.666666666666667e+03
Input Arguments
collapse all
fun
— Integrand
function handle
Integrand, specified as a function handle, defines the functionto be integrated over the planar region xmin
≤x
≤xmax
and ymin
(x
)≤y
≤ymax
(x
).The function fun
must accept two arrays of thesame size and return an array of corresponding values. It must performelement-wise operations.
Data Types: function_handle
xmin
— Lower limit of x
real number
Lower limit of x, specified as a real scalarvalue that is either finite or infinite.
Data Types: double
| single
xmax
— Upper limit of x
real number
Upper limit of x, specified as a real scalarvalue that is either finite or infinite.
Data Types: double
| single
ymin
— Lower limit of y
real number | function handle
Lower limit of y, specified as a real scalarvalue that is either finite or infinite. You can specify ymin
tobe a function handle (a function of x) when integratingover a nonrectangular region.
Data Types: double
| function_handle
| single
ymax
— Upper limit of y
real number | function handle
Upper limit of y, specified as a real scalarvalue that is either finite or infinite. You also can specify ymax
tobe a function handle (a function of x) when integratingover a nonrectangular region.
Data Types: double
| function_handle
| single
Name-Value Arguments
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.
Before R2021a, use commas to separate each name and value, and enclose Name
in quotes.
Example: 'AbsTol',1e-12
sets the absoluteerror tolerance to approximately 12 decimal places of accuracy.
AbsTol
— Absolute error tolerance
nonnegative real number
Absolute error tolerance, specified as the comma-separated pairconsisting of 'AbsTol'
and a nonnegative real number. integral2
usesthe absolute error tolerance to limit an estimate of the absoluteerror, |q – Q|, where q isthe computed value of the integral and Q is the(unknown) exact value. integral2
might providemore decimal places of precision if you decrease the absolute errortolerance. The default value is 1e-10
.
Note
AbsTol
and RelTol
worktogether. integral2
might satisfy the absoluteerror tolerance or the relative error tolerance, but not necessarilyboth. For more information on using these tolerances, see the Tips section.
Example: 'AbsTol',1e-12
sets the absoluteerror tolerance to approximately 12 decimal places of accuracy.
Data Types: double
| single
RelTol
— Relative error tolerance
nonnegative real number
Relative error tolerance, specified as the comma-separated pairconsisting of 'RelTol'
and a nonnegative real number. integral2
usesthe relative error tolerance to limit an estimate of the relativeerror, |q – Q|/|Q|,where q is the computed value of the integral and Q isthe (unknown) exact value. integral2
might providemore significant digits of precision if you decrease the relativeerror tolerance. The default value is 1e-6
.
Note
RelTol
and AbsTol
worktogether. integral2
might satisfy the relativeerror tolerance or the absolute error tolerance, but not necessarilyboth. For more information on using these tolerances, see the Tips section.
Example: 'RelTol',1e-9
sets the relative errortolerance to approximately 9 significant digits.
Data Types: double
| single
Method
— Integration method
'auto'
(default) | 'tiled'
| 'iterated'
Integration method, specified as the comma-separated pair consistingof 'Method'
and one of the methods described below.
Integration Method | Description |
---|---|
'auto' | For most cases, integral2 uses the 'tiled' method.It uses the 'iterated' method when any of the integrationlimits are infinite. This is the default method. |
'tiled' | integral2 transforms the region of integrationto a rectangular shape and subdivides it into smaller rectangularregions as needed. The integration limits must be finite. |
'iterated' | integral2 calls integral toperform an iterated integral. The outer integral is evaluated over xmin ≤x ≤xmax . The inner integral is evaluatedover ymin(x) ≤y ≤ymax(x) .The integration limits can be infinite. |
Example: 'Method','tiled'
specifies the tiledintegration method.
Data Types: char
| string
Tips
The
integral2
function attemptsto satisfy:whereabs(q - Q) <= max(AbsTol,RelTol*abs(q))
q
isthe computed value of the integral andQ
is the(unknown) exact value. The absolute and relative tolerances providea way of trading off accuracy and computation time. Usually, the relativetolerance determines the accuracy of the integration. However ifabs(q)
issufficiently small, the absolute tolerance determines the accuracyof the integration. You should generally specify both absolute andrelative tolerances together.The
'iterated'
method can be moreeffective when your function has discontinuities within the integrationregion. However, the best performance and accuracy occurs when yousplit the integral at the points of discontinuity and sum the resultsof multiple integrations.When integrating over nonrectangular regions, thebest performance and accuracy occurs when
ymin
,ymax
,(or both) are function handles. Avoid setting integrand function valuesto zero to integrate over a nonrectangular region. If you must dothis, specify'iterated'
method.Use the
'iterated'
method whenymin
,ymax
,(or both) are unbounded functions.When parameterizing anonymous functions, be aware that parameter values persist for the life of the function handle. For example, the function
fun = @(x,y) x + y + a
uses the value ofa
at the timefun
was created. If you later decide to change the value ofa
, you must redefine the anonymous function with the new value.If you are specifying single-precision limits of integration,or if
fun
returns single-precision results, youmight need to specify larger absolute and relative error tolerances.
References
[1] L.F. Shampine “VectorizedAdaptive Quadrature in MATLAB®,” Journalof Computational and Applied Mathematics, 211, 2008, pp.131–140.
[2] L.F. Shampine, "MATLAB Programfor Quadrature in 2D." Applied Mathematicsand Computation. Vol. 202, Issue 1, 2008, pp. 266–274.
Extended Capabilities
C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.
Usage notes and limitations:
Variable-sizing must be enable when method is
auto
oriterated
.Function handles that return sparse matrix output are not supported.
Generated code issues a warning if the size of the internal storage arrays is not large enough. If a warning occurs, as a workaround, you can try to divide the region of integration into pieces and sum the integrals over each piece.
Thread-Based Environment
Run code in the background using MATLAB® backgroundPool
or accelerate code with Parallel Computing Toolbox™ ThreadPool
.
This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment.
Version History
Introduced in R2012a
See Also
integral | integral3 | trapz
Topics
- Singularity on Interior of Integration Domain
- Parameterizing Functions
- Create Function Handle
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- Deutsch
- English
- Français
- United Kingdom (English)
Contact your local office