Workshop 3 - Procedural Programming & Loop Statement

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

UST ELECTRICAL ENGINEERING DEPARTMENT

WORKSHOP GUIDE

WORKSHOP 3: Procedural Programming & Loop


Statements
I. OBJECTIVES:
1. To develop MATLAB programs that utilize key programming concepts, including script and
function M files, input and output commands, and control flow statements.
2. To strengthen their problem-solving skills and enhance their understanding of MATLAB
programming.

II. INSTRUMENTS AND COMPONENTS:

MATLAB software

III. PROCEDURE:

Part 1: Script M-File

Problem. You are given three resistors connected in series. Write a MATLAB script to
calculate the total resistance in the circuit. The values of the resistors are provided in
the script.

Instruction:

1. Create a script named total_resistance_series.m.


2. Declare variables for the values of three resistors.
3. Calculate the total resistance using the formula : 𝑹𝑻𝑶𝑻𝑨𝑳 = 𝑹𝟏 +𝑹𝟐 +𝑹𝟑
4. Display the total resistance on the screen using disp() or fprintf().

Part 2: Function M-File

Problem. Create a function that calculates the power dissipated by a resistor given the
voltage across the resistor and its resistance.

Instruction:

1. Create a function file named calculate_power.m.


2. The function should take two inputs: voltage (V) and resistance (R).
𝑽𝟐
3. Calculate the power using the formula: 𝑷= 𝑹
4. Return the calculated power from the function.

Part 3: Input and Output Commands

Problem. Use input commands to ask the user for voltage and resistance, then calculate
the current using Ohm’s Law. Finally, output the current to the user.

Instruction:

1. Create a script named calculate_current.m.


2. Use the input() function to prompt the user for voltage and resistance.

Workshop 3– Procedural Programming Page 1 of 3


UST ELECTRICAL ENGINEERING DEPARTMENT
WORKSHOP GUIDE

𝑽
3. Calculate the current using the formula: 𝑰= 𝑹
4. Display the current using disp() or fprintf().

Part 4: Control Flow Statement (IF-ELSE)

Problem. A resistor consumes power in a circuit. If the power exceeds 50 watts, the
program should display a warning. Otherwise, it should display the power consumption.

Instruction:

1. Create a script named check_power_limit.m.


2. Use input() to take user inputs for voltage and resistance.
3. Use the calculate_power() function from Exercise 2 to calculate the power.
4. Use an if-else statement to check if the power exceeds 50 watts.
5. Display a warning if the power is above 50 watts, otherwise, display the
calculated power.

Part 5: Control Flow Statement (SWITCH)

Problem. Provide DC circuit that can be classified based on the number of resistors
connected in the circuit. Based on the number of resistors, the circuit can be categorized
into the following:

1. If there is only 1 resistor, it's a simple resistive circuit.


2. If there are 2 resistors, the user will choose whether the resistors are connected
in series or parallel, and the program will calculate the total resistance.
3. For 3 or more resistors, output "Complex Circuit."

Instruction:

1. Create a script named circuit_type_switch.m.


2. Prompt the user to input the number of resistors.
3. Use a switch statement to determine the circuit type based on the number of
resistors.
4. If there are 2 resistors, prompt the user to input whether they are connected in
series or parallel, and calculate the total resistance accordingly.
5. For 3 or more resistors, output "Complex Circuit."
6. Display the result for all scenarios.

Part 6: Loop Statement (WHILE Loop)

Problem. Use a while loop to continuously prompt the user to input voltage and
resistance, calculate the power, and display the result. The program should only stop if
the user enters 0 for the voltage.

Instruction:

1. Create a script named power_calculator_loop.m.


2. Use a while loop to repeatedly ask the user for voltage and resistance.
3. Calculate the power using the calculate_power() function from Exercise 2.

Workshop 3– Procedural Programming Page 2 of 3


UST ELECTRICAL ENGINEERING DEPARTMENT
WORKSHOP GUIDE

4. Exit the loop when the user enters 0 for voltage.

Part 7: Loop Statement (For Loop)

Problem. Use a for loop to calculate the total resistance of a set of resistors connected
in series. The user will input the number of resistors and then input the resistance
values one by one. The program will calculate the total resistance of the series circuit
and display it.

Instruction:

1. Create a script named series_resistance_calculator.m.


2. Ask the user for the number of resistors in the series circuit.
3. Use a for loop to prompt the user to input the resistance value of each resistor.
4. Calculate the total resistance of the series circuit using the formula 𝑹𝑻𝑶𝑻𝑨𝑳 =
𝑹𝟏 +𝑹𝟐+ ⋯ + 𝑹𝒏 .
5. Display the total resistance at the end.

Deliverables:

1. Each group is required to submit their MATLAB files (.m) for all seven tasks listed
above. Submit all seven .m files, properly named, in a single compressed folder
labeled with your group name.

2. Groups are encouraged to collaborate and discuss solutions within their teams.
However, each group will need to present or demonstrate their solutions and
explain their approach to the class for their assigned problem:

• Group 1: Part 1
• Group 2: Part 2
• Group 3: Part 3
• Group 4: Part 4
• Group 5: Part 5
• Group 6: Part 6
• Group 7: Part 7

3. Along with the MATLAB files, each group should prepare a presentation in
PowerPoint format. The slides should adhere to the following guidelines:

• Title slide: Include the group number, the names of all members, and the
assigned task.
• Introduction: Briefly describe the problem or task you are addressing.
• Approach: Explain the logic, methods, and any algorithms used to solve the
task.
• Code snippets: Include relevant MATLAB code snippets and explain what
each part of the code does.
• Results: Show the results generated from your MATLAB code (e.g., output
values, plots, etc.).

Workshop 3– Procedural Programming Page 3 of 3


UST ELECTRICAL ENGINEERING DEPARTMENT
WORKSHOP GUIDE

• Conclusion: Summarize your solution and findings.

Keep your slides concise and clear, limiting text where possible. Visual aids, such
as diagrams or charts, are encouraged.

4. Be prepared to explain your approach, the logic behind your solution, and make
sure your code works during the implementation when presenting in class.

---- END OF WORKSHOP ----

Workshop 3– Procedural Programming Page 4 of 3

You might also like