- 1、本文档共32页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
第6章_M文件和函数句柄
A=pi/2; Heig=@sin; d=feval(Heig,A) d = 1 de=eval(Heig,A) ??? Error using == eval Function eval not defined for variables of class function_handle. 作 业 请分别写出用for和while循环语句计算下式的程序,此外,请写出一种避免循环的计算程序 第6章 M文件和程序控制 6.1 Matlab控制流 6.2 脚本文件和函数文件 6.3 Matlab的函数类别 6.4 函数句柄 6.1 Matlab控制流 for循环 while循环 if-else-end结构 switch-case结构 6.1.1 For循环结构 for n = array {commands} end for n=1:10 x(n)=sin(n*pi/10); end x x = Columns 1 through 6 0.3090 0.5868 0.8090 0.9511 1.0000 0.9511 0.8090 Columns 8 through 10 0.5868 0.3090 0.0000 for循环不能用for循环内重新赋值循环变量n来终止 ; 在for循环内接受任何有效的MATLAB数组; for循环可按需要嵌套 ; 当有一个等效的数组方法来解给定的问题时,应避免用for循环 ; 为了得到最大的速度,在 for循环被执行之前,应预先分配数组 . for循环的重要说明 for n=1:10 x(n)=sin(n*pi/10); n=10; end x x = Columns 1 through 6 0.3090 0.5868 0.8090 0.9511 1.0000 0.9511 0.8090 Columns 8 through 10 0.5868 0.3090 0.0000 for n=1:5 for m=5:-1:1 A(n,m)=n^2+m^2; end disp(n) end 1 2 3 4 5 A A = 2 5 10 16 26 5 8 13 20 29 10 13 18 25 34 16 20 25 32 41 26 29 34 41 50 n=1:10; x=sin(n.*pi/10) x = Columns 1 through 6 0.3090 0.5868 0.8090 0.9511 1.0000 0.9511 0.8090 Columns 8 through 10 0.5868 0.3090 0.0000 x=zeros(1,10); % preallocated memory for x for n=1:10 x(n)=sin(n*pi/10); end; 6.1.2 while循环结构 while expression {commands} end num=0;EPS=1; while (1+EPS)1 EPS=EPS/2; num=num+1; end num num = 53 EPS=EPS^2 EPS = 2.2204e-016 6.1.3 if-else-end分支结构 if expression {commands} end if expression commands evaluated if True else commands evaluated if False end if expression1 commands evaluated if expression1 is True elseif expression2 commands evaluated if expression2 is True elseif …… . else commands evaluated if no other expression is True end break和continue的用法 EPS=1; for num=1:1000 EPS=EPS/2; if (1+EPS)=1
文档评论(0)