1、第05节MATLAB绘图例题源程序第5章 MATLAB绘图例5.1 在0x2 区间内,绘制曲线y=2e-0.5xsin(2x)。x=0:pi/100:2*pi;y=2*exp(-0.5*x).*sin(2*pi*x);plot(x,y)例5.2 绘制曲线t=-pi:pi/100:pi;x=t.*cos(3*t);y=t.*sin(t).*sin(t);plot(x,y);例5.3 用不同线型和颜色在同一坐标内绘制曲线y=2e-0.5xsin(2x)及其包络线。x=(0:pi/100:2*pi);y1=2*exp(-0.5*x)*1,-1;y2=2*exp(-0.5*x).*sin(2*pi*x
2、);x1=(0:12)/2;y3=2*exp(-0.5*x1).*sin(2*pi*x1);plot(x,y1,k:,x,y2,b-,x1,y3,rp);例5.4 用不同标度在同一坐标内绘制曲线y1=e-0.5xsin(2x)及曲线y2=1.5e-0.1xsin(x)。x1=0:pi/100:2*pi;x2=0:pi/100:3*pi;y1=exp(-0.5*x1).*sin(2*pi*x1);y2=1.5*exp(-0.1*x2).*sin(x2);plotyy(x1,y1,x2,y2);例5.5 绘制分段函数曲线并添加图形标注。x=linspace(0,10,100);y=;for x0=
3、x if x0=8 y=y,1; elseif x0=6 y=y,5-x0/2; elseif x0=4 y=y,2; elseif x0=0 y=y,sqrt(x0); endendplot(x,y)axis(0 10 0 2.5) %设置坐标轴title(分段函数曲线); %加图形标题xlabel(Variable X); %加X轴说明ylabel(Variable Y); %加Y轴说明text(2,1.3,y=x1/2); %在指定位置添加图形说明text(4.5,1.9,y=2);text(7.3,1.5,y=5-x/2);text(8.5,0.9,y=1);例5.6 用图形保持功能在
4、同一坐标内绘制曲线y=2e-0.5xsin(2x)及其包络线。x=(0:pi/100:2*pi);y1=2*exp(-0.5*x)*1,-1;y2=2*exp(-0.5*x).*sin(2*pi*x);plot(x,y1,b:);axis(0,2*pi,-2,2); %设置坐标hold on; %设置图形保持状态plot(x,y2,k);legend(包络线,包络线,曲线y); %加图例hold off; %关闭图形保持grid %网格线控制例5.7 在一个图形窗口中以子图形式同时绘制正弦、余弦、正切、余切曲线。x=linspace(0,2*pi,60);y=sin(x);z=cos(x);t
5、=sin(x)./(cos(x)+eps);ct=cos(x)./(sin(x)+eps);subplot(2,2,1); plot(x,y);title(sin(x);axis(0,2*pi,-1,1);subplot(2,2,2);plot(x,z);title(cos(x);axis(0,2*pi,-1,1);subplot(2,2,3);plot(x,t);title(tangent(x);axis(0,2*pi,-40,40);subplot(2,2,4);plot(x,ct);title(cotangent(x);axis(0,2*pi,-40,40);请看下面的程序。x=lins
6、pace(0,2*pi,60);y=sin(x);z=cos(x);t=sin(x)./(cos(x)+eps);ct=cos(x)./(sin(x)+eps);subplot(2,2,1); %选择22个区中的1号区stairs(x,y);title(sin(x)-1);axis (0,2*pi,-1,1);subplot(2,1,2); %选择21个区中的2号区stem(x,y);title(sin(x)-2);axis (0,2*pi,-1,1);subplot(4,4,3); %选择44个区中的3号区plot(x,y);title(sin(x);axis (0,2*pi,-1,1);s
7、ubplot(4,4,4); %选择44个区中的4号区plot(x,z);title(cos(x);axis (0,2*pi,-1,1);subplot(4,4,7); %选择44个区中的7号区plot(x,t);title(tangent(x);axis (0,2*pi,-40,40);subplot(4,4,8); %选择44个区中的8号区plot(x,ct);title(cotangent(x);axis (0,2*pi,-40,40);例5.8 分别以条形图、填充图、阶梯图和杆图形式绘制曲线y=2e-0.5x。x=0:0.35:7;y=2*exp(-0.5*x);subplot(2,2
8、,1);bar(x,y,g);title(bar(x,y,g);axis(0,7,0,2); subplot(2,2,2);fill(x,y,r);title(fill(x,y,r);axis(0,7,0,2);subplot(2,2,3);stairs(x,y,b);title(stairs(x,y,b);axis(0,7,0,2);subplot(2,2,4);stem(x,y,k);title(stem(x,y,k);axis(0,7,0,2);例5.9 绘制=sin(2)cos(2)的极坐标图。theta=0:0.01:2*pi;rho=sin(2*theta).*cos(2*thet
9、a);polar(theta,rho,k); 例5.10 绘制y=10x2的对数坐标图并与直角线性坐标图进行比较。x=0:0.1:10;y=10*x.*x;subplot(2,2,1);plot(x,y);title(plot(x,y);grid on;subplot(2,2,2);semilogx(x,y);title(semilogx(x,y);grid on;subplot(2,2,3);semilogy(x,y);title(semilogy(x,y);grid on;subplot(2,2,4);loglog(x,y);title(loglog(x,y);grid on;例5.11
10、用fplot函数绘制f(x)=cos(tan(x)的曲线。先建立函数文件myf.m:function y=myf(x)y=cos(tan(pi*x);再用fplot函数绘制myf.m函数的曲线:fplot(myf,-0.4,1.4,1e-4)得到如图5.12所示曲线。从图5.12中可看出,在x=0.5附近采样点十分密集。也可以直接用fplot函数绘制f(x)=cos(tan(x)的曲线:fplot(cos(tan(pi*x),-0.4,1.4,1e-4)例5.12 绘制图形:(1) 某次考试优秀、良好、中等、及格、不及格的人数分别为:7,17,23,19,5,试用饼图作成绩统计分析。(2) 绘
11、制复数的相量图:3+2i、5.5-i和-1.5+5i。subplot(1,2,1);pie(7,17,23,19,5);title(饼图);legend(优秀,良好,中等,及格,不及格);subplot(1,2,2);compass(3+2i,5.5-i,-1.5+5i);title(相量图);例5.13 绘制空间曲线: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(Line in 3-D Space);text(0,0,0,origin);xlabel(X),ylabe
12、l(Y),zlabel(Z);grid;例5.14 已知6x30,15y36,求不定方程2x+5y=126的整数解。x=7:29;y=16:35;x,y=meshgrid(x,y); %在7,2916,35区域生成网格坐标z=2*x+5*y;k=find(z=126); %找出解的位置x(k),y(k) %输出对应位置的x,y即方程的解例5.15 用三维曲面图表现函数z=sin(y)cos(x)。程序1:x=0:0.1:2*pi;x,y=meshgrid(x);z=sin(y).*cos(x);mesh(x,y,z);xlabel(x-axis),ylabel(y-axis),zlabel(z
13、-axis);title(mesh);程序2:x=0:0.1:2*pi;x,y=meshgrid(x);z=sin(y).*cos(x);surf(x,y,z);xlabel(x-axis),ylabel(y-axis),zlabel(z-axis);title(surf);程序3:x=0:0.1:2*pi;x,y=meshgrid(x);z=sin(y).*cos(x);plot3(x,y,z);xlabel(x-axis),ylabel(y-axis),zlabel(z-axis);title(plot3-1);grid;例5.16 绘制两个直径相等的圆管的相交图形。%两个等直径圆管的交线
14、m=30;z=1.2*(0:m)/m;r=ones(size(z);theta=(0:m)/m*2*pi;x1=r*cos(theta);y1=r*sin(theta); %生成第一个圆管的坐标矩阵z1=z*ones(1,m+1);x=(-m:2:m)/m;x2=x*ones(1,m+1);y2=r*cos(theta); %生成第二个圆管的坐标矩阵z2=r*sin(theta);surf(x1,y1,z1); %绘制竖立的圆管axis equal,axis offhold onsurf(x2,y2,z2); %绘制平放的圆管axis equal,axis offtitle(两个等直径圆管的交线);hold off例5.17 分析由函数z=x2-2y2构成的曲面形状及与平面z=a的交线。x,y=meshgrid(-10:0.2:10);z1=(x.2-2*y.2)+eps; %第1个曲面a=input(a=
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1