200 likes | 410 Views
Introduction to Engineering MATLAB - 14. Loops for – end loops The break command Nested loops. DEFINITION 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.
E N D
Introduction to EngineeringMATLAB - 14 Loops for – end loops The break command Nested loops
DEFINITION 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.
s is the step value, which is the increment in k after each pass m is the value of k in the first pass p is the value of k in the last pass Variable for k = m : s : p . . commands . . end for – 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:
for k = m : s : p . . commands . . end Example: If k = 1:2:9 there are five loops. The values of k are: 1 3 5 7 9 for – 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 = m+s, and the commands between the for and the endare executed with the new value of k. • The process repeats itself until the last pass where k = p
RULES REGARDING THEfor k = m:s:p COMMAND • The step value s can be negative (i.e. k = 25:-5:10 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 = 8:10:50 produces five loops with: k = 8, 18, 28, 38, 48).
If a step value is not entered, the default is 1 Semicolon, so a is not printed after each pass a = 6.667 because in the last pass k = 20 EXAMPLES OF for – end LOOPS Type in the command window: >> for k = 10:2:20 a = k/3; end >> a a = 6.6667 >> k k = 20 >> for k = 1:3:10 x = k^2 end x = 1 x = 16 x = 49 x = 100 >> for k = 5:9 b = 2*k end b = 10 b = 12 b = 14 b = 16 b = 18
COMMENTS ABOUT for LOOPS • For every for command a computer program MUST have an endcommand. • for loops can be used in the command window and in script and function files. • A semicolon is not needed after the fork = m:s:p 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.)
EXAMPLES 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/2^n % The program asks the user to input % the number of terms n. sumser=0; n = input('the number of terms is: '); for k = 1:n sumser = sumser + (-1)^k*k/2^k; end disp('The sum of the series is:') disp(sumser)
EXECUTING THE SERIES SUM FILE IN THECOMMAND WINDOW >> Lecture9Example1 the number of terms is: 4 The sum of the series is: -0.1250 >> Lecture9Example1 the number of terms is: 5 The sum of the series is: -0.2813 >> Lecture9Example1 the number of terms is: 15 The sum of the series is: -0.2224 >> Lecture9Example1 the number of terms is: 20 The sum of the series is: -0.2222
THE 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.
EXAMPLE 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)
This end is due to the if This end is due to the for xi = 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 >= 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
EXECUTING THE SAVING GOAL SCRIPT FILE IN THE COMMAND WINDOE >> 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 >> 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
for k = 1 : 3 for n = 1 : 5 . commands . end end NESTED 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
EXAMPLE 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)
Nested loop matrix = 0; n = input('Enter the number of rows '); m = input('Enter the number of columns '); for i = 1:n for j = 1:m if i == j matrix(i,j) = 1; else matrix(i,j) = 7; end end end disp('The matrix is:') disp(matrix)
EXECUTING THE CREATING A MATRIX FILE IN THE COMMAND WINDOW >> 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 >> 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 >> Lecture9Example3 Enter the number of rows 4 Enter the number of columns 2 The matrix is: 1 7 7 1 7 7 7 7
ASSIGNMENT 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.