Lab 5
Lab 5
Lab 5
I. Introduction
PROBLEM SOLVING
In this chapter, we will learn the advantages of having a computer to solve engineering
problems. In problem analysis, there are three elements that we need, the definition or
model of the problem, the input and the output specifications. To solve such problem,
an algorithm design is needed. For which, the tools and flowchart will be needed. Lastly,
the computer solution a working source code is needed using a programming
language, and our case, the GNU Octave.
Figure 5.1 below shows the cycle at which an engineer usually sees a problem. This is
the problem analysis cycle for solving problems using a programming language. When
the problem is identified, the algorithm is then formulated and its corresponding
flowchart. From this, a machine-readable code will be written based on the flowchart
created. A solution is then finally made. However, as an engineer, our inquisitive minds
never fail to see the problem in a wider perspective after finding a solution. This will
then lead for us to see more interesting problems that needs specific solutions. This
makes the problem analysis process a cycle. Yes, it may be tedious for an engineer,
but that is our nature, because we are problem solvers.
Problem
Solution Algorithm
Code Flowchart
As an example, let as take the following problem. This is the classic area of the
triangle problem as shown in Figure 5.2. Here the following can be defined:
By writing the following code, one can easily solve the problem at hand.
b=input('Input b:');
h=input('Input h:');
Area=0.5*b*h
This code can be easily identified if you have correctly identified the parts of the
previously presented flowchat. But what should be the parts of a flowchart? These are
listed below:
Another example will utilize the previously learned for statement. In this case, the
following array should be displayed:
P a g e 37 | 70
Given the sequence, you can identify that it has 2 as a base and the first element has
an exponent of 0 and last having 5.
start
stop
FUNCTIONS
You can create a user-defined function to save time in performing routine process. This
is different from the built-in functions where it does a non-editable function. Here, the
user-defined function can be edited by the user to make it execute the desired function
or process. Having a user-defined function will greatly lessen the number of lines in
your code, which is a good thing. Its pattern is as follows:
function[output_variables]=function_name(input_variables)
See the example in Time to Act: Functions to have an overview on how functions
work.
Additionally, there are specific functions that performs special processes like reading
and writing from an external Ms Excel file or .txt file. For the MsExcel file, GNU Octave
can read it using xlsread function and using xlswrite lets you write or edit values
in the MsExcel file. For the text file, the fopen and fclose functions can be used. You
can also study on the fread and fwrite functions also for further readings and
practice.
Functions for spreadsheet style I/O (.xls .xlsx .sxc .ods .dbf .wk1 etc.) are provided in
the io package. See <https://octave.sourceforge.io/io/>.
P a g e 38 | 70
However, in this manual, since most of these functions vary as the files like MsExcel
updates its file extensions (e.g. xls to xlsx) and for better compatibility, we will be using
only .txt files and .csv files. Text files can be easily made in the note pad and it has a
wide compatibility. For .csv files, the MsExcel files can be easily converted to .csv files
by saving it as .csv or exporting it as .csv. Feel free to test and practice other importing
data functions as you go through the laboratory exercises.
II. Objectives
At the end of the activity, the students are expected to:
1. Analyze various problems relative to power systems;
2. Create an algorithm to solve problems;
3. Use GNU Octave as a programming language to solve problems;
4. Formulate a function script; and
5. Demonstrate opening and accessing data from MsExcel and text files.
III. Materials/Equipment
Computer with a full version of GNU Octave installed
IV. Procedure
1. Start the GNU Octave as instructed at the start of Laboratory Exercise 1 Part IV.
2. Create an m-file and write the following code:
X=input(‘Enter X: ‘);
if X<0
disp(‘negative’)
elseif X==0
disp(‘zero’)
else
disp(‘positive’)
end
P a g e 39 | 70
clc;clear;
Tip: In the sample codes, clc and clear commands are always
present to ensure that the previous data and lines are deleted
before running the code. The % when included in line starts a
comment line, it is ignored by the machine and displayed in
green. Use this to explain or have some markers.
10. Execute the file again and try letters and see the results. Record it in Part V.4.
1. Create a new file in the editor window and write the following code:
CtoF(0)<enter>
X=CtoF(37)<enter>
y=CtoF(X)<enter>
y=CtoF(100)<enter>
help CtoF<enter>
P a g e 40 | 70
TIME TO ACT Open and Access CSV and Text Files
4. As you can notice, this is a typical network data set for a 4-bus power system.
Save this file as sample_data_set.csv. Note: Make sure to save this file in the
current directory, this may be used in future laboratory exercises.
5. In GNU Octave, create a new file in the editor window and write the following
code:
clear;clc;
Z=importdata('sample.txt')
Y=importdata('sample_line_data.csv')
X= csvread('sample_line_data.csv')
6. Save and execute the script and observe its output in the command window and
workspace window. Record the responses in Part V.5.
7. When finished, close Octave and log off and shut down the computer.
P a g e 41 | 70
V. Observation Matrix
Instructions: Put your observations and calculated values in the space provided
1.
Problem Solving
2.
Problem Solving
3.
Problem Solving
4.
Problem Solving
5.
Functions
CtoF(0)<enter>
X=CtoF(37)<enter>
y=CtoF(X)<enter>
y=CtoF(100)<enter>
6.
Open and Access CSV and Text Files
P a g e 42 | 70
VI. Guide Questions
In this part, please provide your answers to the guide questions in the space provided.
1. In the previous example (for statement to display X = [1, 2, 4, 8, 16, 32]), rewrite
the code using while statement.
3. Why do you think functions can shorten your main script or code?
P a g e 43 | 70
PRACTICE PROBLEMS
In this part, you are encouraged to solve the following problems at home.
You are not required to submit the code and responses of these problems
but it is especially designed to prepare you for the succeeding activities.
Read, practice and enjoy learning GNU Octave. Good luck!
1. Create the shortest m-file to solve for X, Y and Z. Use only functions
or operators appeared in this note.
3X + 4Y + 5Z = 49
4X + 7Y + 3Z = 51
5X + 6Y + nZ= 61
where n is input variable
2. Write the flowchart and create the shortest m-file to count the number of elements in
matrix C that have the magnitude above an input: n( 1+ 1j)
Write the flowchart and create the shortest m-file to sort the elements in A in ascending
order. DO NOT use the sort(), max(), min() function provided by GNU Octave.
5. Create the shortest m-file to convert the input value of degree Celsius to Fahrenheit and
vice versa. The input format would be “input:##F” or “input:##C” and the program should
return the converted degree depends on the last alphabet.
P a g e 44 | 70