本科数模培训-matlab2(wyy).ppt
《本科数模培训-matlab2(wyy).ppt》由会员分享,可在线阅读,更多相关《本科数模培训-matlab2(wyy).ppt(40页珍藏版)》请在三一办公上搜索。
1、2023/10/15,1,第二部分 程序基本流程,Matlab,2023/10/15,2,1 关系运算符和逻辑运算符,运算符 运算=等于=不等于 大于=大于或等于 小于=小于或等于,2023/10/15,3,逻辑运算符&逻辑与|逻辑或xor 逻辑与或 逻辑非,2023/10/15,4,2023/10/15,5,2 选择结构(分支语句),选择结构的语句有if语句和switch语句。1 if语句 格式一:if 条件 语句组 end,格式二:if 条件 语句组1 else 语句组2 end,2023/10/15,6,【例】输入三角形的三条边,求面积。,A=input(请输入三角形的三条边:);if
2、A(1)+A(2)A(3)else disp(不能构成一个三角形。)end运行:请输入三角形的三条边:4 5 6 9.9216,2023/10/15,7,disp(This program solves for the roots of a quadratic);disp(equation of the form A*X2+B*X+C=0.);a=input(Enter the coefficient A:);b=input(Enter the coefficient B:);c=input(Enter the coefficient C:);%Calculate discriminantdi
3、scriminant=b2-4*a*c;if discriminant 0%there are two real roots,so.x1=(-b+sqrt(discriminant)/(2*a);x2=(-b-sqrt(discriminant)/(2*a);,【例】设计并编写一个程序,用来求解一元二次方程的根。,2023/10/15,8,disp(This equation has two real roots:);fprintf(x1=%fn,x1);fprintf(x2=%fn,x2);else if discriminant=0%there is one repeated root,s
4、o.x1=(-b)/(2*a);disp(This equation has two identical real roots:);fprintf(x1=x2=%fn,x1);else%there are complex roots,so.real_part=(-b)/(2*a);imag_part=sqrt(abs(discriminant)/(2*a);disp(This equation has complex roots:);fprintf(x1=%f+i%f n,real_part,imag_part);fprintf(x2=%f-i%f n,real_part,imag_part)
5、;end,2023/10/15,9,2switch语句 switch语句根据表达式的取值不同,分别执行不同的语句,其语句格式为:,2023/10/15,10,【例】某商场对顾客所购买的商品实行打折销售,标准如下(商品价格用price来表示):price200 没有折扣 200price500 3%折扣 500price1000 5%折扣 1000price2500 8%折扣 2500price5000 10%折扣5000price 14%折扣输入所售商品的价格,求其实际销售价格。,2023/10/15,11,程序如下:price=input(请输入商品价格);switch fix(price/
6、100)case 0,1%价格小于200 rate=0;case 2,3,4%价格大于等于200但小于500 rate=3/100;case num2cell(5:9)%价格大于等于500但小于1000 rate=5/100;case num2cell(10:24)%价格大于等于1000但小于2500 rate=8/100;case num2cell(25:49)%价格大于等于2500但小于5000 rate=10/100;otherwise%价格大于等于5000 rate=14/100;endprice=price*(1-rate)%输出商品实际销售价格,2023/10/15,12,whil
7、e语句的一般格式为:while(条件)循环体语句 end 其执行过程为:若条件成立,则执行循环体语句,执行后再判断条件是否成立,如果不成立则跳出循环。,3 while 循环,2023/10/15,13,例:统计问题一组数据的平均数(数学期望)和标准差。平均数的定义如下:其中xi 代表n 个样本中的第i 个样本。如果所有的输入数据都可以在一个数组中得到,这些数据的平均数就可以通过公式(4.1)直接计算出来,或应用MATLAB 的内建函数mean。标准差的定义如下:,2023/10/15,14,%Initialize sums.n=0;sum_x=0;sum_x2=0;%Read in first
8、 valuex=input(Enter first value:);%While Loop to read input values.while x=0%Accumulate sums.n=n+1;sum_x=sum_x+x;sum_x2=sum_x2+x2;%Read in next value x=input(Enter next value:);end,2023/10/15,15,%Check to see if we have enough input data.if n 2%Insufficient information disp(At least 2 values must be
9、 entered!);else%There is enough information,so%calculate the mean and standard deviation x_bar=sum_x/n;std_dev=sqrt(n*sum_x2-sum_x2)/(n*(n-1);%Tell user.fprintf(The mean of this data set is:%fn,x_bar);fprintf(The standard deviation is:%fn,std_dev);fprintf(The number of data points is:%fn,n);end,2023
10、/10/15,16,for语句的格式为:for 循环变量=表达式1:表达式2:表达式3 循环体语句 end 其中表达式1的值为循环变量的初值,表达式2的值为步长,表达式3的值为循环变量的终值。步长为1时,表达式2可以省略。,4 for 循环,2023/10/15,17,2023/10/15,18,2023/10/15,19,例:计算the day of year%Get day,month,and year to convertdisp(This program calculates the day of year given the);disp(current date.);month=in
11、put(Enter current month(1-12):);day=input(Enter current day(1-31):);year=input(Enter current year(yyyy):);%Check for leap year,and add extra day if necessaryif mod(year,400)=0 leap_day=1;%Years divisible by 400 are leap yearselseif mod(year,100)=0 leap_day=0;%Other centuries are not leap yearselseif
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 本科 数模 培训 matlab2 wyy
![提示](https://www.31ppt.com/images/bang_tan.gif)
链接地址:https://www.31ppt.com/p-6300666.html