同济大学:《Matlab在机械设计中的应用》课程电子教案(PPT课件)Chapter 04 Branches and Loops

Branches and Loops (1/42 Branches >Branches are used to select and execute specific sections of the code while skipping other sections >Selection of different sections depend on a condition statement >We will learn: √if statement √ switch statement CPHAW try/catch statement @月协大学 TONGJI UNIVERSITY
Branches and Loops (1/42) Branches ➢ Branches are used to select and execute specific sections of the code while skipping other sections ➢ Selection of different sections depend on a condition statement ➢ We will learn: ✓ if statement ✓ switch statement ✓ try/catch statement

Branches and Loops (2/42 Branch:if >if'Statement condition false if condition ) statement 1 statement 2 true statement end group @日济大学 TONGJI UNIVERSITY
Branches and Loops (2/42) Branch: if ➢ ‘if’ Statement condition statement group true false if ( condition ), statement 1 statement 2 ... end

Branches and Loops (3/42 Branch:if Conditions can be: any real value (0 is false,non-zero is true) combination of relational and logical operators ·e.g.(x>0)&(x<10) √logical functions ·isempty() isnumericO,ischar() isinf(),isnan() exist() CPHAW @月协大学 TONGJI UNIVERSITY
Branches and Loops (3/42) Branch: if ➢ Conditions can be: ✓ any real value (0 is false, non-zero is true) ✓ combination of relational and logical operators • e.g. ( x > 0 ) & ( x < 10 ) ✓ logical functions • isempty() • isnumeric(), ischar() • isinf(), isnan() • exist()

Branches and Loops (4/42 Branch:if Examples: √if(rl00), disp([Grade must be in [0,100]range']) end √if isinf(result), disp(Result is infinite'); end @日济大学 AW TONGJI UNIVERSITY
Branches and Loops (4/42) Branch: if Examples: ✓ if ( r 100 ) ), disp( [ ‘Grade must be in [0,100] range’ ] ); end ✓ if isinf( result ), disp( ‘Result is infinite’ ); end

Branches and Loops (5/42 Branch:if >Water tank example: r=input('Enter the radius of the tank base(in meters):); if(r0), disp([There is'num2str(space)'m3 extra space']); else @月停大学 disp(Tank is full'); TONGJI UNIVERSITY end
Branches and Loops (5/42) Branch: if ➢ Water tank example: r = input('Enter the radius of the tank base (in meters):'); if ( r 0 ), disp( [ 'There is ' num2str(space) 'm3 extra space' ] ); else disp( 'Tank is full' ); end

Branches and Loops (6/42 Branch:if > if-else if(condition), statement 1 true condition false statement 2 statement group 1 else statement statement statement 1 group 1 group 2 statement 2 statement group 2 end PHAW @日济大学 TONGJI UNIVERSITY
Branches and Loops (6/42) Branch: if condition statement group 1 true false statement group 2 if ( condition ), statement 1 statement 2 ... else statement 1 statement 2 ... end ➢ if-else statement group 1 statement group 2

Branches and Loops(7/42 Branch:if > if-else if(condition 1 ) statement1 statement 2 statement true condition false group 1 1 elseif condition 2 ) statement 1 statement group 1 true condition false statement2 statement 2 group 2 else statement statement group 2 group 3 statement 1 statement statement 2 group 3 CPHAW end @月儕大学 TONGJI UNIVERSITY
Branches and Loops (7/42) Branch: if condition 1 statement group 1 true false condition 2 statement group 2 statement group 3 true false if ( condition 1 ), statement 1 statement 2 ... elseif ( condition 2 ), statement 1 statement 2 ... else statement 1 statement 2 ... end statement group 1 statement group 2 statement group 3 ➢ if-else

Branches and Loops (8/42 Branch:if > Example: Finding roots of the quadratic equation "ax2+bx +c=0" Pseudocode: d b2-4ac ifd>0, two real roots else if d=0, two identical roots else two complex roots @月济大学 TONGJI UNIVERSITY
Branches and Loops (8/42) Branch: if ➢ Example: Finding roots of the quadratic equation “ax2 + bx + c = 0” ➢ Pseudocode: d = b2 – 4ac if d > 0, two real roots else if d == 0, two identical roots else two complex roots

Branches and Loops (9/42 Branch:if Prompt the user for the coefficients of the equation disp ("This program finds 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 the coefficient C:') Calculate discriminant discriminant =b2-4 a c; Solve for the roots,depending on the value of the discriminant if discriminant >0%there are two real roots,so... x1 =(-b +sgrt(discriminant))/(2 a ) x2 =(-b-sgrt(discriminant))/(2 *a ) disp ('This equation has two real roots:) fprintf ('xI =%fn',x1); fprintf ('x2 =%fn',x2): elseif discriminant =0%there is one repeated root,so... x1=(-b)/(2*a方 disp ('This equation has two identical real roots:) fprintf ('xI =x2 =%f n',x1); else there are complex roots,so... real_part =(-b)/(2 a); imag part=sqt(abs(discriminant))/(2*a)方 disp ("This equation has complex roots:) fprintf('x1 =%f+i%fn',real_part,imag_part ) 细月济大学 TONGJI UNIVERSITY fprintf('x2=%f-i%fn',real_part,imag_part ) end
Branches and Loops (9/42) Branch: if % Prompt the user for the coefficients of the equation disp ('This program finds 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 the coefficient C: '); % Calculate discriminant discriminant = b^2 - 4 * a * c; % Solve for the roots, depending on the value of the discriminant if discriminant > 0 % there are two real roots, so... x1 = ( -b + sqrt(discriminant) ) / ( 2 * a ); x2 = ( -b - sqrt(discriminant) ) / ( 2 * a ); disp ('This equation has two real roots:'); fprintf ('x1 = %f\n', x1); fprintf ('x2 = %f\n', x2); elseif discriminant == 0 % there is one repeated root, so... x1 = ( -b ) / ( 2 * a ); disp ('This equation has two identical real roots:'); fprintf ('x1 = x2 = %f\n', 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

Branches and Loops (10/42 Branch:if if-else statement and nested if statement Example:Assigning letter grades Range Grade 100≥grade>95 A 95≥grade>86 B 86≥grade>76 C 76≥grade>66 D 66≥grade>0 F How can we compute the letter corresponding to a given numeric grade? 翻日济大学 TONGJIUNIVERSITY
Branches and Loops (10/42) Branch: if ➢ if-else statement and nested if statement Example: Assigning letter grades How can we compute the letter corresponding to a given numeric grade? Range Grade 100 grade > 95 A 95 grade > 86 B 86 grade > 76 C 76 grade > 66 D 66 grade > 0 F
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 同济大学:《Matlab在机械设计中的应用》课程电子教案(PPT课件)Chapter 03Top-down and bottom-up design.ppt
- 同济大学:《Matlab在机械设计中的应用》课程电子教案(PPT课件)Chapter 02 MATLAB Basics(负责人:陈明).ppt
- 同济大学:《Matlab在机械设计中的应用》课程电子教案(PPT课件)Chapter 01 MATLAB Programming for Mechanical Engineering(Introduction to MATLAB).ppt
- 同济大学:《Matlab在机械设计中的应用》课程教学资源(试卷习题)Final Examination(B)The First Semester(2013-2014).pdf
- 同济大学:《Matlab在机械设计中的应用》课程教学资源(试卷习题)Final Examination(A)The First Semester(2013-2014).pdf
- 运城学院:《机械原理》课程教学资源(课件讲稿)第6章 齿轮.pdf
- 运城学院:《机械原理》课程教学资源(课件讲稿)第2章 平面机构的结构分析.pdf
- 运城学院:《机械原理》课程教学资源(课件讲稿)第8章 其他常见机构.pdf
- 运城学院:《机械原理》课程教学资源(课件讲稿)第9章 机械中的摩擦与效率.pdf
- 运城学院:《机械原理》课程教学资源(课件讲稿)第5章 凸轮机构.pdf
- 运城学院:《机械原理》课程教学资源(课件讲稿)第10章 机械的平衡.pdf
- 运城学院:《机械原理》课程教学资源(课件讲稿)第7章 轮系(Gear Train).pdf
- 运城学院:《机械原理》课程教学资源(课件讲稿)第3章 平面机构的运动分析(Kinematic Analysis).pdf
- 运城学院:《机械原理》课程教学资源(课件讲稿)第4章 平面四杆机构.pdf
- 运城学院:《机械原理》课程教学资源(课件讲稿)第1章 绪论(负责人:倪娟).pdf
- 运城学院:《机械原理》课程教学资源(教案讲义)第1章 绪论(打印版).pdf
- 运城学院:《机械原理》课程教学资源(教案讲义)第9章 机械中的摩擦和效率.doc
- 运城学院:《机械原理》课程教学资源(教案讲义)第8章 其他常用机构.doc
- 运城学院:《机械原理》课程教学资源(教案讲义)第11章 机械系统运动方案的设计.doc
- 运城学院:《机械原理》课程教学资源(教案讲义)第10章 机械的平衡.doc
- 同济大学:《Matlab在机械设计中的应用》课程电子教案(PPT课件)Chapter 05 Plotting.ppt
- 同济大学:《Matlab在机械设计中的应用》课程电子教案(PPT课件)Chapter 06 User-defined Functions.ppt
- 同济大学:《Matlab在机械设计中的应用》课程电子教案(PPT课件)Chapter 07 Sparse Arrays, Cell Arrays, and Structures.ppt
- 同济大学:《Matlab在机械设计中的应用》课程电子教案(PPT课件)Chapter 08 Advanced Mathematics.ppt
- 同济大学:《Matlab在机械设计中的应用》课程电子教案(PPT课件)Chapter 09 Probability and statistics.ppt
- 同济大学:《Matlab在机械设计中的应用》课程电子教案(PPT课件)Chapter 10 Graphical User Interface.ppt
- 同济大学:《Matlab在机械设计中的应用》课程电子教案(PPT课件)Chapter 11 Simulink.pptx
- 同济大学:《汽车构造》课程电子教案(课件讲稿)第一章 内燃机工作原理及总体构造 The Working Principles and Overall Structure of Internal Combustion Engines.pdf
- 同济大学:《汽车构造》课程电子教案(课件讲稿)第二章 机体组及曲柄连杆机构 Engine Block, Crank and Connecting Rod Mechanism.pdf
- 同济大学:《汽车构造》课程电子教案(课件讲稿)第三章 配气机构 Valve Trains(负责人:李理光).pdf
- 同济大学:《汽车构造》课程电子教案(课件讲稿)第四章 汽油机燃油供给系统 Fuel Supply System For Gasoline Engine.pdf
- 同济大学:《汽车构造》课程电子教案(课件讲稿)第六章 进气、排气及增压系统 Intake, Exhaust and Boost Systems.pdf
- 同济大学:《汽车构造》课程电子教案(课件讲稿)第五章 柴油机燃油供给系统 Fuel Supply System for Diesel Engines.pdf
- 同济大学:《汽车构造》课程电子教案(课件讲稿)第七章 发动机冷却系 Cooling System.pdf
- 同济大学:《汽车构造》课程电子教案(课件讲稿)第九章 起动系统 Starting System.pdf
- 同济大学:《汽车构造》课程电子教案(课件讲稿)第八章 发动机润滑系 Lubrication System for Automotive Engines.pdf
- 同济大学:《汽车构造》课程电子教案(课件讲稿)第十章 发动机点火系统 Engine Ignition System.pdf
- 同济大学:《汽车构造》课程电子教案(课件讲稿)第十二章 发动机有害排放物的控制系统 Control System of Harmful Emissions in Engine Exhaust.pdf
- 同济大学:《汽车构造》课程电子教案(课件讲稿)Automobile structure(Types of modern automobiles).pdf
- 同济大学:《汽车构造》课程电子教案(课件讲稿)Chapter 13 传动系统 Overview of automobile drivetrain.pdf