theta = 0:0.01*pi:2*pi;
x = sin(theta);
y = cos(theta);
z = cos(4*theta);
plot3(x,y,z,'LineWidth',2);
hold on;
theta = 0:0.02*pi:2*pi;
x = sin(theta);
y = cos(theta);
z = cos(4*theta);
plot3(x,y,z,'rd','MarkerSize',10,'LineWidth',2);
绘制函数z=f(x,y)的三维网格图的过程:
matlab中提供了mesh函数用于实现绘制网格图:
[X,Y] = meshgrid(-3:.5:3);
Z = 2 * X.^2-3 * Y.^2;
subplot(2,2,1)
plot3(X,Y,Z)
title('plot3')
subplot(2,2,2)
mesh(X,Y,Z)
title('mesh')
subplot(2,2,3)
meshc(X,Y,Z)
title('meshc')
subplot(2,2,4)
meshz(X,Y,Z)
title('meshz')
set(gcf,'Color','w');
表示三维空间内数据的变化规律。函数有surf、surfc和surfl
[x,y] = meshgrid(-3:1/8:3);
z = peaks(x,y);
subplot(2,2,1);surf(z);
title('surf(z)绘制形式');
subplot(2,2,2);surf(x,y,z);
title('surf(x,y,z)绘图形式');
subplot(2,2,3);surfl(x,y,z);
title('surfl(x,y,z)绘图形式');
subplot(2,2,4);surfc(x,y,z);
title('surfc(x,y,z)绘图形式');
光照是利用方向官员照亮物体的技术,这项技术能使表面微妙的差异更容易看到,光照也能用来对三维的图像增加现实感。
camlight函数:
surf(peaks)
axis vis3d
h = camlight('left');
for i = 1:20;
camorbit(10,0)
camlight(h,'left')
pause(.1)
end
light函数:
% 准备数据
[X,Y] = meshgrid(-1:0.1:1);
Z = sin(X.^2.*pi) + cos(Y.*pi);
% 设置无限远平行光源光照效果
subplot(1,2,1);
surf(X,Y,Z);
light('Style','infinit','Position',[0 -0.6 1]);
title('无限远平行光')
% 设置本地光源辐射源光照效果
subplot(1,2,2);
surf(X,Y,Z);
light('Style','local','Position',[0 -0.6 1]);
title('本地辐射光')
lighting函数:
[x,y,z] = sphere(25);
subplot(2,2,1);surf(x,y,z);
axis equal;shading interp;
hold on;
title('lighting none')
subplot(2,2,2);surf(x,y,z);
axis equal;
light('position',[0,0.5 1]);
shading interp;lighting flat;
hold on;
title('lighting flat');
subplot(2,2,3);surf(x,y,z);
axis equal;
light('position',[0,0.5 1]);
shading interp;lighting gouraud;
hold on;
title('lighting gouraud');
subplot(2,2,4);surf(x,y,z);
axis equal;
light('position',[0,0.5 1]);
shading interp;lighting phong;
hold on;
title('lighting phong');
set(gcf,'color','w');
lightangle函数:
sphere(25);
axis vis3d
h = light;
for az = -50:10:50
lightangle(h,az,30)
pause(.2)
end
等值线图又叫等高线图。默认情况下,MATLAB就是画出了相应于一系列相等的空间Z值得等值线。matlab提供了contour和contour3函数绘制二维和三维的等高线。下面的格式contour换成contour3就是三维的等值线。
MATLAB中,也提供了相应的函数用于实现特殊数的三维绘图。
t = 0:pi/10:2*pi;
[X1,Y1,Z1] = cylinder(2 + cos(t));
subplot(2,3,1);surf(X1,Y1,Z1)
axis square;title('三维柱面图');
subplot(2,3,2);sphere
axis equal;title('三维球体');
x1 = [1 3 0.5 2.5 2];
explode = [0 1 0 0 0];
subplot(2,3,3);pie3(x1,explode)
title('三维饼图');axis equal;
X2 = [0 1 1 2;1 1 2 2;0 0 1 1];
Y2 = [1 1 1 1;1 0 1 0;0 0 0 0];
Z2 = [1 1 1 1;1 0 1 0;0 0 0 0];
C = [0.5000 1.0000 1.0000 0.5000;
1.0000 0.5000 0.5000 0.1667;
0.3330 0.3330 0.5000 0.5000];
subplot(2,3,4);fill3(X2,Y2,Z2,C);
colormap hsv
title('三维填充图');axis equal;
[x2,y2] = meshgrid(-3:.5:3,-3:.1:3);
z2 = peaks(x2,y2);
subplot(2,3,5);ribbon(y2,z2)
colormap hsv
title('三维彩带图');axis equal;
[X3,Y3] = meshgrid(-2:0.25:2,-1:0.2:1);
Z3 = X3 .* exp(-X3.^2 - Y3.^2);
[U,V,W] = surfnorm(X3,Y3,Z3);
subplot(2,3,6);quiver3(X3,Y3,Z3,U,V,W,0.5);
hold on
surf(X3,Y3,Z3);
colormap hsv
view(-35,45);
title('三维向量场图');axis equal;
set(gcf,'Color','w');
从不同的角度观察物体,所看到的物体形状是不一样的。同样,从不同视点绘制的三维图形其形状也是不一样的。
视点的位置可由方位角和仰角表示。方位角又称旋转角,它是视点与原点连线在xy平面上的投影与y轴负方向形成的角度,正值表示逆时针,负值表示顺时针。仰角又称为视角,它是视点与原点连线与xy平面的夹角,正值表示视点在xy平面上方。负值表示视点在xy平面下方。
matlab中提供了view和rotate函数用于设置观察图的视角。view函数用于调整图形的视角效果。
x = -5:0.5:5;
[x,y] = meshgrid(x);
z = x.^2-y.^2-2;
subplot(2,2,1);surf(x,y,z);
view(-38.5,30);
title('方位角为-38.5,俯视角为30');
subplot(2,2,2);surf(x,y,z);
view(-38.5+90,30);
title('方位角为-38.5+90,俯视角为30');
subplot(2,2,3);surf(x,y,z);
view(-38.5,60);
title('方位角-38.5,俯视角为60');
subplot(2,2,4);surf(x,y,z);
view(180,0);
title('方位角为180,俯视角为0');
set(gcf,'color','w'); % 设置图形的背景颜色
rotate函数用于旋转三维图形,函数的格式为
sp11 = subplot(2,2,1);
hll = surf(sp11,peaks(20));
title('无旋转')
sp12 = subplot(2,2,2);
h12 = surf(sp12,peaks(20));
title('绕x轴旋转')
zdir = [1 0 0];
rotate(h12,zdir,25)
sp21 = subplot(2,2,3);
h21 = surf(sp21,peaks(20));
title('绕Y轴旋转')
zdir = [0 1 0];
rotate(h21,zdir,25)
sp22 = subplot(2,2,4);
h22 = surf(sp22,peaks(20));
title('绕X-Y轴旋转')
zidr = [1 1 0];
rotate(h22,zdir,25)
set(gcf,'color','w');
在MATLAB中,提供了meshgrid、slice、contourslice函数,可充分体现四维图形的效果。
1.slice函数
matlab提供了中表现四维空间的方式,即使用色彩,这种方式需要用户调用slice函数来显示“切片”图。
[x,y,z] = meshgrid(-2:.2:2,-2:.25:2,-2:.16:2);
v = x.*exp(-x.^2-y.^2-z.^2);
xslice = [-1.2,.8,2];
yslice = 2;
zslice = [-2,0];
[xsp,ysp,zsp] = sphere;
slice(x,y,z,v,[-2,2],2,-2)
for i = -3:.2:3
hsp = surface(xsp + i,ysp,zsp);
rotate(hsp,[1 0 0],90)
xd = get(hsp,'XData');
yd = get(hsp,'YData');
zd = get(hsp,'ZData');
delete(hsp)
hold on
hslicer = slice(x,y,z,v,xd,yd,zd);
axis tight
xlim([-3,3])
view(-10,35)
drawnow
delete(hslicer)
hold off
end
2.contourslice函数
用于实现三元函数切面等高线的效果图。
contourslice(X,Y,Z,V,Sx,Sy,Sz):X,Y,Z是维数为m x n x p的自变量“格点”数组;V是与X,Y,Z同维的函数值数组;Sx,Sy,Sz是决定切片位置的数值向量。假如取空阵,就表示不取切片。
[x y z v] = flow;
h = contourslice(x,y,z,v,[1:9],[],[0],linspace(-8,2,10));
axis([0,10,-3,3,-3,3]);daspect([1,1,1])
camva(24);
camproj perspective;
campos([-3,-15,5])
set(gcf,'Color',[.5,.5,.5],'Renderer','zbuffer')
set(gca,'Color','black','XColor','white',...
'YColor','white','Zcolor','white')
box on
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/183158.html原文链接:https://javaforall.cn