% Plot f(x,y) = x e^(-x^2 - y^2) with contours and its gradient as arrows. x = linspace(-2, 2, 30); y = linspace(-2, 2, 30); [X,Y] = meshgrid(x,y); F = X .* exp(-X.^2 - Y.^2); % evaluate f(x,y) on mesh contour(x,y,F); % draw contour plot of f % let dfdy = y component of grad F = df/dy dfdx = (1-2*X.^2) .* exp(-X.^2 - Y.^2); % evaluate dfdx on mesh dfdy = -2*X.*Y .* exp(-X.^2 - Y.^2); % evaluate dfdy on mesh hold on quiver(x,y,dfdx,dfdy); % draw quiver plot xlabel('x') ylabel('y') title('f(x,y) = x e^{-x^2 - y^2} with \nabla f arrow plot') colorbar()