Combine Contour Plot and Quiver Plot
Display contour lines and gradient vectors on the same plot.
Plot 10 contours of over a grid from -2 to 2 in thexandydirections.
[X,Y] = meshgrid(-2:0.2:2); Z = X .* exp(-X.^2 - Y.^2); contour(X,Y,Z,10)
Calculate the 2-D gradient ofZ
using thegradient
function. Thegradient
function returnsU
as the gradient in thex-direction andV
as the gradient in they-direction. Display arrows indicating the gradient values using thequiver
function.
[U,V] = gradient(Z,0.2,0.2); holdonquiver(X,Y,U,V) holdoff