%%
% Demo showing the gradient is orthogonal to contour curves

[x, y, f] = peaks(32);  f = f/5 + 6;
%[x, y, f] = sombrero(32);  f = 4*f + 5;

[fx, fy] = gradient(f);

figure(1); clf;
surf(x, y, f);
grid on
xlabel('x'); ylabel('y'); zlabel('z');
title('z = f(x,y)');

figure(2); clf;
quiver(x, y, 3*fx, 3*fy, 'linewidth', 2)
axis equal
hold on;
contour(x, y, f, 10, 'linewidth', 2, 'linecolor', 'r')
xlabel('x'); ylabel('y');

%%
% Note: the gradient vector is a vector in the x-y plane,
% it points in the direction where the function increases
% most rapidly, but it does not itself point uphill
figure(3); clf;
quiver(x, y, 3*fx, 3*fy, 'linewidth', 2)
hold on;
contour(x, y, f, 10, 'linewidth', 2, 'linecolor', 'r')
grid on
view(3)
zlim([0 10])
pause
xlabel('x'); ylabel('y'); zlabel('z')
%surf(x, y, 5*f + 3)
surf(x, y, f)
zlim([0 10])
%axis equal

