matlab编程实例100例

合集下载

matlab优化算法100例

matlab优化算法100例

matlab优化算法100例1. 线性规划问题的优化算法:线性规划问题是一类目标函数和约束条件都是线性的优化问题。

Matlab中有很多优化算法可以解决线性规划问题,如单纯形法、内点法等。

下面以单纯形法为例介绍线性规划问题的优化算法。

单纯形法是一种迭代算法,通过不断改变基础解来寻找问题的最优解。

它的基本思想是从一个可行解出发,通过改变基本变量和非基本变量的取值来逐步逼近最优解。

2. 非线性规划问题的优化算法:非线性规划问题是一类目标函数和约束条件至少有一个是非线性的优化问题。

Matlab中有很多优化算法可以解决非线性规划问题,如拟牛顿法、共轭梯度法等。

下面以拟牛顿法为例介绍非线性规划问题的优化算法。

拟牛顿法是一种逐步逼近最优解的算法,通过近似目标函数的二阶导数信息来构造一个二次模型,然后通过求解该二次模型的最优解来更新当前解。

3. 全局优化问题的优化算法:全局优化问题是一类目标函数存在多个局部最优解的优化问题。

Matlab中有很多优化算法可以解决全局优化问题,如遗传算法、模拟退火算法等。

下面以遗传算法为例介绍全局优化问题的优化算法。

遗传算法是一种模拟生物进化过程的优化算法,通过基因编码、选择、交叉和变异等操作来不断迭代演化一组个体,最终找到全局最优解。

4. 多目标优化问题的优化算法:多目标优化问题是一类存在多个目标函数并且目标函数之间存在冲突的优化问题。

Matlab中有很多优化算法可以解决多目标优化问题,如多目标粒子群优化算法、多目标遗传算法等。

下面以多目标粒子群优化算法为例介绍多目标优化问题的优化算法。

多目标粒子群优化算法是一种基于粒子群优化算法的多目标优化算法,通过在粒子的速度更新过程中考虑多个目标函数来实现多目标优化。

5. 其他优化算法:除了上述提到的优化算法,Matlab还提供了很多其他的优化算法,如模拟退火算法、蚁群算法等。

这些算法可以根据具体的问题选择合适的算法进行求解。

综上所述,Matlab提供了丰富的优化算法,可以解决不同类型的优化问题。

matlab数学建模100例

matlab数学建模100例

matlab数学建模100例Matlab是一种强大的数学建模工具,广泛应用于科学研究、工程设计和数据分析等领域。

在这篇文章中,我们将介绍100个使用Matlab进行数学建模的例子,帮助读者更好地理解和应用这个工具。

1. 线性回归模型:使用Matlab拟合一组数据点,得到最佳拟合直线。

2. 多项式拟合:使用Matlab拟合一组数据点,得到最佳拟合多项式。

3. 非线性回归模型:使用Matlab拟合一组数据点,得到最佳拟合曲线。

4. 插值模型:使用Matlab根据已知数据点,估计未知数据点的值。

5. 数值积分:使用Matlab计算函数的定积分。

6. 微分方程求解:使用Matlab求解常微分方程。

7. 矩阵运算:使用Matlab进行矩阵的加减乘除运算。

8. 线性规划:使用Matlab求解线性规划问题。

9. 非线性规划:使用Matlab求解非线性规划问题。

10. 整数规划:使用Matlab求解整数规划问题。

11. 图论问题:使用Matlab解决图论问题,如最短路径、最小生成树等。

12. 网络流问题:使用Matlab解决网络流问题,如最大流、最小费用流等。

13. 动态规划:使用Matlab解决动态规划问题。

14. 遗传算法:使用Matlab实现遗传算法,求解优化问题。

15. 神经网络:使用Matlab实现神经网络,进行模式识别和预测等任务。

16. 支持向量机:使用Matlab实现支持向量机,进行分类和回归等任务。

17. 聚类分析:使用Matlab进行聚类分析,将数据点分成不同的类别。

18. 主成分分析:使用Matlab进行主成分分析,降低数据的维度。

19. 时间序列分析:使用Matlab进行时间序列分析,预测未来的趋势。

20. 图像处理:使用Matlab对图像进行处理,如滤波、边缘检测等。

21. 信号处理:使用Matlab对信号进行处理,如滤波、频谱分析等。

22. 控制系统设计:使用Matlab设计控制系统,如PID控制器等。

matlab编程实例100例之欧阳育创编

matlab编程实例100例之欧阳育创编

1-32是:图形应用篇33-66是:界面设计篇67-84是:图形处理篇85-100是:数值分析篇实例1:三角函数曲线(1)function shili01h0=figure('toolbar','none',...'position',[198 56 350 300],...'name','实例01');h1=axes('parent',h0,...'visible','off');x=-pi:0.05:pi;y=sin(x);plot(x,y);xlabel('自变量X');ylabel('函数值Y');title('SIN( )函数曲线');grid on实例2:三角函数曲线(2)function shili02h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例02');x=-pi:0.05:pi;y=sin(x)+cos(x);plot(x,y,'-*r','linewidth',1);grid onxlabel('自变量X');ylabel('函数值Y');title('三角函数');实例3:图形的叠加function shili03h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例03');x=-pi:0.05:pi;y1=sin(x);y2=cos(x);plot(x,y1,...'-*r',...x,y2,...'--og');grid onxlabel('自变量X');ylabel('函数值Y');title('三角函数');实例4:双y轴图形的绘制function shili04h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例04');x=0:900;a=1000;b=0.005;y1=2*x;y2=cos(b*x);[haxes,hline1,hline2]=plotyy(x,y1,x,y2,'semilogy','plot'); axes(haxes(1))ylabel('semilog plot');axes(haxes(2))ylabel('linear plot');实例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,1)plot(sin(t),cos(t))axis equalsubplot(2,2,2)z=sin(x)-cos(y);plot(t,z)axis([0 2*pi -2 2])subplot(2,2,3)h=sin(x)+cos(y);plot(t,h)axis([0 2*pi -2 2])subplot(2,2,4)g=(sin(x).^2)-(cos(y).^2);plot(t,g)axis([0 2*pi -1 1])实例6:图形标注function shili06h0=figure('toolbar','none',...'position',[200 150 450 400],...'name','实例06');t=0:pi/10:2*pi;h=plot(t,sin(t));xlabel('t=0到2\pi','fontsize',16);ylabel('sin(t)','fontsize',16);title('\it{从 0to2\pi 的正弦曲线}','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),...['\leftarrow最小值=',num2str(y(imin))],... 'fontsize',16)text(x(imax),y(imax),...['\leftarrow最大值=',num2str(y(imax))],...'fontsize',16)实例7:条形图形function shili07h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例07');tiao1=[562 548 224 545 41 445 745 512];tiao2=[47 48 57 58 54 52 65 48];t=0:7;bar(t,tiao1)xlabel('X轴');ylabel('TIAO1值');h1=gca;h2=axes('position',get(h1,'position'));plot(t,tiao2,'linewidth',3)set(h2,'yaxislocation','right','color','none','xticklabel',[])实例8:区域图形function shili08h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例08');x=91:95;profits1=[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,profits1,'facecolor',[0.5 0.9 0.6],...'edgecolor','b',...'linewidth',3)hold onarea(x,profits2,'facecolor',[0.9 0.85 0.7],...'edgecolor','y',...'linewidth',3)hold onarea(x,profits3,'facecolor',[0.3 0.6 0.7],...'edgecolor','r',...'linewidth',3)hold onarea(x,profits4,'facecolor',[0.6 0.5 0.9],...'edgecolor','m',...'linewidth',3)hold offset(gca,'xtick',[91:95])set(gca,'layer','top')gtext('\leftarrow第一季度销量')gtext('\leftarrow第二季度销量')gtext('\leftarrow第三季度销量')gtext('\leftarrow第四季度销量')xlabel('年','fontsize',16);ylabel('销售量','fontsize',16);实例9:饼图的绘制function shili09h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例09');t=[54 21 35;68 54 35;45 25 12;48 68 45;68 54 69];x=sum(t);h=pie(x);textobjs=findobj(h,'type','text');str1=get(textobjs,{'string'});val1=get(textobjs,{'extent'});oldext=cat(1,val1{:});names={'商品一:';'商品二:';'商品三:'};str2=strcat(names,str1);set(textobjs,{'string'},str2)val2=get(textobjs,{'extent'});newext=cat(1,val2{:});offset=sign(oldext(:,1)).*(newext(:,3)-oldext(:,3))/2; pos=get(textobjs,{'position'});textpos=cat(1,pos{:});textpos(:,1)=textpos(:,1)+offset;set(textobjs,{'position'},num2cell(textpos,[3,2]))实例10:阶梯图function shili10h0=figure('toolbar','none',...'position',[200 150 450 400],...'name','实例10');a=0.01;b=0.5;t=0:10;f=exp(-a*t).*sin(b*t);stairs(t,f)hold onplot(t,f,':*')hold offglabel='函数e^{-(\alpha*t)}sin\beta*t的阶梯图'; gtext(glabel,'fontsize',16)xlabel('t=0:10','fontsize',16)axis([0 10 -1.2 1.2])实例11:枝干图function shili11h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例11');x=0:pi/20:2*pi;y1=sin(x);y2=cos(x);h1=stem(x,y1+y2);hold onh2=plot(x,y1,'^r',x,y2,'*g');hold offh3=[h1(1);h2];legend(h3,'y1+y2','y1=sin(x)','y2=cos(x)') xlabel('自变量X');ylabel('函数值Y');title('正弦函数与余弦函数的线性组合');实例12:罗盘图function shili12h0=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/180;[x,y]=pol2cart(rdirection,windpower); compass(x,y);desc={'风向和风力','北京气象台','10月1日0:00到','10月1日12:00'};gtext(desc)实例13:轮廓图function shili13h0=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.^4-1).^(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 shili14h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例14');axis([0 10 0 10]);hold onx=[];y=[];n=0;disp('单击鼠标左键点取需要的点'); disp('单击鼠标右键点取最后一个点'); but=1;while but==1[xi,yi,but]=ginput(1);plot(xi,yi,'bo')n=n+1;disp('单击鼠标左键点取下一个点'); x(n,1)=xi;y(n,1)=yi;endt=1:n;ts=1:0.1:n;xs=spline(t,x,ts);ys=spline(t,y,ts);plot(xs,ys,'r-');hold off实例14:交互式图形function shili14h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例14');axis([0 10 0 10]);hold onx=[];y=[];n=0;disp('单击鼠标左键点取需要的点'); disp('单击鼠标右键点取最后一个点'); but=1;while but==1[xi,yi,but]=ginput(1);plot(xi,yi,'bo')n=n+1;disp('单击鼠标左键点取下一个点'); x(n,1)=xi;y(n,1)=yi;endt=1:n;ts=1:0.1:n;xs=spline(t,x,ts);ys=spline(t,y,ts);plot(xs,ys,'r-');hold off实例15:变换的傅立叶函数曲线function shili15h0=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],'min',1,'max',20)for j=1:20plot(fft(eye(j+16)))set(h,'value',j)m(:,j)=getframe(gcf);endclf;axes('position',[0 0 1 1]);movie(m,30)实例16:劳伦兹非线形方程的无序活动function shili15h0=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],'min',1,'max',20)for j=1:20plot(fft(eye(j+16)))set(h,'value',j)m(:,j)=getframe(gcf);endclf;axes('position',[0 0 1 1]);movie(m,30)实例17:填充图function shili17h0=figure('toolbar','none',...'position',[200 150 450 250],... 'name','实例17');t=(1:2:15)*pi/8;x=sin(t);y=cos(t);fill(x,y,'r')axis square offtext(0,0,'STOP',...'color',[1 1 1],...'fontsize',50,...'horizontalalignment','center')例18:条形图和阶梯形图function shili18h0=figure('toolbar','none',...'position',[200 150 450 250],... 'name','实例18');subplot(2,2,1)x=-3:0.2:3;y=exp(-x.*x);bar(x,y)title('2-D Bar Chart')subplot(2,2,2)x=-3:0.2:3;y=exp(-x.*x);bar3(x,y,'r')title('3-D Bar Chart')subplot(2,2,3)x=-3:0.2:3;y=exp(-x.*x);stairs(x,y)title('Stair Chart')subplot(2,2,4)x=-3:0.2:3;y=exp(-x.*x);barh(x,y)title('Horizontal Bar Chart')实例19:三维曲线图function shili19h0=figure('toolbar','none',...'position',[200 150 450 400],... 'name','实例19');subplot(2,1,1)x=linspace(0,2*pi);y1=sin(x);y2=cos(x);y3=sin(x)+cos(x);z1=zeros(size(x));z2=0.5*z1;z3=z1;plot3(x,y1,z1,x,y2,z2,x,y3,z3) grid onxlabel('X轴');ylabel('Y轴');zlabel('Z轴');title('Figure1:3-D Plot')subplot(2,1,2)x=linspace(0,2*pi);y1=sin(x);y2=cos(x);y3=sin(x)+cos(x);z1=zeros(size(x));z2=0.5*z1;z3=z1;plot3(x,z1,y1,x,z2,y2,x,z3,y3) grid onxlabel('X轴');zlabel('Z轴');title('Figure2:3-D Plot')实例20:图形的隐藏属性function shili20h0=figure('toolbar','none',...'position',[200 150 450 300],... 'name','实例20');subplot(1,2,1)[x,y,z]=sphere(10);mesh(x,y,z)axis offtitle('Figure1:Opaque')hidden onsubplot(1,2,2)[x,y,z]=sphere(10);mesh(x,y,z)axis offtitle('Figure2:Transparent') hidden off实例21PEAKS函数曲线function shili21h0=figure('toolbar','none',...'position',[200 100 450 450],... 'name','实例21');[x,y,z]=peaks(30);subplot(2,1,1)x=x(1,:);y=y(:,1);i=find(y>0.8&y<1.2);j=find(x>-0.6&x<0.5);z(i,j)=nan*z(i,j);surfc(x,y,z)xlabel('X轴');zlabel('Z轴');title('Figure1:surfc函数形成的曲面') subplot(2,1,2)x=x(1,:);y=y(:,1);i=find(y>0.8&y<1.2);j=find(x>-0.6&x<0.5);z(i,j)=nan*z(i,j);surfl(x,y,z)xlabel('X轴');ylabel('Y轴');zlabel('Z轴');title('Figure2:surfl函数形成的曲面') 实例22:片状图function shili22h0=figure('toolbar','none',...'position',[200 150 550 350],...'name','实例22');subplot(1,2,1)x=rand(1,20);y=rand(1,20);z=peaks(x,y*pi);t=delaunay(x,y);trimesh(t,x,y,z)hidden offtitle('Figure1:Triangular Surface Plot'); subplot(1,2,2)x=rand(1,20);y=rand(1,20);z=peaks(x,y*pi);t=delaunay(x,y);trisurf(t,x,y,z)title('Figure1:Triangular Surface Plot');实例23:视角的调整function shili23h0=figure('toolbar','none',...'position',[200 150 450 350],... 'name','实例23');x=-5:0.5:5;[x,y]=meshgrid(x);r=sqrt(x.^2+y.^2)+eps;z=sin(r)./r;subplot(2,2,1)surf(x,y,z)xlabel('X-axis')ylabel('Y-axis')zlabel('Z-axis')title('Figure1')view(-37.5,30)subplot(2,2,2)surf(x,y,z)xlabel('X-axis')ylabel('Y-axis')zlabel('Z-axis')title('Figure2')view(-37.5+90,30)subplot(2,2,3)surf(x,y,z)xlabel('X-axis')。

matlab编程实例100例之欧阳语创编

matlab编程实例100例之欧阳语创编

1-32是:图形应用篇33-66是:界面设计篇67-84是:图形处理篇85-100是:数值分析篇实例1:三角函数曲线(1)function shili01h0=figure('toolbar','none',...'position',[198 56 350 300],... 'name','实例01');h1=axes('parent',h0,...'visible','off');x=-pi:0.05:pi;y=sin(x);plot(x,y);xlabel('自变量X');ylabel('函数值Y');title('SIN( )函数曲线');grid on实例2:三角函数曲线(2)function shili02h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例02');x=-pi:0.05:pi;y=sin(x)+cos(x);plot(x,y,'-*r','linewidth',1);grid onxlabel('自变量X');ylabel('函数值Y');title('三角函数');实例3:图形的叠加function shili03h0=figure('toolbar','none',...'position',[200 150 450 350],... 'name','实例03');x=-pi:0.05:pi;y1=sin(x);y2=cos(x);plot(x,y1,...'-*r',...x,y2,...'--og');grid onxlabel('自变量X');ylabel('函数值Y');title('三角函数');实例4:双y轴图形的绘制function shili04h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例04');x=0:900;a=1000;b=0.005;y1=2*x;y2=cos(b*x);[haxes,hline1,hline2]=plotyy(x,y1,x,y2,'semilogy','pl ot');axes(haxes(1))ylabel('semilog plot');axes(haxes(2))ylabel('linear plot');实例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,1)plot(sin(t),cos(t))axis equalsubplot(2,2,2)z=sin(x)-cos(y);plot(t,z)axis([0 2*pi -2 2])subplot(2,2,3)h=sin(x)+cos(y);plot(t,h)axis([0 2*pi -2 2])subplot(2,2,4)g=(sin(x).^2)-(cos(y).^2);plot(t,g)axis([0 2*pi -1 1])实例6:图形标注function shili06h0=figure('toolbar','none',...'position',[200 150 450 400],...'name','实例06');t=0:pi/10:2*pi;h=plot(t,sin(t));xlabel('t=0到2\pi','fontsize',16);ylabel('sin(t)','fontsize',16);title('\it{从 0to2\pi 的正弦曲线}','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),...['\leftarrow最小值=',num2str(y(imin))],...'fontsize',16)text(x(imax),y(imax),...['\leftarrow最大值=',num2str(y(imax))],...'fontsize',16)实例7:条形图形function shili07h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例07');tiao1=[562 548 224 545 41 445 745 512];tiao2=[47 48 57 58 54 52 65 48];t=0:7;bar(t,tiao1)xlabel('X轴');ylabel('TIAO1值');h1=gca;h2=axes('position',get(h1,'position'));plot(t,tiao2,'linewidth',3)set(h2,'yaxislocation','right','color','none','xticklabel ',[])实例8:区域图形function shili08h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例08');x=91:95;profits1=[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,profits1,'facecolor',[0.5 0.9 0.6],... 'edgecolor','b',...'linewidth',3)hold onarea(x,profits2,'facecolor',[0.9 0.85 0.7],... 'edgecolor','y',...'linewidth',3)hold onarea(x,profits3,'facecolor',[0.3 0.6 0.7],... 'edgecolor','r',...'linewidth',3)hold onarea(x,profits4,'facecolor',[0.6 0.5 0.9],... 'edgecolor','m',...'linewidth',3)hold offset(gca,'xtick',[91:95])set(gca,'layer','top')gtext('\leftarrow第一季度销量')gtext('\leftarrow第二季度销量')gtext('\leftarrow第三季度销量')gtext('\leftarrow第四季度销量')xlabel('年','fontsize',16);ylabel('销售量','fontsize',16);实例9:饼图的绘制function shili09h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例09');t=[54 21 35;68 54 35;45 25 12;48 68 45;68 54 69];x=sum(t);h=pie(x);textobjs=findobj(h,'type','text');str1=get(textobjs,{'string'});val1=get(textobjs,{'extent'});oldext=cat(1,val1{:});names={'商品一:';'商品二:';'商品三:'}; str2=strcat(names,str1);set(textobjs,{'string'},str2)val2=get(textobjs,{'extent'});newext=cat(1,val2{:});offset=sign(oldext(:,1)).*(newext(:,3)-oldext(:,3))/2; pos=get(textobjs,{'position'});textpos=cat(1,pos{:});textpos(:,1)=textpos(:,1)+offset;set(textobjs,{'position'},num2cell(textpos,[3,2]))实例10:阶梯图function shili10h0=figure('toolbar','none',...'position',[200 150 450 400],...'name','实例10');a=0.01;b=0.5;t=0:10;f=exp(-a*t).*sin(b*t);stairs(t,f)hold onplot(t,f,':*')hold offglabel='函数e^{-(\alpha*t)}sin\beta*t的阶梯图'; gtext(glabel,'fontsize',16)xlabel('t=0:10','fontsize',16)axis([0 10 -1.2 1.2])实例11:枝干图function shili11h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例11');x=0:pi/20:2*pi;y1=sin(x);y2=cos(x);h1=stem(x,y1+y2);hold onh2=plot(x,y1,'^r',x,y2,'*g');hold offh3=[h1(1);h2];legend(h3,'y1+y2','y1=sin(x)','y2=cos(x)') xlabel('自变量X');ylabel('函数值Y');title('正弦函数与余弦函数的线性组合');实例12:罗盘图function shili12h0=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/180;[x,y]=pol2cart(rdirection,windpower); compass(x,y);desc={'风向和风力','北京气象台','10月1日0:00到','10月1日12:00'};gtext(desc)实例13:轮廓图function shili13h0=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.^4-1).^(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 shili14h0=figure('toolbar','none',...'position',[200 150 450 250],... 'name','实例14');axis([0 10 0 10]);hold onx=[];y=[];n=0;disp('单击鼠标左键点取需要的点'); disp('单击鼠标右键点取最后一个点'); but=1;while but==1[xi,yi,but]=ginput(1);plot(xi,yi,'bo')n=n+1;disp('单击鼠标左键点取下一个点'); x(n,1)=xi;y(n,1)=yi;endt=1:n;ts=1:0.1:n;xs=spline(t,x,ts);ys=spline(t,y,ts);plot(xs,ys,'r-');hold off实例14:交互式图形function shili14h0=figure('toolbar','none',...'position',[200 150 450 250],... 'name','实例14');axis([0 10 0 10]);hold onx=[];y=[];n=0;disp('单击鼠标左键点取需要的点'); disp('单击鼠标右键点取最后一个点'); but=1;while but==1[xi,yi,but]=ginput(1);plot(xi,yi,'bo')n=n+1;disp('单击鼠标左键点取下一个点'); x(n,1)=xi;y(n,1)=yi;endt=1:n;ts=1:0.1:n;xs=spline(t,x,ts);ys=spline(t,y,ts);plot(xs,ys,'r-');hold off实例15:变换的傅立叶函数曲线function shili15h0=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],'min',1,'max',20) for j=1:20plot(fft(eye(j+16)))set(h,'value',j)m(:,j)=getframe(gcf);endclf;axes('position',[0 0 1 1]);movie(m,30)实例16:劳伦兹非线形方程的无序活动function shili15h0=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],'min',1,'max',20) for j=1:20plot(fft(eye(j+16)))set(h,'value',j)m(:,j)=getframe(gcf);endclf;axes('position',[0 0 1 1]);movie(m,30)实例17:填充图function shili17h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例17');t=(1:2:15)*pi/8;x=sin(t);y=cos(t);fill(x,y,'r')axis square offtext(0,0,'STOP',...'color',[1 1 1],...'fontsize',50,...'horizontalalignment','center')例18:条形图和阶梯形图function shili18h0=figure('toolbar','none',...'position',[200 150 450 250],... 'name','实例18');subplot(2,2,1)x=-3:0.2:3;y=exp(-x.*x);bar(x,y)title('2-D Bar Chart')subplot(2,2,2)x=-3:0.2:3;y=exp(-x.*x);bar3(x,y,'r')title('3-D Bar Chart')subplot(2,2,3)x=-3:0.2:3;y=exp(-x.*x);stairs(x,y)title('Stair Chart')subplot(2,2,4)x=-3:0.2:3;y=exp(-x.*x);barh(x,y)title('Horizontal Bar Chart')实例19:三维曲线图function shili19h0=figure('toolbar','none',...'position',[200 150 450 400],... 'name','实例19');subplot(2,1,1)x=linspace(0,2*pi);y1=sin(x);y2=cos(x);y3=sin(x)+cos(x);z1=zeros(size(x));z2=0.5*z1;z3=z1;plot3(x,y1,z1,x,y2,z2,x,y3,z3) grid onxlabel('X轴');ylabel('Y轴');zlabel('Z轴');title('Figure1:3-D Plot') subplot(2,1,2)x=linspace(0,2*pi);y1=sin(x);y2=cos(x);y3=sin(x)+cos(x);z1=zeros(size(x));z2=0.5*z1;z3=z1;plot3(x,z1,y1,x,z2,y2,x,z3,y3) grid onxlabel('X轴');ylabel('Y轴');zlabel('Z轴');title('Figure2:3-D Plot')实例20:图形的隐藏属性function shili20h0=figure('toolbar','none',...'position',[200 150 450 300],... 'name','实例20');subplot(1,2,1)[x,y,z]=sphere(10);mesh(x,y,z)axis offtitle('Figure1:Opaque')hidden onsubplot(1,2,2)[x,y,z]=sphere(10);mesh(x,y,z)axis offtitle('Figure2:Transparent') hidden off实例21PEAKS函数曲线function shili21h0=figure('toolbar','none',...'position',[200 100 450 450],... 'name','实例21');[x,y,z]=peaks(30);subplot(2,1,1)x=x(1,:);y=y(:,1);i=find(y>0.8&y<1.2);j=find(x>-0.6&x<0.5);z(i,j)=nan*z(i,j);surfc(x,y,z)xlabel('X轴');ylabel('Y轴');zlabel('Z轴');title('Figure1:surfc函数形成的曲面') subplot(2,1,2)x=x(1,:);y=y(:,1);i=find(y>0.8&y<1.2);j=find(x>-0.6&x<0.5);z(i,j)=nan*z(i,j);surfl(x,y,z)xlabel('X轴');ylabel('Y轴');zlabel('Z轴');title('Figure2:surfl函数形成的曲面') 实例22:片状图function shili22h0=figure('toolbar','none',...'position',[200 150 550 350],...'name','实例22');subplot(1,2,1)x=rand(1,20);y=rand(1,20);z=peaks(x,y*pi);t=delaunay(x,y);trimesh(t,x,y,z)hidden offtitle('Figure1:Triangular Surface Plot'); subplot(1,2,2)x=rand(1,20);y=rand(1,20);z=peaks(x,y*pi);t=delaunay(x,y);trisurf(t,x,y,z)title('Figure1:Triangular Surface Plot');实例23:视角的调整function shili23h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例23');x=-5:0.5:5;[x,y]=meshgrid(x);r=sqrt(x.^2+y.^2)+eps;z=sin(r)./r;subplot(2,2,1)surf(x,y,z)xlabel('X-axis')ylabel('Y-axis')zlabel('Z-axis')title('Figure1')view(-37.5,30)subplot(2,2,2)surf(x,y,z)xlabel('X-axis')ylabel('Y-axis')zlabel('Z-axis')title('Figure2')欧阳语创编view(-37.5+90,30)subplot(2,2,3)surf(x,y,z)xlabel('X-axis')ylabel('Y-axis')欧阳语创编。

