




已阅读5页,还剩35页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
2019/6/5,1,第二部分 程序基本流程,Matlab,2019/6/5,2,1 关系运算符和逻辑运算符,运算符 运算 = 等于 = 不等于 大于 = 大于或等于 小于 = 小于或等于,2019/6/5,3,逻辑运算符 & 逻辑与 | 逻辑或 xor 逻辑与或 逻辑非,2019/6/5,4,2019/6/5,5,2 选择结构(分支语句),选择结构的语句有if语句和switch语句。 1 if语句 格式一: if 条件 语句组 end,格式二: if 条件 语句组1 else 语句组2 end,2019/6/5,6,【例】 输入三角形的三条边,求面积。,A=input(请输入三角形的三条边:); if A(1)+A(2)A(3) else disp(不能构成一个三角形。) end 运行: 请输入三角形的三条边:4 5 6 9.9216,2019/6/5,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 discriminant discriminant = 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);,【例】设计并编写一个程序,用来求解一元二次方程的根。,2019/6/5,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, so . 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); end,2019/6/5,9,2switch语句 switch语句根据表达式的取值不同,分别执行不同的语句,其语句格式为:,2019/6/5,10,【例】某商场对顾客所购买的商品实行打折销售,标准如下(商品价格用price来表示): price200 没有折扣 200price500 3%折扣 500price1000 5%折扣 1000price2500 8%折扣 2500price5000 10%折扣 5000price 14%折扣 输入所售商品的价格,求其实际销售价格。,2019/6/5,11,程序如下: price=input(请输入商品价格); switch fix(price/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; end price=price*(1-rate) %输出商品实际销售价格,2019/6/5,12,while语句的一般格式为: while (条件) 循环体语句 end 其执行过程为:若条件成立,则执行循环体语句,执行后再判断条件是否成立,如果不成立则跳出循环。,3 while 循环,2019/6/5,13,例:统计问题 一组数据的平均数(数学期望)和标准差。平均数的定义如下: 其中xi 代表n 个样本中的第i 个样本。如果所有的输入数据都可以在一个数组中得到,这些数据的平均数就可以通过公式(4.1)直接计算出来,或应用MATLAB 的内建函数mean。 标准差的定义如下:,2019/6/5,14,% Initialize sums. n = 0; sum_x = 0; sum_x2 = 0; % Read in first value x = 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,2019/6/5,15,% Check to see if we have enough input data. if n 2 % Insufficient information disp(At least 2 values must be 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,2019/6/5,16,for语句的格式为: for 循环变量=表达式1:表达式2:表达式3 循环体语句 end 其中表达式1的值为循环变量的初值,表达式2的值为步长,表达式3的值为循环变量的终值。步长为1时,表达式2可以省略。,4 for 循环,2019/6/5,17,2019/6/5,18,2019/6/5,19,例: 计算the day of year % Get day, month, and year to convert disp(This program calculates the day of year given the ); disp(current date.); month = input(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 necessary if mod(year,400) = 0 leap_day = 1; % Years divisible by 400 are leap years elseif mod(year,100) = 0 leap_day = 0; % Other centuries are not leap years elseif mod(year,4) = 0 leap_day = 1; % Otherwise every 4th year is a leap year else leap_day = 0; % Other years are not leap years end,2019/6/5,20,% Calculate day of year by adding current day to the % days in previous months. day_of_year = day; for ii = 1:month - 1 % Add days in months from January to last month switch (ii) case 1,3,5,7,8,10,12, day_of_year = day_of_year + 31; case 4,6,9,11, day_of_year = day_of_year + 30; case 2, day_of_year = day_of_year + 28 + leap_day; end end % Tell user fprintf(The date %2d/%2d/%4d is day of year %d.n, . month, day, year, day_of_year);,2019/6/5,21,5 break语句和continue语句,break语句用于终止循环的执行。当在循环体内执行到该语句时,程序将跳出循环,继续执行循环语句的下一语句。 continue语句控制跳过循环体中的某些语句。当在循环体内执行到该语句时,程序将跳过循环体中所有剩下的语句,继续下一次循环。,2019/6/5,22,例 求100,200之间第一个能被21整除的整数。 程序如下: for n=100:200 if rem(n,21)=0 continue end break end n,2019/6/5,23,6 循环的嵌套,如果一个循环结构的循环体又包括一个循环结构,就称为循环的嵌套,或称为多重循环结构。,2019/6/5,24,例: 若一个数等于它的各个真因子之和,则称该数为完数,如6=1+2+3,所以6是完数。求1,500之间的全部完数。 for m=1:500 s=0; for k=1:m/2 if rem(m,k)=0 s=s+k; end end if m=s disp(m); end end,2019/6/5,25,8 逻辑数组与向量化,“逻辑”数据类型在MATLAB 中并不真实存在。其实,它是带特定逻辑属性标准数字型数据类型。逻辑型数组通过所有的关系运算符和逻辑运算符创建。它们区别于数字型的是在调用whos 命令时,(logical)会出现在类型的后面。,2019/6/5,26,当调用whos 命令时,结果如下。注意b 后面的(logical)修饰符。,2019/6/5,27,语句c=logical(a),将会把a 值赋于c,从而使c 带有一定的逻辑性。 一个数组的逻辑属性可以通任何的数学运算去除。例如,如果我们在c 数组加0,数组的值不会改变,而它的逻辑属性将会消失,2019/6/5,28,逻辑数组的作用 算术运算中能提供一个屏蔽(mask)。 屏蔽(mask)是指一个数组,它从另一个数组选择所需的元素参与运算。指定的运算只在选择的元素上执行,而不执行原有的元素。,2019/6/5,29,2019/6/5,30,用循环结构和选择结构计算上述问题 然用逻辑数组的方法运算速度要快得多,2019/6/5,31,例:用最小二乘法画噪声数据的近似曲线 下落物体将会作匀加速度运动,它的速度符合下面的公式 v(t) = at + v0 (4.3) v(t)代表物体在t 时刻的速度。加速度为g,初速度v0 为0。,2019/6/5,32,如果我们确定了待定系数m 和b,那么我们就确定了解析式4.4。 y=mx+b (4.4) 确定待定系数m 和b 的标准方法为最小二乘法。之所以称为最小二乘法,是因为根据偏差的平方和为最小的条件来选择常数m 和b 的。公式如下:,2019/6/5,33,其中,x 代表所有测量值x 之和,y 代表所有测量值y 之和,xy 代表所有对应的x与y 的乘积之和, 代表测值量x 的数学期望。 代表测值量y 的数学期望。已知有一系列含有噪声的数据(x,y),编写程序用最小二乘法计算出m 和b。数据要求从键盘输入,画出每一个数据点还有画出最适合的直线。,2019/6/5,34,设计算法 这个问题被分解为6 个大步骤: Get the number of input data points Read the input data values Calculate the required statistics Calculate the slop and intercept Write out the slop and intercept Plot the input points and the fitted line,2019/6/5,35,% Script file: lsqfit.m,disp(This program performs a leastsquares fit of an ); disp(input data set to a straight line.); n_points = input(Enter the number of input x y points: ); % Read the input data for ii = 1:n_points temp = input(Enter x y pair: ); x(ii) = temp(1); y(ii) = temp(2); end,2019/6/5,36,% Accumulate statistics sum_x = 0; sum_y = 0; sum_x2 = 0; sum_xy = 0; for ii = 1:n_points sum_x = sum_x + x(ii); sum_y = sum_y + y(ii); sum_x2 = sum_x2 + x(ii)2; sum_xy = sum_xy + x(ii) * y(ii); end % Now calculate the slope and intercept. x_bar = sum_x / n_points; y_bar = sum_y / n_points; slope = (sum_xy - sum_x * y_bar) / ( sum_x2 - sum_x * x_bar); y_int = y_bar - slope * x_bar;,2019/6/5,37,% Tell user. disp(Regression coefficients for the leastsquares line:); fprintf( Slope (m) = %8.3fn, slope); fprintf( Intercept (b) = %8.3fn, y_int); fprintf( No of points = %8dn, n_points); % Plot the data points as blue circles with no connecting lines. plot(x,y,bo); hold on; % Create the fitted line xmin = min(x); xmax = max(x); ymin = slope * xmin + y_int; ymax = slope * xmax + y_int;,2019/6/5,38,% Plot a solid red line with no markers plot(xmin xmax,ymin ymax,r,LineWidth,2); hold off; % Add a
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年骨科中医护理试题
- 围产期护理试题及答案
- 危重患儿护理试题及答案
- 王者荣耀真实试题及答案
- 推拿手法考试题及答案
- 图书情报试题及答案
- 2025年电机维修策划及质量控制协议
- 2025年足彩合买代购契约协议样本
- 2025年幼儿园重建改造合作协议模板
- 企业跨境运营中的合规挑战
- 配电柜维护保养施工方案范文
- 2025年山东东营市事业单位上半年统考(7.26)历年自考难、易点模拟试卷(共500题附带答案详解)
- 管理学基础-形考任务二-国开-参考资料
- 投标标前协议书范本
- 注塑领班工作总结
- 全面指南:2024年医学整形美容医院员工手册
- 2025年中国经济信息社福建分公司招聘笔试参考题库含答案解析
- 2025年度食用菌产业园区公共设施运营管理合同3篇
- 《费孝通-乡土中国》差序格局
- 2023-2024学年天津市和平区八年级(下)期末数学试卷(含答案)
- 2021去远方上海研学旅行方案申请及综合反思表
评论
0/150
提交评论