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.
- xc
- Sampled x coordinate data from which to compute the gradient, paired with yc.
- m
- A matrix of sampled data from which to compute the gradient.
- f
- The function for which to compute the gradient.
- p
- The points at which to compute the gradient when using f.
- d
- One half of the central difference interval, used in all dimensions.
- x,y,...
- Each value is one half of the central difference interval used in its corresponding dimension.
Outputs
- dx,dy,...
- The gradient vector components for each dimension.
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