西南科技大学本科生课程备课教案
计算机技术在安全工程中的应用
——Matlab 入门及应用
授课教师:徐中慧
班级:
专业:安全技术及工程
第七章逻辑函数与控制结构
课型:新授课
教具:多媒体教学设备,matlab 教学软件
一、目标与要求
能够正确使用函数find,正解和正确使用if/else 系列命令,理解switch/case 结构,正确使用for 循环和while 循环。
二、教学重点与难点
本堂课教学的重点在于引导学生在编写matlab 程序时能够熟练运用控制结构的相关函数实现相应的功能。
三、教学方法
本课程主要通过讲授法、演示法、练习法等相结合的方法来引导学生掌控本堂课的学习内容。四、教学内容
课后习题讲解
%% one
a=[15 3 22;3 8 5;14 3 82];
b=[1 5 6]';
c=[12 18 5 2];
d=a(:,3)
%% two e=[b,d]
%% three
f=[b;d]
%% four
g=[c(1:3);a]
%% five
h=[a(1,3),c(1,2),b(2,1)]
? ? ? ?
%% Variable Definition
Density_mercury=13560;
g=9.81;
p=0:10000:100000;
%% mercury
h_mercury=p./(Density_mercury*g)
%% water
h_water=h_mercury.*13560/1000
%% output
h=[h_mercury',h_water']
(3) 已知下列矩阵: ? 1 10 42 6 ? ? 5 8 78 23? x = ? ? ?56 45 9 13? ?23 22 8 9 ?
?1 2 3 ? y = ?4 10 12 ? ??7 21 27?? z = [10 22 5 13]
①查找各矩阵中所有大于 10 的元素的序号,输出结果用单序号方法表示。
②查找各矩阵中所有大于 10 的元素的序号,输出结果用该元素的下标表示。
③查找各矩阵中所有大于 10 的元素。
④查找各矩阵中所有大于 10 小于 40 的元素的序号,输出结果用单序号方法表示。
⑤查找各矩阵中所有大于 10 小于 40 的元素的序号,输出结果用该元素的下标表示。
⑥查找各矩阵中所有大于 10 小于 40 的元素
⑦用 length 和 find 查询各矩阵中数值在 0 到 10 之间或 70 到 80 之间元素的个数。
%% Variable Definition
x=[1 10 42 6;5 8 78 23;56 45 9 13;23 22 8 9];
y=[1 2 3;4 10 12;7 21 27];
z=[10 22 5 13];
%% one
a=find(x>10),b=find(y>10),c=find(z>10),
%% two
[a2,A2]=find(x>10),[b2,B2]=find(y>10),[c2,C2]=find(z>10),
%% three
a3=x(a),b3=y(b),c3=z(c),
%% four
a4=find(x>10&x<40),b4=find(y>10&y<40),c4=find(z>10&z<40),
%% five
[a5,A5]=find(x>10&x<40)
[b5,B5]=find(y>10&y<40)
[c5,C5]=find(z>10&z<40)
%% six
a6=x(a4),b6=y(b4),c6=z(c4)
%% seven
a7=find((x>0&x<10)|(x>70&x<80));
aa=length(a7)
b7=find((y>0&y<10)|(y>70&y<80));
bb=length(b7)
c7=find((z>0&z<10)|(z>70&z<80));
cc=length(c7)
(4)使用if、if/else、elseif 等结构创建函数解决下列问题,假设函数的输入数据为标量,对函数进行
测试。
①.假设某州的法定饮酒年龄为21 岁,创建函数判断是否达到法定饮酒年龄。
②.许多游乐场所规定了参与者的最低身高。假设某种游戏要求参与者必须高于155,创建函数判断是否符合身高要求。
③在产品制造时,通常会规定产品尺寸的容差。假设某产品的长度为5.4㎝,可以允许有 0.1㎝的正负误差。创建函数判断产品是否符合该技术要求。
④固体燃料火箭发动机有三级。第一级的燃料耗尽后与导弹分离,第二级点火。然后第二级燃尽分离后第三级点火。最后,第三级燃尽后与导弹分离。各级的燃烧时间如下所示:
级1 0~100s
级2 100~170s
级3 170~260s
创建函数判断火箭正处于第一级、第二级、第三级和自由飞行(无动力阶段)中的哪个阶段?
①age=input('Enter a value of your age: ');
if age>21
disp('You have reached legal drinking age !')
else
disp('You''v not reached legal drinking age !')
end
②Height=input('Enter a value of your height: ');
if height>155
disp('You have reached the height !')
else
disp('You''v not reached the height !')
end
③size=input('Enter a value of size: ');
if size<5.3
disp(' It''s not a accepted product !')
elseif size>5.5
disp(' It''s not a accepted product !')
else
disp(' It''s a accepted product!')
end
或size=input('Enter a value of size: ');
if size>5.3&size<5.5
disp(' It''s a accepted product!')
else
disp(' It''s not a accepted product !')
end
④time=input('Enter a value of time: ');
if time>0&time<100
disp('one')
elseif time>0&time<170
disp('two')
elseif time>0&time<260
disp('three')
elseif time>260
disp('free fly')
else
disp('You input a error information !')
end
接第六课内容
(4)s w i t c h/c a s e结构
在 Matlab 中, switch/case 结构可以根据一个给定参数的不同取值,执行不同的命令。它和if/else/elseif 类似,都属于条件分支结构。凡是能够用 if/else/elseif 结构实现的程序功能都可以用switch/case 结构实现。而且用 switch/case 结构实现程序代码更容易理解。switch/case 结构和 elseif 结构的最大区别在于判断的条件既可以是标题也可以是字符串,在实际情况下,字符串形式的判断条件比
数字形式要常用。S w i t c h/c a s e的语法结构如下:
switch variable
case option1
code to be executed if variable is equal to option 1
case option2
code to be executed if variable is equal to option 2
………………
case option n
code to be executed if variable is equal to option n
otherwise
code to be executed if variable is not equal to any of the options
end
实例
创建函数用来显示三个城市的飞机票价格
city=input('Enter the name of a city: ','s')
switch city
case 'Boston'
disp('$345')
case 'Denver'
disp('$150')
case 'Honolulu'
disp('Stay home and study')
otherwise
disp('Not on file')
end
语句中 otherwise 不是必需的,这条语句表示当所有的情况都不满足时,程序的输出结果。在 C 语言中也有 switch/case 结构。Matlab 和 C 语言的不同之处在于当某一条件为 true 时,Matlab 不再验证其它条件是否满足。
例题
世界上许多国家没有对公制单位的使用做强制性规定,这些国家包括美国、英国、利比亚和缅甸。在美国既可以使用公制单位也可以使用英制单位。例如,老式汽车中既使用了公制单位也使用了英制单位,而1989 年以后生产的新款汽车全部采用公制单位。酒的单位是升,牛奶的单位是加仑,距离的单位是英里,功率的单位是瓦特。公制单位和英制单位经常会发生混淆。如果驾车从美国到加拿大旅行就会发现在美国汽油的单位的加仑,而在加拿大汽油的单位是升。
用 switch/case 结构编写一段程序,要求具有以下功能:
?由用户输入汽油的单位(升或加仑)
?提示用户输入想购买汽油的数量
?假设每加仑汽油 2.89 美元,计算汽油的总价
1.问题描述
计算汽油的总价
2.输入/输出描述
输入:指定汽油的计量单位是加仑还是升以及购买的汽油的数量
输出:每加仑2.89 美元,计算汽油的总价
clear,clc
%Define the cost per gallon
rate=2.89;
%Ask the user to input gallons or liters
unit=input('Enter gallons or liters','s');
%Use to switch/case to determine the conversion factor
switch unit
case 'gallons'
factor=1;
case 'liters'
factor=0.264;
otherwise
disp('Not available')
factor=0;
end
%Ask the user how much gas he/she would like to buy
volume=input('Enter the volume you would like to buy in\n');
%Calculate the cost of the gas
if factor~=0
cost=volume*factor*rate;
%Send the results to the screen
fprintf('That will be $ %5.2f for %5.1f %s\n',cost,volume,unit)
end
(5)menu 结构
函数 menu 常和 switch/case 结构一起使用。运行后,在屏幕上会出现一个菜单,菜单的内容可以由编程人员设定。函数 menu 的语法结构为
input=menu(‘Message to the user’,’text for button 1’,’text for button 2’,etc.)在前面飞机票的例子中如果使用 menu,用户就可以在菜单上选择任意一个城市。由于用户不可能选择“not on file”作为城市名,因此原程序中可以去掉 otherwise 语句:
city=menu('Select a city from the menu:','Boston','Denver','Honolulu')
switch city
case 1
disp('$345')
case 2
disp('$150')
case 3
disp('Stay home and study')
end
注:使用菜单这种图形化界面可以大大减少用户出现拼写错误的可能性。同时在 case 语句中用序号代替了字符串。这个脚本程序运行后,屏幕上会出现如下图所示的菜单,用户可以选择任意一个选项,matlab 就会出现相关的结果。
加油问题,采用菜单方式
在上例中用 switch/case 实现了以加仑或升为单位加油的功能。程序中存在一个问题:即如果用户拼写错误,程序将无法运行。例如:当程序提示用户输入 gallons 或liters 时,用户错误的输入litters
程序将输出Not available
如果在程序中使用菜单功能,那么用户只需选择按键就可以完成操作,这样就避免了错误地发生。修改后的程序仍然采用 switch/case 结构,不过需要添加菜单功能。
1.问题描述
计算汽油的总价
2.输入/输出描述
输入使用菜单确定汽油的计量单位是加仑还是升
输出假设每加仑汽油2.89 美元,计算汽油的总价
clc,clear
%% Define the cost per gallon
rate=2.89;
%% Ask the user to input gallons or liters ,using a menu
disp('Use the menu box to make your selection')
choice=menu('Measure the gasoline in liters or gallons?','gallons','liters');
switch choice
case 1
factor=1;
unit='gallons';
case 2
factor=0.264;
unit='liter';
end
%% Ask the user how much gas he/she would like to by
volume=input(['Enter the volume you would like to buy in ',unit,':\n']);
%% Calculate the cost of the gas
cost=volume*factor*rate;
%% Send the results to the screen
fprintf('That will be $ %5.2f for %5.1f %s\n',cost ,volume ,unit)
(6)循环结构——for 循环
循环结构一般用于重复操作。Matlab 中有两种不同的循环结构:for 循环和 while 循环。如果已知循环的次数,多采用 for 循环。如果需要满足某一条件时停止循环,多采用 while 循环。虽然在程序开发过程中广泛采用循环结构,但是在编程时还是应该尽量避免出现循环结构。在 Matlab 中,可以使用 find 命令或矢量化的方法避免循环结构,所谓矢量化就是操作过程中尽量用矢量代替元素。减少循环结构有利于提高程序的运行速度,简化编程步骤。
for 循环结构非常简单。第一行标识循环,定义索引矩阵。循环体每执行一次,索引值都会发生一次变化。循环标识行后紧接着是执行的代码,最后是循环结束标志 end。总之,for 循环的一般结构如下:for index=[matrix]
commands to be executed
end
Eg: ①for k=[1 3 5]
k
end
②for k=1:3;
a=5.^k
end
提示
大部分计算机程序不具有 Matlab 中矩阵运算能力,因此常利用循环体定义数组。实际上,在 Matlab 中利用下述代码创建适量更简单。
①for k=1:5
a(k)=k.^2
end
②k=1:5,a=k.^2
for 循环常和if 语句配合使用
例如:在下面这段程序中,统计考试成绩为 90 分以上的有多少人?
scores=[76 45 98 97];
count=0;
for k=1:length(scores)
if scores(k)>90
count=count+1;
end
end
disp(count)
在大多数情况下,f o r循环中的索引矩阵是一个单行矩阵。如果索引矩阵是一个二维矩阵,m a t l a b每次
for k=[1 2 3;1 4 9;1 8 27]
a=k'
end
for 循环的用法总结:
●循环体以 for 语句开始,以 end 语句结束。
●循环体的第一行用索引矩阵定义循环的次数。
●for 循环的索引必须是一个变量,每执行一次循环,索引值就会发生变化。
●可以用前面介绍过的任何一种方法定义索引矩阵,但是一般情况下采用冒号方式定义。
●如果索引矩阵是一个行矢量,那么每执行循环一次,依次取矢量中的一个元素作为索引值。
●如果索引矩阵是二维矩阵,那么每执行循环一次,依次取矩阵中下一列的值,即在这种情况下索引值
是一个列矢量。
●for 循环结束后,索引值等于最后一次执行的数值。
●对程序代码矢量化可以避免 for 循环。
例题
用 for 循环计算阶乘
阶乘就是1 到N 所有的数相乘。例如5 的阶乘就是1*2*3*4*5=120。在数学中,常用感叹号表示阶乘:5!
就是 5 的阶乘。Matlab 的内置函数 factorial 就可以计算阶乘。现在我们编一小段程序来计算阶乘。
x=input('Enter a value for factorial\n');
a=1;
for k=1:x
a=a*k;
end
disp(a)
(7)循环结构——w h il e循环
while 循环和for 循环类似,两者的最大区别在于while 循环的次数是不固定的。只要判断条件为true,循环体就会被执行。while 循环的语法结构如下:
while criterion
commands to be executed
end
Eg: k=0;
while k<3
k=k+1;
end
disp(k)
实例
While 循环还可以检查用户的输入错误。例如:计算以 10 为底的对数时要求输入数据为正数。用 while 循环检查输入数据是否为正数,如果不是,提示用户输入正确的数值。程序一直提示用户直到输入正确有效的数据为止。
x=input('Enter a positive value of x\n');
while x<=0
disp('log(x) is not defined for negative numbers')
x=input('Enter a positive value of x\n');
end
y=log10(x)
(7)b r e ak和c o n t i nu e
break 命令可以提前终止循环。Break 语句一般嵌套于 while 或for 循环中,用于中止最内层循环。
n=0;
while n<10
n=n+1;
a=input('Enter a value greater than 0:');
if a<=0
disp('You must enter a positive number ')
disp('This program will terminate')
break
end
disp('The natural log of that number is ')
disp(log(a))
end
命令 continue 的用法和 break 类似,此命令的作用是结束本次循环,直接进入下一次循环。
n=0;
while n<10
n=n+1;
a=input('Enter a value greater than 0:');
if a<=0
disp('You must enter a positive number ')
disp('Try again')
continue
end
disp('The natural log of that number is ')
disp(log(a))
end
在这段程序中,如果输入负数,则执行下一次循环,直到 n 大于 10.