1、西南科技大学本科生课程备课教案计算机技术在安全工程中的应用Matlab 入门及应用授课教师:徐中慧班 级:专 业:安全技术及工程第六章 逻辑函数与控制结构课型:新授课 教具:多媒体教学设备,matlab 教学软件一、目标与要求能够正确使用函数 find,正解和正确使用 if/else 系列命令,理解 switch/case 结构,正确使用 for 循环和 while 循环。二、教学重点与难点本堂课教学的重点在于引导学生在编写 matlab 程序时能够熟练运用控制结构的相关函数实现相应的功能。三、教学方法本课程主要通过讲授法、演示法、练习法等相结合的方法来引导学生掌控本堂课的学习内容。四、教学内
2、容上机内容回顾(1)创建温度换算表。下列等式描述了华氏温度(T F) 、摄氏温度(T C) 、开氏温度(T K)和兰金温度(T R)之间的换算关系: 0954.6732FRCRKT根据表达式解答以下问题:(a)创建数据表,把 00F 到 2000F 的华氏温度换算成开氏温度。由用户输入华氏温度的步长,用disp 和 fprintf 给表格添加标题和表头并输出格式化数据。x=input(请输入步长 );tf=0:x:200;tk=5/9*(tf+459.67);disp( 温度换算表);disp(华氏 开氏);fprintf(%3.0f %6.2fn,tf;tk)请输入步长 20温度换算表华氏
3、开氏0 255.3720 266.4840 277.5960 288.7180 299.82100 310.93120 322.04140 333.15160 344.26180 355.37200 366.48(b)创建一个有 25 行数据的表,把摄氏温度换算成兰金温度。由用户输入起始温度和合适的步长,用 disp 和 fprintf 给表格添加标题和表头并输出格式化数据。x=input(请输入起始摄氏温度);y=input(请输入步长 );tc=x:y:x+24*y;tr=9/5*tc+32+459.67;disp( 温度换算表);disp(摄氏 兰金);fprintf(%3.0f %6.
4、2fn,tc;tr)请输入起始摄氏温度 0请输入步长 20温度换算表摄氏 兰金0 491.6720 527.6740 563.6760 599.6780 635.67100 671.67120 707.67140 743.67160 779.67180 815.67200 851.67220 887.67240 923.67260 959.67280 995.67300 1031.67320 1067.67340 1103.67360 1139.67380 1175.67400 1211.67420 1247.67440 1283.67460 1319.67480 1355.67(c)创建数据
5、表,把摄氏温度换算成华氏温度。由用户输入起始温度、步长和数据的行数。用 disp和 fprintf 给表格添加标题和表头并输出格式化数据。m=input(请输入起始摄氏温度);n=input(请输入步长 );w=input(请输入数据的行数 )tc=m:n:m+(w-1)*n;tf=9/5*tc+32;disp( 温度换算表);disp(摄氏 华氏);fprintf(%3.0f %6.2fn,tc;tf)请输入起始摄氏温度 0请输入步长 10请输入数据的行数 5w =5温度换算表摄氏 华氏0 32.0010 50.0020 68.0030 86.0040 104.00(2)在 t=0 时刻,火
6、箭的发动机关闭,此时火箭的海拔高度为 500m,提升速度为 125m/s。考虑重力加速度,火箭的高度是时间的函数: 29.8()150,htttt计算时间在 0 到 30 秒内火箭的高度,并画出曲线: 用函数 ginput 估算火箭的最大高度和火箭返回地面的时间。 t=0:30; h=-9.8/2*t.2+125*t+500; plot(t,h) axis(0,30,0,1500) x,y=ginput(4) m=max(y) a,b=ginput(4) n=mean(a)x =11.993112.546112.753512.4770y =1.0e+003 *1.29171.29171.296
7、11.2961m =1.2961e+003a =29.066829.066828.928629.2742b =-2.19302.19302.19302.1930n =29.0841 用 disp 把结果在命令窗口显示出来。disp(火箭的最大高度(单位/m):);disp(m)disp(火箭返回地面的时间(单位/s):);disp(n)火箭的最大高度(单位/m):1.2961e+003火箭返回地面的时间(单位/s):29.0841(3)完成下面的问题,用 ginput 在图形上截取数据: 绘制一个圆形并定义角度数组,范围在 0 到 2 之间,步长为 /100。 用 ginput 在圆周上截取两
8、个点。 用 hold on 保持图形,用直线连接刚刚截取两个点。 用勾股定理计算两点间距离。m=0:pi/100:2*pi;x=sin(m);y=cos(m);plot(x,y)hold onaxis(-1,1,-1,1)a1,b1=ginput(1);a2,b2=ginput(1);c=a1 a2;d=b1 b2;plot(c,d)z=sqrt(a1-a2)2+(b1-b2)2)z =1.9928一、逻辑函数find命令 find 用于查找矩阵中符合某种条件的元素。例如,美国海军学院要求招收学员的身高必须高于66,假设拟招学员的身高为:height=63 67 65 72 69 78 75;
9、用 find 命令查找符合身高要求的元素序号:accept=find(height=66)accept =2 4 5 6 7函数 find 输出矩阵中符合要求的元素序号。若需要查看身高的数值,可以用元素序号调出该元素height(accept)ans =67 72 69 78 75为了增加输出结果的可读性,使用函数 disp 和 fprintf 显示输出结果:The following candidates meet the height requirementCandidate # 2 is 67 inches tallCandidate # 4 is 72 inches tallCandi
10、date # 5 is 69 inches tallCandidate # 6 is 78 inches tallCandidate # 7 is 75 inches tall完整代码:height=63 67 65 72 69 78 75;accept=find(height=66)height(accept)disp(The following candidates meet the height requirement);fprintf(Candidate # %4.0f is %4.0f inches talln,accept;height(accept)使用逻辑运算符可以创建更加复杂
11、的搜索条件。例如:除身高外,还要求学员的年龄必须在18岁到35岁之间。身高,英寸 年 龄 身高,英寸 年 龄63 18 69 3667 19 78 3465 18 75 1272 20程序代码:applicants=63 18;67 19;65 18;72 20;69 36;78 34;75 12;pass=find(applicants(:,1)=66 则因为输入向量并非所有元素都大于 0,因此则 Matlab 会跳转到 else 部分,输出错误信息。在if/else 语句中使用矢量会有很大的局限性,因此最好使用标量提 示在程序中,使用函数 beep 可以发出嘟嘟 ,用于提示用户出现错误。x
12、=input(Enter a value of x greater than 0: );if x0y=log(x)elsebeepdisp(The input to the log function must be positive)end(3)elseif 结构在 if/else 结构中如果有多层嵌套,就会很难判断到底应该执行哪段程序。如果在程序中使用函数elseif,程序代码就会变得更容易理解。下面这段程序可以根据申请人的年龄判定是否发放驾驶执照:age=input(Enter a value of age: )if age=16,因为当程序运行到这种语句时,申请人的年龄不可能小于16 岁
13、。如果非要按照类似的方式书写条件语句,就会得到错误的结果。age=input(Enter a value of age: )if age16disp(You may have a youth license)elseif age18disp(You may have a standard license)elseif age70disp(Drivers over 70 require a special license)end当 age=16,18 或 70 时,会输出错误结果。一般来说,elseif 结构适用于标量,find 适用于矩阵。在下面的例子中,用 find 对年龄矩阵进行分类,结果
14、用数据表格的形式输出:age=input(Enter a value of age: )set1=find(age=16fprintf(Sorry-Youll have to wait-youre only %3.0fn,age(set1)fprintf(You may have a youth license because youre %3.0fn,age(set2)fprintf(You may have a standard license because youre %3.0fn,age(set3)fprintf(Drivers over 70 require a special l
15、icense.Youre %3.0fn,age(set4)15 17 65 75Sorry-Youll have to wait-youre only 15You may have a youth license because youre 17You may have a standard license because youre 65Drivers over 70 require a special license.Youre 75(4)switch/case 结构在 Matlab 中,switch/case 结构可以根据一个给定参数的不同取值,执行不同的命令。它和if/else/els
16、eif 类似,都属于条件分支结构。凡是能够用 if/else/elseif 结构实现的程序功能都可以用switch/case 结构实现。而且用 switch/case 结构实现程序代码更容易理解。switch/case 结构和 elseif结构的最大区别在于判断的条件既可以是标题也可以是字符串,在实际情况下,字符串形式的判断条件比数字形式要常用。Switch/case 的语法结构如下:switch variablecase option1code to be executed if variable is equal to option 1case option2code to be exec
17、uted if variable is equal to option 2case option ncode to be executed if variable is equal to option notherwisecode to be executed if variable is not equal to any of the optionsend实 例创建函数用来显示三个城市的飞机票价格city=input(Enter the name of a city: ,s)switch citycase Bostondisp($345)case Denverdisp($150)case Honoluludisp(Stay home and study)otherwisedisp(Not on file)end语句中 otherwise 不是必需的,这条语句表示当所有的情况都不满足时,程序的输出结果。在 C 语言中也有 switch/case 结构。Matlab 和 C 语言的不同之处在于当某一条件为 true 时,Matlab 不再验证其它条件是否满足。