Skip to Main Content Skip to Search
Product Documentation

gradient - Numerical gradient

Syntax

FX = gradient(F)
[FX,FY] = gradient(F)
[FX,FY,FZ,...] = gradient(F)
[...] = gradient(F,h)
[...] = gradient(F,h1,h2,...)

Definitions

The gradient of a function of two variables, F(x,y), is defined as

and can be thought of as a collection of vectors pointing in the direction of increasing values of F. In MATLAB software, numerical gradients (differences) can be computed for functions with any number of variables. For a function of N variables, F(x,y,z, ...),

Description

FX = gradient(F) where F is a vector returns the one-dimensional numerical gradient of F. FX corresponds to ∂F/∂x, the differences in x (horizontal) direction.

[FX,FY] = gradient(F) where F is a matrix returns the x and y components of the two-dimensional numerical gradient. FX corresponds to ∂F/∂x, the differences in x (horizontal) direction. FY corresponds to ∂F/∂y, the differences in the y (vertical) direction. The spacing between points in each direction is assumed to be one.

[FX,FY,FZ,...] = gradient(F) where F has N dimensions returns the N components of the gradient of F. There are two ways to control the spacing between values in F:

[...] = gradient(F,h) where h is a scalar uses h as the spacing between points in each direction.

[...] = gradient(F,h1,h2,...) with N spacing parameters specifies the spacing for each dimension of F.

Examples

The statements

v = -2:0.2:2;
[x,y] = meshgrid(v);
z = x .* exp(-x.^2 - y.^2);
[px,py] = gradient(z,.2,.2);
contour(v,v,z), hold on, quiver(v,v,px,py), hold off

produce

Given,

F(:,:,1) = magic(3); F(:,:,2) = pascal(3);
gradient(F)

takes dx = dy = dz = 1.

[PX,PY,PZ] = gradient(F,0.2,0.1,0.2) 

takes dx = 0.2, dy = 0.1, and dz = 0.2.

See Also

del2 | diff

  


» Learn more
» Download free kit
» Get trial software

 © 1984-2012- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS