Presentation is loading. Please wait.

Presentation is loading. Please wait.

February 14, 2005 Lecture 9 - By Paul Lin 1 CPET 190 Problem Solving with MATLAB Lecture 9

Similar presentations


Presentation on theme: "February 14, 2005 Lecture 9 - By Paul Lin 1 CPET 190 Problem Solving with MATLAB Lecture 9"— Presentation transcript:

1 February 14, 2005 Lecture 9 - By Paul Lin 1 CPET 190 Problem Solving with MATLAB Lecture 9 http://www.etcs.ipfw.edu/~lin

2 February 14, 2005 Lecture 9 - By Paul Lin 2 Lecture 9: MATLAB Program Design & Flow Control II 9-1 Control Flow – if, else, elseif, endif, else, elseif, end switch, case, otherwiseswitch, case, otherwise forfor whilewhile break, continuebreak, continue 9-2 Loop Control forfor whilewhile break, continuebreak, continue 9-3 Conditional Statements if, else, elseif, endif, else, elseif, end

3 February 14, 2005 Lecture 9 - By Paul Lin 3 9-1 Control Flow MATLAB Control Flow (keywords) if, else, elseif, endif, else, elseif, end switch, case, otherwiseswitch, case, otherwise forfor whilewhile break, continuebreak, continue Specifying conditions and selections Specifying conditions and selections If, else, elseif, endIf, else, elseif, end switch-case statementsswitch-case statements Repeating a set of commands or functions Repeating a set of commands or functions ForFor whilewhile

4 February 14, 2005 Lecture 9 - By Paul Lin 4 9-2 Loop Control FOR Loop FOR Repeat statements a specific number of times. FOR Repeat statements a specific number of times. The general form of a FOR statement is: The general form of a FOR statement is: FOR variable = n:s:m statement,..., statement END where variable is the loop counter, n is the initial value, s is the loop counter increment or decrement size, m is the final value where variable is the loop counter, n is the initial value, s is the loop counter increment or decrement size, m is the final value Example: Example: for n = 1:10 for n = 1:10 statement 1 statement 2 statement 2.... statement n statement n end end

5 February 14, 2005 Lecture 9 - By Paul Lin 5 9-2 Loop Control Example 9-1: Nested For Loops % multiplytable.m N = 9; A = zeros(N); for row = 1:N, for col = 1:N, for col = 1:N, A(row, col) = A(row, col) = row * col; end endenddisp(A) 1 2 3 4 5 6 7 8 9 2 4 6 8 10 12 14 16 18 3 6 9 12 15 18 21 24 27 4 8 12 16 20 24 28 32 36 5 10 15 20 25 30 35 40 45 6 12 18 24 30 36 42 48 54 7 14 21 28 35 42 49 56 63 8 16 24 32 40 48 56 64 72 9 18 27 36 45 54 63 72 81

6 February 14, 2005 Lecture 9 - By Paul Lin 6 9-2 Loop Control Example 9-2: While loop %whileloop.m n = 1; while n < 6 disp(n) disp(n) n = n + 1; n = n + 1;end Output: 1 2 3 4 WHILE Loop - WHILE Repeat statements an indefinite number of times. The general form of a WHILE statement is: WHILE expression statements statementsEND

7 February 14, 2005 Lecture 9 - By Paul Lin 7 9-2 Loop Control Terminate Loop or Exit Conditional Statements Break – stop execution of loopBreak – stop execution of loop Continue – Exit the inner loop and continuing on outer loopContinue – Exit the inner loop and continuing on outer loop Example 9-3 %break_continue.m k = 1; while k < 10 x = 10 - k^2; x = 10 - k^2; if x < 0 if x < 0 break break end end y = sqrt(x) y = sqrt(x) k = k + 1; k = k + 1;end fprintf('The %d time in the loop -> x less than zero = %d\n', k, x); %The 4th time in the loop -> x %less than zero = -6

8 February 14, 2005 Lecture 9 - By Paul Lin 8 9-2 Loop Control Continue Statement Continue – Pass control to the next iteration of the FOR or WHILE loopContinue – Pass control to the next iteration of the FOR or WHILE loop Example 9-4 %continue_ex.m for k=1:6 if k == 4; if k == 4; continue continue end end fprintf('k = %d\n', k); fprintf('k = %d\n', k);end disp('End of loop') Output: k = 1 k = 2 k = 3 k = 5 k = 6 End of loop

9 February 14, 2005 Lecture 9 - By Paul Lin 9 9-3 Conditional Statements Three Forms of if Statements Three Forms of if Statements if-endif-end if-else-endif-else-end if-elseif-else-endif-elseif-else-end IF- END format IF- END format Single statementSingle statement if condition if condition statement statementend Multiple statementsMultiple statements if condition Statement_1; Statement_2; Statement_2; …. …. Statement_n; Statement_n;end

10 February 14, 2005 Lecture 9 - By Paul Lin 10 9-3 Conditional Statements Example 9-5: compute only if x ≥ 0. %inf_cond1.m x = input('Enter a number: ') y_complex = sqrt(x) if x >= 0 y_real = sqrt(x) y_real = sqrt(x)end % x = 2 % y_complex = 1.4142 % y_real = 1.4142 % x = -2 % y_complex = 0 + 1.4142i

