matlab_tutorial1

Upload: nicholas-ward

Post on 02-Jun-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 MATLAB_tutorial1

    1/3

    MATLAB tutorial 1: using the command line

    Start matlab: open a terminal and type matlab &

    At the matlab command prompt typea = 1

    and press enter. Nextb = 1.0

    and press enter. To see what ariable are c!rrently de"ined typewhos

    #o! sho!ld see somethin$ li%e: whos Name Si'e (ytes )lass Attrib!tes

    a 1x1 * do!bleb 1x1 * do!ble

    An array is a list o" n!mbers assi$ned to one ariable. Typex = + 0 1 , - / we may also !se

    y = 0:0./:/Try

    x-2 = ,,

    To preent matlab "rom printin$ o!t the entire array eery time we chan$e somethin$3 we add asemicolon at the end o" the command:

    x2 = 4105

    Arrays can be manip!lated !sin$ matrix al$ebra:' = ,6yor

    w = ,6y7'Now try

    x6xx.6xx6x8x86x

    Note that the sin$le dash a"ter an array means transpose 9 i.e. a swap o" the rows and col!mns. Notealso that the dot in "ront o" the m!ltiplication si$n o" the second command means we m!ltiply element4by4element.

    et8s try to plot somethin$:plotx3-6x2

    A new plot window sho!ld hae appeared. Now try! = x.6xplotx3!2

  • 8/10/2019 MATLAB_tutorial1

    2/3

    ;e can sae this plot as a pn$ "ile byprint 4dpn$

    or as an encaps!lated postscript "ile byprint 4depsc,

    These commands created the "iles "i$!re1.pn$ and "i$!re1.eps3 which we co!ld rename e.$. m"i$!re1.pn$ plotN?@ command line. ?se the !p and down arrows and pa$e4!p & pa$e4down tonai$ate the "ile. ;hen yo!8re done press .

    oad the data into matlab with:+con$ress3 year3 total3 dems3 reps = textread8partydi.txt83 8s " " " "83B*38headerlines83-2

    (e care"!l here3 there is a space be"ore each si$n.

    et8s ma%e some plots:plotyear3seats2xlabel8#ear82ylabel8Total Co!se Seats82

    Now tryplotyear3dems.Dtotal384b82xlabel8#ear82ylabel8Eercenta$e82

    ;e can oerplot by t!rnin$ the hold onhold onplotyear3reps.Dtotal384r82hold o""

    ;e can add a le$end withle$end8democrats83 8rep!blicans82

    #o! can moe the le$end aro!nd !sin$ the F?> by "irst clic%in$ the arrow3 then selectin$ the le$endbox and dra$$in$ it.

    #o! can $et a sense "or how correlated or anti4correlated2 a dataset is by loo%in$ at the correlationcoe""icient:

    corrcoe"dems.Dtotal3reps.Dtotal2$ies yo! a ,x, matrix o" coe""icients relatin$ each col!mn to itsel" alon$ the dia$onal2 and to theother col!mns2 o""4dia$onals2.

  • 8/10/2019 MATLAB_tutorial1

    3/3

    TASK

    Now that yo! hae completed yo!r t!torial3 letGs do some scienceH

    >nside yo!r home directory3 yo! sho!ld "ind a "ile called Iecent>ndices.txt. ?se in!x to see what isinside the "ile and what eery col!mn o" the data is. The "irst col!mn is the year and the second is the

    month o" the obseration. The third col!mn is the obsered s!nspot n!mber3 the *th

    is the obseredradio "l!x3 and the 10this the obsered AE index a $eoma$netic index2.

    To complete this tas%3 yo! m!st12 $enerate an ima$e pn$2 "iles that contain the s!nspot n!mber as a "!nction o" time3 the radio "l!x asa "!nction o" time3 and the obsered AE index as a "!nction o" time. Elot the lines in di""erent colorsand ma%e a le$end

    ,2 $enerate , postscript "iles3 one that contains the scatter plot o" the s!nspot n!mber on the x4axis andradio "l!x on the y axis and one that contains the s!nspot n!mber s AE index. )alc!late thecorrelation coe""icient between the two al!es and incl!de that in"ormation in the title o" the plot.

    (e s!re to label all axes o" yo!r plots.;hen yo! are done with this tas%3 email the res!lts - "iles2 to Jacob.heeri%h!isenK!ah.ed!

    mailto:[email protected]:[email protected]