matlab编程实例100例(精编文档).doc

matlab编程实例100例(精编文档).doc

【最新整理,下载后即可编辑】1-32是:图形应用篇33-66是:界面设计篇67-84是:图形处理篇85-100是:数值分析篇实例1:三角函数曲线(1)function shili01h0=figure('toolbar','none',...'position',[198****0300],...'name','实例01');h1=axes('parent',h0,...'visible','off');x=-pi:0.05:pi;y=sin(x);plot(x,y);xlabel('自变量X');ylabel('函数值Y');title('SIN( )函数曲线');grid on实例2:三角函数曲线(2)function shili02h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例02');x=-pi:0.05:pi;y=sin(x)+cos(x);plot(x,y,'-*r','linewidth',1);grid onxlabel('自变量X');ylabel('函数值Y');title('三角函数');实例3:图形的叠加function shili03h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例03');x=-pi:0.05:pi;y1=sin(x);y2=cos(x);plot(x,y1,...'-*r',...x,y2,...'--og');grid onxlabel('自变量X');ylabel('函数值Y');title('三角函数');实例4:双y轴图形的绘制function shili04h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例04');x=0:900;a=1000;b=0.005;y1=2*x;y2=cos(b*x);[haxes,hline1,hline2]=plotyy(x,y1,x,y2,'semilogy','plot'); axes(haxes(1))ylabel('semilog plot');axes(haxes(2))ylabel('linear plot');实例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,1)plot(sin(t),cos(t))axis equalsubplot(2,2,2)z=sin(x)-cos(y);plot(t,z)axis([0 2*pi -2 2])subplot(2,2,3)h=sin(x)+cos(y);plot(t,h)axis([0 2*pi -2 2])subplot(2,2,4)g=(sin(x).^2)-(cos(y).^2);plot(t,g)axis([0 2*pi -1 1])实例6:图形标注function shili06h0=figure('toolbar','none',...'position',[200 150 450 400],...'name','实例06');t=0:pi/10:2*pi;h=plot(t,sin(t));xlabel('t=0到2\pi','fontsize',16);ylabel('sin(t)','fontsize',16);title('\it{从0to2\pi 的正弦曲线}','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),...['\leftarrow最小值=',num2str(y(imin))],...'fontsize',16)text(x(imax),y(imax),...['\leftarrow最大值=',num2str(y(imax))],...'fontsize',16)实例7:条形图形function shili07h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例07');tiao1=[562 548 224 545 41 445 745 512];tiao2=[47 48 57 58 54 52 65 48];t=0:7;bar(t,tiao1)xlabel('X轴');ylabel('TIAO1值');h1=gca;h2=axes('position',get(h1,'position'));plot(t,tiao2,'linewidth',3)set(h2,'yaxislocation','right','color','none','xticklabel',[]) 实例8:区域图形function shili08h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例08');x=91:95;profits1=[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,profits1,'facecolor',[0.5 0.9 0.6],...'edgecolor','b',...'linewidth',3)hold onarea(x,profits2,'facecolor',[0.9 0.85 0.7],...'edgecolor','y',...'linewidth',3)hold onarea(x,profits3,'facecolor',[0.3 0.6 0.7],...'edgecolor','r',...'linewidth',3)hold onarea(x,profits4,'facecolor',[0.6 0.5 0.9],...'edgecolor','m',...'linewidth',3)hold offset(gca,'xtick',[91:95])set(gca,'layer','top')gtext('\leftarrow第一季度销量') gtext('\leftarrow第二季度销量') gtext('\leftarrow第三季度销量') gtext('\leftarrow第四季度销量') xlabel('年','fontsize',16);ylabel('销售量','fontsize',16);实例9:饼图的绘制function shili09h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例09');t=[54 21 35;68 54 35;45 25 12;48 68 45;68 54 69];x=sum(t);h=pie(x);textobjs=findobj(h,'type','text');str1=get(textobjs,{'string'});val1=get(textobjs,{'extent'});oldext=cat(1,val1{:});names={'商品一:';'商品二:';'商品三:'};str2=strcat(names,str1);set(textobjs,{'string'},str2)val2=get(textobjs,{'extent'});newext=cat(1,val2{:});offset=sign(oldext(:,1)).*(newext(:,3)-oldext(:,3))/2; pos=get(textobjs,{'position'});textpos=cat(1,pos{:});textpos(:,1)=textpos(:,1)+offset;set(textobjs,{'position'},num2cell(textpos,[3,2]))实例10:阶梯图function shili10h0=figure('toolbar','none',...'position',[200 150 450 400],...'name','实例10');a=0.01;b=0.5;t=0:10;f=exp(-a*t).*sin(b*t);stairs(t,f)hold onplot(t,f,':*')hold offglabel='函数e^{-(\alpha*t)}sin\beta*t的阶梯图'; gtext(glabel,'fontsize',16)xlabel('t=0:10','fontsize',16)axis([0 10 -1.2 1.2])实例11:枝干图function shili11h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例11');x=0:pi/20:2*pi;y1=sin(x);y2=cos(x);h1=stem(x,y1+y2);hold onh2=plot(x,y1,'^r',x,y2,'*g');hold offh3=[h1(1);h2];legend(h3,'y1+y2','y1=sin(x)','y2=cos(x)') xlabel('自变量X');ylabel('函数值Y');title('正弦函数与余弦函数的线性组合'); 实例12:罗盘图function shili12h0=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/180;[x,y]=pol2cart(rdirection,windpower); compass(x,y);desc={'风向和风力','北京气象台','10月1日0:00到','10月1日12:00'};gtext(desc)实例13:轮廓图function shili13h0=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.^4-1).^(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 shili14h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例14');axis([0 10 0 10]);hold onx=[];y=[];n=0;disp('单击鼠标左键点取需要的点'); disp('单击鼠标右键点取最后一个点'); but=1;while but==1[xi,yi,but]=ginput(1);plot(xi,yi,'bo')n=n+1;disp('单击鼠标左键点取下一个点');x(n,1)=xi;y(n,1)=yi;endt=1:n;ts=1:0.1:n;xs=spline(t,x,ts);ys=spline(t,y,ts);plot(xs,ys,'r-');hold off实例14:交互式图形function shili14h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例14');axis([0 10 0 10]);hold onx=[];y=[];n=0;disp('单击鼠标左键点取需要的点'); disp('单击鼠标右键点取最后一个点'); but=1;while but==1[xi,yi,but]=ginput(1);plot(xi,yi,'bo')n=n+1;disp('单击鼠标左键点取下一个点');x(n,1)=xi;y(n,1)=yi;endt=1:n;ts=1:0.1:n;xs=spline(t,x,ts);ys=spline(t,y,ts);plot(xs,ys,'r-');hold off实例15:变换的傅立叶函数曲线function shili15h0=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],'min',1,'max',20)for j=1:20plot(fft(eye(j+16)))set(h,'value',j)m(:,j)=getframe(gcf);endclf;axes('position',[0 0 1 1]);movie(m,30)实例16:劳伦兹非线形方程的无序活动function shili15h0=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],'min',1,'max',20)for j=1:20plot(fft(eye(j+16)))set(h,'value',j)m(:,j)=getframe(gcf);endclf;axes('position',[0 0 1 1]);movie(m,30)实例17:填充图function shili17h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例17');t=(1:2:15)*pi/8;x=sin(t);y=cos(t);fill(x,y,'r')axis square offtext(0,0,'STOP',...'color',[1 1 1],...'fontsize',50,...'horizontalalignment','center') 例18:条形图和阶梯形图function shili18h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例18');subplot(2,2,1)x=-3:0.2:3;y=exp(-x.*x);bar(x,y)title('2-D Bar Chart')subplot(2,2,2)x=-3:0.2:3;y=exp(-x.*x);bar3(x,y,'r')title('3-D Bar Chart')subplot(2,2,3)x=-3:0.2:3;y=exp(-x.*x);stairs(x,y)title('Stair Chart')subplot(2,2,4)x=-3:0.2:3;y=exp(-x.*x);barh(x,y)title('Horizontal Bar Chart')实例19:三维曲线图function shili19h0=figure('toolbar','none',...'position',[200 150 450 400],...'name','实例19');subplot(2,1,1)x=linspace(0,2*pi);y1=sin(x);y2=cos(x);y3=sin(x)+cos(x);z1=zeros(size(x));z2=0.5*z1;z3=z1;plot3(x,y1,z1,x,y2,z2,x,y3,z3)grid onxlabel('X轴');ylabel('Y轴');zlabel('Z轴');title('Figure1:3-D Plot')subplot(2,1,2)x=linspace(0,2*pi);y1=sin(x);y2=cos(x);y3=sin(x)+cos(x);z1=zeros(size(x));z2=0.5*z1;z3=z1;plot3(x,z1,y1,x,z2,y2,x,z3,y3)grid onxlabel('X轴');ylabel('Y轴');zlabel('Z轴');title('Figure2:3-D Plot')实例20:图形的隐藏属性function shili20h0=figure('toolbar','none',...'position',[200 150 450 300],...'name','实例20');subplot(1,2,1)[x,y,z]=sphere(10);mesh(x,y,z)axis offtitle('Figure1:Opaque')hidden onsubplot(1,2,2)[x,y,z]=sphere(10);mesh(x,y,z)axis offtitle('Figure2:Transparent') hidden off实例21PEAKS函数曲线function shili21h0=figure('toolbar','none',...'position',[200 100 450 450],...'name','实例21');[x,y,z]=peaks(30);subplot(2,1,1)x=x(1,:);y=y(:,1);i=find(y>0.8&y<1.2);j=find(x>-0.6&x<0.5);z(i,j)=nan*z(i,j);surfc(x,y,z)xlabel('X轴');ylabel('Y轴');zlabel('Z轴');title('Figure1:surfc函数形成的曲面') subplot(2,1,2)x=x(1,:);y=y(:,1);i=find(y>0.8&y<1.2);j=find(x>-0.6&x<0.5);z(i,j)=nan*z(i,j);surfl(x,y,z)xlabel('X轴');ylabel('Y轴');zlabel('Z轴');title('Figure2:surfl函数形成的曲面') 实例22:片状图function shili22h0=figure('toolbar','none',...'position',[200 150 550 350],...'name','实例22');subplot(1,2,1)x=rand(1,20);y=rand(1,20);z=peaks(x,y*pi);t=delaunay(x,y);trimesh(t,x,y,z)hidden offtitle('Figure1:Triangular Surface Plot'); subplot(1,2,2)x=rand(1,20);y=rand(1,20);z=peaks(x,y*pi);t=delaunay(x,y);trisurf(t,x,y,z)title('Figure1:Triangular Surface Plot'); 实例23:视角的调整function shili23h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例23');x=-5:0.5:5;[x,y]=meshgrid(x);r=sqrt(x.^2+y.^2)+eps;z=sin(r)./r;subplot(2,2,1)surf(x,y,z)xlabel('X-axis')ylabel('Y-axis')zlabel('Z-axis')title('Figure1')view(-37.5,30)subplot(2,2,2)surf(x,y,z)xlabel('X-axis')ylabel('Y-axis')zlabel('Z-axis')title('Figure2')view(-37.5+90,30) subplot(2,2,3)surf(x,y,z)xlabel('X-axis')ylabel('Y-axis')zlabel('Z-axis')title('Figure3')view(-37.5,60)subplot(2,2,4)surf(x,y,z)xlabel('X-axis')ylabel('Y-axis')zlabel('Z-axis')title('Figure4')view(180,0)实例24:向量场的绘制function shili24h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例24');subplot(2,2,1)z=peaks;ribbon(z)title('Figure1')subplot(2,2,2)[x,y,z]=peaks(15);[dx,dy]=gradient(z,0.5,0.5); contour(x,y,z,10)hold onquiver(x,y,dx,dy)hold offtitle('Figure2')subplot(2,2,3)[x,y,z]=peaks(15);[nx,ny,nz]=surfnorm(x,y,z);surf(x,y,z)hold onquiver3(x,y,z,nx,ny,nz)hold offtitle('Figure3')subplot(2,2,4)x=rand(3,5);y=rand(3,5);z=rand(3,5);c=rand(3,5);fill3(x,y,z,c)grid ontitle('Figure4')实例25:灯光定位function shili25h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例25');vert=[1 1 1;1 2 1;2 2 1;2 1 1;1 1 2;12 2;2 2 2;2 1 2];fac=[1 2 3 4;2 6 7 3;4 3 7 8;15 8 4;1 2 6 5;5 6 7 8];grid offsphere(36)h=findobj('type','surface');set(h,'facelighting','phong',...'facecolor',...'interp',...'edgecolor',[0.4 0.4 0.4],...'backfacelighting',...'lit')hold onpatch('faces',fac,'vertices',vert,...'facecolor','y');light('position',[1 3 2]);light('position',[-3 -1 3]);material shinyaxis vis3d offhold off实例26:柱状图function shili26h0=figure('toolbar','none',...'position',[200 50 450 450],...'name','实例26'); subplot(2,1,1)x=[5 2 18 7 39 8 65 5 54 3 2];bar(x)xlabel('X轴');ylabel('Y轴');title('第一子图');subplot(2,1,2)y=[5 2 18 7 39 8 65 5 54 3 2];barh(y)xlabel('X轴');ylabel('Y轴');title('第二子图');实例27:设置照明方式function shili27h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例27');subplot(2,2,1)sphereshading flatcamlight leftcamlight rightlighting flatcolorbaraxis offtitle('Figure1')subplot(2,2,2)sphereshading flatcamlight leftcamlight rightlighting gouraudcolorbaraxis offtitle('Figure2')subplot(2,2,3)sphereshading interpcamlight rightcamlight leftlighting phongaxis offtitle('Figure3')subplot(2,2,4)sphereshading flatcamlight leftcamlight rightlighting nonecolorbaraxis offtitle('Figure4')实例28:羽状图function shili28h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例28');subplot(2,1,1)alpha=90:-10:0;r=ones(size(alpha));m=alpha*pi/180;n=r*10;[u,v]=pol2cart(m,n);feather(u,v)title('羽状图')axis([0 20 0 10])subplot(2,1,2)t=0:0.5:10;y=exp(-x*t);feather(y)title('复数矩阵的羽状图')实例29:立体透视(1)function shili29h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例29');[x,y,z]=meshgrid(-2:0.1:2,...-2:0.1:2,...-2:0.1:2);v=x.*exp(-x.^2-y.^2-z.^2);grid onfor i=-2:0.5:2;h1=surf(linspace(-2,2,20),...linspace(-2,2,20),...zeros(20)+i);rotate(h1,[1 -1 1],30)dx=get(h1,'xdata');dy=get(h1,'ydata');dz=get(h1,'zdata');delete(h1)slice(x,y,z,v,[-2 2],2,-2)hold onslice(x,y,z,v,dx,dy,dz)hold offaxis tightview(-5,10)drawnowend实例30:立体透视(2)function shili30h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例30');[x,y,z]=meshgrid(-2:0.1:2,...-2:0.1:2,...-2:0.1:2);v=x.*exp(-x.^2-y.^2-z.^2); [dx,dy,dz]=cylinder;slice(x,y,z,v,[-2 2],2,-2)for i=-2:0.2:2h=surface(dx+i,dy,dz);rotate(h,[1 0 0],90)xp=get(h,'xdata');yp=get(h,'ydata');zp=get(h,'zdata');delete(h)hold onhs=slice(x,y,z,v,xp,yp,zp);axis tightxlim([-3 3])view(-10,35)drawnowdelete(hs)hold offend实例31:表面图形function shili31h0=figure('toolbar','none',...'position',[200 150 550 250],...'name','实例31');subplot(1,2,1)x=rand(100,1)*16-8;y=rand(100,1)*16-8;r=sqrt(x.^2+y.^2)+eps;z=sin(r)./r;xlin=linspace(min(x),max(x),33); ylin=linspace(min(y),max(y),33); [X,Y]=meshgrid(xlin,ylin);Z=griddata(x,y,z,X,Y,'cubic'); mesh(X,Y,Z)axis tighthold onplot3(x,y,z,'.','Markersize',20) subplot(1,2,2)k=5;n=2^k-1;theta=pi*(-n:2:n)/n;phi=(pi/2)*(-n:2:n)'/n;X=cos(phi)*cos(theta);Y=cos(phi)*sin(theta);Z=sin(phi)*ones(size(theta)); colormap([0 0 0;1 1 1])C=hadamard(2^k);surf(X,Y,Z,C)axis square实例32:沿曲线移动的小球h0=figure('toolbar','none',...'position',[198****8468],...'name','实例32');h1=axes('parent',h0,...'position',[0.15 0.45 0.7 0.5],...'visible','on');t=0:pi/24:4*pi;y=sin(t);plot(t,y,'b')n=length(t);h=line('color',[0 0.5 0.5],...'linestyle','.',...'markersize',25,...'erasemode','xor');k1=uicontrol('parent',h0,...'style','pushbutton',...'position',[80 100 50 30],...'string','开始',...'callback',[...'i=1;',...'k=1;,',...'m=0;,',...'while 1,',...'if k==0,',...'break,',...'end,',...'if k~=0,',...'set(h,''xdata'',t(i),''ydata'',y(i)),',...'drawnow;,',...'i=i+1;,',...'if i>n,',...'m=m+1;,',...'i=1;,',...'end,',...'end,',...'end']);k2=uicontrol('parent',h0,...'style','pushbutton',...'position',[180 100 50 30],...'string','停止',...'callback',[...'k=0;,',...'set(e1,''string'',m),',...'p=get(h,''xdata'');,',...'q=get(h,''ydata'');,',...'set(e2,''string'',p);,',...'set(e3,''string'',q)']); k3=uicontrol('parent',h0,...'style','pushbutton',...'position',[280 100 50 30],...'string','关闭',...'callback','close');e1=uicontrol('parent',h0,...'style','edit',...'position',[60 30 60 20]);t1=uicontrol('parent',h0,...'style','text',...'string','循环次数',...'position',[60 50 60 20]);e2=uicontrol('parent',h0,...'style','edit',...'position',[180 30 50 20]);t2=uicontrol('parent',h0,...'style','text',...'string','终点的X坐标值',...'position',[155 50 100 20]);e3=uicontrol('parent',h0,...'style','edit',...'position',[300 30 50 20]);t3=uicontrol('parent',h0,...'style','text',...'string','终点的Y坐标值',...'position',[275 50 100 20]);实例33:曲线转换按钮h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例33');x=0:0.5:2*pi;y=sin(x);h=plot(x,y);grid onhuidiao=[...'if i==1,',...'i=0;,',...'y=cos(x);,',...'delete(h),',...'set(hm,''string'',''正弦函数''),',...'h=plot(x,y);,',...'grid on,',...'else if i==0,',...'i=1;,',...'y=sin(x);,',...'set(hm,''string'',''余弦函数''),',...'delete(h),',...'h=plot(x,y);,',...'grid on,',...'end,',...'end'];hm=uicontrol(gcf,'style','pushbutton',...'string','余弦函数',...'callback',huidiao);i=1;set(hm,'position',[250 20 60 20]);set(gca,'position',[0.2 0.2 0.6 0.6])title('按钮的使用')hold on实例34:栅格控制按钮h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例34');x=0:0.5:2*pi;y=sin(x);plot(x,y)huidiao1=[...'set(h_toggle2,''value'',0),',...'grid on,',...];huidiao2=[...'set(h_toggle1,''value'',0),',...'grid off,',...];h_toggle1=uicontrol(gcf,'style','togglebutton',...'string','grid on',...'value',0,...'position',[20 45 50 20],...'callback',huidiao1);h_toggle2=uicontrol(gcf,'style','togglebutton',...'string','grid off',...'value',0,...'position',[20 20 50 20],...'callback',huidiao2);set(gca,'position',[0.2 0.2 0.6 0.6])title('开关按钮的使用')实例35:编辑框的使用h0=figure('toolbar','none',...'position',[200 150 350 250],...'name','实例35');f='Please input the letter';huidiao1=[...'g=upper(f);,',...'set(h2_edit,''string'',g),',...];huidiao2=[...'g=lower(f);,',...'set(h2_edit,''string'',g),',...];h1_edit=uicontrol(gcf,'style','edit',...'position',[100 200 100 50],...'HorizontalAlignment','left',...'string','Please input the letter',...'callback','f=get(h1_edit,''string'');',...'background','w',...'max',5,...'min',1);h2_edit=uicontrol(gcf,'style','edit',...'HorizontalAlignment','left',...'position',[100 100 100 50],...'background','w',...'max',5,...'min',1);h1_button=uicontrol(gcf,'style','pushbutton',...'string','小写变大写',...'position',[100 45 100 20],...'callback',huidiao1);h2_button=uicontrol(gcf,'style','pushbutton',...'string','大写变小写',...'position',[100 20 100 20],...'callback',huidiao2);实例36:弹出式菜单h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例36');x=0:0.5:2*pi;y=sin(x);h=plot(x,y);grid onhm=uicontrol(gcf,'style','popupmenu',...'string',...'sin(x)|cos(x)|sin(x)+cos(x)|exp(-sin(x))',...'position',[250 20 50 20]);set(hm,'value',1)huidiao=[...'v=get(hm,''value'');,',...'switch v,',...'case 1,',...'delete(h),',...'y=sin(x);,',...'h=plot(x,y);,',...'grid on,',...'case 2,',...'delete(h),',...'y=cos(x);,',...'h=plot(x,y);,',...'grid on,',...'case 3,',...'delete(h),',...'y=sin(x)+cos(x);,',...'h=plot(x,y);,',...'grid on,',...'case 4,',...'delete(h),',...'y=exp(-sin(x));,',...'h=plot(x,y);,',...'grid on,',...'end'];set(hm,'callback',huidiao)set(gca,'position',[0.2 0.2 0.6 0.6]) title('弹出式菜单的使用')实例37:滑标的使用h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例37');[x,y]=meshgrid(-8:0.5:8);r=sqrt(x.^2+y.^2)+eps;z=sin(r)./r;h0=mesh(x,y,z);h1=axes('position',...[0.2 0.2 0.5 0.5],...'visible','off');htext=uicontrol(gcf,...'units','points',...'position',[20 30 45 15],...'string','brightness',...'style','text');hslider=uicontrol(gcf,...'units','points',...'position',[10 10 300 15],...'min',-1,...'max',1,...'style','slider',...'callback',...'brighten(get(hslider,''value''))'); 实例38:多选菜单h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例38');[x,y]=meshgrid(-8:0.5:8);r=sqrt(x.^2+y.^2)+eps;z=sin(r)./r;h0=mesh(x,y,z);hlist=uicontrol(gcf,'style','listbox',...'string','default|spring|summer|autumn|winter',...'max',5,...'min',1,...'position',[20 20 80 100],...'callback',[...'k=get(hlist,''value'');,',...'switch k,',...'case 1,',...'colormap default,',...'case 2,',...'colormap spring,',...'case 3,',...'colormap summer,',...'case 4,',...'colormap autumn,',...'case 5,',...'colormap winter,',...'end']);实例39:菜单控制的使用h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例39');x=0:0.5:2*pi;y=cos(x);h=plot(x,y);grid onset(gcf,'toolbar','none')hm=uimenu('label','example');huidiao1=[...'set(hm_gridon,''checked'',''on''),',...'set(hm_gridoff,''checked'',''off''),',...'grid on'];huidiao2=[...'set(hm_gridoff,''checked'',''on''),',...'set(hm_gridon,''checked'',''off''),',...'grid off'];hm_gridon=uimenu(hm,'label','grid on',...'checked','on',...'callback',huidiao1);hm_gridoff=uimenu(hm,'label','grid off',...'checked','off',...'callback',huidiao2);实例40:UIMENU菜单的应用h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例40');h1=uimenu(gcf,'label','函数');h11=uimenu(h1,'label','轮廓图',...'callback',[...'set(h31,''checked'',''on''),',...'set(h32,''checked'',''off''),',...'[x,y,z]=peaks;,',...'contour3(x,y,z,30)']);h12=uimenu(h1,'label','高斯分布',...'callback',[...'set(h31,''checked'',''on''),',...'set(h32,''checked'',''off''),',...'mesh(peaks);,',...'axis tight']);。

