compute numerical gradient
gx = gradient(f) [gx, gy] = gradient(f) [gx, gy, ..., gn] = gradient(f) gx = gradient(f, h) [gx, gy, ..., gn] = gradient(f, h) [gx, gy, ..., gn] = gradient(f, hx, hy, ..., hn)
matrix of doubles, representing sampled data
real scalar or vector, corresponding to step size(s) along each dimension. Default is 1.
matrix of doubles, corresponding to the gradient of f with respect to each dimension. Each matrix has the same size as f.
The gradient function computes the partial derivatives of a matrix f using central differences for interior points and first-order differences at the boudaries.
gx = gradient(f) returns the gradient along rows, i.e x-direction. The step between each point of f is equal to 1.
[gx, gy] = gradient(f) returns two matrices: gx
gradient along rows, i.e x-direction and gy, gradient along columns y-direction.
The step between each point of f is equal to 1.
[gx, gy, ..., gn] = gradient(f) returns n matrices if f is a n-dimensional matrix.
If h is provided, it scales the differences accordingly. It can be:
2D gradient
Gradient with step size
[x, y] = meshgrid(-2:0.5:2, -2:0.5:2); f = x.^2 + y.^2; [gx, gy] = gradient(f, 0.5); // at point x0 = 1 and y0 = 1; t = x == 1 & y == -1 // exact value of the gradient at the point (1,-1) is [2 -2] [gx(t), gy(t)] | ![]() | ![]() |
| Version | Description |
| 2026.0.0 | Function added. |