4/5/2004 ENGR 111A - Spring 2004 1
MatLab – Palm Chapter 4, Part 3For and While Loops
Class 11.1 Palm Ch 4.5
4/5/2004 ENGR 111A - Spring 2004 2
RAT 11.1 As in INDIVIDUAL you have 2 minutes to
answer the following question. What is the FINAL value assigned to y in
the following segment of MATLAB code?x = 0;while x < 5 y = sqrt(x); x = x + 1;
end When asked, you have 30-seconds to
submit your paper. Answer: y = 2
4/5/2004 ENGR 111A - Spring 2004 3
Learning Objectives Students should be able to:
Understand Matlab iteration structures:for … end Loops while … end Loops
Identify Implied loops Recognize these structures as
repetition (or iteration) structures. Develop flow (logic) diagrams for
combinations of branching and looping structures
4/5/2004 ENGR 111A - Spring 2004 4
LOOPS A LOOP is a structure for repeating
(iterating) a block of code. MATLAB uses two types of loops.
for loop: number of passes known a priori (p. 210).
while loop: loop terminates when a specified condition is satisfied (p. 221).
4/5/2004 ENGR 111A - Spring 2004 5
for LOOPSSYNTAX:for k = start:step:stop blockendstart = initial valuestep = incremental valuestop = terminating valueSee p. 212 for flowchart of for loop.
Indentation Mandatory!
4/5/2004 ENGR 111A - Spring 2004 6
for LOOP FLOW CHART
Loop structure
4/5/2004 ENGR 111A - Spring 2004 7
EXAMPLE
for k = 5:10:35x = k^2
end
4/5/2004 ENGR 111A - Spring 2004 8
for … end Loops (p. 211)for k = 5:10:35
x = k^2
end Means:
starting with k = 5, execute the statements between for … end;
then increment k by 10 and execute the statements with k = 15; etc. until k > 35.
Then exit the loop (k=? at this point)
4/5/2004 ENGR 111A - Spring 2004 9
for … end Loops cont’d. Step value may be negative
(default is positive 1) For step > 0, loop does not
execute if start > stop For step < 0, loop does not
execute if start < stop If start = stop, loop executes once Non-integer step values give
unpredictable results (don’t use them!)
4/5/2004 ENGR 111A - Spring 2004 10
INDIVIDUAL EXERCISE (5 min.)
Evaluate xn = sin (n/10) for n between 1 and 10 (in increments of 1).
Write a Matlab PROGRAM. (Plot x versus n) Save file on R:\ and name file after
an historical figure.
4/5/2004 ENGR 111A - Spring 2004 11
SOLUTIONfor n = 1:10 x(n) = sin(n*pi/10);endplot(1:10,x) % orplot(x)title(‘In-class… example’)xlabel(‘n’)ylabel(‘x(n)’)
4/5/2004 ENGR 111A - Spring 2004 12
for Loops for loops can be nested (one inside each
other). Each loop is closed with an end.EXAMPLE: Proper indention is a MUST!!N = 10;for I = 1:N for J = 1:N A(I,J) = 1/(I + J – 1); endendWhy is I OK and i not OK?
4/5/2004 ENGR 111A - Spring 2004 13
for Loops for loops can be nested (one inside each
other). Each loop is closed with an end.EXAMPLE: Proper indention is a MUST!!N = 10;for r = 1:N for c = 1:N A(r,c) = 1/(r + c – 1); endendUsing r (for row) and c (for col) is MUCH better!!!
4/5/2004 ENGR 111A - Spring 2004 14
In-class Assignment 11.1 (20 min.)
Work through the nested loop and if example on page 212.
Write an program file to solve T4.5-1 (p. 213)
Save your solutions on the R:\ drive Name your files after an animal.
4/5/2004 ENGR 111A - Spring 2004 15
Solution% PROGRAM example1.m %% Solution to T4.5-1 on p. 213 of the Palm% Matlab book.%% S. Socolofsky% ENGR 111A: 501-503
for r = 1:4 for c = 1:3 A(r,c) = 4 + (r-1)*6 + (c-1)*4; endend
4/5/2004 ENGR 111A - Spring 2004 16
The “break” Statement You can EXIT out of any looping structure
with a “break” (Both for… and while…)
y=0;x=[1 -2 3 -4 5];for k = 1:length(x); if x(k) < 0 break end y = y + sqrt(x(k));end% -- Jump to here on break
4/5/2004 ENGR 111A - Spring 2004 17
The “continue” Statement
You can skip “bad values” in any looping structure with a “continue”
y=0;x=[1 -2 3 -4 5];for k = 1:length(x); if x(k) < 0 continue end y = y + sqrt(x(k));end % Jump to here on continue
4/5/2004 ENGR 111A - Spring 2004 18
Implied Loops (p. 216) You can use built-in loops instead of for
… end loops in most cases. Sometimes it is better, sometimes not. Note the matrix example with simple logic Note the for … end example with more
complex logic Note the find function example for a very
normal situation that you once upon a time had to write code for every time.
4/5/2004 ENGR 111A - Spring 2004 19
Implied Loops (cont.) Many MatLab commands contain
implied loops. Example:
x = [0:5:100];
y = cos(x); Example:
y = find(x>0); See p. 216 for equivalent loops for
these.
4/5/2004 ENGR 111A - Spring 2004 20
while LOOPS The while loop terminates because
a specified condition is satisfied. The number of passes is NOT
known a prioriSYNTAX:while logical expression statementsendSee p. 223 for flowchart of while loop.
4/5/2004 ENGR 111A - Spring 2004 21
while LOOP FLOW CHART
Loop structure
4/5/2004 ENGR 111A - Spring 2004 22
while … end Loops Look at the examples on page 221.
x = 5;while x < 25
disp(x)x = 2*x-1 %-make sure x changes
end Means:
starting with x = 5, execute the statements between the while … end; this displays x and calculates a new value of x and loops back.
Then exit the loop when x exceeds or equals 25.
4/5/2004 ENGR 111A - Spring 2004 23
while … end Loops (p.222)
THE LOOP VARIABLE MUST HAVE A VALUE BEFORE THE while STATEMENT IS EXECUTED
THE LOOP VARIABLE MUST BE CHANGED SOMEHOW BY THE STATEMENTS
Note the usual mistakes toward the bottom of the page.
Make sure that you save your code before testing any while … end loop.
4/5/2004 ENGR 111A - Spring 2004 24
INDIVUDAL EXERCISE (5 min.) Write an equivalent while loop for the
implied loop shown:
x = [0:5:100];
y = sin(x);
disp (y) Save on the R:\ drive Name file after a city outside of
Texas
4/5/2004 ENGR 111A - Spring 2004 25
SOLUTION
x = 0;
while x <= 100
y = sin(x);
x = x+5;
disp(y)
end
4/5/2004 ENGR 111A - Spring 2004 26
NESTED while LOOPS
while loops can also be nested.
Each loop must be closed with an end.
break statements may be used to jump out of loops.
4/5/2004 ENGR 111A - Spring 2004 27
EXAMPLE
r = 1; N = 10; while r <= N c = 1; while c <= N A(r,c) = 1/(r + c -1); c = c + 1; end r = r + 1; end %- note use of r,c instead of i,j.
4/5/2004 ENGR 111A - Spring 2004 28
Assignment 11.1 Individual assignment. Due: Nov 15, 2004 Palm MATLAB;
Chapter 4: #22 (use for-loop in part b), 28 (use a while-loop), T4.5-4 (p. 225)
Extra Problem (see assignments web page)!
Extra Credit (see assignments web page)!!
Read Palm section 4.7.
Top Related