Professional Documents
Culture Documents
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
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.
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.
Task03-5
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. 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
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
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
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.
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
---end of T3---
Task03-12