matlab实用程序百例3

matlab实用程序百例3
'style','pushbutton',...
'string','区域过滤一',...
'backgroundcolor',[0.75 0.75 0.75],...
'callback',[...
'cla,',...
'k=3;,',...
'I=imread(''rice.tif'');,',...
'BW=edge(I,''canny'');,',...
'imshow(BW)']);
'I=imread(''rice.tif'');,',...
'imcontour(I)']);
b2=uicontrol('parent',h0,...
'units','points',...
'tag','b2',...
'style','pushbutton',...
u11=uimenu('parent',u1,...
'tag','u11',...
'label','SALT&PEPPER噪声',...
'backgroundcolor',[0.75 0.75 0.75],...

matlab实用程序实例

matlab实用程序实例

实例1:三角函数曲线(1)function shili01h0=figure('toolbar','none',...'position',[198 56 350 300],...'name','实例01');h1=axes('parent',h0,...'visible','off');x=-pi:0.05:pi;y=sin(x);plot(x,y);xlabel('自变量X');ylabel('函数值Y');title('SIN( )函数曲线');grid on实例2:三角函数曲线(2)function shili02h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例02');x=-pi:0.05:pi;y=sin(x)+cos(x);plot(x,y,'-*r','linewidth',1);grid onxlabel('自变量X');ylabel('函数值Y');title('三角函数');实例3:图形的叠加function shili03h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例03');x=-pi:0.05:pi;y1=sin(x);y2=cos(x);plot(x,y1,...'-*r',...x,y2,...'--og');grid onxlabel('自变量X');ylabel('函数值Y');title('三角函数');实例4:双y轴图形的绘制function shili04h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例04');x=0:900;a=1000;b=0.005;y1=2*x;y2=cos(b*x);[haxes,hline1,hline2]=plotyy(x,y1,x,y2,'semilogy','plot'); axes(haxes(1))ylabel('semilog plot');axes(haxes(2))ylabel('linear plot');实例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,1)plot(sin(t),cos(t))axis equalsubplot(2,2,2)z=sin(x)-cos(y);plot(t,z)axis([0 2*pi -2 2])subplot(2,2,3)h=sin(x)+cos(y);plot(t,h)axis([0 2*pi -2 2])subplot(2,2,4)g=(sin(x).^2)-(cos(y).^2);plot(t,g)axis([0 2*pi -1 1])实例6:图形标注function shili06h0=figure('toolbar','none',...'position',[200 150 450 400],...'name','实例06');t=0:pi/10:2*pi;h=plot(t,sin(t));xlabel('t=0到2\pi','fontsize',16);ylabel('sin(t)','fontsize',16);title('\it{从0to2\pi 的正弦曲线}','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),...['\leftarrow最小值=',num2str(y(imin))],...'fontsize',16)text(x(imax),y(imax),...['\leftarrow最大值=',num2str(y(imax))],...'fontsize',16)实例7:条形图形function shili07h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例07');tiao1=[562 548 224 545 41 445 745 512];tiao2=[47 48 57 58 54 52 65 48];t=0:7;bar(t,tiao1)xlabel('X轴');ylabel('TIAO1值');h1=gca;h2=axes('position',get(h1,'position'));plot(t,tiao2,'linewidth',3)set(h2,'yaxislocation','right','color','none','xticklabel',[]) 实例8:区域图形function shili08h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例08');x=91:95;profits1=[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,profits1,'facecolor',[0.5 0.9 0.6],...'edgecolor','b',...'linewidth',3)hold onarea(x,profits2,'facecolor',[0.9 0.85 0.7],...'edgecolor','y',...'linewidth',3)hold onarea(x,profits3,'facecolor',[0.3 0.6 0.7],...'edgecolor','r',...'linewidth',3)hold onarea(x,profits4,'facecolor',[0.6 0.5 0.9],...'edgecolor','m',...'linewidth',3)hold offset(gca,'xtick',[91:95])set(gca,'layer','top')gtext('\leftarrow第一季度销量')gtext('\leftarrow第二季度销量')gtext('\leftarrow第三季度销量')gtext('\leftarrow第四季度销量')xlabel('年','fontsize',16);ylabel('销售量','fontsize',16);实例9:饼图的绘制function shili09h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例09');t=[54 21 35;68 54 35;45 25 12;48 68 45;68 54 69];x=sum(t);h=pie(x);textobjs=findobj(h,'type','text');str1=get(textobjs,{'string'});val1=get(textobjs,{'extent'});oldext=cat(1,val1{:});names={'商品一:';'商品二:';'商品三:'};str2=strcat(names,str1);set(textobjs,{'string'},str2)val2=get(textobjs,{'extent'});newext=cat(1,val2{:});offset=sign(oldext(:,1)).*(newext(:,3)-oldext(:,3))/2; pos=get(textobjs,{'position'});textpos=cat(1,pos{:});textpos(:,1)=textpos(:,1)+offset;set(textobjs,{'position'},num2cell(textpos,[3,2]))实例10:阶梯图function shili10h0=figure('toolbar','none',...'position',[200 150 450 400],...'name','实例10');a=0.01;b=0.5;t=0:10;f=exp(-a*t).*sin(b*t);stairs(t,f)hold onplot(t,f,':*')hold offglabel='函数e^{-(\alpha*t)}sin\beta*t的阶梯图'; gtext(glabel,'fontsize',16)xlabel('t=0:10','fontsize',16)axis([0 10 -1.2 1.2])。

Matlab100个实例程序

Matlab100个实例程序