11 February 14, 2005 Lecture 9 - By Paul Lin 11 9-3 Conditional Statements Example 9-6: A Grade Calculation Program – Problem Statement Example 9-6: A Grade Calculation Program – Problem Statement Compute average grade of three exams (input from keyboard), then calculate and print a letter grade (on the screen) based on the following policy: Compute average grade of three exams (input from keyboard), then calculate and print a letter grade (on the screen) based on the following policy: A grade: Average >= 90 B grade: 80 <= Avg < 90 C grade: 70 <= Avg <80 D grade: 60 <= Avg < 70 F grade: Avg < 60

12 February 14, 2005 Lecture 9 - By Paul Lin 12 9-3 Conditional Statements Example 9-6: A Grade Calculation Program – using Flowchart Example 9-6: A Grade Calculation Program – using Flowchart

13 February 14, 2005 Lecture 9 - By Paul Lin 13 9-3 Conditional Statements Example 9-6: A Grade Calculation Program – Solution Example 9-6: A Grade Calculation Program – Solution%grade_prog.m t1 = input('Enter Test 1 Score: ') t2 = input('Enter Test 2 Score: ') t3 = input('Enter Test 3 Score: ') % Grade calculation avg = (t1 + t2 + t3)/3; if avg >= 90 Grade = 'A'; Grade = 'A';end if (avg >= 80) & (avg = 80) & (avg < 90) Grade = 'B'; Grade = 'B';end if (avg >= 70) & (avg = 70) & (avg < 80) Grade = 'C'; Grade = 'C';end if (avg >= 60) & (avg = 60) & (avg < 70) Grade = 'D'; Grade = 'D';end if avg < 60 Grade = 'F'; Grade = 'F';end fprintf('Your average score is %g.\n', avg); fprintf('Your grade grade is %c.\n', Grade);

14 February 14, 2005 Lecture 9 - By Paul Lin 14 9-3 Conditional Statements IF-ELSEIF-ELSE-END if expression statements statements elseif expression statements statementselse end if expression statements_1 statements_1 elseif elseif statements_2 statements_2 elseif elseif statements_3 statements_3 else else statements_4 statements_4 end end IF-ELSE-END Construct: if expression statements_1 statements_1else statements_2 statements_2end

15 February 14, 2005 Lecture 9 - By Paul Lin 15 9-3 Conditional Statements Example 9-7: A Full Wave Rectifier

16 February 14, 2005 Lecture 9 - By Paul Lin 16 9-3 Conditional Statements Example 9-7: A Full Wave Rectifier Simulation using IF-END and FOR loop. Pusedocode Transformer voltage ratio 110/10 volts Transformer voltage ratio 110/10 volts e1 = 10*sin(2*pi*f*t); f = 60Hz e1 = 10*sin(2*pi*f*t); f = 60Hz Rectifying signal e1 Rectifying signal e1 If e1 < 0 e1 = -e1; e1 = -e1;end Output – Full-wave rectified voltage (plot) Output – Full-wave rectified voltage (plot)

17 February 14, 2005 Lecture 9 - By Paul Lin 17 9-3 Conditional Statements Example 9-7: Solution %full_rec.m % Primary side voltage Vp = 110; % secondary side voltage Vs = 10; % Frequency is 60 Hz f = 60; % Period is 16.67 ms T = 1/f; dt = 10E-3*T; % For showing 2 cycles t = 0:dt:T; e1 =Vs*sin(2*pi*f*t); len = length(e1); % Rectifier operation for n = 1: len, if e1(n) >= 0 if e1(n) >= 0 e2(n) = e1(n); e2(n) = e1(n); else else e2(n) = -e1(n); e2(n) = -e1(n); end endend % continue on next % slide

18 February 14, 2005 Lecture 9 - By Paul Lin 18 9-3 Conditional Statements Example 9-7: Solution (cont.) %full_rec.m figure(1), plot(t, e2, 'r', t, e1,'b--'), grid on title('Full Wave Rectifier') xlabel('Time in sec') ylabel('Volts') figure(2), subplot(2,1,1), plot(t, e1), grid on title('Input signal') xlabel('Time in sec') ylabel('Volts') figure(2), subplot(2,1,2), plot(t, e2), grid on title('Full Wave Rectified Signal') xlabel('Time in sec') ylabel('Volts')

19 February 14, 2005 Lecture 9 - By Paul Lin 19 Summary Control Flow Control Flow Loop Control Loop Control forfor whilewhile break, continuebreak, continue Conditional Statements Conditional Statements if, else, elseif, endif, else, elseif, end Next - Multiple Decision using switch, case, whatever, end Next - Multiple Decision using switch, case, whatever, end And More applications And More applications

20 February 14, 2005 Lecture 9 - By Paul Lin 20 Question? Answers Email: lin@ipfw.edu


Download ppt "February 14, 2005 Lecture 9 - By Paul Lin 1 CPET 190 Problem Solving with MATLAB Lecture 9"

Similar presentations


Ads by Google