Exp 1-Student Manual

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

Experiment 1 Student Manual

American International University- Bangladesh


Department of Electrical and Electronic Engineering
EEE 4103 : Power Systems Analysis Laboratory

Experiment Name: Introduction to MATLAB.

Abstract:
MATLAB is a very useful and widely used tool in numerous divisions of engineering.
In Digital Signal Processing, MATLAB is an integral part.
It plays an important role in analyzing and performing various operations on signal processing.

Introduction:
The purpose of this experiment is to gain familiarity with MATLAB and build some basic skills in
MATLAB language. The very basics of MATLAB, i.e. writing codes, computing mathematical
calculations, the concept of arrays, plotting data, creating functions etc. are described in this
experiment.

Theory & Methodology:


MATLAB is a high-performance language for technical computing. The name MATLAB stands for
MATRIX LABORATORY. In Academic environment, it is the standard instructional tool for
introductory and advanced courses in mathematics, engineering and science. There are some basic
characters descriptions explained in the table below which are commonly using in MATLAB.

Table 1: Arithmetic operators and special characters

Character Description

+ Addition
− Subtraction
* Multiplication (scalar and array)
/ Division (right)
ˆ Power or exponentiation
: Colon; creates vectors with equally spaced elements Semi-
; colon; suppresses display; ends row in array Comma;
, separates array subscripts
... Continuation of lines
% Percent; denotes a comment; specifies output format Single
quote; creates string; specifies matrix transpose Assignment
= operator
() Parentheses; encloses elements of arrays and input arguments
[] Brackets; encloses matrix elements and output arguments

© Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 1


Experiment 1 Student Manual

Table 2: Array operators

Character Description

.* Array multiplication
./ Array (right) division
.ˆ Array power
.\ Array (left) division
.’ Array (nonconjugated) transpose

Table 3: Relational and logical operators

Character Description

< Less than


≤ Less than or equal to
> Greater than
≥ Greater than or equal to
== Equal to
& Logical or element-wise AND
| Logical or element-wise OR
&& Short-circuit AND
|| Short-circuit OR

Table 4: Managing workspace and file commands

Command Description

cd Change current directory


clc Clear the Command Window
clear(all) Removes all variables from the workspace
clearx Remove x from the workspace
copyfile Copy file or directory
delete Delete files
dir Display directory listing
exist Check if variables or functions are defined
help Display help for MATLAB functions
lookfor Search for specified word in all help entries
mkdir Make new directory Move
movefile file or directory Identify
pwd current directory Remove
rmdir directory
type Display contents of file
what List MATLAB files in current directory
which Locate functions and files
who Display variables currently in the workspace
whos Display information on variables in the workspace

© Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 2


Experiment 1 Student Manual

Table 5: Predefined variables and math constants

Variable Description

ans Value of last variable (answer) Floating-


eps point relative accuracy Imaginary unit
i of a complex number
Inf Infinity (∞)
eps Floating-point relative accuracy
j Imaginary unit of a complex number
NaN Not a number
pi The number π (3.14159 . . .)

Variables
Variables are defined to store numerical values. A valid name for an OCTAVE variable is a
character string containing letters (upper or lower case), digits, and underscores where the first
character must be a letter.
For example, you can store the value 5 in the variable x by entering
> x = 5;
Note that nothing will be printed out because semicolons follow each command. If you want
everything printed out then type
>x=5
The output will be then
x=5

OCTAVE contains some predefined variables. Many of these are contained in the table
below.

Table 6: Some Common Real Mathematical Functions

Function Explanation

abs(x)
The absolute value of x.

exp(x) ex exp(x) ex

cos(x)
cos x
sin(x)
sin x
factorial(n)
n! for n a non-negative integer.
If x ≥0 this is the largest integer which
is ≤x.
fix(x) If x < 0 this is the smallest integer
which is ≥x.

© Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 3


Experiment 1 Student Manual

floor(x)
This is the largest integer which is ≤ x.
The natural log of x, i.e., logex.
log(x)
The common log of x, i.e., log10x.
log10(x)
The remainder of x/y. This is the same
rem(x, y) as mod(x, y) if x, y > 0

round(x) The integer which is closest to x.

If x > 0 this returns +1,


if x < 0 this returns -1, and
sign(x)
if x = 0 this returns 0

sqrt(x)
√x .
tan(x)
tan x.

