Вы находитесь на странице: 1из 23

Laboratory Exercise No.

4
Repetition Structures in MATLAB

1. Objective:
The activity aims to write and use for-end and while-end loops.

2. Intended Learning Outcomes (ILOs):


The students shall be able to:
2.1 Write and use for-end loops
2.2 Write and use 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:

scores = [ 56, 77, 92, 97];


length(scores)

Run it and show the results.


6. Create an m-file with the following contents:
% filename: yourSurname_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)
Run it and show the results.
Note: Starting from Procedure 9, follow the previous ways of naming m-files.
7. Create an m-file displaying the number of students who pass the preliminary grading period in a
class with the prelim grades of the students : 85, 74, 82, 68, 74, 87, 96, 87, 65, 73, 89 and 56.
8. Create an m-file that displays a table that converts angle values from degrees to radians, from 0 to
360 degrees, in increments of 10 degrees.
9. Create an m-file that displays a table that converts the degree centigrade to degree Fahrenheit,
from 0 to 100 degree centigrade, in increments of 5 degree centigrade.
10. Create an m-file that displays a table that converts inches to feet.
11. Create an m-file with the following contents:

k= 0;
while k < 5
k=k+1
end

Run it. Note: k is the counter and is initialized to zero.


12. Create an m-file with the following contents

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:

scores = [ 56, 78, 92, 97,56];


count = 0;
k = 0;
while k < length(scores)
k = k + 1;
if scores(k) > 90
count=count +1;
end
end
disp(count)

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:

x= input(‘Enter a positive value of x’)


while (x <=0)
disp(‘log(x) is not defined for the negative numbers’)
x= input(‘Enter a positive value of x’)
end
y = log(x);
fprintf(‘The log base 10 of %4.2 f is %5.2 f \n’,x,y)

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

Run it and show the results.


19. Create an m-file that prints the square root of the even integers up to n where n is equal to 20.
20. Create an m-file that prints the sine and cosine of a list of angles given in rad , say 0 to π, with an
increment of π/6. Note: Convert them in degrees first before applying the sine and cosine
functions.
21. Create an m-file that computes the sum of the first n integers wherein n is equal to 20.
Course: CHE 508 Computer Applications in ChE Laboratory Exercise No.: 4
Group No.: N/A Section: CH51FC1
Group Members: Date Performed: July 19, 2017
Percil, Queenie Rose I. Date Submitted: July 26, 2017
Instructor:
Engr. Crispulo Maranan
6. Data and Results:

Procedure No. Results


1 Matlab Editor Window:

%filename: Percil_le06_p01.m
for k = [ 1 3 5 7 9]
k

end

Matlab Command Window:

>> 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

Matlab Command Window::

>> Percil_le06_p02

k=

k=

k=

k=

k=

10

k=

12

k=

14
k=

16

k=

18

k=

20

3 Matlab Editor Window:

%filename: Percil_le06_p03.m

for k = 1:2:9
a = 8^k

end

Matlab Command Window:

>> Percil_le06_p03

a=

a=

512

a=

32768

a=

2097152
a=

134217728

4 Matlab Editor Window:

%filename: Percil_le06_p04.m

for k = 5:5:100
a = 8^k

end

Matlab Command Window:

>> 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

5 Matlab Editor Window:

%filename: Percil_le06_p05.m

scores = [ 56, 77, 92, 97];


length(scores)

Matlab Command Window:

>> Percil_le06_po05

ans =

6 Matlab Editor Window:

%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)

Matlab Command Window::

>> Percil_le06_po06

count =

count =

7 Matlab Editor Window:

%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

8 Matlab Editor Window:

%filename: Percil_le06_p08
fprintf ('Degrees to Radians\n')
for degrees=0:10:360

radians=(pi./180).*(degrees);

fprintf('%8.0f %8.2f \n', degrees, radians)

end

Matlab Command Window:

>> 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

9 Matlab Editor Window:

%filename: Percil_le06_p09
fprintf ('centigrade to fahrenheit\n')
for centigrade=0:5:100

fahrenheit=(centigrade*1.8)+(32);

fprintf('%8.0f %8.2f \n', centigrade, fahrenheit)

end

Matlab Command Window:

>> 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);

fprintf('%8.0f %8.2f \n', inches, feet)

end

Matlab Command Window:


:
>> Percil_le06_p10

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

Matlab Command Window:

>> 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

Matlab Command Window:

>> Percil_le06_p12

