matlab入门经典范例

合集下载

MATLAB程序设计及应用实例

MATLAB程序设计及应用实例

MATLAB程序设计及应用实例MATLAB(Matrix Laboratory)是一种用于算法开发、数据分析、可视化和数值计算的高级技术计算语言和环境。

它的强大功能和灵活性使其成为各个领域研究和工程实践中广泛使用的工具。

下面将介绍几个MATLAB程序设计及应用的实例。

1.信号处理:MATLAB是信号处理的强大工具,它可以用于滤波、频谱分析、小波变换、分析和合成音频信号等。

例如,可以利用MATLAB进行语音信号的去噪处理,通过设计特定的滤波器来去除信号中的噪声成分,从而提取出清晰的语音信号。

2.图像处理:MATLAB可以进行图像的加载、处理和分析。

它提供了丰富的图像处理函数和工具箱,可以实现图像的滤波、二值化、边缘检测、图像增强等操作。

例如,可以使用MATLAB对医学图像进行分割,将感兴趣的区域提取出来,辅助医生进行病灶诊断。

3.控制系统设计:MATLAB是控制系统设计的有效工具。

它提供了丰富的控制系统分析和设计函数,可以进行系统建模、模拟和优化。

例如,可以使用MATLAB进行PID控制器的参数调整,通过对系统建模和后续仿真,优化PID控制器的参数,提高控制系统的性能和稳定性。

4.机器学习:MATLAB提供了强大的机器学习和深度学习工具箱,可以进行数据预处理、特征提取、模型训练和评估等操作。

例如,可以利用MATLAB进行图像分类,通过构建深度卷积神经网络模型,将输入的图像进行分类和识别。

5.数值计算:MATLAB对线性代数、数值优化和统计分析等有着强大的支持。

它提供的优化和求解函数可以解决复杂的线性和非线性优化问题,例如最小二乘拟合和参数估计等。

此外,MATLAB还拥有强大的统计分析工具,可以进行假设检验、数据拟合、方差分析等统计分析操作。

6.仿真模拟:MATLAB可以进行动态系统的建模和仿真,通过搭建系统方程和初始条件,可以对系统的动态响应进行模拟。

例如,在电力系统中,可以使用MATLAB进行电力系统稳定性分析,对电力系统的动态响应进行跟踪和分析。

matlab数学建模100例

matlab数学建模100例

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

MATLAB常用函数的使用(解释加实例)

MATLAB常用函数的使用(解释加实例)

MATLAB常用函数的使用(解释加实例)1.常用数学函数:- `sqrt(x)`:求一个数的平方根。

例如,`sqrt(9)`的结果是3- `sin(x)`:计算角度x的正弦值。

例如,`sin(pi/2)`的结果是1- `cos(x)`:计算角度x的余弦值。

例如,`cos(pi/2)`的结果是0。

- `exp(x)`:计算e的x次方。

例如,`exp(1)`的结果是2.71832.数组操作函数:- `length(array)`:返回数组的长度。

例如,`length([1, 2, 3])`的结果是3- `sum(array)`:计算数组元素的和。

例如,`sum([1, 2, 3])`的结果是6- `max(array)`:找出数组中的最大值。

例如,`max([1, 2, 3])`的结果是3- `sort(array)`:对数组进行排序。

例如,`sort([3, 2, 1])`的结果是[1, 2, 3]。

3.矩阵操作函数:- `eye(n)`:生成一个n阶单位矩阵。

例如,`eye(3)`的结果是一个3x3的单位矩阵。

- `zeros(m, n)`:生成一个m行n列的全零矩阵。

例如,`zeros(2, 3)`的结果是一个2x3的全零矩阵。

- `ones(m, n)`:生成一个m行n列的全1矩阵。

例如,`ones(2, 3)`的结果是一个2x3的全1矩阵。

- `rand(m, n)`:生成一个m行n列的随机矩阵。

例如,`rand(2,3)`的结果是一个2x3的随机矩阵。

4.文件操作函数:- `load(filename)`:从文件中加载数据。

例如,`load('data.mat')`将从名为"data.mat"的文件中加载数据。

- `save(filename, data)`:将数据保存到文件中。

例如,`save('data.mat', x)`将变量x保存到名为"data.mat"的文件中。

一、MATLAB之基础入门代码

一、MATLAB之基础入门代码

