例:用plot3函数绘制三维螺旋线。
>> t = linspace(0, 10*pi, 300);
>> plot3(20*sin(t), 20*cos(t), t, 'r', 'linewidth', 2);
>> hold on %图形保持
>> quiver3(0,0,0,1,0,0,25,'k','filled','LineWidth',2);
>> quiver3(0,0,0,0,1,0,25,'k','filled','LineWidth',2);
>> quiver3(0,0,0,0,0,1,40,'k','filled','LineWidth',2);
>> grid on
>> xlabel('X'); ylabel('Y'); zlabel('Z');
>> axis([-25 25 -25 25 0 40]);
>> view(-210,30);
例:用meshgrid函数生成网格矩阵,并用plot函数画出平面网格图形。
[x,y] = meshgrid(1:4, 2:5)
x =
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
y =
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
>> plot(x, y, 'r',x', y', 'r', x, y, 'k.','markersize',18);
>> axis([0 5 1 6]);
>> xlabel('X'); ylabel('Y');
例:绘制三维曲面
的等高线图和梯度场。
>> [X,Y] = meshgrid(-2:.2:2);
>> Z = X.*exp(-X.^2 - Y.^2);
>> [DX,DY] = gradient(Z,0.2,0.2);
>> contour(X,Y,Z) ;
>> hold on ;
>> quiver(X,Y,DX,DY) ;
>> h = get(gca,'Children');
>> set(h, 'Color','k');
例:用mesh,surf,surfl,surfc函数绘制二元正态分布的密度函数图。
>> x = linspace(-3,3,20);
>> y = linspace(-9,9,20);
>> [X, Y] = meshgrid(x,y);
>> Z = mvnpdf([X(:) Y(:)], [0 0], [1 2;2 9]);
>> Z = reshape(Z, size(X));
>> subplot(2, 2, 1);
>> mesh(X, Y, Z);
>> title('mesh');
>> subplot(2, 2, 2);
>> surf(X, Y, Z);
>> alpha(0.5);
>> title('surf');
>> subplot(2, 2, 3);
>> surfl(X, Y, Z);
>> title('surfl');
>> subplot(2, 2, 4);
>> surfc(X, Y, Z);
>> title('surfc');
例:调用函数绘制柱面,球面,椭球面。 % 绘制圆柱面
>> subplot(2,2,1);
>> [x,y,z] = cylinder;
>> surf(x,y,z);
% 绘制哑铃面
>> subplot(2,2,2);
>> t = 0:pi/10:2*pi;
>> [X,Y,Z] = cylinder(2+cos(t));
>> surf(X,Y,Z);
% 绘制球面,半径为10,球心 (1,1,1)
>> subplot(2,2,3);
>> [x,y,z] = sphere;
>> surf(10*x+1,10*y+1,10*z+1); axis equal;
% 绘制椭球面
>> subplot(2,2,4);
>> a=4; b=3;
>> t = -b:b/10:b;
>> [x,y,z] = cylinder(a*sqrt(1-t.^2/b^2),30);
>> surf(x,y,z);
例:绘制三维饼图、三维柱状图、三维火柴杆图、三维填充图、三维向量场图和立体切片图(四维图).
% 饼图
>> subplot(2,3,1);
>> pie3([2347,1827,2043,3025]);
>> title('三维饼图');
% 柱状图
>> subplot(2,3,2);
>> bar3(magic(4));
>> title('三维柱状图');
% 火柴杆图
>> subplot(2,3,3);
>> y=2*sin(0:pi/10:2*pi);
>> stem3(y);
>> title('三维火柴杆图');
% 填充图
>> subplot(2,3,4);
>> fill3(rand(3,5),rand(3,5),rand(3,5), 'y' );
>> title('三维填充图');
% 三维向量场图
>> subplot(2,3,5);
>> [X,Y] = meshgrid(0:0.25:4,-2:0.25:2);
>> Z = sin(X).*cos(Y);
>> [Nx,Ny,Nz] = surfnorm(X,Y,Z);
>> surf(X,Y,Z);
>> hold on;
>> quiver3(X,Y,Z,Nx,Ny,Nz,0.5);
>> title('三维向量场图');
>> axis([0 4 -2 2 -1 1]);
% 立体切片图(四维图)
>> subplot(2,3,6);
>> t = linspace(-2,2,20);
>> [X,Y,Z] = meshgrid(t,t,t);
>> V = X.*exp(-X.^2-Y.^2-Z.^2);
>> xslice = [-1.2,.8,2];
>> yslice = 2;
>> zslice = [-2,0];
>> slice(X,Y,Z,V,xslice,yslice,zslice);
>> title('立体切片图(四维图)');
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/184417.html原文链接:https://javaforall.cn