Note: This page has been translated by MathWorks. Please click here

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

To view all translated materals including this page, select Japan 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*/∂

`1`

.`[`

returns the `FX`

,`FY`

]
= gradient(`F`

)* x* and

`F`

. The additional output `FY`

corresponds
to ∂`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.

Was this topic helpful?