⼀、MATLAB之基础⼊门代码序⾔本篇旨在列出matlab⼊门及在科研中必须掌握的⼀些基础代码,主要内容包括:matkab矩阵运算、绘图、函数流程控制、⽂件、图像、函数等基本操作,以便于随查随⽤,⾄于常⽤的算法本篇鲜有涉及。

%% matlab总述% matlab中基本数据结构是矩阵,即使单独⼀个数也视为⼀个1*1的矩阵%%%% 矩阵创建&赋值操作a=5;b=[1 2 3];c=[1;2;3];d=[1 2 3;4 5 6;7 8 9];x=[0:0.5:2]; % 0.5是步长x=zeros(4); % 4*4零矩阵x=zeros(2,3);% 2*3零矩阵x=ones(5,5); % 1矩阵x=eye(5); % 5阶单位阵,与Octave语法完全⼀致[r,c]=size(x); % 返回x矩阵的⼤⼩x=linspace(0,3,5); % 0到3间均匀⽣成5个点,⽣成函数的采样点[x,y,z]=meshgrid([],[],[]); % ⽤以⽣成函数的采样点%%%% matlab中的保留字piInf % ⽆穷⼤NaN % 未定式,0/0%%%% 数组计算a=a+b;a=a-b;% 数组加减a=b*c;a=b*inv(x)% 数组乘除 inversea=b.*c; % 对应位操作./ .* .^a=a';a=a(:);% 数组拉直%%%% 数组索引与操作% 括号是索引符号,如([],[])x=a(2,2);x=a(5);x=a(1:2,3:4); %([],[])x(x>72)=x(x>72)-72 % 数组的逻辑索引,x>72⾃⾝就是⼀个0/1⼆维数组,x(x>72)就是访问对应为1位置处的值% 数组赋值/改值a(2,2)=0a(5)=9a(1:2,3:4)=ones(2,2)a(1:2,[3,4,7,11])=ones(2,2) % matlab中[1:9],1:9是基本数据类型%%%% 数组的⽐较和逻辑运算,符合条件的同⼀位置为1,不符合则为0x=[1 2 3 4 5 6 7];y=[1 2 4 5 5 6 8];eq=(x==y); %上⾯说过了,x==y⾃⾝也是⼀个0/1⼆维数组eq=(x>2)&(y<5); % 与 &两边既可以是标量也可以是向量 VS. &&两边只能是标量eq=(x>2)|(y<5); % 或eq=xor(x>4,y<5); % 异或x(x<3)=-1; % (x<3)是⼀个0/1数组,x(x<3)就是把(x<3)数组中1对应位置的位置访问,再将访问到的位置赋值1,⽐较和逻辑运算与数组索引结合xy=[x,y]; xy=[x;y]; % ⽤已有数组构建新数组,数组融合img=cat(3,R,G,B); % ⾼级数组融合xy(:,xy(1,:)<4)=0 %逻辑运算结果矩阵也可以⽤来数组索引%%%% 数组操作函数x=[1 2 3;4 5 6;7 8 9];x=flipud(x);%上下转x=fliplr(x);%左右转x=rot90(x);%逆时针旋转90度x=pinv(x);%数组求(伪)逆sum(x,1); % x每列求和sum(x,2); % x每⾏求和sum(x(:)); % 求全和,:表⽰将数组拉直maxn=max(x);% 返回每列最⼤值maxn=max(x);% 返回每列最⼤值maxn=max(x(:)); % 返回全局最⼤值min(); % ⽤法同max()%%%% 常⽤数学函数% 注意matlab中矩阵是基本数据结构,因此所有函数都是对矩阵中每个x_i操作y=sin(x);y=abs(x);%绝对值y=sqrt(x);%开⽅y=ceil(x);%向上取整y=floor(x);%向上取整y=round(x);%四舍五⼊取整y=rand(r,c);%⽣成随机矩阵b=sum(a,idm);%求和函数,dim=1 表⽰对每⼀列求和,dim=2 表⽰对每⼀⾏求和tabulate(detect_result)% detect_result是⼀个列向量,该函数⽤以频数、频率统计%%%% MATLAB函数基本语句for i=1:2:100 %endwhile 1if a<1breakendendfunction [output1,]=functionname(input1,) % 函数定义command1command2output1=%%%% 基本绘图%plotx=0:0.001*pi:2*pi;y=sin(x);z=cos(x);plot(x,y,'-ob','LineWidth',1.5) % 标出数据点的折线图hold onplot(x,z,'rs') % 散点图drawnow % 动画图xlabel('x')ylabel('y')title('图')axis equal % 两轴单位长度相等axis([-2,2,-2,2]) % 控制坐标轴范围set(gca,'XTick',0:pi/2:4*pi); % 设置坐标轴刻度间距,⼀般与下⼀⾏命令搭配set(gca,'XTickLabel',{'0','0.5*pi','pi','1.5*pi','2*pi','2.5*pi','3*pi','3.5*pi','4*pi'})% 设置坐标轴刻度标号xlim([-2,2]) % 控制坐标轴范围text(0,0,'(0,0)') % 在数据曲线上点(x,y)处,标出'(3,5)'legend('cos(x)','sin(x)','sin(x)-cos(x)') % 依照绘图的顺序依次标注图例saveas(gcf,strcat('ch',num2str(i),'.emf')) % 保存plot图⽚,gcf是plot的句柄plot(X)%绘制⼆维矩阵,以⾏号为横坐标,各列为纵坐标plot(X);% plot制作动图for k=1:10plot (fft(eye(k+10))) % eye()单位阵,fft()傅⾥叶变换,plot()绘制矩阵axis equalM(k)=getframe; % 截取当前窗⼝作为影⽚帧endmovie(M,50) %播放多帧图⽚,M 50次%极坐标plar(theta,r,LineSpec)t=0:0.1:3*pi;polar(t,abs(cos(5*t)));% 快速⽅程绘图fplot('x-cos(x^2)',[-4,4]);% 绘制y=x-cos(x^2)ezplot('y-f(x),[-6 6 -8 8]');% ezplot('⽅程式',[xmin xmax ymin ymax])ezplot('cos(3*t)','sin(3*t)',[0:2*pi]);% ezplot('x参数式','y参数式',[tmin,tmax])%%%% 参数⽅程作图(可以画出很有趣的⾮函数图像)t=0:pi/50:2*pi;x=sin(t);y=cos(t);plot(x,y)axis([-1.1,1.1,-1.1,1.1])axis equal%%%%x=sin(t)y=cos(t)z=tplot3(x,y,z) % 三维曲线参数⽅程作图grid on % 开⽹格%%%%三维曲⾯[x,y]=meshgrid(-pi:0.1:pi); % 画函数采样点z=sin(x).*cos(y);mesh(x,y,z) % 画三维曲⾯figure() % 开新画板surf(x,y,z) % 画中间插值的三维曲⾯(有渲染效果)%%%% ⽂件数据读取% .txt纯数据⽂件⽂件data=load('c:\desktop\score.txt')% .txt⽂本⽂件fid=fopen('score.txt','r')line1=fgetl(fid)%数据按分割%数据类型转换line2=fgetl(fid)fclose(fid);fid=fopen('score.txt','w')fprintf(fid,'会当凌绝顶’)fprintf(fid,'%d⽉⼯资 %6.1f\n',[1,2,3,4;20000,19999,20010,25000,23000])fclose(fid);% excel⽂件data=xlsread('filename.xls','Sheet1','A3:C6');data(isnan(data))=0;%空位补零xlswrite('filename.xls',{'t','w'},'Sheet1','B1:C1')%图⽚⽂件img=imread('leave.jpg')% 图⽚读取image(img) % 图⽚显⽰lip(234:435,112:300,:)%图⽚切⽚imshow(lip) % 图⽚显⽰imwrite(img,'c:\\desktop\\figure.emf')% UI交互式导⼊图⽚[FileName PathName FilterIndex]=uigetfile({'*.jpg','*.bmf'},'请导⼊图⽚','*.jpg','MultiSelect','on') if ~ FilterIndexreturnend%视频⽂件data=VideoReader('sport.avi')% data是⼀结构体frame=read(data,25)% 读取视屏中的某⼀帧,即图⽚imshow(frame)% 对视频处理就是循环处理每⼀帧%%%% 线性⽅程求解、拟合、回归x=A\B%⼀元线性拟合x=[2.410 2.468 2.529 2.571 2.618 2.662 2.715 2.747 2.907 2.959 2.997];y=[0 0.800 1.695 2.290 2.965 3.595 4.365 4.810 7.125 7.890 8.425];a=polyfit(x,y,1)%⼀阶拟合 y=a1 x+ a2y=polyval(a,x) % 获得拟合表达式%⾃定义拟合p=fittype('a*x+b*sin(x)+c');% 指定拟合模型f=fit(x,y,p)% 获得拟合函数,x和y必须为列向量plot(f,x,y)% 画出拟合图%差值 x=[1:10] y=[1:10],线性回归求xi对应的yiyi=interp1(x,y,xi,'linear');%线性回归 y=f(x1,x2,x3)%%%%微分⽅程求解%解析解syms y(x);ode=diff(y,x)-y==0;init=y(0)==1;dsolve(ode,init)%dsolve('D2y+4*Dy+24*y=0','y(0)=0,Dy(0)=15','x')%尤克—库塔数值解%%% 符号对象的创建,matlab中之前都是数值计算,这⾥是符号运算a=sym([1/2 sqrt(5)]);y=sym('2*sin(x)*cos(x)');y=simple(y);syms x y;z=cos(x)*sin(y);% 符号表达式及函数的创建, matlab默认是数值运算,符号运算需要提前声明。

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

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

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

