You are on page 1of 11

1

UNITED INTERNATIONAL UNIVERSITY (UIU)


Dept. Of Electrical & Electronic Engineering (EEE)

Course: EEE 110, Title: Simulation Laboratory


Lab Sheet 1
Introduction to MATLAB
What is MATLAB:
MATLAB stands for MATrix LABoratory .MATLAB is a software package for computation in
engineering, science, and applied mathematics. It offers a powerful programming language, excellent
graphics, and a wide range of expert knowledge. MATLAB is published by and a trademark of The
MathWorks, Inc.
Standard Windows :
When the MATLAB program starts, the following window opens which contains four smaller
windows .These windows are default and can be switched on or off if necessary.























Command
window
Command
History window
Current Directory &
Workspace window
Figure 1.1 : The default view of MATLAB desktop
2
Four most used MATLAB Windows
Command
Window
This is where you type and execute commands

Workspace
Window
This shows current variables and allows to edit variables by opening array editor (double
click), to load variables from files and to clear variables.
Current
Directory
window
This shows current directory and MATLAB files in current folder, provides with a handy
way to change folders and to load files.

History window This shows previously executed commands. Commands can be re-executed by double-
clicking.
Working on the command window :
(A)Using MATLAB as a calculator
You can use command window part for various calculations as if you would use a calculator: Type
expressions containing numbers, parenthesis and mathematical operations and press Enter to get the result.
Example :
>>2^sqrt(9)
ans =
8
>> a = 1. 2;
>> b=2. 3;
>> c=4. 5;
>> d=4;
>> a^3+sqr t ( b*d) - 4*c
ans =
- 13. 2388
Note the semicolon after each variable assignment. If you omit the semicolon, then MATLAB echoes back on
the screen the variable value.
(B)Assigning Values to Variables
To create a variable just use it on the left hand side of an equal sign. The following examples show how to
assign values to variables, x and y.
>>x=5
x =
5
>>y =log(2)
y =
0.6931
>>z =10;
<nothing is displayed. Why? Because of the semicolon used. But be confirmed that, a value of 10 is stored in
the variable z but does not display. >
Note that :
(1) You can use who command to list the currently active variables. For the preceding session this results in
>>who
Your variables are:
ans x y z
(2) Use clear command to delete variables from computer memory
>>clear x
3
>>x
??? Undefined function or variable 'x'.
(3) Suppress intermediate output with Semicolon.
MATLAB will print the result of every assignment operation unless the expression on the right hand side is
terminated with a semicolon.
(C)List of useful commands for managing variables :
Command Outcome
clear Removes all variables from memory.
clear x y z Removes only variables x,y,z from the memory .
who Displays a list the variables currently in the memory.
whos Displays a list the variables currently in the memory and their size together with
information about their bytes and class.
(D)Two important command to ensure fairness and readability
Command Outcome
clc Clears contents on the command window ensuring blank working environment
% If used before any line or statement, program ignores the line or statement.
So used to insert any comment in the program.
(E)Predefined variables :
A number of frequently used variables are already defined when MATLAB is started. Some
of the predefined variables are :
Predefined
Variables
Meaning
ans A variable that has the value of the last expression that has not assigned to a specific
variable. If the user does not assign the value of an expression to a variable, MATLAB
automatically stores the result in ans .
pi Value of the number t .
eps The smallest difference between two numbers. Its value is 2.2204e-016 .
inf Used for infinity.
i or j Defined as 1
NaN Stands for Not-a-Number.Used when MATLAB cannot determine a valid numeric
value.For example 0/0.
(F)Complex numbers :
MATLAB also supports complex numbers. The imaginary number is denoted with the symbol i or j, assuming
that you did not use these symbols anywhere in your program (that is very important!). Try the following:
>>z =3 +4i % note that you do not need the * after 4
>>conj(z) % computes the conjugate of z
>>angle(z) % computes the phase of z
>>real(z) % computes the real part of z
>>imag(z) % computes the imaginary part of z
>>abs(z) % computes the magnitude of z