程序代码:(代码标记[code]...[/code] ) 1-32是:图形应用篇33-66是:界面设计篇67-84是:图形处理篇85-100是:数值分析篇实例1:三角函数曲线(1)function shili01h0=figure('toolbar','none',...'position',[198****0300],...'name','实例01');h1=axes('parent',h0,...'visible','off');x=-pi:0.05:pi;y=sin(x);plot(x,y);xlabel('自变量X');ylabel('函数值Y');title('SIN( )函数曲线');grid on实例2:三角函数曲线(2)function shili02h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例02');x=-pi:0.05:pi;y=sin(x)+cos(x);plot(x,y,'-*r','linewidth',1);grid onxlabel('自变量X');ylabel('函数值Y');title('三角函数');实例3:图形的叠加function shili03h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例03');x=-pi:0.05:pi;y1=sin(x);y2=cos(x);plot(x,y1,...'-*r',...x,y2,...'--og');grid onxlabel('自变量X');ylabel('函数值Y');title('三角函数');实例4:双y轴图形的绘制function shili04h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例04');x=0:900;a=1000;b=0.005;y1=2*x;y2=cos(b*x);[haxes,hline1,hline2]=plotyy(x,y1,x,y2,'semilogy','plot'); axes(haxes(1))ylabel('semilog plot');axes(haxes(2))ylabel('linear plot');实例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,1)plot(sin(t),cos(t))axis equalsubplot(2,2,2)z=sin(x)-cos(y);plot(t,z)axis([0 2*pi -2 2])subplot(2,2,3)h=sin(x)+cos(y);plot(t,h)axis([0 2*pi -2 2])subplot(2,2,4)g=(sin(x).^2)-(cos(y).^2);plot(t,g)axis([0 2*pi -1 1])实例6:图形标注function shili06h0=figure('toolbar','none',...'position',[200 150 450 400],...'name','实例06');t=0:pi/10:2*pi;h=plot(t,sin(t));xlabel('t=0到2\pi','fontsize',16);ylabel('sin(t)','fontsize',16);title('\it{从0to2\pi 的正弦曲线}','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),...['\leftarrow最小值=',num2str(y(imin))],... 'fontsize',16)text(x(imax),y(imax),...['\leftarrow最大值=',num2str(y(imax))],...'fontsize',16)实例7:条形图形function shili07h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例07');tiao1=[562 548 224 545 41 445 745 512];tiao2=[47 48 57 58 54 52 65 48];t=0:7;bar(t,tiao1)xlabel('X轴');ylabel('TIAO1值');h1=gca;h2=axes('position',get(h1,'position'));plot(t,tiao2,'linewidth',3)set(h2,'yaxislocation','right','color','none','xticklabel',[])实例8:区域图形function shili08h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例08');x=91:95;profits1=[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,profits1,'facecolor',[0.5 0.9 0.6],...'edgecolor','b',...'linewidth',3)hold onarea(x,profits2,'facecolor',[0.9 0.85 0.7],...'edgecolor','y',...'linewidth',3)hold onarea(x,profits3,'facecolor',[0.3 0.6 0.7],... 'edgecolor','r',...'linewidth',3)hold onarea(x,profits4,'facecolor',[0.6 0.5 0.9],... 'edgecolor','m',...'linewidth',3)hold offset(gca,'xtick',[91:95])set(gca,'layer','top')gtext('\leftarrow第一季度销量')gtext('\leftarrow第二季度销量')gtext('\leftarrow第三季度销量')gtext('\leftarrow第四季度销量')xlabel('年','fontsize',16);ylabel('销售量','fontsize',16);实例9:饼图的绘制function shili09h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例09');t=[54 21 35;68 54 35;45 25 12;48 68 45;68 54 69];x=sum(t);h=pie(x);textobjs=findobj(h,'type','text');str1=get(textobjs,{'string'});val1=get(textobjs,{'extent'});oldext=cat(1,val1{:});names={'商品一:';'商品二:';'商品三:'}; str2=strcat(names,str1);set(textobjs,{'string'},str2)val2=get(textobjs,{'extent'});newext=cat(1,val2{:});offset=sign(oldext(:,1)).*(newext(:,3)-oldext(:,3))/2; pos=get(textobjs,{'position'});textpos=cat(1,pos{:});textpos(:,1)=textpos(:,1)+offset;set(textobjs,{'position'},num2cell(textpos,[3,2]))实例10:阶梯图function shili10h0=figure('toolbar','none',...'position',[200 150 450 400],...'name','实例10');a=0.01;b=0.5;t=0:10;f=exp(-a*t).*sin(b*t);stairs(t,f)hold onplot(t,f,':*')hold offglabel='函数e^{-(\alpha*t)}sin\beta*t的阶梯图'; gtext(glabel,'fontsize',16)xlabel('t=0:10','fontsize',16)axis([0 10 -1.2 1.2])实例11:枝干图function shili11h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例11');x=0:pi/20:2*pi;y1=sin(x);y2=cos(x);h1=stem(x,y1+y2);hold onh2=plot(x,y1,'^r',x,y2,'*g');h3=[h1(1);h2];legend(h3,'y1+y2','y1=sin(x)','y2=cos(x)') xlabel('自变量X');ylabel('函数值Y');title('正弦函数与余弦函数的线性组合');实例12:罗盘图function shili12h0=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/180;[x,y]=pol2cart(rdirection,windpower); compass(x,y);desc={'风向和风力','北京气象台','10月1日0:00到','10月1日12:00'};gtext(desc)实例13:轮廓图function shili13h0=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.^4-1).^(0.25);contour(x,y,abs(f),20)xlabel('实部','fontsize',16);ylabel('虚部','fontsize',16);h=polar([0 2*pi],[0 1]);delete(h)hold oncontour(x,y,abs(f),20)实例14:交互式图形function shili14h0=figure('toolbar','none',...'position',[200 150 450 250],... 'name','实例14');axis([0 10 0 10]);hold onx=[];y=[];n=0;disp('单击鼠标左键点取需要的点'); disp('单击鼠标右键点取最后一个点'); but=1;while but==1[xi,yi,but]=ginput(1);plot(xi,yi,'bo')n=n+1;disp('单击鼠标左键点取下一个点'); x(n,1)=xi;y(n,1)=yi;endt=1:n;ts=1:0.1:n;xs=spline(t,x,ts);ys=spline(t,y,ts);plot(xs,ys,'r-');hold off实例15:变换的傅立叶函数曲线function shili15h0=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],'min',1,'max',20) for j=1:20plot(fft(eye(j+16)))set(h,'value',j)m(:,j)=getframe(gcf);endclf;axes('position',[0 0 1 1]);movie(m,30)实例16:劳伦兹非线形方程的无序活动function shili15h0=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],'min',1,'max',20) for j=1:20plot(fft(eye(j+16)))set(h,'value',j)m(:,j)=getframe(gcf);endclf;axes('position',[0 0 1 1]);movie(m,30)实例17:填充图function shili17h0=figure('toolbar','none',...'position',[200 150 450 250],... 'name','实例17');t=(1:2:15)*pi/8;x=sin(t);y=cos(t);fill(x,y,'r')axis square offtext(0,0,'STOP',...'color',[1 1 1],...'fontsize',50,...'horizontalalignment','center')实例18:条形图和阶梯形图function shili18h0=figure('toolbar','none',...'position',[200 150 450 250],... 'name','实例18');subplot(2,2,1)x=-3:0.2:3;y=exp(-x.*x);bar(x,y)title('2-D Bar Chart')subplot(2,2,2)x=-3:0.2:3;y=exp(-x.*x);bar3(x,y,'r')title('3-D Bar Chart')subplot(2,2,3)x=-3:0.2:3;y=exp(-x.*x);stairs(x,y)title('Stair Chart')subplot(2,2,4)x=-3:0.2:3;y=exp(-x.*x);barh(x,y)title('Horizontal Bar Chart')实例19:三维曲线图function shili19h0=figure('toolbar','none',...'position',[200 150 450 400],... 'name','实例19');subplot(2,1,1)x=linspace(0,2*pi);y1=sin(x);y2=cos(x);y3=sin(x)+cos(x);z1=zeros(size(x));z2=0.5*z1;z3=z1;plot3(x,y1,z1,x,y2,z2,x,y3,z3) grid onxlabel('X轴');ylabel('Y轴');zlabel('Z轴');title('Figure1:3-D Plot')subplot(2,1,2)x=linspace(0,2*pi);y1=sin(x);y2=cos(x);y3=sin(x)+cos(x);z1=zeros(size(x));z2=0.5*z1;z3=z1;plot3(x,z1,y1,x,z2,y2,x,z3,y3) grid onxlabel('X轴');zlabel('Z轴');title('Figure2:3-D Plot')实例20:图形的隐藏属性function shili20h0=figure('toolbar','none',...'position',[200 150 450 300],... 'name','实例20');subplot(1,2,1)[x,y,z]=sphere(10);mesh(x,y,z)axis offtitle('Figure1:Opaque')hidden onsubplot(1,2,2)[x,y,z]=sphere(10);mesh(x,y,z)axis offtitle('Figure2:Transparent') hidden off实例21:PEAKS函数曲线function shili21h0=figure('toolbar','none',...'position',[200 100 450 450],... 'name','实例21');[x,y,z]=peaks(30);subplot(2,1,1)x=x(1,:);y=y(:,1);i=find(y>0.8&y<1.2);j=find(x>-0.6&x<0.5);z(i,j)=nan*z(i,j);surfc(x,y,z)xlabel('X轴');ylabel('Y轴');title('Figure1:surfc函数形成的曲面')subplot(2,1,2)x=x(1,:);y=y(:,1);i=find(y>0.8&y<1.2);j=find(x>-0.6&x<0.5);z(i,j)=nan*z(i,j);surfl(x,y,z)xlabel('X轴');ylabel('Y轴');zlabel('Z轴');title('Figure2:surfl函数形成的曲面')实例22:片状图function shili22h0=figure('toolbar','none',...'position',[200 150 550 350],...'name','实例22');subplot(1,2,1)x=rand(1,20);y=rand(1,20);z=peaks(x,y*pi);t=delaunay(x,y);trimesh(t,x,y,z)hidden offtitle('Figure1:Triangular Surface Plot');subplot(1,2,2)x=rand(1,20);y=rand(1,20);z=peaks(x,y*pi);t=delaunay(x,y);trisurf(t,x,y,z)title('Figure1:Triangular Surface Plot');实例23:视角的调整function shili23h0=figure('toolbar','none',...'position',[200 150 450 350],... 'name','实例23');x=-5:0.5:5;[x,y]=meshgrid(x);r=sqrt(x.^2+y.^2)+eps;z=sin(r)./r;subplot(2,2,1)surf(x,y,z)xlabel('X-axis')ylabel('Y-axis')zlabel('Z-axis')title('Figure1')view(-37.5,30)subplot(2,2,2)surf(x,y,z)xlabel('X-axis')ylabel('Y-axis')zlabel('Z-axis')title('Figure2')view(-37.5+90,30)subplot(2,2,3)surf(x,y,z)xlabel('X-axis')ylabel('Y-axis')zlabel('Z-axis')title('Figure3')view(-37.5,60)subplot(2,2,4)surf(x,y,z)xlabel('X-axis')ylabel('Y-axis')zlabel('Z-axis')title('Figure4')view(180,0)实例24:向量场的绘制function shili24h0=figure('toolbar','none',...'position',[200 150 450 350],... 'name','实例24');subplot(2,2,1)z=peaks;ribbon(z)title('Figure1')subplot(2,2,2)[x,y,z]=peaks(15);[dx,dy]=gradient(z,0.5,0.5); contour(x,y,z,10)hold onquiver(x,y,dx,dy)hold offtitle('Figure2')subplot(2,2,3)[x,y,z]=peaks(15);[nx,ny,nz]=surfnorm(x,y,z);surf(x,y,z)hold onquiver3(x,y,z,nx,ny,nz)hold offtitle('Figure3')subplot(2,2,4)x=rand(3,5);y=rand(3,5);z=rand(3,5);c=rand(3,5);fill3(x,y,z,c)grid ontitle('Figure4')实例25:灯光定位function shili25h0=figure('toolbar','none',...'position',[200 150 450 250],... 'name','实例25');vert=[1 1 1;1 2 1;2 2 1;2 1 1;1 1 2;12 2;2 2 2;2 1 2];fac=[1 2 3 4;2 6 7 3;4 3 7 8;15 8 4;1 2 6 5;5 6 7 8];grid offsphere(36)h=findobj('type','surface');set(h,'facelighting','phong',...'facecolor',...'interp',...'edgecolor',[0.4 0.4 0.4],...'backfacelighting',...'lit')hold onpatch('faces',fac,'vertices',vert,... 'facecolor','y');light('position',[1 3 2]);light('position',[-3 -1 3]); material shinyaxis vis3d offhold off实例26:柱状图function shili26h0=figure('toolbar','none',...'position',[200 50 450 450],...'name','实例26');subplot(2,1,1)x=[5 2 18 7 39 8 65 5 54 3 2];bar(x)xlabel('X轴');ylabel('Y轴');title('第一子图');subplot(2,1,2)y=[5 2 18 7 39 8 65 5 54 3 2];barh(y)xlabel('X轴');ylabel('Y轴');title('第二子图');实例27:设置照明方式function shili27h0=figure('toolbar','none',...'position',[200 150 450 350],... 'name','实例27');subplot(2,2,1)sphereshading flatcamlight leftcamlight rightlighting flatcolorbaraxis offtitle('Figure1')subplot(2,2,2)sphereshading flatcamlight leftcamlight rightlighting gouraudcolorbaraxis offtitle('Figure2')subplot(2,2,3)sphereshading interpcamlight rightcamlight leftlighting phongcolorbaraxis offtitle('Figure3')subplot(2,2,4)sphereshading flatcamlight leftcamlight rightlighting nonecolorbaraxis offtitle('Figure4')实例28:羽状图function shili28h0=figure('toolbar','none',...'position',[200 150 450 350],... 'name','实例28');subplot(2,1,1)alpha=90:-10:0;r=ones(size(alpha));m=alpha*pi/180;n=r*10;[u,v]=pol2cart(m,n);feather(u,v)title('羽状图')axis([0 20 0 10])subplot(2,1,2)t=0:0.5:10;x=0.05+i;y=exp(-x*t);feather(y)title('复数矩阵的羽状图')实例29:立体透视(1)function shili29h0=figure('toolbar','none',...'position',[200 150 450 250],... 'name','实例29');[x,y,z]=meshgrid(-2:0.1:2,...-2:0.1:2,...-2:0.1:2);v=x.*exp(-x.^2-y.^2-z.^2); grid onfor i=-2:0.5:2;h1=surf(linspace(-2,2,20),...linspace(-2,2,20),...zeros(20)+i);rotate(h1,[1 -1 1],30)dx=get(h1,'xdata');dy=get(h1,'ydata');dz=get(h1,'zdata');delete(h1)slice(x,y,z,v,[-2 2],2,-2)hold onslice(x,y,z,v,dx,dy,dz)hold offaxis tightview(-5,10)drawnowend实例30:立体透视(2)function shili30h0=figure('toolbar','none',...'position',[200 150 450 250],... 'name','实例30');[x,y,z]=meshgrid(-2:0.1:2,...-2:0.1:2,...-2:0.1:2);v=x.*exp(-x.^2-y.^2-z.^2); [dx,dy,dz]=cylinder;slice(x,y,z,v,[-2 2],2,-2)for i=-2:0.2:2h=surface(dx+i,dy,dz);rotate(h,[1 0 0],90)xp=get(h,'xdata');yp=get(h,'ydata');zp=get(h,'zdata');delete(h)hold onhs=slice(x,y,z,v,xp,yp,zp);axis tightxlim([-3 3])view(-10,35)drawnowdelete(hs)hold offend实例31:表面图形function shili31h0=figure('toolbar','none',...'position',[200 150 550 250],...'name','实例31');subplot(1,2,1)x=rand(100,1)*16-8;y=rand(100,1)*16-8;r=sqrt(x.^2+y.^2)+eps;z=sin(r)./r;xlin=linspace(min(x),max(x),33); ylin=linspace(min(y),max(y),33); [X,Y]=meshgrid(xlin,ylin);Z=griddata(x,y,z,X,Y,'cubic'); mesh(X,Y,Z)axis tighthold onplot3(x,y,z,'.','Markersize',20)subplot(1,2,2)k=5;n=2^k-1;theta=pi*(-n:2:n)/n;phi=(pi/2)*(-n:2:n)'/n;X=cos(phi)*cos(theta);Y=cos(phi)*sin(theta);Z=sin(phi)*ones(size(theta)); colormap([0 0 0;1 1 1])C=hadamard(2^k);surf(X,Y,Z,C)axis square实例32:沿曲线移动的小球h0=figure('toolbar','none',...'position',[198****8468],... 'name','实例32');h1=axes('parent',h0,...'position',[0.15 0.45 0.7 0.5],... 'visible','on');t=0:pi/24:4*pi;y=sin(t);plot(t,y,'b')n=length(t);h=line('color',[0 0.5 0.5],...'linestyle','.',...'markersize',25,...'erasemode','xor');k1=uicontrol('parent',h0,...'style','pushbutton',...'position',[80 100 50 30],...'string','开始',...'callback',[...'i=1;',...'k=1;,',...'m=0;,',...'while 1,',...'if k==0,',...'break,',...'end,',...'if k~=0,',...'set(h,''xdata'',t(i),''ydata'',y(i)),',...'drawnow;,',...'i=i+1;,',...'if i>n,',...'m=m+1;,',...'i=1;,',...'end,',...'end,',...'end']);k2=uicontrol('parent',h0,...'style','pushbutton',...'position',[180 100 50 30],...'string','停止',...'callback',[...'k=0;,',...'set(e1,''string'',m),',...'p=get(h,''xdata'');,',...'q=get(h,''ydata'');,',...'set(e2,''string'',p);,',...'set(e3,''string'',q)']);k3=uicontrol('parent',h0,...'style','pushbutton',...'position',[280 100 50 30],... 'string','关闭',...'callback','close');e1=uicontrol('parent',h0,...'style','edit',...'position',[60 30 60 20]);t1=uicontrol('parent',h0,...'style','text',...'string','循环次数',...'position',[60 50 60 20]);e2=uicontrol('parent',h0,...'style','edit',...'position',[180 30 50 20]);t2=uicontrol('parent',h0,...'style','text',...'string','终点的X坐标值',...'position',[155 50 100 20]);e3=uicontrol('parent',h0,...'style','edit',...'position',[300 30 50 20]);t3=uicontrol('parent',h0,...'style','text',...'string','终点的Y坐标值',...'position',[275 50 100 20]);实例33:曲线转换按钮h0=figure('toolbar','none',...'position',[200 150 450 250],... 'name','实例33');x=0:0.5:2*pi;y=sin(x);h=plot(x,y);grid on'if i==1,',...'i=0;,',...'y=cos(x);,',...'delete(h),',...'set(hm,''string'',''正弦函数''),',...'h=plot(x,y);,',...'grid on,',...'else if i==0,',...'i=1;,',...'y=sin(x);,',...'set(hm,''string'',''余弦函数''),',...'delete(h),',...'h=plot(x,y);,',...'grid on,',...'end,',...'end'];hm=uicontrol(gcf,'style','pushbutton',... 'string','余弦函数',...'callback',huidiao);i=1;set(hm,'position',[250 20 60 20]);set(gca,'position',[0.2 0.2 0.6 0.6]) title('按钮的使用')hold on实例34:栅格控制按钮h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例34');x=0:0.5:2*pi;y=sin(x);plot(x,y)huidiao1=[...'set(h_toggle2,''value'',0),',...'grid on,',...];'set(h_toggle1,''value'',0),',...'grid off,',...];h_toggle1=uicontrol(gcf,'style','togglebutton',... 'string','grid on',...'value',0,...'position',[20 45 50 20],...'callback',huidiao1);h_toggle2=uicontrol(gcf,'style','togglebutton',... 'string','grid off',...'value',0,...'position',[20 20 50 20],...'callback',huidiao2);set(gca,'position',[0.2 0.2 0.6 0.6])title('开关按钮的使用')实例35:编辑框的使用h0=figure('toolbar','none',...'position',[200 150 350 250],...'name','实例35');f='Please input the letter';huidiao1=[...'g=upper(f);,',...'set(h2_edit,''string'',g),',...];huidiao2=[...'g=lower(f);,',...'set(h2_edit,''string'',g),',...];h1_edit=uicontrol(gcf,'style','edit',...'position',[100 200 100 50],...'HorizontalAlignment','left',...'string','Please input the letter',...'callback','f=get(h1_edit,''string'');',...'background','w',...'max',5,...'min',1);h2_edit=uicontrol(gcf,'style','edit',...'HorizontalAlignment','left',...'position',[100 100 100 50],...'background','w',...'max',5,...'min',1);h1_button=uicontrol(gcf,'style','pushbutton',... 'string','小写变大写',...'position',[100 45 100 20],...'callback',huidiao1);h2_button=uicontrol(gcf,'style','pushbutton',... 'string','大写变小写',...'position',[100 20 100 20],...'callback',huidiao2);实例36:弹出式菜单h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例36');x=0:0.5:2*pi;y=sin(x);h=plot(x,y);grid onhm=uicontrol(gcf,'style','popupmenu',...'string',...'sin(x)|cos(x)|sin(x)+cos(x)|exp(-sin(x))',... 'position',[250 20 50 20]);set(hm,'value',1)huidiao=[...'v=get(hm,''value'');,',...'switch v,',...'case 1,',...'delete(h),',...'y=sin(x);,',...'h=plot(x,y);,',...'grid on,',...'case 2,',...'delete(h),',...'y=cos(x);,',...'h=plot(x,y);,',...'grid on,',...'case 3,',...'delete(h),',...'y=sin(x)+cos(x);,',...'h=plot(x,y);,',...'grid on,',...'case 4,',...'delete(h),',...'y=exp(-sin(x));,',...'h=plot(x,y);,',...'grid on,',...'end'];set(hm,'callback',huidiao)set(gca,'position',[0.2 0.2 0.6 0.6]) title('弹出式菜单的使用')实例37:滑标的使用h0=figure('toolbar','none',...'position',[200 150 450 250],... 'name','实例37');[x,y]=meshgrid(-8:0.5:8);r=sqrt(x.^2+y.^2)+eps;z=sin(r)./r;h0=mesh(x,y,z);h1=axes('position',...[0.2 0.2 0.5 0.5],...'visible','off');htext=uicontrol(gcf,...'units','points',...'position',[20 30 45 15],...'string','brightness',...'style','text');hslider=uicontrol(gcf,...'units','points',...'position',[10 10 300 15],...'min',-1,...'max',1,...'style','slider',...'callback',...'brighten(get(hslider,''value''))');实例38:多选菜单h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例38');[x,y]=meshgrid(-8:0.5:8);r=sqrt(x.^2+y.^2)+eps;z=sin(r)./r;h0=mesh(x,y,z);hlist=uicontrol(gcf,'style','listbox',...'string','default|spring|summer|autumn|winter',... 'max',5,...'min',1,...'position',[20 20 80 100],...'callback',[...'k=get(hlist,''value'');,',...'switch k,',...'case 1,',...'colormap default,',...'case 2,',...'colormap spring,',...'case 3,',...'colormap summer,',...'case 4,',...'colormap autumn,',...'case 5,',...'colormap winter,',...'end']);实例39:菜单控制的使用h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例39');x=0:0.5:2*pi;y=cos(x);h=plot(x,y);grid onset(gcf,'toolbar','none')hm=uimenu('label','example');huidiao1=[...'set(hm_gridon,''checked'',''on''),',...'set(hm_gridoff,''checked'',''off''),',...'grid on'];huidiao2=[...'set(hm_gridoff,''checked'',''on''),',...'set(hm_gridon,''checked'',''off''),',...'grid off'];hm_gridon=uimenu(hm,'label','grid on',... 'checked','on',...'callback',huidiao1);hm_gridoff=uimenu(hm,'label','grid off',... 'checked','off',...'callback',huidiao2);实例40:UIMENU菜单的应用h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例40');h1=uimenu(gcf,'label','函数');h11=uimenu(h1,'label','轮廓图',...'callback',[...'set(h31,''checked'',''on''),',...'set(h32,''checked'',''off''),',...'[x,y,z]=peaks;,',...'contour3(x,y,z,30)']);h12=uimenu(h1,'label','高斯分布',...。

MATLAB数学实验100例题解

MATLAB数学实验100例题解

一元函数微分学实验1 一元函数的图形(基础实验)实验目的 通过图形加深对函数及其性质的认识与理解, 掌握运用函数的图形来观察和分析 函数的有关特性与变化趋势的方法,建立数形结合的思想; 掌握用Matlab 作平面曲线图性的方法与技巧.初等函数的图形2 作出函数x y tan =和x y cot =的图形观察其周期性和变化趋势. 解:程序代码:>> x=linspace(0,2*pi,600); t=sin(x)./(cos(x)+eps);plot(x,t);title('tan(x)');axis ([0,2*pi,-50,50]); 图象:程序代码:>> x=linspace(0,2*pi,100); ct=cos(x)./(sin(x)+eps);plot(x,ct);title('cot(x)');axis ([0,2*pi,-50,50]); 图象:4在区间]1,1[-画出函数xy 1sin =的图形. 解:程序代码:>> x=linspace(-1,1,10000);y=sin(1./x); plot(x,y);axis([-1,1,-2,2]) 图象:二维参数方程作图6画出参数方程⎩⎨⎧==t t t y tt t x 3cos sin )(5cos cos )(的图形:解:程序代码:>> t=linspace(0,2*pi,100);plot(cos(t).*cos(5*t),sin(t).*cos(3*t)); 图象:极坐标方程作图8 作出极坐标方程为10/t e r =的对数螺线的图形. 解:程序代码:>> t=0:0.01:2*pi; r=exp(t/10);polar(log(t+eps),log(r+eps)); 图象:90270分段函数作图10 作出符号函数x y sgn =的图形. 解:程序代码:>> x=linspace(-100,100,10000); y=sign(x); plot(x,y);axis([-100 100 -2 2]);函数性质的研究12研究函数)3(log 3)(35x e x x f x -++=在区间]2,2[-上图形的特征. 解:程序代码:>> x=linspace(-2,2,10000);y=x.^5+3*exp(x)+log(3-x)/log(3); plot(x,y); 图象:实验2 极限与连续(基础实验)实验目的 通过计算与作图, 从直观上揭示极限的本质,加深对极限概念的理解. 掌握用 Matlab 画散点图, 以及计算极限的方法. 深入理解函数连续的概念,熟悉几种间断点的图形 特征,理解闭区间上连续函数的几个重要性质.作散点图14分别画出坐标为)10,,2,1(),4,(),,(3222 =+i i i i i i 的散点图, 并画出折线图. 解:散点图程序代码: >> i=1:10; plot(i,i.^2,'.')或:>> x=1:10;y=x.^2;for i=1:10;plot(x(i),y(i),'r')hold onend折线图程序代码:>> i=1:10;plot(i,i.^2,'-x')程序代码:>> i=1:10;plot(i.^2,4*(i.^2)+i.^3,'.')>> i=1:10;plot(i.^2,4*(i.^2)+i.^3,'-x')数列极限的概念16通过动画观察当∞→n 时数列21n a n =的变化趋势.解:程序代码: >> n=1:100; an=(n.^2); n=1:100; an=1./(n.^2); n=1:100; an=1./(n.^2); for i=1:100plot(n(1:i),an(1:i)),axis([0,100,0,1]) pause(0.1) end 图象:函数的极限18在区间]4,4[-上作出函数xx xx x f --=339)(的图形, 并研究 )(lim x f x ∞→ 和 ).(lim 1x f x →解:作出函数x x xx x f --=339)(在区间]4,4[-上的图形 >> x=-4:0.01:4;y=(x.^3-9*x)./(x.^3-x+eps); plot(x,y)从图上看,()f x 在x →1与x →∞时极限为0两个重要极限 20计算极限⎪⎭⎫⎝⎛+→x x x x x sin 11sin lim )1(0 x x e x 2lim )2(+∞→30sin tan lim )3(xx x x -→ x x x 0lim )4(+→ x xx ln cot ln lim )5(0+→ x x x ln lim )6(20+→ xx xx x x sin cos sin lim)7(20-→ 125523lim )8(323+++-∞→x x x x x xx x e e x x x sin 2lim )9(0----→ xx x x cos 110sin lim )10(-→⎪⎭⎫ ⎝⎛ 解:(1)>> limit(x*sin(1/x)+1/x*sin(x))ans =1(2) >> limit(x^2/exp(x),inf) ans = 0(3) >> limit((tan(x)-sin(8))/x^3) ans =NaN(4) >> limit(x^x,x,0,'right') ans =1(5) >> limit(log(cot(x))/log(x),x,0,'right') ans =-1(6) >> limit(x^2*log(x),x,0,'right') ans =0(7) >> limit((sin(x)-x.*cos(x))./(x.^2.*sin(x)),x,0) ans =1/3(8) >> limit((3*x.^3-2*x.^2+5)/(5*x.^3+2*+1),x,inf) ans =3/5(9) >> limit((exp(x)-exp(-x)-2*x)./(x-sin(x))) ans =2(10) >> limit((sin(x)/x).^(1/(1-cos(x)))) ans =exp(-1/3)实验3 导数(基础实验)实验目的 深入理解导数与微分的概念, 导数的几何意义. 掌握用Matlab 求导数与高 阶导数的方法. 深入理解和掌握求隐函数的导数, 以及求由参数方程定义的函数的导数的方法. 导数概念与导数的几何意义22作函数71232)(23+-+=x x x x f 的图形和在1-=x 处的切线. 解:作函数71232)(23+-+=x x x x f 的图形程序代码: >> syms x;>> y=2*x^3+3*x^2-12*x+7; >> diff(y) ans =6*x^2+6*x-12 >> syms x;y=2*x^3+3*x^2-12*x+7; >> f=diff(y) f =6*x^2+6*x-12 >> x=-1;f1=6*x^2+6*x-12 f1 = -12>> f2=2*x^3+3*x^2-12*x+7 f2 = 20>> x=linspace(-10,10,1000);y1=2*x.^3+3*x.^2-12*x+7; y2=-12*(x+1)+20; plot(x,y1,'r',x,y2,'g')求函数的导数与微分24求函数bx ax x f cos sin )(=的一阶导数. 并求.1⎪⎭⎫⎝⎛+'b a f解:求函数bx ax x f cos sin )(=的一阶导数程序代码: >> syms a b x y;y= sin(a*x)*cos(b*x); D1=diff(y,x,1) 答案:D1 =cos(a*x)*a*cos(b*x)-sin(a*x)*sin(b*x)*b求.1⎪⎭⎫ ⎝⎛+'b a f程序代码: >> x=1/(a+b);>> cos(a*x)*a*cos(b*x)-sin(a*x)*sin(b*x)*b 答案:ans =cos(a/(a+b))*a*cos(b/(a+b))-sin(a/(a+b))*sin(b/(a+b))*b 拉格朗日中值定理26对函数),2)(1()(--=x x x x f 观察罗尔定理的几何意义. (1) 画出)(x f y =与)(x f '的图形, 并求出1x 与.2x 解:程序代码:>> syms x;f=x*(x-1)*(x-2); f1=diff(f) f1 =(x-1)*(x-2)+x*(x-2)+x*(x-1) >> solve(f1) ans =1+1/3*3^(1/2) 1-1/3*3^(1/2)>> x=linspace(-10,10,1000); y1=x.*(x-1).*(x-2);y2 =(x-1).*(x-2)+x.*(x-2)+x.*(x-1); plot(x,y1,x,y2)(2)画出)(x f y 及其在点))(,(11x f x 与))(,(22x f x 处的切线. 程序代码:>> syms x; >> f=x*(x-1)*(x-2); >> f1=diff(f) f1 =(x-1)*(x-2)+x*(x-2)+x*(x-1) >> solve(f1) ans =1+1/3*3^(1/2) 1-1/3*3^(1/2)>> x=linspace(-3,3,1000); >> y1=x.*(x-1).*(x-2);>> y2 =(x-1).*(x-2)+x.*(x-2)+x.*(x-1); >> plot(x,y1,x,y2) >> hold on>> x=1+1/3*3^(1/2); >> yx1=x*(x-1)*(x-2) yx1 =-0.3849>> x=1-1/3*3^(1/2); >> yx2=x*(x-1)*(x-2) yx2 =0.3849x=linspace(-3,3,1000); yx1 =-0.3849*x.^0; yx2 =0.3849*x.^0; plot(x,yx1,x,yx2)28求下列函数的导数:(1) 31+=x e y ; 解:程序代码:>> syms x y; y=exp((x+1)^3); D1=diff(y,1) 答案:D1 =3*(x+1)^2*exp((x+1)^3)(2) )]42ln[tan(π+=x y ;解:程序代码:>> syms x;y=log(tan(x/2+pi/4)); D1=diff(y,1) 答案:D1 =(1/2+1/2*tan(1/2*x+1/4*pi)^2)/tan(1/2*x+1/4*pi)(3) x x y sin ln cot 212+=;解:程序代码:>> syms x;y=1/2*(cot(x))^2+log(sin(x)); D1=diff(y,1) 答案:D1 =cot(x)*(-1-cot(x)^2)+cos(x)/sin(x) (4) xy 2arctan21=. 解:程序代码:>> syms x;>> y=sqrt(2)*atan(sqrt(2)/x); >> D1=diff(y,1) 答案:D1 =-2/x^2/(1+2/x^2)一元函数积分学与空间图形的画法实验4 一元函数积分学(基础实验)实验目的 掌握用Matlab 计算不定积分与定积分的方法. 通过作图和观察, 深入理解定积分的概念和思想方法. 初步了解定积分的近似计算方法. 理解变上限积分的概念. 提高应用 定积分解决各种问题的能力.不定积分计算30求.)1(532⎰-dx x x解:程序代码:>> syms x y;>> y=x^2*(1-x^3)^5; >> R=int(y,x) 答案:R =-1/18*x^18+1/3*x^15-5/6*x^12+10/9*x^9-5/6*x^6+1/3*x^332求.arctan 2⎰xdx x解:程序代码:>> syms x y;>> y=x^2*atan(x); >> R=int(y,x) 答案:R =1/3*x^3*atan(x)-1/6*x^2+1/6*log(x^2+1)定积分计算34 求.)(102⎰-dx x x解:程序代码:>> syms x y; >> y=x-x^2;>> R=int(y,x,0,1) 答案: R =1/6变上限积分 36 画出变上限函数⎰x dt t t 02sin 及其导函数的图形.解:程序代码:>> syms x y t; >> y=t*sin(t^2); >> R=int(y,x,0,x) 答案:R =t*sin(t^2)*x 再求导函数 程序代码:>> DR=diff(R,x,1) 答案:DR =t*sin(t^2)实验5 空间图形的画法(基础实验)实验目的 掌握用Matlab 绘制空间曲面和曲线的方法. 熟悉常用空间曲线和空间曲面 的图形特征,通过作图和观察, 提高空间想像能力. 深入理解二次曲面方程及其图形.一般二元函数作图38作出函数2214y x z ++=的图形.解:程序代码:>> x=linspace(-5,5,500); [x,y]=meshgrid(x); z=4./(1+x.^2+y.^2); mesh(x,y,z);xlabel('x-axis'),ylabel('y-axis'),zlabel('z-axis');title('function')40作出函数)94cos(22y x z +=的图形. 解:程序代码:>> x=-10:0.1:10;[x,y]=meshgrid(x);z=cos(4*x.^2+9*y.^2); mesh(x,y,z);xlabel('x-axis'),ylabel('y-axis'),zlabel('z-axis');title('function')讨论:坐标轴选取范围不同时,图形差异很大,对本题尤为明显,如右图为坐标轴[-1,1]二次曲面42作出单叶双曲面1941222=-+z y x 的图形.(曲面的参数方程为 ,tan 3,cos sec 2,sin sec u z v u y v u x === (.20,2/2/πππ≤≤<<-v u ))解:程序代码:>> v=0:pi/100:2*pi; >> u=-pi/2:pi/100:pi/2; >> [U,V]=meshgrid(u,v); >> x=sec(U).*sin(V); >> y=2*sec(U).*cos(V); >> z=3*tan(U); >> surf(x,y,z)44 可以证明: 函数xy z =的图形是双曲抛物面. 在区域22,22≤≤-≤≤-y x 上作出它的图形.解:程序代码:>> x=-2:0.01:2;[x,y]=meshgrid(x); >> z=x.*y;>> mesh(x,y,z);46 画出参数曲面]2,001.0[],4,0[)5/2/ln(tan cos sin sin sin cos ∈∈⎪⎩⎪⎨⎧++===v u u v v z vu y v u x π 的图形.解:程序代码:>> v=0.001:0.001:2; >> u=0:pi/100:4*pi;>> [U,V]=meshgrid(u,v); >> x=cos(U).*sin(V); >> y=sin(U).*sin(V);>> z=cos(V)+log(tan(V/2)+U/5); >> mesh(x,y,z);空间曲线48 作出空间曲线)60(2,sin ,cos π≤≤===t t z t t y t t x 的图形. 解:程序代码:>> syms t;ezplot3(t*cos(t),t*sin(t),2*t,[0,6*pi])-1010-20-100100xx = t cos(t), y = t sin(t), z = 2 tz50绘制参数曲线 ⎪⎪⎩⎪⎪⎨⎧=+==t z t y t x arctan 211cos 2的图形.解:程序代码:>> t=-2*pi:pi/100:2*pi;x=cos(t).*cos(t);y=1./(1+2*t);z=atan(t); plot3(x,y,z);grid;xlabel('x'),ylabel('y'),zlabel('z')xyz多元函数微积分实验6 多元函数微分学(基础实验)实验目的 掌握利用Matlab 计算多元函数偏导数和全微分的方法, 掌握计算二元函数极值和条件极值的方法. 理解和掌握曲面的切平面的作法. 通过作图和观察, 理解二元 函数的性质、方向导数、梯度和等高线的概念.求多元函数的偏导数与全微分52设),(cos )sin(2xy xy z +=求.,,,222yx zx z y z x z ∂∂∂∂∂∂∂∂∂解:程序代码:>> syms x y;S=sin(x*y)+(cos(x*y))^2; D1=diff(S,'x',1); D2=diff(S,'y',1); D3=diff(S,'x',2); D4=diff(S,'y',2); D1,D2,D3,D4答案: D1 = cos(x*y)*y-2*cos(x*y)*sin(x*y)*yD2 = cos(x*y)*x-2*cos(x*y)*sin(x*y)*xD3 =-sin(x*y)*y^2+2*sin(x*y)^2*y^2-2*cos(x*y)^2*y^2 D4 = -sin(x*y)*x^2+2*sin(x*y)^2*x^2-2*cos(x*y)^2*x^2实验7 多元函数积分学(基础实验)实验目的掌握用Matlab 计算二重积分与三重积分的方法; 深入理解曲线积分、曲面积分的 概念和计算方法. 提高应用重积分和曲线、曲面积分解决各种问题的能力.计算重积分54计算,2dxdy xyD⎰⎰ 其中D 为由,,2y x y x ==+ 2=y 所围成的有界区域.解:程序代码:>> syms x y;int(int(x*y^2,x,2-y,sqrt(y)),y,1,2) 答案:ans =193/120 重积分的应用56求旋转抛物面224y x z --=在Oxy 平面上部的面积.S 解:程序代码:>> int(2*pi*r,r,0,2) 答案: ans =4*pi无穷级数与微分方程实验8 无穷级数(基础实验) 实验目的观察无穷级数部分和的变化趋势,进一步理解级数的审敛法以及幂级数部分和对函数的 逼近. 掌握用Matlab 求无穷级数的和, 求幂级数的收敛域, 展开函数为幂级数以及展 开周期函数为傅里叶级数的方法.数项级数58(1) 观察级数∑∞=121n n的部分和序列的变化趋势.解:程序代码:for i=1:100 s=0; for n=1:i s=s+1/n^2; endplot(i,s,'.');hold on; end(2) 观察级数∑∞=11n n 的部分和序列的变化趋势.>> for i=1:100 s=0; for n=1:i s=s+1/n; endplot(i,s,'.'); hold on; end60 求∑∞=++123841n n n的值.解:程序代码:>> syms n;score=symsum(1/(4*n^2+8*n+3),1,inf) 答案: score =1/6函数的幂级数展开62求x arctan 的5阶泰勒展开式. >> syms x;>> T5=taylor(atan(x),6)答案:T5 =x-1/3*x^3+1/5*x^5实验9 微分方程(基础实验)实验目的 理解常微分方程解的概念以及积分曲线和方向场的概念,掌握利用 Matlab 求微分方程及方程组解的常用命令和方法.求解微分方程64求微分方程 22x xe xy y -=+'的通解. 解:程序代码:>> y=dsolve('Dy+2*x*y=x*exp(-x^2)','x') 答案:y =(1/2*x^2+C1)*exp(-x^2)66求微分方程x e y y y x 2cos 52=+'-''的通解. 解:程序代码:>> y=dsolve('D2y-2*Dy+5*y=exp(x)*cos(2*x)','x') 答案: y =exp(x)*sin(2*x)*C2+exp(x)*cos(2*x)*C1+1/4*exp(x)*sin(2*x)*x68求微分方程组⎪⎪⎩⎪⎪⎨⎧=--=++02y x dtdy e y x dt dxt 在初始条件0,100====t t y x 下的特解.解:程序代码:>> [x,y]=dsolve('Dx+x+2*y-exp(t)','Dy-x-y','x(0)=1','y(0)=0','t') 答案: x = cos(t)y = 1/2*sin(t)-1/2*cos(t)+1/2*exp(t)70求解微分方程,)1(122/5+=+-x x y dx dy 并作出积分曲线. 解:程序代码:>> syms x yy=dsolve('Dy-2*y/(x+1)-(x+1)^(5/2)','x') 答案:y =(2/3*(x+1)^(3/2)+C1)*(x+1)^2 做积分曲线 由>> syms x yx=linspace(-5,5,100); C=input('请输入C 的值:'); y=(2/3*(x+1).^(3/2)+C).*(x+1).^2; plot(x,y)例如对应有: 请输入C 的值:2 请输入C 的值:20矩阵运算与方程组求解实验10 行列式与矩阵实验目的掌握矩阵的输入方法. 掌握利用Matlab 对矩阵进行转置、加、减、数乘、相乘、乘方等运算, 并能求矩阵的逆矩阵和计算方阵的行列式.矩阵A 的转置函数Transpose[A]72 求矩阵⎪⎪⎪⎪⎪⎭⎫⎝⎛411365243271的转置. 解:程序代码:>> A=[1,7,2;3,4,2;5,6,3;1,1,4]; >> Sove=A' 答案:Sove =1 3 5 1 7 4 6 12 234 矩阵线性运算 73设,291724,624543⎪⎪⎭⎫⎝⎛=⎪⎪⎭⎫⎝⎛=B A 求.24,A B B A -+ 解:程序代码:>> A=[3,4,5;4,2,6]; B=[4,2,7;1,9,2];S1=A+BS2=4*B-2*A答案:S1 =7 6 125 11 8S2 =10 0 18-4 32 -474设,148530291724,36242543⎪⎪⎪⎪⎪⎭⎫ ⎝⎛=⎪⎪⎭⎫ ⎝⎛=mb ma 求矩阵ma 与mb 的乘积. 解:程序代码:>> ma=[3,4,5,2;4,2,6,3];>> mb=[4,2,7;1,9,2;0,3,5;8,4,1];>> Sove=ma*mb答案:Sove =32 65 5642 56 65矩阵的乘法运算 75设,101,530291724⎪⎪⎪⎭⎫ ⎝⎛=⎪⎪⎪⎭⎫ ⎝⎛=B A 求AB 与,A B T 并求.3A解:程序代码:>> A=[4 2 7;1 9 2;0 3 5];B=[1;0;1];>> AB=A*BAB =1135>> BTA=B'*ABTA =4 5 12>> A3=A^3A3 =119 660 555141 932 44454 477 260求方阵的逆76 设,5123641033252312⎪⎪⎪⎪⎪⎭⎫ ⎝⎛=A 求.1-A 解:程序代码:>> A=[2,1,3,2;5,2,3,3;0,1,4,6;3,2,1,5];Y=inv(A)答案:Y =-1.7500 1.3125 0.5000 -0.68755.5000 -3.6250 -2.0000 2.37500.5000 -0.1250 0.0000 -0.1250-1.2500 0.6875 0.5000 -0.312577 设,221331317230,5121435133124403⎪⎪⎪⎪⎪⎭⎫ ⎝⎛=⎪⎪⎪⎪⎪⎭⎫ ⎝⎛=B A 求.1B A - 解:程序代码:>> A=[3 0 4 4 ;2 1 3 3 ;1 5 3 4;1 2 1 5];B=[0 3 2 ;7 1 3;1 3 3 ;1 2 2];Solve=A'*B答案:Solve =16 16 1714 20 2225 26 2830 37 3978 解方程组⎪⎩⎪⎨⎧-=-+=+-=++.2442,63,723z y x z y x z y x解:程序代码:>> A=[3 2 1;1 -1 3;2 4 -4];b=[7 6 -2];>> A\b'答案:ans =1.00001.00002.0000求方阵的行列式79 求行列式 .3351110243152113------=D 解:程序代码:>> A=[3,1,-1,2;-5,1,3,-4;2,0,1,-1;1,-5,3,-3];D=det(A)答案:D =4080求.11111111111122222222d d d d c c c c b b b b a a a a D ++++= 解:程序代码:>> syms a b c d;D=[a^2+1/a^2 a 1/a 1;b^2+1/b^2 b 1/b 1;c^2+1/c^2 c 1/c 1;d^2+1/d^2 d 1/d 1];det(D)答案:ans =-(-c*d^2*b^3+c^2*d*b^3-c^3*d^2*a+c^3*d*a^2*b^4+c*d^2*a^3-c^3*d^2*a*b^4-c^2*d*a^3-c*d^2*b^3*a^4+c^2*d*b^3*a^4+c^3*d^2*b*a^4-c^3*d*b^2*a^4-c^2*d^3*b*a^4+c*d^3*b^2*a^4+c*d ^2*a^3*b^4-c^2*d*a^3*b^4+c^3*d^2*b-c^3*d*b^2-c^2*d^3*b+c*d^3*b^2+c^3*d*a^2+c^2*d^3*a-c *d^3*a^2-b*d^2*a^3+b^2*d*a^3+b^3*d^2*a-b^3*d*a^2-b^2*d^3*a+b*d^3*a^2+b*c^2*a^3-b^2*c*a ^3-b^3*c^2*a+b^3*c*a^2+b^2*c^3*a-b*c^3*a^2+c^2*d^3*a*b^4-c*d^3*a^2*b^4-b*d^2*a^3*c^4+b ^2*d*a^3*c^4+b^3*d^2*a*c^4-b^3*d*a^2*c^4-b^2*d^3*a*c^4+b*d^3*a^2*c^4+b*c^2*a^3*d^4-b^2*c*a^3*d^4-b^3*c^2*a*d^4+b^3*c*a^2*d^4+b^2*c^3*a*d^4-b*c^3*a^2*d^4)/a^2/c^2/d^2/b^281 计算范德蒙行列式.1111145444342413534333231252423222154321x x x x x x x x x x x x x x x x x x x x 解:程序代码:>> syms x1 x2 x3 x4 x5; >> A=[1,1,1,1,1;x1,x2,x3,x4,x5;x1^2,x2^2,x3^2,x4^2,x5^2;x1^3,x2^3,x3^3,x4^3,x5^3;x1^4,x2^4,x3^4,x4^4,x5^4];>> DC=det(A);>> DS=simple(DC)答案:DS =(-x5+x4)*(x3-x5)*(x3-x4)*(-x5+x2)*(x2-x4)*(x2-x3)*(-x5+x1)*(x1-x4)*(x1-x3)*(x1-x2)82 设矩阵 ,60975738723965110249746273⎪⎪⎪⎪⎪⎪⎭⎫ ⎝⎛----=A 求.),(|,|3A A tr A 解:程序代码:>> A=[3,7,2,6,-4;7,9,4,2,0;11,5,-6,9,3;2,7,-8,3,7;5,7,9,0,-6];>> D=det(A),T=trace(A),A3=A^3答案:D =11592T =3A3=726 2062 944 294 -3581848 3150 26 1516 2281713 2218 31 1006 4041743 984 -451 1222 384801 2666 477 745 -125向量的内积83 求向量}3,2,1{=u 与}0,1,1{-=v 的内积.解:程序代码:>> u=[1 2 3];v=[1 -1 0];solve=dot(u,v)答案:solve =-184设,001001⎪⎪⎪⎭⎫⎝⎛=λλλA 求.10A 一般地?=k A (k 是正整数).解:程序代码:>> syms r;>> A=[r,1,0;0,r,1;0,0,r];>> A^10答案:ans =[ r^10, 10*r^9, 45*r^8][ 0, r^10, 10*r^9][ 0, 0, r^10]85.求⎪⎪⎪⎪⎪⎪⎭⎫⎝⎛+++++a a a a a 1111111111111111111111111的逆.解:程序代码:>> syms aA=[1+a,1,1,1,1;1,1+a,1,1,1;1,1,1+a,1,1;1,1,1,1+a,1;1,1,1,1,1+a];solve=inv(A)答案:solve =[ 1/a*(a+4)/(a+5), -1/a/(a+5), -1/a/(a+5), -1/a/(a+5),-1/a/(a+5)] [ -1/a/(a+5), 1/a*(a+4)/(a+5), -1/a/(a+5), -1/a/(a+5),-1/a/(a+5)][ -1/a/(a+5), -1/a/(a+5), 1/a*(a+4)/(a+5), -1/a/(a+5), -1/a/(a+5)][ -1/a/(a+5), -1/a/(a+5), -1/a/(a+5), 1/a*(a+4)/(a+5), -1/a/(a+5)][ -1/a/(a+5), -1/a/(a+5), -1/a/(a+5), -1/a/(a+5), 1/a*(a+4)/(a+5)] 实验11 矩阵的秩与向量组的极大无关组实验目的 学习利用Matlab 求矩阵的秩,作矩阵的初等行变换; 求向量组的秩与极大无关组. 求矩阵的秩86 设,815073*********⎪⎪⎪⎭⎫ ⎝⎛-------=M 求矩阵M 的秩.解:程序代码:>> M=[3,2,-1,-3,-2;2,-1,3,1,-3;7,0,5,-1,-8];R=rank(M)答案:R=2向量组的秩87求向量组)0,3,0,2(),2,5,4,0(),1,1,2,1(231=--=-=ααα的秩.解:程序代码:>> A=[1,2,-1,1;0,-4,5,-2;2,0,3,0];R=rank(A)答案:R =288向量组)7,5,1,3(),5,4,3,1(),1,1,1,1(),3,2,1,1(4321==-==αααα是否线性相关?解:由>> A=[1 1 2 3;1 -1 1 1;1 3 4 5;3 1 5 7];rank(A)ans = 3即rank(A)=3 小于阶数489向量组)3,1,1(),2,1,3(),7,2,2(321=-==ααα是否线性相关?解:由>> A3=[2,2,7;3,-1,2;1,1,3];R=rank(A3)得 R = 3即rank(A3)=3 等于阶数3故向量组线性无关。

matlab编程实例100例之欧阳道创编

matlab编程实例100例之欧阳道创编

1-32是:图形应用篇33-66是:界面设计篇67-84是:图形处理篇85-100是:数值分析篇实例1:三角函数曲线(1)function shili01h0=figure('toolbar','none',...'position',[198 56 350 300],...'name','实例01');h1=axes('parent',h0,...'visible','off');x=-pi:0.05:pi;y=sin(x);plot(x,y);xlabel('自变量X');ylabel('函数值Y');title('SIN( )函数曲线');grid on实例2:三角函数曲线(2)function shili02h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例02');x=-pi:0.05:pi;y=sin(x)+cos(x);plot(x,y,'-*r','linewidth',1);grid onxlabel('自变量X');ylabel('函数值Y');title('三角函数');实例3:图形的叠加function shili03h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例03');x=-pi:0.05:pi;y1=sin(x);y2=cos(x);plot(x,y1,...'-*r',...x,y2,...'--og');grid onxlabel('自变量X');ylabel('函数值Y');title('三角函数');实例4:双y轴图形的绘制function shili04h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例04');x=0:900;a=1000;b=0.005;y1=2*x;y2=cos(b*x);[haxes,hline1,hline2]=plotyy(x,y1,x,y2,'semilogy','plot'); axes(haxes(1))ylabel('semilog plot');axes(haxes(2))ylabel('linear plot');实例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,1)plot(sin(t),cos(t))axis equalsubplot(2,2,2)z=sin(x)-cos(y);plot(t,z)axis([0 2*pi -2 2])subplot(2,2,3)h=sin(x)+cos(y);plot(t,h)axis([0 2*pi -2 2])subplot(2,2,4)g=(sin(x).^2)-(cos(y).^2);plot(t,g)axis([0 2*pi -1 1])实例6:图形标注function shili06h0=figure('toolbar','none',...'position',[200 150 450 400],...'name','实例06');t=0:pi/10:2*pi;h=plot(t,sin(t));xlabel('t=0到2\pi','fontsize',16);ylabel('sin(t)','fontsize',16);title('\it{从 0to2\pi 的正弦曲线}','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),...['\leftarrow最小值=',num2str(y(imin))],... 'fontsize',16)text(x(imax),y(imax),...['\leftarrow最大值=',num2str(y(imax))],... 'fontsize',16)实例7:条形图形function shili07h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例07');tiao1=[562 548 224 545 41 445 745 512];tiao2=[47 48 57 58 54 52 65 48];t=0:7;bar(t,tiao1)xlabel('X轴');ylabel('TIAO1值');h1=gca;h2=axes('position',get(h1,'position'));plot(t,tiao2,'linewidth',3)set(h2,'yaxislocation','right','color','none','xticklabel',[])实例8:区域图形function shili08h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例08');x=91:95;profits1=[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,profits1,'facecolor',[0.5 0.9 0.6],...'edgecolor','b',...'linewidth',3)hold onarea(x,profits2,'facecolor',[0.9 0.85 0.7],...'edgecolor','y',...'linewidth',3)hold onarea(x,profits3,'facecolor',[0.3 0.6 0.7],...'edgecolor','r',...'linewidth',3)hold onarea(x,profits4,'facecolor',[0.6 0.5 0.9],...'edgecolor','m',...'linewidth',3)hold offset(gca,'xtick',[91:95])set(gca,'layer','top')gtext('\leftarrow第一季度销量')gtext('\leftarrow第二季度销量')gtext('\leftarrow第三季度销量')gtext('\leftarrow第四季度销量')xlabel('年','fontsize',16);ylabel('销售量','fontsize',16);实例9:饼图的绘制function shili09h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例09');t=[54 21 35;68 54 35;45 25 12;48 68 45;68 54 69];x=sum(t);h=pie(x);textobjs=findobj(h,'type','text');str1=get(textobjs,{'string'});val1=get(textobjs,{'extent'});oldext=cat(1,val1{:});names={'商品一:';'商品二:';'商品三:'};str2=strcat(names,str1);set(textobjs,{'string'},str2)val2=get(textobjs,{'extent'});newext=cat(1,val2{:});offset=sign(oldext(:,1)).*(newext(:,3)-oldext(:,3))/2; pos=get(textobjs,{'position'});textpos=cat(1,pos{:});textpos(:,1)=textpos(:,1)+offset;set(textobjs,{'position'},num2cell(textpos,[3,2]))实例10:阶梯图function shili10h0=figure('toolbar','none',...'position',[200 150 450 400],...'name','实例10');a=0.01;b=0.5;t=0:10;f=exp(-a*t).*sin(b*t);stairs(t,f)hold onplot(t,f,':*')hold offglabel='函数e^{-(\alpha*t)}sin\beta*t的阶梯图'; gtext(glabel,'fontsize',16)xlabel('t=0:10','fontsize',16)axis([0 10 -1.2 1.2])实例11:枝干图function shili11h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例11');x=0:pi/20:2*pi;y1=sin(x);y2=cos(x);h1=stem(x,y1+y2);hold onh2=plot(x,y1,'^r',x,y2,'*g');hold offh3=[h1(1);h2];legend(h3,'y1+y2','y1=sin(x)','y2=cos(x)')xlabel('自变量X');ylabel('函数值Y');title('正弦函数与余弦函数的线性组合');实例12:罗盘图function shili12h0=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/180;[x,y]=pol2cart(rdirection,windpower); compass(x,y);desc={'风向和风力','北京气象台','10月1日0:00到','10月1日12:00'};gtext(desc)实例13:轮廓图function shili13h0=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.^4-1).^(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 shili14h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例14');axis([0 10 0 10]);hold onx=[];y=[];n=0;disp('单击鼠标左键点取需要的点'); disp('单击鼠标右键点取最后一个点'); but=1;while but==1[xi,yi,but]=ginput(1);plot(xi,yi,'bo')n=n+1;disp('单击鼠标左键点取下一个点'); x(n,1)=xi;y(n,1)=yi;endt=1:n;ts=1:0.1:n;xs=spline(t,x,ts);ys=spline(t,y,ts);plot(xs,ys,'r-');hold off实例14:交互式图形function shili14h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例14');axis([0 10 0 10]);hold onx=[];y=[];n=0;disp('单击鼠标左键点取需要的点'); disp('单击鼠标右键点取最后一个点'); but=1;while but==1[xi,yi,but]=ginput(1);plot(xi,yi,'bo')n=n+1;disp('单击鼠标左键点取下一个点'); x(n,1)=xi;y(n,1)=yi;endt=1:n;ts=1:0.1:n;xs=spline(t,x,ts);ys=spline(t,y,ts);plot(xs,ys,'r-');hold off实例15:变换的傅立叶函数曲线function shili15h0=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],'min',1,'max',20)for j=1:20plot(fft(eye(j+16)))set(h,'value',j)m(:,j)=getframe(gcf);endclf;axes('position',[0 0 1 1]);movie(m,30)实例16:劳伦兹非线形方程的无序活动function shili15h0=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],'min',1,'max',20)for j=1:20plot(fft(eye(j+16)))set(h,'value',j)m(:,j)=getframe(gcf);endclf;axes('position',[0 0 1 1]);movie(m,30)实例17:填充图function shili17h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例17');t=(1:2:15)*pi/8;x=sin(t);y=cos(t);fill(x,y,'r')axis square offtext(0,0,'STOP',...'color',[1 1 1],...'fontsize',50,...'horizontalalignment','center')例18:条形图和阶梯形图function shili18h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例18');subplot(2,2,1)x=-3:0.2:3;y=exp(-x.*x);bar(x,y)title('2-D Bar Chart')subplot(2,2,2)x=-3:0.2:3;y=exp(-x.*x);bar3(x,y,'r')title('3-D Bar Chart')subplot(2,2,3)x=-3:0.2:3;y=exp(-x.*x);stairs(x,y)title('Stair Chart')subplot(2,2,4)x=-3:0.2:3;y=exp(-x.*x);barh(x,y)title('Horizontal Bar Chart')实例19:三维曲线图function shili19h0=figure('toolbar','none',...'position',[200 150 450 400],... 'name','实例19');subplot(2,1,1)x=linspace(0,2*pi);y1=sin(x);y2=cos(x);y3=sin(x)+cos(x);z1=zeros(size(x));z2=0.5*z1;z3=z1;plot3(x,y1,z1,x,y2,z2,x,y3,z3) grid onxlabel('X轴');ylabel('Y轴');zlabel('Z轴');title('Figure1:3-D Plot') subplot(2,1,2)x=linspace(0,2*pi);y1=sin(x);y2=cos(x);y3=sin(x)+cos(x);z1=zeros(size(x));z2=0.5*z1;z3=z1;plot3(x,z1,y1,x,z2,y2,x,z3,y3) grid onxlabel('X轴');ylabel('Y轴');zlabel('Z轴');title('Figure2:3-D Plot')实例20:图形的隐藏属性function shili20h0=figure('toolbar','none',...'position',[200 150 450 300],... 'name','实例20');subplot(1,2,1)[x,y,z]=sphere(10);mesh(x,y,z)axis offtitle('Figure1:Opaque')hidden onsubplot(1,2,2)[x,y,z]=sphere(10);mesh(x,y,z)axis offtitle('Figure2:Transparent') hidden off实例21PEAKS函数曲线function shili21h0=figure('toolbar','none',...'position',[200 100 450 450],...'name','实例21');[x,y,z]=peaks(30);subplot(2,1,1)x=x(1,:);y=y(:,1);i=find(y>0.8&y<1.2);j=find(x>-0.6&x<0.5);z(i,j)=nan*z(i,j);surfc(x,y,z)xlabel('X轴');ylabel('Y轴');zlabel('Z轴');title('Figure1:surfc函数形成的曲面') subplot(2,1,2)x=x(1,:);y=y(:,1);i=find(y>0.8&y<1.2);j=find(x>-0.6&x<0.5);z(i,j)=nan*z(i,j);surfl(x,y,z)xlabel('X轴');ylabel('Y轴');zlabel('Z轴');title('Figure2:surfl函数形成的曲面') 实例22:片状图function shili22h0=figure('toolbar','none',...'position',[200 150 550 350],...'name','实例22');subplot(1,2,1)x=rand(1,20);y=rand(1,20);z=peaks(x,y*pi);t=delaunay(x,y);trimesh(t,x,y,z)hidden offtitle('Figure1:Triangular Surface Plot'); subplot(1,2,2)x=rand(1,20);y=rand(1,20);z=peaks(x,y*pi);t=delaunay(x,y);trisurf(t,x,y,z)title('Figure1:Triangular Surface Plot');实例23:视角的调整function shili23h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例23');x=-5:0.5:5;[x,y]=meshgrid(x);r=sqrt(x.^2+y.^2)+eps;z=sin(r)./r;subplot(2,2,1)surf(x,y,z)xlabel('X-axis')ylabel('Y-axis')zlabel('Z-axis')title('Figure1')view(-37.5,30)subplot(2,2,2)surf(x,y,z)xlabel('X-axis')ylabel('Y-axis')zlabel('Z-axis')title('Figure2')view(-37.5+90,30)subplot(2,2,3)surf(x,y,z)xlabel('X-axis')。

