Introduction to Engineering MATLAB - 14
description
Transcript of Introduction to Engineering MATLAB - 14
Introduction to EngineeringMATLAB - 14
Loops for – end loops The break command Nested loops
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.
DEFINITION OF A LOOP
Example of a Loop Use
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
Variable
m is the value ofk in the first pass
s is the step value, which is theincrement in k after each pass
p is the value ofk in the last pass
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 end
are executed with the new value of k.
The process repeats itself until the
last pass where k = p
for k = m : s : p . . commands . .end
for – end LOOPS
Example:
If k = 1:2:9
there are five
loops. The
values of k are:
1 3 5 7 9
RULES REGARDING THE for 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).
>> for k = 1:3:10
x = k^2
end
x =
1
x =
16
x =
49
x =
100
EXAMPLES OF for – end LOOPS
Type in the command window:
>> for k = 5:9
b = 2*k
end
b =
10
b =
12
b =
14
b =
16
b =
18
>> for k = 10:2:20
a = k/3;
end
>> a
a =
6.6667
>> k
k =
20
a = 6.667becausein the lastpass k = 20
Semicolon, soa is not printedafter each pass
If a step value is notentered, the default is 1
COMMENTS 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 = 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)
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
This end is due to the if
This end is due to the for
EXECUTING THE SAVING GOAL SCRIPT FILEIN 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
NESTED for LOOPS
A for loop can be nested within another for loop.
for k = 1 : 3 for n = 1 : 5 . commands . endend
Every time k is increased by 1 the
nested loop loops five times with the
value of n ranging from 1 through 5.
k = 1 n = 1 k = 2 n = 1 k = 3 n = 1k = 1 n = 2 k = 2 n = 2 k = 3 n = 2k = 1 n = 3 k = 2 n = 3 k = 3 n = 3k = 1 n = 4 k = 2 n = 4 k = 3 n = 4k = 1 n = 5 k = 2 n = 5 k = 3 n = 5
Overall the commands will be executed 15 times with the values of:
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)
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)
Nestedloop
EXECUTING THE CREATING A MATRIX FILEIN 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:
1. Problem 26, page 61 in the textbook.
2. Problem 27, page 61 in the textbook. Change the text to read: Use the break command to determine how many ………
3. 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.