Complex Numbers
There are standard commands for obtaining the real part, the imaginary part, and the complex
conjugate of a complex number or variable.
For example
> x = 3 - 4i
x = 3 - 4i
>real(x)
ans = 3
> imag(x)
ans = -4
> conj(x)
ans = 3 + 4i

Table 7: Some Common Complex Mathematical Functions

Function Explanation

abs(z)
The absolute value of z = x + iy.

The angle of z. This is calculated by atan2(y, x).


angle(z)

conj(z)
z*= x - iy.

imag(z)
The imaginary part of z, i.e., y.

real(z)
The real part of z, i.e., x.

© Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 4


Experiment 1 Student Manual

Arrays: Vector and Matrix Calculations


Generating Matrices
To generate the matrix below

1 2 3
 
 4 5 6
7 8 9

type

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

returns with

A=

1 2 3

4 5 6

7 8 9

or type

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

A=

1 2 3

4 5 6

7 8 9

The transpose of a matrix A, denoted ted as AT, is calculated by A.’(i.e., a period followed by a
single quote mark).

For example

The vector

1
2
3
X= 4 = [1 2 3 4 5 6]T
5
6
7

© Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 5


Experiment 1 Student Manual

can be entered as
> x=[1 2 3 4 5 6].'

returns with
x=

For example, to create the column vector x=(1,2,3, …,10), enter


> x=[1:10]'

returns with
x=

10

Also the length of x can be found using length (x) returns the number of elements in vector x.
For example
> length(x)

ans = 10

© Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 6


Experiment 1 Student Manual

Elementary Matrices
For example,
> b = zeros(5)
or
> b = zeros(5, 5)

generates a 5X5 zero matrix.


type the following.
> b=zeros(3)

returns with

b=

0 0 0

0 0 0

0 0 0

Also,

> b = zeros(3, 5) generates a 3X5 zero matrix.

Type the following.

> b = zeros(3, 5)

returns with

b=

0 0 0 0 0

0 0 0 0 0

0 0 0 0 0

Similarly, you can generate a matrix with all ones by ones(n) or ones(m, n).
For example
> b = ones(3, 5)

returns with
b=

1 1 1 1 1

1 1 1 1 1

© Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 7


Experiment 1 Student Manual

11111

You can also generate the identity matrix, i.e., the matrix with ones on the main diagonal and
zeroes off of it, by using the command eye with the same arguments as above.
For example
> b=eye(3)

returns with

b=

Diagonal Matrix

100

0 1 0

0 0 1

Plot
plot(y) plots the columns of Y versus their index.

For example

x= linspace(0,2*pi,30);

> y=sin(x);

> plot(y), title('y vs x');

returns with

the following graph.

© Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 8


Experiment 1 Student Manual
Manual

Subplot
Create axes in tiled positions.

H = subplot(m,n,p) breaks the Figure window into an m-by-n matrix of small axes, selects the p-th axes for
the current plot, and returns the axis handle. The axes are counted along the top row of the Figure window,
then the secondrow, etc.

For example

> x= linspace(0,2*pi,30);

> y=sin(x);

> z=cos(x);

> subplot(2,1,1)

> plot(y), title('plot y vs x');

> subplot(2,1,2)

> plot(z), title('plot x vs x');

plots y on the top half of the window and z on the bottom half

and returns with

Software Requirement:

• MATLAB (2016a or onwards)


Experiment 1 Student Manual
Manual

Experimental procedure:

Write the matlab code and generate output for the following expressions-

1. M=5

2. N=6

3. P=M+Ni

4. Q=conjugate of P

5. R=real part of P

6. S=imaginary part of P

7. X=

8. Y=transpose matrix of x

9. A=5× 6 zero matrix

10. B=6× 4 ones matrix

11. C=5× 5 identity matrix

12. Generate a sinusoidal wave having magnitude of 6.

13. Generate a cosine wave having magnitude of 10.

14. Subplot waves in question 12 &13 in one single window.

Discussion and Conclusion:

In this experiment, a detailed introduction to MATLAB was given. The interface of MATLAB and
the basic building blocks were discussed. After the completion of this lab, students were able to
understand the structure of the software and will be able to write codes, plotting graphs, making
functions and compute simple and complex mathematical computations.

Reference(s):

[1] Kothari, D P, and I J Nagrath. Modern Power System Analysis. 3rd ed. New Delhi: Tata
McGraw-Hill, 2009. Print.
[2] AIUB Lab manual.
Experiment 1 Student Manual
Manual

© Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB) 10

You might also like