Você está na página 1de 12

f

(
x
)














-5
-20000
-15000
-10000
-5000
0
5000
f
(
x
)
UNIV
FACU
Depa

ADVAN
Wo
Tw
-4
VERSITY OF
ULTY OF A
artment of
NCED E
MA
orkin
wo-Di
-3
F TORONT
APPLIED SC
Electrical
MAT
ENGINE
ATLAB
ng W
imen
-2
O
CIENCE AND
and Comp
T290H
EERING

B Mod
With A
sion







-1
x
D ENGINEE
puter Engin
H1F
G MAT
dule #
Array
al Pl
0
ERING
neering
THEMAT
#1
ys an
lottin
1
TICS
nd
ng
2 3
Course M
University

The fi
command
however
distribute
(Matrices

Preparati
1.
2.

3.

Working

Input via

Get yo
the fol

Comm
display

>> 3

This w

ans =
10.5

The up
for ma
Down
comm

Previo

The re
of a M

>> 14
ans =

>> an
ans =
7.00
MAT290H1F
y of Toronto
irst step in l
ds. There ar
we will prim
es MATLAB).
s and Vectors
ion (Require
Read throug

Watch the G
http://www.
or if you ar
Start button
In the exerc
you try thes
g With Array
the comman
ourself setup
llowing exam
mands in MA
yed on the sc
+ 7.5
will give a res
=
5000
p and down a
aking small c
keys allow y
and: 3 + 7
ous command
esult of the la
MATLAB built-
4/4
= 3.5000
ns *2
=
00
EXER
learning how
re many onli
marily focus o
. This first
s) and two-di
ed to do befor
gh the posted
Getting Starte
mathworks.c
re running M
, .
ise tables giv
e commands
ys (Matrices
d-line
p at one of t
mples, enter
ATLAB are e
creen immedi
sult in the com
arrow keys a
hanges to co
you to naviga
.5.
ds are also ac
ast performed
-in variable. I

MATL
RCISE
w to use MA
ine sources w
on those reso
t module wi
imensional p
re you start t
d Introduction
ed with MATL
com/demos/m
MATLAB, it ca
ven below, p
in MATLAB.
s and Vector
he facultys
these in MA
executed by
iately. As an
mmand wind
allow you to
ommands you
ate through th
ccessible thro
d computatio
It can be use
LAB Module #
2
ES - A
ATLAB is to b
which can g
ources availa
ill cover the
lotting.
these exercis
n to MATLAB
LAB video dem
mATLAB/getti
an be access
redict the ou

rs)
computers
ATLAB and w
pressing E
example, try
dow as follow
recall previo
u have alread
he command
ough the Com
on is ascribed
d in the next
#1
ARRA
become fam
give you a b
able through
e general ar
ses)
document.
monstration w
ing-started-w
ed by selecti
utput that wil
and run MA
work along w
Enter or Re
y
ws
ous comman
dy entered. A
d window. Pr
mmand Histor
d to the varia
t command. F
AYS
miliar with its
basic introduc
Mathworks (
reas of work
which is avai
with-matlab-v
ing Demos f
ll be given by
ATLAB. As y
with the mod
eturn, with
nds, which ca
As well, the P
ress the up a
ry window.
able ans, wh
For instance,
Fall
ECE Depar
s basic setup
ction to MAT
(the company
king with A
ilable at:
video-tutorial
from the MA
y MATLAB, b
you read thr
dule.
the output
an be very he
Page Up and
arrow to reca
hich is an exa

l 2011
rtment
p and
TLAB,
y that
Arrays
l.html
ATLAB
before
rough
being
elpful
Page
all the
ample
Course MAT290H1F MATLAB Module #1 Fall 2011
University of Toronto 3 ECE Department
You can also define your own variables. Type in the following commands to create the variables a and b:

>> a = 14/4
a =
3.5000

>> b = a+1
b =
4.5000

The command whos displays a list of variables, their sizes, and their data class. Try it now.

The entry of very large and very small numbers can be simplified using scientific notation. For example,
enter the numbers
0
= 0.000000000008854 = 8.854 x 10
-12
,
0
= 4 x 10
-7
,
and 6.022 x 10
23
by typing:

>> epsilon = 8.854e-12

and

>> mu = 4*pi*1e-7

and

>> Avogadros_constant = 6.022e23

Do the following exercises:

1. Evaluate the following expressions using MATLAB. Fill in the table as you go.

Expression Prediction Result from MATLAB
8 * 5 + 3
8 * (5 + 3)
10 / 2 / 5 3 + 2 * 4
3 ^ 2 ^ 3
exp(j*pi/4)
c = a^39 where a = 3
1/sqrt(epsilon*mu)
where epsilon and mu are defined
as above.


2. When the command is followed by a semicolon ;, the output is suppressed. Check the
difference between the following expressions:

>> 3 + 7.5 % without semicolon
>> 3 + 7.5; % with semicolon

Note that the last stored answer can always be viewed by typing ans in the command window.
Also note that the text following the % sign is ignored by MATLAB. It is useful for inserting
comments, explanations or reminders.
Course M
University
Working

A mat
colum
or spa
rows. F

by typi

>> A
A =
1 2 3
4 5 6
7 8

Try th

>> A2
A2 =
1 2
-1 1

Notice
from 1

Transp
rows w

>> A2
A2 =
1 2
-1 1

>> A2
ans =
1 -1
2 1
3 3
4 5

Standa
would

>> [
ans =
4 6
10 12
MAT290H1F
y of Toronto
g with Mat
trix of size n
n vectors are
ces are used
For example
ing
= [1 2 3
3
6
9
is example o
2 = [1:4;

3 4
3 5
e how the co
1 to 4 in steps
posing a mat
with the corre
2

3 4
3 5
2 % tra
=




ard matrix mu
be executed
1 2;3 4]*
=
6
2
trices
k is a rect
e special type
to separate
, define the m
3; 4 5 6;
of how to spe
; -1:2:5]
olon ':' is use
s of 1; -1:2
trix can be a
esponding co
anspose o
ultiplication
d in MATLAB
*[2 0;1 3
MATL
tangular arra
es of matrice
elements in
matrix
7 8 9]
ecify a matrix

ed to specify
:5 means fr
achieved with
olumns, as sh
of A2
is done with
j
1
S
by typing:
3]
LAB Module #
4
ay of number
es, with size
a row, and s
% row by
x in MATLAB
y a range of
om -1 to 5 in
h a simple c
hown in the fo
the * comm
1 2
S 4
[ j
2 u
1 S
[
#1
rs having n r
of n 1, and
semicolons a

y row inp
B:
numbers as
n increments
command, w
following exa
mand, so the c
[
rows and k c
d 1 k, respe
are used to se
put
elements. 1
of 2.
where transpo
ample:
calculation gi
Fall
ECE Depar
columns. Row
ectively. Com
eparate indiv
:4 simply m
osing interch
iven by:
l 2011
rtment
w and
mmas
vidual
means
anges
Course MAT290H1F MATLAB Module #1 Fall 2011
University of Toronto 5 ECE Department
While the dot product, or element by element multiplication, is done by using the .* command.
For example, the result of

>> [1 2;3 4].*[2 0;1 3]
ans =
2 0
3 12

Try these examples:

Expression Prediction Result from MATLAB
A2*A2

A2.*A2

A2.*A2


Building Matrices and Extracting Parts of Matrices

It is often necessary to build a larger matrix from a smaller one; for example

>> x = [4; -1], y = [-1 3]
x =
4
-1
y =
-1 3

>> z = [x y] % z consists of the columns x and y
z =
4 -1
-1 3

Right after the above example, try

>> z^2

>> z.^2

What is the difference between the two results?

Next type in

>> z = x + y

>> z^2 % does this work?

>> z.^2 % what about this?

Course M
University
What
may h

Specif
first ro

>> A
ans =


and en

>> A
ans =


>> A
ans =




Partial

>> A
ans =



We ca

>> A
A =





Review
the MA
M
M
M
Sp
Sp
E
T
Many
that th
MAT290H1F
y of Toronto
do you get a
elp you answ

fic elements o
ow of the mat
(1,2)
=
2
ntire rows, or
(1,:)
=
1
(:,2)
=
2
5
8
l rows, or col
(1:2,2)
=
2
5
an also make
(1:2,1) =
1 2
1 5
7 8
w the summa
ATLAB Prim
Managing Co
Managing Var
Matrix and Ar
pecial Chara
pecial Variab
Elementary M
Trigonometric
of these com
hey are there.
as a result no
wer these que

of a matrix c
trix A (as def
r columns, are
2 3
lumns, can be
use of the co
= [1;1]
3
6
9
aries of the c
er:
mmands and
riables and t
rray Operato
acters,
bles and Con
Math Function
c Functions.
mmands are
You can alw
MATL
ow? Why do
estions.
an also be ac
fined above),
e displayed t
e displayed b
olon operator
commands li
d Functions,
the Workspac
ors,
nstants,
ns, and

not immedia
ways use he
LAB Module #
6
es one work


ccessed. For
, you would t
through the u
by limiting th
r to assign va
sted in the fo
ce,
ately useful t
lp to get mo
#1
k and not the


r example, to
type:
use of the col
he range:
alues to a part
ollowing tabl
to our purpo
ore informati
e other? The


o display the
lon operator:
t of a matrix
les of the Re
oses, but you
on about them
Fall
ECE Depar
e size com


2
nd
element o
:
eference secti
u should be a
m.
l 2011
rtment
mmand

of the
ion of
aware
Course MAT290H1F MATLAB Module #1 Fall 2011
University of Toronto 7 ECE Department
Do the following exercise:

1. Given the vectors (or row matrices) x = [1 3 7], y = [2 4 2] and the matrices A = [3 1 6; 5 2 7],
B = [1 4; 7 8; 2 2], determine which of the following statements will correctly execute (and if
not, try to understand why) and provide the result or explanation why it doesnt work:

Expression Prediction Result from MATLAB/Explanation
x + y
x + A
[x; y]
B [x y]
A 3
B * A
A * B
A* B
2 * B
B(:,2)
A.^2
exp(B(1,1))
cos(pi*A)
sin((pi/2)*A(1,2))
exp(j*pi/4*B)

Course MAT290H1F MATLAB Module #1 Fall 2011
University of Toronto 8 ECE Department
Applications Arrays

1. Solving sets of simultaneous equations
In ECE212 (Circuit Analysis) you often need to solve sets of simultaneous equations which
result from the basic techniques of circuit analysis (nodal and mesh analysis). Doing this by
hand can be a time consuming process, but in MATLAB the solutions to these equations can be
easily found.

This is of course only useful if we are dealing with cases where the coefficients of the
equations are numbers. In cases where the coefficients contain one or more variables we must
know how to solve this by hand. This might occur in a design-type problem where the value of
a resistor is not known and the purpose of the problem is to determine the correct resistor value
for a desired circuit operation.

To start, consider the set of equations given by:
SI
1
6I
2
= u
1uI
1
+ 14I
2
= 2

This can be converted to matrix form as:
j
S 6
1u 14
[ _
I
1
I
2
_ = j
u
2
[
With the solution given by:
_
I
1
I
2
_ = j
S 6
1u 14
[
-1
j
u
2
[ = j
1.2
1
[
In MATLAB this can be solving using the inv command (see help inv):
>> inv([5 -6;-10 14])*[0;2]

ans =

1.2000
1.0000

Note, that the matrix multiplier, *, is used rather than the dot multipler, .*, since we want
real matrix multiplication rather than element-by-element multiplication.

Use MATLAB to solve the following sets of equations:
a)
16I
1
2I
2
4I
3
= 6
2I
1
+6I
2
2I
4
= u
4I
1
2I
2
+ 4I
3
+ 6I
4
= u
I
3
+ I
4
= S

b)
9I
1
2I
2
2I
3
= 4
2I
1
+ 1uI
2
4I
3
I
4
I
5
= 6
2I
1
4I
2
+ 9I
3
= 6
I
2
+ 8I
4
SI
5
= u
I
2
SI
4
+ 4I
5
= 6

Course M
University



2. R
T
co
(w
la
po

In
hi
bu
br
of

U




F
re



MAT290H1F
y of Toronto
c) Us
fo
the
Roots of Poly
The fundamen
omplex coeff
where z can b
ater in the ter
olynomials.
n our study o
igher-order p
uilt-in functi
rowser in the
f how this fu
Use this funct
a) m

b) m
c) m
d) 2m
or the first th
esult.
se circuit ana
r the mesh cu
ese mesh cur
ynomials
ntal theorem
ficients has a
be real or com
rm, but for no

of ordinary di
polynomials.
ons, roots.
e command w
unction can b
tion to determ
3
+Sm
2
4
4
2m
2
+ 1
2
+2m + 2
m
8
+ 9m
7
+
hree polynom
MATL
alysis techniq
urrents given
rrents.
of algebra st
at least one ro
mplex). We
ow we will fo
ifferential equ
MATLAB ca
. Find the de
window, ,
e used in the
mine the root
4 = u
= u
= u
2m
5
+ m
4
+
mials, verify b
LAB Module #
9
ques to determ
n in the circui
tates that any
oot, meaning
will see a sim
ocus on using
uations we w
an be used to
escription of
or by typing
reference pa
s of the follo
+ 2m
3
+ 2m
by hand calcu
#1
mine the four
it below. Th
y non-constan
g that f(z) = 0
mple proof of
g MATLAB to
will find that
find these ro
this function
g help roo
age for this fu
owing polyno
m
2
+2m + 1
ulation that M
r equations n
hen use MATL
nt polynomia
for at least o
f this using c
o find the roo
we need to f
oots through
n through eith
ots. Note, th
function (doc
omials.
1 = u
MATLAB is gi
Fall
ECE Depar
needed to sol
LAB to solve
al with real or
one number,
complex anal
ots of
find the roots
one of its ma
her the functi
here are exam
c roots).
iving the cor
l 2011
rtment
ve
for

r
z
lysis
of
any
ion
mples
rrect
Course M
University

Plotting

Preparat
a. V
b. U
These
MATL

Two-Di

The plo
matrix) y
with strai
[x(n), y(n
same num

Type the

>> x =
>> y =
>> plot
>> plot

More tha
command

>> clf
>> plot

Or, the h

>> figu
>> plot
>> hold
>> plot

Multiple
command

>> figu
>> subp
>> plot
>> subp
>> plot
MAT290H1F
y of Toronto
E
g In MATL
tion: To beg
Visualizing D
Using Basic P
e are availab
LAB, it can b
imensional
t command
y, the comma
ight lines. Th
n)]. Note tha
mber of elem
following co
linspace
2.^x;
t(x)
t(x,y) %
an one plot c
d. Try:
% Th
t(x,2*x.^
old comman
ure % Th
t(x,2*x.^
d
t(x,4*x.^
plots can a
d:
ure
plot(2,1,
t(x,2*x.^
plot(2,1,
t(x,4*x.^
EXER
LAB
in, first watc
Data (4:07)
Plotting Fun
ble at http://w
be accessed b
l Plots
can be used t
and plot(y
he command
t x and y mu
ents).
ommands afte
e(0,10,10
% to get
an be made
is clears
^2,x,4*x.
nd can be use
his opens
^2)
^2)
lso be creat
,1) % Bre
^2) % Plo
,2) % Swi
^2) % Plo
MATL
RCISE
ch the follow
nctions (5:30
www.mathw
by selecting
to produce a
) draws the p
plot(x,y)
ust be both ei
er predicting
00);
a graphi
in the same
s the cur
^2)
ed. Try:
s up a ne
ted on differ
eaks the
ots the f
itches th
ots the f
LAB Module #
10
ES - PL
wing video d
0)
works.com/pr
Demos from
graphical dis
points [1, y(1
) does the sa
ither row or c
the results:
c represe
figure by sp
rrent fig
w figure
rent axes in
figure wi
unction i
e current
unction i
#1
LOTT
demonstratio
roducts/matl
m the MATLA
splay of a set
1)], [2, y(2)],
ame for the p
column vecto
entation
pecifying mu
gure windo
window
the same w
indow int
in the to
t axes to
in the bo
TING
ons:
lab/demos.ht
AB Start butt
t of data poin
, . . ., [n, y(n)
points [x(1), y
ors of the sam

ultiple argum
ow
window, usin
to two ax
op set of
o the bot
ottom set
Fall
ECE Depar
tml or if you
ton, .
nts. For a vec
)] and connec
y(1)], [x(2), y
me length (i.e
ments in the p
ng the subp
xes (top
f axes
ttom set
t of axes
l 2011
rtment
u are running
ctor (or row
cts these poin
y(2)], . . . ,
e. must have
plot
plot
and botto
g
nts
the
om)
Course M
University
Often it
following

>> figu
>> subp
>> plot
>> subp
>> sem
>> subp
>> log

Review th
MATLAB
Two D
Graph
Figure

Do the fo

1. Create

by us
comm

>> x

Now t
and se
repres

The pr
by sel
of inte
the plo

2. Create
over th
and yo

Add an
>> t
>> xl
>> yl

Note t
comm
MAT290H1F
y of Toronto
is useful to
g commands:
ure
plot(3,1,
t(x,x.^2)
plot(3,1,
ilogx(x,x
plot(3,1,
log(x,x.^
he summarie
B Primer:
Dimensional G
h Annotation
e Window/Ax
ollowing exe
e a new plot o
(0, 0), (2,
ing the com
mand:
= [0,2,3
type help
earch for p
enting them
roperties of t
ecting the m
erest, then yo
ot to have a g
e a new plot o
he range u
ou will need t
n appropriate
itle(A D
label(Ti
label(Vo
that the titl
mand.
plot a funct

,1)
)
,2) % Swi
x.^2) % P
,3) % Swi
^2) % Plo
es of the com
Graphs
xes Creation
ercises:
of the 8 point
4), (3.5, 7),
mmand plot
3.5,5,7.2
plot follow
plot. After
with blue squ
the plot can a
enu View
ou can adjust
green dotted l
of the expone
t 1u. Yo
to use the do
e title and x a
Decaying
ime (s))
oltage (V
le, xlabel
MATL
ion on a sem
itches th
Plots the
itches th
ots the f
mmands listed
and Control
ts whose (x
, (5, 9.3), (
t(x,y,'ro
2,9.34,11
wing the MA
r reading ab
uares instead
also be chang
Property Ed
t its colour, l
line with gre
entially decay
:(t)
ou can use th
ot operator, .
and y label to
Sinusoid

V))
l, and ylabe
LAB Module #
11
milog or a lo
e current
function
e current
unction w
d in the follow
,y) coordin
(7.2, 6.12),
o'). You c
,12.44];y
ATLAB promp
bout the pl
d of red circle
ged through t
ditor. Once t
line thickness
en stars.
ying sinusoid
= c
-0.5t
sin
he linspac
*, to properl
o your plot. F
)
el command
#1
og-log set o
t axes to
n with th
t axes to
with both
wing tables o
ates are
(9.34, 2), (1
can create th
y=[0,4,7,
pt, or access
lot comman
es.
the figure pro
the editor is
s, line type,
d voltage fun
(4t)
e command
ly multiply th
For example,
ds must follo
f axes. For
o the mid
he x-axis
o the bot
h axes se
of the Refere
11, 4.2), (12
he required
,9.3,6.12
the main M
nd, plot the
operties toolb
open, if you
etc. Use thi
nction, given
to create the
he exp and s
, you can use
ow rather tha
Fall
ECE Depar
r example, tr
ddle set
s set to
ttom set
et to a lo
ence section o
2.44, 0)
vectors with
2,2,4.2,0
ATLAB help
e same poin
bar button,
click on the
s editor to ch
by
required t v
sin function
e the comman
an precede th
l 2011
rtment
ry the
a log sca
og scale
of the
h the
];
menu
nts by
, or
trace
hange
vector
ns.
nds:
e plot
ale
Course MAT290H1F MATLAB Module #1 Fall 2011
University of Toronto 12 ECE Department

You can adjust the range of the axes of this plot by using the command axis. For example try the
command:
>> axis([0 5 -1 1])

What are the new ranges of the x and y axes?

Note, the command axis equal sets the ranges of the x and y axes to be the same.

Applications Two-Dimensional Plots

1. Graphical Determination of the Roots of a Polynomial
One of the great uses of programs such as MATLAB is that it can be used to easily graph
complicated functions. One use for this utility is that the real-valued roots to a polynomial can
be determined graphically by plotting the function and identifying where the curve crosses the
independent variable axis (i.e., the x-axis).

As an example, determine the real-valued roots of the polynomials given by:

a) m
2
+2m + 2 = u

b) 2m
8
+ 9m
7
+ 2m
5
+ m
4
+ 2m
3
+ 2m
2
+2m + 1 = u

You plot for part b) should look similar to the figure on the first page of this module.