matlab编程经典例题

matlab编程经典例题

例3-3 求一元二次方程ax2 +bx+c=0的根。 程序如下: a=input('a=?'); b=input('b=?'); c=input('c=?'); d=b*b-4*a*c; x=[(-b+sqrt(d))/(2*a),(-b-sqrt(d))/(2*a)]; disp(['x1=',num2str(x(1)),',x2=',num2str(x(2))]);
(2) 双分支if语句: if 条件 语句组1 else 语句组2 end 当条件成立时,执行语句组1,否则执行语 句组2,语句组1或语句组2执行后,再执行 if语句的后继语句。
例3-4 计算分段函数的值。 程序如下: x=input('请输入x的值:'); if x<=0 y= (x+sqrt(pi))/exp(2); else y=log(x+sqrt(1+x*x))/2; end Y
(3) 多分支if语句: if 条件1 语句组1 elseif 条件2 语句组2 …… elseif 条件m 语句组m else 语句组n end 语句用于实现多分支选择结构。
例3-5 输入一个字符,若为大写字母,则输出其 对应的小写字母;若为小写字母,则输出其对应 的大写字母;若为数字字符则输出其对应的数值, 若为其他字符则原样输出。 c=input('请输入一个字符','s'); if c>='A' & c<='Z' disp(setstr(abs(c)+abs('a')-abs('A'))); elseif c>='a'& c<='z' disp(setstr(abs(c)- abs('a')+abs('A'))); elseif c>='0'& c<='9' disp(abs(c)-abs('0')); else disp(c); end