4
(G)Elementary Math functions used in MATLAB:
Function Description Example
sqrt(x) Square root >>sqrt(16)
ans =
4
exp(x) Exponential (e
x
) >>exp(3)
ans =
20.0855
abs(x) Absolute value >>abs(-4)
ans =
4
>>abs(3+4j)
ans =
5
log(x) Natural logarithm (Base e ) >>log(1000)
ans =
6.9078
log10(x) Base 10 logarithm . >>log10(1000)
ans =
3
factorial(x) The factorial function x!
(x must be a positive integer.)

>>factorial(5)
ans =
120
sin(x) Sine of angle x (x in radians) >>sin(pi/6)
ans =
0.5000
cos(x) cosine of angle x (x in radians) >>cos(pi/4)
ans =
0.7071
tan(x) Tangent of angle x (x in radians) >>tan(pi/3)
ans =
1.7321
tand(x) Cotangent of angle x (x in degree) >>tand(60)
ans =
1.7321
asin(x) Inverse sine of x >>asin(sqrt(3)/2)
ans =
1.0472
(Note that result is in radians)
round(x) Round to the nearest integer >>round(17/5)
5
ans =
3
ceil(x) Round towards infinity. >>ceil(11/5)
ans =
3
floor(x) Round towards minus infinity. >>floor(9/4)
ans =
2
rem(x,y) Returns the remainder after x is divided
by y.
>>rem(13,5)
ans =
3
sign(x) Signum function.
Returns 1 if x>0
Returns -1 if x<0
>>sign(-5)
ans =
-1
(H)Creating Vectors and Matrices
The array or matrix is a fundamental form that uses to store and manipulate data.
Unlike C programming language , array index starts from 1 in MATLAB instead of 0 in C programming.
To create a matrix In MATLAB, use the brackets. Numbers (or variables) inside the brackets can be separated
by commas, spaces, or semicolons, where commas and spaces are row separators; semicolons are column
separators. For example,
>>a=[2,5,7]
a =
2 5 7
>>b=[3,4,a]
b =
3 4 2 5 7
>>c=[2 3;4 5]
c =
2 3
4 5
(I)Colon Operator
MATLAB colon operator is a compact way to create vectors. For example,
>>A=1:0.1:1.5
A =
1.0000 1.1000 1.2000 1.3000 1.4000 1.5000
Where 0.1 is the increment, if omitted, it is assumed to be one.
>>B =1:5
B =
1 2 3 4 5


(J)Create some basic matrices using MATLAB built-in functions: ones, zeros, eye
6
Commands Description Example
zeros(m,n) A matrix of dimension mn having all elements 0
>>zeros(3,4)
ans =
0 0 0 0
0 0 0 0
0 0 0 0
ones(m,n) A matrix of dimension mn having all elements 1
>>ones(3,4)
ans =
1 1 1 1
1 1 1 1
1 1 1 1
eye(n) An identity matrix of dimension nn
>>eye(4)
ans =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
(K)Determine size of a matrix
Command Description
Example
size(x) Determines size of the matrix
>>x=[1 2 3;4 5 6;7 8 9]
x =
1 2 3
4 5 6
7 8 9
>>size(x)
ans =

3 3

(L)Addressing Vectors or Matrices:
Command Meaning Example when
A =
1 3 5 7 9 11
2 4 6 8 10 12
3 6 9 12 15 18
4 8 12 16 20 24
5 10 15 20 25 30
A(:,n) Refers to the elements in the rows of column n of the matrix
A
>>B=A(:,3)
B =
5
6
9
12
15
A(n,:) Refers to the elements in all the columns of row n of
the matrix A.
>>C=A(2,:)
C =
2 4 6 8 10 12
A(:,m:n) Refers to the elements in all the rows between columns m
and n of the matrix A
>>E=A(:,2:4)
E =
3 5 7
4 6 8
6 9 12
8 12 16
10 15 20
7
A(m:n,:) Refers to the elements in all the columns between rows
m and n of the matrix A.
>>D=A(2:4,:)
D =
2 4 6 8 10 12
3 6 9 12 15 18
4 8 12 16 20 24
A(m:n,p:q) Refers to the elements in rows m trough n and columns
p through q of the matrix A.
>>F=A(1:3,2:4)
F =
3 5 7
4 6 8
6 9 12

