0% found this document useful (0 votes)
29 views11 pages

Exp 1

This document provides an introduction to MATLAB including basic syntax, variables, arrays, matrices and common mathematical functions. It describes how to perform basic operations and defines several important terms. Various tables are included that list operators, commands, predefined variables and functions.

Uploaded by

jarksumit21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views11 pages

Exp 1

This document provides an introduction to MATLAB including basic syntax, variables, arrays, matrices and common mathematical functions. It describes how to perform basic operations and defines several important terms. Various tables are included that list operators, commands, predefined variables and functions.

Uploaded by

jarksumit21
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 11

Experiment 1 Lab Manual

American International University- Bangladesh


Department of Electrical and Electronic Engineering
EEE1234: 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 Lab 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
clear x Remove x from the workspace
copyfile Copy file or directory
delete Delete files
dir exist Display directory listing
help Check if variables or functions are defined
lookfor Display help for MATLAB functions
mkdir Search for specified word in all help entries
movefile Make new directory Move
pwd file or directory Identify
rmdir current directory Remove
type directory
what Display contents of file
which List MATLAB files in current directory
who Locate functions and files
whos
© Dept. of EEE, Faculty of Display
Engineering,variables currently
American International in the workspace
University-Bangladesh (AIUB)
2 Display information on variables in the workspace
Experiment 1 Lab Manual

Table 5: Predefined variables and math constants

Variable Description

ans Value of last variable (answer)


eps Floating-point relative accuracy
i Imaginary unit 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.
© Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB)
3
Experiment 1 Lab Manual

If x ≥0 this is the largest integer which


is ≤x.
fix(x) If x < 0 this is the smallest integer
which is ≥x.

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)

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


4
Experiment 1 Lab Manual

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.

Arrays: Vector and Matrix Calculations


Generating Matrices
To generate the matrix below

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

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


5
Experiment 1 Lab 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=

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


6
Experiment 1 Lab Manual

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

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).
© Dept. of EEE, Faculty of Engineering, American International University-Bangladesh (AIUB)
7
Experiment 1 Lab Manual

For example
> b = ones(3, 5)

returns with
b=

1 1 1 1 1

1 1 1 1 1

1 1 1 1 1

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

1 0 0

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 Lab 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

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


9
Experiment 1 Lab Manual

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.

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


10

You might also like