用矩阵散点做等高线图和梯度场图

matlab提供了可以根据矩阵做等高线的命令contour,和求梯度的函数gradient,可以根据梯度用quiver命令将梯度做成箭头图。但要注意一些参数的设置。

一般情况下,例如:

x=-2:0.2:2;

y=-2:0.2:2;

[X,Y]=meshgrid(x,y);

Z=X.^3+Y.^2;

contour(X,Y,Z);

[DX,DY]=gradient(Z,0.2,0.2);

hold on;

quiver(X,Y,DX,DY);

作图如下:

设置一下不同的坐标,纵坐标的范围为 [-20 20],间距为2。

x=-2:0.2:2;

y=-20:2:20;

[X,Y] = meshgrid(x,y);

Z=X.^3+Y.^2;

contour(X,Y,Z);

[DX,DY]=gradient(Z,0.2,0.2);

hold on;

quiver(X,Y,DX,DY);

也可以让箭头图的密度稀疏一些,x的离散间距设置为0.4,y的离散间距设置为4。

x=-2:0.4:2;

y=-20:4:20;

[X,Y] = meshgrid(x,y);

Z=X.^3+Y.^2;

contour(X,Y,Z);

[DX,DY]=gradient(Z,0.2,0.2);

hold on;

quiver(X,Y,DX,DY);

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *