4.5 matlab三维曲面(mesh、fmesh、meshc、meshz、surf、fsurf、surfc、surfl) | 您所在的位置:网站首页 › 数学中平面和曲面的区别 › 4.5 matlab三维曲面(mesh、fmesh、meshc、meshz、surf、fsurf、surfc、surfl) |
1、平面网格数据的生成 (1)利用矩阵运算生成 (2)利用meshgrid函数生成 [X,Y]=meshgrid(x, y); 其中,参数x、y为向量,存储网格点坐标的X、Y为矩阵。 %方法1 x = 2:6; %五个元素的行向量 y = (3:8)'; %六个元素的列向量 X = ones(size(y))*x; %矩阵X的每一行都是向量x,行数等于向量y的元素的个数 Y = y*ones(size(x)); %矩阵Y的每一行都是向量y,列数等于向量的元素的个数 %结果 >> X = ones(size(y))*x X = 2 3 4 5 6 2 3 4 5 6 2 3 4 5 6 2 3 4 5 6 2 3 4 5 6 2 3 4 5 6 >> Y = y*ones(size(x)) Y = 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 6 6 6 6 6 7 7 7 7 7 8 8 8 8 8 %方法2 x = 2:6; y = (3:8)'; [X,Y] = meshgrid(x,y); %结果 X = 2 3 4 5 6 2 3 4 5 6 2 3 4 5 6 2 3 4 5 6 2 3 4 5 6 2 3 4 5 6 Y = 3 3 3 3 3 4 4 4 4 4 5 5 5 5 5 6 6 6 6 6 7 7 7 7 7 8 8 8 8 8例1:绘制空间曲线 x = 2:6; y = (3:8)'; [X,Y] = meshgrid(x,y); Z = randn(size(X)); %randn函数生成与X同型的矩阵Z,Z的大小也是6行5列 plot3(X,Y,Z) %图形窗口出现5条折线 xlabel('X') ylabel('Y') zlabel('Z') grid on legend()2、绘制三维曲面的函数 mesh(x, y,z,c) mesh函数用于绘制三维网格图 surf(x, y,z,c) surf函数用于绘制三维曲面图,各线条之间的补面用颜色填充 一般情况下xyz是同型矩阵,其中,x、y是网格坐标矩阵,z是网格点上的高度矩阵,c用于指定在不同高度下的曲面颜色。 t = -2:0.2:2; [X,Y] = meshgrid(t); Z = X.*exp(-X.^2-Y.^2); subplot(2,2,1) mesh(X,Y,Z); subplot(2,2,2) surf(X,Y,Z); subplot(2,1,2) plot3(X,Y,Z); grid onmesh函数和surf函数的其他调用格式: mesh(z, c) surf(z, c) 当x、y省略时,z矩阵的第2维下标当作x轴坐标,z矩阵的第1维下标当作y轴坐标。 t = 1:10; z = [0.5*t;2*t;3*t]; mesh(z) title('mesh(z)','fontsize',16)带等高线的三维网格曲面函数meshc 在xy平面上绘制曲面,在z轴方向的等高线 带底座的三维网格曲面函数meshz 在xy平面止绘制曲面的底座 具有等高线的曲面函数surfc 具有光照效果的曲面函数surfl 例3 :用4种方式绘制函数z=(x- 1 ) ^2+(y-2) ^2-1的曲面图,其中x ∈ [0,2],y∈ [1,3]。 [X,Y] = meshgrid(0:0.1:2,1:0.1:3); %生成网格矩阵x,y Z = (X-1).^2+(Y-2).^2-1; %生成高度矩阵z subplot(2,2,1) meshc(X,Y,Z) title('meshc(X,Y,Z)','color','r') subplot(2,2,2) meshz(X,Y,Z) title('meshz(X,Y,Z)','color','r') subplot(2,2,3) surfc(X,Y,Z) title('surfc(X,Y,Z)','color','r') subplot(2,2,4) surfl(X,Y,Z) title('surfl(X,Y,Z)','color','r')3、标准三维曲面 ( 1 ) sphere函数:绘制三维球面 [x, y,z]=sphere(n) sphere函数将新产生3个n加阶的方阵,采用这3个矩阵可以绘制出圆心位于原点,半径为1的单位球体 ( 2 ) cylinder函数:绘制三维柱面 [x, y, z]=cylinder(R, n) 参数R是个向量,存放柱面各个等间隔高度上的半径,n表示在圆桂圆周上有n个间隔点 例4:用cylinder函数分别绘制柱面、花瓶和圆锥面。 subplot(1,3,1) [x,y,z] = cylinder; %不带参数,默认生成2行21列的网格坐标矩阵 surf(x,y,z) %绘制柱面 title('柱面','color','r','fontsize',20) subplot(1,3,2) t = linspace(0,2*pi,40); [x,y,z] = cylinder(2+cos(t),30); %向量有40个元素,生成40行31列的网格坐标矩阵 surf(x,y,z) %绘制花瓶 title('花瓶','color','r','fontsize',20) subplot(1,3,3) [x,y,z] = cylinder(0:0.2:2,30); surf(x,y,z) %绘制圆锥面 title('圆锥面','color','r','fontsize',20)例5 :用cylinder数绘制两个相互垂直且直径相等的圆柱面的相交图形。 [x,y,z] = cylinder(1,60); z = [-1*z(2,:);z(2,:)]; surf(x,y,z) hold on surf(y,z,x) axis equal |
CopyRight 2018-2019 实验室设备网 版权所有 |