MAT290 Matlab Module1

Embed Size (px)

Citation preview

  • 8/4/2019 MAT290 Matlab Module1

    1/12

    -5-20000

    -15000

    -10000

    -5000

    0

    5000

    UNI

    FAC

    Dep

    ADVA

    W

    T

    -4

    ERSITY O

    LTY OF A

    rtment of

    CED

    MA

    rki

    o-

    -3

    TORONT

    PPLIED S

    Electrical

    MA

    NGIN

    TLA

    g

    men

    -2

    O

    IENCE AN

    and Comp

    290H

    ERIN

    Mo

    ith

    sion

    -1x

    ENGINE

    uter Engi

    1F

    MAT

    ule

    rra

    al P

    0

    RING

    eering

    HEMA

    1

    s a

    otti

    1

    ICS

    d

    g

    2 3

  • 8/4/2019 MAT290 Matlab Module1

    2/12

    Course

    Universit

    The ficomman

    however

    distribute(Matrices

    Preparat

    1.2.

    3.

    Workin

    Input via

    Get y

    the fo

    Comdispla

    >> 3

    This

    ans10.

    The ufor mDown

    comm

    Previo

    The reof a

    >> 1ans

    >> aans7.00

    AT290H1F

    of Toronto

    rst step in ls. There a

    we will pri

    s MATLAB).and Vector

    on (RequirRead throu

    Watch the

    http://www.or if you ar

    Start button

    In the exerc

    you try thes

    With Arra

    the comman

    urself setu

    lowing exa

    ands in Med on the sc

    + 7.5

    ill give a re

    5000

    and down

    king small ckeys allow

    and: 3 + 7

    us comman

    sult of the lATLAB built

    /43.5000

    s *2

    00

    EXEearning hoe many onl

    arily focus

    This first) and two-di

    d to do befo

    h the posted

    etting Start

    mathworks.e running

    , .

    ise tables gi

    e commands

    s (Matrice

    d-line

    at one of t

    ples, enter

    TLAB arereen immedi

    ult in the co

    arrow keys

    hanges to coou to navig

    .5.

    s are also ac

    st performein variable.

    MAT

    CISto use M

    ne sources

    n those res

    module wimensional p

    e you start

    Introductio

    d with MATL

    om/demos/

    ATLAB, it c

    en below, p

    in MATLAB.

    and Vecto

    he facultys

    these in M

    xecuted byately. As an

    mand win

    llow you to

    mmands yote through t

    cessible thro

    computatiot can be use

    AB Module

    2

    ES -TLAB is to

    hich can g

    urces availa

    ll cover thlotting.

    hese exerci

    to MATLAB

    AB video de

    ATLAB/gettin be access

    redict the ou

    s)

    computers

    TLAB and

    pressing Eexample, tr

    ow as follo

    recall previ

    have alreade command

    ugh the Co

    n is ascribed in the next

    1

    RR become famive you a b

    ble through

    general a

    es)document.

    onstration

    ng-started-ed by select

    tput that wil

    and run M

    ork along

    nter or Re

    s

    us comman

    y entered.window. P

    mand Histo

    to the variacommand.

    YSiliar with itasic introdu

    Mathworks

    eas of wor

    hich is ava

    ith-matlab-ing Demos

    l be given b

    TLAB. As

    ith the mo

    turn, with

    ds, which c

    s well, theess the up a

    ry window.

    ble ans, whor instance,

    Fall

    ECE Depar

    s basic setution to MAthe compan

    ing with

    lable at:

    ideo-tutoriarom the M

    MATLAB,b

    ou read thr

    ule.

    the output

    n be very h

    ageUp andrrow to rec

    ich is an ex

    2011

    tment

    and

    LAB,

    that

    rrays

    .html

    TLAB

    efore

    ough

    being

    elpful

    Pagell the

    mple

  • 8/4/2019 MAT290 Matlab Module1

    3/12

    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/4a =3.5000

    >> b = a+1b =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 numbers0 = 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.Evaluatethe 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 thedifference 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 insertingcomments, explanations or reminders.

  • 8/4/2019 MAT290 Matlab Module1

    4/12

    Course

    Universit

    Workin

    A mat

    columor spa

    rows.

    by typ

    >> AA =1 24 57 8

    Try th

    >> AA2 =1 2-1 1

    Notice

    from 1

    Trans

    rows

    >> AA2 =1 2-1 1

    >> Aans1 -12 13 34 5

    Stand

    would

    >> [ans410 1

    AT290H1F

    of Toronto

    with Ma

    rix of size n

    n vectors arces are used

    or example

    ng

    = [1 2

    3

    9

    is example o

    = [1:43 43 5

    how the c

    to 4 in step

    osing a ma

    ith the corr

    3 43 5

    % tr

    rd matrix m

    be executed

    2;3 4]

    rices

    kis a rec

    special typto separate

    , define the

    ; 4 5 6;

    f how to spe

    -1:2:5]

    lon ':' is us

    of 1; -1:2

    rix can be

    sponding co

    nspose o

    ltiplication

    in MATLAB

    [2 0;1 3

    MAT

    angular arra

    s of matriceelements in

    atrix

    7 8 9]

    cify a matri

    d to specify

    :5 means fr

    chieved wit

    lumns, as sh

    f A2

    is done with

    by typing:

    ]

    AB Module

    4

    y of number

    s, with sizea row, and

    % row b

    in MATLAB

    a range of

    om -1 to 5 i

    a simple c

    own in the f

    the * comm24

    2 01 3

    1

    s having nrofn 1, anemicolons

    row in

    :

    numbers as

    increments

    ommand, w

    ollowing ex

    and, so the c

    ows and kc1 k, resp

    re used to s

    ut

    elements. 1

    of 2.

    here transpo

    mple:

    alculation gi

    Fall

    ECE Depar

    olumns. Ro

    ectively. Coeparate indi

    :4 simply

    sing interch

    ven by:

    2011

    tment

    and

    masidual

    eans

    anges

  • 8/4/2019 MAT290 Matlab Module1

    5/12

    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

    Trytheseexamples:

    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-1y =-1 3

    >> z = [x y] % z consists of the columns x and yz =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?

  • 8/4/2019 MAT290 Matlab Module1

    6/12

    Course

    Universit

    What

    may h

    Specif

    first r>> Aans

    and en>> Aans

    >> Aans

    Partial

    >> Aans

    We ca>> AA =

    Revie

    the M

    SS

    E

    Many

    that th

    AT290H1F

    of Toronto

    do you get

    elp you ans

    ic elements

    w of the ma

    (1,2)

    2

    tire rows, or

    (1,:)

    1

    (:,2)

    258

    rows, or col

    (1:2,2)

    25

    n also make

    (1:2,1)

    1 21 57 8

    the summ

    TLAB Prim

    anaging Coanaging Va

    atrix and A

    ecial Charecial Varia

    lementary

    rigonometriof these co

    ey are there.

    s a result n

    er these que

    f a matrix c

    rixA (as de

    columns, ar

    2 3

    umns, can b

    use of the co

    [1;1]

    369

    ries of the

    er:

    mmands andiables and t

    ray Operato

    cters,les and Co

    ath Functio

    Functions.mands are

    You can al

    MAT

    w? Why do

    stions.

    an also be a

    ined above),

    e displayed t

    e displayed

    lon operator

    ommands li

    Functions,he Workspa

    rs,

    stants,

    s, and

    not immedi

    ays use he

    AB Module

    6

    es one work

    cessed. For

    you would

    hrough the u

    y limiting t

    to assign va

    sted in the f

    e,

    tely useful

    lp to get mo

    1

    and not th

    example, to

    ype:

    se of the col

    e range:

    lues to a par

    llowing tab

    o our purpo

    re informati

    other? Th

    display the

    on operator:

    t of a matrix

    es of the Re

    ses, but you

    on about the

    Fall

    ECE Depar

    size com

    2nd element

    :

    ference secti

    should be

    .

    2011

    tment

    mand

    of the

    on of

    ware

  • 8/4/2019 MAT290 Matlab Module1

    7/12

    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)

  • 8/4/2019 MAT290 Matlab Module1

    8/12

    Course MAT290H1F MATLAB Module #1 Fall 2011

    University of Toronto 8 ECE Department

    Applications Arrays

    1. Solving sets of simultaneous equationsIn 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 byhand 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 theequations 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 ofa resistor is not known and the purpose of the problem is to determine the correct resistor valuefor a desired circuit operation.

    To start, consider the set of equations given by:

    5 6 = 0

    10 + 14 = 2

    This can be converted to matrix form as:

    5 610 14 =

    02

    With the solution given by:

    = 5 6

    10 14

    02 = 1.21

    In MATLAB this can be solving using the inv command (see help inv):>> inv([5 -6;-10 14])*[0;2]

    ans =

    1.20001.0000

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

    Use MATLAB to solve the following sets of equations:

    a)

    16 2 4 = 62 + 6 2 = 0

    4 2 + 4 + 6 = 0 + = 3

    b)

    9 2 2 = 42 + 10 4 = 6

    2 4 + 9 = 6 + 8 3 = 0 3 + 4 = 6

  • 8/4/2019 MAT290 Matlab Module1

    9/12

    Course

    Universit

    2.Tc

    (l

    p

    I

    hb

    b

    o

    F

    r

    AT290H1F

    of Toronto

    c) Ufoth

    oots of Pol

    he fundamemplex coef

    herezcanter in the ter

    olynomials.

    our study o

    gher-orderilt-in functi

    owser in th

    how this fu

    se this funct

    a)

    b) c)

    d) 2or the first t

    sult.

    e circuit an

    r the mesh cese mesh cur

    nomials

    tal theoremicients has a

    e real or com, but for n

    f ordinary di

    olynomials.ons, roots.

    command

    nction can b

    ion to deter

    + 3 2 + 1

    + 2 + 2 + 9 +

    ree polyno

    MAT

    lysis techni

    rrents givenrents.

    of algebra stt least one r

    plex). Wew we will f

    fferential eq

    MATLAB caFind the de

    indow, ,

    e used in the

    ine the root

    = 0

    = 0

    = 02 +

    ials, verify

    AB Module

    9

    ues to deter

    in the circu

    ates that anyot, meaning

    will see a sicus on usin

    ations we

    n be used toscription of

    or by typing

    reference p

    s of the follo

    2 + 2

    y hand calc

    1

    ine the fou

    t below. Th

    non-constathatf(z) = 0

    ple proof oMATLAB t

    ill find that

    find these rthis function

    help ro

    ge for this f

    wing polyn

    + 2 +

    lation that

    r equations n

    en use MAT

    t polynomiafor at least

    f this using cfind the roo

    we need to f

    ots throughthrough eit

    ts. Note, t

    nction (do

    mials.

    = 0

    ATLAB is g

    Fall

    ECE Depar

    eeded to sol

    AB to solve

    l with real one number,

    omplex analts of

    ind the roots

    one of its mer the functi

    ere are exa

    roots).

    ving the cor

    2011

    tment

    ve

    for

    z

    ysis

    of

    nyon

    ples

    rect

  • 8/4/2019 MAT290 Matlab Module1

    10/12

    Course

    Universit

    Plottin

    Prepara

    a.b.Thes

    MAT

    Two-Di

    The plo

    matrix)

    with strai

    [x(n), y(n

    same nu

    Type the

    >> x =

    >> y =

    >> plo

    >> plo

    More tha

    comman

    >> clf>> plo

    Or, the h

    >> fig

    >> plo

    >> hol

    >> plo

    Multiple

    comman>> fig

    >> sub

    >> plo

    >> sub

    >> plo

    AT290H1F

    of Toronto

    In MAT

    ion: To beg

    isualizingsing Basic

    e are availa

    AB, it can

    mensional

    t command

    , the comma

    ght lines. Th

    )]. Note tha

    ber of elem

    following c

    linspac

    2.^x;

    t(x)

    t(x,y)

    n one plot c

    . Try:

    % Tht(x,2*x.

    old comma

    ure % T

    t(x,2*x.

    d

    t(x,4*x.

    plots can a

    :

    ure

    plot(2,1

    t(x,2*x.

    plot(2,1

    t(x,4*x.

    XEAB

    in, first wat

    ata (4:07)lotting Fun

    le at http://

    e accessed

    Plots

    can be used

    nd plot(y

    e command

    t x and y m

    ents).

    mmands aft

    (0,10,10

    to get

    an be made

    is clear2,x,4*x.

    d can be us

    is opens

    2)

    2)

    lso be creat

    1) % Bre

    2) % Plo

    2) % Swi

    2) % Plo

    MAT

    CISE

    h the follo

    ctions (5:3

    ww.mathw

    y selecting

    o produce a

    ) draws the

    plot(x,y

    st be both ei

    r predicting

    0);

    a graphi

    in the same

    the cu^2)

    ed. Try:

    up a ne

    ed on diffe

    aks the

    ts the f

    tches th

    ts the f

    AB Module

    10

    S - P

    ing video d

    )

    orks.com/p

    Demos fro

    graphical di

    oints [1, y(

    does the s

    ther row or

    the results:

    c repres

    figure by s

    rent fig

    w figure

    ent axes in

    figure w

    unction

    e curren

    unction

    1

    OT

    emonstratio

    oducts/matl

    the MATL

    splay of a se

    )], [2, y(2)],

    me for the p

    olumn vect

    ntation

    ecifying m

    ure wind

    window

    the same

    ndow in

    n the t

    axes t

    n the b

    ING

    ns:

    ab/demos.h

    B Start but

    t of data poi

    . . ., [n, y(n)

    oints [x(1),

    rs of the sa

    ltiple argum

    ow

    indow, usi

    o two ax

    p set of

    the bot

    ttom set

    Fall

    ECE Depar

    ml or if yo

    on, .

    ts. For a ve

    ] and conne

    (1)], [x(2),

    e length (i.

    ents in the

    g the sub

    es (top

    axes

    tom set

    of axes

    2011

    tment

    are runnin

    tor (or row

    ts these poi

    (2)], . . . ,

    . must have

    lot

    lot

    and bott

  • 8/4/2019 MAT290 Matlab Module1

    11/12

    Course

    Universit

    Often itfollowin

    >> fig

    >> sub

    >> plo>> sub

    >> sem

    >> sub

    >> log

    Review t

    MATLAB Two

    Grap

    Figur

    Do the f

    1. Create

    by us

    comm

    >> x

    Now

    and s

    repres

    The p

    by selof intthe pl

    2. Create

    over t

    and y

    Add a>> t

    >> x

    >> y

    Note t

    comm

    AT290H1F

    of Toronto

    s useful tocommands:

    ure

    plot(3,1

    t(x,x.^2)plot(3,1

    ilogx(x,

    plot(3,1

    log(x,x.

    e summarie

    Primer:

    imensionalAnnotation

    Window/A

    llowing ex

    a new plot

    (0, 0), (2,

    ing the co

    and:

    = [0,2,

    ype help

    earch for

    enting them

    operties of t

    ecting the mrest, then yt to have a

    a new plot

    e range 0 u will need

    n appropriatitle(A

    abel(T

    abel(V

    hat the tit

    and.

    plot a funct

    1)

    2) % Swi

    .^2) % P

    3) % Swi

    2) % Plo

    s of the com

    Graphs

    es Creation

    rcises:

    f the 8 poin

    4), (3.5, 7),

    mand plo

    .5,5,7.2

    plot follo

    lot. Afte

    with blue sq

    he plot can a

    enu Viewu can adjustreen dotted

    f the expon

    10. Yto use the do

    title andxecaying

    me (s))

    ltage (V

    e, xlabel

    MAT

    ion on a se

    tches th

    lots the

    tches th

    ts the f

    mands liste

    and Control

    s whose (x

    (5, 9.3),

    (x,y,'r

    ,9.34,11

    ing the MA

    reading a

    ares instea

    lso be chang

    Property Eits colour, line with gre

    ntially deca

    u can use th

    t operator, .

    ndy label toSinusoid

    ))

    , and ylab

    AB Module

    11

    ilog or a l

    e curren

    functio

    e curren

    unction

    in the follo

    ,y) coordin

    7.2, 6.12),

    '). You c

    ,12.44];

    TLAB promp

    out the pl

    of red circl

    ed through t

    itor. Onceine thicknesen stars.

    ing sinusoi

    = . sine linspac

    *, to properl

    your plot.)

    l comman

    1

    og-log set o

    axes t

    with t

    axes t

    ith bot

    ing tables

    ates are

    (9.34, 2), (

    an create t

    =[0,4,7,

    t, or access

    ot comma

    s.

    he figure pr

    he editor iss, line type,

    voltage fu

    4e command

    y multiply t

    or example,

    s must follo

    f axes. For

    the mid

    e x-axis

    the bot

    axes se

    f the Refere

    11, 4.2), (1

    e required

    9.3,6.12

    the main M

    d, plot the

    perties tool

    open, if youetc. Use thi

    ction, given

    to create the

    e exp and

    , you can use

    w rather tha

    Fall

    ECE Depar

    example, tr

    dle set

    set to

    tom set

    t to a l

    nce section

    .44, 0)

    vectors wit

    ,2,4.2,0

    ATLAB help

    same poin

    ar button,

    click on thes editor to c

    by

    required t

    in functio

    the comma

    n precede th

    2011

    tment

    y the

    a log sc

    og scale

    of the

    h the

    ];

    menu

    ts by

    , or

    traceange

    ector

    s.

    ds:

    e plot

  • 8/4/2019 MAT290 Matlab Module1

    12/12

    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 thecommand:>> axis([0 5 -1 1])

    What are the new ranges of the

    and

    axes?

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

    Applications Two-Dimensional Plots

    1. Graphical Determination of the Roots of a PolynomialOne 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-valuedroots to a polynomial canbe determined graphically by plotting the function and identifying where the curve crosses the

    independent variable axis (i.e., thex-axis).

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

    a) + 2 + 2 = 0b) 2 + 9 + 2 + + 2 + 2 + 2 + 1 = 0

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