Вы находитесь на странице: 1из 20

February 14, 2005 Lecture 9 - By Paul Lin 1

CPET 190
Problem Solving with MATLAB
Lecture 9



http://www.etcs.ipfw.edu/~lin
February 14, 2005 Lecture 9 - By Paul Lin 2
Lecture 9: MATLAB Program Design
& Flow Control II
9-1 Control Flow
if, else, elseif, end
switch, case, otherwise
for
while
break, continue
9-2 Loop Control
for
while
break, continue
9-3 Conditional Statements
if, else, elseif, end
February 14, 2005 Lecture 9 - By Paul Lin 3
9-1 Control Flow
MATLAB Control Flow (keywords)
if, else, elseif, end
switch, case, otherwise
for
while
break, continue

Specifying conditions and selections
If, else, elseif, end
switch-case statements
Repeating a set of commands or functions
For
while
February 14, 2005 Lecture 9 - By Paul Lin 4
9-2 Loop Control
FOR Loop
FOR Repeat statements a specific number of times.
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
Example:
for n = 1:10
statement 1
statement 2
..
statement n
end
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,
A(row, col) =
row * col;
end
end
disp(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
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)
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
END
February 14, 2005 Lecture 9 - By Paul Lin 7
9-2 Loop Control
Terminate Loop or Exit Conditional Statements
Break stop execution of loop
Continue Exit the inner loop and continuing on outer loop
Example 9-3
%break_continue.m
k = 1;
while k < 10
x = 10 - k^2;
if x < 0
break
end
y = sqrt(x)
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
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 loop
Example 9-4
%continue_ex.m
for k=1:6
if k == 4;
continue
end
fprintf('k = %d\n', k);
end
disp('End of loop')
Output:
k = 1
k = 2
k = 3
k = 5
k = 6
End of loop
February 14, 2005 Lecture 9 - By Paul Lin 9
9-3 Conditional Statements
Three Forms of if Statements
if-end
if-else-end
if-elseif-else-end

IF- END format
Single statement
if condition
statement
end
Multiple statements
if condition
Statement_1;
Statement_2;
.
Statement_n;
end
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)
end
% x = 2
% y_complex = 1.4142
% y_real = 1.4142
% x = -2
% y_complex = 0 + 1.4142i
x y
Real Axis
Imaginary
Axis
I or J operator
90 degree
0 + 1.4142 j
1.4142
February 14, 2005 Lecture 9 - By Paul Lin 11
9-3 Conditional Statements
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:

A grade: Average >= 90
B grade: 80 <= Avg < 90
C grade: 70 <= Avg <80
D grade: 60 <= Avg < 70
F grade: Avg < 60
February 14, 2005 Lecture 9 - By Paul Lin 12
9-3 Conditional Statements
Example 9-6: A Grade Calculation Program using
Flowchart
Start
Avg >=90
80 <= Avg < 90
70 <= Avg < 80 70 <= Avg < 80
60 <= Avg < 70
Avg < 60
End
Input T1, T2, and T3
Scores;
Compute Average
Grade = A,
Print
Grade = B,
Print
Grade = C,
Print
Grade = D,
Print
Grade = F,
Print
No
No
No
No
Yes
Yes
Yes
Yes
Yes
February 14, 2005 Lecture 9 - By Paul Lin 13
9-3 Conditional Statements
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';
end
if (avg >= 80) & (avg < 90)
Grade = 'B';
end
if (avg >= 70) & (avg < 80)
Grade = 'C';
end
if (avg >= 60) & (avg < 70)
Grade = 'D';
end
if avg < 60
Grade = 'F';
end
fprintf('Your average score
is %g.\n', avg);
fprintf('Your grade grade is
%c.\n', Grade);
February 14, 2005 Lecture 9 - By Paul Lin 14
9-3 Conditional Statements
IF-ELSEIF-ELSE-END
if expression
statements
elseif expression
statements
else
statements
end
if expression
statements_1
elseif
statements_2
elseif
statements_3
else
statements_4
end
IF-ELSE-END Construct:
if expression
statements_1
else
statements_2
end
February 14, 2005 Lecture 9 - By Paul Lin 15
9-3 Conditional Statements
Example 9-7: A Full Wave Rectifier
AC 60Hz
Sine wave
RL = 100 ohms
VL
5
1 2
3
4
6
7
8
9
10
10V
110V
Transformer
Bridge
Rectifier
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
e1 = 10*sin(2*pi*f*t); f = 60Hz
Rectifying signal e1
If e1 < 0
e1 = -e1;
end
Output Full-wave rectified voltage (plot)
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
e2(n) = e1(n);
else
e2(n) = -e1(n);
end
end

% continue on next
% slide
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')
0 0.002 0.004 0.006 0.008 0.01 0.012 0.014 0.016 0.018
-10
-5
0
5
10
Input signal
Time in sec
V
o
l
t
s
0 0.002 0.004 0.006 0.008 0.01 0.012 0.014 0.016 0.018
0
2
4
6
8
10
Full Wave Rectified Signal
Time in sec
V
o
l
t
s
February 14, 2005 Lecture 9 - By Paul Lin 19
Summary
Control Flow
Loop Control
for
while
break, continue
Conditional Statements
if, else, elseif, end
Next - Multiple Decision using switch,
case, whatever, end
And More applications
February 14, 2005 Lecture 9 - By Paul Lin 20
Question?
Answers

Email: lin@ipfw.edu

Вам также может понравиться