MATLAB 三维曲面
平面网格数据的生成
1.利用矩阵运算生成
x=2:6;
y=(3:8)';
X=ones(size(y))*x;
Y=y*ones(size(x));
2.利用meshgrid函数生成
[X,Y]=meshgrid(x,y);
x=2:6;
y=(3:8)';
[X,Y]=meshgrid(x,y);
eg:绘制空间曲线
x=2:6;
y=(3:8)';
[X,Y]=meshgrid(x,y);
Z=randn(size(X));
plot3(X,Y,Z);
grid on;
eg:
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
eg:
t=1:5;
z=[0.5*t;2*t;3*t];
mesh(z);
%带等高线的三维网络曲面函数meshc %带底座的三维网络曲面函数meshz %具有等高线的曲面函数surfc %具有光照效果的曲面函数surfl
eg:
[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('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)')
标准三维曲面:
%sphere函数 [x,y,z]=sphere(n) %cylinder函数 [x,y,z]=cylinder(R,n)
eg:用cylinder函数分别绘制柱面,花瓶和圆锥面。
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);
eg:用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
eg:(peaks函数)
p1=peaks(10);
p2=peaks;
p3=peaks(-3:0.2:3);
[x,y]=meshgrid(-2:0.1:2,0:0.1:5);
p4=peaks(x,y);
%fsurf函数和fmesh函数 %fsurf(funx,funy,funz,uvlims) %fmesh(funx,funy,funz,uvlims)
eg:
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,5,-2,2])
hold off