三维绘图
三维曲线的基本函数
plot3函数
plot3(x1,y1,z1,’选项’)
程序示例:
t=0:pi/50:2*pi;
x=8*cos(t);
y=4*sqrt(2)*sin(t);
z=-4*sqrt(2)*sin(t);
plot3(x,y,z,'p');
title('3D');
text(0,0,0,'origin');
xlabel('x'),ylabel('y'),zlabel('z');
grid;
图示:
三维曲线
meshgrid函数
x=a:dx:b;
y=c:dy:d;
[x,y]=meshgrid(x,y);
绘制三维曲线函数
mesh(x,y,z,c)
surf(x,y,z,c)
程序示例:
x=0:0.1:2*pi;
[x,y]=meshgrid(x);
z=sin(y).*cos(x);
mesh(x,y,z);
xlabel('x'),ylabel('y'),zlabel('z');
title('mesh');
figure(2);
surf(x,y,z);
xlabel('x'),ylabel('y'),zlabel('z');
title('surf');
图示:
标准三维曲面
sphere函数
cylinder函数
[x,y,z]=sphere(n)
[x,y,z]=cylinder(R,n)
程序示例:
t=0:pi/20:2*pi;
[x,y,z]=cylinder(2+sin(t),30);
subplot(1,3,1);
surf(x,y,z);
[x,y,z]=sphere;
subplot(1,3,2);
surf(x,y,z);
axis equal
[x,y,z]=peaks(30);
subplot(1,3,3);
meshz(x,y,z);
图示:
其他三维图形
bar3、 bar3h、 pie3、 fill3、 scatter3、 stem3 、quiver3
用法与二维相同
隐函数绘图
ezplot函数
- 对于函数y=f(x)
ezplot(f):在默认-2π<x<2π绘制y=f(x)的图形,其中f可以是函数表达式或者函数文件名
ezplot(f,[a,b]):在a<x<b绘制y=f(x) - 对于隐函数f(x,y)=0
ezplot(f):在默认-2π<x<2π绘制f(x,y)=0的图形
ezplot(f,[a,b]):在a<x<b绘制f(x,y)=0的图形
ezplot(f,[xmin,xmax,ymin,ymax]):在xmin<x<xmax和ymin<y<ymax绘制f(x,y)=0的图形 - 对于参数方程x=x(t)和y=y(t)
ezplot(x,y):在默认-2π<x<2π绘制x=x(t)和y=y(t)的图形
ezplot(x,y,[tmin,tmax]):在tmin<t<tmax绘制x=x(t)和y=y(t)的图形
程序示例:
subplot(2,2,1);
ezplot('x^2+y^2-9');axis equal
subplot(2,2,2);
ezplot(@(x,y) x^3+y^3-5*x*y+1/5);
subplot(2,2,3);
ezplot('cos(tan(pi*x))',[0,1])
subplot(2,2,4);
ezplot('8*cos(t)','4*sqrt(2)*sin(t)',[0,2*pi])
图示:
隐函数三维绘图
ezcontour、 ezcontourf、 ezmesh 、ezmeshc、 ezplot3 、ezpolar、 ezsurf、 ezsurfc
调用方法基本相同以ezsurf(f)为例
ezsurf(f):绘制z=f(x,y),默认-2π<x<2π,-2π<y<2π
ezsurf(f,[xmin,xmax,ymin,ymax]):在指定的区间绘制曲面z=f(x,y)
ezsurf(x,y,z):在默认区间-2π<x<2π,-2π<y<2π上绘制方程x=x(s,t),y=y(s,t),z=z(s,t)的曲面
ezsurf(x,y,z,[smin,smax,tmin,tmax]):使用指定的区域绘制参数方程曲面
绘制下列曲面:
0≤s≤8,0≤t≤5π
程序示例:
ezsurf(‘exp(-s)*cos(t)’,'exp(-s)sin(t)’,‘t’,[0,8,0,5pi])
图示:
图形修饰处理
view函数
view(az,el)
程序示例:
subplot(2,2,1);
mesh(peaks);
view(-37.5,30);
subplot(2,2,2);
mesh(peaks);
view(0,90);
subplot(2,2,3);
mesh(peaks);
view(90,0);
subplot(2,2,4);
mesh(peaks);
view(-7,-10);
图示:
图形的裁剪
NaN常数
程序示例:
x=0:pi/10:4*pi;
y=sin(x);
i=find(abs(y)>0.5);
x(i)=NaN;
plot(x,y);
图示: