You are on page 1of 7

Laboratory Exercise 3

Matrices and Logic

I. Introduction

Matrices are important in power systems calculations. The complexity of the power
systems is lessened when the totality of the system is modelled in a matrix form. Most
of the parameters of the power system can be easily plotted and recognized when
represented in a matrix form. As an electrical engineer, one must be familiar and, if
possible, master the different operations in this form. In this section, we will be
introduced to the intricacies of GNU Octave operations for matrices.

The matrix will be defined by three data types:


1. Number of rows
2. Number of columns
3. Type of data (real, integer, Boolean, string or polynomial)

II. Objectives
At the end of the activity, the students are expected to:
1. Recall the different types of matrices;
2. Recognize the importance of matrices and logic in power systems;
3. Demonstrate basic operations with matrices in GNU Octave.

III. Materials/Equipment
Computer with a full version of GNU Octave installed

IV. Procedure

TIME TO ACT Creating Matrices and Accessing its Elements

1. Start the GNU Octave as instructed at the start of Laboratory Exercise 1 Part IV.
2. Create an m-file with the filename “OctaveLabEx3_Surname”.
3. Type and run: Record its response in Part V.1.

A= [9, 8, 7; 6, 5, 4; 3, 2, 1]

The square brackets are used to mark the beginning and end of matrix, commas or spaces or
tabs separate the values in different columns and semicolons separate values of different rows.
Typing B=[] creates an empty matrix.

There are different functions which can generate matrices. Browse the help menu to have a
better understanding of the following functions. Some are as follows:

1. “eye” - generates an identity matrix


2. “linspace” - generates linearly spaced vector
3. “ones” - generates matrix made of ones
4. “zeros” - generates matrix made of zeroes
5. “rand” - generates random numbers

4. The elements of the matrix can be accessed by typing one of the following, given that the
matrix is name is A with size m by n:
• Type A to access the whole matrix
• Type A(m,n) to access a specific element on the matrix located in mth row and
nth column
• Type A(:,n) to access all rows in the nth column

P a g e 21 | 70
• Type A(m,:) to access all columns in the mth row.
To perform mathematical operations in matrices, one must follow the rules applicable for
matrix operations (i.e. must be same size).

Access the following elements in Matrix A and write your answer in Part V.2.
1. 8th element;
2. Row 2 and Column 3;
3. First element; and
4. Last element.

Practice the following in the command window and write the responses in Part V.3.

ants=zeros(2)<enter>
birds=ones(2,6)<enter>
cat=[1 1 2;3 4 5;2 3 3] <enter>
dog=cat(:,1) <enter>
elep=cat(2:3) <enter>
fish=cat(1:2,2:3) <enter>
gun=cat([1 3],[1 3]) <enter>
horse=cat([1 3],2:3) <enter>
ice=sum(cat) <enter>
kid=diag(cat) <enter>
jar=sum(cat,2) <enter>
luck=car*(1+i) <enter>
man1=transpose(cat) <enter>
new1=(cat)’ <enter>
man1(:,2)=[] <enter>
man2=transpose(luck) <enter>
new2=(luck)’ <enter>
new1(1,:)=[] <enter>
zeros(4) <enter>
ones(5) <enter>
diag([1 2 3]) <enter>
diag([1;2;3]) <enter>
eye(4) <enter>
eye(5,3) <enter>
rand(4) <enter>
rand(5,3) <enter>
randn(4) <enter>
randn(5,3) <enter>
magic(5) <enter>
help rand<enter>

TIME TO ACT Matrix Operations

1. Clear both the workspace and the command window.


2. Use the command window and perform the following basic operations in matrices.
You don’t need to record the results but make sure to study them carefully.

a1=[81 6;3 5 7;4 9 2] <enter>


a2=a1*10<enter>
[a1 a2] <enter>
[a1;a2] <enter>
a2*a1<enter>
a2.*a1<enter>
a2/a1<enter>

P a g e 22 | 70
a2./a1<enter>
a1^2<enter>
a1.^2<enter>
a1\a2<enter>
a2/a1<enter>
b1=[28;34;28] <enter>
result1=a1\b1<enter>
result2=b1/a1<enter>
result3=inv(a1)*b1<enter>
det(a1) <enter>

TIME TO ACT Logic

Logical values are supported in GNU Octave. This means that when you compare two
variables, the resulting value will be a logical value, either true or false depending on the
comparison. It can either be one or zero. Verify it on the following exercises. Write the answers
on Part V.4.

These are the logical operators in GNU Octave:

< less than


<= less than or equal
> greater than
>= greater than or equal
== equal
~= not equal
& logical AND
| logical OR
~ logical complement (NOT)
xor exclusive OR

5==5 <enter>
6<5 <enter>
5==5|6==5<enter>
xor(3>2,7==5) <enter>
m=[1 2 3 4 5] <enter>
n=[5 4 3 2 1] <enter>
m<n<enter>
~(m==n) <enter>

P a g e 23 | 70
V. Observation Matrix

Instructions: Put your observations and calculated values in the space provided
1.
Creating Matrices and Accessing its Elements

2.
Creating Matrices and Accessing its Elements
1. 2. 3. 4.

3.
Creating Matrices and Accessing its Elements
ants=zeros(2)<enter>

birds=ones(2,6)<enter>

cat=[1 1 2;3 4 5;2 3 3] <enter>

dog=cat(:,1) <enter>

elep=cat(2:3) <enter>

fish=cat(1:2,2:3) <enter>

gun=cat([1 3],[1 3]) <enter>

horse=cat([1 3],2:3) <enter>

ice=sum(cat) <enter>
kid=diag(cat) <enter>

jar=sum(cat,2) <enter>
luck=cat*(1+i) <enter>

P a g e 24 | 70
man1=transpose(cat) <enter>

new1=(cat)’ <enter>

man1(:,2)=[] <enter>

man2=transpose(luck) <enter>

new2=(luck)’ <enter>

new1(1,:)=[] <enter>

zeros(3) <enter>

ones(2) <enter>

diag([1 2 3]) <enter>

diag([1;2;3]) <enter>

eye(4) <enter>

eye(4,3) <enter>

rand(4) <enter>

rand(5,3) <enter>

P a g e 25 | 70
randn(4) <enter>

randn(5,3) <enter>

magic(5) <enter>

help rand<enter> Describe:

4.
Logic
5==5 <enter>
6<5 <enter>
5==5|6==5 <enter>
xor(3>2,7==5) <enter>
m=[1 2 3 4 5] <enter>
n=[5 4 3 2 1] <enter>
m<n <enter>
~(m==n) <enter>

P a g e 26 | 70
VI. Guide Questions
In this part, please provide your answers to the guide questions in the space provided.

1. Describe your experience in using GNU Octave for matrices. Compared to


using your calculator or Ms Excel, is it better or worse?

2. What are some rules in performing matrix operations?

P a g e 27 | 70

You might also like