Upload
bkmmizan
View
213
Download
0
Embed Size (px)
Citation preview
8/13/2019 121_EEE110_LabSheet01
1/11
1
UNITED INTERNATIONALUNIVERSITY (UIU)
Dept. Of Electrical & Electronic Engineering (EEE)
Course: EEE 110, Title: Simulation Laboratory
L a b Sh eet 1
Introduction to MATLABWhat 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 windowCurrent Directory &
Workspace window
Figure 1.1 : The default view of MATLAB desktop
8/13/2019 121_EEE110_LabSheet01
2/11
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 calculatorYou can use command window part for various calculations as if you would use a calculator: Typeexpressions 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*cans =- 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 VariablesTo 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= 5x =
5>> y =log(2)
y =
0.6931
>> z = 10;
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
8/13/2019 121_EEE110_LabSheet01
3/11
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 .
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
8/13/2019 121_EEE110_LabSheet01
4/11
4
(G)Elementary Math functions used in MATLAB:
Function Description Example
sqrt(x) Square root >> sqrt(16)
ans =
4
exp(x) Exponential (ex) >> 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)
8/13/2019 121_EEE110_LabSheet01
5/11
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> sign(-5)
ans =
-1
(H)Creating Vectors and MatricesThe 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 separatedby 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 34 5
(I)Colon OperatorMATLAB 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
8/13/2019 121_EEE110_LabSheet01
6/11
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 11 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 00 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 112 4 6 8 10 12
3 6 9 12 15 184 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 =569
1215
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 86 9 128 12 16
10 15 20
8/13/2019 121_EEE110_LabSheet01
7/11
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 123 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 numbersbetween 0 and 1
>> b= rand(3)
b =
0.4568 0.4447 0.92180.0158 0.6154 0.7382
0.8214 0.7919 0.1763
rand(m,n) Generates an mn matrix of random numbersbetween 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
232221
131211
AAA
AAAA and
232221
131211
BBB
BBBB
Then ,
BABABA
BABABABA
232322222121
131312121111
And,
BABABA
BABABABA
232322222121
131312121111
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/13/2019 121_EEE110_LabSheet01
8/11
8
-7 -7 -7
-7 -7 -7
(B) Array Multiplications :
Mathematical Review :
If
434241
333231
232221
131211
AAA
AAA
AAA
AAA
A
and
3231
2221
1211
BB
BB
BB
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,
434241
333231
232221
131211
AAA
AAA
AAA
AAA
AB
3231
2221
1211
BB
BB
BB
=
324322421241314321421141
323322321231313321321131
322322221221312321221121
321322121211311321121111
BABABABABABA
BABABABABABA
BABABABABABA
BABABABABABA
A numerical example :
A=
825
162
341
and B=
62
31
45
Then,
AB
825
162
341
62
31
45
=
8.62.35.48.22.15.5
1.66.32.41.26.12.5
3.64.31.42.31.45.1
=
7443
3218
3415
MATLAB Example :
>> A=[1 4 3;2 6 1;5 2 8];>> B=[5 4;1 3;2 6];
>> A*B
ans =
15 3418 3243 74
(C) Inverse of a MatrixMATLAB Example :>> A=[2 1 4;4 1 8;2 -1 3];
>> inv(A)
ans =
5.5000 -3.5000 2.0000
8/13/2019 121_EEE110_LabSheet01
9/11
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-1B >> 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.000033.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.00001.0000 2.0000 -2.0000
0.5000 2.0000 -1.5000
(E)Element-By-Element Operations (Very Very Important)
If two matrices are
333231
232221
131211
AAA
AAA
AAA
A and
333231
232221
131211
BBB
BBB
BBB
B
Then, observe the following operation :
Command Meaning OutcomeA.*B Multiplies
every element
of A with
correspondingelement of B
333332323131
232322222121
131312121111
BABABA
BABABA
BABABA
B*.A
A./B Divides every
element of A
bycorresponding
element of B
333332323131
232322222121
131312121111
B/A/BA/BA
B/A/BA/BA
B/A/BA/BA
B/.A
A.^n Every
element of A
is raised by apower n.
n
33
n
32
n
31
n23
n22
n21
n
13
n
12
n
11
AAA
AAA
AAA
n.^A
Note: Observe carefully difference between using dot and not using dot.
(F)Built in functions for analyzing arrays :
Function Description Examplemean(A) Returns mean value of the elements of vector A. >> A=[5 9 2 4];
>> mean(A)
8/13/2019 121_EEE110_LabSheet01
10/11
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
8/13/2019 121_EEE110_LabSheet01
11/11
11
Report : [Submit as lab report in the next session.]
Q1. Use MATLAB to determine how many elements are in the arraycos(0.1):0.02:log10(105) .
Use MATLAB to find the 25th
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 traveledby 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 :
W10W,87.60,10R,5I,255Vfor,WRIcosVI
cosVIEfficiency c
0
c
2
Q4. Use matrix operations to solve the following system of linear equations :
0z3y10x6
4z2y8x2
8z6y2x4
[Hints : AX=B
Where, A=
3106
282
62-4
and, B=
0
4
8
Q5. Create the following matrix C :
352821147
1512963
108642
C
Use the matrix C to:
Create a six element column vector named Athat contains the elements of the 2nd
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.