Escolar Documentos
Profissional Documentos
Cultura Documentos
Malicdem, Roxette H.
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 4k 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)
Malicdem, Roxette H.
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
Course:
Group No.:
Group Members:
Procedure
No.
1
Results
EDITOR WINDOW:
for k= [1 3 5 7 9]
k
end
COMMAND WINDOW:
>> Malicdem_le06_p01
k= 1
k= 3
k= 5
k= 7
k= 9
2
EDITOR WINDOW:
for k= [2 4 6 8 10 12 14 16 18 20]
k
end
COMMAND WINDOW:
>> Malicdem_le06_p01
k=2
k=4
k=6
k=8
k = 10
k = 12
k = 14
k = 16
k = 18
k = 20
3
EDITOR WINDOW:
for k = 1:2:9
a = 8^k
end
EDITOR WINDOW:
>> Malicdem_le06_p013
a=8
a = 512
a = 32768
a = 2097152
a = 134217728
Malicdem, Roxette H.
EDITOR WINDOW:
for scores = [ 56, 77, 92, 97];
length(scores)
end
COMMAND WINDOW:
>> Malicdem_le06_p05
ans = 1
ans = 1
ans = 1
ans = 1
5
EDITOR WINDOW:
scores = [56,77,92,97];
count = 0;
for k = 1:length(scores)
if scores(k) > 90
count = count + 1
end
end
disp(count)
COMMAND WINDOW:
>> Malicdem_le06_p06
count = 1
count =
2
2
6
EDITOR WINDOW:
scores = [7. 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(count)
COMMAND WINDOW:
>> Malicdem_le06_p07
count =
1
count =
2
count =
3
count =
4
count =
Malicdem, Roxette H.
5
count =
6
6
EDITOR WINDOW:
fprintf('Degrees to Radians\n')
fprintf('Degrees Radians\n')
for degrees = 0:10:360
radians = (pi./180).*(degrees);
fprintf('%8.0f %8.2f \n', degrees, radians)
end
COMMAND WINDOW:
>> Malicdem_le06_p08
Degrees to Radians
Degrees 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
Malicdem, Roxette H.
10
320
5.59
330
5.76
340
5.93
350
6.11
360
6.28
EDITOR WINDOW:
fprintf('Degrees Centigrade to Degree Fahrenheit \n')
fprintf('Degrees Centigrade Degree Fahrenheit\n')
for Centigrade = 0:5:100;
Fahrenheit = (Centigrade*9/5)+32 ;
fprintf('%8.0f %8.2f \n', Centigrade, Fahrenheit)
end
COMMAND WINDOW:
>> Malicdem_le06_p09
Degrees Centigrade to Degree Fahrenheit
Degrees Centigrade Degree 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
EDITOR WINDOW:
fprintf('Inches to Feet\n')
fprintf('Inches Feet\n')
for inches = 0:1:10
feet = (inches/12);
fprintf('%8.0f %8.2f \n', inches, feet)
end
COMMAND WINDOW:
>> Malicdem_le06_p10
Inches to Feet
Inches Feet
0
0.00
1
0.08
2
0.17
3
0.25
Malicdem, Roxette H.
11
4
0.33
5
0.42
6
0.50
7
0.58
8
0.67
9
0.75
10
0.83
EDITOR WINDOW:
k= 0;
while k < 5
k = k + 1
end
COMMAND WINDOW:
>> Malicdem_le06_p11
12
k = 1
k = 2
k = 3
k = 4
k = 5
EDITOR WINDOW:
k = 0;
while k < 5
k = k + 1;
a(k)= 6^k
end
COMMAND WINDOW:
>> Malicdem_le06_p12
a
a
a
a
a
13
=
=
=
=
=
6
6
6
6
6
36
36
36
36
36
EDITOR WINDOW:
k= 0;
while k < 20
k = k + 4
end
COMMAND WINDOW:
>> Malicdem_le06_p13
k = 4
k = 8
k = 12
k = 16
k = 20
Malicdem, Roxette H.
216
216
216
216
216
1296
1296
1296
1296
1296
7776
7776
7776
7776
7776
14
15
EDITOR WINDOW:
scores = [ 45, 18, 52, 97,36];
count = 0;
k = 0;
while k < length(scores)
k = k + 1;
if scores(k) > 90
count=count +1;
end
end
disp(count)
COMMAND WINDOW:
>> Malicdem_le06_p14
1
EDITOR WINDOW:
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)
COMMAND WINDOW:
>> Malicdem_le06_p15
Enter a positive value of x 04
16
x = 4
The log base 10 of >> 27
ans = 27
EDITOR WINDOW:
fprintf('Degrees to Radians\n')
fprintf('\nDegrees Radians\n')
degrees=0;
while degrees < 360;
degrees = degrees + 20;
radians = (pi/180)*degrees;
fprintf('%8.0f %8.2f \n', degrees, radians);
end
COMMAND WINDOW:
>> Untitled
Degrees to Radians
Degrees Radians
20
0.35
40
0.70
60
1.05
80
1.40
100
1.75
120
2.09
140
2.44
Malicdem, Roxette H.
17
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
EDITOR WINDOW:
fprintf('Inches to Feet\n')
fprintf('\nInches Feet\n')
inches=0;
while inches < 5;
inches = inches + 1;
feet = (1/12)*inches;
fprintf('%8.0f %8.2f \n', inches, feet);
end
COMMAND WINDOW:
>> Malicdem_spec17
Inches to Feet
Inches
18
19
Feet
1
0.08
2
0.17
3
0.25
4
0.33
5
0.42
EDITOR WINDOW:
n = 5;
for i=1:n
fprintf( '%6d
end
COMMAND WINDOW:
>> Malicdem_spec18
1
1.0000
2
1.4142
3
1.7321
4
2.0000
5
2.2361
EDITOR WINDOW:
n = 20;
for i=1:n
fprintf( '%6d
sqrt(i));
end
COMMAND WINDOW:
>> Malicdem_spec19
1
1.0000
Malicdem, Roxette H.
%8.4f\n', i, sqrt(i));
%8.4f\n', i,
20
2
1.4142
3
1.7321
4
2.0000
5
2.2361
6
2.4495
7
2.6458
8
2.8284
9
3.0000
10
3.1623
11
3.3166
12
3.4641
13
3.6056
14
3.7417
15
3.8730
16
4.0000
17
4.1231
18
4.2426
19
4.3589
20
4.4721
EDITOR WINDOW:
n=180;
fprintf(' Degrees
for i=0:pi/6:pi
fprintf( '%8.2f
sin(i),cos(i));
end
21
Sine
%8.2f
\n');
%8.2f\n', i,
COMMAND WINDOW:
>> Malicdem_spec20
Degrees
Sine
Cosine
0.00
0.00
1.00
30.00
-0.99
0.15
60.00
-0.30
-0.95
90.00
0.89
-0.45
120.00
0.58
0.81
150.00
-0.71
0.70
180.00
-0.80
-0.60
EDITOR WINDOW:
n=20;
for i=1:n;
S=(n/2)*(1+n);
end
disp(' The Sum of the Integers is:');
disp(S)
COMMAND WINDOW:
>> Malicdem_spec21
The Sum of the Integers is:
210
Malicdem, Roxette H.
Cosine
7. Conclusion:
I therefore conclude that MATLAB programming is not just good in solving simple matrices but
also in solving simple mathematical operations as well as the square root and conversions of different
trigonometric which we can find hard to solve manually.
T I P V P A A 0 5 4 D
Revision Status/Date: 0/2009 September 09
BEGINNER
1
ACCEPTABLE
2
PROFICIENT
3
Members
do
not
demonstrate
needed
skills.
Members occasionally
demonstrate needed
skills.
Members always
demonstrate
needed skills.
Experimental Set-up
Process Skills
Member
do
not
demonstrate targeted
process skills.
Members occasionally
demonstrate targeted
process skills.
Safety Precautions
I. Laboratory Skills
Manipulative
Skills
Cooperative
Teamwork
Neatness
Orderliness
Malicdem, Roxette H.
and
and
Members
finish
ahead of time with
complete data and
time to revise data.
Members are on
tasks and have
defined
responsibilities at
all times. Group
conflicts
are
cooperatively
managed at all
times.
Clean and orderly
workplace at all
times during and
after
the
experiment.
SCORE
Ability
to
do
independent work
Members
supervision
teacher.
require
by the
Members
require
occasional supervision
by the teacher.
Members do not
need
to
be
supervised by the
teacher.
TOTAL SCORE
RATING
TotalScore
24
) x 100%
Evaluated by:
_______________________________________
Printed Name and Signature of Faculty Member
___________________________
Malicdem, Roxette H.
Date:
Malicdem, Roxette H.