Note: This page has been translated by MathWorks. Click here to see

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Numerical gradient

`FX = gradient(F)`

```
[FX,FY]
= gradient(F)
```

```
[FX,FY,FZ,...,FN]
= gradient(F)
```

`[___] = gradient(F,h)`

`[___] = gradient(F,hx,hy,...,hN)`

returns
the one-dimensional numerical
gradient of vector `FX`

= gradient(`F`

)`F`

. The output `FX`

corresponds
to ∂*F*/∂*x*, which
are the differences in the *x* (horizontal) direction.
The spacing between points is assumed to be `1`

.

`[`

returns the `FX`

,`FY`

]
= gradient(`F`

)*x* and *y* components
of the two-dimensional numerical
gradient of matrix `F`

. The additional output `FY`

corresponds
to ∂*F*/∂*y*, which
are the differences in the *y* (vertical) direction.
The spacing between points in each direction is assumed to be `1`

.

`gradient`

calculates the *central
difference* for interior data points. For example, consider
a matrix with unit-spaced data, `A`

, that has horizontal
gradient `G = gradient(A)`

. The interior gradient
values, `G(:,j)`

, are

G(:,j) = 0.5*(A(:,j+1) - A(:,j-1));

The subscript `j`

varies between `2`

and `N-1`

,
with `N = size(A,2)`

.

`gradient`

calculates values along the edges
of the matrix with *single-sided differences*:

G(:,1) = A(:,2) - A(:,1); G(:,N) = A(:,N) - A(:,N-1);

If you specify the point spacing, then `gradient`

scales
the differences appropriately. If you specify two or more outputs,
then the function also calculates differences along other dimensions
in a similar manner. Unlike the `diff`

function, `gradient`

returns
an array with the same number of elements as the input.