Professional Documents
Culture Documents
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
end
On command window:
Cuesta_le06_p01
k =
1
k =
3
k =
5
k =
7
k =
9
2 On script window: <Cuesta_le06_p02.m>
for k=[2:2:20]
k
end
On command window:
Cuesta_le06_p03
k=
2
k=
4
k=
6
k=
8
k=
10
k=
12
k=
14
k=
16
k=
18
k=
20
3 On script window: <Cuesta_le06_p03.m>
for k=1:2:9
a=8^k
end
On command window:
Cuesta_le06_p03
a =
8
a =
512
a =
32768
a =
2097152
a =
134217728
4 On script window: <Cuesta_le06_p04.m>
for k=5:5:100
a=4^k
end
On command window:
Cuesta_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 On script window: <Cuesta_le06_p05.m>
scores=[56, 77, 92, 97];
length(scores)
On command window:
Cuesta_le06_p05
ans =
4
6 On script window: <Cuesta_le06_p06.m>
scores=[56, 77, 92, 97];
count=0;
for k=1:length(scores)
if scores(k)>50
count=count + 1
end
end
disp(count)
On command window:
Cuesta_le06_p06
count =
1
count =x
2
2
7 On script window: <Cuesta_le06_p07.m>
scores=[85, 74, 82, 68, 74, 87, 96, 87, 65, 73, 89, 56];
count=0;
for k=1:length(scores)
if scores(k)>=75
count=count + 1;
end
end
disp(' ')
disp('Number of passers:')
disp(' ')
disp(count)
On command window:
Cuesta_le06_p07
Number of passers:
6
8 On script window: <Cuesta_le06_p08.m>
fprintf('Degree-Radian Table\n')
for deg=0:10:360
rad=(pi/180)*deg;
end
On command window:
Cuesta_le06_p08
Degree-Radian Table
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 On script window: <Cuesta_le06_p09.m>
fprintf('Centigrade-Fahrenheit Table\n')
for Centigrade=0:5:100
Fahrenheit=32+(9/5)*Centigrade;
end
On command window:
Cuesta_le06_p09
Centigrade-Fahrenheit Table
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 On script window: <Cuesta_le06_p10.m>
fprintf('Inches-Feet Table\n')
for Inches=1:10
Feet=12*Inches;
end
On command window:
Cuesta_le06_p10
Inches-Feet Table
1 12.00
2 24.00
3 36.00
4 48.00
5 60.00
6 72.00
7 84.00
8 96.00
9 108.00
10 120.00
11 On script window: <Cuesta_le06_p11.m>
k=0;
while k<5
k = k+1
end
On command window:
Cuesta_le06_p11
k =
1
k =
2
k =
3
k =
4
k =
5
12 On script window: <Cuesta_le06_p12.m>
k=0;
while k<5
k=k + 1;
a(k)=6^k
end
On command window:
Cuesta_le06_p12
a =
6 36 216 1296 7776
a =
6 36 216 1296 7776
a =
6 36 216 1296 7776
a =
6 36 216 1296 7776
a =
6 36 216 1296 7776
13 On script window: <Cuesta_le06_p13.m>
fprintf('The first multiple of 4 that is less than 20\n')
for k=0:4:20
fprintf('%5.0f\n', k)
end
On command window:
Cuesta_le06_p13
The first multiple of 4 that is less than 20
0
4
8
12
16
20
14 On script window: <Cuesta_le06_p14.m>
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)
On command window:
Cuesta_le06_p14
2
15 On script window: <Cuesta_le06_p15.m>
x=input('Enter a positive value of x: ')
while(x<=0)
end
y=log(x);
fprintf('The log base 10 of %4.2f is %5.2f \n', x, y)
On command window:
Cuesta_le06_p15
Enter a positive value of x: -1
x =
-1
x =
while deg<=360
deg=deg + 10;
rad=(pi/180)*deg;
end
On command window:
Cuesta_le06_p16
Degree-Radian Table
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
370 6.46
17 On script window: <Cuesta_le06_p17.m>
fprintf('Inches-Feet Table\n')
Inches=0;
while Inches<=10
Inches=Inches + 1;
Feet=12*Inches;
end
On command window:
Cuesta_le06_p17
Inches-Feet Table
1 12.00
2 24.00
3 36.00
4 48.00
5 60.00
6 72.00
7 84.00
8 96.00
9 108.00
10 120.00
11 132.00
18 On script window: <Cuesta_le06_p18.m>
n=5;
for i=1:n
end
On command window:
Cuesta_le06_p18
1 1.0000
2 1.4142
3 1.7321
4 2.0000
5 2.2361
19 On script window: <Cuesta_le06_p19.m>
n=20;
for int=0:2:n
end
On command window:
Cuesta_le06_p19
0 0.00000
2 1.41421
4 2.00000
6 2.44949
8 2.82843
10 3.16228
12 3.46410
14 3.74166
16 4.00000
18 4.24264
20 4.47214
20 On script window: <Cuesta_le06_p20.m>
fprintf(' Rad Deg Sin Cos\n')
rad=0;
while rad<=pi
rad=rad+(pi/6);
deg=(rad*180)/pi;
x=sin(deg);
y=cos(deg);
end
On command window:
Cuesta_le06_p20
Rad Deg Sin Cos
1 30.00 -0.988 0.1543
1 60.00 -0.305 -0.9524
2 90.00 0.894 -0.4481
2 120.00 0.581 0.8142
3 150.00 -0.715 0.6993
3 180.00 -0.801 -0.5985
4 210.00 0.468 -0.8839
21 On script window: <Cuesta_le06_p21.m>
int=input('Enter an integer from 1 to 20: ')
for int=1:n;
sum=(n/2)*(1+n);
end
disp(sum)
On command window:
Cuesta_le06_p21
Enter an integer from 1 to 20: 7
int =
I therefore conclude that MATLAB is so versatile that it cannot just compute differential equations and
solid mensuration problems, as we did previously, but also loop-involving problems.. By making use of the
script window and making an m file, you just have to write the loop commands such as the if and while
and then make questions where you have to input the necessary conditions to fulfill the formula. This
laboratory exercise made me further understands the functions of MATLAB and its features. Thanks to this
laboratory experiment, I could now make a loop-type calculator (solving for the arithmetic series and
making conversion tables) by just typing codes.
8. Assessment (Rubric for Laboratory Performance):