%% Plot Gradient and Contours
% Plot the gradient and contours of the function z = xe^{-x^2-y^2}. Use the quiver function to plot the gradient and the contour function to plot the contours.
% First, create a grid of x- and y-values that are equally spaced. Use them to calculate z. Then, find the gradient of z by specifying the spacing between points.

spacing = 0.2;
[X,Y] = meshgrid(-2:spacing:2);
Z = X.*exp(-X.^2 - Y.^2);
[DX,DY] = gradient(Z,spacing);

% Display the gradient vectors as a quiver plot. Then, display contour lines in the same axes. Adjust the display so that the gradient vectors appear perpendicular to the contour lines by calling axis equal.

quiver(X,Y,DX,DY)
hold on
contour(X,Y,Z)
axis equal
hold off

% Copyright 2020 The MathWorks, Inc.