You are on page 1of 21

Matlab Programming – Mechatronics Engineering – SPUP – 2019/2020

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


Teknik Mekatronika

Task-04
Branching Control

Objectives:
1. Students are able to make branching control programs by using if or if-else
2. Students are able to make branching control programs by using switch-case

if

if-else

if-else bertingkat

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

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. Copy this below script and paste in the editor window of
M-file (script).

%Task-4a: Branching Control by using IF-ELSE on Matlab


clc;
disp
('************************************************************');
disp ('* Branching Control by using IF-ELSE on Matlab*');
disp ('* by: (your name), Student Reg. No.: (….…)
*');
disp
('************************************************************');
disp (' ');
clear all;

disp ('Choices of Program:');


disp ('1 . Calculate the area of rectangles');
disp ('2 . Calculate the area of triangles');
disp ('************************************');
disp (' ');
pilihan = input('Input your choice: ');
if (pilihan ~=1 && pilihan ~=2)
pilihan = input ('The available choice is only 1 or 2, please
reenter your choice: ');
end

if pilihan == 1
n = input ('How many rectangles ? ');
disp (' ')
for i = 1:n;
disp (['No. : ', num2str(i)]);
p = input ('Input the lenght in cm = ');
l = input ('Input the width in cm = ');
L = p * l;

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

fprintf ('The area of the rectangle is = %5.2f cm^2 \n', L


);
disp (' ')
end
else
n = input ('How many triangles ? ');
disp (' ')
for i = 1:n;
disp (['No. : ', num2str(i)]);
alas = input ('Input the base in cm = ');
tinggi = input ('Input the height in cm = ');
L = 0.5 * alas * tinggi;
fprintf ('The area of the triangle is = %5.2f cm^2 \n',
L );
disp (' ')
end
end

fprintf('Thank you friend\n');

Save your and run it. The values of input variables must be p or 2p or 3p ……
Print screen the result and paste it in the below box.

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

2. < untuk no. stambuk GANJIL> Make a branching control program (script)
to calculate the determinant and roots of a quadratic equation. Remember
that there are 3 types of roots depend on the value of the determinant, i.e.:
(1)two real numbers solution, (2)one real number solution, and (3)two
complex numbers solution. Use if-else to determine the right type of root.
Note: don’t use roots function to calculate the roots.

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

Task04-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. The values of input
variables must be p or 2p or 3p ……. If there is no mistake in your program, copy
and paste the script in the below box.

%Task-4b: Branching Control by using IF-ELSE on Matlab


clc;
disp
('***********************************************************')
;
disp ('* Branching Control by using IF-ELSE on Matlab
*');
disp ('* by: Putri Wandini, Student Reg. No.: 44419019
*');
disp
('***********************************************************')
;
disp (' ');
clear all;
disp(' ');
fprintf ('This program is for calculate the determinant and
roots of a quadratic equation\n');
a = input ('input a = ');
b = input ('input b = ');
c = input ('input c = ');

D = b^2 - 4 * a * c;
fprintf ('determinant of %dx^2 + (%dx) + (%d) = %.3f\
n',a,b,c,D);
if D > 0
x1=(-b+sqrt(D))/(2*a);
x2=(-b-sqrt(D))/(2*a);

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

fprintf ('the roots of the quadratic equation is two


real numbers solution\n');
fprintf ('the roots of the quadratic equation are value
of x1 = %.3f and value of x2 = %.3f\n',x1,x2);
elseif D == 0
x1 = (-b)/(2*a);
fprintf ('the roots of the quadratic equation is one
real number solution\n');
fprintf ('the roots of the quadratic equation are value
of x1 = x2 = %.3f \n ',x1);
else
x1 =(-b/(2*a))+(sqrt(D))/(2*a);
x2 =(-b/(2*a))-(sqrt(D))/(2*a);
fprintf ('the roots of the quadratic equation is two
complex numbers solution\n');
fprintf ('the roots of the quadratic equation are value
of x1 = %.3f and value of x2 = %.3f\n\n',x1,x2);
end

fprintf ('================= :) there are many ways to go to


Rome :) ===================');

Print screen the result of type 1 (two real numbers solution) and paste it in the
below box. ******For example: a=p, b=3p, c=2p ******

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

Print screen the result of type 2 (one real number solution) and paste it in the
below box. ******For example: a=p, b=2p, c=p ******

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

Print screen the result of type 3 (two complex numbers solution) and paste it in the
below box. ******For example: a=p, b=2p, c=3p ******

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

3. < untuk no. stambuk GENAP> Make a branching control program (script)
to calculate the charge of used electricity for a month. Consider the 3 types
of customers’ power, i.e.: (1)450 VA, (2)900VA-RTM, and (3)1300 VA. Use if-
else to determine the right type of customers’ power.

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

Notice:
Input Variables:
ID Pelanggan, Daya, Pemakaian bulan lalu (kWh), Pemakaian bulan ini (kWh)
Process:
kWh = Pemakaian bulan ini (kWh) - Pemakaian bulan lalu (kWh)
Charge (Rp) = Tarif (/kWh) * kWh
Output: Charge (Rp)

Tarif (/kWh) is determined based on Daya as follows:


1. If Daya is 450 VA then Tarif (/kWh) is Rp 415
2. If Daya is 900 VA-RTM then Tarif (/kWh) is Rp 1352
3. If Daya is 1300 VA then Tarif (/kWh) is Rp 1467.28

Save your script with an appropriate name and run it. The values of input
variables must be p or 2p or 3p ……. If there is no mistake in your program, copy
and paste the script in the below box.

Print screen the result of type 1 (450 VA) and paste it in the below box.

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

Print screen the result of type 2 (900VA-RTM) and paste it in the below box.

Print screen the result of type 3 (1300 VA) and paste it in the below box.

switch - case

4. Make an M-file (script) to to make a branching control by using switch-case.

Task04-12
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).

%Task-4c: Branching Control by using switch-case on Matlab


clc;
disp
('****************************************************************
');
disp ('* Branching Control by using switch-case on Matlab*');
disp ('* by: (your name), Student Reg. No.: (….…)
*');
disp
('****************************************************************
');

clear all;
clc;

fprintf('Choose a program:\n');
fprintf('1: Calculate the area of a square \n');
fprintf('2: Calculate the area of a rectangle \n');
fprintf('3: Calculate the area of a circle \n \n');
pilihan=input('What is your choice? ');

switch pilihan
case 1
a=input('Input the lenght of the square (in cm): ');
luas=a*a;
fprintf('The area of the square is %3.2f cm^2\n',luas);
case 2
a=input('Input the lenght of the rectangle (in cm): ');
b=input('Input the width of the rectangle (in cm): ');
luas=a*b;
fprintf('The area of the rectangle is %3.2f cm^2\n',luas);
case 3
r=input('Input the radius of the circle (in cm): ');
luas=3.14*r^2;
fprintf('The area of the circle is %3.2f cm^2\n',luas);
otherwise
fprintf('Re-input the right choice...!!')
end
fprintf('Thank you \n');

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

Save your script and run it. Print screen the result and paste it in the below box.

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

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

5. <untuk no. stambuk GANJIL> Make a program (script) to calculate the


total resistance of some connected resistors. Remember that there are 3
types of resistor circuits, i.e.: (1)series, (2)parallel, and (3)combination of
series and parallel. Use switch-case to select a type of resistor circuit (in
form of menu).

Save your script with an appropriate name and run it. The values of input
variables must be p or 2p or 3p ……. If there is no mistake in your program, copy
and paste the script in the below box.

Print screen the result of case 1 (series) and paste it in the below box.

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

Print screen the result of case 2 (parallel) and paste it in the below box.

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

Print screen the result of case 3 (combination of series and parallel) and paste it
in the below box.

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

6. <untuk no. stambuk GENAP> Make a program (script) to convert a


temperature in oC to other units of temperature. In this case, please make
three conversions, i.e.: (1)convert from C to R (2) convert from C to F and (3)
convert from C to K. Use switch-case to select a type of temperature
conversion (in form of menu).

Task04-19
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. The values of input
variables must be p or 2p or 3p ……. If there is no mistake in your program, copy
and paste the script in the below box.

Print screen the result of case 1 (CR) and paste it in the below box.

Print screen the result of case 2 (CF) and paste it in the below box.

Print screen the result of case 3 (CK) and paste it in the below box.

7. Congratulation, you had just finished your fourth task. 

---end of T4---

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

Task04-21

You might also like