
clear;clc;close all
[X,Y] = meshgrid(-:.:); % 产生网格数据X和Y
Z = X.*exp(-X.^ - Y.^); % 计算网格点处曲面上的Z值
[DX,DY] = gradient(Z,0.2,0.2); % 计算曲面上各点处的梯度
figure;
contour(X,Y,Z) ; % 绘制等高线
title('等高线') figure;
quiver(X,Y,DX,DY) ; % 绘制梯度场
title('梯度场') figure;
contour(X,Y,Z) ; % 绘制等高线
hold on
quiver(X,Y,DX,DY) ; % 绘制梯度场
h = get(gca,'Children'); % 获取当前axes对象的所有子对象的句柄
set(h, 'Color','k'); % 设置当前axes对象的所有子对象的颜色为黑色