上机实验中遇到的一些Matlab函数.ppt_第1页
上机实验中遇到的一些Matlab函数.ppt_第2页
上机实验中遇到的一些Matlab函数.ppt_第3页
上机实验中遇到的一些Matlab函数.ppt_第4页
上机实验中遇到的一些Matlab函数.ppt_第5页
已阅读5页,还剩28页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

上机实验中遇到的一些Matlab函数,(1)* .* . (2)a:b:c linspace(a,b,c) (3)log log10 (4)exp (5)length size (6)round ceil floor (7)inv mod,3.1 Matlab 的程序类型 3.2 程序流程控制 3.3 关系与逻辑运算符 3.4 函数 3.5 绘图,第三章 MATLAB 编程基础,3.1 Matlab程序类型,1、 脚本文件 以.m格式进行存取,包含一连串的MATLAB指令和必要的注解,所有的变量都存在Workspace中,没有输入参数,也不返回参数。 2、 函数文件(help function) 以.m格式存在,函数接受输入参数,然后执行并输出结果。,3.2 程序流程控制,1、 If, else, elseif 2、 For 3、While 4、Switch,1、 If, else, elseif,TYPE 1: if expression statements end Example, if mod(a,2)= =0 a=a+3; end,TYPE 2: if expression statements else statements end Example: if(sum(A(:,2)10) A(1,1)=A(1,1)+B(1,1) A(2,1)=2*A(2,1) else A(1,1)=A(1,1)+A(1,1); A(2,1)=0.5 end,TYPE 3: if expression statements elseif expression statements else statements end,If语句可以嵌套 if expression 1 statements else if expression 2 statements else statements end end,步长缺省值为1,可以在正实数或负实数范围内任意指定。对于正数,循环变量的值大于终止值时,循环结束;对于负数,循环变量的值小于终止值时,循环结束。循环结构可以嵌套使用。,2、for循环语句 基本格式 for 循环变量起始值:步长:终止值 循环体 end,例:help for,3、while循环语句 基本格式 while 表达式 循环体 end,若表达式为真,则执行循环体的内容,执行后再判断表达式 是否为真,若不为真,则跳出循环体,向下继续执行。,While循环和for循环的区别在于,while循环结构的循环体被执行的次数不是确定的,而for结构中循环体的执行次数是确定的。,4、Switch语句 switch expression can be a scalar or a string case test1 command set 1 case test 2 command set 2 otherwise command set last end,3.3 关系和逻辑运算符,关系运算符 假设有:A=1 2 -1 -5 B=0 2 3 1 大于 AB ans=1 0 0 0 A1 ans=0 1 0 0 = 大于等于 A=B ans=1 1 0 0 = 等于 A=B ans=0 1 0 0 ; A=1 ans=1 0 0 0 = 不等于 A=B ans=1 0 1 1 ; A=1 ans=0 1 1 1, 非 A=0 0 1 0 1=0,注意:在处理逻辑运算时,运算元只有两个值即0和1,所以如果指定的数为0,MATLAB认为其为0,而任何数不等于0,则认为是1。,设有:A=5 -4 0 -0.5 B=0 1 0 9,& 与 A&B=0 1 0 1 A&1=1 1 0 1,| 或 A|B=1 1 0 1 A|1=1 1 1 1,Compute,ss=1; k=2;exact=pi2/6 while abs(1-ss/exact)=1e-4 ss=ss+1/k2; k=k+1; end,3.4 函数的编写,格式:function 返回变量列表=函数名(输入变量列表) function outarg1, outarg2, = fname(inarg1, inarg2,),求n! function k=factorial(n) if nargin1,error (输入变量个数错误,只能输入 个数量);end if nargout1,error (输出变量个数过多);end if abs(n-floor(n) epsn0 %(判定n是否为非负整数) error (n应该为非负数); end if n1 k=k*factorial(n-1); else if(n=0 | n=1) k=1; end end,3.4 画图,axis,plot, subplot, figure, hold on, hold off,plot(y) y = 4 2 3 4 6 plot(x,y) t = 0:0.1:4*pi y = sin(t) plot(t,y) plot(x1,y1,x2,y2,) t1 = 0:0.1:2*pi t2 = 0:0.1:4 plot(t1,sin(t1),t2,sqrt(t2) plot(x,y,option) 如 t1= 0:0.4:2*pi t2= 1:0.1;4; plot(t1,sin(t1), :,t2;cos(t2), g);,semilogx 横坐标为对数 semilogy 纵坐标为对数 loglog 横纵坐标均为对数 实例演示程序: t=0.1:0.1:2*pi; y=sin(t); semilogx(t,y) grid on,图形窗口的分割 Subplot(m,n,I) 分成m行n列,并选定I 实例演示程序: subplot(2,1,1) t=0.1:0.1:2*pi y=sin(t) semilogx(t,y) grid on subplot(2,1,2) x=1:0.01:5; y=exp(x) plot (x,y),线的颜色、线的类型、标记类型 The line colors have yellow, magenta, cyan, red, green, blue, white and black. The line styles have solid line (-) , dotted line (:), dot-dash line ( -.), dash line (- -) and no line (). The markers have point (.), circle(o), x-mark(x), plus(+), star(*), square(s), diamond(d), triangle(v), up-triangle(), pentagram(p), etc.,图形标注,坐标加标注 图形加标题 xlabel, ylabel, zlabel title, text,坐标轴 axis (xmin xmax ymin ymax); hold on; hold off,实例演示程序: t=0:0.1:4*pi; y=sin(t); plot(t,y) xlabel(x轴(04pi)); ylabel(y轴) title(正弦波,Fontsize, 12),图例: 为图形加图例便于观察分析 legend(string1,string2,string3,) 实例演示: t=0:0.1:4*pi plot(t,sin(t), :,t,cos(t), *) legend(sin wave, cos wave, None),三维图例: mesh , surf , plot3,xx1=linspace(-3,3,20) xx2=linspace(-3,13,17) x1,x2=meshgrid(xx1,xx2); z=x1.4+3*x1.2+x2.2-2*x1-2*x2-2*x1.2.*x2+6 surf(x1,x2,z),Q1(米3/秒),Q2(米3/秒),截面积A(米2),水位高度H(米),仿真实例:水位系统,系统建模,实体,水池、管道、阀门,属性,参数信息: A,输入信息: Q1 Q2,输出(状态)信息: H,活动

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论