# integral2

Numerically evaluate double integral

## Syntax

• `q = integral2(fun,xmin,xmax,ymin,ymax)` example
• `q = integral2(fun,xmin,xmax,ymin,ymax,Name,Value) ` example

## Description

example

````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 additional options with one or more `Name,Value` pair arguments.```

## Examples

collapse all

### Integrate Triangular Region with Singularity at the Boundary

The function

$f\left(x,y\right)=\frac{1}{{\left(\sqrt{x+y}\right)}^{}\left(1+x+y\right)}$

is undefined when x and y 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 )`

Integrate over the triangular region bounded by 0 ≤ x ≤ 1 and 0 ≤ y ≤ 1 – x.

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

### Evaluate Double Integral in Polar Coordinates

Define the function

$f\left(\theta ,r\right)=\frac{r}{{\sqrt{r\mathrm{cos}\theta +r\mathrm{sin}{\theta }^{}}}^{}{\left(1+r\mathrm{cos}\theta +r\mathrm{sin}\theta \right)}^{2}}$

```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 r.

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

Integrate over the region bounded by 0 ≤ θ ≤ π/2 and 0 ≤ r ≤ rmax.

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

### Evaluate Double Integral of Parameterized Function with Specific Method and Error Tolerance

Create the anonymous 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 0 ≤ x ≤ 5 and -5 ≤ y ≤ 0. 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.666666666666666e+03```

## Input Arguments

collapse all

### `fun` — Integrandfunction handle

Integrand, specified as a function handle, defines the function to be integrated over the planar region `xmin` ≤ `x` ≤ `xmax` and `ymin`(`x`) ≤ `y` ≤ `ymax`(`x`). The function `fun` must accept two arrays of the same size and return an array of corresponding values. It must perform element-wise operations.

Data Types: `function_handle`

### `xmin` — Lower limit of xreal number

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

Data Types: `double` | `single`

### `xmax` — Upper limit of xreal number

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

Data Types: `double` | `single`

### `ymin` — Lower limit of yreal number | function handle

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

Data Types: `double` | `function_handle` | `single`

### `ymax` — Upper limit of yreal number | function handle

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

Data Types: `double` | `function_handle` | `single`

### Name-Value Pair Arguments

Specify optional comma-separated pairs of `Name,Value` arguments. `Name` is the argument name and `Value` is the corresponding value. `Name` must appear inside single quotes (`' '`). You can specify several name and value pair arguments in any order as `Name1,Value1,...,NameN,ValueN`.

Example: `'AbsTol',1e-12` sets the absolute error tolerance to approximately 12 decimal places of accuracy.

### `'AbsTol'` — Absolute error tolerancenonnegative real number

Absolute error tolerance, specified as the comma-separated pair consisting of `'AbsTol'` and 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. The default value is `1e-10`.

 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: `'AbsTol',1e-12` sets the absolute error tolerance to approximately 12 decimal places of accuracy.

Data Types: `double` | `single`

### `'RelTol'` — Relative error tolerancenonnegative real number

Relative error tolerance, specified as the comma-separated pair consisting of `'RelTol'` and 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. The default value is `1e-6`.

 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: `'RelTol',1e-9` sets the relative error tolerance to approximately 9 significant digits.

Data Types: `double` | `single`

### `'Method'` — Integration method`'auto'` (default) | `'tiled'` | `'iterated'`

Integration method, specified as the comma-separated pair consisting of `'Method'` and one of the methods described below.

Integration MethodDescription
`'auto'`For most cases, `integral2` uses the `'tiled'` method. It uses the `'iterated'` method when any of the integration limits are infinite. This is the default method.
`'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.

Example: `'Method','tiled'` specifies the tiled integration method.

## Output Arguments

collapse all

### `q` — Computed integralnumeric value

Computed integral of `fun(x,y)` over the specified region, returned as a numeric value.

collapse all

### Tips

• The `integral2` function attempts to satisfy:

`abs(q - Q) <= max(AbsTol,RelTol*abs(q))`
where `q` is the computed value of the integral and `Q` is the (unknown) exact value. 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. You should generally 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 integrating 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 this, specify `'iterated'` method.

• Use the `'iterated'` method when `ymin`, `ymax`, (or both) are unbounded functions.

• When paramaterizing 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 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 are specifying single-precision limits of integration, or if `fun` returns single-precision results, you might need to specify larger absolute and relative error tolerances.