Skip to content

gradient

general · scripts/general/gradient.m

: fx = gradient (f)
: [fx, fy, fz, …] = gradient (f)
: […] = gradient (f, h)
: […] = gradient (f, hx, hy, …)

Numerical gradient of the sampled array f.

The first output differentiates along dimension 2 (the columns, "x"), the second along dimension 1 (the rows, "y") and output k >= 3 along dimension k. Interior points use the central difference (f(k+1) - f(k-1)) / (x(k+1) - x(k-1)), the end points the one-sided difference. A dimension of length one has derivative zero. A vector with a single output is differentiated along its length.

The spacing arguments are either a scalar (uniform spacing) or a vector of sample coordinates with one entry per element of f along the respective dimension. A single spacing argument applies to every dimension; the default spacing is 1.

As an extension, gradient (fcn, x0) and gradient (fcn, x0, delta) return the numerical derivative of the function handle fcn at the points x0.