% example illustrating the gradient operator
clear;clf
[x,y] =meshgrid(-2: .2: 2, -2: .2: 2);
[xx,yy] =meshgrid(-2: .4: 2, -2: .4: 2);
z=exp(-x.^2-y.^2);
zz=exp(-xx.^2-yy.^2);
surf(x,y,z)
xlabel('$$x$$','Interpreter','Latex','Fontsize',14)
ylabel('$$y$$','Interpreter','Latex','Fontsize',14)
zlabel('$$f(x,y)$$','Interpreter','Latex','Fontsize',14)
title('$$f(x,y) = e^{-(x^2+y^2)}$$','Interpreter','Latex','Fontsize',14)
figure;
contour(x,y,z, 5)
hold on
[u,v] = gradient (zz, .2);
quiver(xx,yy,u,v)
xlabel('$$x$$','Interpreter','Latex','Fontsize',14)
ylabel('$$y$$','Interpreter','Latex','Fontsize',14)
title('grad$$f(x,y)$$','Interpreter','Latex','Fontsize',14)
hold off