收藏 分销(赏)

matlab编程语言实用程序百例.pdf

上传人:曲**** 文档编号:5459588 上传时间:2024-11-07 格式:PDF 页数:143 大小:2.18MB 下载积分:15 金币
下载 相关 举报
matlab编程语言实用程序百例.pdf_第1页
第1页 / 共143页
matlab编程语言实用程序百例.pdf_第2页
第2页 / 共143页


点击查看更多>>
资源描述
matlab实用程序百例 4B.一 一 ,.O.1,5JUQ3 J 5r 5 J 5 J 5 J 5 J 3 JQ3 J 5 WQ 3 W J编者注:经过长时间的收集和整理,供同学们现在和以后的学习和应用参考。我相信,这些实例将会使你终身收益。1 rA ,%?W W 5W W W W*.!.*%?!”2 JL32是:图形应用篇33-66是:界面设计篇67-84是:图形处理篇85-100是:数值分析篇第一部分图形应用篇实例1:三角函数曲线(1)function shiliOlh0二figure(toolbar,none;.position,198 56 350 300,.name;实例 01);h 1 二axes(parent,hO,.VisibleVoff);x=-pi:0.05:pi;y=sin(x);plot(x,y);xlabelC 自变量 X);ylabel。函数值 Y!);title(SIN()函数曲线);grid on实例2:三角函数曲线(2)function shili02hO=figure(,toolbar,none,.position:200 150 450 350,.name;实例 02);x=-pi:0.05:pi;y=sin(x)+cos(x);plot(x,yZ-*r*,linewidth1,1);grid onxlabel。自变量 X);ylabel。函数值 Y);title。三角函数);实例3:图形的叠加function shiliO3hO=figure(,toolbar,none,.position1,200 150 450 350,.name;实例 03);x=-pi:0.05:pi;yl=sin(x);y2=cos(x);plot(x,yl,.x,y2,.-og);grid onxlabel。自变量 X);ylabelC函数值 Y1);title,三角函数);实例4:双y轴图形的绘制function shili04h0二figure(toolbar?none,.,position*,200 150 450 250,.hame;实例 04);x=0:900;a=1000;b=0.005;yl=2*x;y2=cos(b*x);haxes,hlinel,hline2=plotyy(x,yl,x,y2,semilogy,plot,);axes(haxes(l)ylabelCemilog plot*);axes(haxes(2)ylabel(linear plot1);实例5:单个轴窗口显示多个图形function shili05h0二figure(toolbar?none,.,position;200 150 450 250,.name?实例 05);t=0:pi/10:2*pi;x,y=meshgrid(t);subplot(2,2,l)plot(sin(t),cos(t)axis equalsubplot(2,2,2)z=sin(x)-cos(y);plot(t,z)axis(O 2*pi-2 2)subplot(2,2,3)h=sin(x)+cos(y);plot(t,h)axis(O 2*pi-2 2)subplot(2,2,4)g=(sin(x).A2)-(cos(y).A2);plot(t,g)axis(0 2*pi-l 1)实例6:图形标注function shili06hO=figure(toolbar,none,.position;200 150 450 400,.hame;实例 06);t=0:pi/10:2*pi;h=plot(t,sin(t);xlabel(*t=0 到 2pi,fontsize,16);ylabel(,sin(t),fontsize,16);title(it从 0to2pi 的正弦曲线?fontsize,16)x=get(h,xdata,);y=get(h;ydata);imin=find(min(y)=y);imax=find(max(y)=y);text(x(imin),y(imin),.Meftarrow 最d、值=;num2str(y(imin),fontsize,16)text(x(imax),y(imax),.Meftarrow 最大值二;num2str(y(imax),fontsize,16)实例7:条形图形 function shili07hO=figure(,toolbar,none,.position*,200 150 450 350,.name;实例 07);tiaoU562 548 224 545 41 445 745 512;tiao2=47 48 57 58 54 52 65 48;t=0:7;bar(t,tiaol)xlabel(!X 轴);ylabel(!TIAOl 值);hl=gca;h2二axes(position,get(hl,position);plot(t,tiao2,linewidth*,3)set(h2,yaxislocation?right?coloT,none?xticklabel,)实例8:区域图形function shili08hO=figure(,toolbar,none,.position;200 150 450 250,.hame;实例 08);x=91:95;profitsl=88 75 84 93 77;profits2=51 64 54 56 68;profits3=42 54 34 25 24;profits4=26 38 18 15 4;area(x,profits 1/facecolor1,0.5 0.9 0.6,.dgecolorVb,.linewidth)hold onarea(x,profits!,Tacecolor1,0.9 0.85 0.7,.edgecolor,y,.linewidth,3)hold onarea(x,profits3,facecolor,0.3 0.6 0.7,.edgecoloT,T,.linewidth,3)hold onarea(x,profits4,facecolor,0.6 0.5 0.9,.edgecolor,m,.linewidth)hold offset(gca,tick*,91:95)set(gca,layer1,op*)gtext(Meftarrow 第一季度销量)gtext(Meftarrow 第二季度销量)gtext(Meftarrow第三季度销量)gtext(Meftarrow第四季度销量)xlabel(年?fontsize;16);ylabel(销售量?fontsize,16);实例9:饼图的绘制function shili09hO二figure(toolbar;none;.,position;200 150 450 250,.hame;实例 09);仁54 21 35;68 54 35;45 25 12;48 68 45;68 54 69;x=sum(t);h=pie(x);textobj s=findobj(h,ftype1,*text*);strl=get(textobjs,string);vail=get(textobjs,extent);oldext=cat(l,vall:);names=商品一:商品二:商品三:;str2=strcat(names,str 1);set(textobjs,String*,str2)val2=get(textobjs,Extent1);newext=cat(l,val2:);offset=sign(oldext(:,l).*(newext(:,3)-oldext(:,3)/2;pos=get(textobj s,position);textpos=cat(1,pos:);textpos(:,1)=textpos(:,1)+offset;set(textobjs,position,num2cell(textpos,3,2)实例10:阶梯图function shililOhO=figure(,toolbar,none,.,position1,200 150 450 400,.name;实例 10);a=0.01;b=0.5;仁 0:10;f=exp(-a*t).*sin(b*t);stairs(t,f)hold onplot。,:*)hold offglabel=,函数 eA-(alpha*t)sinbeta*t 的阶梯图,;gtext(glabel/fontsize,16)xlabel(t=0:10?fontsize,16)axis(0 10-1.2 1.2)实例n:枝干图function shilillhO二figure(toolbar?none,.position1,200 150 450 350,.name?实例 H);x=0:pi/20:2*pi;yl=sin(x);y2=cos(x);h 1=stem(x,yl+y2);hold onh2=plot(x,yl;Ar,x,y2;*g,);hold offh3=hl(l);h2;legend(h3,y 1+y2 7y 1=sin(x),y2=cos(x)xlabel。自变量 X);ylabelC函数值 Y);title,正弦函数与余弦函数的线性组合);实例12:罗盘图function shilil2hO=figure(,toolbar,none,.position:200 150 450 250,.name;实例 12);winddirection=54 24 65 84256 12 235 62125 324 34 254;windpower=2 5 5 36 8 12 76 14 10 8;rdirection=winddirection*pi/l 80;x,y=pol2cart(rdirection,windpower);compass(x,y);desc二风向和风力北京气象台;月1日0:00到;10 月 1 日 12:00;gtext(desc)实例13:轮廓图function shilil3h0二figure(toolbar?none,.position;200 150 450 250,.name;实例 13);th,r=meshgrid(0:10:360)*pi/180,0:0.05:1);x,y=pol2cart(th,r);z=x+i*y;f=(z.A4-l).A(0.25);contour(x,y,abs(f),20)axis equalxlabel。实部fontsize,16);ylabel(虚部?fontsize,16);h=polar(0 2*pi,0 1);delete(h)hold oncontour(x,y,abs(f),20)实例14:交互式图形function shilil4hO二figure(toolbar,none;.,position1,200 150 450 250,.name?实例 14);axis(0 10 0 10);hold onx=;y=;n=0;dispC单击鼠标左键点取需要的点);dispC单击鼠标右键点取最后一个点);but=l;while but=lxi?yi?but=ginput(l);plot(xi,yi,bo)n=n+l;dispC单击鼠标左键点取下一个点);x(n,l)=xi;y(n,l)=yi;endt=l:n;ts=l:0.1:n;xs=spline(t,x,ts);ys=spline(t,y,ts);plot(xs,ys,T-);hold off实例15:变换的傅立叶函数曲线function shilil5hO二figure(toolbar?none,.position;200 150 450 250,.name;实例 15);axis equalm=moviein(20,gcf);setCgca/nextplotVreplacechildren*)h=uicontrol(style,slider;position;100 10 500 20;minj;max;20)for 1:20plot(fft(eye(j+16)set(h,!value1,j)m(:,j)=getframe(gcf);endelf;axes(position,0 0 1 1);movie(m,30)实例16:劳伦兹非线形方程的无序活动function shilil5hO=figure(,toolbar,none,.position:200 150 450 250,.name;实例 15);axis equalm=moviein(20,gcf);set(gca,nextplot,replacechildren)h=uicontrol(style,slider;position;100 10 500 20;minU/max;20)forj=l:20plot(fft(eye(j+l 6)set(h,Value*,j)m(:,j)=getframe(gcf);endelf;axes(position,0 0 1 1);movie(m,30)实例17:填充图function shilil7hO二figure(toolbar?none,.position;200 150 450 250,.name;实例 17);t=(l:2:15)*pi/8;x=sin(t);y=cos(t);axis square offtext(0,0,STOP,.color,l 1 1,.fontsize,50,.horizontalalignment*,Center1)例18:条形图和阶梯形图function shilil8hO二figure(toolbar?none,.position;200 150 450 250,.name;实例 18);subplot(2,2,l)x=-3:0.2:3;y=exp(-x.*x);bar(x,y)title(12-D Bar Chart1)subplot(2,2,2)x=-3:0.2:3;y=exp(-x.*x);bar3(x,y,T)title(13-D Bar Chart*)subplot(2,2,3)x=-3:0.2:3;y=exp(-x.*x);stairs(x,y)title(!Stair Chart1)subplot(2,2,4)x=-3:0.2:3;y=exp(-x.*x);barh(x,y)titleCHorizontal Bar Chart1)实例19:三维曲线图function shilil9hO二figure(toolbar;none;.position;200 150 450 400,.name;实例 19);subplot(2,l,l)x=linspace(0,2*pi);yl=sin(x);y2=cos(x);y3=sin(x)+cos(x);zl=zeros(size(x);z2=0.5*zl;z3=zl;plot3(x,yl,zl,x,y2,z2,x,y3,z3)grid onxlabel(!X 轴);ylabelCY 轴);zlabel(!Z 轴);title(Tigurel:3-D Plot1)subplot(2,l,2)x=linspace(0,2*pi);yl=sin(x);y2=cos(x);y3=sin(x)+cos(x);zl=zeros(size(x);z2=0.5*zl;z3=zl;plot3(x,zl,yl,x,z2,y2,x,z3,y3)grid onxlabel(fX 轴);ylabelCY 轴);zlabel(fZ 轴);title(!Figure2:3-D Plot1)实例20:图形的隐藏属性function shili20h0二figure(toolbar,none;.position:200 150 450 300,.name;实例 20);subplot(1,2,1)x,y,z=sphere(l 0);mesh(x,y,z)axis offtitle(Tigurel:Opaque*)hidden onsubplot(l,2,2)x,y,z=sphere(l 0);mesh(x,y,z)axis offtitle(,Figure2:Transparent,)hidden off实例21PEAKS函数曲线function shili21hO二figure(toolbar,none,.,position,200 100 450 450,.name;实例 21);x,y,z=peaks(30);subplot(2,l,l)x=x(l,:);y=y(:,D;i=find(y0.8&y-0.6&x0.8&y-0.6&x0,.if yushu=0,.beishu=beishu-1end,.dday二yearday+365*dyear+beishu+1;,if dyearstep(l,l 2*z,hline=plot(t,y),ifget(rl;,value)=l;,.grid on,.七 nd,.,ifget(r2,nvalue,=l,.grid off;,.end);tl=uicontrol(parent,hO,.nitsYpoints1,.W/tr,.Style,text,string,strl,sprintf(,%1.4g,z),.position,100 120 150 20,.backgroundcolor,0.75 0.75 0.75);b 1=uicontrol(parent,hO,.units,points,.W/br,.tyleVpushbutton1,.String?关闭;.position;80 50 80 30,.backgroundcolor1,0.75 0.75 0.75,.fontsize5,.fallback Vclose1);实例61:浏览流体数据hO=figure(,toolbar,none.position,198 56 450 468,.hame;实例 611);h 1=axes(parent,h0,.position,0.3 0.45 0.5 0.5,.,visible;off1);x,y,z,v=flow;xmin=min(x(:);ymin=min(y(:);zmin=min(z(:);xmax=max(x(:);ymax=max(y(:);zmax=max(z(:);u 1=uimenu(parent,hO,.label;绘图background;0.75 0.75 0.75);ull 二uimenu(parent,u 1,.tag,un;.label?绕X轴旋转-45度;background1,0.75 0.75 0.75,.callback,1.tla,1,.hslice=surf(linspace(xmin,xmax,100),linspace(ymin,ymax,100),zeros(100);,.frotate(hslice,-1,0,0,-45),.,xd=get(hslice,nxdatan);/,.yd二get(hslice,“ydata);,zd=get(hslice Jzdata);u 12=uimenu(parent,u 1,.,tagVul2,.label/绕Y轴旋转-45度;background;0.75 0.75 0.75,.callback;cla,.hslice二surf(linspace(xmin,xmax,100)Jinspace(ymin,ymax,100),zeros(100);,.totateChslice,0,-1,0,-45),.xd=get(hslice,“xdata);,yd=get(hslice,nydataH)zd=get(hslice Jzdata);b 1 二uicontrol(parent,hO,.style;pushbutton;.nits*,joints,.W/br,.backgroundcolor,0.75 0.75 0.75,.String?设置颜色;position;50 120 60 25,.callback;delete(hslice),.rh=slice(x,y,z,v,xd,yd,zd)set(hjfacecolor,interp”,edgecolor,none,diffusestrength,0.8);b2二uicontrol(parent,hO,.style;pushbutton;.nits*,joints*,.ackgroundcolor,0.75 0.75 0.75,.string?添加切片匚position;240 120 60 25,.callback;hold on,.hx=slice(x,y,z,v,xmax,I);,.set(hx,facecolor,interp,edgecolor”,none);b3=uicontrol(parent,h0,.style;pushbutton,.unitsJpoints,.tag-b3,backgroundcolor,0.75 0.75 0.75,.string?添加切片 2.position:240 70 60 20,.callback;hold on,.,hy=slice(x,y,z,v,ymax,set(hy,facecolor,interpJedgecolor,none);b4=uicontrol(,parent,h0,.tyle*,pushbutton1,.units?points,.backgroundcolor,0.75 0.75 0.75,.string?添加切片 3.position;240 20 60 20,.tailback;hold on,.*hz=slice(x,y,z,v,zmax-set(hz,facecolor“Jinterp,edgecolor,none);b5 二uicontrol(parent,hO,.style;pushbutton;.nits*,joints,.tag,b5;.backgroundcolor,0.75 0.75 0.75,.string?灯光效果.position1,50 70 60 20,.callback;,daspect(l 1 1),1,.axis tight,.*box on,.,view(-38.5,16),!?.nzoom(1.4),.,camproj perspective,*,.lightangle(-45,45),);b6=uicontrol(,parent,h0,.style;pushbutton;.units,points,tag?b6,.ackgroundcolor,0.75 0.75 0.75,.String1,tolorbar1,.position;50 20 60 20,.tailback,colorbar(”horiz);b7二uicontrol(parent,hO,.style;pushbutton;.units,points,backgroundcolor,0.75 0.75 0.75,.String;关闭fontsize,14,.position1,145 75 60 20,.tailback1,lose1);实例62:简单计算器hO二figure(toolbar?none;.(position;200 60 220 240,.hame;实例 62);bO二uicontrol(parent,hO,.units?points;.(tagVbO1,.tyle,pushbutton,.string?。,.fontsize,12,.position0 15 35 20,.backgroundcolor1,0.75 0.75 0.75,.callback;,ifk=nOn&i=O,;.匕rrordlg(数字首位不能为0n);,.else,.?k=k;0 lfk=nOOn;,.,k=n0n;;,.end,set(el,string”,k);,.end);b 15=uicontrol(parent,hO,.nits*,joints1,.tagVbl5;.,style,pushbutton*,.string;士,fontsize,12,.position;45 15 35 20,.backgroundcolor,0.75 0.75 0.75,.callback.,k=get(el,nstringn);;,.if g,m=m+str2num(k)匕nd,.,m二m-str2num(k)end,m=m*str2num(k)end,.tifk=nOn;,.七rrordlg(除数不能为0n);;,.end,.,m=m/str2num(k)end,;.set(e 1,string”,num2str(m)0;);bl uicontrolCarenthO,.units,points,.tyle Vpushbutton1,.string;*,.fontsize,12,.,position1,85 15 35 20,.backgroundcolor,0.75 0.75 0.75,.callback,1.m=str2num(k);/,.,set(el,String,n0n);;,.七nd,;.,k=get(el,String);;,.m=nn-str2num(k)end,.,ifg=n-,7,.m=m-str2num(k).end,,m=m*str2num(k)end,;.ifk=nOn;,.七rrordlg(除数不能为 0);,.七nd,.,m=m/s tr2num(k);,七 nd,.*set(e 1,nstringn,num2str(m)1=1;,.end,.g=+TD;b 16=uicontrol(parent,h0,.units,points,tag?bl6,.tyle*,pushbutton1,.string?关闭,fontsize,12,.position1,125 15 35 20,.ackgroundcolor*,0.75 0.75 0.75,.callback;close);b 1 二uicontrol(parent,hO,.units,points,W/br,.tyleVpushbutton1,.String1,fontsize,12,.position1,5 45 35 20,.ackgroundcolor,0.75 0.75 0.75,.callback;,fk=01,.vnn-end,.k=k,T;,1.set(el,string”,k););b2=uicontrol(parent,h0,.units?points,.tag?b2.style,pushbutton;.string,2,.fontsize,12,.position;45 45 35 20,.backgroundcolor,0.75 0.75 0.75,.callback;nn-1 K一 刀,end,.k工set(el,string,;);b3二uicontrol(parent,hO,.units,points,style;pushbutton,.string;3,.fontsize,12,.position;85 45 35 20,.ackgroundcolor,0.75 0.75 0.75,.callback;A一,,end,.,set(el,string”,k););b 14=uicontrol(,parent,h0,.units,points,tag,bl4,.style;pushbutton;.string/,fontsize,12,.positioiT,125 45 35 20,.backgroundcolor,0.75 0.75 0.75,.,callback.*m=str2num(k),set(el,string,n0n);;,.end/,.k=get(el,string);,.七rrordlg(除数不能为*if(k=0);.,m=m+str2num(k)ifgi,1.,m=m-str2num(k).七nd,.m=m*str2num(k),m二m/str2num(k);七 nd,.*set(e 1/string,num2str(m),end,.1=1;,.end,.k二0;,.w;);b4=uicontrol(,parent,h0,.units,points,tag?b4;.style;pushbutton,.string;4,.fontsize,12,.position1,5 75 35 20,.backgroundcolor,0.75 0.75 0.75,.tailback;v,n,-n一 刀V七 nd,.set(el,string”,k););b5 二uicontrol(parent,hO,.nitsYpoints1,.tag-b51.style;pushbutton;.string,5,.fontsize,12,.position;45 75 35 20,.backgroundcolor,0.75 0.75 0.75,.callback;tifk=n0,V,.nn-1 R-9?,set(el,string”,k););b6=uicontrol(,parent,h0,.units,points,tag?b6,.tyleVpushbutton1,.string76,.fontsize,12,.position;85 75 35 20,.backgroundcolor,0.75 0.75 0.75,.callback;ifk=nOn,.nn-1 R一?end,.set(el,string”,k););b 13二uicontrol(parent,hO,.nitsYpoints1,.style;pushbutton;.string,*,fontsize,12,.position1,125 75 35 20,backgroundcolor,0.75 0.75 0.75,.callback;,m=str2num(k);,.,set(el,string,nOn);;,.End,.,k=get(el,string);,.ifg=+”,l.,m=m+str2num(k)fend/,.,m=m-str2num(k).end,.1m=m*str2num(k)end,.tifk=nOn;,.七rrordlg(除数不能为0);,.end,1,.m=m/str2num(k)七nd,.*set(e l,nstringn,num2str(m)1=1;,.end,.&=*);b7 二uicontrol(parent,hO,.units,points;.tag”.style;pushbutton;.string;7,.fontsize,12,.position1,5 105 35 20,backgroundcolor1,0.75 0.75 0.75,.callback.lfk=nOn;,.,n-R-,end/,.k=k,7;;.set(el,string,;);b8二uicontrol(parent,hO,.units,points,tag-b8.style;pushbutton,.string;8,.fontsize,12,.position;45 105 35 20,.ackgroundcolor,0.75 0.75 0.75,.callback;nn-1 K一 夕,七 nd,.k=k,”8”;;.set(el,string”,k););b9二uicontrol(parent,hO,.units?points,.tag?b9,.style;pushbutton;.string,.fontsize,12,.position;85 105 35 20,.backgroundcolor,0.75 0.75 0.75,.callback;nn-1K一 刀,七nd,.set(el,string”,k););b 12=uicontrol(parent,h0,.units?points,.,tag-bl2;style;pushbutton,.string?-,.fontsize,12,.position,125 105 35 20,.backgroundcolor,0.75 0.75 0.75,.callback.,m二 str2num(k)set(el,“string,“0);,1.七nd,.,k=get(el,nstringn);;,.,m=m+str2num(k)end,.,m=m-str2num(k)end,;.,m=m*str2num(k)end,.tifk=nOn;,.七rrordlg(除数不能为0);;,.end,.,m=m/str2num(k)七 nd,.,set(el,nstringn,num2str(m);/,.1=1;,.七 nd,.e 1=uicontrol(parent,hO,.units,points,.tyleVedit1,.horizontalalignment,Tight;.fontsize,12,.string?。,.position;45 135 115 20,.backgroundcolor,1 1 1);k=get(el,String);i=0;m=0;实例63:字母统计hO=figure(,toolbar,one1,.position;200 150 350 200,.hame;实例 63);chooser1;e 1=uicontrol(parent,hO,.units,points,tagVer,.style/edit,.backgroundcolor,1 1 1,.max,2,.fontsize,12,.horizontalalignment Vleft1,.position;20 20 120 100);11=uicontrol(,parent,hO,.units,points,.W/tr,.string?请输入字母(大小写皆可):,fontsize,10,.ackgroundcolor1,0.75 0.75 0.75,position;20 125 120 15);b 1=uicontrol(parent,hO,.nitsVpoints,.W/br,.style;pushbutton;.String7开始统计;backgroundcolor,0.75 0.75 0.75,.position;180 100 60 20,.callback;,s=get(el,HstringH);,.*n=length(s)氏0;.Tor i=l:n,if(abs(s(i)64)&(abs(s(i)96)&(abs(s(i)-4)&kl;k4二erode(kl)&(k3=0);r,c=find(k4);k5=bwselect(kl,c,r);b 1 二uicontrol(parent,hO,.units,points,W/br,.style;pushbutton;.String;二值分割图;.backgroundcolor,0.75 0.75 0.75,.position:30 110 50 20,.callback;cla,.imshow(kl);b2=uicontrol(parent,h0,.nits*,joints,.1ag?b2.style;pushbutton;.String?滤波结果图;.backgroundcolor,0.75 0.75 0.75,.positionhlOO 110 50 20,.callback;cla,.imshow(k2);b3=uicontrol(parent,h0,.units,points,tagVb3,.tyleVpushbutton1,.string?阈值化图;backgroundcolor,0.75 0.75 0.75,.position1,1
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服