- 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
- 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
第2章_MALAB的程序设计2
第2章 程序设计和M文件 ;2.3 Matlab的流程控制;2.3.1 循环语句;;;;2. while循环; statement1
statement2 block1
…………
elseif control_expr_2;eg. 对于方程, 求其二次方根。
已知
则程序如下:
if (b^2-4*a*c)0
msg(“此方程有两复数根”)
elseif (b^2-4*a*c)==0
msg(“此方程有两恒等的实数根”)
else
msg(“此方程有两不等的实数根”)
end; Notes Concerning the Use of if Constructs;if x 0
…
if y 0
…
end
…
end
The MATLAB interpreter always associates a given end statement with the most recent if statement, so the first end closes the if y 0 statement, while the second end closes the if x 0 statement. ;For example, suppose we have a large program containing a construct like the one that follows.
…
if (test1)
…
if (test2)
…
if (test3)
...
end
…
end
…
end;Example 3.4- Assigning Letter Grades;Solution;(b) One possible structure using nested if constructs is
if grade 95.0
disp(The grade is A.);
else
if grade 86.0
disp(The grade is B.);
else
if grade 76.0
disp(The grade is C.);
else
if grade 66.0
disp(The grade is D.);
else
disp(The grade is F.);
end
end
end
end; statement1
statement2 block2
…………
otherwise;多个条件执行相同语句时,可以写在一个case表达式中,如:;eg. 使用switch结构判断学生成绩的等级,90分以上为优,80~90为良,70~80为中,60~70为及格,60分以下为不及格。;2.3.4 其他控制流;eg. for 中的continue 语句;;eg. for循环中的 break语句;eg. 计算f(x)=ex,使用break语句当f(x)1000时终止计算。;eg. 使用for循环将字符串中的数值取出,遇到非数值则跳过。;相当于:;3、try/catch 错误控制语句;eg.;eg.;Example 3.2- The Quadratic Equation;2. Define the inputs and outputs.;The resulting pseudocode is;4. Turn the algorithm into MATLAB statements.;% Prompt the user for the coefficients of the equation
disp (This program solves for the roots of a quadratic’);
disp (equation of the form A*X^2 + B*X + C = 0. );
a = input (Enter the coefficient A: );
b = input (Enter the coefficient B: );
c = input (Enter
文档评论(0)