• No results found

Lab.-6

N/A
N/A
Protected

Academic year: 2021

Share "Lab.-6"

Copied!
15
0
0

Loading.... (view fulltext now)

Full text

(1)

TECHNOLOGICAL INSTITUTE OF THE PHILIPPINES – MANILA

Laboratory Exercise No. 6

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.

(2)

TECHNOLOGICAL INSTITUTE OF THE PHILIPPINES – MANILA

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

(3)

TECHNOLOGICAL INSTITUTE OF THE PHILIPPINES – MANILA

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;

(4)

TECHNOLOGICAL INSTITUTE OF THE PHILIPPINES – MANILA

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:

Laboratory Exercise No.:

Group No.:

Section:

Group Members:

Date Performed:

Date Submitted:

Instructor:

6. Data and Results:

(5)

TECHNOLOGICAL INSTITUTE OF THE PHILIPPINES – MANILA

Procedure

No.

Results

1

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

(6)

TECHNOLOGICAL INSTITUTE OF THE PHILIPPINES – MANILA

4

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 =

(7)

TECHNOLOGICAL INSTITUTE OF THE PHILIPPINES – MANILA

5

count =

6

6

8

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

(8)

TECHNOLOGICAL INSTITUTE OF THE PHILIPPINES – MANILA

320 5.59 330 5.76 340 5.93 350 6.11 360 6.28

9

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

10

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

(9)

TECHNOLOGICAL INSTITUTE OF THE PHILIPPINES – MANILA

4 0.33 5 0.42 6 0.50 7 0.58 8 0.67 9 0.75 10 0.83

11

EDITOR WINDOW: k= 0; while k < 5 k = k + 1 end COMMAND WINDOW: >> Malicdem_le06_p11 k = 1 k = 2 k = 3 k = 4 k = 5

12

EDITOR WINDOW: k = 0; while k < 5 k = k + 1; a(k)= 6^k end COMMAND WINDOW: >> Malicdem_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

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

(10)

TECHNOLOGICAL INSTITUTE OF THE PHILIPPINES – MANILA

14

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

15

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 x = 4

The log base 10 of >> 27 ans = 27

16

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

(11)

TECHNOLOGICAL INSTITUTE OF THE PHILIPPINES – MANILA

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

17

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 Feet 1 0.08 2 0.17 3 0.25 4 0.33 5 0.42

18

EDITOR WINDOW: n = 5; for i=1:n fprintf( '%6d %8.4f\n', i, sqrt(i)); end COMMAND WINDOW: >> Malicdem_spec18 1 1.0000 2 1.4142 3 1.7321 4 2.0000 5 2.2361

19

EDITOR WINDOW: n = 20; for i=1:n fprintf( '%6d %8.4f\n', i, sqrt(i)); end COMMAND WINDOW: >> Malicdem_spec19 1 1.0000

(12)

TECHNOLOGICAL INSTITUTE OF THE PHILIPPINES – MANILA

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

20

EDITOR WINDOW: n=180;

fprintf(' Degrees Sine Cosine \n');

for i=0:pi/6:pi fprintf( '%8.2f %8.2f %8.2f\n', i, sin(i),cos(i)); end 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

21

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

(13)

TECHNOLOGICAL INSTITUTE OF THE PHILIPPINES – MANILA

T I P – V P A A – 0 5 4 D Revision Status/Date: 0/2009 September 09

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.

8. Assessment (Rubric for Laboratory Performance):

TECHNOLOGICAL INSTITUTE OF THE PHILIPPINES RUBRIC FOR LABORATORY PERFORMANCE

CRITERIA BEGINNER

1 ACCEPTABLE 2 PROFICIENT 3 SCORE I. Laboratory Skills Manipulative Skills Members do not demonstrate needed skills. Members occasionally demonstrate needed skills. Members always demonstrate needed skills.

Experimental Set-up Members are unable to set-up the materials.

Members are able to set-up the materials with supervision.

Members are able to set-up the material with minimum

supervision. Process Skills Member demonstrate targeted do not

process skills. Members occasionally demonstrate targeted process skills. Members always demonstrate targeted process skills.

Safety Precautions Members do not follow safety precautions. Members follow safety precautions most of the time.

Members follow safety precautions at all times.

II. Work Habits

Time Management / Conduct of Experiment

Members do not finish on time with incomplete data.

Members finish on time with incomplete data.

Members finish ahead of time with complete data and time to revise data.

Cooperative and Teamwork

Members do not know their tasks and have no defined responsibilities. Group conflicts have to be settled by the teacher.

Members have defined responsibilities most of the time. Group conflicts are cooperatively managed most of the time.

Members are on tasks and have defined

responsibilities at all times. Group conflicts are cooperatively managed at all times. Neatness and Orderliness

Messy workplace during and after the experiment.

Clean and orderly workplace with occasional mess during and after the experiment.

Clean and orderly workplace at all times during and after the experiment.

(14)

TECHNOLOGICAL INSTITUTE OF THE PHILIPPINES – MANILA

Ability to do independent work Members require supervision by the teacher. Members require occasional supervision by the teacher. Members do not need to be supervised by the teacher.

Other Comments / Observations: TOTAL SCORE RATING = ( 24 TotalScore ) x 100% Evaluated by: _______________________________________

Printed Name and Signature of Faculty Member Date: ___________________________

(15)

TECHNOLOGICAL INSTITUTE OF THE PHILIPPINES – MANILA

Laboratory Exercise No. 6

Repetition Structures in MATLAB

References

Related documents

company has greater control over the employees providing the services and implementing procedures for the handling of intellectual property and confidential information.. The

As we acknowledge in the discussion the next logical step for this work is to progress it into an in vivo model where the scaffolds need to be implanted for long enough ( probably

low high Although not directly related to the repeated-dose endpoint, many category members have been tested for in vivo acute effects in rodents and fish. In addition,

Pembina County Meals &amp; Transportation provides congregate (group) meals and home delivered meals throughout Pembina County.. Congregate meals are available to persons age 60

Services include meal services, community activities, assistance with housekeeping and laundry, medicine administration, personal services (assistance with grooming, money

 Space provided for tent at the Post Race Party to distribute promotional literature or participant give-a-ways (items must be approved by RA Sports Management and Midway

decision-making process and the necessity for training them in order to have successful parental engagement. First, on the topic of voice, most policymakers and professionals

Samanta and Roy [14] established a continuous production inventory model of deteriorating item with shortages and considered that the production rate is changed