You are on page 1of 12

Matlab Programming – Mechatronics Engineering – SPUP – 2019/2020

Name: Putri Wandini Student Reg. No. : 44419019 Class: 1A Teknik


Mekatronika

Task-03
Looping Control

Objectives:
1. Students are able to make looping control programs by using for-end
2. Students are able to make looping control programs by using while

for-end

Instructions:
1. Open your Matlab Program. Let p = last two digits of your student registration
number (e.g. p = 12). Write your p value in the below box

p =19

Open an M-file or script. Show the quadratic number from 1 to p. Copy this below script and
paste in the editor window of M-file (script) to find the answer.

Task03-1
Matlab Programming – Mechatronics Engineering – SPUP – 2019/2020
Name: Putri Wandini Student Reg. No. : 44419019 Class: 1A Teknik
Mekatronika

function a = TestFungsiFor
clc;
p = input('Input your own p = ');
disp ('Elements of a are the quadratic number from 1 to p as follows:');
for y = 1:p
a(y) = y^2;
end

Save your script with name: TestFungsiFor and run it by clicking the run icon or writing
TestFungsiFor on the command window. Print screen the result and paste it in the below box.

2. Copy this below script and paste it on the editor window of M-file (script) to calculate
the average value of some numbers (single data).

Task03-2
Matlab Programming – Mechatronics Engineering – SPUP – 2019/2020
Name: Putri Wandini Student Reg. No. : 44419019 Class: 1A Teknik
Mekatronika

%Task-3b: Calculate the average value


clc;
disp ('*********************************************');
disp ('* Looping Control by using for-end on Matlab*');
disp ('* by: (your name), Student Reg. No.: (….…) *');
disp ('*********************************************');

clear all;
clc;
n = input('Input n = ');
jumlah=0;
for i=1:n
x = input('Input a number = ');
jumlah=jumlah+x;
end
rata2=jumlah/n;
fprintf('The average value of those numbers are = %d \n',rata2);

Save your script with an appropriate name and run it. Print screen the result and paste it in the
below box.

Task03-3
Matlab Programming – Mechatronics Engineering – SPUP – 2019/2020
Name: Putri Wandini Student Reg. No. : 44419019 Class: 1A Teknik
Mekatronika

3. Copy this below script and paste it on the editor window of M-file (script) to calculate
the area of n triangles.

%Task-3c: Calculate the area of n triangles


clc;
disp ('*********************************************');
disp ('* Looping Control by using for-end on Matlab*');
disp ('* by: (your name), Student Reg. No.: (….…) *');
disp ('*********************************************');

clear all;
n = input('Input n = ');

for i = 1:n
disp (['***** Triangle-',num2str(i)]);
alas(i) = input ('Input the base in cm = ');
tinggi(i) = input ('Input the height in cm = ');
L(i) = 0.5 * alas(i) * tinggi(i);
fprintf ('The area of the triangle is = %3.2f cm^2 \n \n',L(i));
end
disp ('Thank you....');

Save your script with an appropriate name and run it. Please use p, 2p, 3p, … as the value of
base and height of the triangles. Print screen the result and paste it in the below box.

Task03-4
Matlab Programming – Mechatronics Engineering – SPUP – 2019/2020
Name: Putri Wandini Student Reg. No. : 44419019 Class: 1A Teknik
Mekatronika

4. Copy this below script and paste it on the editor window of M-file (script) to calculate
the total resistance of n series resistors.

%Task-3d: Calculate the total resistance of n series resistors


clc;
disp ('*********************************************');
disp ('* Looping Control by using for-end on Matlab*');
disp ('* by: (your name), Student Reg. No.: (….…) *');
disp ('*********************************************');

Task03-5
Matlab Programming – Mechatronics Engineering – SPUP – 2019/2020
Name: Putri Wandini Student Reg. No. : 44419019 Class: 1A Teknik
Mekatronika

