Navigation

Operators and Keywords

Function List:

C++ API

: dx = gradient (m)
: [dx, dy, dz, …] = gradient (m)
: […] = gradient (m, s)
: […] = gradient (m, x, y, z, …)
: […] = gradient (f, x0)
: […] = gradient (f, x0, s)
: […] = gradient (f, x0, x, y, …)

Calculate the gradient of sampled data or a function.

If m is a vector, calculate the one-dimensional gradient of m. If m is a matrix the gradient is calculated for each dimension.

[dx, dy] = gradient (m) calculates the one-dimensional gradient for x and y direction if m is a matrix. Additional return arguments can be use for multi-dimensional matrices.

A constant spacing between two points can be provided by the s parameter. If s is a scalar, it is assumed to be the spacing for all dimensions. Otherwise, separate values of the spacing can be supplied by the x, … arguments. Scalar values specify an equidistant spacing. Vector values for the x, … arguments specify the coordinate for that dimension. The length must match their respective dimension of m.

At boundary points a linear extrapolation is applied. Interior points are calculated with the first approximation of the numerical gradient

y'(i) = 1/(x(i+1)-x(i-1)) * (y(i-1)-y(i+1)).

If the first argument f is a function handle, the gradient of the function at the points in x0 is approximated using central difference. For example, gradient (@cos, 0) approximates the gradient of the cosine function in the point x0 = 0. As with sampled data, the spacing values between the points from which the gradient is estimated can be set via the s or dx, dy, … arguments. By default a spacing of 1 is used.

See also: diff, del2.

Package: octave