matlab编程例题

matlab编程例题

matlab编程例题Matlab是一种高级的计算机编程语言和数学计算软件。

它具有强大的数据处理和可视化功能,可以用于各种科学计算、数据分析、模拟和建模等领域。

本文将介绍一些常见的Matlab编程例题,帮助初学者掌握Matlab的基本编程技能。

1. 矩阵运算矩阵是Matlab中最基本的数据类型之一,可以进行各种数学运算。

下面是一些矩阵运算的例子:a = [1 2 3; 4 5 6; 7 8 9]; %定义一个3×3的矩阵b = [10 20 30; 40 50 60; 70 80 90]; %定义另一个3×3的矩阵c = a + b; %矩阵加法d = a - b; %矩阵减法e = a * b; %矩阵乘法f = a' %矩阵转置运行上面的代码,可以得到以下结果:c =11 22 3344 55 6677 88 99d =-9 -18 -27-36 -45 -54-63 -72 -81e =300 360 420660 810 9601020 1260 1500f =1 4 72 5 83 6 92. 绘图Matlab具有强大的绘图功能,可以绘制各种二维和三维图形。

下面是一些绘图的例子:x = linspace(0, 2*pi, 100); %生成一个包含100个点的等间隔向量y = sin(x); %计算sin函数plot(x, y); %绘制sin函数图像z = peaks(25); %生成一个25×25的山峰矩阵surf(z); %绘制3D山峰图像运行上面的代码,可以得到以下结果:sin函数图像:3D山峰图像:3. 文件读写Matlab可以读写各种文件格式,包括文本文件、Excel文件、图像文件等。

下面是一些文件读写的例子:fid = fopen('data.txt', 'r'); %打开名为“data.txt”的文本文件data = fscanf(fid, '%f'); %读取文件中的数据fclose(fid); %关闭文件plot(data); %绘制数据图像A = xlsread('data.xlsx'); %读取名为“data.xlsx”的Excel 文件plot(A(:, 1), A(:, 2)); %绘制Excel文件中的数据图像运行上面的代码,可以得到以下结果:文本文件数据图像:Excel文件数据图像:4. 函数编写Matlab中的函数是一种可重复使用的代码块,可以让程序更加模块化和可读性更高。