disp('This program is used to calculate the total resistance of n series


resistors');
n=input('How many resistors that will be connected in a series circuit? =
');
disp('Input the values of the resistors (R) in Ohm !');
disp('Please use p, 2p, 3p, … as the value of R !');
Rseri=0;
for i=1:n
R=input(['Resistor ', num2str(i),' (Ohm) = ']);
Rseri=Rseri+R;
end
disp(' ');
disp(['The total resistance of the series resistors is = ',
num2str(Rseri) , ' Ohm']);
disp('Thanks ^_^');
disp(' ');

Save your script with an appropriate name and run it. Please use p, 2p, 3p, … as the value of
R . Print screen the result and paste it in the below box.

Task03-6
Matlab Programming – Mechatronics Engineering – SPUP – 2019/2020
Name: Putri Wandini Student Reg. No. : 44419019 Class: 1A Teknik
Mekatronika

5. Make an M-file (script) to calculate the total resistance of n parallel resistors by using
for-end.

Save your script with an appropriate name and run it. If there’s no mistake in your script then
copy the script and paste it it in the below box.

Task03-7
Matlab Programming – Mechatronics Engineering – SPUP – 2019/2020
Name: Putri Wandini Student Reg. No. : 44419019 Class: 1A Teknik
Mekatronika

%Task-3d: Calculate the total resistance of n series resistors


clc;
disp ('***************************************************');
disp ('* Looping Control by using for-end on Matlab *');
disp ('* by: Putri Wandini, Student Reg. No.:44419019 *');
disp ('***************************************************');

disp('This program is used to calculate the total resistance of n


parallel resistors');
n=input('How many resistors that will be connected in a parallel
circuit? = ');
disp('Input the values of the resistors (R) in Ohm !');
disp('Please use p, 2p, 3p, … as the value of R !');
Rparallel=0;
for i=1:n
R=input(['Resistor ', num2str(i),' (Ohm) = ']);
Rparallel=Rparallel + 1/R;
end
disp(' ');
disp(['The total resistance of the parallel resistors is = ',
num2str(Rparallel) , ' Ohm']);
disp('Thanks ^_^');
disp(' ');

Please use p, 2p, 3p, … as the value of R . Print screen the result and paste it in the below
box.

while

Task03-8
Matlab Programming – Mechatronics Engineering – SPUP – 2019/2020
Name: Putri Wandini Student Reg. No. : 44419019 Class: 1A Teknik
Mekatronika

6. Make an M-file (script) to show the incremental value of p from p to p+10.

Copy this below script and paste in the editor window of M-file (script) to find the answer.
Please note that p is not P.

function TestFungsiWhile(p)
%selama nilai p kurang dari p+10
clc;
P = input('Input your own p = ');
p = P;
while p < P+10
disp('The value of p now is : ');
%show the value of p
p
%increment the value of p
p = p+1;
end

Save your script with name: TestFungsiWhile and run it. Print screen the result and paste it
in the below box.

Task03-9
Matlab Programming – Mechatronics Engineering – SPUP – 2019/2020
Name: Putri Wandini Student Reg. No. : 44419019 Class: 1A Teknik
Mekatronika

7. Make an M-file (script) to calculate the area of 3 circles by using while.

Task03-10
Matlab Programming – Mechatronics Engineering – SPUP – 2019/2020
Name: Putri Wandini Student Reg. No. : 44419019 Class: 1A Teknik
Mekatronika

Save your script with an appropriate name and run it. If there’s no mistake in your script then
copy the script and paste it it in the below box.

%Task-3: Calculate the total resistance of n series resistors


clc;
disp ('****************************************************');
disp ('* Looping Control by using while on Matlab *');
disp ('* by: Putri Wandini, Student Reg. No.:44419019 *');
disp ('****************************************************');
disp ('*************Menghitung Luas Lingkaran**************');
n=input('input r = ');

while n<=29
n=n+1;
L = pi * n *n;
fprintf('luas lingkaran = %.2f cm^2\n',L)
end
disp('');

Please use p, 2p, 3p, … as the values of r or d . Print screen the result and paste it in the
below box.

Task03-11
Matlab Programming – Mechatronics Engineering – SPUP – 2019/2020
Name: Putri Wandini Student Reg. No. : 44419019 Class: 1A Teknik
Mekatronika

8. Congratulation, you had just finished your third task. 

---end of T3---

Task03-12

You might also like