matlab数学建模100例

matlab数学建模100例

matlab数学建模100例摘要:一、引言二、MATLAB 简介三、MATLAB 在数学建模中的应用四、MATLAB 数学建模实例详解五、结论正文:一、引言数学建模是一种利用数学方法和工具来解决实际问题的过程,其目的是通过建立数学模型,揭示问题的本质,从而为问题的解决提供理论依据。

在众多的数学建模工具中,MATLAB 因其强大的功能和便捷的操作,成为了广大科研工作者和工程师的首选工具。

本文将介绍MATLAB 在数学建模中的应用,并列举100 个实例,以供读者参考。

二、MATLAB 简介MATLAB(Matrix Laboratory)是一款由美国克里尔梅里诺(Cleve Moler)公司开发的数学软件,主要用于科学计算、数据分析和可视化。

它有以下几个特点:1.强大的矩阵计算能力:MATLAB 的名称即来源于其矩阵计算功能,可以轻松实现大规模矩阵运算。

2.丰富的函数库:MATLAB 提供了丰富的函数库,涵盖了数学、统计、工程等多个领域。

3.便捷的数据分析工具:MATLAB 提供了一系列数据分析工具,如统计分析、信号处理等。

4.可视化功能:MATLAB 可以方便地进行数据可视化,如绘制图形、绘制三维图像等。

