# divergence

Compute divergence of vector field

## Syntax

## Description

computes the numerical
divergence of a 3-D vector field with vector components `div`

= divergence(`X`

,`Y`

,`Z`

,`Fx`

,`Fy`

,`Fz`

)`Fx`

,
`Fy`

, and `Fz`

.

The arrays `X`

, `Y`

, and `Z`

, which
define the coordinates for the vector components `Fx`

,
`Fy`

, and `Fz`

, must be monotonic, but do not need to
be uniformly spaced. `X`

, `Y`

, and `Z`

must be 3-D arrays of the same size, which can be produced by `meshgrid`

.

assumes a default grid of sample points. The default grid points `div`

= divergence(`Fx`

,`Fy`

,`Fz`

)`X`

,
`Y`

, and `Z`

are determined by the expression
`[X,Y,Z] = meshgrid(1:n,1:m,1:p)`

, where ```
[m,n,p] =
size(Fx)
```

. Use this syntax when you want to conserve memory and are not
concerned about the absolute distances between points.

computes the numerical
divergence of a 2-D vector field with vector components `div`

= divergence(`X`

,`Y`

,`Fx`

,`Fy`

)`Fx`

and
`Fy`

.

The matrices `X`

and `Y`

, which define the
coordinates for `Fx`

and `Fy`

, must be monotonic, but do
not need to be uniformly spaced. `X`

and `Y`

must be 2-D
matrices of the same size, which can be produced by `meshgrid`

.

## Examples

## Input Arguments

## More About

## Algorithms

`divergence`

computes the partial derivatives in its definition by using
finite differences. For interior data points, the partial derivatives are calculated using
*central difference*. For data points along the edges, the partial
derivatives are calculated using *single-sided (forward)
difference*.

For example, consider a 2-D vector field **F** that is represented by the matrices `Fx`

and
`Fy`

at locations `X`

and `Y`

with size
`m`

-by-`n`

. The locations are 2-D grids created by
`[X,Y] = meshgrid(x,y)`

, where `x`

is a vector of length
`n`

and `y`

is a vector of length `m`

.
`divergence`

then computes the partial derivatives ∂*F _{x}* /
∂

*x*and ∂

*F*/ ∂

_{y}*y*as

`dFx(:,i) = (Fx(:,i+1) - Fx(:,i-1))/(x(i+1) - x(i-1))`

and`dFy(j,:) = (Fy(j+1,:) - Fy(j-1,:))/(y(j+1) - y(j-1))`

for interior data points.

`dFx(:,1) = (Fx(:,2) - Fx(:,1))/(x(2) - x(1))`

and`dFx(:,n) = (Fx(:,n) - Fx(:,n-1))/(x(n) - x(n-1))`

for data points at the left and right edges.

`dFy(1,:) = (Fy(2,:) - Fy(1,:))/(y(2) - y(1))`

and`dFy(m,:) = (Fy(m,:) - Fy(m-1,:))/(y(m) - y(m-1))`

for data points at the top and bottom edges.

The numerical divergence of the vector field is equal to ```
div = dFx +
dFy
```

.

## Extended Capabilities

## Version History

**Introduced before R2006a**