Matlab100个实例程序

Matlab100个实例程序

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

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

num1=[13];den1=conv([1,1],[1,0]);G1=tf(num1,den1);num2=[5.096,13];ssys1=conv([1,1],[1,0]); ssys2=conv([0.098,1],[1]); den2=conv( ssys1,ssys2); G2=tf(num2,den2); figure(1)margin(G1);hold onmargin(G2);num1=[13];den1=conv([1,1],[1,0]);G1=tf(num1,den1);num2=[5.096,13];ssys1=conv([1,1],[1,0]); ssys2=conv([0.098,1],[1]); den2=conv( ssys1,ssys2); G2=tf(num2,den2); figure(1)margin(G1);hold onmargin(G2);num=[4.56,10];ssys1=conv([1,1],[1,0]); ssys2=conv([0.114,1],[1]); den=conv( ssys1,ssys2); G=tf(num,den);figure(1)bode(G)num=[4.56,10];ssys1=conv([1,1],[1,0]); ssys2=conv([0.114,1],[1]); den=conv( ssys1,ssys2); G=tf(num,den);figure(1)msrgin(G);num1=[13];den1=conv([1,1],[1,0]);G1=tf(num1,den1);num2=[5.096,13];says1=conv([1,1],[1,0]);says2=conv([0.098,1],[1]);den2=conv( says1,says2);G2=tf(num2,den2);says3=feedback(G2,1);step(feedback(G1,1));hold onstep(says3);gridx=linspsce(-2,2) %取值区间函数y=x.^(2);plot(y,x) %绘图函数命令编译M语言时,应该尽量省略”;“这个符号,强行加上时,可能不编译,故而尽量不要加上。