三、MATLAB 在数学建模中的应用MATLAB 在数学建模中的应用非常广泛,包括以下方面:1.建立数学模型:利用MATLAB 可以方便地建立各种数学模型,如方程组、概率分布等。

2.求解数学模型:MATLAB 可以求解各种类型的数学模型,如线性规划、非线性规划等。

3.分析数学模型:MATLAB 可以对数学模型进行各种分析,如稳定性分析、敏感性分析等。

4.可视化分析:MATLAB 可以对数学模型进行可视化分析,如绘制函数图像、绘制三维图像等。

四、MATLAB 数学建模实例详解本文列举了100 个MATLAB 数学建模实例,具体如下:(此处省略100 个实例的详细描述)五、结论MATLAB 作为一款强大的数学软件,在数学建模领域具有广泛的应用。

matlab实用程序百例(6)

matlab实用程序百例(6)

matlab实用程序百例(6)可设置函数曲线光源的用户界面h0=figure('toolbar','none',...'position',[198****8468],...'name','实例53');h1=axes('parent',h0,...'position',[0.15 0.5 0.7 0.5],...'visible','off');[x,y]=meshgrid(-8:0.5:8);r=sqrt(x.^2+y.^2)+eps;z=sin(r)./r;fh=surf(x,y,z);shading interpview([-60 30])camlight leftlightk=light('position',[0 -2 1]);button1=uicontrol('parent',h0,...'style','pushbutton',...'string','设置光线',...'position',[80 60 70 30],...'callback',[...'an1=inputdlg(''光线来源的X轴坐标'');,',... 'k1=str2num(an1{1});,',...'an2=inputdlg(''光线来源的Y轴坐标'');,',... 'k2=str2num(an2{1});,',...'an3=inputdlg(''光线来源的Z轴坐标'');,',... 'k3=str2num(an3{1});,',...'set(lightk,''position'',[k1 k2 k3]);,',...'set(edit1,''string'',[''['',num2str(k1),'''',num2str(k2),'' '',num2str(k3),'']'']);']);button2=uicontrol('parent',h0,...'style','pushbutton',...'string','关闭',...'position',[250 60 70 30],...'callback','close');edit1=uicontrol('parent',h0,...'style','edit',...'max',2,...'min',0,...'fontsize',15,...'backgroundcolor',[1 1 1],...'string','[0 -2 1]',...'position',[80 110 220 30]);text1=uicontrol('parent',h0,...'style','text',...'backgroundcolor',[0.75 0.75 0.75],...'fontsize',15,...'string','光线来源坐标',...'position',[80 140 220 30]);添加效果h0=figure('toolbar','none',...'position',[200 50 300 350],...'name','实例54');h1=axes('parent',h0,...'position',[0.2 0.4 0.6 0.6],...'visible','off');ezsurf('sin(sqrt(x.^2+y.^2))/sqrt(x.^2+y.^2)',[-6*pi,6*pi]) b1=uicontrol('parent',h0,...'units','points',...'tag','b1',...'style','pushbutton',...'string','设置',...'backgroundcolor',[0.75 0.75 0.75],... 'position',[40 50 50 20],...'callback',[...'view(0,75);,',...'shading interp;,',...'lightangle(-45,30);,',...'k=findobj(gca,''type'',''surface'');,'... 'set(k,''facelighting'',''phong'');,',... 'set(k,''ambientstrength'',0.3);,',...'set(k,''diffusestrength'',0.8);,',...'set(k,''specularstrength'',0.9);,',...'set(k,''specularexponent'',25);,',... 'set(k,''backfacelighting'',''unlit'')']); b2=uicontrol('parent',h0,...'units','points',...'tag','b2',...'style','pushbutton',...'string','关闭',...'backgroundcolor',[0.75 0.75 0.75],... 'position',[120 50 50 20],...'callback','close');查询日期h0=figure('toolbar','none',...'position',[198****8468],...'name','实例55');h1=axes('parent',h0,...'position',[0.15 0.5 0.7 0.5],...'visible','off');huidiao=[...'yearnum=str2num(get(edit1,''string''));,',...'monthnum=str2num(get(edit2,''string''));,',...'daynum=str2num(get(edit3,''string''));,',...'monthday=[0 31 28 31 30 31 30 31 31 30 31 30 31];,',... 'dyear=yearnum-2000;,',...'beishu=fix(dyear/4);,',...'yushu=rem(yearnum,4);,',...'if yushu==0,',...'monthday(3)=29;,',...'end,',...'mday=0;,',...'for i=1:monthnum,',...'mday=monthday(i)+mday;,',...'end,',...'yearday=mday+daynum-1;,',...'noweek=fix(yearday/7);,',...'set(edit5,''string'',[''第'',num2str(noweek),''周'']);,',...'if dyear>0,',...'if yushu==0,',...'beishu=beishu-1;,',...'end,',...'dday=yearday+365*dyear+beishu+1;,',...'end,',...'if dyear<=0,',...'dday=365*dyear+yearday+beishu;,',...'end,',...'mweek=rem(dday,7)+7;,',...'set(edit4,''string'',''Sunday'');,',...'end,',...'if mweek==9,',...'set(edit4,''string'',''Monday'');,',...'end,',...'if mweek==10,',...'set(edit4,''string'',''Tuesday'');,',...'end,',...'if mweek==11,',...'set(edit4,''string'',''Wednesday'');,',... 'end,',...'if mweek==12,',...'set(edit4,''string'',''Thursday'');,',... 'end,',...'if mweek==13,',...'set(edit4,''string'',''Friday'');,',...'end,',...'if mweek==7,',...'set(edit4,''string'',''Saturday'');,',... 'end,',...'if mweek==6,',...'set(edit4,''string'',''Friday'');,',...'end,',...'if mweek==5,',...'set(edit4,''string'',''Thursday'');,',... 'end,',...'if mweek==4,',...'set(edit4,''string'',''Wednesday'');,',... 'end,',...'set(edit4,''string'',''Tuesday'');,',... 'end,',...'if mweek==2,',...'set(edit4,''string'',''Monday'');,',... 'end,',...'if mweek==1,',...'set(edit4,''string'',''Sunday'');,',... 'end'];edit1=uicontrol('parent',h0,...'style','edit',...'horizontalalignment','right',...'position',[40 300 50 20]);text1=uicontrol('parent',h0,...'style','text',...'string','年',...'horizontalalignment','left',...'position',[90 300 50 20]);edit2=uicontrol('parent',h0,...'style','edit',...'horizontalalignment','right',...'position',[160 300 50 20]);text2=uicontrol('parent',h0,...'style','text',...'string','月',...'horizontalalignment','left',...'position',[210 300 50 20]);edit3=uicontrol('parent',h0,...'style','edit',...'horizontalalignment','right',...'position',[280 300 50 20]);text3=uicontrol('parent',h0,... 'style','text',...'string','日',...'horizontalalignment','left',...'position',[330 300 50 20]);edit4=uicontrol('parent',h0,... 'style','edit',...'horizontalalignment','left',...'position',[210 200 120 20]); text4=uicontrol('parent',h0,... 'style','text',...'string','查找的日期为',...'horizontalalignment','right',... 'position',[110 200 100 20]); edit5=uicontrol('parent',h0,... 'style','edit',...'horizontalalignment','left',...'position',[210 100 120 20]); text1=uicontrol('parent',h0,... 'style','text',...'string','该日处于',...'horizontalalignment','left',...'position',[160 100 50 20]); button1=uicontrol('parent',h0,... 'style','pushbutton',...'position',[80 40 80 30],...'string','开始',...'callback',huidiao);button2=uicontrol('parent',h0,...'style','pushbutton',...'position',[220 40 80 30],...'string','关闭',...'callback','close');图形效果(1)h0=figure('toolbar','none',...'position',[198****0468],...'name','实例56');h1=axes('parent',h0,...'position',[0.3 0.45 0.5 0.5],...'visible','off');l1=uimenu(gcf,'label','Draw figure',... 'tag','l1');huidiao=[...'if get(r1,''value'')==1,',...'shading faceted,',...'end,',...'if get(r2,''value'')==1,',...'shading flat,',...'end,',...'if get(r3,''value'')==1,',...'shading interp,',...'end,',...'k=get(p1,''value'');,',...'switch k,',...'case 1,',...'colormap(''cool''),',...'case 2,',...'colormap(''spring''),',...'case 3,',...'colormap(''summer''),',...'case 4,',...'colormap(''autumn''),',...'case 5,',...'colormap(''winter''),',...'end'];l11=uimenu('parent',l1,...'label','Surface',...'tag','l11',...'callback',[...'[x,y]=meshgrid(-8:0.5:8);,',... 'r=sqrt(x.^2+y.^2)+eps;,',... 'z=sin(r)./r;,',...'surf(x,y,z),',... huidiao]);l12=uimenu('parent',l1,...'label','Mesh',...'tag','l12',...'callback',[...'mesh(peaks),',... huidiao]);l13=uimenu('parent',l1,...'label','Membrane',...'tag','l13',...'callback',[...'mesh(membrane),',... huidiao]);f1=uicontrol('parent',h0,... 'units','points',...'listboxtop',0,...'position',[12 6 100 101],...'style','frame',...'tag','f1');r1=uicontrol('parent',h0,...'units','points',...'backgroundcolor',[0.753 0.753 0.753],... 'listboxtop',0,...'position',[19.5 58.5 72.75 16.5],...'string','shading faceted',...'style','radiobutton',...'tag','r1',...'value',1,...'callback',[...'shading faceted,',...'set(r1,''value'',1);,',...'set(r2,''value'',0);,',...'set(r3,''value'',0);']);r2=uicontrol('parent',h0,...'units','points',...'backgroundcolor',[0.753 0.753 0.753],... 'listboxtop',0,...'position',[19.5 35.25 78.75 18.75],...'string','shading flat',...'style','radiobutton',...'tag','r2',...'value',0,...'callback',[...'shading flat,',...'set(r2,''value'',1);,',...'set(r1,''value'',0);,',...'set(r3,''value'',0);']);r3=uicontrol('parent',h0,...'units','points',...'backgroundcolor',[0.753 0.753 0.753],... 'listboxtop',0,...'position',[19.5 12.75 71.25 18.75],...'string','shading interp',...'style','radiobutton',...'tag','r3',...'value',0,...'callback',[...'shading interp,',...'set(r3,''value'',1);,',...'set(r1,''value'',0);,',...'set(r2,''value'',0);']);t1=uicontrol('parent',h0,...'units','points',...'backgroundcolor',[0.753 0.753 0.753],... 'fontsize',12,...'listboxtop',0,...'position',[14.25 75.75 90.75 22.5],...'string','平滑处理',...'style','text',...'tag','t1');t2=uicontrol('parent',h0,...'units','points',...'backgroundcolor',[0.753 0.753 0.753],... 'fontsize',12,...'listboxtop',0,...'position',[117 69 72.75 17.5],...'string','设置色调',...'style','text',...'tag','t2');p1=uicontrol('parent',h0,...'units','points',...'backgroundcolor',[0.753 0.753 0.753],...'listboxtop',0,...'position',[116.25 39 72.75 20.25],...'string','Cool|Spring|Summer|Autumn|Winter',... 'style','popupmenu',...'tag','p1',...'value',1,...'callback',[...'k=get(p1,''value'');,',...'switch k,',...'case 1,',...'colormap(''cool''),',...'case 2,',...'colormap(''spring''),',...'case 3,',...'colormap(''summer''),',...'case 4,',...'colormap(''autumn''),',...'case 5,',...'colormap(''winter''),',...'end']);b1=uicontrol('parent',h0,...'units','points',...'backgroundcolor',[0.753 0.753 0.753],...'listboxtop',0,...'position',[12 243 72.75 30.75],...'string','关闭',...'tag','b1',...'callback','close');b2=uicontrol('parent',h0,...'units','points',...'backgroundcolor',[0.753 0.753 0.753],... 'listboxtop',0,...'position',[216.75 67.5 83.25 18.75],... 'string','Colorbar',...'tag','b2',...'callback','colorbar');图形效果h0=figure('toolbar','none',...'position',[168 94.5 315 289.5],...'name','实例57');h1=axes('parent',h0,...'position',[0.4 0.4 0.5 0.5],...'visible','off');f1=uicontrol('parent',h0,...'style','frame',...'position',[15 10 80 70],...'string','dull',...'units','points',...'backgroundcolor',[0.753 0.753 0.753],... 'listboxtop',0,...'tag','r1',...'value',1,...'callback',[...'set(r1,''value'',1);,',...'set(r2,''value'',0);,',...'set(r3,''value'',0);,',...'material dull']);r1=uicontrol('parent',h0,...'style','radiobutton',...'position',[19.5 58.5 72.75 16.5],...'string','dull',...'units','points',...'backgroundcolor',[0.753 0.753 0.753],... 'listboxtop',0,...'tag','r1',...'value',1,...'callback',[...'set(r1,''value'',1);,',...'set(r2,''value'',0);,',...'set(r3,''value'',0);,',...'material dull']);r2=uicontrol('parent',h0,...'style','radiobutton',...'position',[19.5 35.25 72.75 16.5],...'string','metal',...'units','points',...'backgroundcolor',[0.753 0.753 0.753],... 'listboxtop',0,...'tag','r2',...'value',0,...'callback',[...'set(r2,''value'',1);,',...'set(r1,''value'',0);,',...'set(r3,''value'',0);,',...'material metal']);r3=uicontrol('parent',h0,...'style','radiobutton',...'position',[19.5 12.75 72.75 16.5],...'string','shiny',...'units','points',...'backgroundcolor',[0.753 0.753 0.753],... 'listboxtop',0,...'tag','r3',...'value',0,...'callback',[...'set(r3,''value'',1);,',...'set(r1,''value'',0);,',...'set(r2,''value'',0);,',...'material shiny']);u1=uimenu('parent',h0,...'label','绘图',...'backgroundcolor',[0.753 0.753 0.753],... 'tag','u1',...'callback',[...'[x,y]=meshgrid(-8:0.5:8);,',...'r=sqrt(x.^2+y.^2)+eps;,',...'z=sin(r)./r;,',...'surf(x,y,z),',...'shading interp']);b1=uicontrol('parent',h0,...'style','pushbutton',...'position',[19.5 150 60 20],...'string','light',...'units','points',...'backgroundcolor',[0.753 0.753 0.753],... 'listboxtop',0,...'tag','b1',...'callback','camlight headlight');b2=uicontrol('parent',h0,...'style','pushbutton',...'position',[19.5 100 60 20],...'string','关闭',...'units','points',...'backgroundcolor',[0.753 0.753 0.753],... 'listboxtop',0,...'tag','b2',...'callback','close');可控制小球运动速度的用户界面h0=figure('toolbar','none',...'position',[198****0468],...'name','实例58');h1=axes('parent',h0,...'position',[0.25 0.45 0.5 0.5],...'visible','off');t=0:0.1:4*pi;x=sin(t);y=cos(t);plot(x,y)axis equalaxis offh=line('color',[1 0 0],...'linestyle','.',...'xdata',0,...'ydata',1,...'markersize',20,...'erasemode','xor');n=length(t);i=1;speed=0.01;k=0;b1huidiao=[...'k=0;,',...'while 1,',...'set(h,''xdata'',x(i),''ydata'',y(i));,',... 'drawnow,',...'pause(speed),',...'i=i+1;,',...'if i>n,',...'i=1;,',...'end,',...'if k==1,',...'break,',...'end,',...'end'];b1=uicontrol('parent',h0,...'units','points',...'tag','b1',...'style','pushbutton',...'string','开始',...'backgroundcolor',[0.75 0.75 0.75],... 'position',[30 80 50 20],...'callback',b1huidiao);b2=uicontrol('parent',h0,...'units','points',...'style','pushbutton',...'string','停止',...'backgroundcolor',[0.75 0.75 0.75],... 'position',[100 80 50 20],...'callback','k=1;');b3=uicontrol('parent',h0,...'units','points',...'tag','b3',...'style','pushbutton',...'string','关闭',...'backgroundcolor',[0.75 0.75 0.75],... 'position',[170 80 50 20],...'callback',[...'k=1;,',...'close']);s1=uicontrol('parent',h0,...'units','points',...'tag','s1',...'style','slider',...'value',50*speed,...'max',1,...'min',0,...'backgroundcolor',[0.75 0.75 0.75],... 'position',[30 115 190 20],...'callback',[...'m=get(s1,''value'');,',...'speed=m/50;']);t1=uicontrol('parent',h0,...'units','points',...'style','text',...'fontsize',15,...'string','小球运动速度',...'backgroundcolor',[0.75 0.75 0.75],...'position',[30 135 190 20]);设置坐标轴纵横轴比h0=figure('name','实例59');h1=axes('parent',h0,...'position',[0.3 0.45 0.5 0.5],...'visible','off');u1=uimenu('parent',h0,...'label','绘图',...'backgroundcolor',[0.753 0.753 0.753],... 'tag','u1',...'callback',[...'[x,y]=meshgrid(-8:0.5:8);,',...'r=sqrt(x.^2+y.^2)+eps;,',...'z=sin(r)./r;,',...'mesh(x,y,z),',...'shading interp,',...'axis normal']);f1=uicontrol('parent',h0,...'units','points',...'listboxtop',0,...'position',[12 6 100 150],...'style','frame',...'tag','f1');t1=uicontrol('parent',h0,...'units','points',...'backgroundcolor',[0.753 0.753 0.753],... 'listboxtop',0,...'position',[19.5 130 72.75 16.5],...'string','坐标纵横比',...'style','text',...'tag','t1');r1=uicontrol('parent',h0,...'units','points',...'backgroundcolor',[0.753 0.753 0.753],... 'listboxtop',0,...'position',[19.5 110 72.75 16.5],...'string','axis equal',...'style','radiobutton',...'tag','r1',...'value',1,...'callback',[...'set(r1,''value'',1);,',...'set(r2,''value'',0);,',...'set(r3,''value'',0);,',...'set(r4,''value'',0);,',...'set(r5,''value'',0);,',...'axis equal']);r2=uicontrol('parent',h0,...'units','points',...'backgroundcolor',[0.753 0.753 0.753],... 'listboxtop',0,...'position',[19.5 85 72.75 16.5],...'string','axis square',...'style','radiobutton',...'tag','r2',...'value',0,...'callback',[...'set(r2,''value'',1);,',...'set(r1,''value'',0);,',...'set(r3,''value'',0);,',...'set(r4,''value'',0);,',...'set(r5,''value'',0);,',...'axis square']);r3=uicontrol('parent',h0,...'units','points',...'backgroundcolor',[0.753 0.753 0.753],... 'listboxtop',0,...'position',[19.5 60 72.75 16.5],...'string','axis image',...'style','radiobutton',...'tag','r3',...'value',0,...'callback',[...'set(r3,''value'',1);,',...'set(r2,''value'',0);,',...'set(r1,''value'',0);,',...'set(r4,''value'',0);,',...'set(r5,''value'',0);,',...'axis image']);r4=uicontrol('parent',h0,...'units','points',...'backgroundcolor',[0.753 0.753 0.753],... 'listboxtop',0,...'position',[19.5 35 72.75 16.5],...'string','axie vis3d',...'style','radiobutton',...'tag','r4',...'value',0,...'callback',[...'set(r4,''value'',1);,',...'set(r2,''value'',0);,',...'set(r3,''value'',0);,',...'set(r1,''value'',0);,',...'set(r5,''value'',0);,',...'axis vis3d']);r5=uicontrol('parent',h0,...'units','points',...'backgroundcolor',[0.753 0.753 0.753],... 'listboxtop',0,...'position',[19.5 10 72.75 16.5],...'string','axis auto',...'style','radiobutton',...'tag','r5',...'value',0,...'callback',[...'set(r5,''value'',1);,',...'set(r2,''value'',0);,',...'set(r3,''value'',0);,',...'set(r4,''value'',0);,',...'set(r1,''value'',0);,',...'axis auto']);b1=uicontrol('parent',h0,...'units','points',...'backgroundcolor',[0.753 0.753 0.753],... 'listboxtop',0,...'position',[12 243 72.75 30.75],...'string','关闭',...'tag','b1',...'callback','close');b2=uicontrol('parent',h0,...'units','points',...'backgroundcolor',[0.753 0.753 0.753],... 'listboxtop',0,...'position',[216.75 67.5 83.25 18.75],... 'string','Colorbar',...'tag','b2',...'callback','colorbar');动态文本显示h0=figure('toolbar','none',...'position',[198****0468],...'name','实例60');h1=axes('parent',h0,...'position',[0.25 0.45 0.5 0.5],...'visible','off');str1='当前阻尼比=';z=0.52;t=0:0.1:10;y=step(1,[1 2*z 1],t);hline=plot(t,y);grid onr1=uicontrol('parent',h0,...'units','points',...'tag','r1',...'style','radio',...'string','grid on',...'position',[30 120 60 20],...'backgroundcolor',[0.75 0.75 0.75],... 'value',1,...'callback',[...'grid on,',...'set(r1,''value'',1);,',...'set(r2,''value'',0)']);r2=uicontrol('parent',h0,...'units','points',...'tag','r2',...'style','radio',...'string','grid on',...'position',[30 95 60 20],...'backgroundcolor',[0.75 0.75 0.75],... 'value',0,...'callback',[...'grid off,',...'set(r2,''value'',1);,',...'set(r1,''value'',0)']);s1=uicontrol('parent',h0,...'units','points',...'tag','s1',...'style','slider',...'value',z,...'position',[100 95 150 20],...'backgroundcolor',[0.75 0.75 0.75],... 'max',1,...'min',0,...'callback',[...'z=get(s1,''value'');,',...。

matlab仿真实例100题

matlab仿真实例100题

matlab仿真实例100题Matlab是一种强大的数学软件,广泛应用于科学计算、数据分析和工程仿真等领域。

在学习和使用Matlab的过程中,通过实例的方式进行仿真练习是一种非常有效的学习方法。

下面将给出100个Matlab仿真实例题目,帮助读者更好地掌握Matlab的使用。

1. 编写一个程序,计算并输出1到100之间所有奇数的和。

2. 编写一个程序,计算并输出1到100之间所有偶数的乘积。

3. 编写一个程序,计算并输出1到100之间所有素数的个数。

4. 编写一个程序,计算并输出1到100之间所有整数的平方和。

5. 编写一个程序,计算并输出1到100之间所有整数的立方和。

6. 编写一个程序,计算并输出1到100之间所有整数的阶乘和。

7. 编写一个程序,计算并输出1到100之间所有整数的倒数和。

8. 编写一个程序,计算并输出1到100之间所有整数的平均值。

9. 编写一个程序,计算并输出1到100之间所有整数的中位数。

10. 编写一个程序,计算并输出1到100之间所有整数的标准差。

11. 编写一个程序,计算并输出1到100之间所有整数的方差。

12. 编写一个程序,计算并输出1到100之间所有整数的最大值。

13. 编写一个程序,计算并输出1到100之间所有整数的最小值。

15. 编写一个程序,计算并输出1到100之间所有整数的平方根和。

16. 编写一个程序,计算并输出1到100之间所有整数的立方根和。

17. 编写一个程序,计算并输出1到100之间所有整数的对数和。

18. 编写一个程序,计算并输出1到100之间所有整数的指数和。

19. 编写一个程序,计算并输出1到100之间所有整数的正弦和。

20. 编写一个程序,计算并输出1到100之间所有整数的余弦和。

21. 编写一个程序,计算并输出1到100之间所有整数的正切和。

22. 编写一个程序,计算并输出1到100之间所有整数的双曲正弦和。

23. 编写一个程序,计算并输出1到100之间所有整数的双曲余弦和。

matlab优化算法 100例

matlab优化算法 100例

matlab优化算法 100例Matlab是一种非常强大的数学软件,其中内置了许多实用的优化算法。

本文将围绕“Matlab优化算法100例”这个话题,分享一些基本的优化算法的应用实例。

第一步:优化函数的定义在使用Matlab优化算法之前,我们首先需要定义一个优化函数。

我们可以使用Matlab中的符号计算工具箱来定义优化函数,或者直接在脚本中定义一个函数。

下面是一个函数的例子:```function f = myfun(x)f = 0.26*(x(1)^2+x(2)^2)-0.48*x(1)*x(2);end```其中,这个函数计算了二次函数$f(x_1,x_2)=0.26(x_1^2+x_2^2)-0.48x_1x_2$ 的值。

这个函数的最小值可以通过优化算法来求解。

第二步:使用Matlab中的优化算法Matlab中提供了许多实用的优化算法,包括:梯度下降法、共轭梯度法、拟牛顿法、遗传算法、粒子群算法等等。

这些算法可以通过调用相关的函数来使用。

这里以fminunc函数为例,展示如何使用Matlab中的优化算法来求解前面定义的函数myfun的最小值。

```[x,fval,exitflag,output] = fminunc(@myfun, [0.3 0.3])```其中,@myfun是一个函数句柄,[0.3 0.3]是参数的初始值。

此函数将返回优化的最小值,以及一些其他的输出。

第三步:分析结果并优化我们可以使用Matlab中的 plot 函数来将优化结果可视化,以便更好地理解问题。

通过这个可视化,我们可以分析结果并进行优化。

```[X,Y]=meshgrid(-10:0.1:10,-10:0.1:10);Z=0.26*(X.^2+Y.^2)-0.48*X.*Y;figurecontour(X,Y,Z,50);hold onplot(x(1), x(2), 'ro')hold off```这里,我们使用 meshgrid 函数生成一个平面,并计算出函数的值。

matlab应用百例

matlab应用百例

matlab应用百例这是很实用的图像处理例子,一共100个,几乎包含完了常见的函数。

内容:1-32是:图形应用篇33-66是:界面设计篇67-84是:图形处理篇85-100是:数值分析篇实例1:三角函数曲线(1)实例2:三角函数曲线(2)实例3:图形的叠加实例4:双y轴图形的绘制实例5:单个轴窗口显示多个图形实例6:图形标注实例7:条形图形实例8:区域图形实例9:饼图的绘制实例10:阶梯图实例11:枝干图实例12:罗盘图实例13:轮廓图实例14:交互式图形实例15:变换的傅立叶函数曲线实例16:劳伦兹非线形方程的无序活动实例17:填充图例18:条形图和阶梯形图实例19:三维曲线图实例20:图形的隐藏属性实例21PEAKS函数曲线实例22:片状图实例23:视角的调整实例24:向量场的绘制实例25:灯光定位实例26:柱状图实例27:设置照明方式实例28:羽状图实例29:立体透视(1)实例30:立体透视(2)实例31:表面图形实例32:沿曲线移动的小球实例33:曲线转换按钮实例34:栅格控制按钮实例35:编辑框的使用实例36:弹出式菜单实例37:滑标的使用实例38:多选菜单实例39:菜单控制的使用实例40:UIMENU菜单的应用实例41:除法计算器实例42:单选框的使用实例43:添加环境效果实例44:改变坐标轴范围实例45:简单运算器实例46:曲线色彩的修改实例47:曲线标记实例48:修改曲型实例49:指定坐标轴范围实例50:绘制不同函数曲线的用户界面实例51:可设置函数曲线图视角的用户界面实例52:可设置函数曲线图视角的用户界面实例53:可设置函数曲线光源的用户界面实例54:添加效果实例55:查询日期实例56:图形效果(1)实例57:图形效果实例58:可控制小球运动速度的用户界面实例59:设置坐标轴纵横轴比实例60:动态文本显示实例61:浏览流体数据实例62:简单计算器实例63:字母统计实例64:图形的几何操作实例65:时间计算器实例66:数字操作实例67:图像的块操作实例68:图形的过滤操作实例69:图像的频率操作实例70:函数变换实例71:RADON函数变换实例72:图像分析(1)实例73:过滤图像实例74:图像的区域处理实例75:图像的颜色处置实例76:交换显示图像实例77:矢量数据的显示实例78:图像分析(2)实例79:图像逻辑操作实例80:进度条的使用例81:MRI数据的显示实例82:图像类型转换实例83:特殊的图像显示技术实例84:图像的几何操作实例85:拉个朗日插值例86:三次样条插值法实例87:NEWTON插值实例88:hermite插值实例89:mewton形式的hermite插值实例90:平方根法实例91:gauss消去法实例92:三角分解法实例93:jacobi迭代法实例94:gauss迭代法实例95:sor迭代法实例96:最速下降法实例97:共额梯度法实例98:mewton迭代法实例99:broyden迭代法实例100:逆broyden迭代法。

matlab实用程序百例(3)设...

matlab实用程序百例(3)设...

matlab实用程序百例(3)设...matlab实用程序百例实例27:设置照明方式function shili27h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例27');subplot(2,2,1)sphereshading flatcamlight leftcamlight rightlighting flatcolorbaraxis offtitle('Figure1')subplot(2,2,2)sphereshading flatcamlight leftcamlight rightlighting gouraudcolorbaraxis offtitle('Figure2')subplot(2,2,3)sphereshading interpcamlight rightcamlight leftlighting phongcolorbaraxis offtitle('Figure3')subplot(2,2,4)sphereshading flatcamlight leftcamlight rightlighting nonecolorbaraxis offtitle('Figure4')实例28:羽状图function shili28h0=figure('toolbar','none',... 'position',[200 150 450 350],... 'name','实例28');subplot(2,1,1)alpha=90:-10:0;r=ones(size(alpha));m=alpha*pi/180;n=r*10;[u,v]=pol2cart(m,n);feather(u,v)title('羽状图')axis([0 20 0 10])subplot(2,1,2)t=0:0.5:10;x=0.05+i;y=exp(-x*t);feather(y)title('复数矩阵的羽状图')实例29:立体透视(1)function shili29h0=figure('toolbar','none',... 'position',[200 150 450 250],... 'name','实例29');[x,y,z]=meshgrid(-2:0.1:2,...-2:0.1:2,...-2:0.1:2);v=x.*exp(-x.^2-y.^2-z.^2); grid onfor i=-2:0.5:2;h1=surf(linspace(-2,2,20),... linspace(-2,2,20),...zeros(20)+i);rotate(h1,[1 -1 1],30)dx=get(h1,'xdata');dy=get(h1,'ydata');dz=get(h1,'zdata');delete(h1)slice(x,y,z,v,[-2 2],2,-2)hold onslice(x,y,z,v,dx,dy,dz)hold offaxis tightview(-5,10)drawnowend实例30:立体透视(2)function shili30h0=figure('toolbar','none',... 'position',[200 150 450 250],... 'name','实例30');[x,y,z]=meshgrid(-2:0.1:2,...-2:0.1:2,...-2:0.1:2);v=x.*exp(-x.^2-y.^2-z.^2); [dx,dy,dz]=cylinder;slice(x,y,z,v,[-2 2],2,-2)for i=-2:0.2:2h=surface(dx+i,dy,dz); rotate(h,[1 0 0],90)xp=get(h,'xdata');yp=get(h,'ydata');zp=get(h,'zdata');delete(h)hold onhs=slice(x,y,z,v,xp,yp,zp); axis tightxlim([-3 3])view(-10,35)drawnowdelete(hs)hold offend实例31:表面图形function shili31h0=figure('toolbar','none',...'position',[200 150 550 250],... 'name','实例31');subplot(1,2,1)x=rand(100,1)*16-8;y=rand(100,1)*16-8;r=sqrt(x.^2+y.^2)+eps;z=sin(r)./r;xlin=linspace(min(x),max(x),33); ylin=linspace(min(y),max(y),33); [X,Y]=meshgrid(xlin,ylin);Z=griddata(x,y,z,X,Y,'cubic'); mesh(X,Y,Z)axis tighthold onplot3(x,y,z,'.','Markersize',20) subplot(1,2,2)k=5;n=2^k-1;theta=pi*(-n:2:n)/n;phi=(pi/2)*(-n:2:n)'/n;X=cos(phi)*cos(theta);Y=cos(phi)*sin(theta);Z=sin(phi)*ones(size(theta)); colormap([0 0 0;1 1 1])C=hadamard(2^k);surf(X,Y,Z,C)axis square实例32:沿曲线移动的小球h0=figure('toolbar','none',... 'position',[198****8468],... 'name','实例32');h1=axes('parent',h0,...'position',[0.15 0.45 0.7 0.5],... 'visible','on');t=0:pi/24:4*pi;y=sin(t);plot(t,y,'b')n=length(t);h=line('color',[0 0.5 0.5],...'linestyle','.',...'markersize',25,...'erasemode','xor');k1=uicontrol('parent',h0,... 'style','pushbutton',...'position',[80 100 50 30],...'string','开始',...'callback',[...'i=1;',...'k=1;,',...'m=0;,',...'while 1,',...'if k==0,',...'break,',...'end,',...'if k~=0,',...'set(h,''xdata'',t(i),''ydata'',y(i)),',... 'drawnow;,',...'i=i+1;,',...'if i>n,',...'m=m+1;,',...'i=1;,',...'end,',...'end,',...'end']);k2=uicontrol('parent',h0,...'style','pushbutton',...'position',[180 100 50 30],...'string','停止',...'callback',[...'k=0;,',...'set(e1,''string'',m),',...'p=get(h,''xdata'');,',...'q=get(h,''ydata'');,',...'set(e2,''string'',p);,',...'set(e3,''string'',q)']);k3=uicontrol('parent',h0,...'style','pushbutton',...'position',[280 100 50 30],...'string','关闭',...'callback','close');e1=uicontrol('parent',h0,...'style','edit',...'position',[60 30 60 20]);t1=uicontrol('parent',h0,...'style','text',...'string','循环次数',...'position',[60 50 60 20]);e2=uicontrol('parent',h0,...'style','edit',...'position',[180 30 50 20]);t2=uicontrol('parent',h0,...'style','text',...'string','终点的X坐标值',...'position',[155 50 100 20]);e3=uicontrol('parent',h0,...'style','edit',...'position',[300 30 50 20]);t3=uicontrol('parent',h0,...'style','text',...'string','终点的Y坐标值',...'position',[275 50 100 20]);实例33:曲线转换按钮h0=figure('toolbar','none',... 'position',[200 150 450 250],... 'name','实例33');x=0:0.5:2*pi;y=sin(x);h=plot(x,y);grid onhuidiao=[...'if i==1,',...'i=0;,',...'y=cos(x);,',...'delete(h),',...'set(hm,''string'',''正弦函数''),',...'h=plot(x,y);,',...'grid on,',...'else if i==0,',...'i=1;,',...'y=sin(x);,',...'set(hm,''string'',''余弦函数''),',...'delete(h),',...'h=plot(x,y);,',...'grid on,',...'end,',...'end'];hm=uicontrol(gcf,'style','pushbutton',... 'string','余弦函数',...'callback',huidiao);i=1;set(hm,'position',[250 20 60 20]);set(gca,'position',[0.2 0.2 0.6 0.6])title('按钮的使用')hold on实例34:栅格控制按钮h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例34');x=0:0.5:2*pi;y=sin(x);plot(x,y)huidiao1=[...'set(h_toggle2,''value'',0),',...'grid on,',...];huidiao2=[...'set(h_toggle1,''value'',0),',...'grid off,',...];h_toggle1=uicontrol(gcf,'style','togglebutton',... 'string','grid on',...'value',0,...'position',[20 45 50 20],...'callback',huidiao1);h_toggle2=uicontrol(gcf,'style','togglebutton',... 'string','grid off',...'value',0,...'position',[20 20 50 20],...'callback',huidiao2);set(gca,'position',[0.2 0.2 0.6 0.6])title('开关按钮的使用')实例35:编辑框的使用h0=figure('toolbar','none',...'position',[200 150 350 250],...'name','实例35');f='Please input the letter';huidiao1=[...'g=upper(f);,',...'set(h2_edit,''string'',g),',...];huidiao2=[...'g=lower(f);,',...'set(h2_edit,''string'',g),',...];h1_edit=uicontrol(gcf,'style','edit',...'position',[100 200 100 50],...'HorizontalAlignment','left',...'string','Please input the letter',...'callback','f=get(h1_edit,''string'');',...'background','w',...'max',5,...'min',1);h2_edit=uicontrol(gcf,'style','edit',...'HorizontalAlignment','left',...'position',[100 100 100 50],...'background','w',...'max',5,...'min',1);h1_button=uicontrol(gcf,'style','pushbutton',... 'string','小写变大写',...'position',[100 45 100 20],...'callback',huidiao1);h2_button=uicontrol(gcf,'style','pushbutton',... 'string','大写变小写',...'position',[100 20 100 20],...'callback',huidiao2);实例36:弹出式菜单h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例36');x=0:0.5:2*pi;y=sin(x);h=plot(x,y);grid onhm=uicontrol(gcf,'style','popupmenu',... 'string',...'sin(x)|cos(x)|sin(x)+cos(x)|exp(-sin(x))',... 'position',[250 20 50 20]);set(hm,'value',1)huidiao=[...'v=get(hm,''value'');,',...'switch v,',...'case 1,',...'delete(h),',...'y=sin(x);,',...'h=plot(x,y);,',...'grid on,',...'case 2,',...'delete(h),',...'y=cos(x);,',...'h=plot(x,y);,',...'grid on,',...'case 3,',...'delete(h),',...'y=sin(x)+cos(x);,',...'h=plot(x,y);,',...'grid on,',...'case 4,',...'delete(h),',...'y=exp(-sin(x));,',...'h=plot(x,y);,',...'grid on,',...'end'];set(hm,'callback',huidiao)set(gca,'position',[0.2 0.2 0.6 0.6]) title('弹出式菜单的使用')hold on。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

1-32是:图形应用篇33-66是:界面设计篇67-84是:图形处理篇85-100是:数值分析篇实例1:三角函数曲线(1)function shili01h0=figure('toolbar','none',...'position',[198 56 350 300],... 'name','实例01');h1=axes('parent',h0,...'visible','off');x=-pi::pi;y=sin(x);plot(x,y);xlabel('自变量X');ylabel('函数值Y');title('SIN( )函数曲线');grid on实例2:三角函数曲线(2)function shili02h0=figure('toolbar','none',...'position',[200 150 450 350],... 'name','实例02');x=-pi::pi;y=sin(x)+cos(x);plot(x,y,'-*r','linewidth',1);grid onxlabel('自变量X');ylabel('函数值Y');title('三角函数');实例3:图形的叠加function shili03h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例03');x=-pi::pi;y1=sin(x);y2=cos(x);plot(x,y1,...'-*r',...x,y2,...'--og');grid onxlabel('自变量X');ylabel('函数值Y');title('三角函数');实例4:双y轴图形的绘制function shili04h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例04');x=0:900;a=1000;b=;y1=2*x;y2=cos(b*x);[haxes,hline1,hline2]=plotyy(x,y1,x,y2,'semilogy','plot'); axes(haxes(1))ylabel('semilog plot');axes(haxes(2))ylabel('linear plot');实例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,1)plot(sin(t),cos(t))axis equalsubplot(2,2,2)z=sin(x)-cos(y);plot(t,z)axis([0 2*pi -2 2])subplot(2,2,3)h=sin(x)+cos(y);plot(t,h)axis([0 2*pi -2 2])subplot(2,2,4)g=(sin(x).^2)-(cos(y).^2);plot(t,g)axis([0 2*pi -1 1])实例6:图形标注function shili06h0=figure('toolbar','none',...'position',[200 150 450 400],...'name','实例06');t=0:pi/10:2*pi;h=plot(t,sin(t));xlabel('t=0到2\pi','fontsize',16);ylabel('sin(t)','fontsize',16);title('\it{从 0to2\pi 的正弦曲线}','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),...['\leftarrow最小值=',num2str(y(imin))],...'fontsize',16)text(x(imax),y(imax),...['\leftarrow最大值=',num2str(y(imax))],...'fontsize',16)实例7:条形图形function shili07h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例07');tiao1=[562 548 224 545 41 445 745 512];tiao2=[47 48 57 58 54 52 65 48];t=0:7;bar(t,tiao1)xlabel('X轴');ylabel('TIAO1值');h1=gca;h2=axes('position',get(h1,'position'));plot(t,tiao2,'linewidth',3)set(h2,'yaxislocation','right','color','none','xticklabel',[])实例8:区域图形function shili08h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例08');x=91:95;profits1=[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,profits1,'facecolor',[ ],...'edgecolor','b',...'linewidth',3)hold onarea(x,profits2,'facecolor',[ ],...'edgecolor','y',...'linewidth',3)hold onarea(x,profits3,'facecolor',[ ],...'edgecolor','r',...'linewidth',3)hold onarea(x,profits4,'facecolor',[ ],...'edgecolor','m',...'linewidth',3)hold offset(gca,'xtick',[91:95])set(gca,'layer','top')gtext('\leftarrow第一季度销量')gtext('\leftarrow第二季度销量')gtext('\leftarrow第三季度销量')gtext('\leftarrow第四季度销量')xlabel('年','fontsize',16);ylabel('销售量','fontsize',16);实例9:饼图的绘制function shili09h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例09');t=[54 21 35;68 54 35;45 25 12;48 68 45;68 54 69];x=sum(t);h=pie(x);textobjs=findobj(h,'type','text');str1=get(textobjs,{'string'});val1=get(textobjs,{'extent'});oldext=cat(1,val1{:});names={'商品一:';'商品二:';'商品三:'};str2=strcat(names,str1);set(textobjs,{'string'},str2)val2=get(textobjs,{'extent'});newext=cat(1,val2{:});offset=sign(oldext(:,1)).*(newext(:,3)-oldext(:,3))/2; pos=get(textobjs,{'position'});textpos=cat(1,pos{:});textpos(:,1)=textpos(:,1)+offset;set(textobjs,{'position'},num2cell(textpos,[3,2]))实例10:阶梯图function shili10h0=figure('toolbar','none',...'position',[200 150 450 400],...'name','实例10');a=;b=;t=0:10;f=exp(-a*t).*sin(b*t);stairs(t,f)hold onplot(t,f,':*')hold offglabel='函数e^{-(\alpha*t)}sin\beta*t的阶梯图';gtext(glabel,'fontsize',16)xlabel('t=0:10','fontsize',16)axis([0 10 ])实例11:枝干图function shili11h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例11');x=0:pi/20:2*pi;y1=sin(x);y2=cos(x);h1=stem(x,y1+y2);hold onh2=plot(x,y1,'^r',x,y2,'*g');hold offh3=[h1(1);h2];legend(h3,'y1+y2','y1=sin(x)','y2=cos(x)') xlabel('自变量X');ylabel('函数值Y');title('正弦函数与余弦函数的线性组合');实例12:罗盘图function shili12h0=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/180;[x,y]=pol2cart(rdirection,windpower); compass(x,y);desc={'风向和风力','北京气象台','10月1日0:00到','10月1日12:00'};gtext(desc)实例13:轮廓图function shili13h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例13');[th,r]=meshgrid((0:10:360)*pi/180,0::1); [x,y]=pol2cart(th,r);z=x+i*y;f=(z.^4-1).^;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 shili14h0=figure('toolbar','none',...'position',[200 150 450 250],... 'name','实例14');axis([0 10 0 10]);hold onx=[];y=[];n=0;disp('单击鼠标左键点取需要的点'); disp('单击鼠标右键点取最后一个点'); but=1;while but==1[xi,yi,but]=ginput(1);plot(xi,yi,'bo')n=n+1;disp('单击鼠标左键点取下一个点'); x(n,1)=xi;y(n,1)=yi;endt=1:n;ts=1::n;xs=spline(t,x,ts);ys=spline(t,y,ts);plot(xs,ys,'r-');hold off实例14:交互式图形function shili14h0=figure('toolbar','none',...'position',[200 150 450 250],... 'name','实例14');axis([0 10 0 10]);hold onx=[];y=[];n=0;disp('单击鼠标左键点取需要的点');disp('单击鼠标右键点取最后一个点');but=1;while but==1[xi,yi,but]=ginput(1);plot(xi,yi,'bo')n=n+1;disp('单击鼠标左键点取下一个点');x(n,1)=xi;y(n,1)=yi;endt=1:n;ts=1::n;xs=spline(t,x,ts);ys=spline(t,y,ts);plot(xs,ys,'r-');hold off实例15:变换的傅立叶函数曲线function shili15h0=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],'min',1,'max',20)for j=1:20plot(fft(eye(j+16)))set(h,'value',j)m(:,j)=getframe(gcf);endclf;axes('position',[0 0 1 1]);movie(m,30)实例16:劳伦兹非线形方程的无序活动function shili15h0=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],'min',1,'max',20)for j=1:20plot(fft(eye(j+16)))set(h,'value',j)m(:,j)=getframe(gcf);endclf;axes('position',[0 0 1 1]);movie(m,30)实例17:填充图function shili17h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例17');t=(1:2:15)*pi/8;x=sin(t);y=cos(t);fill(x,y,'r')axis square offtext(0,0,'STOP',...'color',[1 1 1],...'fontsize',50,...'horizontalalignment','center')例18:条形图和阶梯形图function shili18h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例18');subplot(2,2,1)x=-3::3;y=exp(-x.*x);bar(x,y)title('2-D Bar Chart')subplot(2,2,2)x=-3::3;y=exp(-x.*x);bar3(x,y,'r')title('3-D Bar Chart')subplot(2,2,3)x=-3::3;y=exp(-x.*x);stairs(x,y)title('Stair Chart')subplot(2,2,4)x=-3::3;y=exp(-x.*x);barh(x,y)title('Horizontal Bar Chart')实例19:三维曲线图function shili19h0=figure('toolbar','none',...'position',[200 150 450 400],... 'name','实例19');subplot(2,1,1)x=linspace(0,2*pi);y1=sin(x);y2=cos(x);y3=sin(x)+cos(x);z1=zeros(size(x));z2=*z1;z3=z1;plot3(x,y1,z1,x,y2,z2,x,y3,z3)grid onxlabel('X轴');ylabel('Y轴');zlabel('Z轴');title('Figure1:3-D Plot')subplot(2,1,2)x=linspace(0,2*pi);y1=sin(x);y2=cos(x);y3=sin(x)+cos(x);z1=zeros(size(x));z2=*z1;z3=z1;plot3(x,z1,y1,x,z2,y2,x,z3,y3)grid onxlabel('X轴');ylabel('Y轴');zlabel('Z轴');title('Figure2:3-D Plot')实例20:图形的隐藏属性function shili20h0=figure('toolbar','none',...'position',[200 150 450 300],... 'name','实例20');subplot(1,2,1)[x,y,z]=sphere(10);mesh(x,y,z)axis offtitle('Figure1:Opaque')hidden onsubplot(1,2,2)[x,y,z]=sphere(10);mesh(x,y,z)axis offtitle('Figure2:Transparent')hidden off实例21PEAKS函数曲线function shili21h0=figure('toolbar','none',...'position',[200 100 450 450],... 'name','实例21');[x,y,z]=peaks(30);subplot(2,1,1)x=x(1,:);y=y(:,1);i=find(y>&y<;j=find(x>&x<;z(i,j)=nan*z(i,j);surfc(x,y,z)xlabel('X轴');ylabel('Y轴');zlabel('Z轴');title('Figure1:surfc函数形成的曲面')subplot(2,1,2)x=x(1,:);y=y(:,1);i=find(y>&y<;j=find(x>&x<;z(i,j)=nan*z(i,j);surfl(x,y,z)xlabel('X轴');ylabel('Y轴');zlabel('Z轴');title('Figure2:surfl函数形成的曲面')实例22:片状图function shili22h0=figure('toolbar','none',...'position',[200 150 550 350],...'name','实例22');subplot(1,2,1)x=rand(1,20);y=rand(1,20);z=peaks(x,y*pi);t=delaunay(x,y);trimesh(t,x,y,z)hidden offtitle('Figure1:Triangular Surface Plot');subplot(1,2,2)x=rand(1,20);y=rand(1,20);z=peaks(x,y*pi);t=delaunay(x,y);trisurf(t,x,y,z)title('Figure1:Triangular Surface Plot');实例23:视角的调整function shili23h0=figure('toolbar','none',...'position',[200 150 450 350],...'name','实例23');x=-5::5;[x,y]=meshgrid(x);r=sqrt(x.^2+y.^2)+eps;z=sin(r)./r;subplot(2,2,1)surf(x,y,z)xlabel('X-axis')ylabel('Y-axis')zlabel('Z-axis')title('Figure1')view,30)subplot(2,2,2)surf(x,y,z)xlabel('X-axis')ylabel('Y-axis')zlabel('Z-axis')title('Figure2')view+90,30)subplot(2,2,3)surf(x,y,z)xlabel('X-axis')ylabel('Y-axis')zlabel('Z-axis')title('Figure3')view,60)subplot(2,2,4)surf(x,y,z)xlabel('X-axis')ylabel('Y-axis')zlabel('Z-axis')title('Figure4')view(180,0)实例24:向量场的绘制function shili24h0=figure('toolbar','none',...'position',[200 150 450 350],... 'name','实例24');subplot(2,2,1)z=peaks;ribbon(z)title('Figure1')subplot(2,2,2)[x,y,z]=peaks(15);[dx,dy]=gradient(z,,;contour(x,y,z,10)hold onquiver(x,y,dx,dy)hold offtitle('Figure2')subplot(2,2,3)[x,y,z]=peaks(15);[nx,ny,nz]=surfnorm(x,y,z);surf(x,y,z)hold onquiver3(x,y,z,nx,ny,nz)hold offtitle('Figure3')subplot(2,2,4)x=rand(3,5);y=rand(3,5);z=rand(3,5);c=rand(3,5);fill3(x,y,z,c)grid ontitle('Figure4')实例25:灯光定位function shili25h0=figure('toolbar','none',...'position',[200 150 450 250],... 'name','实例25');vert=[1 1 1;1 2 1;2 2 1;2 1 1;1 1 2;12 2;2 2 2;2 1 2];fac=[1 2 3 4;2 6 7 3;4 3 7 8;15 8 4;1 2 6 5;5 6 7 8];grid offsphere(36)h=findobj('type','surface');set(h,'facelighting','phong',...'facecolor',...'interp',...'edgecolor',[ ],...'backfacelighting',...'lit')hold onpatch('faces',fac,'vertices',vert,... 'facecolor','y');light('position',[1 3 2]);light('position',[-3 -1 3]);material shinyaxis vis3d offhold off实例26:柱状图function shili26h0=figure('toolbar','none',...'position',[200 50 450 450],... 'name','实例26');subplot(2,1,1)x=[5 2 18 7 39 8 65 5 54 3 2];bar(x)xlabel('X轴');ylabel('Y轴');title('第一子图');subplot(2,1,2)y=[5 2 18 7 39 8 65 5 54 3 2];barh(y)xlabel('X轴');ylabel('Y轴');title('第二子图');实例27:设置照明方式function shili27h0=figure('toolbar','none',...'position',[200 150 450 350],... 'name','实例27');subplot(2,2,1)sphereshading flatcamlight leftcamlight rightlighting flatcolorbaraxis offtitle('Figure1')subplot(2,2,2)sphereshading flatcamlight leftcamlight rightlighting gouraudcolorbaraxis offtitle('Figure2')subplot(2,2,3)sphereshading interpcamlight rightcamlight leftlighting phongcolorbaraxis offtitle('Figure3')subplot(2,2,4)sphereshading flatcamlight leftcamlight rightlighting nonecolorbaraxis offtitle('Figure4')实例28:羽状图function shili28h0=figure('toolbar','none',...'position',[200 150 450 350],... 'name','实例28');subplot(2,1,1)alpha=90:-10:0;r=ones(size(alpha));m=alpha*pi/180;n=r*10;[u,v]=pol2cart(m,n);feather(u,v)title('羽状图')axis([0 20 0 10])subplot(2,1,2)t=0::10;x=+i;y=exp(-x*t);feather(y)title('复数矩阵的羽状图')实例29:立体透视(1)function shili29h0=figure('toolbar','none',...'position',[200 150 450 250],... 'name','实例29');[x,y,z]=meshgrid(-2::2,...-2::2,...-2::2);v=x.*exp(-x.^2-y.^2-z.^2);grid onfor i=-2::2;h1=surf(linspace(-2,2,20),...linspace(-2,2,20),...zeros(20)+i);rotate(h1,[1 -1 1],30)dx=get(h1,'xdata');dy=get(h1,'ydata');dz=get(h1,'zdata');delete(h1)slice(x,y,z,v,[-2 2],2,-2)hold onslice(x,y,z,v,dx,dy,dz)hold offaxis tightview(-5,10)drawnowend实例30:立体透视(2)function shili30h0=figure('toolbar','none',...'position',[200 150 450 250],... 'name','实例30');[x,y,z]=meshgrid(-2::2,...-2::2,...-2::2);v=x.*exp(-x.^2-y.^2-z.^2);[dx,dy,dz]=cylinder;slice(x,y,z,v,[-2 2],2,-2)for i=-2::2h=surface(dx+i,dy,dz);rotate(h,[1 0 0],90)xp=get(h,'xdata');yp=get(h,'ydata');zp=get(h,'zdata');delete(h)hold onhs=slice(x,y,z,v,xp,yp,zp);axis tightxlim([-3 3])view(-10,35)drawnowdelete(hs)hold offend实例31:表面图形function shili31h0=figure('toolbar','none',...'position',[200 150 550 250],... 'name','实例31');subplot(1,2,1)x=rand(100,1)*16-8;y=rand(100,1)*16-8;r=sqrt(x.^2+y.^2)+eps;z=sin(r)./r;xlin=linspace(min(x),max(x),33);ylin=linspace(min(y),max(y),33); [X,Y]=meshgrid(xlin,ylin);Z=griddata(x,y,z,X,Y,'cubic');mesh(X,Y,Z)axis tighthold onplot3(x,y,z,'.','Markersize',20)subplot(1,2,2)k=5;n=2^k-1;theta=pi*(-n:2:n)/n;phi=(pi/2)*(-n:2:n)'/n;X=cos(phi)*cos(theta);Y=cos(phi)*sin(theta);Z=sin(phi)*ones(size(theta));colormap([0 0 0;1 1 1])C=hadamard(2^k);surf(X,Y,Z,C)axis square实例32:沿曲线移动的小球h0=figure('toolbar','none',...'position',[198 56 408 468],...'name','实例32');h1=axes('parent',h0,...'position',[ ],...'visible','on');t=0:pi/24:4*pi;y=sin(t);plot(t,y,'b')n=length(t);h=line('color',[0 ],...'linestyle','.',...'markersize',25,...'erasemode','xor');k1=uicontrol('parent',h0,...'style','pushbutton',...'position',[80 100 50 30],...'string','开始',...'callback',[...'i=1;',...'k=1;,',...'m=0;,',...'while 1,',...'if k==0,',...'break,',...'end,',...'if k~=0,',...'set(h,''xdata'',t(i),''ydata'',y(i)),',... 'drawnow;,',...'i=i+1;,',...'if i>n,',...'m=m+1;,',...'i=1;,',...'end,',...'end,',...'end']);k2=uicontrol('parent',h0,...'style','pushbutton',...'position',[180 100 50 30],...'string','停止',...'callback',[...'k=0;,',...'set(e1,''string'',m),',...'p=get(h,''xdata'');,',...'q=get(h,''ydata'');,',... 'set(e2,''string'',p);,',...'set(e3,''string'',q)']);k3=uicontrol('parent',h0,...'style','pushbutton',...'position',[280 100 50 30],...'string','关闭',...'callback','close');e1=uicontrol('parent',h0,...'style','edit',...'position',[60 30 60 20]);t1=uicontrol('parent',h0,...'style','text',...'string','循环次数',...'position',[60 50 60 20]);e2=uicontrol('parent',h0,...'style','edit',...'position',[180 30 50 20]);t2=uicontrol('parent',h0,...'style','text',...'string','终点的X坐标值',...'position',[155 50 100 20]);e3=uicontrol('parent',h0,...'style','edit',...'position',[300 30 50 20]);t3=uicontrol('parent',h0,...'style','text',...'string','终点的Y坐标值',...'position',[275 50 100 20]);实例33:曲线转换按钮h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例33');x=0::2*pi;y=sin(x);h=plot(x,y);grid onhuidiao=[...'if i==1,',...'i=0;,',...'y=cos(x);,',...'delete(h),',...'set(hm,''string'',''正弦函数''),',... 'h=plot(x,y);,',...'grid on,',...'else if i==0,',...'i=1;,',...'y=sin(x);,',...'set(hm,''string'',''余弦函数''),',... 'delete(h),',...'h=plot(x,y);,',...'grid on,',...'end,',...'end'];hm=uicontrol(gcf,'style','pushbutton',... 'string','余弦函数',...'callback',huidiao);i=1;set(hm,'position',[250 20 60 20]);set(gca,'position',[ ])title('按钮的使用')hold on实例34:栅格控制按钮h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例34');x=0::2*pi;y=sin(x);plot(x,y)huidiao1=[...'set(h_toggle2,''value'',0),',...];huidiao2=[...'set(h_toggle1,''value'',0),',...'grid off,',...];h_toggle1=uicontrol(gcf,'style','togglebutton',... 'string','grid on',...'value',0,...'position',[20 45 50 20],...'callback',huidiao1);h_toggle2=uicontrol(gcf,'style','togglebutton',... 'string','grid off',...'value',0,...'position',[20 20 50 20],...'callback',huidiao2);set(gca,'position',[ ])title('开关按钮的使用')实例35:编辑框的使用h0=figure('toolbar','none',...'position',[200 150 350 250],...'name','实例35');f='Please input the letter';huidiao1=[...'g=upper(f);,',...'set(h2_edit,''string'',g),',...];huidiao2=[...'g=lower(f);,',...'set(h2_edit,''string'',g),',...];h1_edit=uicontrol(gcf,'style','edit',...'position',[100 200 100 50],...'HorizontalAlignment','left',...'string','Please input the letter',...'callback','f=get(h1_edit,''string'');',...'background','w',...'max',5,...'min',1);h2_edit=uicontrol(gcf,'style','edit',...'HorizontalAlignment','left',...'position',[100 100 100 50],...'max',5,...'min',1);h1_button=uicontrol(gcf,'style','pushbutton',... 'string','小写变大写',...'position',[100 45 100 20],...'callback',huidiao1);h2_button=uicontrol(gcf,'style','pushbutton',... 'string','大写变小写',...'position',[100 20 100 20],...'callback',huidiao2);实例36:弹出式菜单h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例36');x=0::2*pi;y=sin(x);h=plot(x,y);grid onhm=uicontrol(gcf,'style','popupmenu',...'string',...'sin(x)|cos(x)|sin(x)+cos(x)|exp(-sin(x))',... 'position',[250 20 50 20]);set(hm,'value',1)huidiao=[...'v=get(hm,''value'');,',...'switch v,',...'case 1,',...'delete(h),',...'y=sin(x);,',...'h=plot(x,y);,',...'grid on,',...'case 2,',...'delete(h),',...'y=cos(x);,',...'h=plot(x,y);,',...'grid on,',...'case 3,',...'delete(h),',...'y=sin(x)+cos(x);,',...'h=plot(x,y);,',...'grid on,',...'case 4,',...'delete(h),',...'y=exp(-sin(x));,',...'h=plot(x,y);,',...'grid on,',...'end'];set(hm,'callback',huidiao)set(gca,'position',[ ])title('弹出式菜单的使用')实例37:滑标的使用h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例37');[x,y]=meshgrid(-8::8);r=sqrt(x.^2+y.^2)+eps;z=sin(r)./r;h0=mesh(x,y,z);h1=axes('position',...[ ],...'visible','off');htext=uicontrol(gcf,...'units','points',...'position',[20 30 45 15],...'string','brightness',...'style','text');hslider=uicontrol(gcf,...'units','points',...'position',[10 10 300 15],...'min',-1,...'max',1,...'style','slider',...'callback',...'brighten(get(hslider,''value''))');实例38:多选菜单h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例38');[x,y]=meshgrid(-8::8);r=sqrt(x.^2+y.^2)+eps;z=sin(r)./r;h0=mesh(x,y,z);hlist=uicontrol(gcf,'style','listbox',...'string','default|spring|summer|autumn|winter',...'max',5,...'min',1,...'position',[20 20 80 100],...'callback',[...'k=get(hlist,''value'');,',...'switch k,',...'case 1,',...'colormap default,',...'case 2,',...'colormap spring,',...'case 3,',...'colormap summer,',...'case 4,',...'colormap autumn,',...'case 5,',...'colormap winter,',...'end']);实例39:菜单控制的使用h0=figure('toolbar','none',...'position',[200 150 450 250],...'name','实例39');x=0::2*pi;y=cos(x);h=plot(x,y);grid onset(gcf,'toolbar','none')hm=uimenu('label','example');huidiao1=[...'set(hm_gridon,''checked'',''on''),',... 'set(hm_gridoff,''checked'',''off''),',... 'grid on'];huidiao2=[...'set(hm_gridoff,''checked'',''on''),',... 'set(hm_gridon,''checked'',''off''),',... 'grid off'];hm_gridon=uimenu(hm,'label','grid on',...'checked','on',...'callback',huidiao1);hm_gridoff=uimenu(hm,'label','grid off',...'checked','off',...'callback',huidiao2);实例40:UIMENU菜单的应用。

相关文档
最新文档