What is the definition of the GRADIENT function in MATLAB 6.5 (R13)?

69 views (last 30 days)

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 17 Feb 2021
Edited: MathWorks Support Team on 17 Feb 2021
The algorithm that is used to calculate the partial derivatives for the gradient function is a "one-sided difference scheme on the edges." We selected this scheme over the "central differencing in the interior" due to a slight advantage in speed. Although the central scheme has proven to be slightly more accurate in specific cases, the general advantage is speed.
The following is from the help for GRADIENT:
FX corresponds to dF/dx, the differences in the x (column) direction.
FY corresponds to dF/dy, the differences in the y (row) direction.
help gradient
[FX,FY] = GRADIENT(F) returns the numerical gradient of the
matrix F. FX corresponds to dF/dx, the differences in the
x (column) direction. FY corresponds to dF/dy, the differences
in the y (row) direction. The spacing between points in each
direction is assumed to be one. When F is a vector, DF = GRADIENT(F)
is the 1-D gradient.
Examples:
[x,y] = meshgrid(-2:.2:2, -2:.2:2);
z = x .* exp(-x.^2 - y.^2);
[px,py] = gradient(z,.2,.2);
contour(z),hold on, quiver(px,py), hold off
A short example will help us understand this function:
[x,y] = meshgrid(-1:1, -1:1)
x =
-1 0 1
-1 0 1
-1 0 1
y =
-1 -1 -1
0 0 0
1 1 1
z = x .* exp(x - y.^2)
z =
-0.1353 0 1.0000
-0.3679 0 2.7183
-0.1353 0 1.0000
[px,py] = gradient(z)
px =
0.1353 0.5677 1.0000
0.3679 1.5431 2.7183
0.1353 0.5677 1.0000
py =
-0.2325 0 1.7183
0 0 0
0.2325 0 -1.7183
[FX,FY] = GRADIENT(F) returns the numerical gradient of the matrix F. FX corresponds to dF/dx, the differences in the x (column) direction. FY corresponds to dF/dy, the differences in the y (row) direction.
The "difference in row direction or DF/DY" can be obtained by moving along columns (X).
Notice the difference between "difference in row direction DF/DY" and "row (X)".
You can get the "difference in row direction" by moving in "column". Similarly you can get "difference in column direction" by moving in "row"
Consider the following matrix:
X1 X2 X3
Y1 1 2 -2
Y2 3 4 1
Y3 9 3 6
When you move along any column, you get a variation in Y, and X is fixed. This is DF/DY or "difference in row direction". Similarly for DF/DX. when you move along any row, you get a variation in X, while Y is fixed.
Another example is given below. This produces a plot of the gradient of a function.
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
For more help on the GRADIENT function, please visit the following link:

More Answers (0)

Categories

Find more on ROC - AUC in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!