MATLAB 程序范例
matlab_简明实例教程
matlab_简明实例教程MATLAB是一种强大的科学计算工具,广泛应用于科学研究、数据分析和工程计算等领域。
它具有简单易用的语法和丰富的函数库,可以快速实现复杂的计算任务。
本教程将为你提供一些简单实例,帮助你入门MATLAB。
1.计算圆的面积和周长```matlabradius = input('请输入圆的半径:');area = pi * radius^2;circumference = 2 * pi * radius;disp(['圆的面积为:', num2str(area)]);disp(['圆的周长为:', num2str(circumference)]);```2.计算两个向量的点积```matlabv1 = input('请输入向量1(用逗号分隔元素):');v2 = input('请输入向量2(用逗号分隔元素):');dot_product = dot(v1, v2);disp(['两个向量的点积为:', num2str(dot_product)]);```3.绘制正弦曲线```matlabx = 0:0.1:2*pi;y = sin(x);plot(x, y);xlabel('x');ylabel('sin(x)');title('正弦曲线');```4.求解方程```matlabsyms x;eqn = x^2 - 4 == 0;sol = solve(eqn, x);disp(['方程的解为:', char(sol)]); ```5.读取和写入文件```matlabfilename = 'data.txt';data = importdata(filename);disp('文件中的数据:');disp(data);output = [1 2 3; 4 5 6; 7 8 9];dlmwrite('result.txt', output, 'delimiter', '\t', 'precision', 4);disp('结果已保存到result.txt文件中。
数值分析matlab程序实例
1,秦九韶算法,求出P(x=3)=2+4x+5x^2+2x^3的值clear all;x=3;n=3;a(1)=2;a(2)=4;a(3)=5;a(4)=2v(1)=a(n+1);for k=2:(n+1);v(k)=x*v(k-1)+a(n-k+2);endp=v(n+1)p=,1132,一次线型插值程序:利用100.121.求115的开方。
clear all;x1=100;x2=121;y1=10;y2=11;x=115;l1=(x-x2)/(x1-x2);l2=(x-x1)/(x2-x1);p1=l1*y1+l2*y2p1=10.71433,分段插值程序,已知为S1(x)为(0,0),(1,1),(2,5)(3,8)上的分段一次插值,求S1(1.5).clear allx=[0123];y=[0158];n=length(x);a=1.5;for i=2:nif(x(i-1)<=a<x(i));endendH1=y(i-1)+(y(i)-y(i-1))/(x(i)-x(i-1))*(a-x(i-1))H1=3.50004)曲线拟合:用一个5次多项式在区间[0,2π]内逼近函数sin(x)。
clear allX=linspace(0,2*pi,50);Y=sin(X);[P,S]=polyfit(X,Y,5)plot(X,Y,'k*',X,polyval(P,X),'k-')P=-0.00560.0874-0.39460.26850.87970.0102S=R:[6x6double]df:44normr:0.03375)求有理分式的导数clear allP=[3,5,0,-8,1,-5];Q=[10,5,0,0,6,0,0,7,-1,0,-100];[p,q]=polyder(P,Q)6)将以下数据按从小到大排序:4.3 5.7 5.2 1.89.4a=[4.35.75.21.89.4];b(1:100)=0;n=1;b(a*10)=1;for k=1:100a(n)=k/10;if b(k)>0a(n)=k/10;n=n+1;endendaa=1.8000 4.3000 5.2000 5.70009.400010.00007)用二分法求方程x 3-x-1=0在[1,2]内的近似根,要求误差不超过10-3。
matlab典型例子(吐血整理)
一、在同一坐标系内,分别用红色实线和蓝色点划线绘制曲线y1(t)=e0.1sin(0.3πt)和y2(t)=2e -0.5cos(0.6πt),其中t ∈[0, 3],并用星号标记出两条曲线的交叉点。
请写出实现上述功能的M 文件程序代码,并给出每条语句注释说明。
t=0:0.01:3%t 取值[0,3]步长为0.01y1=exp(0.1)*sin(0.3*pi*t)%写出公式y1(t)=e0.1sin(0.3πt) plot(t,y1,'r-')%显示该图形 hold on%继续显示y2=2*exp(-0.5)*cos(0.6*pi*t)%写出公式y2(t)=2e -0.5cos(0.6πt) plot(t,y2,'b-.') %显示该图形r0=abs(y2-exp(0.1)*sin(0.3*pi*t))<=0.015%设定差值的绝对值<=0.015 yy=r0.*y1;xx=r0.*t;plot(xx(r0~=0),yy(r0~=0),'r*')%用*标记交叉点 结果如下:-1.5-1-0.50.511.5二、求解多项式3x4-8x2+x -10除以多项式x2+x+3;计算方程3x4-8x2+x -10=0的解。
请编写实现上述运算的M 程序代码,并给出每条语句注释说明。
a=[3 0 -8 1 -10]%3x4-8x2+x -10的系数矩阵b=[1 1 3]% x2+x+3的系数矩阵[q,r]=deconv(a,b)%求根和余。
q 为根,r 为余 结果如下: q =3 -3 -14 r =0 0 0 24 32a=[3 0 -8 1 -10]%多项式3x4-8x2+x-10=0的矩阵 b=roots(a)%求根 结果如下:b =-1.9319 1.8581 0.0369 + 0.9629i 0.0369 - 0.9629i三、编写M 文件,计算下题:取自变量x 的区间为[-6,6],数据间隔为0.01,有函数y=sin(x)+0.5, z=f(x,y)=3sin(x)-y/10,绘制三维曲线图z 。
matlab简单实例
matlab简单实例
当谈到MATLAB的简单实例时,有很多可以选择的示例。
以下是一个关于计算圆的面积和周长的简单实例:
matlab.
% 输入圆的半径。
radius = input('请输入圆的半径,');
% 计算圆的面积。
area = pi radius^2;
% 计算圆的周长。
circumference = 2 pi radius;
% 显示结果。
fprintf('圆的面积为,%.2f\n', area);
fprintf('圆的周长为,%.2f\n', circumference);
在这个实例中,我们首先通过`input`函数从用户获取圆的半径。
然后,我们使用半径计算圆的面积和周长,分别存储在`area`和
`circumference`变量中。
最后,我们使用`fprintf`函数将结果显
示出来。
这个实例展示了MATLAB的基本语法和数学计算功能。
你可以根
据需要修改和扩展这个实例,以适应更复杂的计算或问题。
除了计算圆的面积和周长,MATLAB还可以用于数据分析、图像
处理、信号处理、控制系统设计等各种应用领域。
你可以根据自己
的兴趣和需求,选择适合的实例进行学习和实践。
希望这个简单的实例能够帮助你了解MATLAB的基本用法和潜力。
如果你有其他关于MATLAB的问题,请随时提问。
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
'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],...
Matlab100个实例程序-matlab简单程序实例
程序代码:(代码标记[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实用程序实例
实例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个实例程序
程序代码:(代码标记[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程序算例
matlab程序算例Matlab程序算例Matlab是一种广泛应用于科学和工程领域的高级计算机编程语言及环境。
它的简洁、高效和强大的功能使得许多人选择使用Matlab来解决复杂的数学和工程问题。
在本文中,我将以一个具体的Matlab程序算例为例,详细说明每一步是如何完成的。
那么我们首先来看一下这个具体的Matlab程序算例。
假设我们希望计算并绘制一个二维正弦函数,代码如下:matlab设置步长,定义x轴的范围dx = 0.1;x = 0:dx:10;计算对应的y值y = sin(x);绘制图像plot(x, y);在这个例子中,我们通过定义一个步长`dx`和一个x轴的范围`x`来生成一系列的x值。
然后,我们使用`sin()`函数计算对应的y值,并将结果保存在`y`中。
最后,我们使用`plot()`函数绘制x和y的图像。
现在,让我们一步一步来回答这个程序算例中的问题。
第一步:设置步长和定义x轴的范围。
matlabdx = 0.1;x = 0:dx:10;这里我们设置步长`dx`为0.1,表示x轴上两个相邻点之间的间距为0.1。
然后,我们使用冒号运算符`:`创建一个从0到10的向量`x`,其中每个元素之间的间隔为`dx`。
也就是说,`x`中的元素为0, 0.1, 0.2, …, 9.9, 10。
第二步:计算对应的y值。
matlaby = sin(x);这里,我们使用`sin()`函数计算每个x值对应的正弦值,并将结果保存在`y`中。
例如,如果x的第一个元素为0,则使用`sin(0)`计算得到y的第一个元素的值。
第三步:绘制图像。
matlabplot(x, y);最后,我们使用`plot()`函数将x和y的值绘制成图像。
这样就可以观察到x和y之间的关系。
在这个例子中,由于x的范围是从0到10,并且y是对应的正弦值,因此我们将得到一个周期为2π的正弦函数的图像。
以上就是这个Matlab程序算例的每一步的解释。
matlab十个简单案例编写
matlab十个简单案例编写1. 求解线性方程组线性方程组是数学中常见的问题之一,而MATLAB提供了用于求解线性方程组的函数。
例如,我们可以使用"linsolve"函数来求解以下线性方程组:2x + 3y = 74x - 2y = 2代码如下所示:A = [2, 3; 4, -2];B = [7; 2];X = linsolve(A, B);disp(X);解释:上述代码定义了一个2x2的矩阵A和一个2x1的矩阵B,分别表示线性方程组的系数矩阵和常数向量。
然后,使用linsolve函数求解线性方程组,结果存储在X中,并通过disp函数打印出来。
运行代码后,可以得到x=2和y=1的解。
2. 求解非线性方程除了线性方程组外,MATLAB还可以用于求解非线性方程。
例如,我们可以使用"fzero"函数求解以下非线性方程:x^2 + 2x - 3 = 0代码如下所示:fun = @(x) x^2 + 2*x - 3;x0 = 0;x = fzero(fun, x0);disp(x);解释:上述代码定义了一个匿名函数fun,表示非线性方程。
然后,使用fzero函数传入fun和初始值x0来求解非线性方程的根,并通过disp函数打印出来。
运行代码后,可以得到x=1的解。
3. 绘制函数图像MATLAB提供了强大的绘图功能,可以帮助我们可视化函数的形状和特征。
例如,我们可以使用"plot"函数绘制以下函数的图像:y = cos(x)代码如下所示:x = linspace(0, 2*pi, 100);y = cos(x);plot(x, y);解释:上述代码首先使用linspace函数生成一个从0到2π的100个等间距点的向量x,然后计算对应的cos值,并存储在向量y中。
最后,使用plot函数将x和y作为横纵坐标绘制出函数图像。
运行代码后,可以看到cos函数的周期性波动图像。
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 程序例子
例5-1 在0≤x≤2π区间内,绘制曲线 y=2e -0.5x cos(4πx) 程序如下: x=0:pi/100:2*pi;y=2*exp(-0.5*x).*cos(4*pi*x); plot(x,y)例5-2 绘制曲线 。
程序如下: t=0:0.1:2*pi; x=t.*sin(3*t); y=t.*sin(t).*sin(t); plot(x,y);例5-3 分析下列程序绘制的曲线。
x1=linspace(0,2*pi,100); x2=linspace(0,3*pi,100); x3=linspace(0,4*pi,100); y1=sin(x1); y2=1+sin(x2); y3=2+sin(x3); x=[x1;x2;x3]'; y=[y1;y2;y3]'; plot(x,y,x1,y1-1)例5-4 用不同标度在同一坐标内绘制曲线y1=0.2e -0.5x cos(4πx) 和y2=2e -0.5x cos(πx)。
程序如下: x=0:pi/100:2*pi;y1=0.2*exp(-0.5*x).*cos(4*pi*x); y2=2*exp(-0.5*x).*cos(pi*x); plotyy(x,y1,x,y2);例5-5 采用图形保持,在同一坐标内绘制曲线y1=0.2e-0.5x cos(4πx) 和y2=2e -0.5x cos(πx)。
程序如下: x=0:pi/100:2*pi;y1=0.2*exp(-0.5*x).*cos(4*pi*x); plot(x,y1) hold ony2=2*exp(-0.5*x).*cos(pi*x); plot(x,y2); hold off例5-6 在同一坐标内,分别用不同线型和颜色绘制曲线y1=0.2e -0.5x cos(4πx) 和y2=2e -0.5x cos(πx),标记两曲线交叉点。
程序如下:x=linspace(0,2*pi,1000); y1=0.2*exp(-0.5*x).*cos(4*pi*x); y2=2*exp(-0.5*x).*cos(pi*x);k=find(abs(y1-y2)<1e-2); %查找y1与y2相等点(近似相等)的下标x1=x(k); %取y1与y2相等点的x 坐标y3=0.2*exp(-0.5*x1).*cos(4*pi*x1); %求y1与y2值相等点的y 坐标plot(x,y1,x,y2,'k:',x1,y3,'bp');例5-7 在0≤x≤2π区间内,绘制曲线y1=2e -0.5x 和y2=cos(4πx),并给图形添加图形标注。
Matlab一些典型程序的编写
1.离散时间系统:stem:画数字序列针状图。
参数:画线处坐标t;画线处数值x。
例:t=[0:0.1:2];x=cos(pi*t+0.6);stem(t,x);2.conv:实现两个序列的卷积。
参数:序列向量a,b。
输出参数:a,b 卷积的结果向量y。
例:a=[1 1 1 1 1];b=[1 2 3 4 5 6 7 8 9];c=conv(a,b);stem(c);3.设单位脉冲响应h(n)等于sin(0.5n),n≥0,输入x(n)等于sin(0.2n),n≥0。
计算当n=0,1,…,40时的响应y(n).(2-5)n=0:40;x = sin(.2*n);h = sin(.5*n);y = conv(x,h);stem(n,y(1:length(n)))4.有连续时间信号:)2/8cos()3/4cos(cos )(321ππ++++=t A t A t A t x 画出信号的波形。
3-1t = 0:20/400:20;w1 = 1; w2 = 4; w3 = 8;A1 = input('Input the amplitude A1 for w1 = 1: ');A2 = input('Input the amplitude A2 for w2 = 4: ');A3 = input('Input the amplitude A3 for w3 = 8: ');x = A1*cos(w1*t)+A2*cos(w2*t+pi/3)+A3*cos(w3*t+pi/2);5.画出矩形脉冲序列。
3-2t = -3:6/1000:3;N = input('Number of harmonics ');c0 = 0.5;w0 = pi;xN = c0*ones(1,length(t)); % dc componentfor k=1:2:N, % even harmonics are zerotheta = ((-1)^((k-1)/2) - 1)*pi/2;xN = xN + 2/k/pi*cos(k*w0*t + theta);endplot(t,xN)title(['Example 3.4, N = ',num2str(N)])xlabel('Time (sec)')ylabel(['x',num2str(N),'(t)'])6.画出信号)()(t e t x bt ε-=的幅频谱和相频谱 3.8w = 0:0.2:50;b = 10;X = (1)./(b+j*w);clfsubplot(211),plot(w,abs(X)); % plot magnitude of Xtitle('Example 3.8')xlabel('Frequency (rad/sec)')ylabel('|X|')subplot(212),plot(w,angle(X)*180/pi); % plot angle of X in deg.xlabel('Frequency (rad/sec)')ylabel('Angle(X), degrees')subplot(111)7.利用符号运算计算拉氏变换求的拉氏变换:)()(t e t x bt ε-=6.3 syms x b tx = exp(-b*t);X = laplace(x)%x = sym(1); % creates a symbolic expression for 1X = laplace(x)8.用residue 求拉氏反变换。
MATLAB程序设计范文精简版
MATLAB程序设计范文精简版MATLAB程序设计MATLAB程序设计简介基本语法MATLAB的基本语法与其他编程语言类似,包括变量定义、运算符、控制流程等。
以下是一些常用的基本语法:变量定义MATLAB中的变量不需要预先声明类型,直接使用即可。
变量名是大小写敏感的,并且不能使用保留字作为变量名。
matlabx = 5;y = 'Hello MATLAB!';z = [1 2 3 4 5];运算符与其他编程语言一样,MATLAB支持各种数学运算符和逻辑运算符,可以进行加减乘除、比较和逻辑操作等。
matlabMATLAB程序设计范文精简版a = 5 + 3;b = 7 2;c = (a > b) && (b < 10);控制流程MATLAB提供了各种控制流程语句,如条件语句、循环语句等,可以根据条件执行不同的操作。
matlabif x > 0disp('x is positive');elseif x < 0disp('x is negative');elsedisp('x is zero');endfor i = 1:5disp(i);endwhile x < 10x = x + 1;end函数定义和调用函数是MATLAB程序设计的重要组成部分,可以封装一些常用的操作和算法,并在需要时调用。
以下是函数的定义和调用示例:matlabfunction result = add(a, b)result = a + b;endx = 3;y = 4;z = add(x, y);disp(z);数据处理和可视化MATLAB提供了丰富的数据处理和可视化工具,可以帮助用户对数据进行分析和展示。
以下是一些常用的数据处理和可视化操作示例:加载和保存数据matlabdata = load('data.txt');save('result.mat', 'data');统计分析MATLAB提供了丰富的统计函数,可以进行各种统计分析操作,如求平均值、标准差、相关系数等。
Matlab程序66例(测试成功)
Matlab程序1、function B=bsjz(A)%计算并输出方阵A的伴随矩阵%10C 104100176 黄金炳[r,c]=size(A); %获取矩阵A的维度if r~=c %判断输入的不是方阵时,输出提示disp('输入的不是方阵');elseif r==1 %当输入的是行列式的值不为0的一阶方阵时,%方阵A的伴随矩阵就是它本身B=A;else%当输入的是行列式值不为0的一阶以上的方阵时for k=1:r %扫描,计算A(k,l)处的余子式并赋给B(l,k)for l=1:cC=A;C(k,:)=[];C(:,l)=[];B(l,k)=(-1)^(k+l)*det(C);endendend2、function string=czzb(A)%输入矩阵,查找矩阵中存在子矩阵[2 2;2 2]的位置并输出%10C[r,c]=size(A); %获取矩阵的维度if r>1 %判断输入的矩阵是否符合要求if c>1string=('该矩阵中存在子矩阵[2 2;2 2]的位置为');%初始化字符串变量stringfor a=1:r-1 %循环for b=1:c-1if A(a,b)==2 %判断是否存在子矩阵[2 2;2 2]if A(a+1,b)==2if A(a,b+1)==2if A(a+1,b+1)==2string=[string,' ',num2str(a),'行',...num2str(b),'列'];%存在子矩阵[2 2;2 2]时,把它此时的位置赋给stringendendendendendend3、function[B]=dxpx(A)%对输入的矩阵进行从小到大排序并输出%10C 104100176 黄金炳[m,n]=size(A); %获取矩阵的大小B=A; %把矩阵A的元素赋给二维矩阵Bfor e=1:m %对矩阵中的元素进行大小比较、排序for f=1:nfor c=1:mfor d=1:nif B(e,f)<B(c,d)x=B(e,f);B(e,f)=B(c,d);B(c,d)=x;endendendendend4、function edde(A,delt)%提取图像的水平边缘%10C 104100176 黄金炳figure(1):imshow(A); %在一个窗体中显示A[m,n]=size(A); %获取A的维度B(1:m,1:n)=uint8(255); %初始化Bfor k=1:m-1 %获取边缘的点并赋给Bfor p=1:nif abs(A(k+1,p)-A(k,p))>deltB(k,p)=uint8(0);endendendfigure(2):imshow(B); %在另一个窗体中显示B5、function B=fdet(A)%计算矩阵A的行列式%10C 104100176 黄金炳[r,c]=size(A); %获取矩阵A的维度B=0; %初始化B为0if r~=c %判断输入的不是方阵时,输出提示disp('输入的不是方阵');elseif r==1 %当输入的是一阶方阵时,方阵A的行列式的值是它本身 B=A;else%当输入的是一阶以上的方阵时for k=1:r %计算行列式的值C=A;C(1,:)=[];C(:,k)=[];B=B+(-1)^(1+k)*A(1,k)*fdet(C);endend6、function string=fdjx(A)%获取矩阵不为零的元素并输出它们的位置%10C 104100176 黄金炳[r,c]=size(A); %获取矩阵的维度string=('矩阵不为零的元素及其位置:'); %初始化字符串变量stringfor a=1:r %循环找出矩阵A中不为零的元素并把它以及它的位置赋到string中for b=1:cd=A(a,b);if d~=0string=[string,' 元素a(',num2str(a),',',...num2str(b),')=',num2str(d),'的位置:',...num2str(a),'行',num2str(b),'列 '];endendend7、function fint(a)%判断输入的数值是正整数、负整数、零、正小数或负小数%10C 104100176 黄金炳if a==0 %判断a是不是零disp('零');elseif a==fix(a) %判断a是不是整数if a==abs(a) %判断a的正负disp('正整数');elsedisp('负整数');endelseif a==abs(a) %判断a的正负disp('正小数');elsedisp('负小数');endendend8、function B=frem(A,n)%矩阵A每个元素对n取余并保存到B中输出%10C 104100176 黄金炳[r,c]=size(A); %获取矩阵A的维度if n==abs(fix(n)) %判断输入的n是否为正整数for d=1:r %对每个元素进行操作for f=1:cx=A(d,f); %对元素取余并保存到矩阵B中while x-n>=0x=x-n;endB(d,f)=x;endendelsedisp('frem(A,n): n输入不正确:n必须为正整数');end9、function [B]=ftril(A,n)%取矩阵正对角线以上n行的对角线以下的元素构成下三角矩阵%10C 104100176 黄金炳[a,b]=size(A); %获取矩阵A的维度if nargin==1 %当输入的参数个数为一个时,设置n为0 n=0;endif n==fix(n) %判断n是否符合要求if n>(b-1)disp('ftril(A,n): n输入的不正确:超出范围');elseif n<(1-a)disp('ftril(A,n): n输入的不正确:超出范围');elseB=A;for c=1:a %开始逐个把除构成下三角矩阵的其他元素赋值为0 for d=1:bif c<(d-n)B(c,d)=0;endendendendelsedisp('ftril(A,n): n输入的不正确:n必须为整数');end10、function [B]=ftriu(A,n)%取矩阵正对角线以上n行的对角线以下的元素构成上三角矩阵%10C 104100176 黄金炳[a,b]=size(A); %获取矩阵A的维度if nargin==1 %当输入的参数个数为一个时,设置n为0n=0;endif n==fix(n) %判断n是否符合要求if n>(b-1)disp('ftriu(A,n): n输入的不正确:超出范围');elseif n<(1-a)disp('ftriu(A,n): n输入的不正确:超出范围');elseB=A;for c=1:a %开始逐个把除构成上三角矩阵的其他元素赋值为0 for d=1:bif c>(d-n)B(c,d)=0;endendendendelsedisp('ftril(A,n): n输入的不正确:n必须为整数');end11、function string=jtwt(ts,js) %ts为鸡兔总头数,js为鸡兔总脚数%输入鸡兔的总头数和总脚数,分别输出鸡兔的只数。
Matlab8个例子
Matlab8个例子1、囧function happynewyearaxis off;set(gcf,'menubar','none','toolbar','none');for k=1:20h=text(rand,rand,...['\fontsize{',num2str(unifrnd(20,50)),'}\fontname {隶书} 新年快乐'],...'color',rand(1,3),'Rotation',360 * rand);pause(0.5)End2、小猫进洞function t=cat_in_holl(n)t=zeros(1,n);for k=1:nc=unifdnd(3,1);while c~=1if c==2t(k)=t(k)+4;elset(k)=t(k)+6;endc=unifdnd(3,1);endt(k)=t(k)+2;End3、Slowfunction example2_3_6stic;A=unidrnd(100,10,7);B=zeros(10,3);for m=1:10a=A(m,:);b=[4,6,8];for ii=1:3dd=a(a==b(ii));if isempty(dd)==0b(ii)=0;endendB(m,:)=b;tocendA,BFastfunction example2_3_6fast2clearA = unidrnd(100,1000000,7);B = repmat([4,6,8],1000000,1);tic;C = [any(AA == 4,2) any(AA == 6,2) any(AA == 8,2)];B(C) = 0;Toc4、随机行走法function [mx,minf]=randwalk(f,x,lamda,epsilon,N)%随机行走法求函数的极小值。
Matlab操作实例-1
Matlab基础知识实例常用命令及操作的熟悉1 最简单的计算器使用法【例1】求23)]47(212[÷-⨯+的算术运算结果。
(1)用键盘在MATLAB 指令窗中输入以下内容 >> (12+2*(7-4))/3^2(2)在上述表达式输入完成后,按【Enter 】键,该就指令被执行。
(3)在指令执行后,MATLAB 指令窗中将显示以下结果。
ans = 2【例2】简单矩阵⎥⎥⎥⎦⎤⎢⎢⎢⎣⎡=987654321A 的输入步骤。
(1)在键盘上输入下列内容A = [1,2,3; 4,5,6; 7,8,9](2)按【Enter 】键,指令被执行。
(3)在指令执行后,MATLAB 指令窗中将显示以下结果:A =1 2 34 5 67 8 9【例3】矩阵的分行输入A=[1,2,34,5,67,8,9](以下是显示结果)A =1 2 34 5 67 8 9【例4】指令的续行输入(以下格式在除Notebook外的MATLAB环境中可运行)S = 1 – 1/2 + 1/3 –1/4 + 1/5 – 1/6 ...+ 1/7 – 1/8S =0.63452数值、变量和表达式2.1复数和复数矩阵【例5】复数ieziziz63212,21,43π=+=+=表达,及计算321zzzz=。
(1)经典教科书的直角坐标表示法z1= 3 + 4iz1 =3.0000 +4.0000i(2)采用运算符构成的直角坐标表示法和极坐标表示法z2 = 1 + 2 * i %运算符构成的直角坐标表示法z3=2*exp(i*pi/6) %运算符构成的极坐标表示法z=z1*z2/z3z2 =1.0000 +2.0000iz3 =1.7321 + 1.0000iz =0.3349 + 5.5801i【例6】复数矩阵的生成及运算A=[1,3;2,4]-[5,8;6,9]*iB=[1+5i,2+6i;3+8*i,4+9*i]C=A*BA =1.0000 - 5.0000i 3.0000 - 8.0000i2.0000 - 6.0000i 4.0000 - 9.0000i B =1.0000 + 5.0000i2.0000 + 6.0000i3.0000 + 8.0000i4.0000 + 9.0000i C =1.0e+002 *0.9900 1.1600 - 0.0900i 1.1600 + 0.0900i 1.3700【例7】求上例复数矩阵C 的实部、虚部、模和相角。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
第一题x=-3:0.1:3; %x取-3到3y=1:0.1:5; %y取1到5[x,y]=meshgrid(x,y); %创建网格数据a=3.*(1-x).*exp(-x.^2-(y+1).^2);b=10.*(x./5-x.^3-y.^5).*exp(-x.^2-y.^2);c=(1./3).*exp(-(x+1).^2-y.^2);z=a-b-c; %输入函数式subplot(2,2,1);mesh(x,y,z); %画出立体网状图hidden off %显示被遮盖的网线axis tighttitle('图形的隐含部分表现出来')subplot(2,2,2)mesh(x,y,z)shading flattitle('带阴影的三维图形')z=peaks(x,y); %计算函数值subplot(2,2,3);meshc(x,y,z); %绘制带等高线的三维图形title('带等高线的三维图形')subplot(2,2,4)contour3(x,y,z,20); %绘制三维图形的等高线xlabel('X-axis'),ylabel('Y-axis'),zlabel('Z-axis')title(‘三维图形的等高线’)sum=0;for i=0:63,part=1;for j=1:i,part=2.^j;endfprintf('part(%d)=%d.\n',i,part); sum=sum+part;endsumsum=0;i=0;k=0;while i<63i=i+1;k=2.^i;sum=sum+kend>> part(0)=1.part(1)=2.part(2)=4.part(3)=8.part(4)=16.part(5)=32.part(6)=64.part(7)=128.part(8)=256.part(9)=512.part(10)=1024.part(11)=2048.part(12)=4096.part(13)=8192.part(14)=16384.part(15)=32768.part(16)=65536.part(17)=131072.part(18)=262144.part(19)=524288.part(20)=1048576.part(21)=2097152.part(22)=4194304.part(23)=8388608.part(24)=16777216.part(25)=33554432.part(26)=67108864.part(27)=134217728.part(28)=268435456.part(29)=536870912.part(30)=1073741824.part(34)=1.717987e+010. part(35)=3.435974e+010. part(36)=6.871948e+010. part(37)=1.374390e+011. part(38)=2.748779e+011. part(39)=5.497558e+011. part(40)=1.099512e+012. part(41)=2.199023e+012. part(42)=4.398047e+012. part(43)=8.796093e+012. part(44)=1.759219e+013. part(45)=3.518437e+013. part(46)=7.036874e+013. part(47)=1.407375e+014. part(48)=2.814750e+014. part(49)=5.629500e+014. part(50)=1.125900e+015. part(51)=2.251800e+015. part(52)=4.503600e+015. part(53)=9.007199e+015. part(54)=1.801440e+016. part(55)=3.602880e+016. part(56)=7.205759e+016. part(57)=1.441152e+017. part(58)=2.882304e+017. part(59)=5.764608e+017. part(60)=1.152922e+018. part(61)=2.305843e+018. part(62)=4.611686e+018. part(63)=9.223372e+018. sum =1.8447e+019sum =2sum =6sum =14sum =30sum =62sum =126sum =254sum =5102046 sum =4094 sum =8190 sum =16382 sum =32766 sum =65534 sum =131070 sum =262142 sum =524286 sum =1048574 sum =2097150 sum =4194302 sum =8388606 sum =16777214 sum =33554430 sum =67108862 sum =134217726 sum =268435454 sum =536870910 sum =1.0737e+009 sum =2.1475e+009 sum =4.2950e+009 sum =8.5899e+009 sum =1.7180e+010 sum =3.4360e+0105.4976e+011 sum =1.0995e+012 sum =2.1990e+012 sum =4.3980e+012 sum =8.7961e+012 sum =1.7592e+013 sum =3.5184e+013 sum =7.0369e+013 sum =1.4074e+014 sum =2.8147e+014 sum =5.6295e+014 sum =1.1259e+015 sum =2.2518e+015 sum =4.5036e+015 sum =9.0072e+015 sum =1.8014e+016 sum =3.6029e+016 sum =7.2058e+016 sum =1.4412e+017 sum =2.8823e+017 sum =5.7646e+017 sum =1.1529e+018 sum =2.3058e+018A=[4,12,20;12,45,78;20,78,136] %输入矩阵A入B=[1,2,3;4,5,6;7,8,9] %输入矩阵BI=eye(3) %输入单位矩阵IA+5*BA-B*IA.*B %矩阵A、B内元素对元素的相乘运算A*B %进行矩阵相乘运算A.^B %矩阵A、B中元素对元素进行指数运算A/B %矩阵A右除矩阵BA\B %矩阵A左除矩阵BA =4 12 2012 45 7820 78 136B =1 2 34 5 67 8 9I =1 0 00 1 00 0 1ans =9 22 3532 70 10855 118 181ans =3 10 178 40 7213 70 127ans =4 24 6048 225 468140 624 1224ans =192 228 264738 873 10081284 1518 1752ans =1.0e+019 *0.0000 0.0000 0.00000.0000 0.0000 0.00000.0000 0.0001 1.5917Warning: Matrix is singular to working precision.> In C:\Documents and Settings\Administrator\桌面\c.m at line 9Inf Inf InfWarning: Matrix is singular to working precision.> In C:\Documents and Settings\Administrator\桌面\c.m at line 10ans =Inf Inf InfInf Inf InfInf Inf Inf第四题t=-1:0.025:1 %对t的取值,步长为0.025x=sin(t.^-1) %输入函数plot(x,'-m')t =Columns 1 through 15-1.0000 -0.9750 -0.9500 -0.9250 -0.9000 -0.8750 -0.8500 -0.8250 -0.8000 -0.7750 -0.7500 -0.7250 -0.7000 -0.6750 -0.6500Columns 16 through 30-0.6250 -0.6000 -0.5750 -0.5500 -0.5250 -0.5000 -0.4750 -0.4500 -0.4250 -0.4000 -0.3750 -0.3500 -0.3250 -0.3000 -0.2750Columns 31 through 45-0.2500 -0.2250 -0.2000 -0.1750 -0.1500 -0.1250 -0.1000 -0.0750 -0.0500 -0.0250 0 0.0250 0.0500 0.0750 0.1000Columns 46 through 600.1250 0.1500 0.1750 0.2000 0.2250 0.2500 0.2750 0.3000 0.3250 0.3500 0.3750 0.4000 0.4250 0.4500 0.4750Columns 61 through 750.5000 0.5250 0.5500 0.5750 0.6000 0.6250 0.6500 0.6750 0.7000 0.7250 0.7500 0.7750 0.8000 0.8250 0.8500Columns 76 through 810.8750 0.9000 0.9250 0.9500 0.9750 1.0000x =Columns 1 through 15-0.8415 -0.8550 -0.8687 -0.8825 -0.8962 -0.9098 -0.9233 -0.9364 -0.9490 -0.9609 -0.9719 -0.9817 -0.9899 -0.9960 -0.9995Columns 16 through 30-0.9996 -0.9954 -0.9859 -0.9696 -0.9447 -0.9093 -0.8605 -0.7952 -0.7094 -0.5985 -0.4573 -0.2806 -0.0646 0.1906 0.4748Columns 31 through 450.7568 0.9643 0.9589 0.5387 -0.3742 -0.9894 0.5440 -0.6940 -0.9129 -0.7451 NaN 0.7451 0.9129 0.6940 -0.5440Columns 46 through 600.9894 0.3742 -0.5387 -0.9589 -0.9643 -0.7568 -0.4748 -0.1906 0.0646 0.2806 0.4573 0.5985 0.7094 0.7952 0.8605Columns 61 through 750.9093 0.9447 0.9696 0.9859 0.9954 0.9996 0.9995 0.9960 0.9899 0.9817 0.9719 0.9609 0.9490 0.9364 0.9233Columns 76 through 810.9098 0.8962 0.8825 0.8687 0.8550 0.8415t=-pi:pi/100:pi %给出t的取值范围,步长为pi/100t =-2.6704 -2.6389 -2.6075 -2.5761 -2.5447 -2.5133 -2.4819 -2.4504 -2.4190 -2.3876 -2.3562 -2.3248 -2.2934 -2.2619 -2.2305Columns 31 through 45-2.1991 -2.1677 -2.1363 -2.1049 -2.0735 -2.0420 -2.0106 -1.9792 -1.9478 -1.9164 -1.8850 -1.8535 -1.8221 -1.7907 -1.7593Columns 46 through 60-1.7279 -1.6965 -1.6650 -1.6336 -1.6022 -1.5708 -1.5394 -1.5080 -1.4765 -1.4451 -1.4137 -1.3823 -1.3509 -1.3195 -1.2881Columns 61 through 75-1.2566 -1.2252 -1.1938 -1.1624 -1.1310 -1.0996 -1.0681 -1.0367 -1.0053 -0.9739 -0.9425 -0.9111 -0.8796 -0.8482 -0.8168Columns 76 through 90-0.7854 -0.7540 -0.7226 -0.6912 -0.6597 -0.6283 -0.5969 -0.5655 -0.5341 -0.5027 -0.4712 -0.4398 -0.4084 -0.3770 -0.3456Columns 91 through 105-0.3142 -0.2827 -0.2513 -0.2199 -0.1885 -0.1571 -0.1257 -0.0942 -0.0628 -0.0314 0 0.0314 0.0628 0.0942 0.1257Columns 106 through 1200.1571 0.1885 0.2199 0.2513 0.2827 0.3142 0.3456 0.3770 0.4084 0.4398 0.4712 0.5027 0.5341 0.5655 0.5969Columns 121 through 1350.6283 0.6597 0.6912 0.7226 0.7540 0.7854 0.8168 0.8482 0.8796 0.9111 0.9425 0.9739 1.0053 1.0367 1.0681Columns 136 through 1501.0996 1.1310 1.1624 1.1938 1.2252 1.2566 1.2881 1.3195 1.3509 1.3823 1.4137 1.4451 1.4765 1.5080 1.5394Columns 151 through 1651.5708 1.6022 1.6336 1.6650 1.6965 1.7279 1.7593 1.7907 1.8221 1.8535 1.8850 1.9164 1.9478 1.97922.0106Columns 166 through 1802.0420 2.0735 2.1049 2.1363 2.1677 2.1991 2.2305 2.2619 2.2934 2.3248 2.3562 2.3876 2.4190 2.4504 2.4819Columns 181 through 1952.5133 2.5447 2.5761 2.6075 2.6389 2.6704 2.7018 2.7332 2.7646 2.7960 2.8274 2.8588 2.8903 2.9217 2.9531Columns 196 through 2012.98453.0159 3.0473 3.0788 3.1102 3.1416>>(2)x=sin(tan(t))-tan(sin(t)) %输入函数x =Columns 1 through 150.0000 0.0628 0.1257 0.1888 0.2520 0.3154 0.3792 0.4433 0.5079 0.5729 0.6385 0.7046 0.7714 0.8388 0.9069Columns 16 through 300.9757 1.0453 1.1156 1.1866 1.2583 1.3306 1.4035 1.47661.5500 1.6232 1.6960 1.7678 1.8380 1.9059 1.9702Columns 31 through 452.0296 2.0821 2.1254 2.1561 2.1700 2.1614 2.1230 2.0456 1.9179 1.7278 1.4654 1.1333 0.7708 0.5062 0.63541.3376 1.0158 0.7626 0.5687 0.4225 0.3129 0.2312 0.1705 0.1254 0.0920 0.0672 0.0489 0.0355 0.0256 0.0183Columns 76 through 900.0130 0.0092 0.0064 0.0045 0.0031 0.0021 0.0014 0.0009 0.0006 0.0004 0.0002 0.0001 0.0001 0.0000 0.0000Columns 91 through 1050.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0 -0.0000 -0.0000 -0.0000 -0.0000Columns 106 through 120-0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0001 -0.0001 -0.0002 -0.0004 -0.0006 -0.0009 -0.0014Columns 121 through 135-0.0021 -0.0031 -0.0045 -0.0064 -0.0092 -0.0130 -0.0183 -0.0256 -0.0355 -0.0489 -0.0672 -0.0920 -0.1254 -0.1705 -0.2312Columns 136 through 150-0.3129 -0.4225 -0.5687 -0.7626 -1.0158 -1.3376 -1.7252 -2.1387 -2.4495 -2.3613 -1.4855 -0.5326 -2.4567 -1.7362 -1.1621Columns 151 through 165-1.1510 -1.9494 -1.3652 -0.6279 -2.5288 -1.5466 -0.6354 -0.5062 -0.7708 -1.1333 -1.4654 -1.7278 -1.9179 -2.0456 -2.1230Columns 166 through 180-2.1614 -2.1700 -2.1561 -2.1254 -2.0821 -2.0296 -1.9702 -1.9059 -1.8380 -1.7678 -1.6960 -1.6232 -1.5500 -1.4766 -1.4035Columns 181 through 195-1.3306 -1.2583 -1.1866 -1.1156 -1.0453 -0.9757 -0.9069 -0.8388 -0.7714 -0.7046 -0.6385 -0.5729 -0.5079 -0.4433 -0.3792Columns 196 through 201-0.3154 -0.2520 -0.1888 -0.1257 -0.0628 -0.0000>> plot(x,'-y') %用黄色的细实线画出图形>>t =Columns 1 through 15-3.1416 -3.1102 -3.0788 -3.0473 -3.0159 -2.9845 -2.9531 -2.9217 -2.8903 -2.8588 -2.8274 -2.7960 -2.7646 -2.7332 -2.7018Columns 16 through 30-2.6704 -2.6389 -2.6075 -2.5761 -2.5447 -2.5133 -2.4819 -2.4504 -2.4190 -2.3876 -2.3562 -2.3248 -2.2934 -2.2619 -2.2305Columns 31 through 45-2.1991 -2.1677 -2.1363 -2.1049 -2.0735 -2.0420 -2.0106 -1.9792 -1.9478 -1.9164 -1.8850 -1.8535 -1.8221 -1.7907 -1.7593Columns 46 through 60-1.7279 -1.6965 -1.6650 -1.6336 -1.6022 -1.5708 -1.5394 -1.5080 -1.4765 -1.4451 -1.4137 -1.3823 -1.3509 -1.3195 -1.2881Columns 61 through 75-1.2566 -1.2252 -1.1938 -1.1624 -1.1310 -1.0996 -1.0681 -1.0367 -1.0053 -0.9739 -0.9425 -0.9111 -0.8796 -0.8482 -0.8168Columns 76 through 90-0.7854 -0.7540 -0.7226 -0.6912 -0.6597 -0.6283 -0.5969 -0.5655-0.0628 -0.0314 0 0.0314 0.0628 0.0942 0.1257Columns 106 through 1200.1571 0.1885 0.2199 0.2513 0.2827 0.3142 0.3456 0.3770 0.4084 0.4398 0.4712 0.5027 0.5341 0.5655 0.5969Columns 121 through 1350.6283 0.6597 0.6912 0.7226 0.7540 0.7854 0.8168 0.8482 0.8796 0.9111 0.9425 0.9739 1.0053 1.0367 1.0681Columns 136 through 1501.0996 1.1310 1.1624 1.1938 1.2252 1.2566 1.2881 1.3195 1.3509 1.3823 1.4137 1.4451 1.4765 1.5080 1.5394Columns 151 through 1651.5708 1.6022 1.6336 1.6650 1.6965 1.7279 1.7593 1.7907 1.8221 1.8535 1.8850 1.9164 1.9478 1.97922.0106Columns 166 through 1802.0420 2.0735 2.1049 2.1363 2.1677 2.1991 2.2305 2.2619 2.2934 2.3248 2.3562 2.3876 2.4190 2.4504 2.4819Columns 181 through 1952.5133 2.5447 2.5761 2.6075 2.6389 2.6704 2.7018 2.7332 2.7646 2.7960 2.8274 2.8588 2.8903 2.9217 2.9531Columns 196 through 2012.98453.0159 3.0473 3.0788 3.1102 3.1416x =Columns 1 through 150.0000 0.0628 0.1257 0.1888 0.2520 0.3154 0.3792 0.4433 0.5079 0.5729 0.6385 0.7046 0.7714 0.8388 0.9069Columns 16 through 300.9757 1.0453 1.1156 1.1866 1.2583 1.3306 1.4035 1.47661.5500 1.6232 1.6960 1.7678 1.8380 1.9059 1.9702Columns 31 through 452.0296 2.0821 2.1254 2.1561 2.1700 2.1614 2.1230 2.0456 1.9179 1.7278 1.4654 1.1333 0.7708 0.5062 0.6354Columns 46 through 601.54662.5288 0.6279 1.3652 1.9494 1.1510 1.1621 1.73622.4567 0.5326 1.4855 2.3613 2.4495 2.1387 1.7252Columns 61 through 751.3376 1.0158 0.7626 0.5687 0.4225 0.3129 0.2312 0.1705 0.1254 0.0920 0.0672 0.0489 0.0355 0.0256 0.0183Columns 76 through 900.0130 0.0092 0.0064 0.0045 0.0031 0.0021 0.0014 0.0009 0.0006 0.0004 0.0002 0.0001 0.0001 0.0000 0.0000Columns 91 through 1050.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0 -0.0000 -0.0000 -0.0000 -0.0000Columns 106 through 120-0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0001 -0.0001 -0.0002 -0.0004 -0.0006 -0.0009 -0.0014Columns 121 through 135-0.0021 -0.0031 -0.0045 -0.0064 -0.0092 -0.0130 -0.0183 -0.0256 -0.0355 -0.0489 -0.0672 -0.0920 -0.1254 -0.1705 -0.2312Columns 151 through 165-1.1510 -1.9494 -1.3652 -0.6279 -2.5288 -1.5466 -0.6354 -0.5062 -0.7708 -1.1333 -1.4654 -1.7278 -1.9179 -2.0456 -2.1230Columns 166 through 180-2.1614 -2.1700 -2.1561 -2.1254 -2.0821 -2.0296 -1.9702 -1.9059 -1.8380 -1.7678 -1.6960 -1.6232 -1.5500 -1.4766 -1.4035Columns 181 through 195-1.3306 -1.2583 -1.1866 -1.1156 -1.0453 -0.9757 -0.9069 -0.8388 -0.7714 -0.7046 -0.6385 -0.5729 -0.5079 -0.4433 -0.3792Columns 196 through 201-0.3154 -0.2520 -0.1888 -0.1257 -0.0628 -0.0000第五题A=[7.5,3.5,0,0;8,22,4.1,0;0,9,103,-1.5;0,0,3.7,19.3]B=[5,7,6,5;7,10,8,7;6,8,10,9;5,7,9,10]DA=det(A) %求解矩阵A的值DB=det(B) %求解矩阵B的值tA=trace(A) %求解矩阵A的迹tB=trace(B) %求解矩阵B的迹rA=rank(A) %求解矩阵A的秩rB=rank(B) %求解矩阵B的秩pA=poly(A) %求解矩阵A的特征多项式pB=poly(B) %求解矩阵B的特征多项式A =7.5000 3.5000 0 08.0000 22.0000 4.1000 00 9.0000 103.0000 -1.50000 0 3.7000 19.3000B =5 76 57 10 8 76 8 10 95 7 9 10DA =2.6776e+005DB =1tA=151.8000tB =35rA =4rB =4pA =1.0e+005 *0.0000 -0.0015 0.0570 -0.7457 2.6776pB =1.0000 -35.0000 146.0000 -100.0000 1.0000第六题A=[7,2,1,-2;9,15,3,-2;-2,-2,11,5;1,3,2,13]A =7 2 1 -29 15 3 -2-2 -2 11 51 32 13>> b=[4,7,-1,0]'b =47-1>> x=A\bx =0.49790.14450.0629-0.0813>> ra=rank(A) %A的秩ra =4>> rab=rank([A,b]) %看b是否在A的列空间中rab =4xs=A\b; %求出特解xg=null(A); %求出奇次方程解c=rand(1); %随机数ba=A*(xs+c*xg); %计算A与“一个随机的全解”的乘积ba norm(ba-b) %检查ba与b的接近程度第七题syms x %生成符号变量f0=sym(sin(x)/x) %建立符号表达式int(f0,x,0,2) %计算表达式f0在变量x从0到2的定积分syms xa=1/((x-0.3).^2+0.01);b=1/((x-0.9).^2+0.04);f1=sym(a-b-6)int(f1,x,0,1) %计算表达式f1在变量x从0到1的定积分f2=sym(exp(x.^2))f3=sym(1/(4-sin(16*pi*x)))int(f2,x,0,2) %计算表达式f2从0到2的定积分int(f3,x,2,4) %计算表达式f3从2到4的定积分f=f2+f3f0 =sin(x)/xans =sinint(2)f1 =1/((x-3/10)^2+1/100)-1/((x-9/10)^2+1/25)-6ans =10*atan(7)-5*atan(1/2)-6+10*atan(3)-5*atan(9/2)f2 =exp(x^2)f3 =1/(4-sin(16*pi*x))ans =-1/2*i*erf(2*i)*pi^(1/2)ans =2/15*15^(1/2)f =exp(x^2)+1/(4-sin(16*pi*x))第八题x0=1:10;%创建横坐标向量y0=[-2.9414,-1.3807,-1.1918,-0.7284,-0.5484,-0.2656,-0.963,0.9794,0.9800,1.8860]; %创建纵坐标向量p=polyfit(x0,y0,3)%进行三阶多项式拟合x1=1:0.1:10y1=polyval(p,x1)%计算拟合曲线数据plot(x0,y0,'o',x1,y1,'m')%绘制原始数据和拟合曲线hold onx=1:0.25:10;%创建插值点y1=interp1(x0,y0,x,'cubic')%三次插值plot(x0,y0,'*',x,y1,'-g')%绘制图形legend('原始数据','拟合插值','三次插值') %图形标注p =0.0190 -0.3074 1.8106 -4.2733x1 =Columns 1 through 121.0000 1.50002.0000 2.50003.0000 3.50004.0000 4.50005.0000 5.50006.0000 6.5000Columns 13 through 197.0000 7.5000 8.0000 8.5000 9.0000 9.5000 10.0000y1 =Columns 1 through 12-2.7512 -2.1852 -1.7303 -1.3722 -1.0967 -0.8897 -0.7369 -0.6240 -0.5369 -0.4614 -0.3832 -0.2881Columns 13 through 19-0.1619 0.0096 0.2406 0.5454 0.9382 1.4332 2.0445y1 =Columns 1 through 12-2.9414 -2.3974 -1.9224 -1.5666 -1.3807 -1.3164 -1.2777 -1.2433 -1.1918 -1.0938 -0.9590 -0.8247Columns 13 through 24-0.7284 -0.6741 -0.6335 -0.5953 -0.5484 -0.4733 -0.3795 -0.2995 -0.2656 -0.3746 -0.6143 -0.8540Columns 25 through 36-0.9630 -0.6596 0.0081 0.6757 0.9794 0.9796 0.9797 0.9798 0.9800 1.0580 1.2633 1.5534Column 371.8860。