【正文】
22 Hangzhou Dianzi University 61 Chapter 3 Branching Statements and Program Design 10/23/2022 Hangzhou Dianzi University 62 l i m i t s = axisl i m i t s = 8 8 1 1 a x i s ([ 0 pi 0 1 ])Chapter 3 Branching Statements and Program Design 10/23/2022 Hangzhou Dianzi University 63 Chapter 3 Branching Statements and Program Design 10/23/2022 Hangzhou Dianzi University 64 Plotting Multiple Plots on the Same Axes hold on/off Example x = pi : pi / 20 : pi 。 This function sets the x and y limits of the plot to the specified values. axis equal This mand sets the axis increments to be equal on both axes. axis square This mand makes the current axis box square. axis normal This mand cancels the effect of axis equal and axis square. axis off This mand turns off all axis labeling, tick marks, and background. axis on This mand turns on all axis labeling, tick marks, and background. Chapter 3 Branching Statements and Program Design 10/23/2022 Hangzhou Dianzi University 58 Good Programming Practice MATLAB mands are really implemented by functions, and the MATLAB interpreter is smart enough to substitute the function call whenever it encounters the mand. It is always possible to call the mand directly as a function instead of using the mand syntax. axis on。 i nt 2 str ( i nd ex ) 39。The value is out of range.39。)。 else if grade disp (39。The grade is F.39。)。 else disp (39。 value3) 0 1 0 1 1 0 Chapter 3 Branching Statements and Program Design 10/23/2022 Hangzhou Dianzi University 25 Logical Functions Function Purpose ischar (a) Returns a 1 if a is a character array and a 0 otherwise. isempty (a) Returns a 1 if a is an empty array and a 0 otherwise. isinf (a) Returns a 1 if the value of is Inf and a 0 otherwise. isnan (a) Returns a 1 if the value of a is NaN and a 0 otherwise. isnumeric (a) Returns a 1 if the a is a numeric array and a 0 otherwise. Chapter 3 Branching Statements and Program Design 10/23/2022 Hangzhou Dianzi University 26 Quiz Chapter 3 Branching Statements and Program Design 10/23/2022 Hangzhou Dianzi University 27 Branches ?if。 a = = b ans = ? ans = 0 sin (pi) = 1016 Chapter 3 Branching Statements and Program Design 10/23/2022 Hangzhou Dianzi University 18 Roundoff Errors: During puter calculations, two theoretically equal numbers can differ slightly, causing an equality or inequality test to fail. Solution: abs (a b) ans = 1 Chapter 3 Branching Statements and Program Design 10/23/2022 Hangzhou Dianzi University 19 Good Programming Practice Be cautious about testing for equality with numeric values, since roundoff errors can cause two variables that should be equal to fail a test for equality. Instead, test to see if the variables are nearly equal within the roundoff error to be expected on the puter you are working with. Chapter 3 Branching Statements and Program Design 10/23/2022 Hangzhou Dianzi University 20 Logic Operators Three binary operators: AND, OR, XOR l1 op l2 One unary operators: NOT op l1 Operator Operation amp。10/23/2022 1 Chapter 3 Branching Statements and Program Design Chapter 3 Branching Statements and Program Design 10/23/2022 Hangzhou Dianzi University 2 Se qu en t i al P ro gr am s — F i xe d or de rCo nt ro l t he o rd erBranche s Loo psCo m pl ex p ro gr amA f or m al p ro gr am d es i gn p ro ce du re — Top do wn Des i gnChapter 3 Branching Statements and Program Design 10/23/2022 Hangzhou Dianzi University 3 Introduction to TopDown Design Techniques What‘s the most difficult part of program job? ans: To understand the problem you are trying to solve. Chapter 3 Branching Statements and Program Design 10/23/2022 Hangzhou Dianzi University 4 Topdown design It is the process of starting with a large task and breaking down into smaller, more easily understandable pieces (subtasks), which perform a portion of the desired task. Each subtask may in turn be subdivided into smaller subtasks if necessary. Each subtask can be coded an tested independently. Chapter 3 Branching Statements and Program Design 10/23/2022 Hangzhou Dianzi University 5 St ar tSt ar t t he p r og r am y ou ar e t r yi ng t o so l veDef i ne r eq ui r ed i np ut s an d ou t pu t sDes i gn t he a l go r i t hmCo nv er t a l go r i t hm i nt o M ATLAB s t at em en t sTe st t he r es ul t i ng M ATLAB p r og r amDec om po si t i onSt ep wi se r ef i ne m en tFi ni sh ed !Chapter 3 Branching Statements and Program Design 10/23/2022 Hangzhou Dianzi University 6 1. Clearing state the problem that you are trying to solve. 2. Define the inputs required by the program and the outputs to be produced by the program. 3. Design the algorithm that you intend to implement in the program. ? deposition ? stepwise refinement pseudocode 4. Turn the algorithm into MATLAB statements. 5. Test the resulting MATLAB program. Chapter 3 Branching Statements and Program Design 10/23/2022 Hangzhou Dianzi University 7 St ar tUni t te st in g of individu al subta sksSu cces si ve bu il ds( add in g su bt as ks t o th e p r og r am )Beta r el eas eFi ni sh ed pr og r am !Su bt as ks v al id at ed se par at el yAs m any t im es as n eces sa r ySu bt as ks co m bi ned i nt o a s in gl e p r og r amAlp ha r el eas e As m any t im es as n