Title: Introduction to Engineering MATLAB - 14
1Introduction to EngineeringMATLAB - 14
- Loops
- for end loops
- The break command
- Nested loops
2DEFINITION OF A LOOP
- A loop is a set of commands in a computer
program that are being repeated. - Each repetition of the loop is called a pass.
- The number of passes can be set to be fixed,
or the looping process is terminated when a
specified condition is satisfied. - In each pass some or all of the variables that
are defined in the loop obtain new values.
3Example of a Loop Use
4for end LOOPS
for end loops are used when the number of
passes is known in advance. A variable is used
to control the looping process. The general
structure of a for end loop is
5for end LOOPS
- In the first pass, k m, and the computer
executes the commands between the for and the
end. -
- The computer goes back to the for command for
the second pass. k obtains a new value equal to
k ms, and the commands between the for and the
end are executed with the new value of k. -
- The process repeats itself until the last
pass where k p
6RULES REGARDING THE for k msp COMMAND
- The step value s can be negative (i.e. k
25-510 produces four loops with k 25,
20, 15, 10). - If s is omitted, the step value is 1
(default). - If m equals to p, the loop is executed once.
- The value of k should not be redefined within
the loop. - The looping continues until the value of k
exceeds the value of p (i.e. k 81050
produces five loops with k 8, 18, 28,
38, 48).
7EXAMPLES OF for end LOOPS
Type in the command window
gtgt for k 10220 a k/3 end gtgt a a
6.6667 gtgt k k 20
gtgt for k 1310 x k2 end x 1 x
16 x 49 x 100
gtgt for k 59 b 2k end b 10 b
12 b 14 b 16 b 18
8COMMENTS ABOUT for LOOPS
- For every for command a computer program MUST
have an end command. - for loops can be used in the command window
and in script and function files. - A semicolon is not needed after the for k
msp command to suppress printing. - To display the value of k in each pass
(sometimes useful for debugging) type k
as one of the commands in the loop. - Loops can include conditional statements and
any other MATLAB commands (functions,
plots, etc.)
9EXAMPLES OF USING for end LOOP
A script file that demonstrates the use of a
for - end loop. The file calculates the sum of
n terms of the series (-1)n n/2n The
program asks the user to input the number of
terms n. sumser0 n input('the number of
terms is ') for k 1n sumser sumser
(-1)kk/2k end disp('The sum of the series
is') disp(sumser)
10EXECUTING THE SERIES SUM FILE IN THECOMMAND
WINDOW
gtgt Lecture9Example1 the number of terms is
4 The sum of the series is -0.1250 gtgt
Lecture9Example1 the number of terms is 5 The
sum of the series is -0.2813
gtgt Lecture9Example1 the number of terms is
15 The sum of the series is -0.2224 gtgt
Lecture9Example1 the number of terms is 20 The
sum of the series is -0.2222
11THE break COMMAND
The break command stops the execution of a loop.
When MATLAB encounters a break command within a
loop, MATLAB jumps to the end command of the loop
and continues to executes the commands that
follow. The break command is typically used
within a conditional statement (if statement) to
terminate the execution of a loop if some
condition is satisfied.
12EXAMPLE OF USING THE break COMMAND
A script file that demonstrates the use of the
break command. Given an initial investment,
saving goal and expected return, the file
calculates the number of years it will take to
reach the goal.
(The file continues on the next slide)
13xi input('Enter the initial investment ()
') xf input('Enter the saving goal () ') r
input('Enter the expected return per year ()
') disp(' ') for k 1 100 xk xi(1
r/100)k if xk gt xf disp('The
number of years it will') disp('take to
reach the goal is') disp(k)
break end end if k 100 disp('It will
take more than') disp('100 years to reach the
goal') end
14EXECUTING THE SAVING GOAL SCRIPT FILE IN THE
COMMAND WINDOE
gtgt Lecture9Example2 Enter the initial investment
() 2000 Enter the saving goal () 5000 Enter
the expected return per year () 6 The number
of years it will take to reach the goal is 16
gtgt Lecture9Example2 Enter the initial investment
() 1500 Enter the saving goal ()
100000 Enter the expected return per year () 4
It will take more than 100 years to reach the
goal
15NESTED for LOOPS
A for loop can be nested within another for loop.
Every time k is increased by 1 the nested loop
loops five times with the value of n ranging from
1 through 5.
Overall the commands will be executed 15 times
with the values of
k 1 n 1 k 2 n 1 k 3 n 1 k 1
n 2 k 2 n 2 k 3 n 2 k 1 n
3 k 2 n 3 k 3 n 3 k 1 n 4 k
2 n 4 k 3 n 4 k 1 n 5 k 2
n 5 k 3 n 5
16EXAMPLE OF NESTED for LOOP
A script file that demonstrates the use of
nested for - end loop. The file creates a
matrix in which all the terms are 7 except the
terms whose row number is equal to the column
number. These terms are equal to 1.
(The script file continues on the next slide)
17matrix 0 n input('Enter the number of rows
') m input('Enter the number of columns
') for i 1n for j 1m if i j
matrix(i,j) 1 else
matrix(i,j) 7 end end end disp('The
matrix is') disp(matrix)
18EXECUTING THE CREATING A MATRIX FILE IN THE
COMMAND WINDOW
gtgt Lecture9Example3 Enter the number of rows
3 Enter the number of columns 3 The matrix is
1 7 7 7 1 7 7 7
1 gtgt Lecture9Example3 Enter the number of rows
3 Enter the number of columns 5 The matrix is
1 7 7 7 7 7 1 7
7 7 7 7 1 7 7
gtgt Lecture9Example3 Enter the number of rows
4 Enter the number of columns 2 The matrix is
1 7 7 1 7 7 7 7
19ASSIGNMENT 10
- Problem 26, page 61 in the textbook.
- Problem 27, page 61 in the textbook. Change the
text to read Use the break command to determine
how many - Writes a program in a script file that creates a
multiplication table by using a nested for loop.
The program asks the user to input the number of
rows and the number of columns. The program
prints the multiplication table. Executes the
file in the command window to create a
multiplication table with 16 rows and 12 columns. - Submit a printout of the script file and a
printout of the command window.