Академический Документы
Профессиональный Документы
Культура Документы
4
Repetition Structures in MATLAB
1. Objective:
The activity aims to write and use for-end and while-end loops.
3. Discussion :
In order to create any computer program, the organization of the statements that make it must be given
due consideration. The sections of the MATLAB code or any computer code for that matter are
classified into sequences, selection structures and repetition structures.
Loops is the other name for repetition structures and it has five basic parts that includes:
1. a parameter that serves as a way to end the loop or not
2. starting value for the parameter
3. in each time of the loop there should be a way to change the parameter which provides a means
to stop the execution of the repetition
4. to decide when to end the repetition there should be a comparison to a criterion using the
parameter
5. there should be a calculation inside the repetition structure
There are two different types of loops that are being supported by matlab that includes:
1. the for loop and
2. the while loop
Midpoint break loop is the third type of loop that makes use of two additional commands which
are break and continue. Whenever the number of times to repeat the loop is known the for loop is
the easiest to use. Whenever there is a need to keep repeating the instructions until a criterion is
met the while loop is the easiest to use. Whenever the commands in the loop is
executed at least once but the decision to exit the loop is based on some criterion the midpoint
break loop is the easiest to use.
Matlab programs can be composed that avoid loops by using either the find command or by
vectorizing the code which means that the entire vectors are operated at a time instead of one
element at a time. Because vectorized programs run faster and requires few programming steps. It is
a good idea to avoid loops if possible.
The for loop structure is fairly simple. The starting point of the for loop is the command for followed by
the index that determines when to end the repetitions. This index changes for every time it passes
through the loop. The group of commands which are to be executed follows the starting line. And the
command end provides the ending of the loop. The structure is:
for index = [ matrix ]
commands here
end
The format for a while loop is
while criterion
Commands here
end
While loops continue until some criterion is met.
With loop control statements, you can repeatedly execute a block of code.
4. Resources:
Matlab
5. Procedure:
1. Create an m-file with the following contents:
% filename: yourSurname_le06_p01.m
for k = [ 1 3 5 7 9]
k
end
Run it and show the results. Note: k here is an index matrix and for loop is executed depending on
the number of elements in the index matrix. In this case, it executes five times.
2. Create an m-file ( filename: yourSurname_le06_p02.m) using for loop which results in the values of
k equal to even numbers from 2 to 20, one at a time. Run it and show the results.
3. Create an m-file with the following contents:
% filename: yourSurname_le06_p03.m
for k = 1:2:9
a = 8^k
end
Run it and show the results. Note: The index matrix use a colon operator.
4. Create an m-file (filename: yourSurname_le06_p04.m) using for loop and an index matrix with a
colon operator which results in the values of a equal to 4 k wherein k has values from 5 to 100 with
an interval of 5. Run it and show the results.
5. Create an m-file ( filename: yourSurname_le06_po05.m) with the following contents:
k= 0;
while k < 5
k=k+1
end
k = 0;
while k < 5
k = k + 1;
a(k)= 6^k
end
Run it.
13. Create an m-file that displays the first multiple of 4 that is less than 20.
14. Create an m-file with the following contents:
Run it. Note: Variable count is used to count how many values are greater than 90 and variable k is
used to count how many times the loop is executed.
15. Create an m-file with the following contents:
Run it. Consider an input of a negative and a positive number one at a time.
16. Create an m-file that displays a table that converts degrees to radians, from 0 to 360 degrees, in
increments of 20 degrees using a while loop.
17. Create an m-file that displays a conversion table of inches to feet using a while loop.
18. Create an m-file with the following contents:
n = 5;
for i=1:n
fprintf( '%6d %8.4f\n', i, sqrt(i));
end
%filename: Percil_le06_p01.m
for k = [ 1 3 5 7 9]
k
end
>> Percil_le06_p01
k=
k=
k=
k=
k=
9
2 Matlab Editor Window:
%filename: Percil_le06_p02.m
for k = [ 2 4 6 8 10 12 14 16 18 20]
k
end
>> Percil_le06_p02
k=
k=
k=
k=
k=
10
k=
12
k=
14
k=
16
k=
18
k=
20
%filename: Percil_le06_p03.m
for k = 1:2:9
a = 8^k
end
>> Percil_le06_p03
a=
a=
512
a=
32768
a=
2097152
a=
134217728
%filename: Percil_le06_p04.m
for k = 5:5:100
a = 8^k
end
>> Percil_le06_p04
a=
1024
a=
1048576
a=
1.0737e+09
a=
1.0995e+12
a=
1.1259e+15
a=
1.1529e+18
a=
1.1806e+21
a=
1.2089e+24
a=
1.2379e+27
a=
1.2677e+30
a=
1.2981e+33
a=
1.3292e+36
a=
1.3611e+39
a=
1.3938e+42
a=
1.4272e+45
a=
1.4615e+48
a=
1.4966e+51
a=
1.5325e+54
a=
1.5693e+57
a=
1.6069e+60
%filename: Percil_le06_p05.m
>> Percil_le06_po05
ans =
%filename: Percil_le06_p06.m
scores = [56,77,92,97];
count = 0;
for k = 1:length(scores)
if scores(k) > 90
count = count + 1
end
end
disp(count)
>> Percil_le06_po06
count =
count =
%filename: Percil_le06_p07
prelimgrades = [90,80,75,50,87,48,46,45,75,80,88,95];
count = 0;
for k = 1:length(prelimgrades)
if prelimgrades(k) >= 50
count = count + 1;
end
end
disp(count)
Matlab Command Window:
>> Percil_le06_p07
%filename: Percil_le06_p08
fprintf ('Degrees to Radians\n')
for degrees=0:10:360
radians=(pi./180).*(degrees);
end
>> Percil_le06_p08
Degrees to Radians
0 0.00
10 0.17
20 0.35
30 0.52
40 0.70
50 0.87
60 1.05
70 1.22
80 1.40
90 1.57
100 1.75
110 1.92
120 2.09
130 2.27
140 2.44
150 2.62
160 2.79
170 2.97
180 3.14
190 3.32
200 3.49
210 3.67
220 3.84
230 4.01
240 4.19
250 4.36
260 4.54
270 4.71
280 4.89
290 5.06
300 5.24
310 5.41
320 5.59
330 5.76
340 5.93
350 6.11
360 6.28
%filename: Percil_le06_p09
fprintf ('centigrade to fahrenheit\n')
for centigrade=0:5:100
fahrenheit=(centigrade*1.8)+(32);
end
>> Percil_le06_p09
centigrade to fahrenheit
0 32.00
5 41.00
10 50.00
15 59.00
20 68.00
25 77.00
30 86.00
35 95.00
40 104.00
45 113.00
50 122.00
55 131.00
60 140.00
65 149.00
70 158.00
75 167.00
80 176.00
85 185.00
90 194.00
95 203.00
100 212.00
10 Matlab Editor Window:
%filename: Percil_le06_p10
fprintf ('inches to feet\n')
for inches=0:5:50
feet=inches*(12);
end
inches to feet
0 0.00
5 60.00
10 120.00
15 180.00
20 240.00
25 300.00
30 360.00
35 420.00
40 480.00
45 540.00
50 600.00
11 Matlab Editor Window:
%filename: Percil_le06_p11
k= 0;
while k < 5
k=k+1
end
>> Percil_le06_p11
k=
k=
k=
k=
k=
5
12 Matlab Editor Window:
%filename: Percil_le06_p12
k = 0;
while k < 5
k = k + 1;
a(k)= 6^k
end
>> Percil_le06_p12
a=
a=
6 36
a=
6 36 216
a=
6 36 216 1296
a=
%filename: Percil_le06_p13
k= 0;
while k < 20
k=k+4
end
>> Percil_le06_p13
k=
k=
k=
12
k=
16
k=
20
%filename: Percil_le06_p14
scores = [ 56, 78, 92, 97,56];
count = 0;
k = 0;
k = k + 1;
if scores(k) > 90
count=count +1;
end
end
disp(count)
>> Percil_le06_p14
%filename: Percil_le06_p15
x= input('Enter a positive value of x: ')
while (x <=0)
end
y = log(x);
>> Percil_le06_p15
x=
18
ans =
%filename: Percil_le06_p16
fprintf ('radians to degrees\n')
degrees=0;
while degrees<=360
degrees=degrees+20;
radians=(pi./180).*(degrees);
end
>> Percil_le06_p16
radians to degrees
20 0.35
40 0.70
60 1.05
80 1.40
100 1.75
120 2.09
140 2.44
160 2.79
180 3.14
200 3.49
220 3.84
240 4.19
260 4.54
280 4.89
300 5.24
320 5.59
340 5.93
360 6.28
380 6.63
17 Matlab Editor Window:
%filename: Percil_le06_p17
fprintf ('inches to feet\n')
inches=0;
while inches<=50;
inches=inches+10;
feet=inches*(12);
end
>> Percil_le06_p17
inches to feet
10 120.00
20 240.00
30 360.00
40 480.00
50 600.00
60 720.00
%filename: Percil_le06_p18
n = 5;
for i=1:n
end
>> Percil_le06_p18
1 1.0000
2 1.4142
3 1.7321
4 2.0000
5 2.2361
%filename: Percil_le06_p19
n = 20;
for i=0:2:n
end
>> Percil_le06_p19
0 0.0000
2 1.4142
4 2.0000
6 2.4495
8 2.8284
10 3.1623
12 3.4641
14 3.7417
16 4.0000
18 4.2426
20 4.4721
%filename: Percil_le06_p20
fprintf ('radians degrees sine cosine\n')
radians=0;
while radians<=360
radians=radians+10;
degrees= (radians*180)/pi;
x=sin(degrees);
y=cos(degrees);
end
>> Percil_le06_p20
%filename: Percil_le06_p21
n= input('Enter integers from 1-20:')
for i=1:n;
Sum=(n/2)*(1+n);
end
disp(Sum)
>> Percil_le06_p21
n=
7. Conclusion:
I therefore conclude that in this experiment, I was able to write and use for-end and while-end loops in
order to create a computer program using the matlab application.
8. Assessment (Rubric for Laboratory Performance):
TECHNOLOGICAL INSTITUTE OF THE PHILIPPINES
RUBRIC FOR MODERN TOOL USAGE
(Engineering Programs)
Student Outcome (e): Use the techniques, skills, and modern engineering tools necessary for engineering
practice in complex engineering activities.
Program: Chemical Engineering Course: CHE 508 Section: CH51FC1 1st Sem SY : 2017-2018
Performance Unsatisfactory Developing Satisfactory Very Satisfactory Score
Indicators 1 2 3 4
1. Apply Fails to identify Identifies Identifies modern Recognizes the
appropriate any modern modern techniques and is benefits and
techniques, techniques to techniques but able to apply constraints of
skills, and perform fails to apply these in modern
modern discipline- these in performing engineering tools
tools to specific performing discipline-specific and shows
perform a engineering discipline- engineering task. intention to apply
discipline- task. specific them for
specific engineering engineering
engineering task. practice.
task.
2. Demonstrate Fails to apply Attempts to Shows ability to Shows ability to
skills in any modern apply modern apply fundamental apply the most
applying tools to solve tools but has procedures in appropriate and
different engineering difficulties to using modern effective modern
techniques problems. solve tools when solving tools to solve
and modern engineering engineering engineering
tools to problems. problems. problems.
solve
engineering
problems.
3. Recognize Does not Recognizes Recognizes the Recognizes the
the benefits recognize the some benefits benefits and need for benefits
and benefits and and constraints of and constraints of
constraints constraints of constraints of modern modern
of modern modern modern engineering tools engineering tools
engineering engineering engineering and shows and makes good
tools. tools. tools. intention to apply use of them for
them for engineering
engineering practice.
practice.
Total Score
Mean Score = (Total Score / 3)
Percentage Rating = (Total Score / 12) x 100%
Evaluated by: Engr. Crispulo G. Maranan July 26, 2017
Printed Name and Signature of Faculty Member Date