a=

a=

6 36
a=

6 36 216

a=

6 36 216 1296

a=

6 36 216 1296 7776

13 Matlab Editor Window:

%filename: Percil_le06_p13
k= 0;
while k < 20

k=k+4

end

Matlab Command Window:

>> Percil_le06_p13

k=

k=

k=

12

k=
16

k=

20

14 Matlab Editor Window:

%filename: Percil_le06_p14
scores = [ 56, 78, 92, 97,56];
count = 0;

k = 0;

while k < length(scores)

k = k + 1;

if scores(k) > 90

count=count +1;

end

end

disp(count)

Matlab Command Window:

>> Percil_le06_p14

15 Matlab Editor Window:

%filename: Percil_le06_p15
x= input('Enter a positive value of x: ')
while (x <=0)

disp('log(x) is not defined for the negative numbers')

x= input('Enter a positive value of x: ')

end
y = log(x);

fprintf('The log base 10 of %4.2 f is %5.2 f \n',x,y)

Matlab Command Window:

>> Percil_le06_p15

Enter a positive value of x: 18

x=

18

The log base 10 of >> 2

ans =

16 Matlab Editor Window:

%filename: Percil_le06_p16
fprintf ('radians to degrees\n')
degrees=0;

while degrees<=360

degrees=degrees+20;

radians=(pi./180).*(degrees);

fprintf('%8.0f %8.2f \n', degrees, radians)

end

Matlab Command Window:

>> 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);

fprintf('%8.0f %8.2f \n', inches, feet);

end

Matlab Command Window:

>> Percil_le06_p17

inches to feet
10 120.00
20 240.00
30 360.00
40 480.00
50 600.00
60 720.00

18 Matlab Editor Window:

%filename: Percil_le06_p18
n = 5;
for i=1:n

fprintf( '%6d %8.4f\n', i, sqrt(i));

end

Matlab Command Window:

>> Percil_le06_p18

1 1.0000
2 1.4142
3 1.7321
4 2.0000
5 2.2361

19 Matlab Editor Window:

%filename: Percil_le06_p19
n = 20;
for i=0:2:n

fprintf( '%6d %8.4f\n', i, sqrt(i));

end

Matlab Command Window:

>> 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

20 Matlab Editor Window:

%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);

fprintf('%10.0f %10.2f %10.3f %10.4f\n',radians,degrees,x,y)

end

Matlab Command Window:

>> Percil_le06_p20

radians degrees sine cosine


10 572.96 0.928 0.3736
20 1145.92 0.693 -0.7209
30 1718.87 -0.410 -0.9122
40 2291.83 -0.999 0.0393
50 2864.79 -0.337 0.9416
60 3437.75 0.748 0.6642
70 4010.70 0.895 -0.4453
80 4583.66 -0.079 -0.9969
90 5156.62 -0.954 -0.2995
100 5729.58 -0.634 0.7731
110 6302.54 0.480 0.8772
120 6875.49 0.993 -0.1177
130 7448.45 0.262 -0.9651
140 8021.41 -0.797 -0.6034
150 8594.37 -0.858 0.5143
160 9167.32 0.157 0.9876
170 9740.28 0.975 0.2236
180 10313.24 0.572 -0.8206
190 10886.20 -0.548 -0.8367
200 11459.16 -0.981 0.1954
210 12032.11 -0.185 0.9827
220 12605.07 0.842 0.5388
230 13178.03 0.815 -0.5801
240 13750.99 -0.234 -0.9723
250 14323.94 -0.989 -0.1463
260 14896.90 -0.505 0.8630
270 15469.86 0.612 0.7911
280 16042.82 0.962 -0.2719
290 16615.78 0.107 -0.9942
300 17188.73 -0.882 -0.4709
310 17761.69 -0.766 0.6424
320 18334.65 0.310 0.9509
330 18907.61 0.998 0.0681
340 19480.57 0.436 -0.9000
350 20053.52 -0.672 -0.7405
360 20626.48 -0.938 0.3467
370 21199.44 -0.029 0.9996

21 Matlab Editor Window:

%filename: Percil_le06_p21
n= input('Enter integers from 1-20:')

for i=1:n;

Sum=(n/2)*(1+n);

end

disp('The sum of integers is:')

disp(Sum)

Matlab Command Window:

>> Percil_le06_p21

Enter integers from 1-20:5

n=

The sum of integers is:


15

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

Вам также может понравиться