02a Matlab Intro

Embed Size (px)

Citation preview

  • 8/10/2019 02a Matlab Intro

    1/51

    2014-02-24ETH Zrich

    Modeling and Simulating Social Systems

    with MATLAB

    Lecture 2 Introduction to MATLAB

    Chair of Sociology, in articular of

    Modeling and Si!ulation

    "li#ia $oolley, To%ia& 'uhn, (ario Bia&ini, (ir) *el%ing

  • 8/10/2019 02a Matlab Intro

    2/51

    2014-02-24 Modeling and Simulating Social Systems with MATLAB 22

    Schedule of the course

    1+0224!2

    00

    100

    1+0

    240

    10

    0+04

    1404

    0.0.

    120.

    1/0.

    20.

    Introduction to

    MATLAB

    Introduction to

    &ocial-&cience

    !odeling and

    &i!ulation&

    $or)ing on

    roect&

    &e!inarthe&i&3

    *anding in &e!inar the&i&

    and gi#ing a re&entation

    canceled

  • 8/10/2019 02a Matlab Intro

    3/51

    2014-02-24 Modeling and Simulating Social Systems with MATLAB

    "eadlines

    (eadline for roect roo&al&

    Monday #$ March 2!#4at !idnight

    (eadline for roect reort&

    Tue&day #% May 2!#4 at !idnight

    "ral roect re&entation&

    $edne&day #4 Mayto 5riday#& May 2!#4

  • 8/10/2019 02a Matlab Intro

    4/51

    2014-02-24 Modeling and Simulating Social Systems with MATLAB 44

    MATLAB

    $hy MATLAB6

    Language i& 7uic) to learn, ea&y to u&e, rich in

    functionality, good lotting a%ilitie&

    MATLAB i& co!!ercial &oft8are fro!

    Math$or)&, %ut there are free MATLAB clone&

    8ith li!ited functionality "cta#e and Scila%3

    MATLAB can %e do8nloaded fro! ide&eth9ch

  • 8/10/2019 02a Matlab Intro

    5/51

    2014-02-24 Modeling and Simulating Social Systems with MATLAB ..

    MATLAB en'ironment

  • 8/10/2019 02a Matlab Intro

    6/51

    2014-02-24 Modeling and Simulating Social Systems with MATLAB

    (hat is MATLAB)

    MATLAB deri#e& it& na!e fro! matri: la*oratory

    Interreted language ;o co!ilation li)e in Crocedural?i!erati#e rogra!!ing

    Matri:?array?#ector rogra!!ing

    "%ect-oriented rogra!!ing3

  • 8/10/2019 02a Matlab Intro

    7/512014-02-24 Modeling and Simulating Social Systems with MATLAB ++

    +'er'iew , (hat is MATLAB)

    MATLAB deri#e& it& na!e fro! !atri: la%oratory Scalars

    @ector&

    Matrice&

    x11

  • 8/10/2019 02a Matlab Intro

    8/512014-02-24 Modeling and Simulating Social Systems with MATLAB

    +'er'iew , (hat is MATLAB)

    MATLAB deri#e& it& na!e fro! !atri: la%oratory Scalar&

    -ectors

    Matrice&

    x11 x12 x13

    x11

    x12

    x13

  • 8/10/2019 02a Matlab Intro

    9/512014-02-24 Modeling and Simulating Social Systems with MATLAB //

    +'er'iew , (hat is MATLAB)

    MATLAB deri#e& it& na!e fro! !atri: la%oratory Scalar&

    @ector&

    Matrices

    x11 x12 x13

    x21 x22 x23

  • 8/10/2019 02a Matlab Intro

    10/512014-02-24 Modeling and Simulating Social Systems with MATLAB 1010

    +'er'iew , (hat is MATLAB)

    MATLAB deri#e& it& na!e fro! !atri: la%oratory Scalar&

    @ector&

    Matrice& Multi,dimensional

    x111 x121 x131x211 x221 x231

  • 8/10/2019 02a Matlab Intro

    11/512014-02-24 Modeling and Simulating Social Systems with MATLAB 1111

    .oc/et calculator

    MATLAB can %e u&ed a& a oc)et calculator

    >> 1+2+3

    ans=

    6

    >> (1+2)/3

    ans=

    1

  • 8/10/2019 02a Matlab Intro

    12/512014-02-24 Modeling and Simulating Social Systems with MATLAB 1212

    -aria*les and o0erators

    @aria%le a&&ign!ent i& !ade 8ith D

    @aria%le na!e& are ca&e &en&iti#eNum, num, NUM are all different #aria%le&

    >> num=10

    num =

    10

    The &e!icolon ED cancel& the #alidation di&lay

    >> B=5;

    >> C=10*B

    C =

    50

  • 8/10/2019 02a Matlab Intro

    13/512014-02-24 Modeling and Simulating Social Systems with MATLAB 11

    -aria*les and o0erators

    Ba&ic oerator& < - F ? addition &u%traction !ultilication di#i&ion

    G H:onentiation

    &7rt3 S7uare root co!!ent

    >> a=2; % First trm

    >> !=5; % "#$n trm

    >> #=&; % 'ir trm

    >> =a*(srt(#) + !2);>>

    =

    56

  • 8/10/2019 02a Matlab Intro

    14/512014-02-24 Modeling and Simulating Social Systems with MATLAB 1414

    "ata structures1 -ectors

    @ector& are u&ed to &tore a &et of &calar& @ector& are defined %y u&ing &7uare %rac)et J K

    >> =-0 2 . 10

    =

    0 2 . 10

  • 8/10/2019 02a Matlab Intro

    15/512014-02-24 Modeling and Simulating Social Systems with MATLAB 1.1.

    "ata structures1 "efining 'ectors

    >> =026

    =

    0 2 . 6

    @ector& can %e u&ed to generate a regular li&t of

    &calar& %y !ean& of colon D n1kn2generate a #ector of #alue& going fro! n1to n2

    8ith &te k

    The default #alue ofk i& 1

    >> =25

    =

    2 3 . 5

  • 8/10/2019 02a Matlab Intro

    16/512014-02-24 Modeling and Simulating Social Systems with MATLAB 11

    "ata structures1 Accessing 'ectors

    >> =1053;

    >> (2)ans =

    15

    Acce&& to the #alue& contained in a #ector x(i) return the ithele!ent of #ectorx

    x(i)i& a &calar and can %e a&&igned a ne8 #alue

    >> =15;

    >> (3)=10;

    >>

    =

    1 2 10 . 5

  • 8/10/2019 02a Matlab Intro

    17/512014-02-24 Modeling and Simulating Social Systems with MATLAB 1+1+

    "ata structures1 Sie of 'ectors

    >> =1053;

    >> s=nt()

    s =

    5

    @ector& oeration& The co!!and length(x)return the &i9e of the #ectorx

    x(i)return an error if i>length(x)>> =1053;

    >> (6)

    444 n #s matri imnsi$ns

  • 8/10/2019 02a Matlab Intro

    18/512014-02-24 Modeling and Simulating Social Systems with MATLAB 11

    "ata structures1 3ncrease sie of 'ectors

    >> =15;

    >> (6)=10;

    >>

    =

    1 2 3 . 5 10

    @ector& oeration& @ector &i9e& can %e dyna!ically increa&ed %y

    a&&igning a ne8 #alue, out&ide the #ector

  • 8/10/2019 02a Matlab Intro

    19/512014-02-24 Modeling and Simulating Social Systems with MATLAB 1/1/

    "ata structures1 3ncrease sie of 'ectors

    >> =15;

    >> (6)=10;

    >>

    =

    1 2 3 . 5 10

    @ector& oeration& @ector &i9e& can %e dyna!ically increa&ed %y

    a&&igning a ne8 #alue, out&ide the #ector

    Important: the first element of

    a vector has index 1

  • 8/10/2019 02a Matlab Intro

    20/512014-02-24 Modeling and Simulating Social Systems with MATLAB 2020

    "ata structures1 Su*,'ectors

    @ector& oeration& Su%#ector& can %e addre&&ed %y u&ing a colon

    x(i:j)return the &u% #ector ofx&tarting fro! the ith

    ele!ent to the th

    one>> =1022;

    >> =(2.);

    >>

    =

    12 1. 16

  • 8/10/2019 02a Matlab Intro

    21/512014-02-24 Modeling and Simulating Social Systems with MATLAB 2121

    "ata structures1 Matrices

    Matrice& are t8o di!en&ional #ector&

    Can %e defined %y u&ing &e!icolon into &7uare

    %rac)et& J K

    >> =-0 2 . ; 1 3 5 ; 7 7 7

    =0 2 .1 3 57 7 7

    >> =-1. ; 57 ; 128

    =1 2 3 .5 6 8 71 3 5 8

  • 8/10/2019 02a Matlab Intro

    22/512014-02-24 Modeling and Simulating Social Systems with MATLAB 2222

    "ata structures1 Matrices

    >> =-0 2 . ; 1 3 5 ; 7 7 7

    =0 2 .1 3 5

    7 7 7

    >> =(2,3)

    =

    5

    Acce&&ing the ele!ent& of a !atri: x(i,j) return the #alue located atithline andjthcolu!n

    iandjcan %e relaced %y a colon D to acce&& the

    entire line or colu!n

  • 8/10/2019 02a Matlab Intro

    23/512014-02-24 Modeling and Simulating Social Systems with MATLAB 22

    "ata structures1 Matrices

    >> =-0 2 . ; 1 3 5 ; 7 7 7

    =0 2 .1 3 5

    7 7 7

    >> =(2,)

    =

    1 3 5

    Acce&& to the #alue& contained in a !atri: x(i,j) return the #alue located atithline andjthcolu!n

    iandjcan %e relaced %y a colon D to acce&& the

    entire line or colu!n

  • 8/10/2019 02a Matlab Intro

    24/51

    2014-02-24 Modeling and Simulating Social Systems with MATLAB 2424

    "ata structures1 Matrices

    >> =-0 2 . ; 1 3 5 ; 7 7 7

    =0 2 .1 3 57 7 7

    >> =(,3)

    =

    .

    5

    7

    Acce&& to the #alue& contained in a !atri: x(i,j) return the #alue located atithline andjthcolu!n

    iandjcan %e relaced %y a colon D to acce&& the

    entire line or colu!n

  • 8/10/2019 02a Matlab Intro

    25/51

    2014-02-24 Modeling and Simulating Social Systems with MATLAB 2.2.

    Matrices o0erations1 Trans0ose

    Tran&o&e !atri: S8itche& line& and colu!n&

    transpose(x) or &i!ly x

    >> =-13 ; .6 =1 2 3. 5 6

    >> trans9$s() =

    1 .2 53 6

    >> : =1 .2 5

    3 6

  • 8/10/2019 02a Matlab Intro

    26/51

    2014-02-24 Modeling and Simulating Social Systems with MATLAB 22

    Matrices o0erations

    Inter-!atrice& oeration& CA> =-1 2;3 . ; B=-2 2;1 1;

    >> C=+B

    C =3 .

    . 5

    >> C=

  • 8/10/2019 02a Matlab Intro

    27/51

    2014-02-24 Modeling and Simulating Social Systems with MATLAB 2+2+

    Matrices o0erations1 Multi0lication

    Inter-!atrice& oeration& CAFB i& a !atri: roduct eturn& C 8ith

    C(i,j) = (k=1 to N)A(i,k)*B(k,j)

    ; i& the nu!%er of colu!n& of A 8hich !u&t e7ual the

    nu!%er of ro8& of B

  • 8/10/2019 02a Matlab Intro

    28/51

    2014-02-24 Modeling and Simulating Social Systems with MATLAB 22

    Matrices o0erations

    Inter-!atrice& oeration& CA> =-1 2;3 . ; B=-2 2;1 1;

    >> C=+B

    C =3 .

    . 5

    >> C=

  • 8/10/2019 02a Matlab Intro

    29/51

    2014-02-24 Modeling and Simulating Social Systems with MATLAB 2/2/

    Element,wise multi0lication

    Inter-!atrice& oeration& CAFB return& C 8ith C(i,j) = A(i,j)*B(i,j)

    A and B !u&t ha#e the &a!e &i9e, unle&& one of the! i& a &calar

    >> =-2 2 2;. . .;

    >> B=-2 2 2;1 1 1;

    >> C=*BC =. . .. . .

  • 8/10/2019 02a Matlab Intro

    30/51

    2014-02-24 Modeling and Simulating Social Systems with MATLAB 00

    Element,wise di'ision

    Inter-!atrice& oeration& CA?B return& C 8ith C(i,j) = A(i,j)/B(i,j)

    A and B !u&t ha#e the &a!e &i9e, unle&& one of the! i& a &calar

    >> =-2 2 2;. . .;

    >> B=-2 2 2;1 1 1;

    >> C=/B

    C =

    1 1 1. . .

  • 8/10/2019 02a Matlab Intro

    31/51

    2014-02-24 Modeling and Simulating Social Systems with MATLAB 11

    Matrices o0erations1 "i'ision

    Inter-!atrice& oeration& :A% return& the &olution of the linear e7uation AF:%

    A i& a n-%y-n !atri: and % i& a colu!n #ector of &i9e n

    >> =-3 2

  • 8/10/2019 02a Matlab Intro

    32/51

    2014-02-24 Modeling and Simulating Social Systems with MATLAB 22

    Matrices1 reating

    Matrice& can al&o created %y the&e co!!and&

    ran(n, m)

    a !atri: of &i9e n: m, containing rando!

    nu!%er& J0,1K

    r$s(n, m), $ns(n, m)a !atri: containing 0 or 1 for all ele!ent&

  • 8/10/2019 02a Matlab Intro

    33/51

    2014-02-24 Modeling and Simulating Social Systems with MATLAB

    Matrices "imensions

    >> = r$s(3,.);

    >> si()

    ans =

    3 .

    >> si(,1)

    ans =

    3

    >> si(,2)

    ans =

    .

    si() return& info a%out a !atri:D& di!en&ion&

  • 8/10/2019 02a Matlab Intro

    34/51

    2014-02-24 Modeling and Simulating Social Systems with MATLAB 44

    The forloo0

    @ector& are often roce&&ed 8ith loo& in order

    to acce&& and roce&& each #alue, one after the

    other

    Synta:

    ?$r i=

    @

    n

    $ith- ithe na!e of the running #aria%le

    - xa #ector containing the &e7uence of #alue& a&&igned to i

  • 8/10/2019 02a Matlab Intro

    35/51

    2014-02-24 Modeling and Simulating Social Systems with MATLAB ..

    The forloo0

    >> ?$r i=13

    i2n

    i =

    1

    i =

    .

    i =

    &

    MATLAB 8ait& for the )ey8ord n%efore co!uting

    the re&ult

  • 8/10/2019 02a Matlab Intro

    36/51

    2014-02-24 Modeling and Simulating Social Systems with MATLAB

    The forloo0

    >> ?$r i=13

    (i)=i2;n

    >>

    =

    1 . &

    MATLAB 8ait& for the )ey8ord n%efore co!uting

    the re&ult

  • 8/10/2019 02a Matlab Intro

    37/51

    2014-02-24 Modeling and Simulating Social Systems with MATLAB ++

    onditional statements1 if

    The )ey8ord ifi& u&ed to te&t a condition Synta:

    i? (#$niti$n)

    sun# $? #$mmansn

    The condition i& a Boolean oeration

    The &e7uence of co!!and& i& e:ecuted if the te&ted

    condition i& true

  • 8/10/2019 02a Matlab Intro

    38/51

    2014-02-24 Modeling and Simulating Social Systems with MATLAB

    Logical o0erators

    Logical oerator& N , O le&& than, greater than

    e7ual to

    PP and QQ or

    R not Rtrue i& !l"e3

    1 &tand& for true, 0 &tand& for fal&e3

  • 8/10/2019 02a Matlab Intro

    39/51

    2014-02-24 Modeling and Simulating Social Systems with MATLAB //

    onditional statements1 E5am0le

    >> trs$=5;

    >> =.5;

    >> i? (Atrs$)

    i?? = trs$ < ;

    n

    >> i??

    i?? =

    05

    An e:a!le

  • 8/10/2019 02a Matlab Intro

    40/51

    2014-02-24 Modeling and Simulating Social Systems with MATLAB 4040

    onditional statements1 else

    The )ey8ord elsei& otional Synta:

    i? (#$niti$n)

    sun# $? #$mmans n1

    s

    sun# $? #$mmans n2

    n

    >> i? (Atrs$)

    i?? = trs$ < ;

    s

    i?? = trs$;

    n

  • 8/10/2019 02a Matlab Intro

    41/51

    2014-02-24 Modeling and Simulating Social Systems with MATLAB 4141

    Scri0ts and functions

    H:ternal file& u&ed to &tore and &a#e &e7uence&

    of co!!and&

    Scrit& Si!le &e7uence of co!!and&

    lo%al #aria%le&

    5unction&

    (edicated to a articular ta&) Inut& and outut&

    Local #aria%le&

  • 8/10/2019 02a Matlab Intro

    42/51

    2014-02-24 Modeling and Simulating Social Systems with MATLAB 4242

    Scri0ts and functions

    Should %e &a#ed a& mfile&

    5ro! the

    (irectory $indo8

  • 8/10/2019 02a Matlab Intro

    43/51

    2014-02-24 Modeling and Simulating Social Systems with MATLAB 44

    Scri0ts and functions

    Scrit& Create ! file, eg sumD#t$rm

    Tye co!!and& in the file

    Tye the file na!e, eg sumD#t$r, in the co!!and8indo8

    %sum of 4 values in x

    =-1 3 5 8;

    =(1)+(2)+(3)+(.);

    sum-ectorm

    >> sumD#t$r

    =16

  • 8/10/2019 02a Matlab Intro

    44/51

    2014-02-24 Modeling and Simulating Social Systems with MATLAB 4444

    Scri0ts and functions

    Ma)e &ure that the file i& in your 8or)ing

    directory

  • 8/10/2019 02a Matlab Intro

    45/51

    2014-02-24 Modeling and Simulating Social Systems with MATLAB 4.4.

    Scri0ts and functions

    5unction& Create ! file, eg a!s$utDam

    (eclare inut& and outut& in the fir&t line of the file,

    function [out1, out2, ] = functionName (in1, in2, )

    eg ?un#ti$n - = a!s$utDa()

    U&e the function in the co!!and 8indo8

    functionName(in1, in2, )

    eg a!s$utDa()

  • 8/10/2019 02a Matlab Intro

    46/51

    2014-02-24 Modeling and Simulating Social Systems with MATLAB 44

    Scri0ts and functions

    ?un#ti$n - = a!s$utDa()

    % Compute the absolute value of x

    i? (A0)

    = > =a!s$utDa(>

    =

    5

  • 8/10/2019 02a Matlab Intro

    47/51

    2014-02-24 Modeling and Simulating Social Systems with MATLAB 4+4+

    Scri0ts and functions

    ?un#ti$n - = a!s$utDa()

    % Compute the absolute value of x

    i? (A0)

    = > =a!s$utDa(>

    =

    5

  • 8/10/2019 02a Matlab Intro

    48/51

    2014-02-24 Modeling and Simulating Social Systems with MATLAB 44

    E5ercise #

    om0ute1

    a) b)

    c)

    Slide&?e:erci&e& 888&o!&eth9ch?!atla%

    255

    10718

    +

    =

    100

    0i

    i

    =

    10

    5

    2 )(i

    ii

  • 8/10/2019 02a Matlab Intro

    49/51

    2014-02-24 Modeling and Simulating Social Systems with MATLAB 4/4/

    E5ercise 2

    Sol#e for 51

    4522

    32

    223321432

    4321

    4321

    4321

    4321

    =++

    =

    =++=+

    xxxx

    xxxx

    xxxx

    xxxx

  • 8/10/2019 02a Matlab Intro

    50/51

    2014-02-24 Modeling and Simulating Social Systems with MATLAB .0.0

    E5ercise %

    6i*onacci se7uence1 8rite a function 8hich

    co!ute& the 5i%onacci &e7uence of a gi#en

    nu!%er nand return& the re&ult in a #ector

    The 5i%onacci &e7uence F(n) i& gi#en %y

  • 8/10/2019 02a Matlab Intro

    51/51

    8eferences

    htt??888!ath8or)&ch?roduct&?!atla%?inde:

    ht!l

    htt??888!ath8or)&ch?!atla%central?inde:ht!

    l htt&??ide&eth9ch?

    http://www.mathworks.ch/products/matlab/index.htmlhttp://www.mathworks.ch/products/matlab/index.htmlhttp://www.mathworks.ch/matlabcentral/index.htmlhttp://www.mathworks.ch/matlabcentral/index.htmlhttps://ides.ethz.ch/https://ides.ethz.ch/http://www.mathworks.ch/matlabcentral/index.htmlhttp://www.mathworks.ch/matlabcentral/index.htmlhttp://www.mathworks.ch/products/matlab/index.htmlhttp://www.mathworks.ch/products/matlab/index.html