gradient

Compute the gradient of a real function or sampled data.

Syntax

dx = gradient(yc,xc)

dx = gradient(m)

[dx,dy,...] = gradient(m)

[...] = gradient(m,d)

[...] = gradient(m,x,y,...)

[...] = gradient(f,p)

[...] = gradient(f,p,d)

[...] = gradient(f,p,x,y,...)

Inputs

yc
Sampled y coordinate data from which to compute the gradient, paired with xc.
A vector can be either a row or a column. For a 2D matrix, gradients are computed by row.
The gradients at the first and last points are computed with forward and backward differences, respectively. The other points use central differences.
Type: double
Dimension: vector | matrix
xc
Sampled x coordinate data from which to compute the gradient, paired with yc.
A vector is used for each computed gradient vector in yc. A 2D matrix must have the same dimensions as yc.
Type: double
Dimension: vector | matrix
m
A matrix of sampled data from which to compute the gradient.
Data with two or more dimensions is stored by column. One dimensional data can be stored either as a row or a column.
The gradients at the first and last points are computed with forward and backward differences, respectively. The other points use central differences.
The first and second dimensions are assigned to 'y' and 'x', respectively. This convention is so that the 'x' axis is horizontal.
Type: double
Dimension: matrix
f
The function for which to compute the gradient.
The function must operate on vector arguments for each coordinate. See the example below.
Type: char | string | handle
p
The points at which to compute the gradient when using f.
Data with two or more dimensions is stored by column. One dimensional data can be stored either as a row or a column.
Type: double
Dimension: matrix
d
One half of the central difference interval, used in all dimensions.
The default is 1.
Type: double
Dimension: scalar
x,y,...
Each value is one half of the central difference interval used in its corresponding dimension.
The default for each value is 1.
Type: double
Dimension: scalar

Outputs

dx,dy,...
The gradient vector components for each dimension.
Type: double

Example

Function input with a common interval size for all dimensions:
function obj = Func(x,y)
    obj = 2*(x-3).^2 - 5*(x-3).*(y-2) + 4*(y-2).^2 + 6;
end

points = [2,5;3,6;4,7];
[dx,dy] = gradient(@Func, points, 0.001)
dx = [Matrix] 3 x 1
-19.00000
-20.00000
-21.00000
dy = [Matrix] 3 x 1
29.00000
32.00000
35.00000
Matrix data input wtih separate interval sizes for each dimension:
m = [1 125 729 2197; 8 216 1000 2744; 27 343 1331 3375; 64 512 1728 4096];
[dx,dy] = gradient(m, 0.1, 0.2)
dx = [Matrix] 4 x 4
1240  3640  10360  14680
2080  4960  12640  17440
3160  6520  15160  20440
4480  8320  17920  23680
dy = [Matrix] 4 x 4
 35  455  1355  2735
 65  545  1505  2945
140  740  1820  3380
185  845  1985  3605
Matrix inputs with (x,y) coordinate data:
yc = [1, 4, 9, 16; 25, 36, 49, 64];
xc = [1, 2, 3, 4];
dx = gradient(yc, xc)
dx = [Matrix] 2 x 4
 3   4   6   7
11  12  14  15