【正文】
ements and Program Design 10/23/2022 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 。 P l o t o f s i n ( x ) v s x 39。p l o t ( x , y ) 。 garbage 1 2 3 garbage (?1‘, ?2‘, ?3‘) Chapter 3 Branching Statements and Program Design 10/23/2022 Hangzhou Dianzi University 59 Only functions with character arguments can be treated as mands. Chapter 3 Branching Statements and Program Design 10/23/2022 Hangzhou Dianzi University 60 x = 2 * pi : pi / 20 : 2 * 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。endChapter 3 Branching Statements and Program Design 10/23/2022 Hangzhou Dianzi University 54 Quiz Chapter 3 Branching Statements and Program Design 10/23/2022 Hangzhou Dianzi University 55 Additional Plotting Features Chapter 3 Branching Statements and Program Design 10/23/2022 Hangzhou Dianzi University 56 Controlling x and yaxis Plotting Limits To display only the subset of the data that you are interest. axis mand/function Chapter 3 Branching Statements and Program Design 10/23/2022 Hangzhou Dianzi University 57 Command Description v = axis。 I l l eg al s ub s cr i pt : 39。 num 2 str ( a ( i nd ex ) ) ] ) 。 i nt 2 str ( i nd ex ) 39。 di s p ([ 39。 En t er s ub s cr i pt o f e l em en t t o di s pl ay : 39。 end Chapter 3 Branching Statements and Program Design 10/23/2022 Hangzhou Dianzi University 52 The try/catch Construct Special form branching construct designed to trap error. tr yS ta te m e nt 1 S ta te m e nt 2 T r y B l oc kc a tc hS ta te m e nt 1 S ta te m e nt 2 Ca tc h B l oc ke nd??????????Chapter 3 Branching Statements and Program Design 10/23/2022 Hangzhou Dianzi University 53 % I ni t i al i ze a r r aya = [ 1 3 2 5 ] 。The value is out of range.39。)。 case {2, 4, 6, 8, 10}, disp (39。The value is odd.39。)。 else disp (39。The grade is D.39。)。 else if grade disp (39。The grade is B.39。)。 end Chapter 3 Branching Statements and Program Design 10/23/2022 Hangzhou Dianzi University 45 (b) if grade disp (39。The grade is F.39。)。 elseif grade disp (39。The grade is C.39。)。 elseif grade disp (39。The grade is A.39。)。 else disp (39。This equation has two identical real roots.39。)。 ?try/catch Chapter 3 Branching Statements and Program Design 10/23/2022 Hangzhou Dianzi University 28 The if Construct if c o n tr o l _ e x p r _ 1S ta te m e n t 1S ta te m e n t 2 B l o c k 1e l se if c o n tr o l _ e x p r _ 2S ta te m e n t 1S ta te m e n t 2 B l o c k 2e l seS ta te m e n t 1S ta te m e n t 2 B l o c k 3e n d???????????????Chapter 3 Branching Statements and Program Design 10/23/2022 Hangzhou Dianzi University 29 There can be any number of elseif clause in an if construct, but there can be at most one else clause. Note that the MATLAB keyword end in this construct is different from the function end. Chapter 3 Branching Statements and Program Design 10/23/2022 Hangzhou Dianzi University 30 Quadratic equation ?If , 2 distinct real roots. ?If , single root. ?If , 2 plex roots 2 0a x b x c? ? ?2 42b b a cxa? ? ??2 40b ac??2 40b ac??2 40b ac??Chapter 3 Branching Statements and Program Design 10/23/2022 Hangzhou Dianzi University 31 if (b^2 4*a*c) 0 Write msg that equation has two plex roots. elseif (b^2 4*a*c) == 0 Write msg that equation has two identical real roots. else Write msg that equation has two distinct real roots. end Chapter 3 Branching Statements and Program Design 10/23/2022 Hangzhou Dianzi University 32 if (b^2 4*a*c) 0 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 numer