(M) Generation of random numbers :
Command Description Example
rand Generates a single random number between 0 and 1 >>rand
ans =
0.2311
rand(1,n) Generates an n elements row vector of random
numbers between 0 and 1
>>a=rand (1,4)
a =
0.6068 0.4860 0.8913 0.7621
rand(n) Generates an nn matrix of random numbers
between 0 and 1
>>b=rand(3)
b =
0.4568 0.4447 0.9218
0.0158 0.6154 0.7382
0.8214 0.7919 0.1763
rand(m,n) Generates an mn matrix of random numbers
between 0 and 1
>>c=rand(2,4)
c =
0.4568 0.4447 0.9218 0.3529
0.0158 0.6154 0.7382 0.9533
randperm(n) Generates a row vector with n elements that are
random permutation of integers 1 through n .
randperm(8)
ans =
8 2 7 4 3 6 5 1

Mathematical operations on Arrays
(A)Addition and Subtraction of Arrays :
Mathematical Review:
If
(

=
23 22 21
13 12 11
A A A
A A A
A and
(

=
23 22 21
13 12 11
B B B
B B B
B
Then ,
(

+ + +
+ + +
= +
B A B A B A
B A B A B A
B A
23 23 22 22 21 21
13 13 12 12 11 11

And,
(



=
B A B A B A
B A B A B A
B A
23 23 22 22 21 21
13 13 12 12 11 11

MATLAB Example :
>>A=[1 2 3;4 5 6];
>>B=[8 9 10;11 12 13];
>>A+B
ans =
9 11 13
15 17 19
>>A-B
ans =
8
-7 -7 -7
-7 -7 -7
(B) Array Multiplications :
Mathematical Review :
If
(
(
(
(

=
43 42 41
33 32 31
23 22 21
13 12 11
A A A
A A A
A A A
A A A
A
and
(
(
(

=
32 31
22 21
12 11
B B
B B
B B
B

Be sure that, number of columns of Matrix A is equal to number of rows of Matrix B .Otherwise multiplication
is not defined.
Then,
(
(
(
(

=
43 42 41
33 32 31
23 22 21
13 12 11
A A A
A A A
A A A
A A A
AB
(
(
(

32 31
22 21
12 11
B B
B B
B B

=
(
(
(
(

+ + + +
+ + + +
+ + + +
+ + + +
32 43 22 42 12 41 31 43 21 42 11 41
32 33 22 32 12 31 31 33 21 32 11 31
32 23 22 22 12 21 31 23 21 22 11 21
32 13 22 12 12 11 31 13 21 12 11 11
B A B A B A B A B A B A
B A B A B A B A B A B A
B A B A B A B A B A B A
B A B A B A B A B A B A

A numerical example :
A=
(
(
(

8 2 5
1 6 2
3 4 1
and B=
(
(
(

6 2
3 1
4 5

Then,
= AB
(
(
(

8 2 5
1 6 2
3 4 1
(
(
(

6 2
3 1
4 5
=
(
(
(

+ + + +
+ + + +
+ + + +
8.6 2.3 5.4 8.2 2.1 5.5
1.6 6.3 2.4 1.2 6.1 2.5
3.6 4.3 1.4 2 . 3 1 . 4 5 . 1
=
(
(
(

74 43
32 18
34 15

MATLAB Example :
>>A=[1 4 3;2 6 1;5 2 8];
>>B=[5 4;1 3;2 6];
>>A*B
ans =
15 34
18 32
43 74
(C) Inverse of a Matrix
MATLAB Example :
>>A=[2 1 4;4 1 8;2 -1 3];
>>inv(A)
ans =
5.5000 -3.5000 2.0000
9
2.0000 -1.0000 0
-3.0000 2.0000 -1.0000
Note: The Command >>A^-1 will have the same effect.
(D)Left Division and right Division
Command Name Meaning Example
A\B Left Division A
-1
B >>A=[1 2 3;4 5 6;7 8 9];
>>B=[10 11 12;13 14 15;16 17 18];
>>A\B % Left division
ans =
-20.0000 -17.0000 -18.0000
33.0000 26.0000 27.0000
-12.0000 -8.0000 -8.0000

A/B Right Division AB
-1
>>A/B % right division
ans =
1.0000 3.0000 -3.0000
1.0000 2.0000 -2.0000
0.5000 2.0000 -1.5000


(E)Element-By-Element Operations (Very Very Important)
If two matrices are
(
(
(

=
33 32 31
23 22 21
13 12 11
A A A
A A A
A A A
A and
(
(
(

=
33 32 31
23 22 21
13 12 11
B B B
B B B
B B B
B
Then, observe the following operation :
Command Meaning Outcome
A.*B Multiplies
every element
of A with
corresponding
element of B
(
(
(

=
33 33 32 32 31 31
23 23 22 22 21 21
13 13 12 12 11 11
B A B A B A
B A B A B A
B A B A B A
B * . A
A./B Divides every
element of A
by
corresponding
element of B
(
(
(

=
33 33 32 32 31 31
23 23 22 22 21 21
13 13 12 12 11 11
B / A /B A /B A
B / A /B A /B A
B / A /B A /B A
B / . A
A.^n Every
element of A
is raised by a
power n.
(
(
(
(

=
n
33
n
32
n
31
n
23
n
22
n
21
n
13
n
12
n
11
A A A
A A A
A A A
n .^ A
Note: Observe carefully difference between using dot and not using dot.
(F)Built in functions for analyzing arrays :
Function Description Example
mean(A) Returns mean value of the elements of vector A. >>A=[5 9 2 4];
>>mean(A)
10
ans =
5
c =max(A) Returns maximum value of the elements of vector A >>c=max(A)
c =
9
[d n] =max(A) Returns maximum value of the elements of vector A and
also return its position.
>>A=[5 9 2 4];
>>[d n] =max(A)
d =
9
n =
2
c=min(A) Returns minimum value of the elements of vector A >>A=[3 4 5 7 -5 10];
>>c=min(A)
c =
-5
[d n]=min(A) Returns maximum value of the elements of vector A and
also return its position.
>>A=[3 4 5 7 -5 10];
>>[d n]=min(A)
d =
-5
n =
5
sum(A) Returns the sum of the vector A. >>A=[5 9 2 4];
>>sum(A)
ans =
20
sort(A) Arranges the elements of vector A in ascending order. >>A=[5 9 2 4];
>>sort(A)
ans =
2 4 5 9
median(A) Returns the median value of the elements of vector A. >>A=[5 9 2 4];
>>median(A)
ans =
4.5000
std(A) Returns the standard deviation of the elements of vector A. >>A=[5 9 2 4];
>>std(A)
ans =
2.9439
det(A) Returns the determinant of matrix A.
A must be square matrix.
>>A=[2 4 6;6 9 1;0 2 7];
>>det(A)
ans =
26
dot(a,b) Returns the scalar or dot product of two vectors a and b. >>a=[1 2 3];
>>b=[3 4 5];
>>dot(a,b)
ans =
26
cross(a,b) Returns the scalar or dot product of two vectors a and b. >>a=[1 2 3];
>>b=[3 4 5];
>>cross(a,b)
ans =
-2 4 -2



11
Report : [Submit as lab report in the next session.]
Q1. Use MATLAB to determine how many elements are in the array
cos(0.1):0.02:log10(105) .
Use MATLAB to find the 25
th
element.
Q2. The table 1.1 gives the speed of an aircraft on each leg of a certain trip and the time
spent on each leg . Compute the km traveled on each leg and the sum of km traveled
by all four legs.

Legs
1 2 3 4
Speed (Km/hr) 200 250 400 300
Time(Hr) 2 5 3 4
Table 1.1
Q3. Write MATLAB Command to evaluate the following :
W 10 W , 87 . 60 , 10 R , 5 I , 255 V for ,
W R I cos VI
cos VI
Efficiency
c
0
c
2
= = u = = =
+ + u
u
=
Q4. Use matrix operations to solve the following system of linear equations :

0 z 3 y 10 x 6
4 z 2 y 8 x 2
8 z 6 y 2 x 4
= + +
= + +
= +

[Hints : AX=B
Where, A=
(
(
(

3 10 6
2 8 2
6 2 - 4
and, B=
(
(
(

0
4
8



Q5. Create the following matrix C :
(
(
(

=
35 28 21 14 7
15 12 9 6 3
10 8 6 4 2
C
Use the matrix C to:
Create a six element column vector named A that contains the elements of the 2
nd

and 3rd column of C.



Lab Sheet prepared by:
B.K.M. Mizanur Rahman
Assistant Professor , Department of EEE,
United International University.
Last update : Spring 2013


The easiest and best way to learn MATLAB is to use MATLAB.

You might also like