Professional Documents
Culture Documents
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).
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
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.
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
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)
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
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).
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
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
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 (CR) and paste it in the below box.
Print screen the result of case 2 (CF) and paste it in the below box.
Print screen the result of case 3 (CK) and paste it in the below box.
---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