您好,欢迎访问三七文档
当前位置:首页 > 建筑/环境 > 电气安装工程 > 应用MATLAB图形函数和绘图实例
应用MATLAB图形函数和绘图实例例1.输入MATLAB程序如下:x=0:0.2:12;y1=Bessel(1,x);y2=Bessel(2,x);y3=Bessel(3,x);figure(1)subplot(2,2,1)h=plot(x,y1,x,y2,x,y3);set(h,‘LineWidth’,2,{‘LineStyle’},{‘--’;‘:’,‘-.’})set(h,{‘Color’},{‘R’;‘G’;‘B’})axis([012–0.51])gridonxlabel(‘Time’)ylabel(‘Amplitude’)legend(h,‘First’,‘Second’,‘Third’)title(‘BesselFunctions’)[y,ix]=min(y1);text(x(ix),y,‘FirstMin\rightarrow’,…‘HorizontalAlignment’,‘right’)Print–depsc-tiff–r200myplot应用图形编辑模式如图所示。同学们自己练习各项功能。例2.输入MATLAB程序如下:t=0:pi/100:2*pi;y=sin(t);plot(t,y)gridon继续输入程序如下:y2=sin(t–0.25);y3=sin(t–0.5);plot(t,y,t,y2,t,y3)按此按钮开始图形编辑模式按这些钮开始增加字符箭头和线改变一个轴的3D视角可以对线的类型进行定义:t=0:pi/100:2*pi;y=sin(t);y2=sin(t–0.25);y3=sin(t–0.5);plot(t,y,‘-’,t,y2,‘--’,t,y3,‘:’)练习,对红的颜色进行编辑。例3.只绘数据点。输入MATLAB程序如下:x=0:pi/15:4*pi;y=exp(2*cos(x));plot(x,y,‘r+’)进一步对图形进行设置:x=0:pi/15:4*pi;y=exp(2*cos(x));plot(x,y,‘-r’,x,y,‘ok’)继续对线类型进行设置:x=0:pi/15:4*pi;y1=exp(2*cos(x));y2=exp(2*sin(x));plot(x,y1,‘-*k’,x,y2,‘-.ok’)练习:把这两条曲线高成不同颜色。例4.矩阵的线绘。z=peaks;%矩阵为4949plot(z)变换方向来绘图:y=1:length(peaks);plot(peaks,y)例5.用双Y轴绘曲线。t=0:pi/20:2*pi;y=exp(sin(t));plotyy(t,y,t,y,‘plot’,‘stem’)把线性轴和对数轴合并绘图。t=0:900;A=1000;a=0.005;b=0.005;z1=A*exp(-a*t);z2=sin(b*t);[haxes,hline1,hline2]=plotyy(t,z1,t,z2,‘semilogy’,‘plot’);axes(haxes(1))ylabel(‘SemilogPlot’)axes(haxes(2))ylabel(‘LinearPlot’)set(hline2,‘LineStyle’,‘--’)例6.轴的限制和标记。命令为:axis([xmin,xmax,ymin,ymax])。专用标记和标记标号:x=-pi:.1:pi;y=sin(x);plot(x,y)set(gca,‘XTick’,-pi:pi/2:pi)set(gca,‘XTickLabel’,{‘-pi’,‘-pi/2’,‘0’,‘pi/2’,‘pi’})xlabel(‘-\pi\leg\Theta\leg\pi’)ylabel(‘sin(\Theta)’)title(‘plotofsin(\Theta)’)text(-pi/4,sin(-pi/4),‘\leftarrowsin(-\pi\div4)’,…‘HorizontalAlignment’,‘left’)set(findobj(gca,‘Type’,‘line’,‘Color’,[001],…‘Color’,[0.500.5],‘Linewidth’,2)例7.绘椭圆。t=0:pi/20:2*pi;plot(sin(t),2*cos(t))gridon当再加入命令axissquare后,绘出图形为如果所加入命令axisequal后,绘出图形为如果所加入命令axisequaltight后,绘出图形为例8.显示多个图形。t=0:pi/20:2*pi;[x,y]=meshgrid(t);subplot(2,2,1)plot(sin(t),cos(t))axisequalsubplot(2,2,2)z=sin(x)+cos(y);plot(t,z)axis([02*pi-22])subplot(2,2,3)z=sin(x).*cos(y);plot(t,z)axis([02*pi-11])subplot(2,2,4)z=(sin(x).^2)–(cos(y).^2);plot(t,z)axis([02*pi-11])例9.计算标注文字的位置。t=0:900;holdon;plot(t,0.25*exp(-0.005*t));text(300,.25*exp(-0.005*300),‘\bullet\leftarrow\fontname{times}0.25{\ite}^{-0.005{\itt}}att=300’,‘FontSize’,14)holdoff;例10.更详细标注信息。x=linspace(0,2*pi,60);a=sin(x);b=cos(x);holdonstem_handles=stem(x,a+b);plot_handles=plot(x,a,‘—r’,x,b,‘--g’);xlabel(‘Timein\musecs’)ylabel(‘Magnitude’)title(‘LinearCombinationofTwoFunctions’)legend_handles=[stem_handles;plot_handles];legend(legend_handles,‘a+b’,‘a=sin(x)’,‘b=cos(x)’)例11.更为复杂的标注说明,包括不同统计量。x=0:.2:12;plot(x,bessel(1,x),x,bessel(2,x),x,bessel(3,x));legend('First','Second','Third');legend('First','Second','Third',-1)b=bar(rand(10,5),'stacked');colormap(summer);holdonx=plot(1:10,5*rand(10,1),'marker','square','markersize',12,...'markeredgecolor','y','markerfacecolor',[.60.6],...'linestyle','-','color','r','linewidth',2);holdofflegend([b,x],'Carrots','Peas','Peppers','GreenBeans',...'Cucumbers','Eggplant')x=0:.2:12;plot(x,bessel(1,x),x,bessel(2,x),x,bessel(3,x));legend('First','Second','Third');legend('First','Second','Third',-1)b=bar(rand(10,5),'stacked');colormap(summer);holdonx=plot(1:10,5*rand(10,1),'marker','square','markersize',12,...'markeredgecolor','y','markerfacecolor',[.60.6],...'linestyle','-','color','r','linewidth',2);holdofflegend([b,x],'Carrots','Peas','Peppers','GreenBeans',...'Cucumbers','Eggplant')离散数据图形例1.二维的茎图。用函数teytcos。1)连续图形alpha=.02;beta=.5;t=0:4:200y=exp(-alpha*t).*sin(beta*t);plot(t,y)2)二维的茎图函数为stem(t,y),个体具体如下:alpha=.02;beta=.5;t=0:4:200;y=exp(-alpha*t).*sin(beta*t);stem(t,y)xlabel(‘Timein\musecs’)ylabel(‘Magnitude’)把茎图和线图同时画在一个图上。请参考上一节中的例10.例2.画一个3DFFT的茎图。th=(0:127)/128*2*pi;x=cos(th);y=sin(th);f=abs(fft(ones(10,1),128));stem3(x,y,f',‘d’,‘fill’)view([-6530])xlabel(‘Real’)ylabel(‘Imaginary’)zlabel(‘Amplitude’)title(‘MagnitudeFrequencyResponse’)例3.把茎图和线图同时画在一个图上。t=0:.1:10;s=0.1+i;y=exp(-s*t);stem3(real(y),imag(y),t,‘r’)holdonplot3(real(y),imag(y),t,‘r’)holdoffview(-39.5,62)xlabel(‘Real’)ylabel(‘Imaginary’)zlabel(‘Amplitude’)例4.函数的阶梯绘图。alpha=0.01;beta=0.5;t=0:10;f=exp(-alpha*t).*sin(beta*t);stairs(t,f)holdonplot(t,f,‘--*’)holdofflabel=‘Stairstepplotofe^{-(\alpha*t)}sin\beta*t’;text(0.5,-0.2,label,‘FontSize’,14)xlabel(‘t=0:10’,‘FontSize’,14)axis([010–1.21.2])例5.方向和速度矢量图。函数有compass,feather,quiver,quiver3。wdir=[45909045360335360270335270335335];knots=[668639689101412];rdir=wdir*pi/180;[x,y]=pol2cart(rdir,knots);compass(x,y)desc={‘WindDirectionandStrengthat’,‘LoganAirportfor’,‘Nov.3at1800through’,‘Nov.4at0600’};text(-28,15,desc)例6.应用Feather函数绘画。theta=90:-10:0;r=ones(size(theta));[u,v]=pol2cart(theta*pi/180,r*10);feather(u,v)axisequal例7.绘复数函数。t=0:0.5:10;s=0.05+i;z=exp(-s*t);feather(z)例8.二维的Quiver函数绘画。n=-2.0:.2:2.0;[X,Y,Z]=peaks(n);contour(X,Y,Z,10)[U,V]=gradient(Z,.2);holdonquiver(X,Y,U,V)holdoff例9.三维的Quiver函数绘2)(2ttvtzz。vz=10;a=-32;t=a:.1:1;z=vz*t+1/2*a*t.^2;vx=2;x=vx*t;vy=3;y=vy*t;u=gradient(x);v=gradient(y);w=gradient(
本文标题:应用MATLAB图形函数和绘图实例
链接地址:https://www.777doc.com/doc-5122702 .html