D 三维曲线
文章目录
D.a plot3函数
例子:x = [0.2,1.8,2.5]; y = [1.3,2.8,1.1]; z = [0.2,1.2,1.6]; plot3(x,y,z) grid on axis([0,3,1,3,0,2]);
例子:t = linspace(0,10*pi,200); x = sin(t) + t.*cos(t); y = cos(t) - t.*sin(t); z = t; subplot(1,2,1) plot3(x,y,z) grid on subplot(1,2,2) %每隔四个元素取一个 plot3(x(1:4:200),y(1:4:200),z(1:4:200)) grid on
例子:t = 0:0.01:2*pi; t = t'; x = [t,t,t,t,t]; y = [sin(t),sin(t)+1,sin(t)+2,sin(t)+3,sin(t)+4]; z = [t,t,t,t,t]; plot3(x,y,z)
等价于:
例子:t1 = 0:0.01:1.5*pi; t2 =0:0.01:2*pi; t3 = 0:0.01:3*pi; plot3(t1,sin(t1),t1,t2,sin(t2)+1,t2,... t3,sin(t3)+2,t3)
例子:t = 0:pi/50:6*pi; x = cos(t); y = sin(t); z = 2*t; plot3(x,y,z,'p') xlabel('X'); ylabel('Y'); zlabel('Z'); grid on
D.b fplot3函数
例子:xt = @(t)exp(-t/10).*sin(5*t); yt = @(t)exp(-t/10).*cos(5*t); zt = @(t)t; fplot3(xt,yt,zt,[-12,12],'r-.');%红色 点划线
E 三维曲面
E.a 平面网格数据的生成
选定一矩形区域,左下角坐标为(2,3),右上角坐标为(6,8),在x方向分成(6-2)份,y方向分成(8-3)份,将矩阵分成5行4列20个小矩形,交点数目为(6*5)个。用两个矩阵存储每个小点的x,y坐标。矩阵X,Y就是该矩形区域的X-Y平面网格生成矩阵。x = 2:6;; y =(3:8)'; %ones 全1矩阵 X = ones(size(y))*x; Y = y*ones(size(x));
例子:x = 2:6; y = (3:8)'; [X,Y] = meshgrid(x,y); Z = randn(size(X)); plot3(X,Y,Z); grid on;
E.b 绘制三维曲面的mesh函数和surf函数
t = -2:0.2:2;
[X,Y] = meshgrid(t);
Z = X.*exp(-X.^2 - Y.^2);
subplot(1,3,1)
%网格图
mesh(X,Y,Z)
subplot(1,3,2)
%曲面图
surf(X,Y,Z)
subplot(1,3,3)
%线条图
plot3(X,Y,Z);
grid on
颜色沿着z轴变化t = 1:5; z = [0.5*t;2*t;3*t]; mesh(z);
[x,y] = meshgrid(0:0.1:2,1:0.1:3);
z = (x-1).^2+(y-2).^2-1;
subplot(2,2,1);
meshc(x,y,z);
title('meshc(x,y,z)');
subplot(2,2,2);
meshz(x,y,z);
title('meshz(x,y,z)');
subplot(2,2,3);
surfc(x,y,z);
title('surfc(x,y,z)');
subplot(2,2,4);
surfl(x,y,z);
title('surfl(x,y,z)');
E.c 标准三维曲面(sphere;cylinder;peaks)
分别用于绘制球面和柱面。
sphere将产生3个n+1阶的方阵,可以绘制出圆心位于原点,半径为1的单位球体,如果调用该函数时,不带输出参数,则直接绘制球面,n决定球面圆滑程度,默认20,n越大,越光滑。
cyinder函数的R是一个向量,存放柱面各个等间隔高度上的半径,n表示在圆柱圆周上有n个间隔点,默认20个,n越大,越光滑。如果R是一个标量,则生成一个圆柱面。
例子:subplot(1,3,1); [x,y,z] = cylinder; surf(x,y,z); subplot(1,3,2); t = linspace(0,2*pi,40); [x,y,z] = cylinder(2+cos(t),30); surf(x,y,z); subplot(1,3,3); [x,y,z] = cylinder(0:0.2:2,30); surf(x,y,z);
例子:[x,y,z] = cylinder(1,60); z = [-1*z(2,:);z(2,:)]; surf(x,y,z); hold on surf(y,z,x); axis equal
p1 = peaks(10); p2 = peaks; p3 = peaks(-1:0.2:3); [x,y] = meshgrid(-1:0.1:2,0:0.1:5); p4 = peaks(x,y);
subplot(2,2,1); p1 = peaks(10); surf(p1); subplot(2,2,2); p2 = peaks; surf(p2); subplot(2,2,3); p3 = peaks(-1:0.2:3); surf(p3); subplot(2,2,4); [x,y] = meshgrid(-1:0.1:2,0:0.1:5); p4 = peaks(x,y); surf(p4);
peak(n):参数为一个标量,生成n阶方阵
peaks(V)、peaks(x,y):若参数为向量或网格矩阵代入 f ( x , y ) f(x,y) f(x,y),计算得到高度矩阵,用peaks生成的矩阵,可以用于mesh、surf的参数绘制多峰函数曲面图。
E.d fsurf函数和fmesh函数
funx = @(u,v)u.*sin(v);
funy = @(u,v)-u.*cos(v);
funz = @(u,v)v;
fsurf(funx,funy,funz,[-5,5,-5,-2])
hold on
fmesh(funx,funy,funz,[-5,2,-2,2])
hold off
F 图形修饰处理
F.a 视点处理(view)
视点可由方位角和仰角表示。
[x,y] = meshgrid(0:0.1:2,1:0.1:3);
z = (x-1).^2+(y-2).^2-1;
subplot(2,2,1);
mesh(x,y,z);
title('方位角=-37.5{\circ},仰角=30{\circ}');
subplot(2,2,2);
mesh(x,y,z);
view(0,90);
title('方位角=0{\circ},仰角=90{\circ}');
subplot(2,2,3);
mesh(x,y,z);
view(90,0);
title('方位角=90{\circ},仰角=0{\circ}');
subplot(2,2,4);
mesh(x,y,z);
view(-45,-60)
title('方位角=-45{\circ},仰角=-60{\circ}');
view(2):二维角度;
view(3):三维角度
F.b 色彩处理(colormap;shading)
surf(peaks) colormap hot
c = [0,0.2,0.4,0.6,0.8,1]'; %cmap = gray(6); cmap = [c,c,c]; surf(peaks) colormap(cmap)
例子:[x,y,z] = cylinder(pi:-pi/5:0,10); colormap(lines); subplot(1,3,1); surf(x,y,z); shading flat; subplot(1,3,2); surf(x,y,z); shading interp; subplot(1,3,3); surf(x,y,z); %默认着色方式 shading faceted;
F.c 图形的裁剪处理
t = linspace(0,2*pi,100);
x = sin(t);
y = cos(t);
p = y > 0.5;
y(p) = NaN;
plot(x,y)
axis([-1.1,1.1,-1.1,1.1])
axis square
grid on
例子(直径的3/4):
例子:[X,Y,Z] = sphere(60); p = Z > 0.5; Z(p) = NaN; surf(X,Y,Z) axis([-1,1,-1,1,-1,1]) axis equal view(-45,20)