切记,切记!k=13;den=conv([1 0],[1 1]);G1=tf(k,den);Wm=5.1;L=20*log10(k/(Wm*sqrt(1+Wm^(2)));s=10^(-0.1*L);T=1/(Wm*sqrt(s));G2=tf([s*T 1],[T 1]);G=G1*G2;figure(1)msrgin(G1);hold onmsrgin(G);s = struct('type', {'big','little'}, 'color', {'red'}, ...'x', {3 4});rmfield(s,'x') %(用来删除某一项)s(1).x='1' %(用来添加某一项)s=ones(2,2),c=cell(size(s))(用来建立单位矩阵)s=1;b=2;c=3;x=[5,b,c,9;s*c,s+c,c/b 8;1 6 7 1]x(:,:)s=linspsce(1,1,10)s=eye(2,2) %(用来建立单位矩阵)s=ones(2,2) %(用于建立全“一”矩阵)s=[1 2]s=[3 4 5;6,7,8;10 11 12]; (建立矩阵的方法) y=s^2inv(s)s*ss=[1 2;3 4]b=[3 4;5 6]y=s>bsscii=chsr(reshspe(32:127,32,3)')y=sin(x)plot(y,x)x=[0 1 2;3 4 5;6 7 8];msgic(3)function [m]=fexch(x,y)m=x+y;clesr;x=1;y=2;fexch(x,y)function [m]=fexch(x)m=2*sqrt(x.^2+10)-5;fexch(3)x=1;fexch(x)x=linspsce(1,9)y=sin(x)plot(x,y)xm=input('whst's your nsme?','s');输入字符串要加标记符号reply=input('Do you wsnt more? Y/N [Y]: `)x=input('请输入一个数字');y=sbs(x)if c>='s'&c<='Z'disp(setstr(sbs(c)+sbs('s')-sbs('s')));ends=[1,2,3,4,5,6];B=[7;8;9;10;11;12;];trycstchc=s.*B;lssterr %显示出错原因endfor n=1:5x(n)=n^2;enddisp(x'); %(作用是显示输出量)s=0;n=1;while n<=10s=s+n;n=n+1;enddisp(s);逻辑运算符号表示:&(与),I(或),~(非)rem(求余数的运算)。

x=-pi:pi/10:pi; %以pi/10为步长y=tsn(sin(x))-sin(tsn(x));plot(x,y,':r')x1=-10:1:10;x2=-10:1:10;y1=2*x1;y2=x2.^2;plot(x1,y1,x2,y2)x=-10:1:10;y=2*x;sxis([-5 5 -5 5])t=0:0.05:7;plot(t,sin(t))set(gcs,'xtick',[0 1.4 3.14 5 6.28])(显示给定的坐标值)set(gcs,'xticklsbel',‘’)显示给定的字符串或者是汉字等x1=-10:10;x2=-10:10;y1=x1.^2;y2=x2.^2;plot(x1,y1,x2,y2)gridsubplot(1,2,1);pie([7,17,23,19,5]);title('饼图');legend('优秀','良好','中等','及格','不及格'); subplot(1,2,2);compsss([3+2i,4.5-i,-1.5+5i]);title('向量图');t=0:pi/50:10*pi;plot3(sin(t),cos(t),t)grid onsxis squsrex=-10:10;y=2*exp(-0.5*x);plot(x,y);gridpolsr(thets,rho,选项)其中thets为极坐标极角,rho为极坐标矢径,选项的内容与plot函数一致。

x=-10:10;y=x.^2;semilogy(x,y)(以Y坐标按对数坐标分度)loglog(x,y) (以X坐标按对数坐标分度)semilogx(x,y)(以X和Y坐标按对数坐标分度)绘制三维图形用函数plot3(x,y,z).[x,y]=meshgrid(-3:.125:3);z=pesks(x,y);meshc(x,y,z);sxis([-3 3 -3 3 -10 5])figuresurfc(x,y,z)t=0:0.01:2*pi; %画圆的程序y1=sin(t);y2=cos(t);plot(y1,y2)gridsxis('equsl')(此段很重要,让X和Y坐标轴等比例显示,用来画圆)k=polyder(p),返回多项式P得不导函数。

k=polyder(s,b) 返回多项式s与b的乘积的导函数。

[q,d]=polyder(b,s),返回多项式b整除s的导函数,其中分子多项式返回给q,分母多项式返回给d。

s=[3,6,9];b=[1,2,0];k=conv(s,b)r=roots(p)(求多项式等于零的根)p=poly(r)(由根求出对应多项式的表达式)p=[1 2 4 5 7 8 9 4 3 1 1];x=roots(p)(求根)x=poly(p)(求多项式)多项式求值:当X为何值时,求多项式P的值具体如下y=polyvsl(p,x)(求职函数)s=[1 8 0 0 -10];x=roots(s)(求根)再MATLAB中用polyfit函数求最小二乘拟合多项式的系数,再用polyvsl函数按所得的多项式计算所给出的点上的函数近似值。

poly函数调用格式:[p,s]=polyfit(x,y,m)。

函数根据采样点X和采样点函数值Y,产生一个M次多项式P及其在采样点的误差向量s,其中X,Y 是两个等长的向量,P是一个长度为M+1的向量,P的元素为多项式系数。

t=1900:10:1990;p=[75.995 91.972 105.711 123.203 131.699 150.697 179.323 203.212 226.505 249.633];interp1(t,p,1985) %插值取样x=1900:1:2000;y=interp1(t,p,x,'cubic');(linesr线性插值nesrest最近点插值cubic三次多项式插值spline三次样条插值)plot(t,p,':o',x,y,'-r')(':o'是对图形的颜色形状作出要求)。

有关矩阵的几个公式inv:矩阵求逆,det:矩阵行列式的值,eig:矩阵的特征值[V,D]=eig(s)表示求矩阵的全部特征值构成的对角阵D。

s的特征向量分别为V的列向量。

rsnk:矩阵的秩,trsce:矩阵的迹s=[1 2 3;4 5 6;7 8 9];[V,D]=eig(s)simulink(调用模块库命令)。

控制系统工具箱s=tf('s');(使用此语句可以直接书写表达式)G=(12*s^3+12*s+20)/(2*s^4+4*s^3+6*s^2+2)s=zpk('s')(使用此语句可以直接书写表达式)若分子分母为一常数,即等于零没有根,则表示为z=[],p=[]表示此项无根。

H=tf([1 -1],[1 4 5],'inputdelay',0.35)(表示输入延迟0.35秒)Hd=c2d(H,0.1,'foh')(表示采样周期为0.1秒)num=[2 0 9 1];den=[1,1,4,4];[r p k]=residue(num,den):作用是将传递函数转换为子传递函数和的形式,其中r 和p分别为各个子传递函数的增益和极点,k为部分分式展开后的余项(常数项)。

相关文档
最新文档