%% Plot Streamlines to Visualize Gradient of a Surface Plot
% First, create a surface plot.

z = peaks;
surf(z);
shading interp;

%
% Compute the two-dimensional gradient of z. Use the x and y components as the vector data input for streamslice. Set the color of the streamlines to black.

[u,v] = gradient(z);
l = streamslice(u,v);
set(l,'Color','k');

%
% Displace the streamlines on the z-axis so that they are drawn on the surface of the curve.

for i=1:length(l)
    zi = interp2(z,l(i).XData, l(i).YData);
    l(i).ZData = zi;
end

% Copyright 2020 The MathWorks, Inc.