Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
167 views

Matlab 6

Uploaded by

Abdou
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
167 views

Matlab 6

Uploaded by

Abdou
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 296

MATLAB

STUDENT VERSION

Computation

Visualization

Programming

Learning MATLAB
Version 6 (R elease 12)
How to Contact The MathWorks:

www. ma t h wo r k s . c o m Web
f t p . ma t h wo r k s . c o m An on ym ou s F TP ser ver
c o mp . s o f t - s y s . ma t l a b Newsgr ou p

s u g g e s t @ma t h wo r k s . c o m P r odu ct en h a n cem en t su ggest ion s


b u g s @ma t h wo r k s . c o m Bu g r epor t s
d o c @ma t h wo r k s . c o m Docu m en t a t ion er r or r epor t s

ISBN 0-9672195-3-1

L earn in g M AT L AB
 COP YRIGH T 1999 - 2001 by Th e Ma t h Wor ks, In c.
Th e soft wa r e descr ibed in t h is docu m en t is fu r n ish ed u n der a licen se a gr eem en t . Th e soft wa r e m a y be u sed
or copied on ly u n der t h e t er m s of t h e licen se a gr eem en t . No pa r t of t h is m a n u a l m a y be ph ot ocopied or r epr o-
du ced in a n y for m wit h ou t pr ior wr it t en con sen t fr om Th e Ma t h Wor ks, In c.
F E DE RAL ACQUISITION: Th is pr ovision a pplies t o a ll a cqu isit ion s of t h e P r ogr a m a n d Docu m en t a t ion by
or for t h e feder a l gover n m en t of t h e Un it ed St a t es. By a ccept in g deliver y of t h e P r ogr a m , t h e gover n m en t
h er eby a gr ees t h a t t h is soft wa r e qu a lifies a s "com m er cia l" com pu t er soft wa r e wit h in t h e m ea n in g of F AR
P a r t 12.212, DF ARS P a r t 227.7202-1, DF ARS P a r t 227.7202-3, DF ARS P a r t 252.227-7013, a n d DF ARS P a r t
252.227-7014. Th e t er m s a n d con dit ion s of Th e Ma t h Wor ks, In c. Soft wa r e Licen se Agr eem en t sh a ll per t a in
t o t h e gover n m en t ’s u se a n d disclosu r e of t h e P r ogr a m a n d Docu m en t a t ion , a n d sh a ll su per sede a n y
con flict in g con t r a ct u a l t er m s or con dit ion s. If t h is licen se fa ils t o m eet t h e gover n m en t ’s m in im u m n eeds or
is in con sist en t in a n y r espect wit h feder a l pr ocu r em en t la w, t h e gover n m en t a gr ees t o r et u r n t h e P r ogr a m
a n d Docu m en t a t ion , u n u sed, t o Ma t h Wor ks.
MATLAB, Sim u lin k, St a t eflow, H a n dle Gr a ph ics, a n d Rea l-Tim e Wor ksh op a r e r egist er ed t r a dem a r ks, a n d
Ta r get La n gu a ge Com piler is a t r a dem a r k of Th e Ma t h Wor ks, In c.
Ot h er pr odu ct or br a n d n a m es a r e t r a dem a r ks or r egist er ed t r a dem a r ks of t h eir r espect ive h older s.
P r in t in g H ist or y: Au gu st 1999 F ir st pr in t in g New m a n u a l
J a n u a r y 2001 Secon d pr in t in g Revised for MATLAB 6.0 (Relea se 12)
Contents
In tro d u c tio n
1
Abo u t th e S tu d e n t Ve rs io n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2
St u den t Use P olicy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2
Differ en ces Bet ween t h e St u den t Ver sion
a n d t h e P r ofession a l Ver sion . . . . . . . . . . . . . . . . . . . . . . . . . . 1-3

Obta in in g Ad d itio n a l Ma th Wo rk s P ro d u c ts . . . . . . . . . . . . . 1-5

Ge ttin g S ta rte d w ith MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . 1-6

F in d in g Re fe re n c e In fo rm a tio n . . . . . . . . . . . . . . . . . . . . . . . . 1-7

Tro u ble s h o o tin g a n d Oth e r Re s o u rc e s . . . . . . . . . . . . . . . . . . 1-8


Docu m en t a t ion Libr a r y . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-8
Ma t h Wor ks Web Sit e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-10
Ma t h Wor ks E du ca t ion Web Sit e . . . . . . . . . . . . . . . . . . . . . . . 1-10
MATLAB Rela t ed Books . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-10
Ma t h Wor ks St or e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-10
Usen et Newsgr ou p . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-10
Ma t h Wor ks Kn owledge Ba se . . . . . . . . . . . . . . . . . . . . . . . . . . 1-10
Tech n ica l Su ppor t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-11
P r odu ct Regist r a t ion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-11

Abo u t MATLAB a n d S im u lin k . . . . . . . . . . . . . . . . . . . . . . . . . 1-12


Wh a t Is MATLAB? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-12
Wh a t Is Sim u lin k? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-14

iii
In s ta lla tio n
2
In s ta llin g o n Win d o w s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2
Syst em Requ ir em en t s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2
In st a llin g MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-4
In st a llin g Addit ion a l Toolboxes . . . . . . . . . . . . . . . . . . . . . . . . . . 2-5
Accessin g t h e On lin e Docu m en t a t ion (H elp) . . . . . . . . . . . . . . . 2-6

In s ta llin g o n Lin u x . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7


Syst em Requ ir em en t s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7
In st a llin g MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-8
P ost In st a lla t ion P r ocedu r es . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-13
In st a llin g Addit ion a l Toolboxes . . . . . . . . . . . . . . . . . . . . . . . . . 2-13
Accessin g t h e On lin e Docu m en t a t ion (H elp) . . . . . . . . . . . . . . 2-14

D e v e lo p m e n t En v iro n m e n t
3
In tro d u c tio n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-2

S ta rtin g a n d Qu ittin g MATLAB . . . . . . . . . . . . . . . . . . . . . . . . 3-3


St a r t in g MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-3
Qu it t in g MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-3

MATLAB D e s k to p . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-4

D e s k to p To o ls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-6
Com m a n d Win dow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-6
La u n ch P a d . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-8
H elp Br owser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-8
Cu r r en t Dir ect or y Br owser . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-11
Wor kspa ce Br owser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-12
E dit or /Debu gger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-14

Oth e r D e v e lo p m e n t En v iro n m e n t F e a tu re s . . . . . . . . . . . . . 3-15

iv Contents
Ge ttin g S ta rte d
4
Ma tric e s a n d Ma g ic S qu a re s . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2
E n t er in g Ma t r ices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-3
su m , t r a n spose, a n d dia g . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-4
Su bscr ipt s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-6
Th e Colon Oper a t or . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-7
Th e m a gic F u n ct ion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-8

Ex p re s s io n s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-10
Va r ia bles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-10
Nu m ber s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-10
Oper a t or s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-11
F u n ct ion s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-11
E xa m ples of E xpr ession s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-13

Wo rk in g w ith Ma tric e s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-14


Gen er a t in g Ma t r ices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-14
Th e loa d Com m a n d . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-15
M-F iles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-15
Con ca t en a t ion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-16
Delet in g Rows a n d Colu m n s . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-16

Mo re Abo u t Ma tric e s a n d Arra y s . . . . . . . . . . . . . . . . . . . . . . 4-18


Lin ea r Algebr a . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-18
Ar r a ys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-21
Mu lt iva r ia t e Da t a . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-23
Sca la r E xpa n sion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-24
Logica l Su bscr ipt in g . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-25
Th e fin d F u n ct ion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-26

Co n tro llin g Co m m a n d Win d o w In p u t a n d Ou tp u t . . . . . . . 4-28


Th e for m a t Com m a n d . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-28
Su ppr essin g Ou t pu t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-30
E n t er in g Lon g Com m a n d Lin es . . . . . . . . . . . . . . . . . . . . . . . . 4-30
Com m a n d Lin e E dit in g . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-30

v
Gra p h ic s
5
B a s ic P lo ttin g . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-2
Cr ea t in g a P lot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-2
Mu lt iple Da t a Set s in On e Gr a ph . . . . . . . . . . . . . . . . . . . . . . . . 5-3
Specifyin g Lin e St yles a n d Color s . . . . . . . . . . . . . . . . . . . . . . . . 5-4
P lot t in g Lin es a n d Ma r ker s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-5
Im a gin a r y a n d Com plex Da t a . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-6
Addin g P lot s t o a n E xist in g Gr a ph . . . . . . . . . . . . . . . . . . . . . . . 5-7
F igu r e Win dows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-9
Mu lt iple P lot s in On e F igu r e . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-9
Con t r ollin g t h e Axes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-10
Axis La bels a n d Tit les . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-12
Sa vin g a F igu r e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-13

Ed itin g P lo ts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-14
In t er a ct ive P lot E dit in g . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-14
Usin g F u n ct ion s t o E dit Gr a ph s . . . . . . . . . . . . . . . . . . . . . . . . 5-14
Usin g P lot E dit in g Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-15
Usin g t h e P r oper t y E dit or . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-16

Me s h a n d S u rfa c e P lo ts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-18
Visu a lizin g F u n ct ion s of Two Va r ia bles . . . . . . . . . . . . . . . . . . 5-18

Im a g e s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-24

P rin tin g Gra p h ic s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-26

Ha n d le Gra p h ic s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-28
Gr a ph ics Object s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-28
Set t in g Object P r oper t ies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-31
F in din g t h e H a n dles of E xist in g Object s . . . . . . . . . . . . . . . . . 5-33

Gra p h ic s U s e r In te rfa c e s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-35


Gr a ph ica l User In t er fa ce Design Tools . . . . . . . . . . . . . . . . . . . 5-35

An im a tio n s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-37
E r a se Mode Met h od . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-37
Cr ea t in g Movies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-38

vi Contents
P ro g ra m m in g w ith MATLAB
6
F lo w Co n tro l . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2
if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2
swit ch a n d ca se . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-4
for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-4
wh ile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-5
con t in u e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-5
br ea k . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-6

Oth e r D a ta S tru c tu re s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-7


Mu lt idim en sion a l Ar r a ys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-7
Cell Ar r a ys . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-9
Ch a r a ct er s a n d Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-11
St r u ct u r es . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-14

S c rip ts a n d F u n c tio n s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-17


Scr ipt s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-17
F u n ct ion s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-18
Globa l Va r ia bles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-20
P a ssin g St r in g Ar gu m en t s t o F u n ct ion s . . . . . . . . . . . . . . . . . . 6-20
Th e eva l F u n ct ion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-22
Vect or iza t ion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-23
P r ea lloca t ion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-23
F u n ct ion H a n dles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-24
F u n ct ion F u n ct ion s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-24

D e m o n s tra tio n P ro g ra m s In c lu d e d w ith MATLAB . . . . . . 6-27

S y m bo lic Ma th To o lbo x
7
In tro d u c tio n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-2

Ge ttin g He lp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-4

v ii
Ge ttin g S ta rte d . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-5
Sym bolic Object s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-5
Cr ea t in g Sym bolic Va r ia bles a n d E xpr ession s . . . . . . . . . . . . . . 7-6
Sym bolic a n d Nu m er ic Con ver sion s . . . . . . . . . . . . . . . . . . . . . . 7-7
Cr ea t in g Sym bolic Ma t h F u n ct ion s . . . . . . . . . . . . . . . . . . . . . 7-14

Ca lc u lu s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-16
Differ en t ia t ion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-16
Lim it s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-20
In t egr a t ion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-22
Sym bolic Su m m a t ion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-27
Ta ylor Ser ies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-28
E xt en ded Ca lcu lu s E xa m ple . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-29

S im p lific a tio n s a n d S u bs titu tio n s . . . . . . . . . . . . . . . . . . . . . 7-43


Sim plifica t ion s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-43
Su bst it u t ion s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-51

Va ria ble -P re c is io n Arith m e tic . . . . . . . . . . . . . . . . . . . . . . . . . 7-58


Over view . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-58
E xa m ple: Usin g t h e Differ en t Kin ds of Ar it h m et ic . . . . . . . . . 7-59
An ot h er E xa m ple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-61

Lin e a r Alg e bra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-63


Ba sic Algebr a ic Oper a t ion s . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-63
Lin ea r Algebr a ic Oper a t ion s . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-64
E igen va lu es . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-68
J or da n Ca n on ica l F or m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-74
Sin gu la r Va lu e Decom posit ion . . . . . . . . . . . . . . . . . . . . . . . . . 7-76
E igen va lu e Tr a ject or ies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-78

S o lv in g Equ a tio n s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-86


Solvin g Algebr a ic E qu a t ion s . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-86
Sever a l Algebr a ic E qu a t ion s . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-87
Sin gle Differ en t ia l E qu a t ion . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-90
Sever a l Differ en t ia l E qu a t ion s . . . . . . . . . . . . . . . . . . . . . . . . . 7-92

v iii Contents
MATLAB Qu ic k Re fe re n c e
A

S y m bo lic Ma th To o lbo x Qu ic k Re fe re n c e
B

ix
x Contents
1

In t r odu ct ion
Abo u t th e S tu d e n t Ve rs io n . . . . . . . . . . . . . 1-2

Obta in in g Ad d itio n a l Ma th Wo rk s P ro d u c ts . . . . . . 1-5

Ge ttin g S ta rte d w ith MATLAB . . . . . . . . . . . . 1-6

F in d in g Re fe re n c e In fo rm a tio n . . . . . . . . . . . 1-7

Tro u ble s h o o tin g a n d Oth e r Re s o u rc e s . . . . . . . . 1-8

Abo u t MATLAB a n d S im u lin k . . . . . . . . . . . . 1-12


1 Introduction

About the Student Version


MATLAB ® & Sim u lin k ® a r e t h e pr em ier soft wa r e pa cka ges for t ech n ica l
com pu t a t ion , da t a a n a lysis, a n d visu a liza t ion in edu ca t ion a n d in du st r y. Th e
St u den t Ver sion of MATLAB & Sim u lin k pr ovides a ll of t h e fea t u r es of
pr ofession a l MATLAB, wit h n o lim it a t ion s, a n d t h e fu ll fu n ct ion a lit y of
pr ofession a l Sim u lin k, wit h m odel sizes u p t o 300 blocks. Th e St u den t Ver sion
gives you im m edia t e a ccess t o t h e h igh -per for m a n ce n u m er ic com pu t in g power
you n eed.
MATLAB a llows you t o focu s on you r cou r se wor k a n d a pplica t ion s r a t h er t h a n
on pr ogr a m m in g det a ils. It en a bles you t o solve m a n y n u m er ica l pr oblem s in a
fr a ct ion of t h e t im e it wou ld t a ke you t o wr it e a pr ogr a m in a lower level
la n gu a ge. MATLAB h elps you bet t er u n der st a n d a n d a pply con cept s in
a pplica t ion s r a n gin g fr om en gin eer in g a n d m a t h em a t ics t o ch em ist r y, biology,
a n d econ om ics.
Sim u lin k, in clu ded wit h t h e St u den t Ver sion , pr ovides a block dia gr a m t ool for
m odelin g a n d sim u la t in g dyn a m ica l syst em s, in clu din g sign a l pr ocessin g,
con t r ols, com m u n ica t ion s, a n d ot h er com plex syst em s.
Th e Sym bolic Ma t h Toolbox, a lso in clu ded wit h t h e St u den t Ver sion , is ba sed
on t h e Ma ple ® V sym bolic ker n el a n d let s you per for m sym bolic com pu t a t ion s
a n d va r ia ble-pr ecision a r it h m et ic.
MATLAB pr odu ct s a r e u sed in a br oa d r a n ge of in du st r ies, in clu din g
a u t om ot ive, a er ospa ce, elect r on ics, en vir on m en t a l, t elecom m u n ica t ion s,
com pu t er per iph er a ls, fin a n ce, a n d m edica l. Mor e t h a n 400,000 t ech n ica l
pr ofession a ls a t t h e wor ld’s m ost in n ova t ive t ech n ology com pa n ies,
gover n m en t r esea r ch la bs, fin a n cia l in st it u t ion s, a n d a t m or e t h a n 2,000
u n iver sit ies r ely on MATLAB a n d Sim u lin k a s t h e fu n da m en t a l t ools for t h eir
en gin eer in g a n d scien t ific wor k.

Student Use Policy


Th is St u den t Licen se is for u se in con ju n ct ion wit h cou r ses offer ed a t a
degr ee-gr a n t in g in st it u t ion . Th e Ma t h Wor ks offer s t h is licen se a s a specia l
ser vice t o t h e st u den t com m u n it y a n d a sks you r h elp in seein g t h a t it s t er m s
a r e n ot a bu sed.
To u se t h is St u den t Licen se, you m u st be a st u den t u sin g t h e soft wa r e in
con ju n ct ion wit h cou r ses offer ed a t degr ee-gr a n t in g in st it u t ion s.

1-2
About the Student Version

You m a y n ot u se t h is St u den t Licen se a t a com pa n y or gover n m en t la b. Also,


you m a y n ot u se it for r esea r ch or for com m er cia l or in du st r ia l pu r poses. In
t h ese ca ses, you ca n a cqu ir e t h e a ppr opr ia t e pr ofession a l or a ca dem ic ver sion
of t h e soft wa r e by con t a ct in g Th e Ma t h Wor ks.

Differences Betw een the Student Version a nd the


Professiona l Version
MATLAB
Th is ver sion of MATLAB pr ovides fu ll su ppor t for a ll la n gu a ge fea t u r es a s well
a s gr a ph ics, ext er n a l in t er fa ce a n d Applica t ion P r ogr a m In t er fa ce su ppor t , a n d
a ccess t o ever y ot h er fea t u r e of t h e pr ofession a l ver sion of MATLAB.

N ote MATLAB does n ot h a ve a m a t r ix size lim it a t ion in t h is St u den t


Ver sion .

MATLAB Differences. Th er e a r e a few sm a ll differ en ces bet ween t h e St u den t


Ver sion a n d t h e pr ofession a l ver sion of MATLAB:

• Th e MATLAB pr om pt in t h e St u den t Ver sion is


EDU>>

• Th e win dow t it le ba r s in clu de t h e wor ds


<St u den t Ver sion >

• All pr in t ou t s con t a in t h e foot er


St u d e n t Ve r s i o n o f MATLAB

Th is foot er is n ot a n opt ion t h a t ca n be t u r n ed off; it will a lwa ys a ppea r in


you r pr in t ou t s.

1-3
1 Introduction

Simulink
Th is St u den t Ver sion con t a in s t h e com plet e Sim u lin k pr odu ct , wh ich is u sed
wit h MATLAB t o m odel, sim u la t e, a n d a n a lyze dyn a m ica l syst em s.

Simulink Differences.

• Models a r e lim it ed t o 300 blocks.


• Th e win dow t it le ba r s in clu de t h e wor ds
<St u den t Ver sion >

• All pr in t ou t s con t a in t h e foot er


St u d e n t Ve r s i o n o f MATLAB

Th is foot er is n ot a n opt ion t h a t ca n be t u r n ed off; it will a lwa ys a ppea r in


you r pr in t ou t s.

N ote Usin g S im u lin k , wh ich is a ccessible fr om t h e H elp br owser, con t a in s a ll


of t h e Sim u lin k r ela t ed in for m a t ion in t h e L earn in g S im u lin k book plu s
a ddit ion a l, a dva n ced in for m a t ion .

Symbolic Math Toolbox


Th e Sym bolic Ma t h Toolbox in clu ded wit h t h is St u den t Ver sion let s you u se a n
im por t a n t su bset of Ma ple. You ca n a ccess a ll of t h e fu n ct ion s in t h e
pr ofession a l ver sion of t h e Sym bolic Ma t h Toolbox except ma p l e , ma p l e i n i t ,
mf u n , mf u n l i s t , a n d mh e l p . F or a com plet e list of a ll t h e a va ila ble fu n ct ion s, see
Appen dix B, “Sym bolic Ma t h Toolbox Qu ick Refer en ce.”

1-4
Obtaining Additional MathW orks Products

O bta ining Additiona l M a thW ork s Products


Ma n y college cou r ses r ecom m en d MATLAB a s t h eir st a n da r d in st r u ct ion a l
soft wa r e. In som e ca ses, t h e cou r ses m a y r equ ir e pa r t icu la r t oolboxes,
blockset s, or ot h er pr odu ct s. Ma n y of t h ese pr odu ct s a r e a va ila ble for st u den t
u se. You m a y pu r ch a se a n d down loa d t h ese a ddit ion a l pr odu ct s a t specia l
st u den t pr ices fr om t h e Ma t h Wor ks St or e a t www. ma t h wo r k s . c o m/ s t o r e .
Alt h ou gh m a n y pr ofession a l t oolboxes a r e a va ila ble a t st u den t pr ices fr om t h e
Ma t h Wor ks St or e, n ot ever y on e is a va ila ble for st u den t u se. Som e of t h e
t oolboxes you ca n pu r ch a se in clu de:

• Com m u n ica t ion s


• Con t r ol Syst em
• F u zzy Logic
• Im a ge P r ocessin g
• Neu r a l Net wor k
• Opt im iza t ion
• Sign a l P r ocessin g
• St a t ist ics
• St a t eflow ® (A dem o ver sion of St a t eflow is in clu ded
wit h you r St u den t Ver sion .)

F or a n u p-t o-da t e list of wh ich t oolboxes a r e a va ila ble, visit t h e Ma t h Wor ks


St or e.

N ote Th e t oolboxes t h a t a r e a va ila ble for t h e St u den t Ver sion of MATLAB &
Sim u lin k h a ve t h e sa m e fu n ct ion a lit y a s t h e fu ll, pr ofession a l ver sion s.
H owever, t h ese st u den t ver sion s will on ly wor k wit h t h e St u den t Ver sion .
Likewise, t h e pr ofession a l ver sion s of t h e t oolboxes will n ot wor k wit h t h e
St u den t Ver sion .

1-5
1 Introduction

Getting Sta rted w ith M ATLAB

W ha t I W a nt W ha t I Should Do

I n eed t o in st a ll MATLAB. See Ch a pt er 2, “In st a lla t ion ,” in t h is book.

I wa n t t o st a r t MATLAB. (P C) You r Ma t h Wor ks docu m en t a t ion CD m u st be in you r


CD-ROM dr ive t o st a r t MATLAB. Dou ble-click t h e MATLAB
icon on you r deskt op.

(Lin u x ) E n t er t h e ma t l a b com m a n d.

I’m n ew t o MATLAB a n d St a r t by r ea din g Ch a pt er s 1 t h r ou gh 6 of L earn in g M AT L AB .


wa n t t o lea r n it qu ickly. Th e m ost im por t a n t t h in gs t o lea r n a r e h ow t o en t er m a t r ices,
h ow t o u se t h e : (colon ) oper a t or , a n d h ow t o in voke fu n ct ion s.
You will a lso get a br ief over view of gr a ph ics a n d pr ogr a m m in g
in MATLAB. Aft er you m a st er t h e ba sics, you ca n a ccess t h e r est
of t h e docu m en t a t ion t h r ou gh t h e on lin e h elp fa cilit y (H elp).

I wa n t t o look a t som e Th er e a r e n u m er ou s dem on st r a t ion s in clu ded wit h MATLAB.


sa m ples of wh a t you ca n do You ca n see t h e dem os by select in g D e m o s fr om t h e He lp m en u .
wit h MATLAB. (Lin u x u ser s t ype d e mo a t t h e MATLAB pr om pt .) Th er e a r e
dem os in m a t h em a t ics, gr a ph ics, visu a liza t ion , a n d m u ch m or e.
You a lso will fin d a la r ge select ion of dem os a t
www. ma t h wo r k s . c o m/ d e mo s .

1-6
Finding Reference Information

Finding Reference Informa tion

W ha t I W a nt W ha t I Should Do

I wa n t t o kn ow h ow t o u se a Use t h e on lin e h elp fa cilit y (H elp). To a ccess H elp, u se t h e


specific fu n ct ion . com m a n d h e l p b r o ws e r or u se t h e He lp m en u . Th e MATLAB
F u n ct ion Refer en ce is a lso a va ila ble fr om H elp in P DF for m a t
(u n der P rin ta ble D o c u m e n ta tio n ) if you wa n t t o pr in t ou t a n y
of t h e fu n ct ion descr ipt ion s in h igh -qu a lit y for m . N o te : You r
Ma t h Wor ks docu m en t a t ion CD m u st be in you r CD-ROM dr ive
t o a ccess H elp.

I wa n t t o fin d a fu n ct ion for Th er e a r e sever a l ch oices:


a specific pu r pose bu t I don ’t
kn ow it s n a m e. • See “MATLAB Qu ick Refer en ce” in t h is book for a list of
MATLAB fu n ct ion s.
• F r om H elp, per u se t h e MATLAB fu n ct ion s by Ca t egor y or
Alph a bet ica lly.
• Use l o o k f o r (e.g., l o o k f o r i n v e r s e ) fr om t h e com m a n d lin e.
• Use In dex or Sea r ch fr om H elp.

I wa n t t o lea r n a bou t a Use H elp t o loca t e t h e a ppr opr ia t e sect ion s in Usin g M AT L AB .
specific t opic like spa r se
m a t r ices, or din a r y
differ en t ia l equ a t ion s, or cell
a r r a ys.

I wa n t t o kn ow wh a t Use H elp t o see t h e F u n ct ion Refer en ce by Ca t egor y, or see


fu n ct ion s a r e a va ila ble in a Appen dix A, “MATLAB Qu ick Refer en ce,” in t h is book for a list
gen er a l a r ea . of MATLAB fu n ct ion s. H elp pr ovides a ccess t o t h e r efer en ce
pa ges for t h e h u n dr eds of fu n ct ion s in clu ded wit h MATLAB.

I wa n t t o lea r n a bou t t h e See Ch a pt er 7, “Sym bolic Ma t h Toolbox,” a n d Appen dix B,


Sym bolic Ma t h Toolbox. “Sym bolic Ma t h Toolbox Qu ick Refer en ce,” in t h is book. For
com plet e descr ipt ion s of t h e Sym bolic Ma t h Toolbox fu n ct ion s,
u se H elp a n d select Re fe re n c e fr om S y m bo lic Ma th To o lbo x .

1-7
1 Introduction

Troubleshooting a nd O ther Resources

W ha t I W a nt W ha t I Should Do

I h a ve a MATLAB specific Visit t h e Tech n ica l Su ppor t sect ion


pr oblem I wa n t h elp wit h . (www. ma t h wo r k s . c o m/ s u p p o r t ) of t h e Ma t h Wor ks Web sit e a n d
sea r ch t h e Kn owledge Ba se of pr oblem solu t ion s.

I wa n t t o r epor t a bu g or Use H elp or sen d e-m a il t o b u g s @ma t h wo r k s . c o m or


m a ke a su ggest ion . s u g g e s t @ma t h wo r k s . c o m.

Docum enta tion Libra r y


You r St u den t Ver sion of MATLAB & Sim u lin k con t a in s m u ch m or e
docu m en t a t ion t h a n t h e t wo pr in t ed books, L earn in g M AT L AB a n d L earn in g
S im u lin k . On you r CD is a per son a l r efer en ce libr a r y of ever y book a n d
r efer en ce pa ge dist r ibu t ed by Th e Ma t h Wor ks. Access t h is docu m en t a t ion
libr a r y fr om H elp.

N ote E ven t h ou gh you h a ve t h e docu m en t a t ion set for t h e Ma t h Wor ks fa m ily


of pr odu ct s, n ot ever y pr odu ct is a va ila ble for t h e St u den t Ver sion of MATLAB
& Sim u lin k. For a n u p-t o-da t e list of a va ila ble pr odu ct s, visit t h e Ma t h Wor ks
St or e. At t h e st or e you ca n a lso pu r ch a se pr in t ed m a n u a ls for t h e MATLAB
fa m ily of pr odu ct s.

Accessing the Online Documentation


Access t h e on lin e docu m en t a t ion (H elp) dir ect ly fr om you r pr odu ct CD. (Lin u x
u ser s sh ou ld r efer t o Ch a pt er 2, “In st a lla t ion ,” for specific in for m a t ion on
con figu r in g a n d a ccessin g t h e on lin e H elp fr om t h e CD.)

1 P la ce t h e CD in you r CD-ROM dr ive.

2 Select F u ll P ro d u c t F a m ily He lp fr om t h e He lp m en u .

H elp a ppea r s in a sepa r a t e win dow.

1-8
Troubleshooting and Other Resources

Tutorialsandreferencefor
MATLAB

Tutorialsandreferencefor
Simulink

Tutorialsandreferencefor
Stateflow

Tutorialsandreferencefor
Symbolic Math Toolbox

N ote Wh en you st a r t MATLAB for t h e fir st t im e, t h e H elp Na viga t or


displa ys en t r ies for a ddit ion a l pr odu ct s. To lea r n h ow t o ch a n ge t h e displa yed
pr odu ct list , see t h e “P r odu ct F ilt er ” on pa ge 3-10.

1-9
1 Introduction

M a thW ork s W eb Site


Use you r br owser t o visit t h e Ma t h Wor ks Web sit e, www. ma t h wo r k s . c o m. You ’ll
fin d lot s of in for m a t ion a bou t Ma t h Wor ks pr odu ct s a n d h ow t h ey a r e u sed in
edu ca t ion a n d in du st r y, pr odu ct dem os, a n d MATLAB ba sed books. F r om t h e
Web sit e you will a lso be a ble t o a ccess ou r t ech n ica l su ppor t r esou r ces, view a
libr a r y of u ser a n d com pa n y su pplied M-files, a n d get in for m a t ion a bou t
pr odu ct s a n d u pcom in g even t s.

M a thW ork s Educa tion W eb Site


Th is edu ca t ion -specific Web sit e, www. ma t h wo r k s . c o m/ e d u c a t i o n , con t a in s
m a n y r esou r ces for va r iou s br a n ch es of en gin eer in g, m a t h em a t ics, a n d scien ce.
Ma n y of t h ese in clu de t ea ch in g exa m ples, books, a n d ot h er r ela t ed pr odu ct s.
You will a lso fin d a com pr eh en sive list of lin ks t o Web sit es wh er e MATLAB is
u sed for t ea ch in g a n d r esea r ch a t u n iver sit ies.

M ATLAB Rela ted Book s


H u n dr eds of MATLAB r ela t ed books a r e a va ila ble fr om m a n y differ en t
pu blish er s. An u p-t o-da t e list is a va ila ble a t www. ma t h wo r k s . c o m/ s u p p o r t /
books .

M a thW ork s Store


Th e Ma t h Wor ks St or e (www. ma t h wo r k s . c o m/ s t o r e ) gives you a n ea sy wa y t o
pu r ch a se a dd-on pr odu ct s a n d docu m en t a t ion .

Usenet N ew sgroup
If you h a ve a ccess t o Usen et n ewsgr ou ps, you ca n join t h e a ct ive com m u n it y of
pa r t icipa n t s in t h e MATLAB specific gr ou p, c o mp . s o f t - s y s . ma t l a b . Th is
for u m is a ga t h er in g of pr ofession a ls a n d st u den t s wh o u se MATLAB a n d h a ve
qu est ion s or com m en t s a bou t it a n d it s a ssocia t ed pr odu ct s. Th is is a gr ea t
r esou r ce for posin g qu est ion s a n d a n swer in g t h ose of ot h er s. Ma t h Wor ks st a ff
a lso pa r t icipa t es a ct ively in t h is n ewsgr ou p.

M a thW ork s Know ledge Ba se


You ca n a ccess t h e Ma t h Wor ks Kn owledge Ba se fr om t h e Su ppor t lin k on ou r
Web sit e. Ou r Tech n ica l Su ppor t gr ou p m a in t a in s t h is da t a ba se of fr equ en t ly
a sked qu est ion s (F AQ). You ca n per u se t h e Kn owledge Ba se t o qu ickly loca t e

1-10
Troubleshooting and Other Resources

r eleva n t da t a . You will fin d n u m er ou s exa m ples on gr a ph ics, m a t h em a t ics,


AP I, Sim u lin k, a n d ot h er s. You ca n a n swer m a n y of you r qu est ion s by spen din g
a few m in u t es wit h t h is a r ou n d-t h e-clock r esou r ce.

Technica l Suppor t
Th e Ma t h Wor ks does n ot pr ovide t eleph on e t ech n ica l su ppor t t o u ser s of t h e
St u den t Ver sion of MATLAB & Sim u lin k. Th er e a r e n u m er ou s ot h er veh icles
of t ech n ica l su ppor t t h a t you ca n u se. Th e Addit ion a l Sou r ces of In for m a t ion
sect ion in t h e CD h older iden t ifies t h e wa ys t o obt a in su ppor t .
Regist er ed u ser s of t h e St u den t Ver sion of MATLAB & Sim u lin k ca n u se ou r
elect r on ic t ech n ica l su ppor t ser vices t o a n swer pr odu ct qu est ion s. Visit ou r
Tech n ica l Su ppor t Web sit e a t www. ma t h wo r k s . c o m/ s u p p o r t .
Aft er ch eckin g t h e a va ila ble Ma t h Wor ks sou r ces for h elp, if you st ill ca n n ot
r esolve you r pr oblem , you sh ou ld con t a ct you r in st r u ct or . You r in st r u ct or
sh ou ld be a ble t o h elp you , bu t if n ot , t h er e is t eleph on e t ech n ica l su ppor t for
r egist er ed in st r u ct or s wh o h a ve a dopt ed t h e St u den t Ver sion of MATLAB &
Sim u lin k in t h eir cou r ses.

Product Registra tion


Visit t h e Ma t h Wor ks Web sit e (www. ma t h wo r k s . c o m/ s t u d e n t ) a n d r egist er
you r St u den t Ver sion .

1-11
1 Introduction

About M ATLAB a nd Simulink


W ha t Is M ATLAB?
MATLAB is a h igh -per for m a n ce la n gu a ge for t ech n ica l com pu t in g. It
in t egr a t es com pu t a t ion , visu a liza t ion , a n d pr ogr a m m in g in a n ea sy-t o-u se
en vir on m en t wh er e pr oblem s a n d solu t ion s a r e expr essed in fa m ilia r
m a t h em a t ica l n ot a t ion . Typica l u ses in clu de:

• Ma t h a n d com pu t a t ion
• Algor it h m developm en t
• Modelin g, sim u la t ion , a n d pr ot ot ypin g
• Da t a a n a lysis, explor a t ion , a n d visu a liza t ion
• Scien t ific a n d en gin eer in g gr a ph ics
• Applica t ion developm en t , in clu din g gr a ph ica l u ser in t er fa ce bu ildin g

MATLAB is a n in t er a ct ive syst em wh ose ba sic da t a elem en t is a n a r r a y t h a t


does n ot r equ ir e dim en sion in g. Th is a llows you t o solve m a n y t ech n ica l
com pu t in g pr oblem s, especia lly t h ose wit h m a t r ix a n d vect or for m u la t ion s, in
a fr a ct ion of t h e t im e it wou ld t a ke t o wr it e a pr ogr a m in a sca la r n on in t er a ct ive
la n gu a ge su ch a s C or F or t r a n .
Th e n a m e MATLAB st a n ds for m atrix laboratory. MATLAB wa s or igin a lly
wr it t en t o pr ovide ea sy a ccess t o m a t r ix soft wa r e developed by t h e LINP ACK
a n d E ISP ACK pr oject s. Toda y, MATLAB u ses soft wa r e developed by t h e
LAP ACK a n d ARP ACK pr oject s, wh ich t oget h er r epr esen t t h e st a t e-of-t h e-a r t
in soft wa r e for m a t r ix com pu t a t ion .
MATLAB h a s evolved over a per iod of yea r s wit h in pu t fr om m a n y u ser s. In
u n iver sit y en vir on m en t s, it is t h e st a n da r d in st r u ct ion a l t ool for in t r odu ct or y
a n d a dva n ced cou r ses in m a t h em a t ics, en gin eer in g, a n d scien ce. In in du st r y,
MATLAB is t h e t ool of ch oice for h igh -pr odu ct ivit y r esea r ch , developm en t , a n d
a n a lysis.

Toolboxes
MATLAB fea t u r es a fa m ily of a pplica t ion -specific solu t ion s ca lled toolboxes.
Ver y im por t a n t t o m ost u ser s of MATLAB, t oolboxes a llow you t o learn a n d
apply specia lized t ech n ology. Toolboxes a r e com pr eh en sive collect ion s of
MATLAB fu n ct ion s (M-files) t h a t ext en d t h e MATLAB en vir on m en t t o solve

1-12
About MATLAB and Simulink

pa r t icu la r cla sses of pr oblem s. Ar ea s in wh ich t oolboxes a r e a va ila ble in clu de


sign a l pr ocessin g, con t r ol syst em s, n eu r a l n et wor ks, fu zzy logic, wa velet s,
sim u la t ion , a n d m a n y ot h er s.

The MATLAB System


Th e MATLAB syst em con sist s of five m a in pa r t s:

Development Environment. Th is is t h e set of t ools a n d fa cilit ies t h a t h elp you u se


MATLAB fu n ct ion s a n d files. Ma n y of t h ese t ools a r e gr a ph ica l u ser in t er fa ces.
It in clu des t h e MATLAB deskt op a n d Com m a n d Win dow, a com m a n d h ist or y,
a n d br owser s for viewin g h elp, t h e wor kspa ce, files, a n d t h e sea r ch pa t h .

The MATLAB Mathematical Function Library. Th is is a va st collect ion of com pu t a t ion a l


a lgor it h m s r a n gin g fr om elem en t a r y fu n ct ion s like su m , sin e, cosin e, a n d
com plex a r it h m et ic, t o m or e soph ist ica t ed fu n ct ion s like m a t r ix in ver se, m a t r ix
eigen va lu es, Bessel fu n ct ion s, a n d fa st F ou r ier t r a n sfor m s.

The MATLAB language. Th is is a h igh -level m a t r ix/a r r a y la n gu a ge wit h con t r ol


flow st a t em en t s, fu n ct ion s, da t a st r u ct u r es, in pu t /ou t pu t , a n d object -or ien t ed
pr ogr a m m in g fea t u r es. It a llows bot h “pr ogr a m m in g in t h e sm a ll” t o r a pidly
cr ea t e qu ick a n d dir t y t h r ow-a wa y pr ogr a m s, a n d “pr ogr a m m in g in t h e la r ge”
t o cr ea t e com plet e la r ge a n d com plex a pplica t ion pr ogr a m s.

Handle Graphics® . Th is is t h e MATLAB gr a ph ics syst em . It in clu des h igh -level


com m a n ds for t wo-dim en sion a l a n d t h r ee-dim en sion a l da t a visu a liza t ion ,
im a ge pr ocessin g, a n im a t ion , a n d pr esen t a t ion gr a ph ics. It a lso in clu des
low-level com m a n ds t h a t a llow you t o fu lly cu st om ize t h e a ppea r a n ce of
gr a ph ics a s well a s t o bu ild com plet e gr a ph ica l u ser in t er fa ces on you r
MATLAB a pplica t ion s.

The MATLAB Application Program Interface (API). Th is is a libr a r y t h a t a llows you t o


wr it e C a n d F or t r a n pr ogr a m s t h a t in t er a ct wit h MATLAB. It in clu de fa cilit ies
for ca llin g r ou t in es fr om MATLAB (dyn a m ic lin kin g), ca llin g MATLAB a s a
com pu t a t ion a l en gin e, a n d for r ea din g a n d wr it in g MAT-files.

1-13
1 Introduction

W ha t Is Sim ulink ?
Sim u lin k, a com pa n ion pr ogr a m t o MATLAB, is a n in t er a ct ive syst em for
sim u la t in g n on lin ea r dyn a m ic syst em s. It is a gr a ph ica l m ou se-dr iven pr ogr a m
t h a t a llows you t o m odel a syst em by dr a win g a block dia gr a m on t h e scr een
a n d m a n ipu la t in g it dyn a m ica lly. It ca n wor k wit h lin ea r , n on lin ea r ,
con t in u ou s-t im e, discr et e-t im e, m u lt ir a t e, a n d h ybr id syst em s.
B lock sets a r e a dd-on s t o Sim u lin k t h a t pr ovide a ddit ion a l libr a r ies of blocks for
specia lized a pplica t ion s like com m u n ica t ion s, sign a l pr ocessin g, a n d power
syst em s.
R eal-T im e Work sh op ® is a pr ogr a m t h a t a llows you t o gen er a t e C code fr om
you r block dia gr a m s a n d t o r u n it on a va r iet y of r ea l-t im e syst em s.

What Is Stateflow?
St a t eflow is a n in t er a ct ive design t ool for m odelin g a n d sim u la t in g com plex
r ea ct ive syst em s. Tigh t ly in t egr a t ed wit h Sim u lin k a n d MATLAB, St a t eflow
pr ovides Sim u lin k u ser s wit h a n elega n t solu t ion for design in g em bedded
syst em s by givin g t h em a n efficien t wa y t o in cor por a t e com plex con t r ol a n d
su per visor y logic wit h in t h eir Sim u lin k m odels.
Wit h St a t eflow, you ca n qu ickly develop gr a ph ica l m odels of even t -dr iven
syst em s u sin g fin it e st a t e m a ch in e t h eor y, st a t ech a r t for m a lism s, a n d flow
dia gr a m n ot a t ion . Toget h er , St a t eflow a n d Sim u lin k ser ve a s a n execu t a ble
specifica t ion a n d vir t u a l pr ot ot ype of you r syst em design .

N ote You r St u den t Ver sion of MATLAB & Sim u lin k in clu des a
com pr eh en sive dem o ver sion of St a t eflow.

1-14
2

In st a lla t ion
In s ta llin g o n Win d o w s . . . . . . . . . . . . . . . 2-2

In s ta llin g o n Lin u x . . . . . . . . . . . . . . . . . 2-7


2 Installation

Insta lling on W indow s


Sy stem Requirem ents

N ote For t h e m ost u p-t o-da t e in for m a t ion a bou t syst em r equ ir em en t s, see
t h e syst em r equ ir em en t s pa ge, a va ila ble in t h e P r odu ct s a r ea a t t h e
Ma t h Wor ks Web sit e (www. ma t h wo r k s . c o m).

MATLAB and Simulink


• In t el-ba sed P en t iu m , P en t iu m P r o, P en t iu m II, P en t iu m III, or AMD At h lon
per son a l com pu t er
• Micr osoft Win dows 95, Win dows 98, Win dows 2000, Win dows Me, or
Win dows NT 4.0 (wit h Ser vice P a ck 5 or 6a )
• CD-ROM dr ive for in st a lla t ion , pr ogr a m execu t ion , a n d on lin e
docu m en t a t ion
• Disk spa ce va r ies depen din g on size of pa r t it ion . Th e Ma t h Wor ks In st a ller
will in for m you of t h e disk spa ce r equ ir em en t for you r pa r t icu la r pa r t it ion .
• 64 MB RAM m in im u m ; 128 MB RAM st r on gly r ecom m en ded
• 8-bit gr a ph ics a da pt er a n d displa y (for 256 sim u lt a n eou s color s)
• Net sca pe Na viga t or 4.0 or h igh er or Micr osoft In t er n et E xplor er 4.0 or
h igh er is r equ ir ed.

Ot h er r ecom m en ded it em s in clu de:

• Micr osoft Win dows su ppor t ed gr a ph ics a cceler a t or ca r d


• Micr osoft Win dows su ppor t ed pr in t er
• Micr osoft Win dows su ppor t ed sou n d ca r d
• Micr osoft Wor d 7.0 (Office 95), or 8.0 (Office 97), or Office 2000 is r equ ir ed t o
r u n t h e MATLAB Not ebook.

Adobe Acr oba t Rea der is r equ ir ed t o view a n d pr in t t h e MATLAB on lin e


docu m en t a t ion t h a t is in P DF for m a t .

2-2
Installing on W indows

MEX-Files
ME X-files a r e dyn a m ica lly lin ked su br ou t in es t h a t MATLAB ca n
a u t om a t ica lly loa d a n d execu t e. Th ey pr ovide a m ech a n ism by wh ich you ca n
ca ll you r own C a n d F or t r a n su br ou t in es fr om MATLAB a s if t h ey wer e bu ilt -in
fu n ct ion s.

For M ore Informa tion “E xt er n a l In t er fa ces/AP I” pr ovides in for m a t ion on


h ow t o wr it e ME X-files. “E xt er n a l In t er fa ces/AP I Refer en ce” descr ibes t h e
collect ion of AP I fu n ct ion s. Bot h of t h ese a r e a va ila ble fr om H elp.

If you pla n t o bu ild you r own ME X-files, on e of t h e followin g is r equ ir ed:


• Bor la n d C/C++ ver sion 5.0 or 5.02
• Bor la n d C++Bu ilder ver sion 3.0, 4.0, or 5.0
• Com pa q Visu a l F or t r a n ver sion 6.1
• DIGITAL Visu a l F or t r a n ver sion 5.0
• Lcc C ver sion 2.4 (in clu ded wit h MATLAB)
• Micr osoft Visu a l C/C++ ver sion 5.0 or 6.0
• Wa t com C/C++ ver sion 10.6 or 11

N ote For a n u p-t o-da t e list of a ll t h e com piler s su ppor t ed by MATLAB, see
t h e Ma t h Wor ks Tech n ica l Su ppor t Depa r t m en t ’s Tech n ica l Not es a t :

h t t p : / / www. ma t h wo r k s . c o m/ s u p p o r t / t e c h - n o t e s / v 5 / 1 6 0 0 / 1 6 0 1 . s h t ml

2-3
2 Installation

Insta lling M ATLAB


Th is list su m m a r izes t h e st eps in t h e st a n da r d in st a lla t ion pr ocedu r e. You ca n
per for m t h e in st a lla t ion by sim ply followin g t h e in st r u ct ion s in t h e dia log
boxes pr esen t ed by t h e in st a lla t ion pr ogr a m ; it wa lks you t h r ou gh t h is pr ocess.

1 Tu r n off a n y vir u s pr ot ect ion soft wa r e you h a ve r u n n in g.

2 E xit a n y exist in g copies of MATLAB you h a ve r u n n in g.

3 In ser t t h e Ma t h Wor ks pr odu ct CD in t o you r CD-ROM dr ive. Th e


in st a lla t ion pr ogr a m st a r t s a u t om a t ica lly wh en t h e CD-ROM dr ive is r ea dy.
You ca n a lso r u n s e t u p . e x e fr om t h e pr odu ct CD.

4 In st a ll t h e Micr osoft J a va Vir t u a l Ma ch in e (J VM), if pr om pt ed. Th e


Ma t h Wor ks In st a ller r equ ir es t h e Micr osoft J VM.

N o te : Th e J a va in st a lla t ion r equ ir es a syst em r eboot .

5 View t h e We lc o m e scr een a n d r eview t h e Soft wa r e Licen se Agr eem en t .

6 Review t h e St u den t Use P olicy.

7 E n t er you r n a m e a n d sch ool n a m e.

8 To in st a ll t h e com plet e set of soft wa r e (MATLAB, Sim u lin k, a n d t h e


Sym bolic Ma t h Toolbox), m a ke su r e a ll of t h e com pon en t s a r e select ed in t h e
P ro d u c t Lis t dia log box.

9 Specify t h e dest in a t ion dir ect or y, t h a t is, t h e dir ect or y wh er e you wa n t t o


sa ve t h e files on you r h a r d dr ive. To ch a n ge dir ect or ies, u se t h e B ro w s e
bu t t on .

10 Wh en t h e in st a lla t ion is com plet e, ver ify t h e in st a lla t ion by st a r t in g


MATLAB a n d r u n n in g on e of t h e dem o pr ogr a m s. To st a r t MATLAB,
dou ble-click on t h e MATLAB icon t h a t t h e in st a ller cr ea t es on you r deskt op.
To r u n t h e dem o pr ogr a m s, select D e m o s fr om He lp .

N ote Th e Ma t h Wor ks docu m en t a t ion CD m u st be in you r CD-ROM dr ive t o


st a r t MATLAB.

2-4
Installing on W indows

11 Cu st om ize a n y MATLAB en vir on m en t opt ion s, if desir ed. F or exa m ple, t o


in clu de defa u lt defin it ion s or a n y MATLAB expr ession s t h a t you wa n t
execu t ed ever y t im e MATLAB is in voked, cr ea t e a file n a m ed s t a r t u p . m in
t h e $ MATLAB\ t o o l b o x \ l o c a l dir ect or y. MATLAB execu t es t h is file ea ch
t im e MATLAB is in voked.

12 P er for m a n y a ddit ion a l con figu r a t ion by t ypin g t h e a ppr opr ia t e com m a n d a t
t h e MATLAB com m a n d pr om pt . F or exa m ple, t o con figu r e t h e MATLAB
Not ebook, t ype n o t e b o o k - s e t u p . To con figu r e a com piler t o wor k wit h t h e
MATLAB Applica t ion P r ogr a m In t er fa ce, t ype me x - s e t u p .

For M ore Informa tion Th e M AT L AB In stallation Gu id e for PC pr ovides


a ddit ion a l in st a lla t ion in for m a t ion . Th is m a n u a l is a va ila ble fr om H elp.

Insta lling Additiona l Toolbox es


To pu r ch a se a ddit ion a l t oolboxes, visit t h e Ma t h Wor ks St or e a t
(www. ma t h wo r k s . c o m/ s t o r e ). On ce you pu r ch a se a t oolbox, it is down loa ded t o
you r com pu t er .
Wh en you down loa d a t oolbox, you r eceive a n in st a lla t ion pr ogr a m for t h e
t oolbox. To in st a ll t h e t oolbox, r u n t h e in st a lla t ion pr ogr a m by dou ble-clickin g
on it s icon . Aft er you su ccessfu lly in st a ll t h e t oolbox, a ll of it s fu n ct ion a lit y will
be a va ila ble t o you wh en you st a r t MATLAB.

N ote Som e t oolboxes h a ve Re a d Me files a ssocia t ed wit h t h em . Wh en you


down loa d t h e t oolbox, ch eck t o see if t h er e is a Re a d Me file. Th ese files con t a in
im por t a n t in for m a t ion a bou t t h e t oolbox a n d possibly in st a lla t ion a n d
con figu r a t ion n ot es. To view t h e Re a d Me file for a t oolbox, u se t h e wh a t s n e w
com m a n d.

2-5
2 Installation

Accessing the O nline Docum enta tion (H elp)


Access t h e on lin e docu m en t a t ion (H elp) dir ect ly fr om you r docu m en t a t ion CD.

1 P la ce t h e docu m en t a t ion CD in you r CD-ROM dr ive.

2 Select F u ll P ro d u c t F a m ily He lp fr om t h e He lp m en u in t h e MATLAB


Com m a n d Win dow. You ca n a lso t ype h e l p b r o ws e r a t t h e MATLAB pr om pt .

Th e H elp br owser a ppea r s.

2-6
Installing on Linux

Insta lling on Linux

N ote Th e St u den t Ver sion of MATLAB & Sim u lin k for t h e Lin u x pla t for m is
on ly a va ila ble in t h e US a n d Ca n a da .

Sy stem Requirem ents

N ote For t h e m ost u p-t o-da t e in for m a t ion a bou t syst em r equ ir em en t s, see
t h e syst em r equ ir em en t s pa ge, a va ila ble in t h e pr odu ct s a r ea a t t h e
Ma t h Wor ks Web sit e (www. ma t h wo r k s . c o m).

MATLAB and Simulink


• In t el-ba sed P en t iu m , P en t iu m P r o, P en t iu m II, P en t iu m III, or AMD At h lon
per son a l com pu t er
• Lin u x 2.2.x ker n el
- glibc 2.1.x (2.1.2 or h igh er r ecom m en ded)
- gcc 2.95.2 (gcc, g++, g77)
- xF r ee86 3.3.x (3.3.6 or h igh er r ecom m en ded)
• X Win dows (X11R6)
• 110 MB fr ee disk spa ce for MATLAB, Sim u lin k, a n d Sym bolic Ma t h Toolbox
• 64 MB m em or y, a ddit ion a l m em or y st r on gly r ecom m en ded
• 64 MB swa p spa ce
• CD-ROM dr ive for in st a lla t ion a n d on lin e docu m en t a t ion
• 8-bit gr a ph ics a da pt er a n d displa y (for 256 sim u lt a n eou s color s)
• Net sca pe Na viga t or 4.0 or h igh er is r equ ir ed.

Adobe Acr oba t Rea der is r equ ir ed t o view a n d pr in t t h e MATLAB on lin e


docu m en t a t ion t h a t is in P DF for m a t .

2-7
2 Installation

MEX-Files
ME X-files a r e dyn a m ica lly lin ked su br ou t in es t h a t MATLAB ca n
a u t om a t ica lly loa d a n d execu t e. Th ey pr ovide a m ech a n ism by wh ich you ca n
ca ll you r own C a n d F or t r a n su br ou t in es fr om MATLAB a s if t h ey wer e bu ilt -in
fu n ct ion s.

For M ore Informa tion “E xt er n a l In t er fa ces/AP I” pr ovides in for m a t ion on


h ow t o wr it e ME X-files. “E xt er n a l In t er fa ces/AP I Refer en ce” descr ibes t h e
collect ion of AP I fu n ct ion s. Bot h of t h ese a r e a va ila ble fr om H elp.

If you pla n t o bu ild you r own ME X-files, you n eed a n ANSIC C com piler (e.g.,
t h e GNU C com piler , g c c ).

N ote For a n u p-t o-da t e list of a ll t h e com piler s su ppor t ed by MATLAB, see
t h e Ma t h Wor ks Tech n ica l Su ppor t Depa r t m en t ’s Tech n ica l Not es a t :

h t t p : / / www. ma t h wo r k s . c o m/ s u p p o r t / t e c h - n o t e s / v 5 / 1 6 0 0 / 1 6 0 1 . s h t ml

Insta lling M ATLAB


Th e followin g in st r u ct ion s descr ibe h ow t o in st a ll t h e St u den t Ver sion of
MATLAB & Sim u lin k on you r com pu t er .

N ote It is r ecom m en ded t h a t you log in a s r o o t t o per for m you r in st a lla t ion .

Installing the Software


To in st a ll t h e St u den t Ver sion :

1 If you r CD-ROM dr ive is n ot a ccessible t o you r oper a t in g syst em , you will


n eed t o cr ea t e a dir ect or y t o be t h e m ou n t poin t for it .
mk d i r / c d r o m

2 P la ce t h e Ma t h Wor ks pr odu ct CD in t o t h e CD-ROM dr ive.

2-8
Installing on Linux

3 E xecu t e t h e com m a n d t o m ou n t t h e CD-ROM dr ive on you r syst em . F or


exa m ple,
# mo u n t - t i s o 9 6 6 0 / d e v / c d r o m / c d r o m

sh ou ld wor k on m ost syst em s. If you r / e t c / f s t a b file h a s a lin e sim ila r t o


/ de v/ c dr om / c dr om i s o9660 noa ut o, r o, us e r , e xe c 0 0

t h en n on r oot u ser s ca n m ou n t t h e CD-ROM u sin g t h e sim plified com m a n d


$ mo u n t / c d r o m

N ote If t h e e x e c opt ion is m issin g (a s it oft en is by defa u lt , for secu r it y


r ea son s), you will r eceive a “Per m ission den ied” er r or wh en a t t em pt in g t o r u n
t h e in st a ll scr ipt . To r em edy t h is, eit h er u se t h e fu ll m ou n t com m a n d sh own
a bove (a s r o o t ) or a dd t h e e x e c opt ion t o t h e file / e t c / f s t a b .

4 Move t o t h e in st a lla t ion loca t ion u sin g t h e c d com m a n d. F or exa m ple, if you
a r e goin g t o in st a ll in t o t h e loca t ion / u s r / l o c a l / ma t l a b 6 , u se t h e
com m a n ds
c d / us r / l oc a l
mk d i r ma t l a b 6
c d ma t l a b 6

Su bsequ en t in st r u ct ion s in t h is sect ion r efer t o t h is dir ect or y a s $ MATLAB.

5 Ru n t h e CD in st a ll scr ipt .

/ c d r o m/ i n s t a l l _ g l n x 8 6 . s h

Th e welcom e scr een a ppea r s. Select OK t o pr oceed wit h t h e in st a lla t ion .

N ote If you n eed a ddit ion a l h elp on a n y st ep du r in g t h is in st a lla t ion pr ocess,


click t h e He lp bu t t on a t t h e bot t om of t h e dia log box.

2-9
2 Installation

6 Accept or r eject t h e soft wa r e licen sin g a gr eem en t displa yed. If you a ccept
t h e t er m s of t h e a gr eem en t , you m a y pr oceed wit h t h e in st a lla t ion .

7 Th e MATLAB Ro o t D ire c to ry scr een is displa yed. Select OK if t h e


pa t h n a m e for t h e MATLAB r oot dir ect or y is cor r ect ; ot h er wise, ch a n ge it t o
t h e desir ed loca t ion .

8 Th e syst em displa ys you r licen se file. P r ess OK .

2-10
Installing on Linux

9 Th e in st a lla t ion pr ogr a m displa ys t h e P ro d u c t In s ta lla tio n Op tio n s


scr een , wh ich is sim ila r t o t h is.

Th e pr odu ct s you a r e licen sed t o in st a ll a r e list ed in t h e Ite m s to in s ta ll list


box. Th e r igh t list box displa ys t h e pr odu ct s t h a t you do n ot wa n t t o in st a ll.
To in st a ll t h e com plet e St u den t Ver sion of MATLAB & Sim u lin k, you m u st
in st a ll a ll t h e pr odu ct s for wh ich you a r e licen sed (MATLAB, MATLAB
Toolbox, MATLAB Ker n el, Sim u lin k, a n d Sym bolic Ma t h Toolbox). Select
OK .

2-11
2 Installation

10 Th e in st a lla t ion pr ogr a m displa ys t h e In s ta lla tio n D a ta scr een .

Specify t h e dir ect or y loca t ion in you r file syst em for sym bolic lin ks t o t h e
ma t l a b a n d me x scr ipt s. Ch oose a dir ect or y su ch a s / u s r / l o c a l / b i n . You
m u st be logged in a s r o o t t o do t h is.

Select OK t o con t in u e.

11 Th e B e g in P ro d u c t In s ta lla tio n scr een is displa yed. Select OK t o st a r t t h e


in st a lla t ion . Aft er t h e in st a lla t ion is com plet e, t h e P ro d u c t In s ta lla tio n
Co m p le te scr een is displa yed, a ssu m in g you r in st a lla t ion is su ccessfu l.
Select Ex it t o exit fr om t h e set u p pr ogr a m .

12 You m u st edit t h e d o c o p t . m M-file loca t ed in t h e $ MATLAB/ t o o l b o x / l o c a l


dir ect or y t o specify t h e pa t h t o t h e on lin e docu m en t a t ion (H elp). F or
exa m ple, if / c d r o m is t h e pa t h t o you r CD-ROM dr ive, t h en you wou ld u se
/ c d r o m/ h e l p . To set t h e pa t h u sin g t h is exa m ple, ch a n ge t h e lin es in t h e
i f i s u n i x block in t h e d o c o p t . m file t o
i f i s uni x % UNI X
% d o c c md = ' ' ;
% opt i ons = ' ' ;
d o c p a t h = ' / c d r o m/ h e l p ' ;

Th e d o c o p t . m file a lso a llows you t o specify a n a lt er n a t ive Web br owser or


a ddit ion a l in it ia l br owser opt ion s. It is con figu r ed for Net sca pe Na viga t or .

2-12
Installing on Linux

13 If desir ed, cu st om ize a n y MATLAB en vir on m en t opt ion s. F or exa m ple, t o


in clu de defa u lt defin it ion s or a n y MATLAB expr ession s t h a t you wa n t
execu t ed ever y t im e MATLAB is in voked, cr ea t e a file n a m ed s t a r t u p . m in
t h e $ MATLAB/ t o o l b o x / l o c a l dir ect or y. MATLAB execu t es t h is file ea ch
t im e MATLAB is in voked.

14 St a r t MATLAB by en t er in g t h e ma t l a b com m a n d. If you did n ot set u p


sym bolic lin ks in a dir ect or y on you r pa t h , t ype $ MATLAB/ b i n / ma t l a b .

Post Insta lla tion Procedures


Successful Installation
If you wa n t t o u se t h e MATLAB Applica t ion P r ogr a m In t er fa ce, you m u st
con figu r e t h e me x scr ipt t o wor k wit h you r com piler . Also, som e t oolboxes m a y
r equ ir e som e a ddit ion a l con figu r a t ion . F or m or e in for m a t ion , see “In st a llin g
Addit ion a l Toolboxes” la t er in t h is sect ion .

Unsuccessful Installation
If MATLAB does n ot execu t e cor r ect ly a ft er in st a lla t ion :

1 Ch eck t h e “R12 Relea se Not es” for t h e la t est in for m a t ion con cer n in g
in st a lla t ion . Th is docu m en t is a ccessible fr om H elp.

2 Repea t t h e in st a lla t ion pr ocedu r e fr om t h e begin n in g bu t r u n t h e CD in st a ll


scr ipt u sin g t h e - t opt ion .
/ c d r o m/ i n s t a l l _ g l n x 8 6 . s h - t

For M ore Informa tion Th e M AT L AB In stallation Gu id e for UN IX pr ovides


a ddit ion a l in st a lla t ion in for m a t ion . Th is m a n u a l is a va ila ble fr om H elp.

Insta lling Additiona l Toolbox es


To pu r ch a se a ddit ion a l t oolboxes, visit t h e Ma t h Wor ks St or e a t
(www. ma t h wo r k s . c o m/ s t o r e ). On ce you pu r ch a se a t oolbox, it is down loa ded t o
you r com pu t er . Wh en you down loa d a t oolbox on Lin u x, you r eceive a t a r file (a
st a n da r d, com pr essed for m a t t ed file).

2-13
2 Installation

To in st a ll t h e t oolbox, you m u st :

1 P la ce t h e t a r file in $ MATLAB a n d u n -t a r it .

t a r - x f f i l e n a me

2 Ru n i n s t a l l .

Aft er you su ccessfu lly in st a ll t h e t oolbox, a ll of it s fu n ct ion a lit y will be


a va ila ble t o you wh en you st a r t MATLAB.

N ote Som e t oolboxes h a ve Re a d Me files a ssocia t ed wit h t h em . Wh en you


down loa d t h e t oolbox, ch eck t o see if t h er e is a Re a d Me file. Th ese files con t a in
im por t a n t in for m a t ion a bou t t h e t oolbox a n d possibly in st a lla t ion a n d
con figu r a t ion n ot es. To view t h e Re a d Me file for a t oolbox, u se t h e wh a t s n e w
com m a n d.

Accessing the O nline Docum enta tion (H elp)


Access t h e on lin e docu m en t a t ion (H elp) dir ect ly fr om you r docu m en t a t ion CD.

1 P la ce t h e docu m en t a t ion CD in you r CD-ROM dr ive a n d m ou n t it .

2 Select F u ll P ro d u c t F a m ily He lp fr om t h e He lp m en u in t h e MATLAB


Com m a n d Win dow. You ca n a lso t ype h e l p b r o ws e r a t t h e MATLAB pr om pt .

Th e H elp br owser a ppea r s.

2-14
Installing on Linux

2-15
2 Installation

2-16
3
Developm en t
E n vir on m en t
In tro d u c tio n . . . . . . . . . . . . . . . . . . . . 3-2

S ta rtin g a n d Qu ittin g MATLAB . . . . . . . . . . . 3-3

MATLAB D e s k to p . . . . . . . . . . . . . . . . . 3-4

D e s k to p To o ls . . . . . . . . . . . . . . . . . . . 3-6

Oth e r D e v e lo p m e n t En v iro n m e n t F e a tu re s . . . . . . 3-15


3 Development Environment

Introduction
Th is ch a pt er pr ovides a br ief in t r odu ct ion t o st a r t in g a n d qu it t in g MATLAB,
a n d t h e t ools a n d fu n ct ion s t h a t h elp you t o wor k wit h MATLAB va r ia bles a n d
files. F or m or e in for m a t ion a bou t t h e t opics cover ed h er e, see t h e cor r espon din g
t opics u n der “Developm en t E n vir on m en t ” in t h e MATLAB docu m en t a t ion ,
wh ich is a va ila ble on lin e.

3-2
Starting and Quitting MATLAB

Sta rting a nd Q uitting M ATLAB


Sta r ting M ATLAB
On a Micr osoft Win dows pla t for m , t o st a r t MATLAB, dou ble-click t h e
MATLAB sh or t cu t icon on you r Win dows deskt op.
On Lin u x, t o st a r t MATLAB, t ype ma t l a b a t t h e oper a t in g syst em pr om pt .

N ote On t h e Micr osoft Win dows pla t for m , t h e docu m en t a t ion CD m u st be in


you r CD-ROM dr ive t o st a r t MATLAB. On bot h pla t for m s, t h e docu m en t a t ion
CD m u st be in you r CD-ROM dr ive t o a ccess t h e on lin e docu m en t a t ion .

Aft er st a r t in g MATLAB, t h e MATLAB deskt op open s – see “MATLAB


Deskt op” on pa ge 3-4.
You ca n ch a n ge t h e dir ect or y in wh ich MATLAB st a r t s, defin e st a r t u p opt ion s
in clu din g r u n n in g a scr ipt u pon st a r t u p, a n d r edu ce st a r t u p t im e in som e
sit u a t ion s.

Q uitting M ATLAB
To en d you r MATLAB session , select Ex it MATLAB fr om t h e F ile m en u in t h e
deskt op, or t ype q u i t in t h e Com m a n d Win dow. To execu t e specified fu n ct ion s
ea ch t im e MATLAB qu it s, su ch a s sa vin g t h e wor kspa ce, you ca n cr ea t e a n d
r u n a f i n i s h . m scr ipt .

3-3
3 Development Environment

M ATLAB Desk top


Wh en you st a r t MATLAB, t h e MATLAB deskt op a ppea r s, con t a in in g t ools
(gr a ph ica l u ser in t er fa ces) for m a n a gin g files, va r ia bles, a n d a pplica t ion s
a ssocia t ed wit h MATLAB.
Th e fir st t im e MATLAB st a r t s, t h e deskt op a ppea r s a s sh own in t h e followin g
illu st r a t ion , a lt h ou gh you r La u n ch P a d m a y con t a in differ en t en t r ies.

Expand to view Get help. Enter Viewor change Click to move window Close window.
documentation, demos, and MATLAB current outside of desktop.
toolsfor your products. functions. directory.

Viewor use previously run functions. UsetabstogotoWorkspacebrowser Drag the separator bar to resize windows.
or Current Directory browser.

3-4
MATLAB Desktop

You ca n ch a n ge t h e wa y you r deskt op looks by open in g, closin g, m ovin g, a n d


r esizin g t h e t ools in it . You ca n a lso m ove t ools ou t side of t h e deskt op or r et u r n
t h em ba ck in side t h e deskt op (dockin g). All t h e deskt op t ools pr ovide com m on
fea t u r es su ch a s con t ext m en u s a n d keyboa r d sh or t cu t s.
You ca n specify cer t a in ch a r a ct er ist ics for t h e deskt op t ools by select in g
P re fe re n c e s fr om t h e F ile m en u . F or exa m ple, you ca n specify t h e fon t
ch a r a ct er ist ics for Com m a n d Win dow t ext . F or m or e in for m a t ion , click t h e
He lp bu t t on in t h e P re fe re n c e s dia log box.

3-5
3 Development Environment

Desk top Tools


Th is sect ion pr ovides a n in t r odu ct ion t o MATLAB’s deskt op t ools. You ca n a lso
u se MATLAB fu n ct ion s t o per for m m ost of t h e fea t u r es fou n d in t h e deskt op
t ools. Th e t ools a r e:

• “Com m a n d Win dow” on pa ge 3-6


• “Com m a n d H ist or y” on pa ge 3-7
• “La u n ch P a d” on pa ge 3-8
• “H elp Br owser ” on pa ge 3-8
• “Cu r r en t Dir ect or y Br owser ” on pa ge 3-11
• “Wor kspa ce Br owser ” on pa ge 3-12
• “Ar r a y E dit or ” on pa ge 3-13
• “E dit or /Debu gger ” on pa ge 3-14

Com m a nd W indow
Use t h e Co m m a n d Win d o w t o en t er va r ia bles a n d r u n fu n ct ion s a n d M-files.
F or m or e in for m a t ion on con t r ollin g in pu t a n d ou t pu t , see “Con t r ollin g
Com m a n d Win dow In pu t a n d Ou t pu t ” on pa ge 4-28.

Type functionsand
variablesat the
MATLABprompt.

MATLABdisplaysthe
results.

3-6
Desktop Tools

Command History
Lin es you en t er in t h e Com m a n d Win dow a r e logged in t h e Co m m a n d His to ry
win dow. In t h e Com m a n d H ist or y, you ca n view pr eviou sly u sed fu n ct ion s, a n d
copy a n d execu t e select ed lin es.

Timestamp marksthe
start of each session.

Select one or more lines


and right-click to copy,
evaluate, or create an
M-filefromtheselection.

To sa ve t h e in pu t a n d ou t pu t fr om a MATLAB session t o a file, u se t h e d i a r y


fu n ct ion .

N ote If ot h er u ser s sh a r e t h e sa m e m a ch in e wit h you , u sin g t h e sa m e log in


in for m a t ion , t h en t h ey will h a ve a ccess t o t h e fu n ct ion s you r a n du r in g a
session via t h e Com m a n d H ist or y. If you do n ot wa n t ot h er u ser s t o h a ve
a ccess t o t h e Com m a n d H ist or y fr om you r session , select Cle a r Co m m a n d
His to ry fr om t h e Ed it m en u befor e you qu it MATLAB.

Running External Programs


You ca n r u n ext er n a l pr ogr a m s fr om t h e MATLAB Com m a n d Win dow. Th e
excla m a t ion poin t ch a r a ct er ! is a sh ell esca pe a n d in dica t es t h a t t h e r est of t h e
in pu t lin e is a com m a n d t o t h e oper a t in g syst em . Th is is u sefu l for in vokin g

3-7
3 Development Environment

u t ilit ies or r u n n in g ot h er pr ogr a m s wit h ou t qu it t in g MATLAB. On Lin u x, for


exa m ple,
! e ma c s ma g i k . m

in vokes a n edit or ca lled e ma c s for a file n a m ed ma g i k . m. Wh en you qu it t h e


ext er n a l pr ogr a m , t h e oper a t in g syst em r et u r n s con t r ol t o MATLAB.

La unch Pa d
MATLAB’s La u n c h P a d pr ovides ea sy a ccess t o t ools, dem os, a n d
docu m en t a t ion .
Sampleof listingsinLaunchPad– you’ll seelistings
for all productsinstalled on your system.

Help - double-click togodirectly to


documentation for the product.

Demos-double-clicktodisplaythedemo
launcher for the product.

Tools - double-click to open the tool.

Click + to showthe listing for a product.

H elp Brow ser


Use t h e H elp br owser t o sea r ch a n d view docu m en t a t ion for a ll Ma t h Wor ks
pr odu ct s. Th e H elp br owser is a Web br owser in t egr a t ed in t o t h e MATLAB
deskt op t h a t displa ys H TML docu m en t s.

3-8
Desktop Tools

To open t h e H elp br owser , click t h e h elp bu t t on in t h e t oolba r , or t ype


h e l p b r o ws e r in t h e Com m a n d Win dow.

TabsintheHelpNavigator paneprovidedifferent waystofinddocumentation. Viewdocumentationinthedisplay pane.

Usetheclosebox tohidethepane. Dragtheseparator bar toadjust thewidthof thepanes.

Th e H elp br owser con sist s of t wo pa n es, t h e H elp Na viga t or , wh ich you u se t o


fin d in for m a t ion , a n d t h e displa y pa n e, wh er e you view t h e in for m a t ion .

3-9
3 Development Environment

Help Navigator
Use t o H elp Na viga t or t o fin d in for m a t ion . It in clu des:

• P ro d u c t filte r – Set t h e filt er t o sh ow docu m en t a t ion on ly for t h e pr odu ct s


you specify.

N ote In t h e St u den t Ver sion of MATLAB & Sim u lin k, t h e pr odu ct filt er is
in it ia lly set t o displa y a su bset of t h e en t ir e docu m en t a t ion set . You ca n a dd or
delet e wh ich pr odu ct docu m en t a t ion is displa yed by u sin g t h e pr odu ct filt er.

• Co n te n ts t a b – View t h e t it les a n d t a bles of con t en t s of docu m en t a t ion for


you r pr odu ct s.
• In d e x t a b – F in d specific in dex en t r ies (select ed keywor ds) in t h e
Ma t h Wor ks docu m en t a t ion for you r pr odu ct s.
• S e a rc h t a b – Look for a specific ph r a se in t h e docu m en t a t ion . To get h elp for
a specific fu n ct ion , set t h e S e a rc h ty p e t o F u n c tio n N a m e .
• F a v o rite s t a b – View a list of docu m en t s you pr eviou sly design a t ed a s
fa vor it es.

Display Pane
Aft er fin din g docu m en t a t ion u sin g t h e H elp Na viga t or , view it in t h e displa y
pa n e. Wh ile viewin g t h e docu m en t a t ion , you ca n :

• Br owse t o ot h er pa ges – Use t h e a r r ows a t t h e t ops a n d bot t om s of t h e pa ges,


or u se t h e ba ck a n d for wa r d bu t t on s in t h e t oolba r .
• Bookm a r k pa ges – Click t h e Ad d to F a v o rite s bu t t on in t h e t oolba r .
• P r in t pa ges – Click t h e pr in t bu t t on in t h e t oolba r .
• F in d a t er m in t h e pa ge – Type a t er m in t h e F in d in p a g e field in t h e t oolba r
a n d click Go .
Ot h er fea t u r es a va ila ble in t h e displa y pa n e a r e: copyin g in for m a t ion ,
eva lu a t in g a select ion , a n d viewin g Web pa ges.

For More Help


In a ddit ion t o t h e H elp br owser , you ca n u se h elp fu n ct ion s. To get h elp for a
specific fu n ct ion , u se d o c . F or exa m ple, d o c f o r ma t displa ys h elp for t h e

3-10
Desktop Tools

f o r ma t fu n ct ion in t h e H elp br owser . Ot h er m ea n s for get t in g h elp in clu de


con t a ct in g Tech n ica l Su ppor t (h t t p : / / www. ma t h wo r k s . c o m/ s u p p o r t ) a n d
pa r t icipa t in g in t h e n ewsgr ou p for MATLAB u ser s, c o mp . s o f t - s y s . ma t l a b .

Current Director y Brow ser


MATLAB file oper a t ion s u se t h e cu r r en t dir ect or y a n d t h e sea r ch pa t h a s
r efer en ce poin t s. An y file you wa n t t o r u n m u st eit h er be in t h e cu r r en t
dir ect or y or on t h e sea r ch pa t h .
A qu ick wa y t o view or ch a n ge t h e cu r r en t dir ect or y is by u sin g t h e Cu rre n t
D ire c to ry field in t h e deskt op t oolba r a s sh own below.

To sea r ch for , view, open , a n d m a ke ch a n ges t o MATLAB-r ela t ed dir ect or ies
a n d files, u se t h e MATLAB Cu r r en t Dir ect or y br owser . Alt er n a t ively, you ca n
u se t h e fu n ct ion s d i r , c d , a n d d e l e t e .

Use the pathname edit box to view Click the find button to search for content within M-files.
directoriesand their contents.

Double-clickafile
to open it in an
appropriate tool.

Viewthe help
portion of the
selected M-file.

3-11
3 Development Environment

Search Path
To det er m in e h ow t o execu t e fu n ct ion s you ca ll, MATLAB u ses a search path t o
fin d M-files a n d ot h er MATLAB-r ela t ed files, wh ich a r e or ga n ized in
dir ect or ies on you r file syst em . An y file you wa n t t o r u n in MATLAB m u st
r eside in t h e cu r r en t dir ect or y or in a dir ect or y t h a t is on t h e sea r ch pa t h . By
defa u lt , t h e files su pplied wit h MATLAB a n d Ma t h Wor ks t oolboxes a r e
in clu ded in t h e sea r ch pa t h .
To see wh ich dir ect or ies a r e on t h e sea r ch pa t h or t o ch a n ge t h e sea r ch pa t h ,
select S e t P a th fr om t h e F ile m en u in t h e deskt op, a n d u se t h e S e t P a th dia log
box. Alt er n a t ively, you ca n u se t h e p a t h fu n ct ion t o view t h e sea r ch pa t h ,
a d d p a t h t o a dd dir ect or ies t o t h e pa t h , a n d r mp a t h t o r em ove dir ect or ies fr om
t h e pa t h .

W ork spa ce Brow ser


Th e MATLAB wor kspa ce con sist s of t h e set of va r ia bles (n a m ed a r r a ys) bu ilt
u p du r in g a MATLAB session a n d st or ed in m em or y. You a dd va r ia bles t o t h e
wor kspa ce by u sin g fu n ct ion s, r u n n in g M-files, a n d loa din g sa ved wor kspa ces.
To view t h e wor kspa ce a n d in for m a t ion a bou t ea ch va r ia ble, u se t h e
Wor kspa ce br owser , or u se t h e fu n ct ion s wh o a n d wh o s .

Double-click
a variable to
see and
change its
contentsin
the Array
Editor.

3-12
Desktop Tools

To delet e va r ia bles fr om t h e wor kspa ce, select t h e va r ia ble a n d select D e le te


fr om t h e Ed it m en u . Alt er n a t ively, u se t h e c l e a r fu n ct ion .
Th e wor kspa ce is n ot m a in t a in ed a ft er you en d t h e MATLAB session . To sa ve
t h e wor kspa ce t o a file t h a t ca n be r ea d du r in g a la t er MATLAB session , select
S a v e Wo rk s p a c e As fr om t h e F ile m en u , or u se t h e s a v e fu n ct ion . Th is sa ves
t h e wor kspa ce t o a bin a r y file ca lled a MAT-file, wh ich h a s a . ma t ext en sion .
Th er e a r e opt ion s for sa vin g t o differ en t for m a t s. To r ea d in a MAT-file, select
Im p o rt D a ta fr om t h e F ile m en u , or u se t h e l o a d fu n ct ion .

Array Editor
Dou ble-click on a va r ia ble in t h e Wor kspa ce br owser t o see it in t h e Ar r a y
E dit or . Use t h e Ar r a y E dit or t o view a n d edit a visu a l r epr esen t a t ion of on e- or
t wo-dim en sion a l n u m er ic a r r a ys, st r in gs, a n d cell a r r a ys of st r in gs t h a t a r e in
t h e wor kspa ce.
Change valuesof array elements. Change the display format.

Use the tabsto viewthe variablesyou have open in the Array Editor.

3-13
3 Development Environment

Editor/ Debugger
Use t h e E dit or /Debu gger t o cr ea t e a n d debu g M-files, wh ich a r e pr ogr a m s you
wr it e t o r u n MATLAB fu n ct ion s. Th e E dit or /Debu gger pr ovides a gr a ph ica l
u ser in t er fa ce for ba sic t ext edit in g, a s well a s for M-file debu ggin g.

Comment selectedlinesandspecify indentingstyleusingthe Text menu. Find and replace strings.

Set breakpoints
where you want
execution to pause
soyoucanexamine
variables.

Holdthecursorover
a variable and its
current value
appears(known as
a datatip).

You ca n u se a n y t ext edit or t o cr ea t e M-files, su ch a s E m a cs, a n d ca n u se


pr efer en ces (a ccessible fr om t h e deskt op F ile m en u ) t o specify t h a t edit or a s
t h e defa u lt . If you u se a n ot h er edit or , you ca n st ill u se t h e MATLAB E dit or /
Debu gger for debu ggin g, or you ca n u se debu ggin g fu n ct ion s, su ch a s d b s t o p ,
wh ich set s a br ea kpoin t .
If you ju st n eed t o view t h e con t en t s of a n M-file, you ca n displa y it in t h e
Com m a n d Win dow by u sin g t h e t y p e fu n ct ion .

3-14
Other Development Environment Features

O ther Development Environm ent Fea tures


Addit ion a l developm en t en vir on m en t fea t u r es a r e:

• Im por t in g a n d E xpor t in g Da t a – Tech n iqu es for br in gin g da t a cr ea t ed by


ot h er a pplica t ion s in t o t h e MATLAB wor kspa ce, in clu din g t h e Im por t
Wiza r d, a n d pa cka gin g MATLAB wor kspa ce va r ia bles for u se by ot h er
a pplica t ion s.
• Im pr ovin g M-F ile P er for m a n ce – Th e P r ofiler is a t ool t h a t m ea su r es wh er e
a n M-file is spen din g it s t im e. Use it t o h elp you m a ke speed im pr ovem en t s.
• In t er fa cin g wit h Sou r ce Con t r ol Syst em s – Access you r sou r ce con t r ol syst em
fr om wit h in MATLAB, Sim u lin k, a n d St a t eflow.
• Usin g Not ebook – Access MATLAB’s n u m er ic com pu t a t ion a n d visu a liza t ion
soft wa r e fr om wit h in a wor d pr ocessin g en vir on m en t (Micr osoft Wor d).

3-15
3 Development Environment

3-16
4

Get t in g St a r t ed
Ma tric e s a n d Ma g ic S qu a re s . . . . . . . . . . . . . 4-2

Ex p re s s io n s . . . . . . . . . . . . . . . . . . . . 4-10

Wo rk in g w ith Ma tric e s . . . . . . . . . . . . . . . 4-14

Mo re Abo u t Ma tric e s a n d Arra y s . . . . . . . . . . . 4-18

Co n tro llin g Co m m a n d Win d o w In p u t a n d Ou tp u t . . . 4-28


4 Getting Started

M a trices a nd M a gic Squa res


In MATLAB, a m a t r ix is a r ect a n gu la r a r r a y of n u m ber s. Specia l m ea n in g is
som et im es a t t a ch ed t o 1-by-1 m a t r ices, wh ich a r e sca la r s, a n d t o m a t r ices wit h
on ly on e r ow or colu m n , wh ich a r e vect or s. MATLAB h a s ot h er wa ys of st or in g
bot h n u m er ic a n d n on n u m er ic da t a , bu t in t h e begin n in g, it is u su a lly best t o
t h in k of ever yt h in g a s a m a t r ix. Th e oper a t ion s in MATLAB a r e design ed t o be
a s n a t u r a l a s possible. Wh er e ot h er pr ogr a m m in g la n gu a ges wor k wit h
n u m ber s on e a t a t im e, MATLAB a llows you t o wor k wit h en t ir e m a t r ices
qu ickly a n d ea sily. A good exa m ple m a t r ix, u sed t h r ou gh ou t t h is book, a ppea r s
in t h e Ren a issa n ce en gr a vin g Mela n ch olia I by t h e Ger m a n a r t ist a n d a m a t eu r
m a t h em a t icia n Albr ech t Dü r er .

4-2
Matrices and Magic Squares

Th is im a ge is filled wit h
m a t h em a t ica l sym bolism , a n d if
you look ca r efu lly, you will see a
m a t r ix in t h e u pper r igh t
cor n er . Th is m a t r ix is kn own a s
a m a gic squ a r e a n d wa s
believed by m a n y in Dü r er ’s
t im e t o h a ve gen u in ely m a gica l
pr oper t ies. It does t u r n ou t t o
h a ve som e fa scin a t in g
ch a r a ct er ist ics wor t h explor in g.

Entering M a trices
Th e best wa y for you t o get st a r t ed wit h MATLAB is t o lea r n h ow t o h a n dle
m a t r ices. St a r t MATLAB a n d follow a lon g wit h ea ch exa m ple.
You ca n en t er m a t r ices in t o MATLAB in sever a l differ en t wa ys:

• E n t er a n explicit list of elem en t s.


• Loa d m a t r ices fr om ext er n a l da t a files.
• Gen er a t e m a t r ices u sin g bu ilt -in fu n ct ion s.
• Cr ea t e m a t r ices wit h you r own fu n ct ion s in M-files.

St a r t by en t er in g Dü r er ’s m a t r ix a s a list of it s elem en t s. You h a ve on ly t o


follow a few ba sic con ven t ion s:

• Sepa r a t e t h e elem en t s of a r ow wit h bla n ks or com m a s.


• Use a sem icolon , ; , t o in dica t e t h e en d of ea ch r ow.
• Su r r ou n d t h e en t ir e list of elem en t s wit h squ a r e br a cket s, [ ] .

To en t er Dü r er ’s m a t r ix, sim ply t ype in t h e Com m a n d Win dow


A = [ 16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]

4-3
4 Getting Started

MATLAB displa ys t h e m a t r ix you ju st en t er ed,


A =
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1

Th is exa ct ly m a t ch es t h e n u m ber s in t h e en gr a vin g. On ce you h a ve en t er ed t h e


m a t r ix, it is a u t om a t ica lly r em em ber ed in t h e MATLAB wor kspa ce. You ca n
r efer t o it sim ply a s A. Now t h a t you h a ve A in t h e wor kspa ce, t a ke a look a t
wh a t m a kes it so in t er est in g. Wh y is it m a gic?

sum , tra nspose, a nd dia g


You ’r e pr oba bly a lr ea dy a wa r e t h a t t h e specia l pr oper t ies of a m a gic squ a r e
h a ve t o do wit h t h e va r iou s wa ys of su m m in g it s elem en t s. If you t a ke t h e su m
a lon g a n y r ow or colu m n , or a lon g eit h er of t h e t wo m a in dia gon a ls, you will
a lwa ys get t h e sa m e n u m ber . Let ’s ver ify t h a t u sin g MATLAB. Th e fir st
st a t em en t t o t r y is
s u m( A)

MATLAB r eplies wit h


a ns =
34 34 34 34

Wh en you don ’t specify a n ou t pu t va r ia ble, MATLAB u ses t h e va r ia ble a n s ,


sh or t for a n s we r , t o st or e t h e r esu lt s of a ca lcu la t ion . You h a ve com pu t ed a r ow
vect or con t a in in g t h e su m s of t h e colu m n s of A. Su r e en ou gh , ea ch of t h e
colu m n s h a s t h e sa m e su m , t h e ma g i c su m , 34.
H ow a bou t t h e r ow su m s? MATLAB h a s a pr efer en ce for wor kin g wit h t h e
colu m n s of a m a t r ix, so t h e ea siest wa y t o get t h e r ow su m s is t o t r a n spose t h e
m a t r ix, com pu t e t h e colu m n su m s of t h e t r a n spose, a n d t h en t r a n spose t h e
r esu lt . Th e t r a n spose oper a t ion is den ot ed by a n a post r oph e or sin gle qu ot e, ' .
It flips a m a t r ix a bou t it s m a in dia gon a l a n d it t u r n s a r ow vect or in t o a colu m n
vect or . So
A'
pr odu ces

4-4
Matrices and Magic Squares

a ns =
16 5 9 4
3 10 6 15
2 11 7 14
13 8 12 1
An d
s u m( A' ) '

pr odu ces a colu m n vect or con t a in in g t h e r ow su m s


a ns =
34
34
34
34

Th e su m of t h e elem en t s on t h e m a in dia gon a l is ea sily obt a in ed wit h t h e h elp


of t h e d i a g fu n ct ion , wh ich picks off t h a t dia gon a l.
d i a g ( A)

pr odu ces
a ns =
16
10
7
1

and
s u m( d i a g ( A) )

pr odu ces
a ns =
34

Th e ot h er dia gon a l, t h e so-ca lled an tid iagon al, is n ot so im por t a n t


m a t h em a t ica lly, so MATLAB does n ot h a ve a r ea dy-m a de fu n ct ion for it . Bu t a
fu n ct ion or igin a lly in t en ded for u se in gr a ph ics, f l i p l r , flips a m a t r ix fr om left
t o r igh t .

4-5
4 Getting Started

s u m( d i a g ( f l i p l r ( A) ) )

a ns =
34

You h a ve ver ified t h a t t h e m a t r ix in Dü r er ’s en gr a vin g is in deed a m a gic


squ a r e a n d, in t h e pr ocess, h a ve sa m pled a few MATLAB m a t r ix oper a t ion s.
Th e followin g sect ion s con t in u e t o u se t h is m a t r ix t o illu st r a t e a ddit ion a l
MATLAB ca pa bilit ies.

Subscripts
Th e elem en t in r ow i a n d colu m n j of A is den ot ed by A( i , j ) . F or exa m ple,
A( 4 , 2 ) is t h e n u m ber in t h e fou r t h r ow a n d secon d colu m n . F or ou r m a gic
squ a r e, A( 4 , 2 ) is 1 5 . So it is possible t o com pu t e t h e su m of t h e elem en t s in t h e
fou r t h colu m n of A by t ypin g
A( 1 , 4 ) + A( 2 , 4 ) + A( 3 , 4 ) + A( 4 , 4 )

Th is pr odu ces
a ns =
34

bu t is n ot t h e m ost elega n t wa y of su m m in g a sin gle colu m n .


It is a lso possible t o r efer t o t h e elem en t s of a m a t r ix wit h a sin gle su bscr ipt ,
A( k ) . Th is is t h e u su a l wa y of r efer en cin g r ow a n d colu m n vect or s. Bu t it ca n
a lso a pply t o a fu lly t wo-dim en sion a l m a t r ix, in wh ich ca se t h e a r r a y is
r ega r ded a s on e lon g colu m n vect or for m ed fr om t h e colu m n s of t h e or igin a l
m a t r ix. So, for ou r m a gic squ a r e, A( 8 ) is a n ot h er wa y of r efer r in g t o t h e va lu e
1 5 st or ed in A( 4 , 2 ) .

If you t r y t o u se t h e va lu e of a n elem en t ou t side of t h e m a t r ix, it is a n er r or .


t = A( 4 , 5 )
I n d e x e x c e e d s ma t r i x d i me n s i o n s .

On t h e ot h er h a n d, if you st or e a va lu e in a n elem en t ou t side of t h e m a t r ix, t h e


size in cr ea ses t o a ccom m oda t e t h e n ewcom er .
X = A;
X( 4 , 5 ) = 1 7

4-6
Matrices and Magic Squares

X =
16 3 2 13 0
5 10 11 8 0
9 6 7 12 0
4 15 14 1 17

The Colon O pera tor


Th e colon , : , is on e of MATLAB’s m ost im por t a n t oper a t or s. It occu r s in sever a l
differ en t for m s. Th e expr ession
1: 10

is a r ow vect or con t a in in g t h e in t eger s fr om 1 t o 10


1 2 3 4 5 6 7 8 9 10

To obt a in n on u n it spa cin g, specify a n in cr em en t . F or exa m ple,


100: - 7: 50

is
100 93 86 79 72 65 58 51

and
0: pi / 4: pi

is
0 0. 7854 1. 5708 2. 3562 3. 1416

Su bscr ipt expr ession s in volvin g colon s r efer t o por t ion s of a m a t r ix.
A( 1 : k , j )

is t h e fir st k elem en t s of t h e j t h colu m n of A. So


s u m( A( 1 : 4 , 4 ) )

com pu t es t h e su m of t h e fou r t h colu m n . Bu t t h er e is a bet t er wa y. Th e colon by


it self r efer s t o all t h e elem en t s in a r ow or colu m n of a m a t r ix a n d t h e keywor d
e n d r efer s t o t h e last r ow or colu m n . So
s u m( A( : , e n d ) )

4-7
4 Getting Started

com pu t es t h e su m of t h e elem en t s in t h e la st colu m n of A.


a ns =
34

Wh y is t h e m a gic su m for a 4-by-4 squ a r e equ a l t o 34? If t h e in t eger s fr om 1 t o


16 a r e sor t ed in t o fou r gr ou ps wit h equ a l su m s, t h a t su m m u st be
s u m( 1 : 1 6 ) / 4

wh ich , of cou r se, is


a ns =
34

Usin g t h e Sym bolic Ma t h Toolbox, you ca n discover t h a t t h e m a gic su m for a n


3
n -by-n m a gic squ a r e is ( n + n ) ⁄ 2 .

The m a gic Function


MATLAB a ct u a lly h a s a bu ilt -in fu n ct ion t h a t cr ea t es m a gic squ a r es of a lm ost
a n y size. Not su r pr isin gly, t h is fu n ct ion is n a m ed ma g i c .
B = ma g i c ( 4 )

B =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1

Th is m a t r ix is a lm ost t h e sa m e a s t h e on e in t h e Dü r er en gr a vin g a n d h a s a ll
t h e sa m e “m a gic” pr oper t ies; t h e on ly differ en ce is t h a t t h e t wo m iddle colu m n s
a r e exch a n ged. To m a ke t h is B in t o Dü r er ’s A, swa p t h e t wo m iddle colu m n s.
A = B( : , [ 1 3 2 4 ] )

4-8
Matrices and Magic Squares

Th is sa ys “for ea ch of t h e r ows of m a t r ix B, r eor der t h e elem en t s in t h e or der 1,


3, 2, 4.” It pr odu ces
A =
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1

Wh y wou ld Dü r er go t o t h e t r ou ble of r ea r r a n gin g t h e colu m n s wh en h e cou ld


h a ve u sed MATLAB’s or der in g? No dou bt h e wa n t ed t o in clu de t h e da t e of t h e
en gr a vin g, 1514, a t t h e bot t om of h is m a gic squ a r e.

For M ore Informa tion “Usin g MATLAB,” wh ich is a ccessible fr om H elp,


pr ovides com pr eh en sive m a t er ia l on t h e developm en t en vir on m en t ,
m a t h em a t ics, pr ogr a m m in g a n d da t a t ypes, gr a ph ics, 3-D visu a liza t ion ,
ext er n a l in t er fa ces/AP I, a n d cr ea t in g gr a ph ica l u ser in t er fa ces in MATLAB.

4-9
4 Getting Started

Ex pressions
Like m ost ot h er pr ogr a m m in g la n gu a ges, MATLAB pr ovides m a t h em a t ica l
expression s, bu t u n like m ost pr ogr a m m in g la n gu a ges, t h ese expr ession s
in volve en t ir e m a t r ices. Th e bu ildin g blocks of expr ession s a r e:

• Va r ia bles
• Nu m ber s
• Oper a t or s
• F u n ct ion s

Va ria bles
MATLAB does n ot r equ ir e a n y t ype decla r a t ion s or dim en sion st a t em en t s.
Wh en MATLAB en cou n t er s a n ew va r ia ble n a m e, it a u t om a t ica lly cr ea t es t h e
va r ia ble a n d a lloca t es t h e a ppr opr ia t e a m ou n t of st or a ge. If t h e va r ia ble
a lr ea dy exist s, MATLAB ch a n ges it s con t en t s a n d, if n ecessa r y, a lloca t es n ew
st or a ge. F or exa m ple,
n u m_ s t u d e n t s = 2 5

cr ea t es a 1-by-1 m a t r ix n a m ed n u m_ s t u d e n t s a n d st or es t h e va lu e 25 in it s
sin gle elem en t .
Va r ia ble n a m es con sist of a let t er , followed by a n y n u m ber of let t er s, digit s, or
u n der scor es. MATLAB u ses on ly t h e fir st 31 ch a r a ct er s of a va r ia ble n a m e.
MATLAB is ca se sen sit ive; it dist in gu ish es bet ween u pper ca se a n d lower ca se
let t er s. A a n d a a r e n ot t h e sa m e va r ia ble. To view t h e m a t r ix a ssign ed t o a n y
va r ia ble, sim ply en t er t h e va r ia ble n a m e.

N um bers
MATLAB u ses con ven t ion a l decim a l n ot a t ion , wit h a n opt ion a l decim a l poin t
a n d lea din g plu s or m in u s sign , for n u m ber s. S cien tific n otation u ses t h e let t er
e t o specify a power -of-t en sca le fa ct or . Im agin ary n u m bers u se eit h er i or j a s
a su ffix. Som e exa m ples of lega l n u m ber s a r e
3 - 99 0. 0001
9. 6397238 1. 60210e - 20 6. 02252e 23
1i - 3. 14159j 3e 5i

4-10
Expressions

All n u m ber s a r e st or ed in t er n a lly u sin g t h e lon g for m a t specified by t h e IE E E


floa t in g-poin t st a n da r d. F loa t in g-poin t n u m ber s h a ve a fin it e precision of
r ou gh ly 16 sign ifica n t decim a l digit s a n d a fin it e ran ge of r ou gh ly 10 -308 t o
10 +308 .

O pera tors
E xpr ession s u se fa m ilia r a r it h m et ic oper a t or s a n d pr eceden ce r u les.

+ Addit ion

- Su bt r a ct ion

* Mu lt iplica t ion

/ Division

\ Left division (descr ibed in “Ma t r ices a n d Lin ea r


Algebr a ” in Usin g M AT L AB )

^ P ower

' Com plex con ju ga t e t r a n spose

( ) Specify eva lu a t ion or der

Functions
MATLAB pr ovides a la r ge n u m ber of st a n da r d elem en t a r y m a t h em a t ica l
fu n ct ion s, in clu din g a b s , s q r t , e x p , a n d s i n . Ta kin g t h e squ a r e r oot or
loga r it h m of a n ega t ive n u m ber is n ot a n er r or ; t h e a ppr opr ia t e com plex r esu lt
is pr odu ced a u t om a t ica lly. MATLAB a lso pr ovides m a n y m or e a dva n ced
m a t h em a t ica l fu n ct ion s, in clu din g Bessel a n d ga m m a fu n ct ion s. Most of t h ese
fu n ct ion s a ccept com plex a r gu m en t s. F or a list of t h e elem en t a r y m a t h em a t ica l
fu n ct ion s, t ype
he l p e l f un

4-11
4 Getting Started

F or a list of m or e a dva n ced m a t h em a t ica l a n d m a t r ix fu n ct ion s, t ype


he l p s pe c f un
h e l p e l ma t

For M ore Informa tion Appen dix A, “MATLAB Qu ick Refer en ce,” con t a in s
br ief descr ipt ion s of t h e MATLAB fu n ct ion s. Use H elp t o a ccess com plet e
descr ipt ion s of a ll t h e MATLAB fu n ct ion s by ca t egor y or a lph a bet ica lly.

Som e of t h e fu n ct ion s, like s q r t a n d s i n , a r e bu ilt-in . Th ey a r e pa r t of t h e


MATLAB cor e so t h ey a r e ver y efficien t , bu t t h e com pu t a t ion a l det a ils a r e n ot
r ea dily a ccessible. Ot h er fu n ct ion s, like g a mma a n d s i n h , a r e im plem en t ed in
M-files. You ca n see t h e code a n d even m odify it if you wa n t .
Sever a l specia l fu n ct ion s pr ovide va lu es of u sefu l con st a n t s.

pi 3.14159265…

i Im a gin a r y u n it , –1

j Sa m e a s i
– 52
e ps F loa t in g-poin t r ela t ive pr ecision , 2
– 1022
r e a l mi n Sm a llest floa t in g-poin t n u m ber , 2
1023
r e a l ma x La r gest floa t in g-poin t n u m ber , ( 2 – ε )2

I nf In fin it y

Na N Not -a -n u m ber

In fin it y is gen er a t ed by dividin g a n on zer o va lu e by zer o, or by eva lu a t in g well


defin ed m a t h em a t ica l expr ession s t h a t overflow , i.e., exceed r e a l ma x .
Not -a -n u m ber is gen er a t ed by t r yin g t o eva lu a t e expr ession s like 0 / 0 or
I n f - I n f t h a t do n ot h a ve well defin ed m a t h em a t ica l va lu es.

Th e fu n ct ion n a m es a r e n ot r eser ved. It is possible t o over wr it e a n y of t h em


wit h a n ew va r ia ble, su ch a s

4-12
Expressions

e ps = 1. e - 6

a n d t h en u se t h a t va lu e in su bsequ en t ca lcu la t ion s. Th e or igin a l fu n ct ion ca n


be r est or ed wit h
c l e a r e ps

Ex a m ples of Ex pressions
You h a ve a lr ea dy seen sever a l exa m ples of MATLAB expr ession s. H er e a r e a
few m or e exa m ples, a n d t h e r esu lt in g va lu es.
r h o = ( 1 +s q r t ( 5 ) ) / 2
r ho =
1. 6180

a = a b s ( 3 +4 i )
a =
5

z = s qr t ( be s s e l k( 4/ 3, r ho- i ) )
z =
0. 3730+ 0. 3214i

h u g e = e x p ( l o g ( r e a l ma x ) )
huge =
1 . 7 9 7 7 e +3 0 8

t oobi g = pi *huge
t oobi g =
I nf

4-13
4 Getting Started

W ork ing w ith M a trices


Th is sect ion in t r odu ces you t o ot h er wa ys of cr ea t in g m a t r ices.

Genera ting M a trices


MATLAB pr ovides fou r fu n ct ion s t h a t gen er a t e ba sic m a t r ices.

z e r os All zer os

one s All on es

r a nd Un ifor m ly dist r ibu t ed r a n dom elem en t s

r a ndn Nor m a lly dist r ibu t ed r a n dom elem en t s

H er e a r e som e exa m ples.


Z = z e r os ( 2, 4)
Z =
0 0 0 0
0 0 0 0

F = 5*one s ( 3, 3)
F =
5 5 5
5 5 5
5 5 5

N = f i x( 10*r a nd( 1, 10) )


N =
4 9 4 4 8 5 2 6 8 0

R = r a ndn( 4, 4)
R =
1. 0668 0. 2944 - 0. 6918 - 1. 4410
0. 0593 - 1. 3362 0. 8580 0. 5711
- 0. 0956 0. 7143 1. 2540 - 0. 3999
- 0. 8323 1. 6236 - 1. 5937 0. 6900

4-14
W orking with Matrices

The loa d Com m a nd


Th e l o a d com m a n d r ea ds bin a r y files con t a in in g m a t r ices gen er a t ed by ea r lier
MATLAB session s, or r ea ds t ext files con t a in in g n u m er ic da t a . Th e t ext file
sh ou ld be or ga n ized a s a r ect a n gu la r t a ble of n u m ber s, sepa r a t ed by bla n ks,
wit h on e r ow per lin e, a n d a n equ a l n u m ber of elem en t s in ea ch r ow. F or
exa m ple, ou t side of MATLAB, cr ea t e a t ext file con t a in in g t h ese fou r lin es.
16. 0 3. 0 2. 0 13. 0
5. 0 10. 0 11. 0 8. 0
9. 0 6. 0 7. 0 12. 0
4. 0 15. 0 14. 0 1. 0

St or e t h e file u n der t h e n a m e ma g i k . d a t . Th en t h e com m a n d


l o a d ma g i k . d a t

r ea ds t h e file a n d cr ea t es a va r ia ble, ma g i k , con t a in in g ou r exa m ple m a t r ix.


An ea sy wa y t o r ea d da t a in t o MATLAB in m a n y t ext or bin a r y for m a t s is t o
u se t h e Im por t Wiza r d.

M -Files
You ca n cr ea t e you r own m a t r ices u sin g M -files, wh ich a r e t ext files con t a in in g
MATLAB code. Use t h e MATLAB E dit or or a n ot h er t ext edit or t o cr ea t e a file
con t a in in g t h e sa m e st a t em en t s you wou ld t ype a t t h e MATLAB com m a n d
lin e. Sa ve t h e file u n der a n a m e t h a t en ds in . m.
F or exa m ple, cr ea t e a file con t a in in g t h ese five lin es.
A = [ ...
16. 0 3. 0 2. 0 13. 0
5. 0 10. 0 11. 0 8. 0
9. 0 6. 0 7. 0 12. 0
4. 0 15. 0 14. 0 1. 0 ];

St or e t h e file u n der t h e n a m e ma g i k . m. Th en t h e st a t em en t
ma g i k

r ea ds t h e file a n d cr ea t es a va r ia ble, A, con t a in in g ou r exa m ple m a t r ix.

4-15
4 Getting Started

Conca tena tion


Con caten ation is t h e pr ocess of join in g sm a ll m a t r ices t o m a ke bigger on es. In
fa ct , you m a de you r fir st m a t r ix by con ca t en a t in g it s in dividu a l elem en t s. Th e
pa ir of squ a r e br a cket s, [ ] , is t h e con ca t en a t ion oper a t or . F or a n exa m ple, st a r t
wit h t h e 4-by-4 m a gic squ a r e, A, a n d for m
B = [A A+3 2 ; A+4 8 A+1 6 ]

Th e r esu lt is a n 8-by-8 m a t r ix, obt a in ed by join in g t h e fou r su bm a t r ices.


B =

16 3 2 13 48 35 34 45
5 10 11 8 37 42 43 40
9 6 7 12 41 38 39 44
4 15 14 1 36 47 46 33
64 51 50 61 32 19 18 29
53 58 59 56 21 26 27 24
57 54 55 60 25 22 23 28
52 63 62 49 20 31 30 17

Th is m a t r ix is h a lf wa y t o bein g a n ot h er m a gic squ a r e. It s elem en t s a r e a


r ea r r a n gem en t of t h e in t eger s 1 : 6 4 . It s colu m n su m s a r e t h e cor r ect va lu e for
a n 8-by-8 m a gic squ a r e.
s u m( B)

a ns =
260 260 260 260 260 260 260 260

Bu t it s r ow su m s, s u m( B' ) ' , a r e n ot a ll t h e sa m e. F u r t h er m a n ipu la t ion is


n ecessa r y t o m a ke t h is a va lid 8-by-8 m a gic squ a r e.

Deleting Row s a nd Colum ns


You ca n delet e r ows a n d colu m n s fr om a m a t r ix u sin g ju st a pa ir of squ a r e
br a cket s. St a r t wit h
X = A;

Th en , t o delet e t h e secon d colu m n of X, u se


X( : , 2 ) = [ ]

4-16
W orking with Matrices

Th is ch a n ges X t o
X =
16 2 13
5 11 8
9 7 12
4 14 1

If you delet e a sin gle elem en t fr om a m a t r ix, t h e r esu lt isn ’t a m a t r ix a n ym or e.


So, expr ession s like
X( 1 , 2 ) = [ ]

r esu lt in a n er r or . H owever , u sin g a sin gle su bscr ipt delet es a sin gle elem en t ,
or sequ en ce of elem en t s, a n d r esh a pes t h e r em a in in g elem en t s in t o a r ow
vect or . So
X( 2 : 2 : 1 0 ) = [ ]

r esu lt s in
X =
16 9 2 7 13 12 1

4-17
4 Getting Started

M ore About M a trices a nd Arra ys


Th is sect ion s sh ows you m or e a bou t wor kin g wit h m a t r ices a n d a r r a ys,
focu sin g on :

• Lin ea r a lgebr a
• Ar r a ys
• Mu lt iva r ia t e da t a

Linea r Algebra
In for m a lly, t h e t er m s m atrix a n d array a r e oft en u sed in t er ch a n gea bly. Mor e
pr ecisely, a m atrix is a t wo-dim en sion a l n u m er ic a r r a y t h a t r epr esen t s a lin ear
tran sform ation . Th e m a t h em a t ica l oper a t ion s defin ed on m a t r ices a r e t h e
su bject of lin ear algebra.
Dü r er ’s m a gic squ a r e
A =
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1

pr ovides sever a l exa m ples t h a t give a t a st e of MATLAB m a t r ix oper a t ion s.


You ’ve a lr ea dy seen t h e m a t r ix t r a n spose, A'. Addin g a m a t r ix t o it s t r a n spose
pr odu ces a sym m etric m a t r ix.
A + A'

a ns =
32 8 11 17
8 20 17 23
11 17 14 26
17 23 26 2

Th e m u lt iplica t ion sym bol, * , den ot es t h e m atrix m u lt iplica t ion in volvin g in n er


pr odu ct s bet ween r ows a n d colu m n s. Mu lt iplyin g t h e t r a n spose of a m a t r ix by
t h e or igin a l m a t r ix a lso pr odu ces a sym m et r ic m a t r ix.

4-18
More About Matrices and Arrays

A' * A

a ns =
378 212 206 360
212 370 368 206
206 368 370 212
360 206 212 378

Th e det er m in a n t of t h is pa r t icu la r m a t r ix h a ppen s t o be zer o, in dica t in g t h a t


t h e m a t r ix is sin gu lar.
d = d e t ( A)

d =
0

Th e r edu ced r ow ech elon for m of A is n ot t h e iden t it y.


R = r r e f ( A)

R =
1 0 0 1
0 1 0 -3
0 0 1 3
0 0 0 0

Sin ce t h e m a t r ix is sin gu la r , it does n ot h a ve a n in ver se. If you t r y t o com pu t e


t h e in ver se wit h
X = i n v ( A)

you will get a wa r n in g m essa ge


Wa r n i n g : Ma t r i x i s c l o s e t o s i n g u l a r o r b a d l y s c a l e d .
Re s u l t s ma y b e i n a c c u r a t e . RCOND = 1 . 1 7 5 5 3 0 e - 0 1 7 .

Rou n doff er r or h a s pr even t ed t h e m a t r ix in ver sion a lgor it h m fr om det ect in g


exa ct sin gu la r it y. Bu t t h e va lu e of r c o n d , wh ich st a n ds for reciprocal con d ition
estim ate, is on t h e or der of e p s , t h e floa t in g-poin t r ela t ive pr ecision , so t h e
com pu t ed in ver se is u n likely t o be of m u ch u se.

4-19
4 Getting Started

Th e eigen va lu es of t h e m a gic squ a r e a r e in t er est in g.


e = e i g ( A)

e =
34. 0000
8. 0000
0. 0000
- 8. 0000

On e of t h e eigen va lu es is zer o, wh ich is a n ot h er con sequ en ce of sin gu la r it y.


Th e la r gest eigen va lu e is 34, t h e m a gic su m . Th a t ’s beca u se t h e vect or of a ll
on es is a n eigen vect or .
v = one s ( 4, 1)

v =
1
1
1
1

A* v

a ns =
34
34
34
34

Wh en a m a gic squ a r e is sca led by it s m a gic su m ,


P = A/ 3 4

t h e r esu lt is a d ou bly stoch astic m a t r ix wh ose r ow a n d colu m n su m s a r e a ll on e.


P =
0. 4706 0. 0882 0. 0588 0. 3824
0. 1471 0. 2941 0. 3235 0. 2353
0. 2647 0. 1765 0. 2059 0. 3529
0. 1176 0. 4412 0. 4118 0. 0294

4-20
More About Matrices and Arrays

Su ch m a t r ices r epr esen t t h e t r a n sit ion pr oba bilit ies in a M ark ov process.
Repea t ed power s of t h e m a t r ix r epr esen t r epea t ed st eps of t h e pr ocess. F or ou r
exa m ple, t h e fift h power
P^ 5

is
0. 2507 0. 2495 0. 2494 0. 2504
0. 2497 0. 2501 0. 2502 0. 2500
0. 2500 0. 2498 0. 2499 0. 2503
0. 2496 0. 2506 0. 2505 0. 2493

Th is sh ows t h a t a s k a ppr oa ch es in fin it y, a ll t h e elem en t s in t h e k t h power ,


k
p , a ppr oa ch 1 ⁄ 4 .
F in a lly, t h e coefficien t s in t h e ch a r a ct er ist ic polyn om ia l
p o l y ( A)

are
1 - 34 - 64 2176 0

Th is in dica t es t h a t t h e ch a r a ct er ist ic polyn om ia l


d et ( A – λI )
is
4 3 2
λ – 34 λ – 64 λ + 2176 λ
Th e con st a n t t er m is zer o, beca u se t h e m a t r ix is sin gu la r , a n d t h e coefficien t of
t h e cu bic t er m is -34, beca u se t h e m a t r ix is m a gic!

For M ore Informa tion All of t h e MATLAB m a t h fu n ct ion s a r e descr ibed in


t h e “MATLAB F u n ct ion Refer en ce,” wh ich is a ccessible fr om H elp.

Arra y s
Wh en t h ey a r e t a ken a wa y fr om t h e wor ld of lin ea r a lgebr a , m a t r ices becom e
t wo dim en sion a l n u m er ic a r r a ys. Ar it h m et ic oper a t ion s on a r r a ys a r e don e
elem en t -by-elem en t . Th is m ea n s t h a t a ddit ion a n d su bt r a ct ion a r e t h e sa m e

4-21
4 Getting Started

for a r r a ys a n d m a t r ices, bu t t h a t m u lt iplica t ive oper a t ion s a r e differ en t .


MATLAB u ses a dot , or decim a l poin t , a s pa r t of t h e n ot a t ion for m u lt iplica t ive
a r r a y oper a t ion s.
Th e list of oper a t or s in clu des:

+ Addit ion

- Su bt r a ct ion

.* E lem en t -by-elem en t m u lt iplica t ion

./ E lem en t -by-elem en t division

.\ E lem en t -by-elem en t left division

.^ E lem en t -by-elem en t power

.' Un con ju ga t ed a r r a y t r a n spose

If t h e Dü r er m a gic squ a r e is m u lt iplied by it self wit h a r r a y m u lt iplica t ion


A. * A

t h e r esu lt is a n a r r a y con t a in in g t h e squ a r es of t h e in t eger s fr om 1 t o 16, in a n


u n u su a l or der .
a ns =
256 9 4 169
25 100 121 64
81 36 49 144
16 225 196 1

Building Tables
Ar r a y oper a t ion s a r e u sefu l for bu ildin g t a bles. Su ppose n is t h e colu m n vect or
n = ( 0: 9) ' ;

Th en
p o ws = [ n n. ^ 2 2. ^ n]

4-22
More About Matrices and Arrays

bu ilds a t a ble of squ a r es a n d power s of t wo.


p o ws =
0 0 1
1 1 2
2 4 4
3 9 8
4 16 16
5 25 32
6 36 64
7 49 128
8 64 256
9 81 512

Th e elem en t a r y m a t h fu n ct ion s oper a t e on a r r a ys elem en t by elem en t . So


f o r ma t s h o r t g
x = ( 1: 0. 1: 2) ' ;
l ogs = [ x l og10( x) ]

bu ilds a t a ble of loga r it h m s.


l ogs =
1. 0 0
1. 1 0. 04139
1. 2 0. 07918
1. 3 0. 11394
1. 4 0. 14613
1. 5 0. 17609
1. 6 0. 20412
1. 7 0. 23045
1. 8 0. 25527
1. 9 0. 27875
2. 0 0. 30103

M ultiva ria te Da ta
MATLAB u ses colu m n -or ien t ed a n a lysis for m u lt iva r ia t e st a t ist ica l da t a . E a ch
colu m n in a da t a set r epr esen t s a va r ia ble a n d ea ch r ow a n obser va t ion . Th e
( i , j ) t h elem en t is t h e i t h obser va t ion of t h e j t h va r ia ble.

4-23
4 Getting Started

As a n exa m ple, con sider a da t a set wit h t h r ee va r ia bles:


• H ea r t r a t e
• Weigh t
• H ou r s of exer cise per week

F or five obser va t ion s, t h e r esu lt in g a r r a y m igh t look like


D =
72 134 3. 2
81 201 3. 5
69 156 7. 1
82 148 2. 4
75 170 1. 2

Th e fir st r ow con t a in s t h e h ea r t r a t e, weigh t , a n d exer cise h ou r s for pa t ien t 1,


t h e secon d r ow con t a in s t h e da t a for pa t ien t 2, a n d so on . Now you ca n a pply
m a n y of MATLAB’s da t a a n a lysis fu n ct ion s t o t h is da t a set . F or exa m ple, t o
obt a in t h e m ea n a n d st a n da r d devia t ion of ea ch colu m n :
mu = me a n ( D) , s i g ma = s t d ( D)

mu =
75. 8 161. 8 3. 48

s i g ma =
5. 6303 25. 499 2. 2107

F or a list of t h e da t a a n a lysis fu n ct ion s a va ila ble in MATLAB, t ype


he l p da t a f un

If you h a ve a ccess t o t h e St a t ist ics Toolbox, t ype


he l p s t a t s

Sca la r Ex pa nsion
Ma t r ices a n d sca la r s ca n be com bin ed in sever a l differ en t wa ys. F or exa m ple,
a sca la r is su bt r a ct ed fr om a m a t r ix by su bt r a ct in g it fr om ea ch elem en t . Th e
a ver a ge va lu e of t h e elem en t s in ou r m a gic squ a r e is 8.5, so
B = A - 8. 5

4-24
More About Matrices and Arrays

for m s a m a t r ix wh ose colu m n su m s a r e zer o.


B =
7. 5 - 5. 5 - 6. 5 4. 5
- 3. 5 1. 5 2. 5 - 0. 5
0. 5 - 2. 5 - 1. 5 3. 5
- 4. 5 6. 5 5. 5 - 7. 5

s u m( B)

a ns =
0 0 0 0

Wit h sca la r expa n sion , MATLAB a ssign s a specified sca la r t o a ll in dices in a


r a n ge. F or exa m ple,
B( 1 : 2 , 2 : 3 ) = 0

zer os ou t a por t ion of B


B =
7. 5 0 0 4. 5
- 3. 5 0 0 - 0. 5
0. 5 - 2. 5 - 1. 5 3. 5
- 4. 5 6. 5 5. 5 - 7. 5

Logica l Subscripting
Th e logica l vect or s cr ea t ed fr om logica l a n d r ela t ion a l oper a t ion s ca n be u sed
t o r efer en ce su ba r r a ys. Su ppose X is a n or din a r y m a t r ix a n d L is a m a t r ix of t h e
sa m e size t h a t is t h e r esu lt of som e logica l oper a t ion . Th en X( L) specifies t h e
elem en t s of X wh er e t h e elem en t s of L a r e n on zer o.
Th is kin d of su bscr ipt in g ca n be don e in on e st ep by specifyin g t h e logica l
oper a t ion a s t h e su bscr ipt in g expr ession . Su ppose you h a ve t h e followin g set of
da t a .
x =
2 . 1 1 . 7 1 . 6 1 . 5 Na N 1 . 9 1 . 8 1 . 5 5 . 1 1 . 8 1 . 4 2 . 2 1 . 6 1 . 8

Th e Na N is a m a r ker for a m issin g obser va t ion , su ch a s a fa ilu r e t o r espon d t o


a n it em on a qu est ion n a ir e. To r em ove t h e m issin g da t a wit h logica l in dexin g,

4-25
4 Getting Started

u se f i n i t e ( x ) , wh ich is t r u e for a ll fin it e n u m er ica l va lu es a n d fa lse for Na N


and I nf .
x = x( f i ni t e ( x) )
x =
2. 1 1. 7 1. 6 1. 5 1. 9 1. 8 1. 5 5. 1 1. 8 1. 4 2. 2 1. 6 1. 8

Now t h er e is on e obser va t ion , 5 . 1 , wh ich seem s t o be ver y differ en t fr om t h e


ot h er s. It is a n ou tlier. Th e followin g st a t em en t r em oves ou t lier s, in t h is ca se
t h ose elem en t s m or e t h a n t h r ee st a n da r d devia t ion s fr om t h e m ea n .
x = x ( a b s ( x - me a n ( x ) ) <= 3 * s t d ( x ) )
x =
2. 1 1. 7 1. 6 1. 5 1. 9 1. 8 1. 5 1. 8 1. 4 2. 2 1. 6 1. 8

F or a n ot h er exa m ple, h igh ligh t t h e loca t ion of t h e pr im e n u m ber s in Dü r er ’s


m a gic squ a r e by u sin g logica l in dexin g a n d sca la r expa n sion t o set t h e
n on pr im es t o 0.
A( ~i s p r i me ( A) ) = 0

A =
0 3 2 13
5 0 11 0
0 0 7 0
0 0 0 0

The find Function


Th e f i n d fu n ct ion det er m in es t h e in dices of a r r a y elem en t s t h a t m eet a given
logica l con dit ion . In it s sim plest for m , f i n d r et u r n s a colu m n vect or of in dices.
Tr a n spose t h a t vect or t o obt a in a r ow vect or of in dices. F or exa m ple,
k = f i n d ( i s p r i me ( A) ) '

picks ou t t h e loca t ion s, u sin g on e-dim en sion a l in dexin g, of t h e pr im es in t h e


m a gic squ a r e.
k =
2 5 9 10 11 13

4-26
More About Matrices and Arrays

Displa y t h ose pr im es, a s a r ow vect or in t h e or der det er m in ed by k , wit h


A( k )

a ns =
5 3 2 11 7 13

Wh en you u se k a s a left -h a n d-side in dex in a n a ssign m en t st a t em en t , t h e


m a t r ix st r u ct u r e is pr eser ved.
A( k ) = Na N

A =
16 Na N Na N Na N
Na N 10 Na N 8
9 6 Na N 12
4 15 14 1

4-27
4 Getting Started

Controlling Comma nd W indow Input a nd O utput


So fa r , you h a ve been u sin g t h e MATLAB com m a n d lin e, t ypin g com m a n ds a n d
expr ession s, a n d seein g t h e r esu lt s pr in t ed in t h e Com m a n d Win dow. Th is
sect ion descr ibes h ow t o:

• Con t r ol t h e a ppea r a n ce of t h e ou t pu t va lu es
• Su ppr ess ou t pu t fr om MATLAB com m a n ds
• E n t er lon g com m a n ds a t t h e com m a n d lin e
• E dit t h e com m a n d lin e

The form a t Com m a nd


Th e f o r ma t com m a n d con t r ols t h e n u m er ic for m a t of t h e va lu es displa yed by
MATLAB. Th e com m a n d a ffect s on ly h ow n u m ber s a r e displa yed, n ot h ow
MATLAB com pu t es or sa ves t h em . H er e a r e t h e differ en t for m a t s, t oget h er
wit h t h e r esu lt in g ou t pu t pr odu ced fr om a vect or x wit h com pon en t s of
differ en t m a gn it u des.

N ote To en su r e pr oper spa cin g, u se a fixed-widt h fon t , su ch a s F ixedsys or


Cou r ier.

x = [ 4/ 3 1. 2345e - 6]

f o r ma t s h o r t

1. 3333 0. 0000

f o r ma t s h o r t e

1 . 3 3 3 3 e +0 0 0 1. 2345e - 006

f o r ma t s h o r t g

1. 3333 1. 2345e - 006

4-28
Controlling Command W indow Input and Output

f o r ma t l o n g

1. 33333333333333 0. 00000123450000

f o r ma t l o n g e

1 . 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 e +0 0 0 1. 234500000000000e - 006

f o r ma t l o n g g

1. 33333333333333 1. 2345e - 006

f o r ma t b a n k

1. 33 0. 00

f o r ma t r a t

4/ 3 1/ 810045

f o r ma t h e x

3f f 5555555555555 3e b4b6231a bf d271


3 –3
If t h e la r gest elem en t of a m a t r ix is la r ger t h a n 10 or sm a ller t h a n 10 ,
MATLAB a pplies a com m on sca le fa ct or for t h e sh or t a n d lon g for m a t s.
In a ddit ion t o t h e f o r ma t com m a n ds sh own a bove
f o r ma t c o mp a c t

su ppr esses m a n y of t h e bla n k lin es t h a t a ppea r in t h e ou t pu t . Th is let s you


view m or e in for m a t ion on a scr een or win dow. If you wa n t m or e con t r ol over
t h e ou t pu t for m a t , u se t h e s p r i n t f a n d f p r i n t f fu n ct ion s.

4-29
4 Getting Started

Suppressing O utput
If you sim ply t ype a st a t em en t a n d pr ess Re tu rn or En te r , MATLAB
a u t om a t ica lly displa ys t h e r esu lt s on scr een . H owever , if you en d t h e lin e wit h
a sem icolon , MATLAB per for m s t h e com pu t a t ion bu t does n ot displa y a n y
ou t pu t . Th is is pa r t icu la r ly u sefu l wh en you gen er a t e la r ge m a t r ices. F or
exa m ple,
A = ma g i c ( 1 0 0 ) ;

Entering Long Com m a nd Lines


If a st a t em en t does n ot fit on on e lin e, u se t h r ee per iods, . . . , followed by
Re tu rn or En te r t o in dica t e t h a t t h e st a t em en t con t in u es on t h e n ext lin e. F or
exa m ple,
s = 1 - 1/ 2 + 1/ 3 - 1/ 4 + 1/ 5 - 1/ 6 + 1/ 7 . . .
- 1/ 8 + 1/ 9 - 1/ 10 + 1/ 11 - 1/ 12;

Bla n k spa ces a r ou n d t h e = , + , a n d - sign s a r e opt ion a l, bu t t h ey im pr ove


r ea da bilit y.

Com m a nd Line Editing


Va r iou s a r r ow a n d con t r ol keys on you r keyboa r d a llow you t o r eca ll, edit , a n d
r eu se com m a n ds you h a ve t yped ea r lier . F or exa m ple, su ppose you m ist a ken ly
en t er
r ho = ( 1 + s qt ( 5) ) / 2

You h a ve m isspelled s q r t . MATLAB r espon ds wit h


Un d e f i n e d f u n c t i o n o r v a r i a b l e ' s q t ' .

In st ea d of r et ypin g t h e en t ir e lin e, sim ply pr ess t h e ↑ key. Th e m isspelled


com m a n d is r edispla yed. Use t h e ← key t o m ove t h e cu r sor over a n d in ser t t h e
m issin g r . Repea t ed u se of t h e ↑ key r eca lls ea r lier lin es. Typin g a few
ch a r a ct er s a n d t h en t h e ↑ key fin ds a pr eviou s lin e t h a t begin s wit h t h ose
ch a r a ct er s. You ca n a lso copy pr eviou sly execu t ed com m a n ds fr om t h e
Com m a n d H ist or y. F or m or e in for m a t ion , see “Com m a n d H ist or y” on pa ge 3-7.

4-30
Controlling Command W indow Input and Output

Th e list of a va ila ble com m a n d lin e edit in g keys is differ en t on differ en t


com pu t er s. E xper im en t t o see wh ich of t h e followin g keys is a va ila ble on you r
m a ch in e. (Ma n y of t h ese keys will be fa m ilia r t o u ser s of t h e E m a cs edit or .)

↑ Ctrl+p Reca ll pr eviou s lin e

↓ Ctrl+n Reca ll n ext lin e

← Ctrl+b Move ba ck on e ch a r a ct er

→ Ctrl+f Move for wa r d on e ch a r a ct er

Ctrl+ → Ctrl+r Move r igh t on e wor d

Ctrl+ ← Ctrl+l Move left on e wor d


Ho m e Ctrl+a Move t o begin n in g of lin e
En d Ctrl+e Move t o en d of lin e
Es c Ctrl+u Clea r lin e
Del Ctrl+d Delet e ch a r a ct er a t cu r sor
Backspace Ctrl+h Delet e ch a r a ct er befor e cu r sor
Ctrl+k Delet e t o en d of lin e

Tab Completion
MATLAB com plet es t h e n a m e of a fu n ct ion , va r ia ble, filen a m e, or h a n dle
gr a ph ics pr oper t y if you t ype t h e fir st few let t er s a n d t h en pr ess t h e Ta b key.
If t h er e is a u n iqu e n a m e, t h e n a m e is a u t om a t ica lly com plet ed. If t h er e is m or e
t h a n on e n a m e t h a t st a r t s wit h t h e let t er s you t yped, pr ess t h e Ta b key a ga in
t o see a list of t h e possibilit ies.

4-31
4 Getting Started

4-32
5

Gr a ph ics
B a s ic P lo ttin g . . . . . . . . . . . . . . . . . . . 5-2

Ed itin g P lo ts . . . . . . . . . . . . . . . . . . . 5-14

Me s h a n d S u rfa c e P lo ts . . . . . . . . . . . . . . . 5-18

Im a g e s . . . . . . . . . . . . . . . . . . . . . . 5-24

P rin tin g Gra p h ic s . . . . . . . . . . . . . . . . . 5-26

Ha n d le Gra p h ic s . . . . . . . . . . . . . . . . . . 5-28

Gra p h ic s U s e r In te rfa c e s . . . . . . . . . . . . . . 5-35

An im a tio n s . . . . . . . . . . . . . . . . . . . . 5-37
5 Graphics

Ba sic Plotting
MATLAB h a s ext en sive fa cilit ies for displa yin g vect or s a n d m a t r ices a s
gr a ph s, a s well a s a n n ot a t in g a n d pr in t in g t h ese gr a ph s. Th is sect ion descr ibes
a few of t h e m ost im por t a n t gr a ph ics fu n ct ion s a n d pr ovides exa m ples of som e
t ypica l a pplica t ion s.

For M ore Informa tion “Gr a ph ics” a n d “3-D Visu a liza t ion ” pr ovide in -dept h
cover a ge of MATLAB gr a ph ics a n d visu a liza t ion t ools. Access t h ese fr om H elp.

Crea ting a Plot


Th e p l o t fu n ct ion h a s differ en t for m s, depen din g on t h e in pu t a r gu m en t s. If y
is a vect or , p l o t ( y ) pr odu ces a piecewise lin ea r gr a ph of t h e elem en t s of y
ver su s t h e in dex of t h e elem en t s of y . If you specify t wo vect or s a s a r gu m en t s,
p l o t ( x , y ) pr odu ces a gr a ph of y ver su s x .

F or exa m ple, t h ese st a t em en t s u se t h e c o l o n oper a t or t o cr ea t e a vect or of x


va lu es r a n gin g fr om zer o t o 2 π , com pu t e t h e sin e of t h ese va lu es, a n d plot t h e
r esu lt .
x = 0: pi / 100: 2*pi ;
y = s i n( x) ;
pl ot ( x, y)

Now la bel t h e a xes a n d a dd a t it le. Th e ch a r a ct er s \ p i cr ea t e t h e sym bol π .


xl a be l ( ' x = 0: 2\ pi ' )
y l a b e l ( ' Si n e o f x ' )
t i t l e ( ' Pl o t o f t h e Si n e Fu n c t i o n ' , ' Fo n t Si z e ' , 1 2 )

5-2
Basic Plotting

Plot of the Sine Function


1

0.8

0.6

0.4

0.2
Sine of x

−0.2

−0.4

−0.6

−0.8

−1
0 1 2 3 4 5 6 7
x = 0:2π

M ultiple Da ta Sets in O ne Gra ph


Mu lt iple x -y pa ir a r gu m en t s cr ea t e m u lt iple gr a ph s wit h a sin gle ca ll t o p l o t .
MATLAB a u t om a t ica lly cycles t h r ou gh a pr edefin ed (bu t u ser set t a ble) list of
color s t o a llow discr im in a t ion bet ween ea ch set of da t a . F or exa m ple, t h ese
st a t em en t s plot t h r ee r ela t ed fu n ct ion s of x , ea ch cu r ve in a sepa r a t e
dist in gu ish in g color .
y2 = s i n( x- . 25) ;
y3 = s i n( x- . 5) ;
pl ot ( x, y, x, y2, x, y3)

Th e l e g e n d com m a n d pr ovides a n ea sy wa y t o iden t ify t h e in dividu a l plot s.


l e ge nd( ' s i n( x) ' , ' s i n( x- . 25) ' , ' s i n( x- . 5) ' )

5-3
5 Graphics

1
sin(x)
sin(x−.25)
0.8 sin(x−.5)

0.6

0.4

0.2

−0.2

−0.4

−0.6

−0.8

−1
0 1 2 3 4 5 6 7

For M ore Informa tion See “Defin in g t h e Color of Lin es for P lot t in g” in
“Axes P r oper t ies” in H elp.

Specify ing Line Sty les a nd Colors


It is possible t o specify color , lin e st yles, a n d m a r ker s (su ch a s plu s sign s or
cir cles) wh en you plot you r da t a u sin g t h e p l o t com m a n d.
p l o t ( x , y , ' c o l o r _ s t y l e _ ma r k e r ' )

c o l o r _ s t y l e _ ma r k e r is a st r in g con t a in in g fr om on e t o fou r ch a r a ct er s
(en closed in sin gle qu ot a t ion m a r ks) con st r u ct ed fr om a color , a lin e st yle, a n d
a m a r ker t ype:

• Color st r in gs a r e ' c ' , ' m' , ' y ' , ' r ' , ' g ' , ' b ' , ' w' , a n d ' k ' . Th ese cor r espon d
t o cya n , m a gen t a , yellow, r ed, gr een , blu e, wh it e, a n d bla ck.

5-4
Basic Plotting

• Lin est yle st r in gs a r e ' - ' for solid, ' - - ' for da sh ed, ' : ' for dot t ed, ' - . ' for
da sh -dot , a n d ' n o n e ' for n o lin e.
• Th e m a r ker t ypes a r e ' +' , ' o ' , ' * ' , a n d ' x ' a n d t h e filled m a r ker t ypes ' s '
for squ a r e, ' d ' for dia m on d, ' ^ ' for u p t r ia n gle, ' v ' for down t r ia n gle, ' >'
for r igh t t r ia n gle, ' <' for left t r ia n gle, ' p ' for pen t a gr a m , ' h ' for h exa gr a m ,
a n d n o n e for n o m a r ker .

You ca n a lso edit color , lin e st yle, a n d m a r ker s in t er a ct ively. See “E dit in g
P lot s” on pa ge 5-14 for m or e in for m a t ion .

Plotting Lines a nd M a rk ers


If you specify a m a r ker t ype bu t n ot a lin est yle, MATLAB dr a ws on ly t h e
m a r ker . F or exa m ple,
pl ot ( x, y, ' ks ' )

plot s bla ck squ a r es a t ea ch da t a poin t , bu t does n ot con n ect t h e m a r ker s wit h


a lin e.
Th e st a t em en t
p l o t ( x , y , ' r : +' )

plot s a r ed dot t ed lin e a n d pla ces plu s sign m a r ker s a t ea ch da t a poin t . You
m a y wa n t t o u se fewer da t a poin t s t o plot t h e m a r ker s t h a n you u se t o plot t h e
lin es. Th is exa m ple plot s t h e da t a t wice u sin g a differ en t n u m ber of poin t s for
t h e dot t ed lin e a n d m a r ker plot s.
x1 = 0: pi / 100: 2*pi ;
x2 = 0: pi / 10: 2*pi ;
p l o t ( x 1 , s i n ( x 1 ) , ' r : ' , x 2 , s i n ( x 2 ) , ' r +' )

5-5
5 Graphics

0.8

0.6

0.4

0.2

−0.2

−0.4

−0.6

−0.8

−1
0 1 2 3 4 5 6 7

For M ore Informa tion See “Ba sic P lot t in g” in H elp for m or e exa m ples of
plot t in g opt ion s.

Im a gina r y a nd Com plex Da ta


Wh en t h e a r gu m en t s t o p l o t a r e com plex, t h e im a gin a r y pa r t is ign or ed except
wh en p l o t is given a sin gle com plex a r gu m en t . F or t h is specia l ca se, t h e
com m a n d is a sh or t cu t for a plot of t h e r ea l pa r t ver su s t h e im a gin a r y pa r t .
Th er efor e,
p l o t ( Z)

wh er e Z is a com plex vect or or m a t r ix, is equ iva len t t o


p l o t ( r e a l ( Z) , i ma g ( Z) )

5-6
Basic Plotting

F or exa m ple,
t = 0: pi / 10: 2*pi ;
pl ot ( e xp( i *t ) , ' - o' )
a xi s e qua l

dr a ws a 20-sided polygon wit h lit t le cir cles a t t h e ver t ices. Th e com m a n d,


a x i s e q u a l , m a kes t h e in dividu a l t ick m a r k in cr em en t s on t h e x- a n d y-a xes
t h e sa m e len gt h , wh ich m a kes t h is plot m or e cir cu la r in a ppea r a n ce.

0.8

0.6

0.4

0.2

−0.2

−0.4

−0.6

−0.8

−1
−1 −0.5 0 0.5 1

Adding Plots to a n Ex isting Gra ph


Th e h o l d com m a n d en a bles you t o a dd plot s t o a n exist in g gr a ph . Wh en you
t ype
hol d on

MATLAB does n ot r epla ce t h e exist in g gr a ph wh en you issu e a n ot h er plot t in g


com m a n d; it a dds t h e n ew da t a t o t h e cu r r en t gr a ph , r esca lin g t h e a xes if
n ecessa r y.

5-7
5 Graphics

F or exa m ple, t h ese st a t em en t s fir st cr ea t e a con t ou r plot of t h e p e a k s fu n ct ion ,


t h en su per im pose a pseu docolor plot of t h e sa m e fu n ct ion .
[ x, y, z ] = pe a ks ;
c ont our ( x, y, z , 20, ' k' )
hol d on
pc ol or ( x, y, z )
s ha di ng i nt e r p
hol d of f

Th e h o l d o n com m a n d ca u ses t h e p c o l o r plot t o be com bin ed wit h t h e c o n t o u r


plot in on e figu r e.

For M ore Informa tion See “Cr ea t in g Specia lized P lot s” in H elp for
in for m a t ion on a va r iet y of gr a ph t ypes.

5-8
Basic Plotting

Figure W indow s
Gr a ph in g fu n ct ion s a u t om a t ica lly open a n ew figu r e win dow if t h er e a r e n o
figu r e win dows a lr ea dy on t h e scr een . If a figu r e win dow exist s, MATLAB u ses
t h a t win dow for gr a ph ics ou t pu t . If t h er e a r e m u lt iple figu r e win dows open ,
MATLAB t a r get s t h e on e t h a t is design a t ed t h e “cu r r en t figu r e” (t h e la st figu r e
u sed or clicked in ).
To m a ke a n exist in g figu r e win dow t h e cu r r en t figu r e, you ca n click t h e m ou se
wh ile t h e poin t er is in t h a t win dow or you ca n t ype
f i gur e ( n)

wh er e n is t h e n u m ber in t h e figu r e t it le ba r . Th e r esu lt s of su bsequ en t


gr a ph ics com m a n ds a r e displa yed in t h is win dow.
To open a n ew figu r e win dow a n d m a ke it t h e cu r r en t figu r e, t ype
f i gur e

Clearing the Figure for a New Plot


Wh en a figu r e a lr ea dy exist s, m ost plot t in g com m a n ds clea r t h e a xes a n d u se
t h is figu r e t o cr ea t e t h e n ew plot . H owever , t h ese com m a n ds do n ot r eset figu r e
pr oper t ies, su ch a s t h e ba ckgr ou n d color or t h e color m a p. If you h a ve set a n y
figu r e pr oper t ies in t h e pr eviou s plot , you m a y wa n t t o u se t h e c l f com m a n d
wit h t h e r e s e t opt ion ,
cl f r es et

befor e cr ea t in g you r n ew plot t o set t h e figu r e’s pr oper t ies t o t h eir defa u lt s.

For M ore Informa tion See “F igu r e P r oper t ies” a n d t h e r efer en ce pa ge for
t h e f i g u r e com m a n d in H elp. See “Con t r ollin g Gr a ph ics Ou t pu t ” for
in for m a t ion on h ow t o con t r ol pr oper t y r eset t in g in you r gr a ph ics pr ogr a m s.

M ultiple Plots in O ne Figure


Th e s u b p l o t com m a n d en a bles you t o displa y m u lt iple plot s in t h e sa m e
win dow or pr in t t h em on t h e sa m e piece of pa per . Typin g
s u b p l o t ( m, n , p )

5-9
5 Graphics

pa r t it ion s t h e figu r e win dow in t o a n m-by-n m a t r ix of sm a ll su bplot s a n d select s


t h e p t h su bplot for t h e cu r r en t plot . Th e plot s a r e n u m ber ed a lon g fir st t h e t op
r ow of t h e figu r e win dow, t h en t h e secon d r ow, a n d so on . F or exa m ple, t h ese
st a t em en t s plot da t a in fou r differ en t su br egion s of t h e figu r e win dow.
t = 0: pi / 10: 2*pi ;
[ X, Y, Z] = c y l i n d e r ( 4 * c o s ( t ) ) ;
s u b p l o t ( 2 , 2 , 1 ) ; me s h ( X)
s u b p l o t ( 2 , 2 , 2 ) ; me s h ( Y)
s u b p l o t ( 2 , 2 , 3 ) ; me s h ( Z)
s u b p l o t ( 2 , 2 , 4 ) ; me s h ( X, Y, Z)

Controlling the Ax es
Th e a x i s com m a n d su ppor t s a n u m ber of opt ion s for set t in g t h e sca lin g,
or ien t a t ion , a n d a spect r a t io of plot s. You ca n a lso set t h ese opt ion s
in t er a ct ively. See “E dit in g P lot s” on pa ge 5-14 for m or e in for m a t ion .

5-10
Basic Plotting

Setting Axis Limits


By defa u lt , MATLAB fin ds t h e m a xim a a n d m in im a of t h e da t a t o ch oose t h e
a xis lim it s t o spa n t h is r a n ge. Th e a x i s com m a n d en a bles you t o specify you r
own lim it s
a x i s ( [ x mi n x ma x y mi n y ma x ] )

or for t h r ee-dim en sion a l gr a ph s,


a x i s ( [ x mi n x ma x y mi n y ma x z mi n z ma x ] )

Use t h e com m a n d
a xi s a ut o

t o r e-en a ble MATLAB’s a u t om a t ic lim it select ion .

Setting Axis Aspect Ratio


a x i s a lso en a bles you t o specify a n u m ber of pr edefin ed m odes. F or exa m ple,
a xi s s qua r e

m a kes t h e x-a xes a n d y-a xes t h e sa m e len gt h .


a xi s e qua l

m a kes t h e in dividu a l t ick m a r k in cr em en t s on t h e x- a n d y-a xes t h e sa m e


len gt h . Th is m ea n s
pl ot ( e xp( i *[ 0: pi / 10: 2*pi ] ) )

followed by eit h er a x i s s q u a r e or a x i s e q u a l t u r n s t h e ova l in t o a pr oper


cir cle.
a x i s a u t o n o r ma l

r et u r n s t h e a xis sca lin g t o it s defa u lt , a u t om a t ic m ode.

Setting Axis Visibility


You ca n u se t h e a x i s com m a n d t o m a ke t h e a xis visible or in visible.
a xi s on

m a kes t h e a xis visible. Th is is t h e defa u lt .


a xi s of f

5-11
5 Graphics

m a kes t h e a xis in visible.

Setting Grid Lines


Th e g r i d com m a n d t oggles gr id lin es on a n d off. Th e st a t em en t
gr i d on

t u r n s t h e gr id lin es on a n d
gr i d of f

t u r n s t h em ba ck off a ga in .

For M ore Informa tion See t h e a x i s a n d a x e s r efer en ce pa ges a n d “Axes


P r oper t ies” in H elp.

Ax is La bels a nd Titles
Th e x l a b e l , y l a b e l , a n d z l a b e l com m a n ds a dd x-, y -, a n d z-a xis la bels. Th e
t i t l e com m a n d a dds a t it le a t t h e t op of t h e figu r e a n d t h e t e x t fu n ct ion
in ser t s t ext a n ywh er e in t h e figu r e. A su bset of TeX n ot a t ion pr odu ces Gr eek
let t er s. You ca n a lso set t h ese opt ion s in t er a ct ively. See “E dit in g P lot s” on
pa ge 5-14 for m or e in for m a t ion .
t = - pi : pi / 100: pi ;
y = s i n( t ) ;
pl ot ( t , y)
a xi s ( [ - pi pi - 1 1] )
xl a be l ( ' - \ pi \ l e q {\ i t t } \ l e q \ pi ' )
yl a be l ( ' s i n( t ) ' )
t i t l e ( ' Gr a p h o f t h e s i n e f u n c t i o n ' )
t e x t ( 1 , - 1 / 3 , ' { \ i t No t e t h e o d d s y mme t r y . } ' )

5-12
Basic Plotting

Graph of the sine function


1

0.8

0.6

0.4

0.2
sin(t)

−0.2

Note the odd symmetry.


−0.4

−0.6

−0.8

−1
−3 −2 −1 0 1 2 3
−π ≤ t ≤ π

For M ore Informa tion See “For m a t t in g Gr a ph s” in H elp for a ddit ion a l
in for m a t ion on a ddin g la bels a n d a n n ot a t ion s t o you r gr a ph s.

Sa ving a Figure
To sa ve a figu r e, select S a v e fr om t h e F ile m en u . Th e figu r e is sa ved a s a
F IG-file, wh ich you ca n loa d u sin g t h e o p e n or h g l o a d com m a n ds.

Formats for Importing into Other Applications


You ca n expor t t h e figu r e a s a st a n da r d gr a ph ics for m a t , su ch a s TIF F , for u se
wit h ot h er a pplica t ion s. To do t h is, select Ex p o rt fr om t h e F ile m en u . You ca n
a lso expor t figu r es fr om t h e com m a n d lin e u sin g t h e s a v e a s a n d p r i n t
com m a n ds.

5-13
5 Graphics

Editing Plots
MATLAB for m a t s a gr a ph t o pr ovide r ea da bilit y, set t in g t h e sca le of a xes,
in clu din g t ick m a r ks on t h e a xes, a n d u sin g color a n d lin e st yle t o dist in gu ish
t h e plot s in t h e gr a ph . H owever , if you a r e cr ea t in g pr esen t a t ion gr a ph ics, you
m a y wa n t t o ch a n ge t h is defa u lt for m a t t in g or a dd descr ipt ive la bels, t it les,
legen ds a n d ot h er a n n ot a t ion s t o h elp expla in you r da t a .
MATLAB su ppor t s t wo wa ys t o edit t h e plot s you cr ea t e:

• Usin g t h e m ou se t o select a n d edit object s in t er a ct ively


• Usin g MATLAB fu n ct ion s a t t h e com m a n d-lin e or in a n M-file

Intera ctive Plot Editing


If you en a ble plot edit in g m ode in t h e MATLAB figu r e win dow, you ca n per for m
poin t -a n d-click edit in g of t h e object s in you r gr a ph . In t h is m ode, you select t h e
object or object s you wa n t t o edit by dou ble-clickin g on it . Th is st a r t s t h e
P r oper t y E dit or , wh ich pr ovides a ccess t o pr oper t ies of t h e object t h a t con t r ol
it s a ppea r a n ce a n d beh a vior .
F or m or e in for m a t ion a bou t in t er a ct ive edit in g, see “Usin g P lot E dit in g Mode”
on pa ge 5-15. F or in for m a t ion a bou t edit in g object pr oper t ies in plot edit in g
m ode, see “Usin g t h e P r oper t y E dit or ” on pa ge 5-16.

N ote P lot edit in g m ode pr ovides a n a lt er n a t ive wa y t o a ccess t h e pr oper t ies


of MATLAB gr a ph ic object s. H owever, you ca n on ly a ccess a su bset of object
pr oper t ies t h r ou gh t h is m ech a n ism . You m a y n eed t o u se a com bin a t ion of
in t er a ct ive edit in g a n d com m a n d lin e edit in g t o a ch ieve t h e effect you desir e.

Using Functions to Edit Gra phs


If you pr efer t o wor k fr om t h e MATLAB com m a n d lin e or if you a r e cr ea t in g a n
M-file, you ca n u se MATLAB com m a n ds t o edit t h e gr a ph s you cr ea t e. Ta kin g
a dva n t a ge of MATLAB’s H a n dle Gr a ph ics syst em , you ca n u se t h e s e t a n d g e t
com m a n ds t o ch a n ge t h e pr oper t ies of t h e object s in a gr a ph . F or m or e
in for m a t ion a bou t u sin g com m a n d lin e, see “H a n dle Gr a ph ics” on pa ge 5-28.

5-14
Editing Plots

Using Plot Editing M ode


Th e MATLAB figu r e win dow su ppor t s a poin t -a n d-click st yle edit in g m ode t h a t
you ca n u se t o cu st om ize t h e a ppea r a n ce of you r gr a ph . Th e followin g
illu st r a t ion sh ows a figu r e win dow wit h plot edit in g m ode en a bled a n d la bels
t h e m a in plot edit in g m ode fea t u r es.

Use these toolbar buttonsto add text, arrows, and linesto a graph.
Click thisbuttontostart plot
edit mode.

UsetheEdit, Insert, andTools


menusto add objectsor edit
existingobjectsinthegraph.

Double-click onanobject to
select it.

Positionlabels, legends, and


other objectsbyclickingand
dragging them.

Accessobject-specific plot
edit functionsthrough
context-sensitive pop-up
menus.

5-15
5 Graphics

Using the Proper ty Editor


In plot edit in g m ode, you ca n u se a gr a ph ica l u ser in t er fa ce, ca lled t h e P r oper t y
E dit or , t o edit t h e pr oper t ies of object s in t h e gr a ph . Th e P r oper t y E dit or
pr ovides a ccess t o m a n y pr oper t ies of t h e r oot , figu r e, a xes, lin e, ligh t , pa t ch ,
im a ge, su r fa ces r ect a n gle, a n d t ext object s. F or exa m ple, u sin g t h e P r oper t y
E dit or , you ca n ch a n ge t h e t h ickn ess of a lin e, a dd t it les a n d a xes la bels, a dd
ligh t s, a n d per for m m a n y ot h er plot edit in g t a sks.
Th is figu r e sh ows t h e com pon en t s of t h e P r oper t y E dit or in t er fa ce.

Use these buttonsto move back and forth among the graphicsobjectsyou have edited.

Use the navigation bar to select


the object you want to edit.

Click onatabtoviewagroup
of properties.

Click here to viewa list of


valuesfor thisfield.

Check thischeckbox toseethe


effect of your changesasyou
make them.

Click OKto apply your changes


and dismissthe Property Editor.

Click Cancel to dismissthe Property Editor Click Apply to apply your changes Click Help to get information about
without applying your changes. without dismissing the Property Editor. particular properties.

5-16
Editing Plots

Starting the Property Editor


You st a r t t h e P r oper t y E dit or by dou ble-clickin g on a n object in a gr a ph , su ch
a s a lin e, or by r igh t -clickin g on a n object a n d select in g t h e P ro p e rtie s opt ion
fr om t h e object ’s con t ext m en u .
You ca n a lso st a r t t h e P r oper t y E dit or by select in g eit h er t h e F ig u re
P ro p e rtie s , Ax e s P ro p e rtie s , or Cu rre n t Obje c t P ro p e rtie s fr om t h e figu r e
win dow Ed it m en u . Th ese opt ion s a u t om a t ica lly en a ble plot edit in g m ode, if it
is n ot a lr ea dy en a bled.
On ce you st a r t t h e P r oper t y E dit or , keep it open t h r ou gh ou t a n edit in g session .
It pr ovides a ccess t o a ll t h e object s in t h e gr a ph . If you click on a n ot h er object
in t h e gr a ph , t h e P r oper t y E dit or displa ys t h e set of pa n els a ssocia t ed wit h t h a t
object t ype. You ca n a lso u se t h e P r oper t y E dit or ’s n a viga t ion ba r t o select a n
object in t h e gr a ph t o edit .
To sa ve a figu r e, select S a v e fr om t h e F ile m en u . To sa ve it u sin g a gr a ph ics
for m a t , su ch a s TIF F , for u se wit h ot h er a pplica t ion s, select Ex p o rt fr om t h e
F ile m en u . You ca n a lso sa ve fr om t h e com m a n d lin e – u se t h e s a v e a s
com m a n d, in clu din g a n y opt ion s t o sa ve t h e figu r e in a differ en t for m a t .

5-17
5 Graphics

M esh a nd Surfa ce Plots


MATLAB defin es a su r fa ce by t h e z-coor din a t es of poin t s a bove a gr id in t h e x-y
pla n e, u sin g st r a igh t lin es t o con n ect a dja cen t poin t s. Th e me s h a n d s u r f
plot t in g fu n ct ion s displa y su r fa ces in t h r ee dim en sion s. me s h pr odu ces
wir efr a m e su r fa ces t h a t color on ly t h e lin es con n ect in g t h e defin in g poin t s.
s u r f displa ys bot h t h e con n ect in g lin es a n d t h e fa ces of t h e su r fa ce in color .

V isua lizing Functions of Tw o Va ria bles


To displa y a fu n ct ion of t wo va r ia bles, z = f (x,y):

• Gen er a t e X a n d Y m a t r ices con sist in g of r epea t ed r ows a n d colu m n s,


r espect ively, over t h e dom a in of t h e fu n ct ion .
• Use X a n d Y t o eva lu a t e a n d gr a ph t h e fu n ct ion .

Th e me s h g r i d fu n ct ion t r a n sfor m s t h e dom a in specified by a sin gle vect or or


t wo vect or s x a n d y in t o m a t r ices X a n d Y for u se in eva lu a t in g fu n ct ion s of t wo
va r ia bles. Th e r ows of X a r e copies of t h e vect or x a n d t h e colu m n s of Y a r e
copies of t h e vect or y .

Example – Graphing the sinc Function


Th is exa m ple eva lu a t es a n d gr a ph s t h e t wo-dim en sion a l sin c fu n ct ion , sin (r)/r,
bet ween t h e x a n d y dir ect ion s. R is t h e dist a n ce fr om or igin , wh ich is a t t h e
cen t er of t h e m a t r ix. Addin g e p s (a MATLAB com m a n d t h a t r et u r n s t h e
sm a llest floa t in g-poin t n u m ber on you r syst em ) a voids t h e in det er m in a t e 0/0
a t t h e or igin .
[ X, Y] = me s h g r i d ( - 8 : . 5 : 8 ) ;
R = s q r t ( X. ^ 2 + Y. ^ 2 ) + e p s ;
Z = s i n ( R) . / R;
me s h ( X, Y, Z, ' Ed g e Co l o r ' , ' b l a c k ' )

5-18
Mesh and Surface Plots

0.8

0.6

0.4

0.2

−0.2

−0.4
10

5 10
5
0
0
−5
−5
−10 −10

By defa u lt , MATLAB color s t h e m esh u sin g t h e cu r r en t color m a p. H owever ,


t h is exa m ple u ses a sin gle-color ed m esh by specifyin g t h e Ed g e Co l o r su r fa ce
pr oper t y. See t h e s u r f a c e r efer en ce pa ge for a list of a ll su r fa ce pr oper t ies.
You ca n cr ea t e a t r a n spa r en t m esh by disa blin g h idden lin e r em ova l.
hi dde n of f

See t h e h i d d e n r efer en ce pa ge for m or e in for m a t ion on t h is opt ion .

Example – Colored Surface Plots


A su r fa ce plot is sim ila r t o a m esh plot except t h e r ect a n gu la r fa ces of t h e
su r fa ce a r e color ed. Th e color of t h e fa ces is det er m in ed by t h e va lu es of Z a n d
t h e color m a p (a c o l o r ma p is a n or der ed list of color s). Th ese st a t em en t s gr a ph
t h e sin c fu n ct ion a s a su r fa ce plot , select a color m a p, a n d a dd a color ba r t o
sh ow t h e m a ppin g of da t a t o color .
s u r f ( X, Y, Z)
c o l o r ma p h s v
c ol or ba r

5-19
5 Graphics

0.8
1

0.8

0.6 0.6

0.4

0.2 0.4

−0.2 0.2

−0.4
10

5 10 0

5
0
0
−5
−5 −0.2
−10 −10

See t h e c o l o r ma p r efer en ce pa ge for in for m a t ion on color m a ps.

For M ore Informa tion See “Cr ea t in g 3-D Gr a ph s” in H elp for m or e


in for m a t ion on su r fa ce plot s.

Transparent Surfaces
You ca n m a ke t h e fa ces of a su r fa ce t r a n spa r en t t o a va r yin g degr ee.
Tr a n spa r en cy (r efer r ed t o a s t h e a lph a va lu e) ca n be specified for t h e wh ole
object or ca n be ba sed on a n a lph a m a p, wh ich beh a ves in a wa y a n a logou s t o
color m a ps. F or exa m ple,
s u r f ( X, Y, Z)
c o l o r ma p h s v
a l pha ( . 4)

5-20
Mesh and Surface Plots

pr odu ces a su r fa ce wit h a fa ce a lph a va lu e of 0.4. Alph a va lu es r a n ge fr om 0


(com plet ely t r a n spa r en t ) t o 1 (n ot t r a n spa r en t ).

For M ore Informa tion See “Tr a n spa r en cy” in H elp for m or e in for m a t ion on
u sin g t h is fea t u r e.

Surface Plots with Lighting


Ligh t in g is t h e t ech n iqu e of illu m in a t in g a n object wit h a dir ect ion a l ligh t
sou r ce. In cer t a in ca ses, t h is t ech n iqu e ca n m a ke su bt le differ en ces in su r fa ce
sh a pe ea sier t o see. Ligh t in g ca n a lso be u sed t o a dd r ea lism t o
t h r ee-dim en sion a l gr a ph s.
Th is exa m ple u ses t h e sa m e su r fa ce a s t h e pr eviou s exa m ples, bu t color s it r ed
a n d r em oves t h e m esh lin es. A ligh t object is t h en a dded t o t h e left of t h e
“ca m er a ” (t h a t is t h e loca t ion in spa ce fr om wh er e you a r e viewin g t h e su r fa ce).
s u r f ( X, Y, Z, ' Fa c e Co l o r ' , ' r e d ' , ' Ed g e Co l o r ' , ' n o n e ' )
c a ml i g h t l e f t ; l i g h t i n g p h o n g

5-21
5 Graphics

Manipulating the Surface


Th e Ca m er a Toolba r pr ovides a wa y t o in t er a ct ively explor e 3-D gr a ph ics.
Displa y t h e t oolba r by select in g Ca m e ra To o lba r fr om t h e figu r e win dow's
Vie w m en u . H er e is t h e t oolba r wit h t h e or bit ca m er a t ool select ed:

Th e Ca m er a Toolba r en a bles you t o m ove t h e ca m er a a r ou n d t h e su r fa ce object ,


zoom , a dd a ligh t , a n d per for m ot h er viewin g oper a t ion s wit h ou t issu in g
com m a n ds. Th e followin g pict u r e sh ows t h e su r fa ce viewed by or bit in g t h e
ca m er a t owa r d t h e bot t om . A scen e ligh t h a s been a dded t o illu m in a t e t h e
u n der side of t h e su r fa ce, wh ich is n ot lit by t h e ligh t a dded in t h e pr eviou s
sect ion .

5-22
Mesh and Surface Plots

For M ore Informa tion See t h e “Ligh t in g a s a Visu a liza t ion Tool” a n d “View
Con t r ol wit h t h e Ca m er a Toolba r ” in H elp for in for m a t ion on t h ese
t ech n iqu es.

5-23
5 Graphics

Ima ges
Two-dim en sion a l a r r a ys ca n be displa yed a s im ages, wh er e t h e a r r a y elem en t s
det er m in e br igh t n ess or color of t h e im a ges. F or exa m ple, t h e st a t em en t s
l oa d dur e r
wh o s
Na me Si z e By t e s Cl a s s

X 648x509 2638656 doubl e a r r a y


c a pt i on 2x28 112 c ha r a r r a y
ma p 128x3 3072 doubl e a r r a y

loa d t h e file d u r e r . ma t , a ddin g t h r ee va r ia bles t o t h e wor kspa ce. Th e m a t r ix X


is a 648-by-509 m a t r ix a n d ma p is a 128-by-3 m a t r ix t h a t is t h e color m a p for t h is
im a ge.

N ote MAT-files, su ch a s d u r e r . ma t , a r e bin a r y files t h a t ca n be cr ea t ed on


on e pla t for m a n d la t er r ea d by MATLAB on a differ en t pla t for m .

Th e elem en t s of X a r e in t eger s bet ween 1 a n d 128, wh ich ser ve a s in dices in t o


t h e color m a p, ma p . Th en
i ma g e ( X)
c o l o r ma p ( ma p )
a x i s i ma g e

r epr odu ces Dü r er ’s et ch in g sh own a t t h e begin n in g of t h is book. A h igh


r esolu t ion sca n of t h e m a gic squ a r e in t h e u pper r igh t cor n er is a va ila ble in
a n ot h er file. Type
l oa d de t a i l

a n d t h en u se t h e u pa r r ow key on you r keyboa r d t o r eexecu t e t h e i ma g e ,


c o l o r ma p , a n d a x i s com m a n ds. Th e st a t em en t
c o l o r ma p ( h o t )

a dds som e u n u su a l color in g t o t h e sixt een t h cen t u r y et ch in g. Th e fu n ct ion h o t


gen er a t es a color m a p con t a in in g sh a des of r eds, or a n ges, a n d yellows.

5-24
Images

Typica lly a given im a ge m a t r ix h a s a specific color m a p a ssocia t ed wit h it . See


t h e c o l o r ma p r efer en ce pa ge for a list of ot h er pr edefin ed color m a ps.

For M ore Informa tion See “Displa yin g Bit -Ma pped Im a ges” in H elp for
in for m a t ion on t h e im a ge pr ocessin g ca pa bilit ies of MATLAB.

5-25
5 Graphics

Printing Gra phics


You ca n pr in t a MATLAB figu r e dir ect ly on a pr in t er con n ect ed t o you r
com pu t er or you ca n expor t t h e figu r e t o on e of t h e st a n da r d gr a ph ic file
for m a t s su ppor t ed by MATLAB. Th er e a r e t wo wa ys t o pr in t a n d expor t
figu r es:

• Usin g t h e P rin t opt ion u n der t h e F ile m en u


• Usin g t h e p r i n t com m a n d

Printing from the Menu


Th er e a r e fou r m en u opt ion s u n der t h e F ile m en u t h a t per t a in t o pr in t in g:

• Th e P a g e S e tu p opt ion displa ys a dia log box t h a t en a bles you t o a dju st


ch a r a ct er ist ics of t h e figu r e on t h e pr in t ed pa ge.
• Th e P rin t S e tu p opt ion displa ys a dia log box t h a t set s pr in t in g defa u lt s, bu t
does n ot a ct u a lly pr in t t h e figu r e.
• Th e P rin t P re v ie w opt ion en a bles you t o view t h e figu r e t h e wa y it will look
on t h e pr in t ed pa ge.
• Th e P rin t opt ion displa ys a dia log box t h a t let s you select st a n da r d pr in t in g
opt ion s a n d pr in t t h e figu r e.

Gen er a lly, u se P rin t P re v ie w t o det er m in e wh et h er t h e pr in t ed ou t pu t is wh a t


you wa n t . If n ot , u se t h e P a g e S e tu p dia log box t o ch a n ge t h e ou t pu t set t in gs.
Select t h e P a g e S e tu p dia log box He lp bu t t on t o displa y in for m a t ion on h ow t o
set u p t h e pa ge.

Exporting Figure to Graphics Files


Th e Ex p o rt opt ion u n der t h e F ile m en u en a bles you t o expor t t h e figu r e t o a
va r iet y of st a n da r d gr a ph ics file for m a t s.

Using the Print Command


Th e p r i n t com m a n d pr ovides m or e flexibilit y in t h e t ype of ou t pu t sen t t o t h e
pr in t er a n d a llows you t o con t r ol pr in t in g fr om M-files. Th e r esu lt ca n be sen t
dir ect ly t o you r defa u lt pr in t er or st or ed in a specified file. A wide va r iet y of
ou t pu t for m a t s, in clu din g TIF F , J P E G, a n d P ost Scr ipt , is a va ila ble.
F or exa m ple, t h is st a t em en t sa ves t h e con t en t s of t h e cu r r en t figu r e win dow a s
color E n ca psu la t ed Level 2 P ost Scr ipt in t h e file ca lled ma g i c s q u a r e . e p s . It

5-26
Printing Graphics

a lso in clu des a TIF F pr eview, wh ich en a bles m ost wor d pr ocessor s t o displa y
t h e pict u r e
p r i n t - d e p s c 2 - t i f f ma g i c s q u a r e . e p s

To sa ve t h e sa m e figu r e a s a TIF F file wit h a r esolu t ion of 200 dpi, u se t h e


com m a n d
p r i n t - d t i f f - r 2 0 0 ma g i c s q u a r e . t i f f

If you t ype p r i n t on t h e com m a n d lin e,


pr i nt

MATLAB pr in t s t h e cu r r en t figu r e on you r defa u lt pr in t er .

For M ore Informa tion See t h e p r i n t com m a n d r efer en ce pa ge a n d “Ba sic


P r in t in g a n d E xpor t in g” in H elp for m or e in for m a t ion on pr in t in g.

5-27
5 Graphics

Ha ndle Gra phics


Wh en you u se a plot t in g com m a n d, MATLAB cr ea t es t h e gr a ph u sin g va r iou s
gr a ph ics object s, su ch a s lin es, t ext , a n d su r fa ces (see “Gr a ph ics Object s” on
pa ge 5-28 for a com plet e list ). All gr a ph ics object s h a ve pr oper t ies t h a t con t r ol
t h e a ppea r a n ce a n d beh a vior of t h e object . MATLAB en a bles you t o qu er y t h e
va lu e of ea ch pr oper t y a n d set t h e va lu e of m ost pr oper t ies.
Wh en ever MATLAB cr ea t es a gr a ph ics object , it a ssign s a n iden t ifier (ca lled a
h a n dle) t o t h e object . You ca n u se t h is h a n dle t o a ccess t h e object ’s pr oper t ies.
H a n dle Gr a ph ics is u sefu l if you wa n t t o:

• Modify t h e a ppea r a n ce of gr a ph s.
• Cr ea t e cu st om plot t in g com m a n ds by wr it in g M-files t h a t cr ea t e a n d
m a n ipu la t e object s dir ect ly.

Gra phics O bjects


Gr a ph ics object s a r e t h e ba sic elem en t s u sed t o displa y gr a ph ics a n d u ser
in t er fa ce elem en t s. Th is t a ble list s t h e gr a ph ics object s.

O bject Description

Root Top of t h e h ier a r ch y cor r espon din g t o t h e com pu t er


scr een

F igu r e Win dow u sed t o displa y gr a ph ics a n d u ser in t er fa ces

Axes Axes for displa yin g gr a ph s in a figu r e

Uicon t r ol User in t er fa ce con t r ol t h a t execu t es a fu n ct ion in


r espon se t o u ser in t er a ct ion

Uim en u User-defin ed figu r e win dow m en u

Uicon t ext m en u Pop-u p m en u in voked by r igh t clickin g on a gr a ph ics


object

Im a ge Two-dim en sion a l pixel-ba sed pict u r e

5-28
Handle Graphics

O bject Description

Ligh t Ligh t sou r ces t h a t a ffect t h e color in g of pa t ch a n d


su r fa ce object s

Lin e Lin e u sed by fu n ct ion s su ch a s p l o t , p l o t 3 , s e mi l o g x

Pa t ch F illed polygon wit h edges

Rect a n gle Two-dim en sion a l sh a pe va r yin g fr om r ect a n gles t o


ova ls

Su r fa ce Th r ee-dim en sion a l r epr esen t a t ion of m a t r ix da t a


cr ea t ed by plot t in g t h e va lu e of t h e da t a a s h eigh t s
a bove t h e x-y pla n e

Text Ch a r a ct er st r in g

Object Hierarchy
Th e object s a r e or ga n ized in a t r ee st r u ct u r ed h ier a r ch y r eflect in g t h eir
in t er depen den ce. F or exa m ple, lin e object s r equ ir e a xes object s a s a fr a m e of
r efer en ce. In t u r n , a xes object s exist on ly wit h in figu r e object s. Th is dia gr a m
illu st r a t es t h e t r ee st r u ct u r e.

Root

Figure

Axes Uicontrol Uimenu Uicontextmenu

Image Light Line Patch Rectangle Surface Text

5-29
5 Graphics

Creating Objects
E a ch object h a s a n a ssocia t ed fu n ct ion t h a t cr ea t es t h e object . Th ese fu n ct ion s
h a ve t h e sa m e n a m e a s t h e object s t h ey cr ea t e. F or exa m ple, t h e t e x t fu n ct ion
cr ea t es t ext object s, t h e f i g u r e fu n ct ion cr ea t es figu r e object s, a n d so on .
MATLAB’s h igh -level gr a ph ics fu n ct ion s (like p l o t a n d s u r f ) ca ll t h e
a ppr opr ia t e low-level fu n ct ion t o dr a w t h eir r espect ive gr a ph ics. F or m or e
in for m a t ion a bou t a n object a n d a descr ipt ion of it s pr oper t ies, see t h e
r efer en ce pa ge for t h e object ’s cr ea t ion fu n ct ion . Object cr ea t ion fu n ct ion s h a ve
t h e sa m e n a m e a s t h e object . F or exa m ple, t h e object cr ea t ion fu n ct ion for a xes
object s is ca lled a x e s .

Commands for Working with Objects


Th is t a ble list s com m a n ds com m on ly u sed wh en wor kin g wit h object s.

Function Purpose

c opyobj Copy gr a ph ics object

de l e t e Delet e a n object

f i ndobj F in d t h e h a n dle of object s h a vin g specified pr oper t y va lu es

gc a Ret u r n t h e h a n dle of t h e cu r r en t a xes

gc f Ret u r n t h e h a n dle of t h e cu r r en t figu r e

gc o Ret u r n t h e h a n dle of t h e cu r r en t object

ge t Qu er y t h e va lu e of a n object s pr oper t ies

s et Set t h e va lu e of a n object s pr oper t ies

For M ore Informa tion See t h e “MATLAB F u n ct ion Refer en ce” in H elp for a
descr ipt ion of ea ch of t h ese fu n ct ion s.

5-30
Handle Graphics

Setting O bject Proper ties


All object pr oper t ies h a ve defa u lt va lu es. H owever , you m a y fin d it u sefu l t o
ch a n ge t h e set t in gs of som e pr oper t ies t o cu st om ize you r gr a ph . Th er e a r e t wo
wa ys t o set object pr oper t ies:

• Specify va lu es for pr oper t ies wh en you cr ea t e t h e object .


• Set t h e pr oper t y va lu e on a n object t h a t a lr ea dy exist s.

For M ore Informa tion See “H a n dle Gr a ph ics Object s” in H elp for
in for m a t ion on gr a ph ics object s.

Setting Properties from Plotting Commands


You ca n specify object pr oper t y va lu es a s a r gu m en t s t o object cr ea t ion
fu n ct ion s a s well a s wit h plot t in g fu n ct ion , su ch a s p l o t , me s h , a n d s u r f .
F or exa m ple, plot t in g com m a n ds t h a t cr ea t e lin es or su r fa ces en a ble you t o
specify pr oper t y n a m e/pr oper t y va lu e pa ir s a s a r gu m en t s. Th e com m a n d
p l o t ( x , y , ' Li n e Wi d t h ' , 1 . 5 )

plot s t h e da t a in t h e va r ia bles x a n d y u sin g lin es h a vin g a Li n e Wi d t h pr oper t y


set t o 1.5 poin t s (on e poin t = 1/72 in ch ). You ca n set a n y lin e object pr oper t y
t h is wa y.

Setting Properties of Existing Objects


To m odify t h e pr oper t y va lu es of exist in g object s, you ca n u se t h e s e t com m a n d
or , if plot edit in g m ode is en a bled, t h e P r oper t y E dit or . Th e P r oper t y E dit or
pr ovides a gr a ph ica l u ser in t er fa ce t o m a n y object pr oper t ies. Th is sect ion
descr ibes h ow t o u se t h e set com m a n d. See “Usin g t h e P r oper t y E dit or ” on
pa ge 5-16 for m or e in for m a t ion .
Ma n y plot t in g com m a n ds ca n r et u r n t h e h a n dles of t h e object s cr ea t ed so you
ca n m odify t h e object s u sin g t h e s e t com m a n d. F or exa m ple, t h ese st a t em en t s
plot a five-by-five m a t r ix (cr ea t in g five lin es, on e per colu m n ) a n d t h en set t h e
Ma r k e r t o a squ a r e a n d t h e Ma r k e r Fa c e Co l o r t o gr een .
h = p l o t ( ma g i c ( 5 ) ) ;
s e t ( h , ' Ma r k e r ' , ' s ' , Ma r k e r Fa c e Co l o r ' , ' g ' )

5-31
5 Graphics

In t h is ca se, h is a vect or con t a in in g five h a n dles, on e for ea ch of t h e five lin es


in t h e plot . Th e s e t st a t em en t set s t h e Ma r k e r a n d Ma r k e r Fa c e Co l o r pr oper t ies
of a ll lin es t o t h e sa m e va lu es.

Setting Multiple Property Values


If you wa n t t o set t h e pr oper t ies of ea ch lin e t o a differ en t va lu e, you ca n u se
cell a r r a ys t o st or e a ll t h e da t a a n d pa ss it t o t h e s e t com m a n d. F or exa m ple,
cr ea t e a plot a n d sa ve t h e lin e h a n dles.
h = p l o t ( ma g i c ( 5 ) ) ;

Su ppose you wa n t t o a dd differ en t m a r ker s t o ea ch lin e a n d color t h e m a r ker ’s


fa ce color t o t h e sa m e color a s t h e lin e. You n eed t o defin e t wo cell a r r a ys – on e
con t a in in g t h e pr oper t y n a m es a n d t h e ot h er con t a in in g t h e desir ed va lu es of
t h e pr oper t ies.
Th e p r o p _ n a me cell a r r a y con t a in s t wo elem en t s.
p r o p _ n a me ( 1 ) = { ' Ma r k e r ' } ;
p r o p _ n a me ( 2 ) = { ' Ma r k e r Fa c e Co l o r ' } ;

Th e p r o p _ v a l u e s cell a r r a y con t a in s 10 va lu es – five va lu es for t h e Ma r k e r


pr oper t y a n d five va lu es for t h e Ma r k e r Fa c e Co l o r pr oper t y. Not ice t h a t
p r o p _ v a l u e s is a t wo-dim en sion a l cell a r r a y. Th e fir st dim en sion in dica t es
wh ich h a n dle in h t h e va lu es a pply t o a n d t h e secon d dim en sion in dica t es
wh ich pr oper t y t h e va lu e is a ssign ed t o.
pr op_va l ue s ( 1, 1) = {' s ' };
pr op_va l ue s ( 1, 2) = {ge t ( h( 1) , ' Co l o r ' ) } ;
pr op_va l ue s ( 2, 1) = {' d' };
pr op_va l ue s ( 2, 2) = {ge t ( h( 2) , ' Co l o r ' ) } ;
pr op_va l ue s ( 3, 1) = {' o' };
pr op_va l ue s ( 3, 2) = {ge t ( h( 3) , ' Co l o r ' ) } ;
pr op_va l ue s ( 4, 1) = {' p' };
pr op_va l ue s ( 4, 2) = {ge t ( h( 4) , ' Co l o r ' ) } ;
pr op_va l ue s ( 5, 1) = {' h' };
pr op_va l ue s ( 5, 2) = {ge t ( h( 5) , ' Co l o r ' ) } ;

Th e Ma r k e r Fa c e Co l o r is a lwa ys a ssign ed t h e va lu e of t h e cor r espon din g lin e’s


color (obt a in ed by get t in g t h e lin e’s Co l o r pr oper t y wit h t h e g e t com m a n d).

5-32
Handle Graphics

Aft er defin in g t h e cell a r r a ys, ca ll s e t t o specify t h e n ew pr oper t y va lu es.


s e t ( h , p r o p _ n a me , p r o p _ v a l u e s )

25

20

15

10

0
1 1.5 2 2.5 3 3.5 4 4.5 5

For M ore Informa tion See “St r u ct u r es a n d Cell Ar r a ys” in H elp for
in for m a t ion on cell a r r a ys.

Finding the H a ndles of Ex isting O bjects


Th e f i n d o b j com m a n d en a bles you t o obt a in t h e h a n dles of gr a ph ics object s by
sea r ch in g for object s wit h pa r t icu la r pr oper t y va lu es. Wit h f i n d o b j you ca n
specify t h e va lu e of a n y com bin a t ion of pr oper t ies, wh ich m a kes it ea sy t o pick
on e object ou t of m a n y. F or exa m ple, you m a y wa n t t o fin d t h e blu e lin e wit h
squ a r e m a r ker h a vin g blu e fa ce color .

5-33
5 Graphics

You ca n a lso specify wh ich figu r es or a xes t o sea r ch , if t h er e is m or e t h a n on e.


Th e followin g sect ion s pr ovide exa m ples illu st r a t in g h ow t o u se f i n d o b j .

Finding All Objects of a Certain Type


Sin ce a ll object s h a ve a Ty p e pr oper t y t h a t iden t ifies t h e t ype of object , you ca n
fin d t h e h a n dles of a ll occu r r en ces of a pa r t icu la r t ype of object . F or exa m ple,
h = f i n d o b j ( ' Ty p e ' , ' l i n e ' ) ;

fin ds t h e h a n dles of a ll lin e object s.

Finding Objects with a Particular Property


You ca n specify m u lt iple pr oper t ies t o n a r r ow t h e sea r ch . F or exa m ple,
h = f i n d o b j ( ' Ty p e ' , ' l i n e ' , ' Co l o r ' , ' r ' , ' Li n e St y l e ' , ' : ' ) ;

fin ds t h e h a n dles of a ll r ed, dot t ed lin es.

Limiting the Scope of the Search


You ca n specify t h e st a r t in g poin t in t h e object h ier a r ch y by pa ssin g t h e h a n dle
of t h e st a r t in g figu r e or a xes a s t h e fir st a r gu m en t . F or exa m ple,
h = f i n d o b j ( g c a , ' Ty p e ' , ' t e x t ' , ' St r i n g ' , ' \ p i / 2 ' ) ;

fin ds t h e st r in g π ⁄ 2 on ly wit h in t h e cu r r en t a xes.

Using findobj as an Argument


Sin ce f i n d o b j r et u r n s t h e h a n dles it fin ds, you ca n u se it in pla ce of t h e h a n dle
a r gu m en t . F or exa m ple,
s e t ( f i n d o b j ( ' Ty p e ' , ' l i n e ' , ' Co l o r ' , ' r e d ' ) , ' Li n e St y l e ' , ' : ' )

fin ds a ll r ed lin es a n d set s t h eir lin e st yle t o dot t ed.

For M ore Informa tion See “Accessin g Object H a n dles” in H elp for m or e
in for m a t ion .

5-34
Graphics User Interfaces

Gra phics User Interfa ces


H er e is a sim ple exa m ple illu st r a t in g h ow t o u se H a n dle Gr a ph ics t o bu ild u ser
in t er fa ces. Th e st a t em en t
b = u i c o n t r o l ( ' St y l e ' , ' pus hbut t on' , . . .
' Un i t s ' , ' n o r ma l i z e d' , . . .
' Po s i t i o n ' , [ . 5 . 5 . 2 . 1] , . . .
' St r i n g ' , ' c l i c k he r e ' ) ;

cr ea t es a pu sh bu t t on in t h e cen t er of a figu r e win dow a n d r et u r n s a h a n dle t o


t h e n ew object . Bu t , so fa r , clickin g on t h e bu t t on does n ot h in g. Th e st a t em en t
s = ' s e t ( b , ' ' Po s i t i o n ' ' , [ . 8 * r a n d . 9 * r a n d . 2 . 1 ] ) ' ;

cr ea t es a st r in g con t a in in g a com m a n d t h a t a lt er s t h e pu sh bu t t on ’s posit ion .


Repea t ed execu t ion of
e va l ( s )

m oves t h e bu t t on t o r a n dom posit ion s. F in a lly,


s e t ( b , ' Ca l l b a c k ' , s )

in st a lls s a s t h e bu t t on ’s ca llba ck a ct ion , so ever y t im e you click on t h e bu t t on ,


it m oves t o a n ew posit ion .

Gra phica l User Inter fa ce Design Tools


MATLAB in clu des a set of la you t t ools t h a t sim plify t h e pr ocess of cr ea t in g
gr a ph ica l u ser in t er fa ces (GUIs). Th ese t ools in clu de:

• La you t E dit or – a dd a n d a r r a n ge object s in t h e figu r e win dow.


• Align m en t Tool – a lign object s wit h r espect t o ea ch ot h er .
• P r oper t y In spect or – in spect a n d set pr oper t y va lu es.
• Object Br owser – obser ve a h ier a r ch ica l list of t h e H a n dle Gr a ph ics object s
in t h e cu r r en t MATLAB session .
• Men u E dit or – cr ea t e win dow m en u s a n d con t ext m en u s.

Access t h ese t ools fr om t h e La you t E dit or . To st a r t t h e La you t E dit or , u se t h e


g u i d e com m a n d. F or exa m ple,
gui de

5-35
5 Graphics

displa ys a n em pt y la you t .
To loa d a n exist in g GUI for edit in g, t ype (t h e . f i g is n ot r equ ir ed)
g u i d e my g u i . f i g

or u se Op e n ... fr om t h e F ile m en u on t h e La you t E dit or .

For M ore Informa tion See “Cr ea t in g Gr a ph ica l User In t er fa ces” for m or e
in for m a t ion .

5-36
Animations

Anima tions
MATLAB pr ovides t wo wa ys of gen er a t in g m ovin g, a n im a t ed gr a ph ics:

• Con t in u a lly er a se a n d t h en r edr a w t h e object s on t h e scr een , m a kin g


in cr em en t a l ch a n ges wit h ea ch r edr a w.
• Sa ve a n u m ber of differ en t pict u r es a n d t h en pla y t h em ba ck a s a m ovie.

Era se M ode M ethod


Usin g t h e Er a s e Mo d e pr oper t y is a ppr opr ia t e for lon g sequ en ces of sim ple plot s
wh er e t h e ch a n ge fr om fr a m e t o fr a m e is m in im a l. H er e is a n exa m ple sh owin g
sim u la t ed Br own ia n m ot ion . Specify a n u m ber of poin t s, su ch a s
n = 20

a n d a t em per a t u r e or velocit y, su ch a s
s = . 02

Th e best va lu es for t h ese t wo pa r a m et er s depen d u pon t h e speed of you r


pa r t icu la r com pu t er . Gen er a t e n r a n dom poin t s wit h (x,y) coor din a t es bet ween
–1 ⁄ 2 a n d + 1 ⁄ 2 .
x = r a nd( n, 1) - 0. 5;
y = r a nd( n, 1) - 0. 5;

P lot t h e poin t s in a squ a r e wit h sides a t -1 a n d +1. Sa ve t h e h a n dle for t h e


vect or of poin t s a n d set it s Er a s e Mo d e t o x o r . Th is t ells t h e MATLAB gr a ph ics
syst em n ot t o r edr a w t h e en t ir e plot wh en t h e coor din a t es of on e poin t a r e
ch a n ged, bu t t o r est or e t h e ba ckgr ou n d color in t h e vicin it y of t h e poin t u sin g
a n “exclu sive or ” oper a t ion .
h = pl ot ( x, y, ' . ' ) ;
a xi s ( [ - 1 1 - 1 1] )
a xi s s qua r e
gr i d of f
s e t ( h , ' Er a s e Mo d e ' , ' x o r ' , ' Ma r k e r Si z e ' , 1 8 )

Now begin t h e a n im a t ion . H er e is a n in fin it e wh i l e loop, wh ich you ca n


even t u a lly exit by t ypin g Ctrl+c . E a ch t im e t h r ou gh t h e loop, a dd a sm a ll
a m ou n t of n or m a lly dist r ibu t ed r a n dom n oise t o t h e coor din a t es of t h e poin t s.

5-37
5 Graphics

Th en , in st ea d of cr ea t in g a n en t ir ely n ew plot , sim ply ch a n ge t h e XDa t a a n d


YDa t a pr oper t ies of t h e or igin a l plot .
wh i l e 1
d r a wn o w
x = x + s *r a ndn( n, 1) ;
y = y + s *r a ndn( n, 1) ;
s e t ( h , ' XDa t a ' , x , ' YDa t a ' , y )
e nd

H ow lon g does it t a ke for on e of t h e poin t s t o get ou t side of t h e squ a r e? H ow


lon g befor e a ll of t h e poin t s a r e ou t side t h e squ a r e?

0.8

0.6

0.4

0.2

−0.2

−0.4

−0.6

−0.8

−1
−1 −0.5 0 0.5 1

Crea ting M ovies


If you in cr ea se t h e n u m ber of poin t s in t h e Br own ia n m ot ion exa m ple t o
som et h in g like n = 3 0 0 a n d s = . 0 2 , t h e m ot ion is n o lon ger ver y flu id; it t a kes
t oo m u ch t im e t o dr a w ea ch t im e st ep. It becom es m or e effect ive t o sa ve a
pr edet er m in ed n u m ber of fr a m es a s bit m a ps a n d t o pla y t h em ba ck a s a m ovie.

5-38
Animations

F ir st , decide on t h e n u m ber of fr a m es, sa y


n f r a me s = 5 0 ;

Next , set u p t h e fir st plot a s befor e, except u sin g t h e defa u lt Er a s e Mo d e


(n o r ma l ).
x = r a nd( n, 1) - 0. 5;
y = r a nd( n, 1) - 0. 5;
h = pl ot ( x, y, ' . ' ) ;
s e t ( h , ' Ma r k e r Si z e ' , 1 8 ) ;
a xi s ( [ - 1 1 - 1 1] )
a xi s s qua r e
gr i d of f

Gen er a t e t h e m ovie a n d u se g e t f r a me t o ca pt u r e ea ch fr a m e.
f o r k = 1 : n f r a me s
x = x + s *r a ndn( n, 1) ;
y = y + s *r a ndn( n, 1) ;
s e t ( h , ' XDa t a ' , x , ' YDa t a ' , y )
M( k ) = g e t f r a me ;
e nd

F in a lly, pla y t h e m ovie 30 t im es.


mo v i e ( M, 3 0 )

5-39
5 Graphics

5-40
6
P r ogr a m m in g wit h
MATLAB
F lo w Co n tro l . . . . . . . . . . . . . . . . . . . . 6-2

Oth e r D a ta S tru c tu re s . . . . . . . . . . . . . . . 6-7

S c rip ts a n d F u n c tio n s . . . . . . . . . . . . . . . 6-17

D e m o n s tra tio n P ro g ra m s In c lu d e d w ith MATLAB . . . 6-27


6 Programming with MATLAB

Flow Control
MATLAB h a s sever a l flow con t r ol con st r u ct s:

• i f st a t em en t s
• s wi t c h st a t em en t s
• f o r loops
• wh i l e loops
• c o n t i n u e st a t em en t s
• b r e a k st a t em en t s

For M ore Informa tion See “P r ogr a m m in g a n d Da t a Types” in H elp for a


com plet e discu ssion a bou t pr ogr a m m in g in MATLAB.

if
Th e i f st a t em en t eva lu a t es a logica l expr ession a n d execu t es a gr ou p of
st a t em en t s wh en t h e expr ession is tru e. Th e opt ion a l e l s e i f a n d e l s e
keywor ds pr ovide for t h e execu t ion of a lt er n a t e gr ou ps of st a t em en t s. An e n d
keywor d, wh ich m a t ch es t h e i f , t er m in a t es t h e la st gr ou p of st a t em en t s. Th e
gr ou ps of st a t em en t s a r e delin ea t ed by t h e fou r keywor ds – n o br a ces or
br a cket s a r e in volved.
MATLAB’s a lgor it h m for gen er a t in g a m a gic squ a r e of or der n in volves t h r ee
differ en t ca ses: wh en n is odd, wh en n is even bu t n ot divisible by 4, or wh en n
is divisible by 4. Th is is descr ibed by
i f r e m( n , 2 ) ~= 0
M = o d d _ ma g i c ( n )
e l s e i f r e m( n , 4 ) ~= 0
M = s i n g l e _ e v e n _ ma g i c ( n )
el s e
M = d o u b l e _ e v e n _ ma g i c ( n )
e nd

In t h is exa m ple, t h e t h r ee ca ses a r e m u t u a lly exclu sive, bu t if t h ey wer en ’t , t h e


fir st tru e con dit ion wou ld be execu t ed.

6-2
Flow Control

It is im por t a n t t o u n der st a n d h ow r ela t ion a l oper a t or s a n d i f st a t em en t s wor k


wit h m a t r ices. Wh en you wa n t t o ch eck for equ a lit y bet ween t wo va r ia bles, you
m igh t u se
i f A == B, . . .

Th is is lega l MATLAB code, a n d does wh a t you expect wh en A a n d B a r e sca la r s.


Bu t wh en A a n d B a r e m a t r ices, A == B does n ot t est if t h ey a r e equ a l, it t est s
w h ere t h ey a r e equ a l; t h e r esu lt is a n ot h er m a t r ix of 0’s a n d 1’s sh owin g
elem en t -by-elem en t equ a lit y. In fa ct , if A a n d B a r e n ot t h e sa m e size, t h en
A == B is a n er r or .

Th e pr oper wa y t o ch eck for equ a lit y bet ween t wo va r ia bles is t o u se t h e


i s e q u a l fu n ct ion ,
i f i s e q u a l ( A, B) , . . .

H er e is a n ot h er exa m ple t o em ph a size t h is poin t . If A a n d B a r e sca la r s, t h e


followin g pr ogr a m will n ever r ea ch t h e u n expect ed sit u a t ion . Bu t for m ost
pa ir s of m a t r ices, in clu din g ou r m a gic squ a r es wit h in t er ch a n ged colu m n s,
n on e of t h e m a t r ix con dit ion s A > B, A < B or A == B is t r u e for all elem en t s
a n d so t h e e l s e cla u se is execu t ed.
if A > B
' gr e a t e r '
el s ei f A < B
' l es s '
e l s e i f A == B
' e qua l '
el s e
e r r o r ( ' Un e x p e c t e d s i t u a t i o n ' )
e nd

Sever a l fu n ct ion s a r e h elpfu l for r edu cin g t h e r esu lt s of m a t r ix com pa r ison s t o


sca la r con dit ion s for u se wit h i f , in clu din g
i s e qua l
i s e mp t y
al l
a ny

6-3
6 Programming with MATLAB

sw itch a nd ca se
Th e s wi t c h st a t em en t execu t es gr ou ps of st a t em en t s ba sed on t h e va lu e of a
va r ia ble or expr ession . Th e keywor ds c a s e a n d o t h e r wi s e delin ea t e t h e
gr ou ps. On ly t h e fir st m a t ch in g ca se is execu t ed. Th er e m u st a lwa ys be a n e n d
t o m a t ch t h e s wi t c h .
Th e logic of t h e m a gic squ a r es a lgor it h m ca n a lso be descr ibed by
s wi t c h ( r e m( n , 4 ) ==0 ) + ( r e m( n , 2 ) ==0 )
cas e 0
M = o d d _ ma g i c ( n )
cas e 1
M = s i n g l e _ e v e n _ ma g i c ( n )
cas e 2
M = d o u b l e _ e v e n _ ma g i c ( n )
o t h e r wi s e
e r r o r ( ' Th i s i s i mp o s s i b l e ' )
e nd

N ote Un like t h e C la n gu a ge s wi t c h st a t em en t , MATLAB’s s wi t c h does n ot


fa ll t h r ou gh . If t h e fir st ca se st a t em en t is tru e, t h e ot h er ca se st a t em en t s do
n ot execu t e. So, b r e a k st a t em en t s a r e n ot r equ ir ed.

for
Th e f o r loop r epea t s a gr ou p of st a t em en t s a fixed, pr edet er m in ed n u m ber of
t im es. A m a t ch in g e n d delin ea t es t h e st a t em en t s.
f or n = 3: 32
r ( n ) = r a n k ( ma g i c ( n ) ) ;
e nd
r

Th e sem icolon t er m in a t in g t h e in n er st a t em en t su ppr esses r epea t ed pr in t in g,


a n d t h e r a ft er t h e loop displa ys t h e fin a l r esu lt .

6-4
Flow Control

It is a good idea t o in den t t h e loops for r ea da bilit y, especia lly wh en t h ey a r e


n est ed.
f or i = 1: m
f or j = 1: n
H( i , j ) = 1 / ( i +j ) ;
e nd
e nd

w hile
Th e wh i l e loop r epea t s a gr ou p of st a t em en t s a n in defin it e n u m ber of t im es
u n der con t r ol of a logica l con dit ion . A m a t ch in g e n d delin ea t es t h e st a t em en t s.
H er e is a com plet e pr ogr a m , illu st r a t in g wh i l e , i f , e l s e , a n d e n d , t h a t u ses
in t er va l bisect ion t o fin d a zer o of a polyn om ia l.
a = 0; f a = - I nf ;
b = 3; f b = I nf ;
wh i l e b - a > e p s * b
x = ( a +b ) / 2 ;
f x = x^ 3- 2*x- 5;
i f s i g n ( f x ) == s i g n ( f a )
a = x; f a = f x;
el s e
b = x; f b = f x;
e nd
e nd
x
3
Th e r esu lt is a r oot of t h e polyn om ia l x – 2 x – 5 , n a m ely
x =
2. 09455148154233

Th e ca u t ion s in volvin g m a t r ix com pa r ison s t h a t a r e discu ssed in t h e sect ion on


t h e i f st a t em en t a lso a pply t o t h e wh i l e st a t em en t .

continue
Th e c o n t i n u e st a t em en t pa sses con t r ol t o t h e n ext it er a t ion of t h e f o r or wh i l e
loop in wh ich it a ppea r s, skippin g a n y r em a in in g st a t em en t s in t h e body of t h e

6-5
6 Programming with MATLAB

loop. In n est ed loops, c o n t i n u e pa sses con t r ol t o t h e n ext it er a t ion of t h e f o r


or wh i l e loop en closin g it .
Th e exa m ple below sh ows a c o n t i n u e loop t h a t cou n t s t h e lin es of code in t h e
file, ma g i c . m, skippin g a ll bla n k lin es a n d com m en t s. A c o n t i n u e st a t em en t is
u sed t o a dva n ce t o t h e n ext lin e in ma g i c . m wit h ou t in cr em en t in g t h e cou n t
wh en ever a bla n k lin e or com m en t lin e is en cou n t er ed.
f i d = f o p e n ( ' ma g i c . m' , ' r ' ) ;
c ount = 0;
wh i l e ~f e o f ( f i d )
l i ne = f ge t l ( f i d) ;
i f i s e mp t y ( l i n e ) | s t r n c mp ( l i n e , ' %' , 1 )
c ont i nue
e nd
c ount = c ount + 1;
e nd
d i s p ( s p r i n t f ( ' %d l i n e s ' , c o u n t ) ) ;

brea k
Th e b r e a k st a t em en t let s you exit ea r ly fr om a f o r or wh i l e loop. In n est ed
loops, b r e a k exit s fr om t h e in n er m ost loop on ly.
H er e is a n im pr ovem en t on t h e exa m ple fr om t h e pr eviou s sect ion . Wh y is t h is
u se of b r e a k a good idea ?
a = 0; f a = - I nf ;
b = 3; f b = I nf ;
wh i l e b - a > e p s * b
x = ( a +b ) / 2 ;
f x = x^ 3- 2*x- 5;
i f f x == 0
br e a k
e l s e i f s i g n ( f x ) == s i g n ( f a )
a = x; f a = f x;
el s e
b = x; f b = f x;
e nd
e nd
x

6-6
Other Data Structures

O ther Da ta Structures
Th is sect ion in t r odu ces you t o som e ot h er da t a st r u ct u r es in MATLAB,
in clu din g:

• Mu lt idim en sion a l a r r a ys
• Cell a r r a ys
• Ch a r a ct er s a n d t ext
• St r u ct u r es

For M ore Informa tion For a com plet e discu ssion of MATLAB’s da t a
st r u ct u r es, see “P r ogr a m m in g a n d Da t a Types” in H elp.

M ultidim ensiona l Arra y s


Mu lt idim en sion a l a r r a ys in MATLAB a r e a r r a ys wit h m or e t h a n t wo
su bscr ipt s. Th ey ca n be cr ea t ed by ca llin g z e r o s , o n e s , r a n d , or r a n d n wit h
m or e t h a n t wo a r gu m en t s. F or exa m ple,
R = r a ndn( 3, 4, 5) ;

cr ea t es a 3-by-4-by-5 a r r a y wit h a t ot a l of 3x4x5 = 60 n or m a lly dist r ibu t ed


r a n dom elem en t s.
A t h r ee-dim en sion a l a r r a y m igh t r epr esen t t h r ee-dim en sion a l ph ysica l da t a ,
sa y t h e t em per a t u r e in a r oom , sa m pled on a r ect a n gu la r gr id. Or , it m igh t
(k )
r epr esen t a sequ en ce of m a t r ices, A , or sa m ples of a t im e-depen den t m a t r ix,
A (t ) . In t h ese la t t er ca ses, t h e (i, j)t h elem en t of t h e k t h m a t r ix, or t h e t k t h
m a t r ix, is den ot ed by A( i , j , k ) .
MATLAB’s a n d Dü r er ’s ver sion s of t h e m a gic squ a r e of or der 4 differ by a n
in t er ch a n ge of t wo colu m n s. Ma n y differ en t m a gic squ a r es ca n be gen er a t ed by
in t er ch a n gin g colu m n s. Th e st a t em en t
p = p e r ms ( 1 : 4 ) ;

6-7
6 Programming with MATLAB

gen er a t es t h e 4! = 24 per m u t a t ion s of 1 : 4 . Th e k t h per m u t a t ion is t h e r ow


vect or , p ( k , : ) . Th en
A = ma g i c ( 4 ) ;
M = z e r os ( 4, 4, 24) ;
f or k = 1: 24
M( : , : , k ) = A( : , p ( k , : ) ) ;
e nd

st or es t h e sequ en ce of 24 m a gic squ a r es in a t h r ee-dim en sion a l a r r a y, M. Th e


size of Mis
s i z e ( M)

a ns =
4 4 24

13 16 2 3

8 5 11 10

12 9 7 6
.
.. 1 4 14 15

16 3 2 13

16 28 1131 130 8

10 182 1 17 1 06 12
16 2 3 13
6 1 21 174 165 1
5 11 10 8
15 1 14 15
9 7 6 12

4 14 15 1

It t u r n s ou t t h a t t h e t h ir d m a t r ix in t h e sequ en ce is Dü r er ’s.
M( : , : , 3 )

a ns =
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1

6-8
Other Data Structures

Th e st a t em en t
s u m( M, d )

com pu t es su m s by va r yin g t h e d t h su bscr ipt . So


s u m( M, 1 )

is a 1-by-4-by-24 a r r a y con t a in in g 24 copies of t h e r ow vect or


34 34 34 34

and
s u m( M, 2 )

is a 4-by-1-by-24 a r r a y con t a in in g 24 copies of t h e colu m n vect or


34
34
34
34

F in a lly,
S = s u m( M, 3 )

a dds t h e 24 m a t r ices in t h e sequ en ce. Th e r esu lt h a s size 4-by-4-by-1, so it looks


like a 4-by-4 a r r a y.
S =
204 204 204 204
204 204 204 204
204 204 204 204
204 204 204 204

Cell Arra y s
Cell a r r a ys in MATLAB a r e m u lt idim en sion a l a r r a ys wh ose elem en t s a r e
copies of ot h er a r r a ys. A cell a r r a y of em pt y m a t r ices ca n be cr ea t ed wit h t h e
c e l l fu n ct ion . Bu t , m or e oft en , cell a r r a ys a r e cr ea t ed by en closin g a
m iscella n eou s collect ion of t h in gs in cu r ly br a ces, { } . Th e cu r ly br a ces a r e a lso
u sed wit h su bscr ipt s t o a ccess t h e con t en t s of va r iou s cells. F or exa m ple,
C = { A s u m( A) p r o d ( p r o d ( A) ) }

6-9
6 Programming with MATLAB

pr odu ces a 1-by-3 cell a r r a y. Th e t h r ee cells con t a in t h e m a gic squ a r e, t h e r ow


vect or of colu m n su m s, a n d t h e pr odu ct of a ll it s elem en t s. Wh en C is displa yed,
you see
C =
[ 4x4 doubl e ] [ 1x4 doubl e ] [ 20922789888000]

Th is is beca u se t h e fir st t wo cells a r e t oo la r ge t o pr in t in t h is lim it ed spa ce, bu t


t h e t h ir d cell con t a in s on ly a sin gle n u m ber , 16!, so t h er e is r oom t o pr in t it .
H er e a r e t wo im por t a n t poin t s t o r em em ber . F ir st , t o r et r ieve t h e con t en t s of
on e of t h e cells, u se su bscr ipt s in cu r ly br a ces. F or exa m ple, C{ 1 } r et r ieves t h e
m a gic squ a r e a n d C{ 3 } is 16!. Secon d, cell a r r a ys con t a in copies of ot h er a r r a ys,
n ot poin ters t o t h ose a r r a ys. If you su bsequ en t ly ch a n ge A, n ot h in g h a ppen s t o
C.

Th r ee-dim en sion a l a r r a ys ca n be u sed t o st or e a sequ en ce of m a t r ices of t h e


sam e size. Cell a r r a ys ca n be u sed t o st or e a sequ en ce of m a t r ices of d ifferen t
sizes. F or exa m ple,
M = c e l l ( 8, 1) ;
f or n = 1: 8
M{ n } = ma g i c ( n ) ;
e nd
M

pr odu ces a sequ en ce of m a gic squ a r es of differ en t or der .


M=
[ 1]
[ 2x2 doubl e]
[ 3x3 doubl e]
[ 4x4 doubl e]
[ 5x5 doubl e]
[ 6x6 doubl e]
[ 7x7 doubl e]
[ 8x8 doubl e]

6-10
Other Data Structures

64 2 3 61 60 6 7 57
9 55 54 12 13 51 50 16

17 47 46 20 21 43 42 24

40 26 27 37 36 30 31 33

32 34 35 29 28 38 39 25

41 23 22 44 45 19 18 48

49 15 14 52 53 11 10 56

8 58 59 5 4 62 63 1
.
..

16 2 3 13

5 11 10 8

9 7 6 12

4 14 15 1

8 1 6

3 5 7

4 9 2
1 3

4 2
1

You ca n r et r ieve ou r old fr ien d wit h


M{ 4 }

Cha ra cters a nd Tex t


E n t er t ext in t o MATLAB u sin g sin gle qu ot es. F or exa m ple,
s = ' He l l o '

Th e r esu lt is n ot t h e sa m e kin d of n u m er ic m a t r ix or a r r a y we h a ve been


dea lin g wit h u p t o n ow. It is a 1-by-5 ch a r a ct er a r r a y.

6-11
6 Programming with MATLAB

In t er n a lly, t h e ch a r a ct er s a r e st or ed a s n u m ber s, bu t n ot in floa t in g-poin t


for m a t . Th e st a t em en t
a = doubl e ( s )

con ver t s t h e ch a r a ct er a r r a y t o a n u m er ic m a t r ix con t a in in g floa t in g-poin t


r epr esen t a t ion s of t h e ASCII codes for ea ch ch a r a ct er . Th e r esu lt is
a =
72 101 108 108 111

Th e st a t em en t
s = c ha r ( a )

r ever ses t h e con ver sion .


Con ver t in g n u m ber s t o ch a r a ct er s m a kes it possible t o in vest iga t e t h e va r iou s
fon t s a va ila ble on you r com pu t er . Th e pr in t a ble ch a r a ct er s in t h e ba sic ASCII
ch a r a ct er set a r e r epr esen t ed by t h e in t eger s 3 2 : 1 2 7 . (Th e in t eger s less t h a n
32 r epr esen t n on pr in t a ble con t r ol ch a r a ct er s.) Th ese in t eger s a r e a r r a n ged in
a n a ppr opr ia t e 6-by-16 a r r a y wit h
F = r e s ha pe ( 32: 127, 16, 6) ' ;

Th e pr in t a ble ch a r a ct er s in t h e ext en ded ASCII ch a r a ct er set a r e r epr esen t ed


by F+1 2 8 . Wh en t h ese in t eger s a r e in t er pr et ed a s ch a r a ct er s, t h e r esu lt
depen ds on t h e fon t cu r r en t ly bein g u sed. Type t h e st a t em en t s
c h a r ( F)
c h a r ( F+1 2 8 )

a n d t h en va r y t h e fon t bein g u sed for t h e MATLAB Com m a n d Win dow. Select


P re fe re n c e s fr om t h e F ile m en u . Be su r e t o t r y t h e S y m bo l a n d Win g d in g s
fon t s, if you h a ve t h em on you r com pu t er . H er e is on e exa m ple of t h e kin d of
ou t pu t you m igh t obt a in .

6-12
Other Data Structures

! " # $ %&' ( ) * +, - . /
0 1 2 3 4 5 6 7 8 9 : ; <=>?
@ABCDEFGHI J KLMNO
PQRSTUVWXYZ[ \ ] ^ _
` a b c d e f g h i j k l mn o
p q r s t u v wx y z { | } ~

† ¢ £ § • ¶ ß ®©™´ ¨ ÆØ
¥ ª º æø
¿ ¡ ¬ ƒ « » … ÀÃÕŒœ
- —“ ” ‘ ’ ÿ Ÿ⁄ ¤ ‹ › fifl
‡ · ‚ „ ‰ÂÊÁËÈÍ Î Ï Ì ÓÔ
ÒÚÛÙı ˆ ˜ ¯ ˘ ˙ ˚ ¸ ˝ ˛ ˇ

Con ca t en a t ion wit h squ a r e br a cket s join s t ext va r ia bles t oget h er in t o la r ger
st r in gs. Th e st a t em en t
h = [s, ' wo r l d ' ]

join s t h e st r in gs h or izon t a lly a n d pr odu ces


h =
He l l o wo r l d

Th e st a t em en t
v = [ s ; ' wo r l d ' ]

join s t h e st r in gs ver t ica lly a n d pr odu ces


v =
He l l o
wo r l d

Not e t h a t a bla n k h a s t o be in ser t ed befor e t h e ‘w’ in h a n d t h a t bot h wor ds in v


h a ve t o h a ve t h e sa m e len gt h . Th e r esu lt in g a r r a ys a r e bot h ch a r a ct er a r r a ys;
h is 1-by-11 a n d v is 2-by-5.

To m a n ipu la t e a body of t ext con t a in in g lin es of differ en t len gt h s, you h a ve t wo


ch oices – a pa dded ch a r a ct er a r r a y or a cell a r r a y of st r in gs. Th e c h a r fu n ct ion
a ccept s a n y n u m ber of lin es, a dds bla n ks t o ea ch lin e t o m a ke t h em a ll t h e
sa m e len gt h , a n d for m s a ch a r a ct er a r r a y wit h ea ch lin e in a sepa r a t e r ow. F or
exa m ple,

6-13
6 Programming with MATLAB

S = c h a r ( ' A' , ' r o l l i n g ' , ' s t o n e ' , ' g a t h e r s ' , ' mo me n t u m. ' )

pr odu ces a 5-by-9 ch a r a ct er a r r a y.


S =
A
r ol l i ng
s t one
ga t he r s
mo me n t u m.

Th er e a r e en ou gh bla n ks in ea ch of t h e fir st fou r r ows of S t o m a ke a ll t h e r ows


t h e sa m e len gt h . Alt er n a t ively, you ca n st or e t h e t ext in a cell a r r a y. F or
exa m ple,
C = { ' A' ; ' r o l l i n g ' ; ' s t o n e ' ; ' g a t h e r s ' ; ' mo me n t u m. ' }

is a 5-by-1 cell a r r a y.
C =
' A'
' r ol l i ng'
' s t one '
' ga t he r s '
' mo me n t u m. '

You ca n con ver t a pa dded ch a r a ct er a r r a y t o a cell a r r a y of st r in gs wit h


C = c e l l s t r ( S)

a n d r ever se t h e pr ocess wit h


S = c h a r ( C)

Structures
St r u ct u r es a r e m u lt idim en sion a l MATLAB a r r a ys wit h elem en t s a ccessed by
t ext u a l field d esign ators. F or exa m ple,
S. n a me = ' Ed Pl u m' ;
S. s c o r e = 8 3 ;
S. g r a d e = ' B+'

cr ea t es a sca la r st r u ct u r e wit h t h r ee fields.

6-14
Other Data Structures

S =
n a me : ' Ed Pl u m'
s c or e : 83
g r a d e : ' B+'

Like ever yt h in g else in MATLAB, st r u ct u r es a r e a r r a ys, so you ca n in ser t


a ddit ion a l elem en t s. In t h is ca se, ea ch elem en t of t h e a r r a y is a st r u ct u r e wit h
sever a l fields. Th e fields ca n be a dded on e a t a t im e,
S( 2 ) . n a me = ' To n i Mi l l e r ' ;
S( 2 ) . s c o r e = 9 1 ;
S( 2 ) . g r a d e = ' A- ' ;

or , a n en t ir e elem en t ca n be a dded wit h a sin gle st a t em en t .


S( 3 ) = s t r u c t ( ' n a me ' , ' J e r r y Ga r c i a ' , . . .
' s c o r e ' , 7 0 , ' g r a d e ' , ' C' )

Now t h e st r u ct u r e is la r ge en ou gh t h a t on ly a su m m a r y is pr in t ed.
S =
1 x 3 s t r u c t a r r a y wi t h f i e l d s :
n a me
s c or e
gr a de

Th er e a r e sever a l wa ys t o r ea ssem ble t h e va r iou s fields in t o ot h er MATLAB


a r r a ys. Th ey a r e a ll ba sed on t h e n ot a t ion of a com m a separated list. If you t ype
S. s c o r e

it is t h e sa m e a s t ypin g
S( 1 ) . s c o r e , S( 2 ) . s c o r e , S( 3 ) . s c o r e

Th is is a com m a sepa r a t ed list . Wit h ou t a n y ot h er pu n ct u a t ion , it is n ot ver y


u sefu l. It a ssign s t h e t h r ee scor es, on e a t a t im e, t o t h e defa u lt va r ia ble a n s a n d
du t ifu lly pr in t s ou t t h e r esu lt of ea ch a ssign m en t . Bu t wh en you en close t h e
expr ession in squ a r e br a cket s,
[ S. s c o r e ]

it is t h e sa m e a s
[ S( 1 ) . s c o r e , S( 2 ) . s c o r e , S( 3 ) . s c o r e ]

6-15
6 Programming with MATLAB

wh ich pr odu ces a n u m er ic r ow vect or con t a in in g a ll of t h e scor es.


a ns =
83 91 70

Sim ila r ly, t ypin g


S. n a me

ju st a ssign s t h e n a m es, on e a t t im e, t o a n s . Bu t en closin g t h e expr ession in


cu r ly br a ces,
{ S. n a me }

cr ea t es a 1-by-3 cell a r r a y con t a in in g t h e t h r ee n a m es.


a ns =
' Ed Pl u m' ' To n i Mi l l e r ' ' J e r r y Ga r c i a '

An d
c h a r ( S. n a me )

ca lls t h e c h a r fu n ct ion wit h t h r ee a r gu m en t s t o cr ea t e a ch a r a ct er a r r a y fr om


t h e n a me fields,
a ns =
Ed Pl u m
To n i Mi l l e r
J e r r y Ga r c i a

6-16
Scripts and Functions

Scripts a nd Functions
MATLAB is a power fu l pr ogr a m m in g la n gu a ge a s well a s a n in t er a ct ive
com pu t a t ion a l en vir on m en t . F iles t h a t con t a in code in t h e MATLAB la n gu a ge
a r e ca lled M-files. You cr ea t e M-files u sin g a t ext edit or , t h en u se t h em a s you
wou ld a n y ot h er MATLAB fu n ct ion or com m a n d.
Th er e a r e t wo kin ds of M-files:

• Scr ipt s, wh ich do n ot a ccept in pu t a r gu m en t s or r et u r n ou t pu t a r gu m en t s.


Th ey oper a t e on da t a in t h e wor kspa ce.
• F u n ct ion s, wh ich ca n a ccept in pu t a r gu m en t s a n d r et u r n ou t pu t a r gu m en t s.
In t er n a l va r ia bles a r e loca l t o t h e fu n ct ion .

If you ’r e a n ew MATLAB pr ogr a m m er , ju st cr ea t e t h e M-files t h a t you wa n t t o


t r y ou t in t h e cu r r en t dir ect or y. As you develop m or e of you r own M-files, you
will wa n t t o or ga n ize t h em in t o ot h er dir ect or ies a n d per son a l t oolboxes t h a t
you ca n a dd t o MATLAB’s sea r ch pa t h .
If you du plica t e fu n ct ion n a m es, MATLAB execu t es t h e on e t h a t occu r s fir st in
t h e sea r ch pa t h .
To view t h e con t en t s of a n M-file, for exa m ple, my f u n c t i o n . m, u se
t y p e my f u n c t i o n

Scripts
Wh en you in voke a script, MATLAB sim ply execu t es t h e com m a n ds fou n d in
t h e file. Scr ipt s ca n oper a t e on exist in g da t a in t h e wor kspa ce, or t h ey ca n
cr ea t e n ew da t a on wh ich t o oper a t e. Alt h ou gh scr ipt s do n ot r et u r n ou t pu t
a r gu m en t s, a n y va r ia bles t h a t t h ey cr ea t e r em a in in t h e wor kspa ce, t o be u sed
in su bsequ en t com pu t a t ion s. In a ddit ion , scr ipt s ca n pr odu ce gr a ph ica l ou t pu t
u sin g fu n ct ion s like p l o t .
F or exa m ple, cr ea t e a file ca lled ma g i c r a n k . m t h a t con t a in s t h ese MATLAB
com m a n ds.
% I n v e s t i g a t e t h e r a n k o f ma g i c s q u a r e s
r = z e r os ( 1, 32) ;
f or n = 3: 32
r ( n ) = r a n k ( ma g i c ( n ) ) ;
e nd

6-17
6 Programming with MATLAB

r
ba r ( r )

Typin g t h e st a t em en t
ma g i c r a n k

ca u ses MATLAB t o execu t e t h e com m a n ds, com pu t e t h e r a n k of t h e fir st 30


m a gic squ a r es, a n d plot a ba r gr a ph of t h e r esu lt . Aft er execu t ion of t h e file is
com plet e, t h e va r ia bles n a n d r r em a in in t h e wor kspa ce.

35

30

25

20

15

10

0
0 5 10 15 20 25 30 35

Functions
F u n ct ion s a r e M-files t h a t ca n a ccept in pu t a r gu m en t s a n d r et u r n ou t pu t
a r gu m en t s. Th e n a m e of t h e M-file a n d of t h e fu n ct ion sh ou ld be t h e sa m e.
F u n ct ion s oper a t e on va r ia bles wit h in t h eir own wor kspa ce, sepa r a t e fr om t h e
wor kspa ce you a ccess a t t h e MATLAB com m a n d pr om pt .
A good exa m ple is pr ovided by r a n k . Th e M-file r a n k . m is a va ila ble in t h e
dir ect or y
t o o l b o x / ma t l a b / ma t f u n

6-18
Scripts and Functions

You ca n see t h e file wit h


t ype r a nk

H er e is t h e file.
f u n c t i o n r = r a n k ( A, t o l )
% RANK Ma t r i x r a n k .
% RANK( A) p r o v i d e s a n e s t i ma t e o f t h e n u mb e r o f l i n e a r l y
% i n d e p e n d e n t r o ws o r c o l u mn s o f a ma t r i x A.
% RANK( A, t o l ) i s t h e n u mb e r o f s i n g u l a r v a l u e s o f A
% t ha t a r e l a r ge r t ha n t ol .
% RANK( A) u s e s t h e d e f a u l t t o l = ma x ( s i z e ( A) ) * n o r m( A) * e p s .

s = s v d ( A) ;
i f n a r g i n ==1
t o l = ma x ( s i z e ( A) ' ) * ma x ( s ) * e p s ;
e nd
r = s u m( s > t o l ) ;

Th e fir st lin e of a fu n ct ion M-file st a r t s wit h t h e keywor d f u n c t i o n . It gives t h e


fu n ct ion n a m e a n d or der of a r gu m en t s. In t h is ca se, t h er e a r e u p t o t wo in pu t
a r gu m en t s a n d on e ou t pu t a r gu m en t .
Th e n ext sever a l lin es, u p t o t h e fir st bla n k or execu t a ble lin e, a r e com m en t
lin es t h a t pr ovide t h e h elp t ext . Th ese lin es a r e pr in t ed wh en you t ype
he l p r a nk

Th e fir st lin e of t h e h elp t ext is t h e H 1 lin e, wh ich MATLAB displa ys wh en you


u se t h e l o o k f o r com m a n d or r equ est h e l p on a dir ect or y.
Th e r est of t h e file is t h e execu t a ble MATLAB code defin in g t h e fu n ct ion . Th e
va r ia ble s in t r odu ced in t h e body of t h e fu n ct ion , a s well a s t h e va r ia bles on t h e
fir st lin e, r , A a n d t o l , a r e a ll local t o t h e fu n ct ion ; t h ey a r e sepa r a t e fr om a n y
va r ia bles in t h e MATLAB wor kspa ce.
Th is exa m ple illu st r a t es on e a spect of MATLAB fu n ct ion s t h a t is n ot or din a r ily
fou n d in ot h er pr ogr a m m in g la n gu a ges – a va r ia ble n u m ber of a r gu m en t s. Th e
r a n k fu n ct ion ca n be u sed in sever a l differ en t wa ys.
r a n k ( A)
r = r a n k ( A)
r = r a n k ( A, 1 . e - 6 )

6-19
6 Programming with MATLAB

Ma n y M-files wor k t h is wa y. If n o ou t pu t a r gu m en t is su pplied, t h e r esu lt is


st or ed in a n s . If t h e secon d in pu t a r gu m en t is n ot su pplied, t h e fu n ct ion
com pu t es a defa u lt va lu e. Wit h in t h e body of t h e fu n ct ion , t wo qu a n t it ies
n a m ed n a r g i n a n d n a r g o u t a r e a va ila ble wh ich t ell you t h e n u m ber of in pu t
a n d ou t pu t a r gu m en t s in volved in ea ch pa r t icu la r u se of t h e fu n ct ion . Th e r a n k
fu n ct ion u ses n a r g i n , bu t does n ot n eed t o u se n a r g o u t .

Globa l Va ria bles


If you wa n t m or e t h a n on e fu n ct ion t o sh a r e a sin gle copy of a va r ia ble, sim ply
decla r e t h e va r ia ble a s g l o b a l in a ll t h e fu n ct ion s. Do t h e sa m e t h in g a t t h e
com m a n d lin e if you wa n t t h e ba se wor kspa ce t o a ccess t h e va r ia ble. Th e globa l
decla r a t ion m u st occu r befor e t h e va r ia ble is a ct u a lly u sed in a fu n ct ion .
Alt h ou gh it is n ot r equ ir ed, u sin g ca pit a l let t er s for t h e n a m es of globa l
va r ia bles h elps dist in gu ish t h em fr om ot h er va r ia bles. F or exa m ple, cr ea t e a n
M-file ca lled f a l l i n g . m.
f unc t i on h = f a l l i ng( t )
g l o b a l GRAVI TY
h = 1 / 2 * GRAVI TY* t . ^ 2 ;

Th en in t er a ct ively en t er t h e st a t em en t s
g l o b a l GRAVI TY
GRAVI TY = 3 2 ;
y = f a l l i ng( ( 0: . 1: 5) ' ) ;

Th e t wo globa l st a t em en t s m a ke t h e va lu e a ssign ed t o GRAVI TY a t t h e


com m a n d pr om pt a va ila ble in side t h e fu n ct ion . You ca n t h en m odify GRAVI TY
in t er a ct ively a n d obt a in n ew solu t ion s wit h ou t edit in g a n y files.

Pa ssing String Ar gum ents to Functions


You ca n wr it e MATLAB fu n ct ion s t h a t a ccept st r in g a r gu m en t s wit h ou t t h e
pa r en t h eses a n d qu ot es. Th a t is, MATLAB in t er pr et s
f oo a b c

as
f oo( ' a ' , ' b' , ' c ' )

6-20
Scripts and Functions

H owever , wh en u sin g t h e u n qu ot ed for m , MATLAB ca n n ot r et u r n ou t pu t


a r gu m en t s. F or exa m ple,
l e ge nd a ppl e s or a nge s

cr ea t es a legen d on a plot u sin g t h e st r in gs a p p l e s a n d o r a n g e s a s la bels. If you


wa n t t h e l e g e n d com m a n d t o r et u r n it s ou t pu t a r gu m en t s, t h en you m u st u se
t h e qu ot ed for m .
[ l e gh, obj h] = l e ge nd( ' a ppl e s ' , ' or a nge s ' ) ;

In a ddit ion , you ca n n ot u se t h e u n qu ot ed for m if a n y of t h e a r gu m en t s a r e n ot


st r in gs.

Constructing String Arguments in Code


Th e qu ot ed for m en a bles you t o con st r u ct st r in g a r gu m en t s wit h in t h e code.
Th e followin g exa m ple pr ocesses m u lt iple da t a files, Au g u s t 1 . d a t ,
Au g u s t 2 . d a t , a n d so on . It u ses t h e fu n ct ion i n t 2 s t r , wh ich con ver t s a n
in t eger t o a ch a r a ct er , t o bu ild t h e filen a m e.
f or d = 1: 31
s = [ ' Au g u s t ' i n t 2 s t r ( d ) ' . d a t ' ] ;
l oa d( s )
% Co d e t o p r o c e s s t h e c o n t e n t s o f t h e d - t h f i l e
e nd

A Cautionary Note
Wh ile t h e u n qu ot ed syn t a x is con ven ien t , it ca n be u sed in cor r ect ly wit h ou t
ca u sin g MATLAB t o gen er a t e a n er r or . F or exa m ple, given a m a t r ix A,
A =
0 -6 -1
6 2 - 16
-5 20 - 10

Th e e i g com m a n d r et u r n s t h e eigen va lu es of A.
e i g ( A)
a ns =
- 3. 0710
- 2 . 4 6 4 5 +1 7 . 6 0 0 8 i
- 2. 4645- 17. 6008i

6-21
6 Programming with MATLAB

Th e followin g st a t em en t is n ot a llowed beca u se A is n ot a st r in g, h owever


MATLAB does n ot gen er a t e a n er r or .
ei g A
a ns =
65

MATLAB a ct u a lly t a kes t h e eigen va lu es of ASCII n u m er ic equ iva len t of t h e


let t er A (wh ich is t h e n u m ber 65).

The eva l Function


Th e e v a l fu n ct ion wor ks wit h t ext va r ia bles t o im plem en t a power fu l t ext
m a cr o fa cilit y. Th e expr ession or st a t em en t
e va l ( s )

u ses t h e MATLAB in t er pr et er t o eva lu a t e t h e expr ession or execu t e t h e


st a t em en t con t a in ed in t h e t ext st r in g s .

6-22
Scripts and Functions

Th e exa m ple of t h e pr eviou s sect ion cou ld a lso be don e wit h t h e followin g code,
a lt h ou gh t h is wou ld be som ewh a t less efficien t beca u se it in volves t h e fu ll
in t er pr et er , n ot ju st a fu n ct ion ca ll.
f or d = 1: 31
s = [ ' l o a d Au g u s t ' i n t 2 s t r ( d ) ' . d a t ' ] ;
e va l ( s )
% Pr o c e s s t h e c o n t e n t s o f t h e d - t h f i l e
e nd

Vectoriza tion
To obt a in t h e m ost speed ou t of MATLAB, it ’s im por t a n t t o vect or ize t h e
a lgor it h m s in you r M-files. Wh er e ot h er pr ogr a m m in g la n gu a ges m igh t u se f o r
or DO loops, MATLAB ca n u se vect or or m a t r ix oper a t ion s. A sim ple exa m ple
in volves cr ea t in g a t a ble of loga r it h m s.
x = . 01;
f or k = 1: 1001
y( k) = l og10( x) ;
x = x + . 01;
e nd

A vect or ized ver sion of t h e sa m e code is


x = . 01: . 01: 10;
y = l og10( x) ;

F or m or e com plica t ed code, vect or iza t ion opt ion s a r e n ot a lwa ys so obviou s.
Wh en speed is im por t a n t , h owever , you sh ou ld a lwa ys look for wa ys t o
vect or ize you r a lgor it h m s.

Prea lloca tion


If you ca n ’t vect or ize a piece of code, you ca n m a ke you r f o r loops go fa st er by
pr ea lloca t in g a n y vect or s or a r r a ys in wh ich ou t pu t r esu lt s a r e st or ed. F or
exa m ple, t h is code u ses t h e fu n ct ion z e r o s t o pr ea lloca t e t h e vect or cr ea t ed in
t h e f o r loop. Th is m a kes t h e f o r loop execu t e sign ifica n t ly fa st er .
r = z e r os ( 32, 1) ;
f or n = 1: 32
r ( n ) = r a n k ( ma g i c ( n ) ) ;
e nd

6-23
6 Programming with MATLAB

Wit h ou t t h e pr ea lloca t ion in t h e pr eviou s exa m ple, t h e MATLAB in t er pr et er


en la r ges t h e r vect or by on e elem en t ea ch t im e t h r ou gh t h e loop. Vect or
pr ea lloca t ion elim in a t es t h is st ep a n d r esu lt s in fa st er execu t ion .

Function H a ndles
You ca n cr ea t e a h a n dle t o a n y MATLAB fu n ct ion a n d t h en u se t h a t h a n dle a s
a m ea n s of r efer en cin g t h e fu n ct ion . A fu n ct ion h a n dle is t ypica lly pa ssed in a n
a r gu m en t list t o ot h er fu n ct ion s, wh ich ca n t h en execu t e, or evalu ate, t h e
fu n ct ion u sin g t h e h a n dle.
Con st r u ct a fu n ct ion h a n dle in MATLAB u sin g t h e at sign , @, befor e t h e
fu n ct ion n a m e. Th e followin g exa m ple cr ea t es a fu n ct ion h a n dle for t h e s i n
fu n ct ion a n d a ssign s it t o t h e va r ia ble f h a n d l e .
f h a n d l e = @s i n ;

E va lu a t e a fu n ct ion h a n dle u sin g t h e MATLAB f e v a l fu n ct ion . Th e fu n ct ion


p l o t _ f h a n d l e , sh own below, r eceives a fu n ct ion h a n dle a n d da t a , a n d t h en
per for m s a n eva lu a t ion of t h e fu n ct ion h a n dle on t h a t da t a u sin g f e v a l .
f unc t i on x = pl ot _f ha ndl e ( f ha ndl e , da t a )
pl ot ( da t a , f e va l ( f ha ndl e , da t a ) )

Wh en you ca ll p l o t _ f h a n d l e wit h a h a n dle t o t h e s i n fu n ct ion a n d t h e


a r gu m en t sh own below, t h e r esu lt in g eva lu a t ion pr odu ces a sin e wa ve plot .
p l o t _ f h a n d l e ( @s i n , - p i : 0 . 0 1 : p i )

Function Functions
A cla ss of fu n ct ion s, ca lled “fu n ct ion fu n ct ion s,” wor ks wit h n on lin ea r fu n ct ion s
of a sca la r va r ia ble. Th a t is, on e fu n ct ion wor ks on a n ot h er fu n ct ion . Th e
fu n ct ion fu n ct ion s in clu de:

• Zer o fin din g


• Opt im iza t ion
• Qu a dr a t u r e
• Or din a r y differ en t ia l equ a t ion s

6-24
Scripts and Functions

MATLAB r epr esen t s t h e n on lin ea r fu n ct ion by a fu n ct ion M-file. F or exa m ple,


h er e is a sim plified ver sion of t h e fu n ct ion h u mp s fr om t h e ma t l a b / d e mo s
dir ect or y.
f u n c t i o n y = h u mp s ( x )
y = 1. / ( ( x- . 3) . ^ 2 + . 01) + 1. / ( ( x- . 9) . ^ 2 + . 04) - 6;

E va lu a t e t h is fu n ct ion a t a set of poin t s in t h e in t er va l 0 ≤ x ≤ 1 wit h


x = 0: . 002: 1;
y = h u mp s ( x ) ;

Th en plot t h e fu n ct ion wit h


pl ot ( x, y)

100

90

80

70

60

50

40

30

20

10

0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1

Th e gr a ph sh ows t h a t t h e fu n ct ion h a s a loca l m in im u m n ea r x = 0.6. Th e


fu n ct ion f mi n s e a r c h fin ds t h e m in im izer, t h e va lu e of x wh er e t h e fu n ct ion
t a kes on t h is m in im u m . Th e fir st a r gu m en t t o f mi n s e a r c h is a fu n ct ion h a n dle
t o t h e fu n ct ion bein g m in im ized a n d t h e secon d a r gu m en t is a r ou gh gu ess a t
t h e loca t ion of t h e m in im u m .

6-25
6 Programming with MATLAB

p = f mi n s e a r c h ( @h u mp s , . 5 )
p =
0. 6370

To eva lu a t e t h e fu n ct ion a t t h e m in im izer ,


h u mp s ( p )

a ns =
11. 2528

Nu m er ica l a n a lyst s u se t h e t er m s qu ad ratu re a n d in tegration t o dist in gu ish


bet ween n u m er ica l a ppr oxim a t ion of defin it e in t egr a ls a n d n u m er ica l
in t egr a t ion of or din a r y differ en t ia l equ a t ion s. MATLAB’s qu a dr a t u r e r ou t in es
a r e q u a d a n d q u a d l . Th e st a t em en t
Q = q u a d l ( @h u mp s , 0 , 1 )

com pu t es t h e a r ea u n der t h e cu r ve in t h e gr a ph a n d pr odu ces


Q =
29. 8583

F in a lly, t h e gr a ph sh ows t h a t t h e fu n ct ion is n ever zer o on t h is in t er va l. So, if


you sea r ch for a zer o wit h
z = f z e r o ( @h u mp s , . 5 )

you will fin d on e ou t side of t h e in t er va l


z =
- 0. 1316

6-26
Demonstration Programs Included with MATLAB

Demonstra tion Progra ms Included w ith M ATLAB


MATLAB in clu des m a n y dem on st r a t ion pr ogr a m s t h a t h igh ligh t va r iou s
fea t u r es a n d fu n ct ion s. F or a com plet e list of t h e dem os, a t t h e com m a n d
pr om pt t ype
h e l p d e mo s

To view a specific file, for exa m ple, a i r f o i l , t ype


e di t a i r f oi l

To r u n a dem on st r a t ion , t ype t h e filen a m e a t t h e com m a n d pr om pt . F or


exa m ple, t o r u n t h e a ir foil dem on st r a t ion , t ype
a i r f oi l

N ote Ma n y of t h e dem on st r a t ion s u se m u lt iple win dows a n d r equ ir e you t o


pr ess a key in t h e MATLAB Com m a n d Win dow t o con t in u e t h r ou gh t h e
dem on st r a t ion .

Th e followin g t a bles list som e of t h e cu r r en t dem on st r a t ion pr ogr a m s t h a t a r e


a va ila ble, or ga n ized in t o t h ese ca t egor ies:

• MATLAB Ma t r ix Dem on st r a t ion P r ogr a m s


• MATLAB Nu m er ic Dem on st r a t ion P r ogr a m s
• MATLAB Visu a liza t ion Dem on st r a t ion P r ogr a m s
• MATLAB La n gu a ge Dem on st r a t ion P r ogr a m s
• MATLAB Differ en t ia l E qu a t ion P r ogr a m s
• MATLAB Ga ller y Dem on st r a t ion P r ogr a m s
• MATLAB Ga m e Dem on st r a t ion P r ogr a m s
• MATLAB Miscella n eou s Dem on st r a t ion P r ogr a m s
• MATLAB H elper F u n ct ion s Dem on st r a t ion P r ogr a m s

6-27
6 Programming with MATLAB

M ATLAB M a trix Dem onstra tion Progra m s

a i r f oi l Gr a ph ica l dem on st r a t ion of spa r se m a t r ix fr om NASA


a ir foil.
buc kyde m Con n ect ivit y gr a ph of t h e Bu ckm in st er F u ller geodesic
dom e.
d e l s q d e mo F in it e differ en ce La pla cia n on va r iou s dom a in s.
e i g mo v i e Sym m et r ic eigen va lu e m ovie.
e i gs how Gr a ph ica l dem on st r a t ion of m a t r ix eigen va lu es.
i nt r o In t r odu ct ion t o ba sic m a t r ix oper a t ion s in MATLAB.
i nve r t e r Dem on st r a t ion of t h e in ver sion of a la r ge m a t r ix.
ma t ma n i p In t r odu ct ion t o m a t r ix m a n ipu la t ion .
r r e f mo v i e Com pu t a t ion of r edu ced r ow ech elon for m .
s e p d e mo Sepa r a t or s for a fin it e elem en t m esh .
s pa r s i t y Dem on st r a t ion of t h e effect of spa r sit y or der in gs.
s vds how Gr a ph ica l dem on st r a t ion of m a t r ix sin gu la r va lu es.

M ATLAB N um eric Dem onstra tion Progra m s

be nc h MATLAB ben ch m a r k.
c e ns us P r edict ion of t h e U.S. popu la t ion in t h e yea r 2000.
e 2pi Two-dim en sion a l, visu a l solu t ion t o t h e pr oblem
π e
“Wh ich is gr ea t er, e or π ?”
f f t d e mo Use of t h e F F T fu n ct ion for spect r a l a n a lysis.
f i t d e mo Non lin ea r cu r ve fit wit h sim plex a lgor it h m .
f p l o t d e mo Dem on st r a t ion of plot t in g a fu n ct ion .

6-28
Demonstration Programs Included with MATLAB

M ATLAB N um eric Dem onstra tion Progra m s (Continued)

f unf uns Dem on st r a t ion of fu n ct ion s oper a t in g on ot h er


fu n ct ion s.
l o t k a d e mo E xa m ple of or din a r y differ en t ia l equ a t ion solu t ion .
q u a d d e mo Ada pt ive qu a dr a t u r e.
q h u l l d e mo Tessella t ion a n d in t er pola t ion of sca t t er ed da t a .
qua ke Lom a P r iet a ea r t h qu a ke.
s pl i ne 2d Dem on st r a t ion of g i n p u t a n d s p l i n e in t wo
dim en sion s.
s uns pot s Dem on st r a t ion of t h e fa st Fou r ier t r a n sfor m (F F T)
fu n ct ion in MATLAB u sed t o a n a lyze t h e va r ia t ion s in
su n spot a ct ivit y.
z e r o d e mo Zer o fin din g wit h f z e r o .

M ATLAB V isua liza tion Dem onstra tion Progra m s

c o l o r me n u Dem on st r a t ion of a ddin g a color m a p t o t h e cu r r en t


figu r e.
c p l x d e mo Ma ps of fu n ct ion s of a com plex va r ia ble.
e a r t h ma p Gr a ph ica l dem on st r a t ion s of ea r t h ’s t opogr a ph y.
gr a f 2d Two-dim en sion a l XY plot s in MATLAB.
gr a f 2d2 Th r ee-dim en sion a l XYZ plot s in MATLAB.
gr a f c pl x Dem on st r a t ion of com plex fu n ct ion plot s in MATLAB.
i ma g e d e mo Dem on st r a t ion of MATLAB’s im a ge ca pa bilit y.
i ma g e e x t Dem on st r a t ion of ch a n gin g a n d r ot a t in g im a ge
color m a ps.

6-29
6 Programming with MATLAB

M ATLAB V isua liza tion Dem onstra tion Progra m s (Continued)

l or e nz Gr a ph ica l dem on st r a t ion of t h e or bit a r ou n d t h e


Lor en z ch a ot ic a t t r a ct or.
pe nny Sever a l views of t h e pen n y da t a .
vi be s Vibr a t in g L-sh a ped m em br a n e m ovie.
xf our i e r Gr a ph ica l dem on st r a t ion of Fou r ier ser ies expa n sion .
xpkl e i n Klein bot t le dem o.
xps ound Dem on st r a t ion of MATLAB’s sou n d ca pa bilit y.

M ATLAB La ngua ge Dem onstra tion Progra m s

gr a f 3d Dem on st r a t ion of H a n dle Gr a ph ics for su r fa ce plot s.


hndl a xi s Dem on st r a t ion of H a n dle Gr a ph ics for a xes.
hndl gr a f Dem on st r a t ion of H a n dle Gr a ph ics for lin e plot s.
xpl a ng In t r odu ct ion t o t h e MATLAB la n gu a ge.

M ATLAB Differentia l Equa tion Progra m s

a mp 1 d a e St iff DAE fr om a n elect r ica l cir cu it .


ba l l ode E qu a t ion s of m ot ion for a bou n cin g ba ll u sed by
BALLDEMO.

br us s ode St iff pr oblem , m odellin g a ch em ica l r ea ct ion


(Br u ssela t or ).
bur ge r s ode Bu r ger 's equ a t ion solved u sin g a m ovin g m esh
t ech n iqu e.
f e m1 o d e St iff pr oblem wit h a t im e-depen den t m a ss m a t r ix.
f e m2 o d e St iff pr oblem wit h a t im e-in depen den t m a ss m a t r ix.

6-30
Demonstration Programs Included with MATLAB

M ATLAB Differentia l Equa tion Progra m s (Continued)

hb1da e St iff DAE fr om a con ser va t ion la w.


hb1ode St iff pr oblem 1 of H in dm a r sh a n d Byr n e.
hb3ode St iff pr oblem 3 of H in dm a r sh a n d Byr n e.
ma t 4 b v p F in d t h e fou r t h eigen va lu e of t h e Ma t h ieu ’s equ a t ion .
o d e d e mo Dem on st r a t ion of t h e ODE su it e in t egr a t or s.
o d e e x a mp l e s Br owse t h e MATLAB ODE /DAE /BVP /P DE exa m ples.
or bi t ode Rest r ict ed 3 body pr oblem u sed by ORBI TDEMO.
pde x1 E xa m ple 1 for P DE P E .
pde x2 E xa m ple 2 for P DE P E .
pde x3 E xa m ple 3 for P DE P E .
pde x4 E xa m ple 4 for P DE P E .
r i gi dode E u ler equ a t ion s of a r igid body wit h ou t ext er n a l for ces.
s hoc kbvp Th e solu t ion h a s a sh ock la yer n ea r x = 0.
t wo b v p BVP t h a t h a s exa ct ly t wo solu t ion s.
vdpode Pa r a m et er iza ble va n der Pol equ a t ion (st iff for la r ge µ).

M ATLAB Ga llery Dem onstra tion Progra m s

c r ul l e r Gr a ph ica l dem on st r a t ion of a cr u ller.


kl e i n1 Gr a ph ica l dem on st r a t ion of a Klein bot t le.
knot Tu be su r r ou n din g a t h r ee-dim en sion a l kn ot .
l ogo Gr a ph ica l dem on st r a t ion of t h e MATLAB L-sh a ped
m em br a n e logo.

6-31
6 Programming with MATLAB

M ATLAB Ga llery Dem onstra tion Progra m s (Continued)

mo d e s Gr a ph ica l dem on st r a t ion of 12 m odes of t h e L-sh a ped


m em br a n e.
q u i v d e mo Gr a ph ica l dem on st r a t ion of t h e qu iver fu n ct ion .
s p h a r m2 Gr a ph ica l dem on st r a t ion of sph er ica l su r fa ce
h a r m on ic.
t or i 4 Gr a ph ica l dem on st r a t ion of fou r-lin ked, u n kn ot t ed t or i.

M ATLAB Ga m e Dem onstra tion Progra m s

f i f t een Slidin g pu zzle.


life Con wa y’s Ga m e of Life.
s o ma Som a cu be.
x p b o mb s Min esweeper ga m e.

M ATLAB M iscella neous Dem onstra tion Progra m s

c ha i ngui Ma t r ix ch a in m u lt iplica t ion opt im iza t ion .


c ode c Alph a bet t r a n sposit ion coder /decoder.
c r ul s pi n Spin n in g cr u ller m ovie.
l ogos pi n Movie of t h e Ma t h Wor ks logo spin n in g.
ma k e v a s e Dem on st r a t ion of a su r fa ce of r evolu t ion .
q u a t d e mo Qu a t er n ion r ot a t ion .
s pi nne r Color fu l lin es spin n in g t h r ou gh spa ce.
t r a ve l Tr a velin g sa lesm a n pr oblem .
t r us s An im a t ion of a ben din g br idge t r u ss.

6-32
Demonstration Programs Included with MATLAB

M ATLAB M iscella neous Dem onstra tion Progra m s (Continued)

wr l d t r v Gr ea t cir cle fligh t r ou t es a r ou n d t h e globe.


xphi de Visu a l per cept ion of object s in m ot ion .
xpqua d Su per qu a dr ics plot t in g dem on st r a t ion .

M ATLAB Helper Functions Dem onstra tion Progra m s

buc ky Gr a ph of t h e Bu ckm in st er F u ller geodesic dom e.


c md l n b g n Set u p for com m a n d lin e dem os.
c md l n e n d Clea n u p a ft er com m a n d lin e dem os.
c md l n wi n Dem o ga t ewa y r ou t in e for r u n n in g com m a n d lin e
dem os.
f i n d d e mo Com m a n d t h a t fin ds a va ila ble dem os for in dividu a l
t oolboxes.
he l pf un Ut ilit y fu n ct ion for displa yin g h elp t ext con ven ien t ly.
me mb r a n e Th e Ma t h Wor ks logo.
pe a ks Sa m ple fu n ct ion of t wo va r ia bles.
p l t ma t Com m a n d t h a t displa ys a m a t r ix in a figu r e win dow.

Getting M ore Inform a tion


Th e Ma t h Wor ks Web sit e (www. ma t h wo r k s . c o m) con t a in s n u m er ou s M-files
t h a t h a ve been wr it t en by u ser s a n d Ma t h Wor ks st a ff. Th ese a r e a ccessible by
select in g D o w n lo a d s . Also, Te c h n ic a l N o te s , wh ich is a ccessible fr om ou r
Tech n ica l Su ppor t Web sit e (www. ma t h wo r k s . c o m/ s u p p o r t ), con t a in s
n u m er ou s exa m ples on gr a ph ics, m a t h em a t ics, AP I, Sim u lin k, a n d ot h er s.

6-33
6 Programming with MATLAB

6-34
7

Sym bolic Ma t h Toolbox


In tro d u c tio n . . . . . . . . . . . . . . . . . . . . 7-2

Ge ttin g He lp . . . . . . . . . . . . . . . . . . . . 7-4

Ge ttin g S ta rte d . . . . . . . . . . . . . . . . . . 7-5

Ca lc u lu s . . . . . . . . . . . . . . . . . . . . . . 7-16

S im p lific a tio n s a n d S u bs titu tio n s . . . . . . . . . . 7-43

Va ria ble -P re c is io n Arith m e tic . . . . . . . . . . . . 7-58

Lin e a r Alg e bra . . . . . . . . . . . . . . . . . . . 7-63

S o lv in g Equ a tio n s . . . . . . . . . . . . . . . . . 7-86


7 Symbolic Math Toolbox

Introduction
Th e Sym bolic Ma t h Toolbox in cor por a t es sym bolic com pu t a t ion in t o
MATLAB’s n u m er ic en vir on m en t . Th is t oolbox su pplem en t s MATLAB’s
n u m er ic a n d gr a ph ica l fa cilit ies wit h sever a l ot h er t ypes of m a t h em a t ica l
com pu t a t ion .

Fa cility Covers

Ca lcu lu s Differ en t ia t ion , in t egr a t ion , lim it s, su m m a t ion , a n d


Ta ylor ser ies

Lin ea r Algebr a In ver ses, det er m in a n t s, eigen va lu es, sin gu la r va lu e


decom posit ion , a n d ca n on ica l for m s of sym bolic
m a t r ices

Sim plifica t ion Met h ods of sim plifyin g a lgebr a ic expr ession s

Solu t ion of Sym bolic a n d n u m er ica l solu t ion s t o a lgebr a ic a n d


E qu a t ion s differ en t ia l equ a t ion s

Tr a n sfor m s Fou r ier, La pla ce, z-t r a n sfor m , a n d cor r espon din g
in ver se t r a n sfor m s

Va r ia ble-P r ecision Nu m er ica l eva lu a t ion of m a t h em a t ica l expr ession s


Ar it h m et ic t o a n y specified a ccu r a cy

Th e com pu t a t ion a l en gin e u n der lyin g t h e t oolboxes is t h e ker n el of Ma ple, a


syst em developed pr im a r ily a t t h e Un iver sit y of Wa t er loo, Ca n a da , a n d, m or e
r ecen t ly, a t t h e E idgen össich e Tech n isch e H och sch u le, Zü r ich , Swit zer la n d.
Ma ple is m a r ket ed a n d su ppor t ed by Wa t er loo Ma ple, In c.
Th is ver sion of t h e Sym bolic Ma t h Toolbox is design ed t o wor k wit h MATLAB 6
a n d Ma ple V Relea se 5.
Th e Sym bolic Ma t h Toolbox is a collect ion of m or e t h a n on e-h u n dr ed MATLAB
fu n ct ion s t h a t pr ovide a ccess t o t h e Ma ple ker n el u sin g a syn t a x a n d st yle t h a t
is a n a t u r a l ext en sion of t h e MATLAB la n gu a ge. Th e t oolbox a lso a llows you t o
a ccess fu n ct ion s in Ma ple’s lin ea r a lgebr a pa cka ge. Wit h t h is t oolbox, you ca n
wr it e you r own M-files t o a ccess Ma ple fu n ct ion s a n d t h e Ma ple wor kspa ce.

7-2
Introduction

Th e followin g sect ion s of t h is t u t or ia l pr ovide expla n a t ion a n d exa m ples on


h ow t o u se t h e t oolbox.

Section Covers

“Get t in g H elp” H ow t o get on lin e h elp for Sym bolic Ma t h


Toolbox fu n ct ion s

“Get t in g St a r t ed” Ba sic sym bolic m a t h oper a t ion s

“Ca lcu lu s” H ow t o differ en t ia t e a n d in t egr a t e sym bolic


expr ession s

“Sim plifica t ion s a n d H ow t o sim plify a n d su bst it u t e va lu es in t o


Su bst it u t ion s” expr ession s

“Va r ia ble-P r ecision H ow t o con t r ol t h e pr ecision of


Ar it h m et ic” com pu t a t ion s

“Lin ea r Algebr a ” E xa m ples u sin g t h e t oolbox fu n ct ion s

“Solvin g E qu a t ion s” H ow t o solve sym bolic equ a t ion s

For M ore Informa tion You ca n a ccess com plet e r efer en ce in for m a t ion for
t h e Sym bolic Ma t h Toolbox fu n ct ion s fr om H elp. Also, you ca n pr in t t h e P DF
ver sion of t h e com plet e S ym bolic M ath T oolbox User’s Gu id e (t u t or ia l a n d
r efer en ce in for m a t ion ) fr om t h e Sym bolic Ma t h Toolbox r oa dm a p in H elp.

7-3
7 Symbolic Math Toolbox

Getting Help
Th er e a r e sever a l wa ys t o fin d in for m a t ion on u sin g Sym bolic Ma t h Toolbox
fu n ct ion s. On e, of cou r se, is t o r ea d t h is ch a pt er ! An ot h er is t o u se on lin e H elp,
wh ich con t a in s t u t or ia ls a n d r efer en ce in for m a t ion for a ll t h e fu n ct ion s. You
ca n a lso u se MATLAB’s com m a n d lin e h elp syst em . Gen er a lly, you ca n obt a in
h elp on MATLAB fu n ct ion s sim ply by t ypin g
he l p f unc t i on

wh er e f u n c t i o n is t h e n a m e of t h e MATLAB fu n ct ion for wh ich you n eed h elp.


Th is is n ot su fficien t , h owever , for som e Sym bolic Ma t h Toolbox fu n ct ion s. Th e
r ea son ? Th e Sym bolic Ma t h Toolbox “over loa ds” m a n y of MATLAB’s n u m er ic
fu n ct ion s. Th a t is, it pr ovides sym bolic-specific im plem en t a t ion s of t h e
fu n ct ion s, u sin g t h e sa m e fu n ct ion n a m e. To obt a in h elp for t h e sym bolic
ver sion of a n over loa ded fu n ct ion , t ype
h e l p s y m/ f u n c t i o n

wh er e f u n c t i o n is t h e over loa ded fu n ct ion ’s n a m e. F or exa m ple, t o obt a in h elp


on t h e sym bolic ver sion of t h e over loa ded fu n ct ion , d i f f , t ype
h e l p s y m/ d i f f

To obt a in in for m a t ion on t h e n u m er ic ver sion , on t h e ot h er h a n d, sim ply t ype


he l p di f f

H ow ca n you t ell wh et h er a fu n ct ion is over loa ded? Th e h elp for t h e n u m er ic


ver sion t ells you so. F or exa m ple, t h e h elp for t h e d i f f fu n ct ion con t a in s t h e
sect ion
Ov e r l o a d e d me t h o d s
he l p c ha r / di f f . m
h e l p s y m/ d i f f . m

Th is t ells you t h a t t h er e a r e t wo ot h er d i f f com m a n ds t h a t oper a t e on


expr ession s of cla ss c h a r a n d cla ss s y m, r espect ively. See t h e n ext sect ion for
in for m a t ion on cla ss s y m. F or m or e in for m a t ion on over loa ded com m a n ds, see
“Over loa din g Oper a t or s a n d F u n ct ion s” in Usin g M AT L AB , wh ich is a ccessible
fr om H elp.

7-4
Getting Started

Getting Sta rted


Th is sect ion descr ibes h ow t o cr ea t e a n d u se sym bolic object s. It a lso descr ibes
t h e defa u lt sym bolic va r ia ble. If you a r e fa m ilia r wit h ver sion 1 of t h e Sym bolic
Ma t h Toolbox, plea se n ot e t h a t ver sion 2 u ses su bst a n t ia lly differ en t a n d
sim pler syn t a x.
To get a qu ick on lin e in t r odu ct ion t o t h e Sym bolic Ma t h Toolbox, t ype d e mo s a t
t h e MATLAB com m a n d lin e. MATLAB displa ys t h e MATLAB D e m o s dia log
box. Select S y m bo lic Ma th (in t h e left list box) a n d t h en In tro d u c tio n (in t h e
r igh t list box).

Sy m bolic O bjects
Th e Sym bolic Ma t h Toolbox defin es a n ew MATLAB da t a t ype ca lled a
sym bolic object or s y m (for m or e in for m a t ion on da t a t ypes, t h e MATLAB t opic
“P r ogr a m m in g a n d Da t a Types” in Usin g M AT L AB ). In t er n a lly, a sym bolic
object is a da t a st r u ct u r e t h a t st or es a st r in g r epr esen t a t ion of t h e sym bol. Th e
Sym bolic Ma t h Toolbox u ses sym bolic object s t o r epr esen t sym bolic va r ia bles,
expr ession s, a n d m a t r ices.

7-5
7 Symbolic Math Toolbox

Crea ting Sy m bolic Va ria bles a nd Ex pressions


Th e s y m com m a n d let s you con st r u ct sym bolic va r ia bles a n d expr ession s. F or
exa m ple, t h e com m a n ds
x = s y m( ' x ' )
a = s y m( ' a l p h a ' )

cr ea t e a sym bolic va r ia ble x t h a t pr in t s a s x a n d a sym bolic va r ia ble a t h a t


pr in t s a s a l p h a .
Su ppose you wa n t t o u se a sym bolic va r ia ble t o r epr esen t t h e golden r a t io

1+ 5
ρ = -----------------
2
Th e com m a n d
r h o = s y m( ' ( 1 + s q r t ( 5 ) ) / 2 ' )

a ch ieves t h is goa l. Now you ca n per for m va r iou s m a t h em a t ica l oper a t ion s on
r h o . F or exa m ple,
f = r ho^ 2 - r ho - 1

r et u r n s
f =

( 1 / 2 +1 / 2 * 5 ^ ( 1 / 2 ) ) ^ 2 - 3 / 2 - 1 / 2 * 5 ^ ( 1 / 2 )

Th en
s i mp l i f y ( f )

r et u r n s
0
2
Now su ppose you wa n t t o st u dy t h e qu a dr a t ic fu n ct ion f = a x + b x + c . Th e
st a t em en t
f = s y m( ' a * x ^ 2 + b * x + c ' )
2
a ssign s t h e sym bolic expr ession a x + b x + c t o t h e va r ia ble f . Obser ve t h a t in
t h is ca se, t h e Sym bolic Ma t h Toolbox does n ot cr ea t e va r ia bles cor r espon din g
t o t h e t er m s of t h e expr ession , a , b , c , a n d x . To per for m sym bolic m a t h

7-6
Getting Started

oper a t ion s (e.g., in t egr a t ion , differ en t ia t ion , su bst it u t ion , et c.) on f , you n eed
t o cr ea t e t h e va r ia bles explicit ly. You ca n do t h is by t ypin g
a = s y m( ' a' )
b = s y m( ' b' )
c = s y m( ' c' )
x = s y m( ' x' )

or sim ply
s y ms a b c x

In gen er a l, you ca n u se s y m or s y ms t o cr ea t e sym bolic va r ia bles. We


r ecom m en d you u se s y ms beca u se it r equ ir es less t ypin g.

Sy m bolic a nd N um eric Conversions


Con sider t h e or din a r y MATLAB qu a n t it y
t = 0. 1

Th e s y m fu n ct ion h a s fou r opt ion s for r et u r n in g a sym bolic r epr esen t a t ion of
t h e n u m er ic va lu e st or ed in t . Th e ' f ' opt ion
s y m( t , ' f ' )

r et u r n s a sym bolic floa t in g-poin t r epr esen t a t ion


' 1. 999999999999a ' *2^ ( - 4)

Th e ' r ' opt ion


s y m( t , ' r ' )

r et u r n s t h e r a t ion a l for m
1/ 10

Th is is t h e defa u lt set t in g for s y m. Th a t is, ca llin g s y m wit h ou t a secon d


a r gu m en t is t h e sa m e a s u sin g s y m wit h t h e ' r ' opt ion .
s y m( t )

a ns =
1/ 10

7-7
7 Symbolic Math Toolbox

Th e t h ir d opt ion ' e ' r et u r n s t h e r a t ion a l for m of t plu s t h e differ en ce bet ween
t h e t h eor et ica l r a t ion a l expr ession for t a n d it s a ct u a l (m a ch in e) floa t in g-poin t
va lu e in t er m s of e p s (t h e floa t in g-poin t r ela t ive a ccu r a cy).
s y m( t , ' e ' )

a ns =
1 / 1 0 +e p s / 4 0

Th e fou r t h opt ion ' d ' r et u r n s t h e decim a l expa n sion of t u p t o t h e n u m ber of


sign ifica n t digit s specified by d i g i t s .
s y m( t , ' d ' )

a ns =
. 10000000000000000555111512312578

Th e defa u lt va lu e of d i g i t s is 32 (h en ce, s y m( t , ' d ' ) r et u r n s a n u m ber wit h 32


sign ifica n t digit s), bu t if you pr efer a sh or t er r epr esen t a t ion , u se t h e d i g i t s
com m a n d a s follows.
di gi t s ( 7)
s y m( t , ' d ' )

a ns =
. 1000000

A pa r t icu la r ly effect ive u se of s y m is t o con ver t a m a t r ix fr om n u m er ic t o


sym bolic for m . Th e com m a n d
A = hi l b( 3)

gen er a t es t h e 3-by-3 H ilber t m a t r ix.


A =

1. 0000 0. 5000 0. 3333


0. 5000 0. 3333 0. 2500
0. 3333 0. 2500 0. 2000

By a pplyin g s y m t o A
A = s y m( A)

7-8
Getting Started

you ca n obt a in t h e (in fin it ely pr ecise) sym bolic for m of t h e 3-by-3 H ilber t
m a t r ix.
A =

[ 1, 1/ 2, 1/ 3]
[ 1/ 2, 1/ 3, 1/ 4]
[ 1/ 3, 1/ 4, 1/ 5]

Constructing Real and Complex Variables


Th e s y m com m a n d a llows you t o specify t h e m a t h em a t ica l pr oper t ies of
sym bolic va r ia bles by u sin g t h e ' r e a l ' opt ion . Th a t is, t h e st a t em en t s
x = s y m( ' x ' , ' r e a l ' ) ; y = s y m( ' y ' , ' r e a l ' ) ;

or m or e efficien t ly
s y ms x y r e a l
z = x + i *y

cr ea t e sym bolic va r ia bles x a n d y t h a t h a ve t h e a dded m a t h em a t ica l pr oper t y


of bein g r ea l va r ia bles. Specifica lly t h is m ea n s t h a t t h e expr ession
f = x^ 2 + y^ 2

is st r ict ly n on n ega t ive. H en ce, z is a (for m a l) com plex va r ia ble a n d ca n be


m a n ipu la t ed a s su ch . Th u s, t h e com m a n ds
c onj ( x) , c onj ( z ) , e xpa nd( z *c onj ( z ) )

r et u r n t h e com plex con ju ga t es of t h e va r ia bles


x , x - i * y , x ^ 2 +y ^ 2

Th e c o n j com m a n d is t h e com plex con ju ga t e oper a t or for t h e t oolbox. If


c o n j ( x ) == x r et u r n s 1, t h en x is a r ea l va r ia ble.

To clea r x of it s “r ea l” pr oper t y, you m u st t ype


s y ms x u n r e a l

or
x = s y m( ' x ' , ' u n r e a l ' )

7-9
7 Symbolic Math Toolbox

Th e com m a n d
cl ear x

does n ot m a ke x a n on r ea l va r ia ble.

Creating Abstract Functions


If you wa n t t o cr ea t e a n a bst r a ct (i.e., in det er m in a n t ) fu n ct ion f ( x ) , t ype
f = s y m( ' f ( x ) ' )

Th en f a ct s like f ( x ) a n d ca n be m a n ipu la t ed by t h e t oolbox com m a n ds. To


con st r u ct t h e fir st differ en ce r a t io, for exa m ple, t ype
d f = ( s u b s ( f , ' x ' , ' x +h ' ) - f ) / ' h '

or
s y ms x h
d f = ( s u b s ( f , x , x +h ) - f ) / h

wh ich r et u r n s
df =
( f ( x +h ) - f ( x ) ) / h

Th is a pplica t ion of s y m is u sefu l wh en com pu t in g F ou r ier , La pla ce, a n d


z-t r a n sfor m s.

Example: Creating a Symbolic Matrix


A cir cu la n t m a t r ix h a s t h e pr oper t y t h a t ea ch r ow is obt a in ed fr om t h e pr eviou s
on e by cyclica lly per m u t in g t h e en t r ies on e st ep for wa r d. We cr ea t e t h e
cir cu la n t m a t r ix A wh ose elem en t s a r e a , b , a n d c , u sin g t h e com m a n ds
s y ms a b c
A = [ a b c ; b c a ; c a b]

wh ich r et u r n
A =
[ a , b, c ]
[ b, c , a ]
[ c, a, b ]

7-10
Getting Started

Sin ce A is cir cu la n t , t h e su m over ea ch r ow a n d colu m n is t h e sa m e. Let ’s ch eck


t h is for t h e fir st r ow a n d secon d colu m n . Th e com m a n d
s u m( A( 1 , : ) )

r et u r n s
a ns =
a +b +c

Th e com m a n d
s u m( A( 1 , : ) ) == s u m( A( : , 2 ) ) % Th i s i s a l o g i c a l t e s t .

r et u r n s
a ns =
1

Now r epla ce t h e (2,3) en t r y of A wit h b e t a a n d t h e va r ia ble b wit h a l p h a . Th e


com m a n ds
s y ms a l p h a b e t a ;
A( 2 , 3 ) = b e t a ;
A = s u b s ( A, b , a l p h a )

r et u r n
A =
[ a , a l pha , c]
[ a l pha , c, be t a ]
[ c, a , a l pha ]

F r om t h is exa m ple, you ca n see t h a t u sin g sym bolic object s is ver y sim ila r t o
u sin g r egu la r MATLAB n u m er ic object s.

The Default Symbolic Variable


Wh en m a n ipu la t in g m a t h em a t ica l fu n ct ion s, t h e ch oice of t h e in depen den t
va r ia ble is oft en clea r fr om con t ext . F or exa m ple, con sider t h e expr ession s in
t h e t a ble below.

7-11
7 Symbolic Math Toolbox

M a them a tica l Function M ATLAB Com m a nd


n
f = x f = x^ n

g = sin ( a t + b ) g = s i n( a *t + b)

h = J v (z ) h = be s s e l j ( nu, z )

If we a sk for t h e der iva t ives of t h ese expr ession s, wit h ou t specifyin g t h e


n
in depen den t va r ia ble, t h en by m a t h em a t ica l con ven t ion we obt a in f ′ = n x ,
g′ = a cos ( a t + b ) , a n d h ′ = J v ( z ) ( v ⁄ z ) – J v + 1 ( z ) . Let ’s a ssu m e t h a t t h e
in depen den t va r ia bles in t h ese t h r ee expr ession s a r e x , t , a n d z , r espect ively.
Th e ot h er sym bols, n , a , b , a n d v , a r e u su a lly r ega r ded a s “con st a n t s” or
“pa r a m et er s.” If, h owever , we wa n t ed t o differ en t ia t e t h e fir st expr ession wit h
r espect t o n , for exa m ple, we cou ld wr it e

d d n
------- f ( x ) or ------- x
dn dn

n
t o get x ln x .
By m a t h em a t ica l con ven t ion , in depen den t va r ia bles a r e oft en lower -ca se
let t er s fou n d n ea r t h e en d of t h e La t in a lph a bet (e.g., x, y, or z). Th is is t h e idea
beh in d f i n d s y m, a u t ilit y fu n ct ion in t h e t oolbox u sed t o det er m in e defa u lt
sym bolic va r ia bles. Defa u lt sym bolic va r ia bles a r e u t ilized by t h e ca lcu lu s,
sim plifica t ion , equ a t ion -solvin g, a n d t r a n sfor m fu n ct ion s. To a pply t h is u t ilit y
t o t h e exa m ple discu ssed a bove, t ype
s y ms a b n n u t x z
f = x^ n; g = s i n( a *t + b) ; h = be s s e l j ( nu, z ) ;

Th is cr ea t es t h e sym bolic expr ession s f , g , a n d h t o m a t ch t h e exa m ple. To


differ en t ia t e t h ese expr ession s, we u se d i f f .
di f f ( f )

r et u r n s
a ns =
x^ n*n/ x

7-12
Getting Started

See t h e sect ion “Differ en t ia t ion ” for a m or e det a iled discu ssion of
differ en t ia t ion a n d t h e d i f f com m a n d.
H er e, a s a bove, we did n ot specify t h e va r ia ble wit h r espect t o differ en t ia t ion .
H ow did t h e t oolbox det er m in e t h a t we wa n t ed t o differ en t ia t e wit h r espect t o
x ? Th e a n swer is t h e f i n d s y m com m a n d
f i n d s y m( f , 1 )

wh ich r et u r n s
a ns =
x

Sim ila r ly, f i n d s y m( g , 1 ) a n d f i n d s y m( h , 1 ) r et u r n t a n d z , r espect ively. H er e


t h e secon d a r gu m en t of f i n d s y m den ot es t h e n u m ber of sym bolic va r ia bles we
wa n t t o fin d in t h e sym bolic object f , u sin g t h e f i n d s y m r u le (see below). Th e
a bsen ce of a secon d a r gu m en t in f i n d s y m r esu lt s in a list of a ll sym bolic
va r ia bles in a given sym bolic expr ession . We see t h is dem on st r a t ed below. Th e
com m a n d
f i n d s y m( g )

r et u r n s t h e r esu lt
a ns =
a , b, t

findsym Rule Th e defa u lt sym bolic va r ia ble in a sym bolic expr ession is t h e
let t er t h a t is closest t o ' x ' a lph a bet ica lly. If t h er e a r e t wo equ a lly close, t h e
let t er la t er in t h e a lph a bet is ch osen .

7-13
7 Symbolic Math Toolbox

H er e a r e som e exa m ples.

Ex pression Va ria ble Returned by findsym

x^ n x

s i n ( a * t +b ) t

be s s e l j ( nu, z ) z

w* y + v * z y

e xp( i *t he t a ) t he t a

l og( a l pha *x1) x1

y * ( 4 +3 * i ) + 6 * j y

s qr t ( pi *a l pha ) a l pha

Crea ting Sy m bolic M a th Functions


Th er e a r e t wo wa ys t o cr ea t e fu n ct ion s:

• Use sym bolic expr ession s


• Cr ea t e a n M-file

Using Symbolic Expressions


Th e sequ en ce of com m a n ds
s y ms x y z
r = s qr t ( x^ 2 + y^ 2 + z ^ 2)
t = a t a n( y/ x)
f = s i n( x*y) / ( x*y)

gen er a t es t h e sym bolic expr ession s r , t , a n d f . You ca n u se d i f f , i n t , s u b s ,


a n d ot h er Sym bolic Ma t h Toolbox fu n ct ion s t o m a n ipu la t e su ch expr ession s.

7-14
Getting Started

Creating an M-File
M-files per m it a m or e gen er a l u se of fu n ct ion s. Su ppose, for exa m ple, you wa n t
t o cr ea t e t h e s i n c fu n ct ion s i n ( x ) / x . To do t h is, cr ea t e a n M-file in t h e @s y m
dir ect or y.
f unc t i on z = s i nc ( x)
%SI NC Th e s y mb o l i c s i n c f u n c t i o n
% s i n ( x ) / x . Th i s f u n c t i o n
% a c c e p t s a s y m a s t h e i n p u t a r g u me n t .
i f i s e q u a l ( x , s y m( 0 ) )
z = 1;
el s e
z = s i n( x) / x;
e nd

You ca n ext en d su ch exa m ples t o fu n ct ion s of sever a l va r ia bles. See t h e


MATLAB t opic “P r ogr a m m in g a n d Da t a Types” in Usin g M AT L AB for a m or e
det a iled discu ssion on object -or ien t ed pr ogr a m m in g.

7-15
7 Symbolic Math Toolbox

Ca lculus
Th e Sym bolic Ma t h Toolbox pr ovides fu n ct ion s t o do t h e ba sic oper a t ion s of
ca lcu lu s; differ en t ia t ion , lim it s, in t egr a t ion , su m m a t ion , a n d Ta ylor ser ies
expa n sion . Th e followin g sect ion s ou t lin e t h ese fu n ct ion s.

Differentia tion
Let ’s cr ea t e a sym bolic expr ession .
s y ms a x
f = s i n( a *x)

Th en
di f f ( f )

differ en t ia t es f wit h r espect t o it s sym bolic va r ia ble (in t h is ca se x ), a s


det er m in ed by f i n d s y m.
a ns =
c os ( a *x) *a

To differ en t ia t e wit h r espect t o t h e va r ia ble a , t ype


di f f ( f , a )

wh ich r et u r n s d f ⁄ d a
a ns =
c os ( a *x) *x

To ca lcu la t e t h e secon d der iva t ives wit h r espect t o x a n d a , r espect ively, t ype
di f f ( f , 2)

or
di f f ( f , x, 2)

wh ich r et u r n
a ns =
- s i n( a *x) *a ^ 2

7-16
Calculus

and
di f f ( f , a , 2)

wh ich r et u r n s
a ns =
- s i n( a *x) *x^ 2

Defin e a , b , x , n , t , a n d t h e t a in t h e MATLAB wor kspa ce, u sin g t h e s y m


com m a n d. Th e t a ble below illu st r a t es t h e d i f f com m a n d.

f diff(f)

x^ n x^ n*n/ x

s i n ( a * t +b ) c o s ( a * t +b ) * a

e xp( i *t he t a ) i *e xp( i *t he t a )

To differ en t ia t e t h e Bessel fu n ct ion of t h e fir st kin d, b e s s e l j ( n u , z ) , wit h


r espect t o z , t ype
s y ms n u z
b = be s s e l j ( nu, z ) ;
db = di f f ( b)

wh ich r et u r n s
db =
- b e s s e l j ( n u +1 , z ) +n u / z * b e s s e l j ( n u , z )

Th e d i f f fu n ct ion ca n a lso t a ke a sym bolic m a t r ix a s it s in pu t . In t h is ca se, t h e


differ en t ia t ion is don e elem en t -by-elem en t . Con sider t h e exa m ple
s y ms a x
A = [ c os ( a *x) , s i n( a *x) ; - s i n( a *x) , c os ( a *x) ]

wh ich r et u r n s
A =
[ c os ( a *x) , s i n( a *x) ]
[ - s i n( a *x) , c os ( a *x) ]

7-17
7 Symbolic Math Toolbox

Th e com m a n d
d i f f ( A)

r et u r n s
a ns =
[ - s i n( a *x) *a , c os ( a *x) *a ]
[ - c os ( a *x) *a , - s i n( a *x) *a ]

You ca n a lso per for m differ en t ia t ion of a colu m n vect or wit h r espect t o a r ow
vect or . Con sider t h e t r a n sfor m a t ion fr om E u clidea n (x, y, z) t o sph er ica l
( r , λ, ϕ ) coor din a t es a s given by x = r cos λ cos ϕ , y = r cos λ sin ϕ , a n d
z = r sin λ . Not e t h a t λ cor r espon ds t o eleva t ion or la t it u de wh ile ϕ den ot es
a zim u t h or lon git u de.

(x,y,z)

λ y

ϕ
x

To ca lcu la t e t h e J a cobia n m a t r ix, J , of t h is t r a n sfor m a t ion , u se t h e j a c o b i a n


fu n ct ion . Th e m a t h em a t ica l n ot a t ion for J is

∂( x , y , x )
J = -----------------------
∂( r , λ, ϕ )
F or t h e pu r poses of t oolbox syn t a x, we u se l for λ a n d f for ϕ . Th e com m a n ds
s y ms r l f
x = r *c os ( l ) *c os ( f ) ; y = r *c os ( l ) *s i n( f ) ; z = r *s i n( l ) ;
J = j a c obi a n( [ x; y; z ] , [ r l f ] )

r et u r n t h e J a cobia n

7-18
Calculus

J =

[ c os ( l ) *c os ( f ) , - r *s i n( l ) *c os ( f ) , - r *c os ( l ) *s i n( f ) ]
[ c os ( l ) *s i n( f ) , - r *s i n( l ) *s i n( f ) , r *c os ( l ) *c os ( f ) ]
[ s i n( l ) , r *c os ( l ) , 0]

a n d t h e com m a n d
d e t J = s i mp l e ( d e t ( J ) )

r et u r n s
de t J =
- c os ( l ) *r ^ 2

Not ice t h a t t h e fir st a r gu m en t of t h e j a c o b i a n fu n ct ion m u st be a colu m n


vect or a n d t h e secon d a r gu m en t a r ow vect or . Mor eover , sin ce t h e det er m in a n t
of t h e J a cobia n is a r a t h er com plica t ed t r igon om et r ic expr ession , we u sed t h e
s i mp l e com m a n d t o m a ke t r igon om et r ic su bst it u t ion s a n d r edu ct ion s
(sim plifica t ion s). Th e sect ion “Sim plifica t ion s a n d Su bst it u t ion s” discu sses
sim plifica t ion in m or e det a il.
A t a ble su m m a r izin g d i f f a n d j a c o b i a n follows.

M a them a tica l O pera tor M ATLAB Com m a nd

df d i f f ( f ) or d i f f ( f , x )
dx

df di f f ( f , a )
da

2 di f f ( f , b, 2)
d f
2
db

∂( r , t ) J = j a c obi a n( [ r : t ] , [ u, v] )
J = -----------------
∂( u , v )

7-19
7 Symbolic Math Toolbox

Lim its
Th e fu n da m en t a l idea in ca lcu lu s is t o m a ke ca lcu la t ion s on fu n ct ion s a s a
va r ia ble “get s close t o” or a ppr oa ch es a cer t a in va lu e. Reca ll t h a t t h e defin it ion
of t h e der iva t ive is given by a lim it
f (x + h ) – f (x )
f ′( x ) = lim ------------------------------------
h →0 h

pr ovided t h is lim it exist s. Th e Sym bolic Ma t h Toolbox a llows you t o com pu t e


t h e lim it s of fu n ct ion s in a dir ect m a n n er . Th e com m a n ds
s y ms h n x
l i mi t ( ( c o s ( x +h ) - c o s ( x ) ) / h , h , 0 )

wh ich r et u r n
a ns =
- s i n( x)

and
l i mi t ( ( 1 + x / n ) ^ n , n , i n f )

wh ich r et u r n s
a ns =
e xp( x)

illu st r a t e t wo of t h e m ost im por t a n t lim it s in m a t h em a t ics: t h e der iva t ive (in


t h is ca se of cos x) a n d t h e expon en t ia l fu n ct ion . Wh ile m a n y lim it s

lim f ( x )
x→a

a r e “t wo sided” (t h a t is, t h e r esu lt is t h e sa m e wh et h er t h e a ppr oa ch is fr om t h e


r igh t or left of a), lim it s a t t h e sin gu la r it ies of f ( x ) a r e n ot . H en ce, t h e t h r ee
lim it s

1 1 1
lim --- , lim --- , a n d lim ---
x→0 x x → 0- x x → 0+ x

yield t h e t h r ee dist in ct r esu lt s: u n defin ed, – ∞ , a n d + ∞ , r espect ively.

7-20
Calculus

In t h e ca se of u n defin ed lim it s, t h e Sym bolic Ma t h Toolbox r et u r n s Na N (n ot a


n u m ber ). Th e com m a n d
l i mi t ( 1 / x , x , 0 )

or
l i mi t ( 1 / x )

r et u r n s
a ns =
Na N

Th e com m a n d
l i mi t ( 1 / x , x , 0 , ' l e f t ' )

r et u r n s
a ns =
- i nf

wh ile t h e com m a n d
l i mi t ( 1 / x , x , 0 , ' r i g h t ' )

r et u r n s
a ns =
i nf

Obser ve t h a t t h e defa u lt ca se, l i mi t ( f ) is t h e sa m e a s l i mi t ( f , x , 0 ) . E xplor e


t h e opt ion s for t h e l i mi t com m a n d in t h is t a ble. H er e, we a ssu m e t h a t f is a
fu n ct ion of t h e sym bolic object x .

M a them a tica l O pera tion M ATLAB Com m a nd

lim f ( x ) l i mi t ( f )
x→0

lim f ( x ) l i mi t ( f , x , a ) o r
x→a l i mi t ( f , a )

7-21
7 Symbolic Math Toolbox

M a them a tica l O pera tion M ATLAB Com m a nd

lim f ( x ) l i mi t ( f , x , a , ' l e f t ' )


x → a-

lim f (x ) l i mi t ( f , x , a , ' r i g h t ' )


x → a+

Integra tion
If f is a sym bolic expr ession , t h en
i nt ( f )

a t t em pt s t o fin d a n ot h er sym bolic expr ession , F , so t h a t d i f f ( F) = f . Th a t is,


i n t ( f ) r et u r n s t h e in defin it e in t egr a l or a n t ider iva t ive of f (pr ovided on e exist s
in closed for m ). Sim ila r t o differ en t ia t ion ,
i nt ( f , v)

u ses t h e sym bolic object v a s t h e va r ia ble of in t egr a t ion , r a t h er t h a n t h e


va r ia ble det er m in ed by f i n d s y m. See h ow i n t wor ks by lookin g a t t h is t a ble.

M a them a tica l O pera tion M ATLAB Com m a nd

n +1 i n t ( x ^ n ) or
n x
∫ x d x = -------------- i nt ( x^ n, x)
n +1

π⁄2 i n t ( s i n ( 2 * x ) , 0 , p i / 2 ) or
sin ( 2 x ) d x = 1 i nt ( s i n( 2*x) , x, 0, pi / 2)

0

g = cos ( a t + b ) g = c os ( a *t + b)
i n t ( g ) or
i nt ( g, t )
∫ g (t )d t = sin ( a t + b ) ⁄ a

i n t ( b e s s e l j ( 1 , z ) ) or
∫ J 1 ( z )d z = – J0 ( z )
i nt ( be s s e l j ( 1, z ) , z )

7-22
Calculus

In con t r a st t o differ en t ia t ion , sym bolic in t egr a t ion is a m or e com plica t ed t a sk.
A n u m ber of difficu lt ies ca n a r ise in com pu t in g t h e in t egr a l. Th e
a n t ider iva t ive, F , m a y n ot exist in closed for m ; it m a y defin e a n u n fa m ilia r
fu n ct ion ; it m a y exist , bu t t h e soft wa r e ca n ’t fin d t h e a n t ider iva t ive; t h e
soft wa r e cou ld fin d it on a la r ger com pu t er , bu t r u n s ou t of t im e or m em or y on
t h e a va ila ble m a ch in e. Never t h eless, in m a n y ca ses, MATLAB ca n per for m
sym bolic in t egr a t ion su ccessfu lly. F or exa m ple, cr ea t e t h e sym bolic va r ia bles
s y ms a b t h e t a x y n x 1 u

Th is t a ble illu st r a t es in t egr a t ion of expr ession s con t a in in g t h ose va r ia bles.

f int(f)

x^ n x ^ ( n +1 ) / ( n +1 )

y^ ( - 1) l og( y)

n^ x 1/ l og( n) *n^ x

s i n ( a * t h e t a +b ) - 1 / a * c o s ( a * t h e t a +b )

e xp( - x1^ 2) 1/ 2*pi ^ ( 1/ 2) *e r f ( x1)

1 / ( 1 +u ^ 2 ) a t a n( u)

Th e la st exa m ple sh ows wh a t h a ppen s if t h e t oolbox ca n ’t fin d t h e


a n t ider iva t ive; it sim ply r et u r n s t h e com m a n d, in clu din g t h e va r ia ble of
in t egr a t ion , u n eva lu a t ed.
Defin it e in t egr a t ion is a lso possible. Th e com m a n ds
i nt ( f , a , b)

and
i nt ( f , v, a , b)

a r e u sed t o fin d a sym bolic expr ession for

b b
∫a f ( x ) d x a n d ∫a f ( v ) d v
r espect ively.

7-23
7 Symbolic Math Toolbox

H er e a r e som e a ddit ion a l exa m ples.

f a, b int(f,a ,b)

x^ 7 0, 1 1/ 8

1/ x 1, 2 l og( 2)

l og( x) *s qr t ( x) 0, 1 - 4/ 9

e xp( - x^ 2) 0, i nf 1/ 2*pi ^ ( 1/ 2)

be s s e l j ( 1, z ) 0, 1 1 / 4 * h y p e r g e o m( [ 1 ] , [ 2 , 2 ] , - 1 / 4 )

F or t h e Bessel fu n ct ion (b e s s e l j ) exa m ple, it is possible t o com pu t e a


n u m er ica l a ppr oxim a t ion t o t h e va lu e of t h e in t egr a l, u sin g t h e d o u b l e
fu n ct ion . Th e com m a n d
a = i nt ( be s s e l j ( 1, z ) , 0, 1)

r et u r n s
a =
1 / 4 * h y p e r g e o m( [ 1 ] , [ 2 , 2 ] , - 1 / 4 )

a n d t h e com m a n d
a = doubl e ( a )

r et u r n s
a =
0. 2348

Integration with Real Constants


On e of t h e su bt let ies in volved in sym bolic in t egr a t ion is t h e “va lu e” of va r iou s
pa r a m et er s. F or exa m ple, t h e expr ession
2
–( k x )
e

is t h e posit ive, bell sh a ped cu r ve t h a t t en ds t o 0 a s x t en ds t o ± ∞ for a n y r ea l


n u m ber k . An exa m ple of t h is cu r ve is depict ed below wit h

7-24
Calculus

1
k = -------
2
a n d gen er a t ed, u sin g t h ese com m a n ds.
s y ms x
k = s y m( 1 / s q r t ( 2 ) ) ;
f = e xp( - ( k*x) ^ 2) ;
e z pl ot ( f )

exp(−1/2 x2)

0.8

0.6

0.4

0.2

−3 −2 −1 0 1 2 3
x

2 2
Th e Ma ple ker n el, h owever , does n ot , a priori, t r ea t t h e expr ession s k or x
a s posit ive n u m ber s. To t h e con t r a r y, Ma ple a ssu m es t h a t t h e sym bolic
va r ia bles x a n d k a s a priori in det er m in a t e. Th a t is, t h ey a r e pu r ely for m a l
va r ia bles wit h n o m a t h em a t ica l pr oper t ies. Con sequ en t ly, t h e in it ia l a t t em pt
t o com pu t e t h e in t egr a l

7-25
7 Symbolic Math Toolbox


2
–( k x )
∫ e dx
–∞

in t h e Sym bolic Ma t h Toolbox, u sin g t h e com m a n ds


s y ms x k ;
f = e xp( - ( k*x) ^ 2) ;
i nt ( f , x, - i nf , i nf )

r esu lt s in t h e ou t pu t
De f i n i t e i n t e g r a t i o n : Ca n ' t d e t e r mi n e i f t h e i n t e g r a l i s
c onve r ge nt .
Ne e d t o k n o w t h e s i g n o f - - > k ^ 2
Wi l l n o w t r y i n d e f i n i t e i n t e g r a t i o n a n d t h e n t a k e l i mi t s .

Wa r n i n g : Ex p l i c i t i n t e g r a l c o u l d n o t b e f o u n d .
a ns =
i nt ( e xp( - k^ 2*x^ 2) , x= - i nf . . i nf )

In t h e n ext sect ion , you will see h ow t o m a ke k a r ea l va r ia ble a n d t h er efor e


2
k posit ive.

Real Variables via sym


Not ice t h a t Ma ple is n ot a ble t o det er m in e t h e sign of t h e expr ession k ^ 2 . H ow
does on e su r m ou n t t h is obst a cle? Th e a n swer is t o m a ke k a r ea l va r ia ble, u sin g
t h e s y m com m a n d. On e pa r t icu la r ly u sefu l fea t u r e of s y m, n a m ely t h e r e a l
opt ion , a llows you t o decla r e k t o be a r ea l va r ia ble. Con sequ en t ly, t h e in t egr a l
a bove is com pu t ed, in t h e t oolbox, u sin g t h e sequ en ce
s y ms k r e a l
i nt ( f , x, - i nf , i nf )

wh ich r et u r n s
a ns =
s i g n u m( k ) / k * p i ^ ( 1 / 2 )

Not ice t h a t k is n ow a sym bolic object in t h e MATLAB wor kspa ce a n d a r ea l


va r ia ble in t h e Ma ple ker n el wor kspa ce. By t ypin g
cl ear k

7-26
Calculus

you on ly clea r k in t h e MATLAB wor kspa ce. To en su r e t h a t k h a s n o for m a l


pr oper t ies (t h a t is, t o en su r e k is a pu r ely for m a l va r ia ble), t ype
s y ms k u n r e a l

Th is va r ia t ion of t h e s y ms com m a n d clea r s k in t h e Ma ple wor kspa ce. You ca n


a lso decla r e a sequ en ce of sym bolic va r ia bles w , y, x, z t o be r ea l, u sin g
s y ms w x y z r e a l

In t h is ca se, a ll of t h e va r ia bles in bet ween t h e wor ds s y ms a n d r e a l a r e


a ssign ed t h e pr oper t y r e a l . Th a t is, t h ey a r e r ea l va r ia bles in t h e Ma ple
wor kspa ce.

Sy m bolic Sum m a tion


You ca n com pu t e sym bolic su m m a t ion s, wh en t h ey exist , by u sin g t h e s y ms u m
com m a n d. F or exa m ple, t h e p-ser ies

1 1
1 + ------ + ------ + …
2 2
2 3
2
a dds t o π 2 ⁄ 6 , wh ile t h e geom et r ic ser ies 1 + x + x + … a dds t o 1 ⁄ ( 1 – x ) ,
pr ovided x < 1 . Th r ee su m m a t ion s a r e dem on st r a t ed below.
s y ms x k
s 1 = s y ms u m( 1 / k ^ 2 , 1 , i n f )
s 2 = s y ms u m( x ^ k , k , 0 , i n f )

s1 =

1/ 6*pi ^ 2

s2 =

- 1/ ( x- 1)

7-27
7 Symbolic Math Toolbox

Ta y lor Series
Th e st a t em en t s
s y ms x
f = 1 / ( 5 +4 * c o s ( x ) )
T = t a yl or ( f , 8)

r et u r n
T =
1 / 9 +2 / 8 1 * x ^ 2 +5 / 1 4 5 8 * x ^ 4 +4 9 / 1 3 1 2 2 0 * x ^ 6
8
wh ich is a ll t h e t er m s u p t o, bu t n ot in clu din g, or der eigh t ( O ( x ) ) in t h e
Ta ylor ser ies for f ( x ) .

∞ (n )
n f (a )
∑ (x – a ) ------------------
n!
n =0

Tech n ica lly, T is a Ma cla u r in ser ies, sin ce it s ba sepoin t is a = 0 .


Th e com m a n d
p r e t t y ( T)

pr in t s T in a for m a t r esem blin g t ypeset m a t h em a t ics.


2 4 49 6
1/ 9 + 2/ 81 x + 5/ 1458 x + ------ x
131220

Th ese com m a n ds
s y ms x
g = e xp( x*s i n( x) )
t = t a yl or ( g, 12, 2) ;

gen er a t e t h e fir st 12 n on zer o t er m s of t h e Ta ylor ser ies for g a bou t x = 2 .


Let ’s plot t h ese fu n ct ion s t oget h er t o see h ow well t h is Ta ylor a ppr oxim a t ion
com pa r es t o t h e a ct u a l fu n ct ion g .

7-28
Calculus

xd = 1: 0. 05: 3; yd = s ubs ( g, x, xd) ;


e z pl ot ( t , [ 1, 3] ) ; hol d on;
pl ot ( xd, yd, ' r - . ' )
t i t l e ( ' Ta y l o r a p p r o x i ma t i o n v s . a c t u a l f u n c t i o n ' ) ;
l e g e n d ( ' Fu n c t i o n ' , ' Ta y l o r ' )

Taylor approximation vs. actual function

Function
Taylor
6

1
1 1.2 1.4 1.6 1.8 2 2.2 2.4 2.6 2.8 3
x

Specia l t h a n ks t o P r ofessor Gu n n a r Bä ckst r øm of UME A in Sweden for t h is


exa m ple.

Ex tended Ca lculus Ex a m ple


Th e fu n ct ion

1
f ( x ) = ------------------------------
5 + 4 cos ( x )
pr ovides a st a r t in g poin t for illu st r a t in g sever a l ca lcu lu s oper a t ion s in t h e
t oolbox. It is a lso a n in t er est in g fu n ct ion in it s own r igh t . Th e st a t em en t s

7-29
7 Symbolic Math Toolbox

s y ms x
f = 1 / ( 5 +4 * c o s ( x ) )

st or e t h e sym bolic expr ession defin in g t h e fu n ct ion in f .


Th e fu n ct ion e z p l o t ( f ) pr odu ces t h e plot of f ( x ) a s sh own below.

1/(5+4 cos(x))

0.9

0.8

0.7

0.6

0.5

0.4

0.3

0.2

0.1

−6 −4 −2 0 2 4 6
x

Th e e z p l o t fu n ct ion t r ies t o m a ke r ea son a ble ch oices for t h e r a n ge of t h e x-a xis


a n d for t h e r esu lt in g sca le of t h e y-a xis. It s ch oices ca n be over r idden by a n
a ddit ion a l in pu t a r gu m en t , or by su bsequ en t a x i s com m a n ds. Th e defa u lt
dom a in for a fu n ct ion displa yed by e z p l o t is – 2 π ≤ x ≤ 2 π . To pr odu ce a gr a ph
of f ( x ) for a ≤ x ≤ b , t ype
e z pl ot ( f , [ a b] )

7-30
Calculus

Let ’s n ow look a t t h e secon d der iva t ive of t h e fu n ct ion f .


f 2 = di f f ( f , 2)

f2 =
3 2 / ( 5 +4 * c o s ( x ) ) ^ 3 * s i n ( x ) ^ 2 +4 / ( 5 +4 * c o s ( x ) ) ^ 2 * c o s ( x )

E qu iva len t ly, we ca n t ype f 2 = d i f f ( f , x , 2 ) . Th e defa u lt sca lin g in e z p l o t


cu t s off pa r t of f 2 ’s gr a ph . Set t h e a xes lim it s m a n u a lly t o see t h e en t ir e
fu n ct ion .
e z pl ot ( f 2)
a xi s ( [ - 2*pi 2*pi - 5 2] )

32/(5+4 cos(x))3 sin(x)2+4/(5+4 cos(x))2 cos(x)


2

−1

−2

−3

−4

−5
−6 −4 −2 0 2 4 6
x

F r om t h e gr a ph , it a ppea r s t h a t t h e va lu es of f ′′( x ) lie bet ween -4 a n d 1. As it


t u r n s ou t , t h is is n ot t r u e. We ca n ca lcu la t e t h e exa ct r a n ge for f (i.e., com pu t e
it s a ct u a l m a xim u m a n d m in im u m ).

7-31
7 Symbolic Math Toolbox

Th e a ct u a l m a xim a a n d m in im a of f ′′( x ) occu r a t t h e zer os of f ′′′( x ) . Th e


st a t em en t s
f 3 = di f f ( f 2) ;
pr e t t y( f 3)

com pu t e f ′′′( x ) a n d displa y it in a m or e r ea da ble for m a t .


3
s i n( x) s i n( x) c os ( x) s i n( x)
384 - - - - - - - - - - - - - - - + 96 - - - - - - - - - - - - - - - - 4 - - - - - - - - - - - - - - -
4 3 2
( 5 + 4 c os ( x) ) ( 5 + 4 c os ( x) ) ( 5 + 4 c os ( x) )

We ca n sim plify t h is expr ession u sin g t h e st a t em en t s


f 3 = s i mp l e ( f 3 ) ;
pr e t t y( f 3)

2 2
s i n( x) ( 96 s i n( x) + 80 c os ( x) + 80 c os ( x) - 25)
4 -------------------------------------------------
4
( 5 + 4 c os ( x) )

Now u se t h e s o l v e fu n ct ion t o fin d t h e zer os of f ′′′( x ) .


z = s ol ve ( f 3)

r et u r n s a 5-by-1 sym bolic m a t r ix


z =
[ 0]
[ a t a n ( ( - 2 5 5 - 6 0 * 1 9 ^ ( 1 / 2 ) ) ^ ( 1 / 2 ) , 1 0 +3 * 1 9 ^ ( 1 / 2 ) ) ]
[ a t a n ( - ( - 2 5 5 - 6 0 * 1 9 ^ ( 1 / 2 ) ) ^ ( 1 / 2 ) , 1 0 +3 * 1 9 ^ ( 1 / 2 ) ) ]
[ a t a n ( ( - 2 5 5 +6 0 * 1 9 ^ ( 1 / 2 ) ) ^ ( 1 / 2 ) / ( 1 0 - 3 * 1 9 ^ ( 1 / 2 ) ) ) +p i ]
[ - a t a n ( ( - 2 5 5 +6 0 * 1 9 ^ ( 1 / 2 ) ) ^ ( 1 / 2 ) / ( 1 0 - 3 * 1 9 ^ ( 1 / 2 ) ) ) - p i ]

ea ch of wh ose en t r ies is a zer o of f ′′′( x ) . Th e com m a n ds


f o r ma t ; % De f a u l t f o r ma t o f 5 d i g i t s
z r = doubl e ( z )

7-32
Calculus

con ver t t h e zer os t o d o u b l e for m .


zr =

0
0+ 2. 4381i
0- 2. 4381i
2. 4483
- 2. 4483

So fa r , we h a ve fou n d t h r ee r ea l zer os a n d t wo com plex zer os. H owever , a gr a ph


of f 3 sh ows t h a t we h a ve n ot yet fou n d a ll it s zer os.
e z pl ot ( f 3)
hol d on;
pl ot ( z r , 0*z r , ' r o' )
pl ot ( [ - 2*pi , 2*pi ] , [ 0, 0] , ' g- . ' ) ;
t i t l e ( ' Ze r o s o f f 3 ' )

Zeros of f3

−1

−2

−3

−6 −4 −2 0 2 4 6
x

7-33
7 Symbolic Math Toolbox

Th is occu r s beca u se f ′′′( x ) con t a in s a fa ct or of sin ( x ) , wh ich is zer o a t in t eger


m u lt iples of π . Th e fu n ct ion , s o l v e ( s i n ( x ) ) , h owever , on ly r epor t s t h e zer o a t
x = 0.
We ca n obt a in a com plet e list of t h e r ea l zer os by t r a n sla t in g z r
z r = [ 0 z r ( 4) pi 2*pi - z r ( 4) ]

by m u lt iples of 2 π
z r = [ z r - 2 * p i z r z r +2 * p i ] ;

Now let ’s plot t h e t r a n sfor m ed z r on ou r gr a ph for a com plet e pict u r e of t h e


zer os of f 3 .
p l o t ( z r , 0 * z r , ' k X' )

Zeros of f3

−1

−2

−3

−6 −4 −2 0 2 4 6
x

7-34
Calculus

Th e fir st zer o of f ′′′( x ) fou n d by s o l v e is a t x = 0. We su bst it u t e 0 for t h e


sym bolic va r ia ble in f 2
f 20 = s ubs ( f 2, x, 0)

t o com pu t e t h e cor r espon din g va lu e of f ′′(0 ) .


f 20 =
0. 0494

A look a t t h e gr a ph of f ′′( x ) sh ows t h a t t h is is on ly a loca l m in im u m , wh ich we


dem on st r a t e by r eplot t in g f 2 .
cl f
e z pl ot ( f 2)
a xi s ( [ - 2*pi 2*pi - 4. 25 1. 25] )
yl a be l ( ' f 2' ) ;
t i t l e ( ' Pl o t o f f 2 = f ' ' ' ' ( x) ' )
hol d on
pl ot ( 0, doubl e ( f 20) , ' r o' )
t e x t ( - 1 , - 0 . 2 5 , ' Lo c a l mi n i mu m' )

Th e r esu lt in g plot

7-35
7 Symbolic Math Toolbox

Plot of f2 = f’’(x)

0.5

0
Local minimum
−0.5

−1
f2

−1.5

−2

−2.5

−3

−3.5

−4

−6 −4 −2 0 2 4 6
x

in dica t es t h a t t h e globa l m in im a occu r n ea r x = – π a n d x = π . We ca n


dem on st r a t e t h a t t h ey occu r exa ct ly a t x = ± π , u sin g t h e followin g sequ en ce of
com m a n ds. F ir st we t r y su bst it u t in g – π a n d π in t o f ′′′( x ) .
s i mp l e ( [ s u b s ( f 3 , x , - s y m( p i ) ) , s u b s ( f 3 , x , s y m( p i ) ) ] )

Th e r esu lt
a ns =
[ 0, 0]

sh ows t h a t – π a n d π h a ppen t o be cr it ica l poin t s of f ′′′( x ) . We ca n see t h a t – π


a n d π a r e globa l m in im a by plot t in g f 2 ( - p i ) a n d f 2 ( p i ) a ga in st f 2 ( x ) .
m1 = d o u b l e ( s u b s ( f 2 , x , - p i ) ) ; m2 = d o u b l e ( s u b s ( f 2 , x , p i ) ) ;
p l o t ( - p i , m1 , ' g o ' , p i , m2 , ' g o ' )
t e x t ( - 1 , - 4 , ' Gl o b a l mi n i ma ' )

7-36
Calculus

Th e a ct u a l m in im a a r e m1 , m2
a ns =
[ - 4, - 4]

a s sh own in t h e followin g plot .

Plot of f2 = f’’(x)

0.5

0
Local minimum
−0.5

−1
f2

−1.5

−2

−2.5

−3

−3.5

−4 Global minima

−6 −4 −2 0 2 4 6
x

Th e for egoin g a n a lysis con fir m s pa r t of ou r or igin a l gu ess t h a t t h e r a n ge of


f ′′( x ) is [ - 4 , 1 ] . We ca n con fir m t h e ot h er pa r t by exa m in in g t h e fou r t h zer o
of f ′′′( x ) fou n d by s o l v e . F ir st ext r a ct t h e fou r t h zer o fr om z a n d a ssign it t o a
sepa r a t e va r ia ble
s = z ( 4)

t o obt a in
s =
a t a n ( ( - 2 5 5 +6 0 * 1 9 ^ ( 1 / 2 ) ) ^ ( 1 / 2 ) / ( 1 0 - 3 * 1 9 ^ ( 1 / 2 ) ) ) +p i

7-37
7 Symbolic Math Toolbox

E xecu t in g
s d = doubl e ( s )

displa ys t h e zer o’s cor r espon din g n u m er ic va lu e.


sd =
2. 4483

P lot t in g t h e poin t ( s , f 2 ( s ) ) a ga in st f 2 , u sin g


M1 = d o u b l e ( s u b s ( f 2 , x , s ) ) ;
p l o t ( s d , M1 , ' k o ' )
t e x t ( - 1 , 1 , ' Gl o b a l ma x i mu m' )

visu a lly con fir m s t h a t s is a m a xim u m .

Plot of f2 = f’’(x)

1 Global maximum

0.5

0
Local minimum
−0.5

−1
f2

−1.5

−2

−2.5

−3

−3.5

−4 Global minima

−6 −4 −2 0 2 4 6
x

Th e m a xim u m is M1 = 1 . 0 0 5 1 .

7-38
Calculus

Th er efor e, ou r gu ess t h a t t h e m a xim u m of f ′′( x ) is [-4, 1] wa s close, bu t


in cor r ect . Th e a ct u a l r a n ge is [-4, 1.0051].
Now, let ’s see if in t egr a t in g f ′′( x ) t wice wit h r espect t o x r ecover s ou r or igin a l
fu n ct ion f ( x ) = 1 ⁄ ( 5 + 4 cos x ) . Th e com m a n d
g = i nt ( i nt ( f 2) )

r et u r n s
g =
- 8 / ( t a n ( 1 / 2 * x ) ^ 2 +9 )

Th is is cer t a in ly n ot t h e or igin a l expr ession for f ( x ) . Let ’s look a t t h e differ en ce


f ( x ) – g ( x ).
d = f - g
pr e t t y( d)

1 8
–––––––––––– + –––––––––––––––
5 + 4 c os ( x) 2
t a n( 1/ 2 x) + 9

We ca n sim plify t h is u sin g s i mp l e ( d ) or s i mp l i f y ( d ) . E it h er com m a n d


pr odu ces
a ns =
1

Th is illu st r a t es t h e con cept t h a t differ en t ia t in g f ( x ) t wice, t h en in t egr a t in g t h e


r esu lt t wice, pr odu ces a fu n ct ion t h a t m a y differ fr om f ( x ) by a lin ea r fu n ct ion
of x .
F in a lly, in t egr a t e f ( x ) on ce m or e.
F = i nt ( f )

Th e r esu lt
F =
2/ 3*a t a n( 1/ 3*t a n( 1/ 2*x) )

in volves t h e a r ct a n gen t fu n ct ion .

7-39
7 Symbolic Math Toolbox

Th ou gh F ( x ) is t h e a n t ider iva t ive of a con t in u ou s fu n ct ion , it is it self


discon t in u ou s a s t h e followin g plot sh ows.
e z p l o t ( F)

2/3 atan(1/3 tan(1/2 x))

0.8

0.6

0.4

0.2

−0.2

−0.4

−0.6

−0.8

−1

−6 −4 −2 0 2 4 6
x

Not e t h a t F ( x ) h a s ju m ps a t x = ± π . Th is occu r s beca u se t a n x is sin gu la r a t


x = ±π .
In fa ct , a s
e z pl ot ( a t a n( t a n( x) ) )

sh ows, t h e n u m er ica l va lu e of a t a n ( t a n ( x ) ) differ s fr om x by a piecewise


con st a n t fu n ct ion t h a t h a s ju m ps a t odd m u lt iples of π ⁄ 2 .

7-40
Calculus

atan(tan(x))

1.5

0.5

−0.5

−1

−1.5

−6 −4 −2 0 2 4 6
x

To obt a in a r epr esen t a t ion of F ( x ) t h a t does n ot h a ve ju m ps a t t h ese poin t s, we


m u st in t r odu ce a secon d fu n ct ion , J ( x ) , t h a t com pen sa t es for t h e
discon t in u it ies. Th en we a dd t h e a ppr opr ia t e m u lt iple of J ( x ) t o F ( x )
J = s y m( ' r o u n d ( x / ( 2 * p i ) ) ' ) ;
c = s y m( ' 2 / 3 * p i ' ) ;
F1 = F+c * J
F1 =
2 / 3 * a t a n ( 1 / 3 * t a n ( 1 / 2 * x ) ) +2 / 3 * p i * r o u n d ( 1 / 2 * x / p i )

a n d plot t h e r esu lt .
e z p l o t ( F1 , [ - 6 . 2 8 , 6 . 2 8 ] )

Th is r epr esen t a t ion does h a ve a con t in u ou s gr a ph .

7-41
7 Symbolic Math Toolbox

2/3 atan(1/3 tan(1/2 x))+2/3 π round(1/2 x/π)


2.5

1.5

0.5

−0.5

−1

−1.5

−2

−2.5
−6 −4 −2 0 2 4 6
x

Not ice t h a t we u se t h e dom a in [-6.28, 6.28] in e z p l o t r a t h er t h a n t h e defa u lt


dom a in [– 2 π,2 π] . Th e r ea son for t h is is t o pr even t a n eva lu a t ion of
F 1 = 2 ⁄ 3 a t a n ( 1 ⁄ 3 t a n 1 ⁄ 2 x ) a t t h e sin gu la r poin t s x = – π a n d x = π wh er e
t h e ju m ps in F a n d J do n ot ca n cel ou t on e a n ot h er . Th e pr oper h a n dlin g of
br a n ch cu t discon t in u it ies in m u lt iva lu ed fu n ct ion s like a r ct a n x is a deep a n d
difficu lt pr oblem in sym bolic com pu t a t ion . Alt h ou gh MATLAB a n d Ma ple
ca n n ot do t h is en t ir ely a u t om a t ica lly, t h ey do pr ovide t h e t ools for
in vest iga t in g su ch qu est ion s.

7-42
Simplifications and Substitutions

Simplifica tions a nd Substitutions


Th er e a r e sever a l fu n ct ion s t h a t sim plify sym bolic expr ession s a n d a r e u sed t o
per for m sym bolic su bst it u t ion s.

Sim plifica tions


H er e a r e t h r ee differ en t sym bolic expr ession s.
s y ms x
f = x ^ 3 - 6 * x ^ 2 +1 1 * x - 6
g = ( x- 1) *( x- 2) *( x- 3)
h = x * ( x * ( x - 6 ) +1 1 ) - 6

H er e a r e t h eir pr et t ypr in t ed for m s, gen er a t ed by


pr e t t y( f ) , pr e t t y( g) , pr e t t y( h)

3 2
x - 6 x + 11 x - 6

( x - 1) ( x - 2) ( x - 3)

x ( x ( x - 6) + 11) - 6

Th ese expr ession s a r e t h r ee differ en t r epr esen t a t ion s of t h e sa m e


m a t h em a t ica l fu n ct ion , a cu bic polyn om ia l in x .
E a ch of t h e t h r ee for m s is pr efer a ble t o t h e ot h er s in differ en t sit u a t ion s. Th e
fir st for m , f , is t h e m ost com m on ly u sed r epr esen t a t ion of a polyn om ia l. It is
sim ply a lin ea r com bin a t ion of t h e power s of x . Th e secon d for m , g , is t h e
fa ct or ed for m . It displa ys t h e r oot s of t h e polyn om ia l a n d is t h e m ost a ccu r a t e
for n u m er ica l eva lu a t ion n ea r t h e r oot s. Bu t , if a polyn om ia l does n ot h a ve su ch
sim ple r oot s, it s fa ct or ed for m m a y n ot be so con ven ien t . Th e t h ir d for m , h , is
t h e H or n er , or n est ed, r epr esen t a t ion . F or n u m er ica l eva lu a t ion , it in volves t h e
fewest a r it h m et ic oper a t ion s a n d is t h e m ost a ccu r a t e for som e ot h er r a n ges of
x.

Th e sym bolic sim plifica t ion pr oblem in volves t h e ver ifica t ion t h a t t h ese t h r ee
expr ession s r epr esen t t h e sa m e fu n ct ion . It a lso in volves a less clea r ly defin ed
object ive — wh ich of t h ese r epr esen t a t ion s is “t h e sim plest ”?

7-43
7 Symbolic Math Toolbox

Th is t oolbox pr ovides sever a l fu n ct ion s t h a t a pply va r iou s a lgebr a ic a n d


t r igon om et r ic iden t it ies t o t r a n sfor m on e r epr esen t a t ion of a fu n ct ion in t o
a n ot h er , possibly sim pler , r epr esen t a t ion . Th ese fu n ct ion s a r e c o l l e c t ,
e x p a n d , h o r n e r , f a c t o r , s i mp l i f y , a n d s i mp l e .

collect
Th e st a t em en t
c ol l e c t ( f )

views f a s a polyn om ia l in it s sym bolic va r ia ble, sa y x , a n d collect s a ll t h e


coefficien t s wit h t h e sa m e power of x . A secon d a r gu m en t ca n specify t h e
va r ia ble in wh ich t o collect t er m s if t h er e is m or e t h a n on e ca n dida t e. H er e a r e
a few exa m ples.

f collect(f)

( x- 1) *( x- 2) *( x- 3) x ^ 3 - 6 * x ^ 2 +1 1 * x - 6

x * ( x * ( x - 6 ) +1 1 ) - 6 x ^ 3 - 6 * x ^ 2 +1 1 * x - 6

( 1 +x ) * t + x * t 2 * x * t +t

expand
Th e st a t em en t
e xpa nd( f )

dist r ibu t es pr odu ct s over su m s a n d a pplies ot h er iden t it ies in volvin g fu n ct ion s


of su m s. F or exa m ple,

f ex pa nd(f)

a ∗( x + y ) a ∗x + a ∗y

( x - 1 ) ∗( x - 2 ) ∗( x - 3 ) x ^ 3 - 6 ∗x ^ 2 +1 1 ∗x - 6

x ∗( x ∗( x - 6 ) +1 1 ) - 6 x ^ 3 - 6 ∗x ^ 2 +1 1 ∗x - 6

7-44
Simplifications and Substitutions

f ex pa nd(f)

e x p ( a +b ) e x p ( a ) ∗e x p ( b )

c o s ( x +y ) c os ( x) *c os ( y) - s i n( x) *s i n( y)

c o s ( 3 ∗a c o s ( x ) ) 4*x^ 3- 3*x

horner
Th e st a t em en t
hor ne r ( f )

t r a n sfor m s a sym bolic polyn om ia l f in t o it s H or n er , or n est ed, r epr esen t a t ion .


F or exa m ple,

f horner(f)

x ^ 3 - 6 ∗x ^ 2 +1 1 ∗x - 6 - 6 +( 1 1 +( - 6 +x ) * x ) * x

1 . 1 +2 . 2 ∗x +3 . 3 ∗x ^ 2 1 1 / 1 0 +( 1 1 / 5 +3 3 / 1 0 * x ) * x

factor
If f is a polyn om ia l wit h r a t ion a l coefficien t s, t h e st a t em en t
f a c t or ( f )

expr esses f a s a pr odu ct of polyn om ia ls of lower degr ee wit h r a t ion a l


coefficien t s. If f ca n n ot be fa ct or ed over t h e r a t ion a l n u m ber s, t h e r esu lt is f
it self. F or exa m ple,

f fa ctor(f)

x ^ 3 - 6 ∗x ^ 2 +1 1 ∗x - 6 ( x - 1 ) ∗( x - 2 ) ∗( x - 3 )

x ^ 3 - 6 ∗x ^ 2 +1 1 ∗x - 5 x ^ 3 - 6 ∗x ^ 2 +1 1 ∗x - 5

x ^ 6 +1 ( x ^ 2 +1 ) ∗( x ^ 4 - x ^ 2 +1 )

7-45
7 Symbolic Math Toolbox

H er e is a n ot h er exa m ple in volvin g f a c t o r . It fa ct or s polyn om ia ls of t h e for m


x ^ n + 1 . Th is code
s y ms x ;
n = ( 1: 9) ' ;
p = x. ^ n + 1;
f = f a c t or ( p) ;
[ p, f ]

r et u r n s a m a t r ix wit h t h e polyn om ia ls in it s fir st colu m n a n d t h eir fa ct or ed


for m s in it s secon d.
[ x +1 , x +1 ]
[ x ^ 2 +1 , x ^ 2 +1 ]
[ x ^ 3 +1 , ( x +1 ) * ( x ^ 2 - x +1 ) ]
[ x ^ 4 +1 , x ^ 4 +1 ]
[ x ^ 5 +1 , ( x +1 ) * ( x ^ 4 - x ^ 3 +x ^ 2 - x +1 ) ]
[ x ^ 6 +1 , ( x ^ 2 +1 ) * ( x ^ 4 - x ^ 2 +1 ) ]
[ x ^ 7 +1 , ( x +1 ) * ( 1 - x +x ^ 2 - x ^ 3 +x ^ 4 - x ^ 5 +x ^ 6 ) ]
[ x ^ 8 +1 , x ^ 8 +1 ]
[ x ^ 9 +1 , ( x +1 ) * ( x ^ 2 - x +1 ) * ( x ^ 6 - x ^ 3 +1 ) ]

As a n a side a t t h is poin t , we m en t ion t h a t f a c t o r ca n a lso fa ct or sym bolic


object s con t a in in g in t eger s. Th is is a n a lt er n a t ive t o u sin g t h e f a c t o r fu n ct ion
in MATLAB’s s p e c f u n dir ect or y. F or exa m ple, t h e followin g code segm en t
N = s y m( 1 ) ;
f or k = 2: 11
N( k ) = 1 0 * N( k - 1 ) +1 ;
e nd
[ N' f a c t o r ( N' ) ]

7-46
Simplifications and Substitutions

displa ys t h e fa ct or s of sym bolic in t eger s con sist in g of 1s.


[ 1, 1]
[ 11, ( 11) ]
[ 111, ( 3) *( 37) ]
[ 1111, ( 11) *( 101) ]
[ 11111, ( 41) *( 271) ]
[ 111111, ( 3) *( 7) *( 11) *( 13) *( 37) ]
[ 1111111, ( 239) *( 4649) ]
[ 11111111, ( 11) *( 73) *( 101) *( 137) ]
[ 111111111, ( 3) ^ 2*( 37) *( 333667) ]
[ 1111111111, ( 11) *( 41) *( 271) *( 9091) ]
[ 11111111111, ( 513239) *( 21649) ]

simplify
Th e s i mp l i f y fu n ct ion is a power fu l, gen er a l pu r pose t ool t h a t a pplies a
n u m ber of a lgebr a ic iden t it ies in volvin g su m s, in t egr a l power s, squ a r e r oot s
a n d ot h er fr a ct ion a l power s, a s well a s a n u m ber of fu n ct ion a l iden t it ies
in volvin g t r ig fu n ct ion s, expon en t ia l a n d log fu n ct ion s, Bessel fu n ct ion s,
h yper geom et r ic fu n ct ion s, a n d t h e ga m m a fu n ct ion . H er e a r e som e exa m ples.

f simplify(f)

x ∗( x ∗( x - 6 ) +1 1 ) - 6 x ^ 3 - 6 ∗x ^ 2 +1 1 ∗x - 6

( 1- x^ 2) / ( 1- x) x +1

( 1 / a ^ 3 +6 / a ^ 2 +1 2 / a +8 ) ^ ( 1 / 3 ) ( ( 2 * a +1 ) ^ 3 / a ^ 3 ) ^ ( 1 / 3 )

s y ms x y p o s i t i v e
l o g ( x ∗y ) l o g ( x ) +l o g ( y )

e xp( x) ∗ e xp( y) e x p ( x +y )

be s s e l j ( 2, x) + be s s e l j ( 0, x) 2/ x*be s s e l j ( 1, x)

g a mma ( x +1 ) - x * g a mma ( x ) 0

c os ( x) ^ 2 + s i n( x) ^ 2 1

7-47
7 Symbolic Math Toolbox

simple
Th e s i mp l e fu n ct ion h a s t h e u n or t h odox m a t h em a t ica l goa l of fin din g a
sim plifica t ion of a n expr ession t h a t h a s t h e fewest n u m ber of ch a r a ct er s. Of
cou r se, t h er e is lit t le m a t h em a t ica l ju st ifica t ion for cla im in g t h a t on e
expr ession is “sim pler ” t h a n a n ot h er ju st beca u se it s ASCII r epr esen t a t ion is
sh or t er , bu t t h is oft en pr oves sa t isfa ct or y in pr a ct ice.
Th e s i mp l e fu n ct ion a ch ieves it s goa l by in depen den t ly a pplyin g s i mp l i f y ,
c o l l e c t , f a c t o r , a n d ot h er sim plifica t ion fu n ct ion s t o a n expr ession a n d
keepin g t r a ck of t h e len gt h s of t h e r esu lt s. Th e s i mp l e fu n ct ion t h en r et u r n s
t h e sh or t est r esu lt .
Th e s i mp l e fu n ct ion h a s sever a l for m s, ea ch r et u r n in g differ en t ou t pu t . Th e
for m
s i mp l e ( f )

displa ys ea ch t r ia l sim plifica t ion a n d t h e sim plifica t ion fu n ct ion t h a t pr odu ced
it in t h e MATLAB com m a n d win dow. Th e s i mp l e fu n ct ion t h en r et u r n s t h e
sh or t est r esu lt . F or exa m ple, t h e com m a n d
s i mp l e ( c o s ( x ) ^ 2 + s i n ( x ) ^ 2 )

displa ys t h e followin g a lt er n a t ive sim plifica t ion s in t h e MATLAB com m a n d


win dow
s i mp l i f y :
1

r a d s i mp :
c o s ( x ) ^ 2 +s i n ( x ) ^ 2

c o mb i n e ( t r i g ) :
1

f a c t or :
c o s ( x ) ^ 2 +s i n ( x ) ^ 2

e xpa nd:
c o s ( x ) ^ 2 +s i n ( x ) ^ 2

c onve r t ( e xp) :
( 1 / 2 * e x p ( i * x ) +1 / 2 / e x p ( i * x ) ) ^ 2 - 1 / 4 * ( e x p ( i * x ) - 1 / e x p ( i * x ) ) ^ 2

7-48
Simplifications and Substitutions

c onve r t ( s i nc os ) :
c o s ( x ) ^ 2 +s i n ( x ) ^ 2

c onve r t ( t a n) :
( 1 - t a n ( 1 / 2 * x ) ^ 2 ) ^ 2 / ( 1 +t a n ( 1 / 2 * x ) ^ 2 ) ^ 2 +4 * t a n ( 1 / 2 * x ) ^ 2 /
( 1 +t a n ( 1 / 2 * x ) ^ 2 ) ^ 2

c ol l e c t ( x) :
c o s ( x ) ^ 2 +s i n ( x ) ^ 2

a n d r et u r n s
a ns =
1

Th is for m is u sefu l wh en you wa n t t o ch eck, for exa m ple, wh et h er t h e sh or t est


for m is in deed t h e sim plest . If you a r e n ot in t er est ed in h ow s i mp l e a ch ieves
it s r esu lt , u se t h e for m
f = s i mp l e ( f )

Th is for m sim ply r et u r n s t h e sh or t est expr ession fou n d. F or exa m ple, t h e


st a t em en t
f = s i mp l e ( c o s ( x ) ^ 2 +s i n ( x ) ^ 2 )

r et u r n s
f =
1

If you wa n t t o kn ow wh ich sim plifica t ion r et u r n ed t h e sh or t est r esu lt , u se t h e


m u lt iple ou t pu t for m .
[ F, h o w] = s i mp l e ( f )

Th is for m r et u r n s t h e sh or t est r esu lt in t h e fir st va r ia ble a n d t h e sim plifica t ion


m et h od u sed t o a ch ieve t h e r esu lt in t h e secon d va r ia ble. F or exa m ple, t h e
st a t em en t
[ f , h o w] = s i mp l e ( c o s ( x ) ^ 2 +s i n ( x ) ^ 2 )

7-49
7 Symbolic Math Toolbox

r et u r n s
f =
1

how =
c o mb i n e

Th e s i mp l e fu n ct ion som et im es im pr oves on t h e r esu lt r et u r n ed by s i mp l i f y ,


on e of t h e sim plifica t ion s t h a t it t r ies. F or exa m ple, wh en a pplied t o t h e
exa m ples given for s i mp l i f y , s i mp l e r et u r n s a sim pler (or a t lea st sh or t er )
r esu lt in t wo ca ses.

f sim plify(f) simple(f)

( 1 / a ^ 3 +6 / a ^ 2 +1 2 / a +8 ) ^ ( 1 / 3 ) ( ( 2 * a +1 ) ^ 3 / a ^ 3 ) ^ ( 1 / 3 ) ( 2 * a +1 ) / a

s y ms x y p o s i t i v e
l o g ( x ∗y ) l o g ( x ) +l o g ( y ) l og( x*y)

In som e ca ses, it is a dva n t a geou s t o a pply s i mp l e t wice t o obt a in t h e effect of


t wo differ en t sim plifica t ion fu n ct ion s. F or exa m ple, t h e st a t em en t s
f = ( 1 / a ^ 3 +6 / a ^ 2 +1 2 / a +8 ) ^ ( 1 / 3 ) ;
s i mp l e ( s i mp l e ( f ) )

r et u r n
2 +1 / a

Th e fir st a pplica t ion , s i mp l e ( f ) , u ses r a d s i mp t o pr odu ce ( 2 * a +1 ) / a ; t h e


secon d a pplica t ion u ses c o mb i n e ( t r i g ) t o t r a n sfor m t h is t o 1 / a +2 .
Th e s i mp l e fu n ct ion is pa r t icu la r ly effect ive on expr ession s in volvin g
t r igon om et r ic fu n ct ion s. H er e a r e som e exa m ples.

f sim ple(f)

c o s ( x ) ^ 2 +s i n ( x ) ^ 2 1

2 ∗c o s ( x ) ^ 2 - s i n ( x ) ^ 2 3 ∗c o s ( x ) ^ 2 - 1

c os ( x) ^ 2- s i n( x) ^ 2 c o s ( 2 ∗x )

7-50
Simplifications and Substitutions

f sim ple(f)

c o s ( x ) +( - s i n ( x ) ^ 2 ) ^ ( 1 / 2 ) c o s ( x ) +i ∗s i n ( x )

c o s ( x ) +i ∗s i n ( x ) e x p ( i ∗x )

c o s ( 3 ∗a c o s ( x ) ) 4 ∗x ^ 3 - 3 ∗x

Substitutions
Th er e a r e t wo fu n ct ion s for sym bolic su bst it u t ion : s u b e x p r a n d s u b s .

subexpr
Th ese com m a n ds
s y ms a x
s = s o l v e ( x ^ 3 +a * x +1 )

solve t h e equ a t ion x ^ 3 +a * x +1 = 0 f o r x .


s =
[ 1 / 6 * ( - 1 0 8 +1 2 * ( 1 2 * a ^ 3 +8 1 ) ^ ( 1 / 2 ) ) ^ ( 1 / 3 ) - 2 * a /
( - 1 0 8 +1 2 * ( 1 2 * a ^ 3 +8 1 ) ^ ( 1 / 2 ) ) ^ ( 1 / 3 ) ]
[ - 1 / 1 2 * ( - 1 0 8 +1 2 * ( 1 2 * a ^ 3 +8 1 ) ^ ( 1 / 2 ) ) ^ ( 1 / 3 ) +a /
( - 1 0 8 +1 2 * ( 1 2 * a ^ 3 +8 1 ) ^ ( 1 / 2 ) ) ^ ( 1 / 3 ) +1 / 2 * i * 3 ^ ( 1 / 2 ) * ( 1 /
6 * ( - 1 0 8 +1 2 * ( 1 2 * a ^ 3 +8 1 ) ^ ( 1 / 2 ) ) ^ ( 1 / 3 ) +2 * a /
( - 1 0 8 +1 2 * ( 1 2 * a ^ 3 +8 1 ) ^ ( 1 / 2 ) ) ^ ( 1 / 3 ) ) ]
[ - 1 / 1 2 * ( - 1 0 8 +1 2 * ( 1 2 * a ^ 3 +8 1 ) ^ ( 1 / 2 ) ) ^ ( 1 / 3 ) +a /
( - 1 0 8 +1 2 * ( 1 2 * a ^ 3 +8 1 ) ^ ( 1 / 2 ) ) ^ ( 1 / 3 ) - 1 / 2 * i * 3 ^ ( 1 / 2 ) * ( 1 /
6 * ( - 1 0 8 +1 2 * ( 1 2 * a ^ 3 +8 1 ) ^ ( 1 / 2 ) ) ^ ( 1 / 3 ) +2 * a /
( - 1 0 8 +1 2 * ( 1 2 * a ^ 3 +8 1 ) ^ ( 1 / 2 ) ) ^ ( 1 / 3 ) ) ]

7-51
7 Symbolic Math Toolbox

Use t h e pr et t y fu n ct ion t o displa y s in a m or e r ea da ble for m .


pr e t t y( s )

s =
[ 1/ 3 a ]
[ 1 / 6 %1 - 2 ----- ]
[ 1/ 3 ]
[ %1 ]
[ ]
[ 1/ 3 a 1/ 2 / 1/ 3 a \ ]
[ - 1 / 1 2 %1 + - - - - - + 1/ 2 i 3 | 1 / 6 %1 + 2 -----| ]
[ 1/ 3 | 1/ 3| ]
[ %1 \ %1 / ]
[ ]
[ 1/ 3 a 1/ 2 / 1/ 3 a \ ]
[ - 1 / 1 2 %1 + - - - - - - 1/ 2 i 3 | 1 / 6 %1 + 2 -----| ]
[ 1/ 3 | 1/ 3| ]
[ %1 \ %1 / ]

3 1/ 2
%1 : = - 1 0 8 + 1 2 ( 1 2 a + 81)

Th e p r e t t y com m a n d in h er it s t h e %n (n , a n in t eger ) n ot a t ion fr om Ma ple t o


den ot e su bexpr ession s t h a t occu r m u lt iple t im es in t h e sym bolic object . Th e
s u b e x p r fu n ct ion a llows you t o sa ve t h ese com m on su bexpr ession s a s well a s
t h e sym bolic object r ewr it t en in t er m s of t h e su bexpr ession s. Th e
su bexpr ession s a r e sa ved in a colu m n vect or ca lled s i g ma .
Con t in u in g wit h t h e exa m ple
r = s ube xpr ( s )

r et u r n s
s i g ma =
- 1 0 8 +1 2 * ( 1 2 * a ^ 3 +8 1 ) ^ ( 1 / 2 )

r =
[ 1 / 6 * s i g ma ^ ( 1 / 3 ) - 2 * a / s i g ma ^ ( 1 / 3 ) ]
[ - 1 / 1 2 * s i g ma ^ ( 1 / 3 ) +a / s i g ma ^ ( 1 / 3 ) +1 / 2 * i * 3 ^ ( 1 / 2 ) * ( 1 / 6 * s i g ma ^
( 1 / 3 ) +2 * a / s i g ma ^ ( 1 / 3 ) ) ]

7-52
Simplifications and Substitutions

[ - 1 / 1 2 * s i g ma ^ ( 1 / 3 ) +a / s i g ma ^ ( 1 / 3 ) - 1 / 2 * i * 3 ^ ( 1 / 2 ) * ( 1 / 6 * s i g ma ^
( 1 / 3 ) +2 * a / s i g ma ^ ( 1 / 3 ) ) ]

Not ice t h a t s u b e x p r cr ea t es t h e va r ia ble s i g ma in t h e MATLAB wor kspa ce.


You ca n ver ify t h is by t ypin g wh o s , or t h e com m a n d
s i g ma

wh ich r et u r n s
s i g ma =
- 1 0 8 +1 2 * ( 1 2 * a ^ 3 +8 1 ) ^ ( 1 / 2 )

subs
Let ’s fin d t h e eigen va lu es a n d eigen vect or s of a cir cu la n t m a t r ix A.
s y ms a b c
A = [ a b c ; b c a ; c a b] ;
[ v , E] = e i g ( A)

v =

[ - ( a +( b ^ 2 - b * a - c * b - c * a +a ^ 2 +c ^ 2 ) ^ ( 1 / 2 ) - b ) / ( a - c ) ,
- ( a - ( b ^ 2 - b * a - c * b - c * a +a ^ 2 +c ^ 2 ) ^ ( 1 / 2 ) - b ) / ( a - c ) , 1]
[ - ( b - c - ( b ^ 2 - b * a - c * b - c * a +a ^ 2 +c ^ 2 ) ^ ( 1 / 2 ) ) / ( a - c ) ,
- ( b - c +( b ^ 2 - b * a - c * b - c * a +a ^ 2 +c ^ 2 ) ^ ( 1 / 2 ) ) / ( a - c ) , 1]
[ 1,
1, 1]

E =

[ ( b^ 2- b*a - c *b-
c * a +a ^ 2 +c ^ 2 ) ^ ( 1 / 2 ) , 0, 0]
[ 0, - ( b^ 2- b*a - c *b-
c * a +a ^ 2 +c ^ 2 ) ^ ( 1 / 2 ) , 0]
[ 0, 0, b +c +a ]

Su ppose we wa n t t o r epla ce t h e r a t h er len gt h y expr ession


( b ^ 2 - b * a - c * b - c * a +a ^ 2 +c ^ 2 ) ^ ( 1 / 2 )

7-53
7 Symbolic Math Toolbox

t h r ou gh ou t v a n d E. We fir st u se s u b e x p r
v = s u b e x p r ( v , ' S' )

wh ich r et u r n s
S =
( b ^ 2 - b * a - c * b - c * a +a ^ 2 +c ^ 2 ) ^ ( 1 / 2 )

v =
[ - ( a +S- b ) / ( a - c ) , - ( a - S- b ) / ( a - c ) , 1]
[ - ( b - c - S) / ( a - c ) , - ( b - c +S) / ( a - c ) , 1]
[ 1, 1, 1]

Next , su bst it u t e t h e sym bol S in t o E wit h


E = s u b s ( E, S, ' S' )

E =
[ S, 0, 0]
[ 0, - S, 0]
[ 0, 0 , b +c +a ]

Now su ppose we wa n t t o eva lu a t e v a t a = 1 0 . We ca n do t h is u sin g t h e s u b s


com m a n d.
s ubs ( v, a , 10)

Th is r epla ces a ll occu r r en ces of a in v wit h 10.


[ - ( 1 0 +S- b ) / ( 1 0 - c ) , - ( 1 0 - S- b ) / ( 1 0 - c ) , 1]
[ - ( b - c - S) / ( 1 0 - c ) , - ( b - c +S) / ( 1 0 - c ) , 1]
[ 1, 1, 1]

Not ice, h owever , t h a t t h e sym bolic expr ession r epr esen t ed by S is u n a ffect ed by
t h is su bst it u t ion . Th a t is, t h e sym bol a in S is n ot r epla ced by 10. Th e s u b s
com m a n d is a lso a u sefu l fu n ct ion for su bst it u t in g in a va r iet y of va lu es for
sever a l va r ia bles in a pa r t icu la r expr ession . Let ’s look a t S . Su ppose t h a t in
a ddit ion t o su bst it u t in g a = 1 0 , we a lso wa n t t o su bst it u t e t h e va lu es for 2 a n d
10 for b a n d c , r espect ively. Th e wa y t o do t h is is t o set va lu es for a , b , a n d c in
t h e wor kspa ce. Th en s u b s eva lu a t es it s in pu t u sin g t h e exist in g sym bolic a n d
dou ble va r ia bles in t h e cu r r en t wor kspa ce. In ou r exa m ple, we fir st set

7-54
Simplifications and Substitutions

a = 10; b = 2; c = 10;
s u b s ( S)

a ns =
8

To look a t t h e con t en t s of ou r wor kspa ce, t ype wh o s , wh ich gives


Na me Si z e By t e s Cl a s s

A 3x3 878 s ym obj ect


E 3x3 888 s ym obj ect
S 1x1 186 s ym obj ect
a 1x1 8 doubl e ar r ay
a ns 1x1 140 s ym obj ect
b 1x1 8 doubl e ar r ay
c 1x1 8 doubl e ar r ay
v 3x3 982 s ym obj ect

a , b , a n d c a r e n ow va r ia bles of cla ss d o u b l e wh ile A, E, S , a n d v r em a in sym bolic


expr ession s (cla ss s y m).
If you wa n t t o pr eser ve a , b , a n d c a s sym bolic va r ia bles, bu t st ill a lt er t h eir
va lu e wit h in S , u se t h is pr ocedu r e.
s y ms a b c
s u b s ( S, { a , b , c } , { 1 0 , 2 , 1 0 } )

a ns =
8

Typin g wh o s r evea ls t h a t a , b , a n d c r em a in 1-by-1 s y m object s.


Th e s u b s com m a n d ca n be com bin ed wit h d o u b l e t o eva lu a t e a sym bolic
expr ession n u m er ica lly. Su ppose we h a ve
s y ms t
M = ( 1- t ^ 2) *e xp( - 1/ 2*t ^ 2) ;
P = ( 1- t ^ 2) *s e c h( t ) ;

a n d wa n t t o see h ow Ma n d P differ gr a ph ica lly.


On e a ppr oa ch is t o t ype
e z p l o t ( M) ; h o l d o n ; e z p l o t ( P)

7-55
7 Symbolic Math Toolbox

bu t t h is plot

(1−t2) sech(t)

0.8

0.6

0.4

0.2

−0.2

−0.4

−0.6

−0.8

−6 −4 −2 0 2 4 6
t

does n ot r ea dily h elp u s iden t ify t h e cu r ves.


In st ea d, com bin e s u b s , d o u b l e , a n d p l o t
T = - 6: 0. 05: 6;
MT = d o u b l e ( s u b s ( M, t , T) ) ;
PT = d o u b l e ( s u b s ( P, t , T) ) ;
p l o t ( T, MT, ' b ' , T, PT, ' r - . ' )
t i t l e( ' ' )
l e g e n d ( ' M' , ' P' )
xl a be l ( ' t ' ) ; gr i d

t o pr odu ce a m u lt icolor ed gr a ph t h a t in dica t es t h e differ en ce bet ween Ma n d P .

7-56
Simplifications and Substitutions

1
M
P
0.8

0.6

0.4

0.2

−0.2

−0.4

−0.6

−0.8

−1
−6 −4 −2 0 2 4 6
t

F in a lly t h e u se of s u b s wit h st r in gs gr ea t ly fa cilit a t es t h e solu t ion of pr oblem s


in volvin g t h e F ou r ier , La pla ce, or z-t r a n sfor m s.

7-57
7 Symbolic Math Toolbox

Va ria ble-Precision Arithmetic


O ver view
Th er e a r e t h r ee differ en t kin ds of a r it h m et ic oper a t ion s in t h is t oolbox.

Nu m er ic MATLAB’s floa t in g-poin t a r it h m et ic


Ra t ion a l Ma ple’s exa ct sym bolic a r it h m et ic
VPA Ma ple’s va r ia ble-pr ecision a r it h m et ic

F or exa m ple, t h e MATLAB st a t em en t s


f o r ma t l o n g
1 / 2 +1 / 3

u se n u m er ic com pu t a t ion t o pr odu ce


0. 83333333333333

Wit h t h e Sym bolic Ma t h Toolbox, t h e st a t em en t


s y m( 1 / 2 ) +1 / 3

u ses sym bolic com pu t a t ion t o yield


5/ 6

An d, a lso wit h t h e t oolbox, t h e st a t em en t s


di gi t s ( 25)
v p a ( ' 1 / 2 +1 / 3 ' )

u se va r ia ble-pr ecision a r it h m et ic t o r et u r n
. 8333333333333333333333333

Th e floa t in g-poin t oper a t ion s u sed by n u m er ic a r it h m et ic a r e t h e fa st est of t h e


t h r ee, a n d r equ ir e t h e lea st com pu t er m em or y, bu t t h e r esu lt s a r e n ot exa ct .
Th e n u m ber of digit s in t h e pr in t ed ou t pu t of MATLAB’s dou ble qu a n t it ies is
con t r olled by t h e f o r ma t st a t em en t , bu t t h e in t er n a l r epr esen t a t ion is a lwa ys
t h e eigh t -byt e floa t in g-poin t r epr esen t a t ion pr ovided by t h e pa r t icu la r
com pu t er h a r dwa r e.

7-58
Variable-Precision Arithmetic

In t h e com pu t a t ion of t h e n u m er ic r esu lt a bove, t h er e a r e a ct u a lly t h r ee


r ou n doff er r or s, on e in t h e division of 1 by 3, on e in t h e a ddit ion of 1/2 t o t h e
r esu lt of t h e division , a n d on e in t h e bin a r y t o decim a l con ver sion for t h e
pr in t ed ou t pu t . On com pu t er s t h a t u se IE E E floa t in g-poin t st a n da r d
a r it h m et ic, t h e r esu lt in g in t er n a l va lu e is t h e bin a r y expa n sion of 5/6,
t r u n ca t ed t o 53 bit s. Th is is a ppr oxim a t ely 16 decim a l digit s. Bu t , in t h is
pa r t icu la r ca se, t h e pr in t ed ou t pu t sh ows on ly 15 digit s.
Th e sym bolic oper a t ion s u sed by r a t ion a l a r it h m et ic a r e pot en t ia lly t h e m ost
expen sive of t h e t h r ee, in t er m s of bot h com pu t er t im e a n d m em or y. Th e r esu lt s
a r e exa ct , a s lon g a s en ou gh t im e a n d m em or y a r e a va ila ble t o com plet e t h e
com pu t a t ion s.
Va r ia ble-pr ecision a r it h m et ic fa lls in bet ween t h e ot h er t wo in t er m s of bot h
cost a n d a ccu r a cy. A globa l pa r a m et er , set by t h e fu n ct ion d i g i t s , con t r ols t h e
n u m ber of sign ifica n t decim a l digit s. In cr ea sin g t h e n u m ber of digit s in cr ea ses
t h e a ccu r a cy, bu t a lso in cr ea ses bot h t h e t im e a n d m em or y r equ ir em en t s. Th e
defa u lt va lu e of d i g i t s is 32, cor r espon din g r ou gh ly t o floa t in g-poin t a ccu r a cy.
Th e Ma ple docu m en t a t ion u ses t h e t er m “h a r dwa r e floa t in g-poin t ” for wh a t we
a r e ca llin g “n u m er ic” or “floa t in g-poin t ” a n d u ses t h e t er m “floa t in g-poin t
a r it h m et ic” for wh a t we a r e ca llin g “va r ia ble-pr ecision a r it h m et ic.”

Ex a m ple: Using the Different Kinds of Arithm etic


Rational Arithmetic
By defa u lt , t h e Sym bolic Ma t h Toolbox u ses r a t ion a l a r it h m et ic oper a t ion s, i.e.,
Ma ple’s exa ct sym bolic a r it h m et ic. Ra t ion a l a r it h m et ic is in voked wh en you
cr ea t e sym bolic va r ia bles u sin g t h e s y m fu n ct ion .
Th e s y m fu n ct ion con ver t s a dou ble m a t r ix t o it s sym bolic for m . F or exa m ple, if
t h e dou ble m a t r ix is
A =
1. 1000 1. 2000 1. 3000
2. 1000 2. 2000 2. 3000
3. 1000 3. 2000 3. 3000

it s sym bolic for m , S = s y m( A) , is


S =
[ 11/ 10, 6/ 5, 13/ 10]

7-59
7 Symbolic Math Toolbox

[ 21/ 10, 11/ 5, 23/ 10]


[ 31/ 10, 16/ 5, 33/ 10]

F or t h is m a t r ix A, it is possible t o discover t h a t t h e elem en t s a r e t h e r a t ios of


sm a ll in t eger s, so t h e sym bolic r epr esen t a t ion is for m ed fr om t h ose in t eger s.
On t h e ot h er h a n d, t h e st a t em en t
E = [ e xp( 1) s qr t ( 2) ; l og( 3) r a nd]

r et u r n s a m a t r ix
E =
2. 71828182845905 1. 41421356237310
1. 09861228866811 0. 21895918632809

wh ose elem en t s a r e n ot t h e r a t ios of sm a ll in t eger s, so s y m( E) r epr odu ces t h e


floa t in g-poin t r epr esen t a t ion in a sym bolic for m .
[ 3060513257434037*2^ ( - 50) , 3184525836262886*2^ ( - 51) ]
[ 2473854946935174*2^ ( - 51) , 3944418039826132*2^ ( - 54) ]

Variable-Precision Numbers
Va r ia ble-pr ecision n u m ber s a r e dist in gu ish ed fr om t h e exa ct r a t ion a l
r epr esen t a t ion by t h e pr esen ce of a decim a l poin t . A power of 10 sca le fa ct or ,
den ot ed by ' e ' , is a llowed. To u se va r ia ble-pr ecision in st ea d of r a t ion a l
a r it h m et ic, cr ea t e you r va r ia bles u sin g t h e v p a fu n ct ion .
F or m a t r ices wit h pu r ely dou ble en t r ies, t h e v p a fu n ct ion gen er a t es t h e
r epr esen t a t ion t h a t is u sed wit h va r ia ble-pr ecision a r it h m et ic. Con t in u in g on
wit h ou r exa m ple, a n d u sin g d i g i t s ( 4 ) , a pplyin g v p a t o t h e m a t r ix S
v p a ( S)

gen er a t es t h e ou t pu t
S =
[ 1. 100, 1. 200, 1. 300]
[ 2. 100, 2. 200, 2. 300]
[ 3. 100, 3. 200, 3. 300]

a n d wit h d i g i t s ( 2 5 )
F = v p a ( E)

7-60
Variable-Precision Arithmetic

gen er a t es
F =
[ 2. 718281828459045534884808, 1. 414213562373094923430017]
[ 1. 098612288668110004152823, . 2189591863280899719512718]

Converting to Floating-Point
To con ver t a r a t ion a l or va r ia ble-pr ecision n u m ber t o it s MATLAB
floa t in g-poin t r epr esen t a t ion , u se t h e d o u b l e fu n ct ion .
In ou r exa m ple, bot h d o u b l e ( s y m( E) ) a n d d o u b l e ( v p a ( E) ) r et u r n E.

Another Ex a m ple
Th e n ext exa m ple is per h a ps m or e in t er est in g. St a r t wit h t h e sym bolic
expr ession
f = s y m( ' e x p ( p i * s q r t ( 1 6 3 ) ) ' )

Th e st a t em en t
doubl e ( f )

pr odu ces t h e pr in t ed floa t in g-poin t va lu e


2 . 6 2 5 3 7 4 1 2 6 4 0 7 6 8 7 e +1 7

Usin g t h e secon d a r gu m en t of v p a t o specify t h e n u m ber of digit s,


vpa ( f , 18)

r et u r n s
262537412640768744.

wh er ea s
vpa ( f , 25)

r et u r n s
262537412640768744. 0000000

We su spect t h a t f m igh t a ct u a lly h a ve a n in t eger va lu e. Th is su spicion is


r ein for ced by t h e 30 digit va lu e, v p a ( f , 3 0 )
262537412640768743. 999999999999

7-61
7 Symbolic Math Toolbox

F in a lly, t h e 40 digit va lu e, v p a ( f , 4 0 )
262537412640768743. 9999999999992500725944

sh ows t h a t f is ver y close t o, bu t n ot exa ct ly equ a l t o, a n in t eger .

7-62
Linear Algebra

Linea r Algebra
Ba sic Algebra ic O pera tions
Ba sic a lgebr a ic oper a t ion s on sym bolic object s a r e t h e sa m e a s oper a t ion s on
MATLAB object s of cla ss d o u b l e . Th is is illu st r a t ed in t h e followin g exa m ple.
Th e Given s t r a n sfor m a t ion pr odu ces a pla n e r ot a t ion t h r ou gh t h e a n gle t . Th e
st a t em en t s
s y ms t ;
G = [ c os ( t ) s i n( t ) ; - s i n( t ) c os ( t ) ]

cr ea t e t h is t r a n sfor m a t ion m a t r ix.


G =
[ c os ( t ) , s i n( t ) ]
[ - s i n( t ) , c os ( t ) ]

Applyin g t h e Given s t r a n sfor m a t ion t wice sh ou ld sim ply be a r ot a t ion t h r ou gh


t wice t h e a n gle. Th e cor r espon din g m a t r ix ca n be com pu t ed by m u lt iplyin g G
by it self or by r a isin g G t o t h e secon d power . Bot h
A = G* G

and
A = G^ 2

pr odu ce
A =
[ c os ( t ) ^ 2- s i n( t ) ^ 2, 2*c os ( t ) *s i n( t ) ]
[ - 2*c os ( t ) *s i n( t ) , c os ( t ) ^ 2- s i n( t ) ^ 2]

Th e s i mp l e fu n ct ion
A = s i mp l e ( A)

u ses a t r igon om et r ic iden t it y t o r et u r n t h e expect ed for m by t r yin g sever a l


differ en t iden t it ies a n d pickin g t h e on e t h a t pr odu ces t h e sh or t est
r epr esen t a t ion .

7-63
7 Symbolic Math Toolbox

A =
[ c os ( 2*t ) , s i n( 2*t ) ]
[ - s i n( 2*t ) , c os ( 2*t ) ]

A Given s r ot a t ion is a n or t h ogon a l m a t r ix, so it s t r a n spose is it s in ver se.


Con fir m in g t h is by
I = G. ' *G

wh ich pr odu ces


I =
[ c o s ( t ) ^ 2 +s i n ( t ) ^ 2 , 0]
[ 0 , c o s ( t ) ^ 2 +s i n ( t ) ^ 2 ]

a n d t h en
I = s i mp l e ( I )
I =
[ 1, 0]
[ 0, 1]

Linea r Algebra ic O pera tions


Let ’s do sever a l ba sic lin ea r a lgebr a ic oper a t ion s.
Th e com m a n d
H = hi l b( 3)

gen er a t es t h e 3-by-3 H ilber t m a t r ix. Wit h f o r ma t s h o r t , MATLAB pr in t s


H =
1. 0000 0. 5000 0. 3333
0. 5000 0. 3333 0. 2500
0. 3333 0. 2500 0. 2000

Th e com pu t ed elem en t s of H a r e floa t in g-poin t n u m ber s t h a t a r e t h e r a t ios of


sm a ll in t eger s. In deed, H is a MATLAB a r r a y of cla ss d o u b l e . Con ver t in g H t o
a sym bolic m a t r ix
H = s y m( H)

7-64
Linear Algebra

gives
[ 1, 1/ 2, 1/ 3]
[ 1/ 2, 1/ 3, 1/ 4]
[ 1/ 3, 1/ 4, 1/ 5]

Th is a llows su bsequ en t sym bolic oper a t ion s on H t o pr odu ce r esu lt s t h a t


cor r espon d t o t h e in fin it ely pr ecise H ilber t m a t r ix, s y m( h i l b ( 3 ) ) , n ot it s
floa t in g-poin t a ppr oxim a t ion , h i l b ( 3 ) . Th er efor e,
i n v ( H)

pr odu ces
[ 9, - 36, 30]
[ - 36, 192, - 180]
[ 30, - 180, 180]

and
d e t ( H)

yields
1/ 2160

We ca n u se t h e ba cksla sh oper a t or t o solve a syst em of sim u lt a n eou s lin ea r


equ a t ion s. Th e com m a n ds
b = [ 1 1 1] '
x = H\ b % So l v e Hx = b

pr odu ce t h e solu t ion


[ 3]
[ - 24]
[ 30]

All t h r ee of t h ese r esu lt s, t h e in ver se, t h e det er m in a n t , a n d t h e solu t ion t o t h e


lin ea r syst em , a r e t h e exa ct r esu lt s cor r espon din g t o t h e in fin it ely pr ecise,
r a t ion a l, H ilber t m a t r ix. On t h e ot h er h a n d, u sin g d i g i t s ( 1 6 ) , t h e com m a n d
V = vpa ( hi l b( 3) )

7-65
7 Symbolic Math Toolbox

r et u r n s
[ 1. , . 5000000000000000, . 3333333333333333]
[ . 5000000000000000, . 3333333333333333, . 2500000000000000]
[ . 3333333333333333, . 2500000000000000, . 2000000000000000]

Th e decim a l poin t s in t h e r epr esen t a t ion of t h e in dividu a l elem en t s a r e t h e


sign a l t o u se va r ia ble-pr ecision a r it h m et ic. Th e r esu lt of ea ch a r it h m et ic
oper a t ion is r ou n ded t o 16 sign ifica n t decim a l digit s. Wh en in ver t in g t h e
m a t r ix, t h ese er r or s a r e m a gn ified by t h e m a t r ix con dit ion n u m ber , wh ich for
h i l b ( 3 ) is a bou t 500. Con sequ en t ly,
i n v ( V)

wh ich r et u r n s
[ 9. 000000000000082, - 36. 00000000000039, 30. 00000000000035]
[ - 36. 00000000000039, 192. 0000000000021, - 180. 0000000000019]
[ 30. 00000000000035, - 180. 0000000000019, 180. 0000000000019]

sh ows t h e loss of t wo digit s. So does


d e t ( V)

wh ich gives
. 462962962962958e - 3

and
V\ b

wh ich is
[ 3. 000000000000041]
[ - 24. 00000000000021]
[ 30. 00000000000019]

Sin ce H is n on sin gu la r , t h e n u ll spa ce of H


n u l l ( H)

a n d t h e colu m n spa ce of H
c o l s p a c e ( H)

7-66
Linear Algebra

pr odu ce a n em pt y m a t r ix a n d a per m u t a t ion of t h e iden t it y m a t r ix,


r espect ively. To m a ke a m or e in t er est in g exa m ple, let ’s t r y t o fin d a va lu e s for
H( 1 , 1 ) t h a t m a kes H sin gu la r . Th e com m a n ds
s y ms s
H( 1 , 1 ) = s
Z = d e t ( H)
s o l = s o l v e ( Z)

pr odu ce
H =
[ s , 1/ 2, 1/ 3]
[ 1/ 2, 1/ 3, 1/ 4]
[ 1/ 3, 1/ 4, 1/ 5]

Z =
1/ 240*s - 1/ 270

s ol =
8/ 9

Th en
H = s u b s ( H, s , s o l )

su bst it u t es t h e com pu t ed va lu e of s o l for s in H t o give


H =
[ 8/ 9, 1/ 2, 1/ 3]
[ 1/ 2, 1/ 3, 1/ 4]
[ 1/ 3, 1/ 4, 1/ 5]

Now, t h e com m a n d
d e t ( H)

r et u r n s
a ns =
0

and
i n v ( H)

7-67
7 Symbolic Math Toolbox

pr odu ces a n er r or m essa ge


? ? ? e r r o r u s i n g ==> i n v
Er r o r , ( i n i n v e r s e ) s i n g u l a r ma t r i x

beca u se H is sin gu la r . F or t h is m a t r ix, Z = n u l l ( H) a n d C = c o l s p a c e ( H) a r e


n on t r ivia l.
Z =
[ 1]
[ - 4]
[ 10/ 3]

C =
[ 0, 1]
[ 1, 0]
[ 6/ 5, - 3/ 10]

It sh ou ld be poin t ed ou t t h a t even t h ou gh H is sin gu la r , v p a ( H) is n ot . F or a n y


in t eger va lu e d , set t in g
di gi t s ( d)

a n d t h en com pu t in g
d e t ( v p a ( H) )
i n v ( v p a ( H) )

r esu lt s in a det er m in a n t of size 1 0 ^ ( - d ) a n d a n in ver se wit h elem en t s on t h e


or der of 1 0 ^ d .

Eigenva lues
Th e sym bolic eigen va lu es of a squ a r e m a t r ix A or t h e sym bolic eigen va lu es a n d
eigen vect or s of A a r e com pu t ed, r espect ively, u sin g t h e com m a n ds
E = e i g ( A)
[ V, E] = e i g ( A)

Th e va r ia ble-pr ecision cou n t er pa r t s a r e


E = e i g ( v p a ( A) )
[ V, E] = e i g ( v p a ( A) )

7-68
Linear Algebra

Th e eigen va lu es of A a r e t h e zer os of t h e ch a r a ct er ist ic polyn om ia l of A,


d e t ( A- x * I ) , wh ich is com pu t ed by
p o l y ( A)

Th e m a t r ix H fr om t h e la st sect ion pr ovides ou r fir st exa m ple.


H =
[ 8/ 9, 1/ 2, 1/ 3]
[ 1/ 2, 1/ 3, 1/ 4]
[ 1/ 3, 1/ 4, 1/ 5]

Th e m a t r ix is sin gu la r , so on e of it s eigen va lu es m u st be zer o. Th e st a t em en t


[ T, E] = e i g ( H)

pr odu ces t h e m a t r ices T a n d E. Th e colu m n s of T a r e t h e eigen vect or s of H.


T =

[ 1 , 2 8 / 1 5 3 +2 / 1 5 3 * 1 2 5 8 9 ^ ( 1 / 2 ) , 28/ 153- 2/ 153*12589^ ( 12) ]


[ - 4, 1, 1]
[ 1 0 / 3 , 9 2 / 2 5 5 - 1 / 2 5 5 * 1 2 5 8 9 ^ ( 1 / 2 ) , 2 9 2 / 2 5 5 +1 / 2 5 5 * 1 2 5 8 9 ^ ( 1 2 ) ]

Sim ila r ly, t h e dia gon a l elem en t s of E a r e t h e eigen va lu es of H.


E =

[ 0, 0, 0]
[ 0 , 3 2 / 4 5 +1 / 1 8 0 * 1 2 5 8 9 ^ ( 1 / 2 ) , 0]
[ 0, 0, 32/ 45- 1/ 180*12589^ ( 1/ 2) ]

It m a y be ea sier t o u n der st a n d t h e st r u ct u r e of t h e m a t r ices of eigen vect or s, T,


a n d eigen va lu es, E, if we con ver t T a n d E t o decim a l n ot a t ion . We pr oceed a s
follows. Th e com m a n ds
Td = d o u b l e ( T)
Ed = d o u b l e ( E)

r et u r n
Td =
1. 0000 1. 6497 - 1. 2837
- 4. 0000 1. 0000 1. 0000
3. 3333 0. 7051 1. 5851

7-69
7 Symbolic Math Toolbox

Ed =
0 0 0
0 1. 3344 0
0 0 0. 0878

Th e fir st eigen va lu e is zer o. Th e cor r espon din g eigen vect or (t h e fir st colu m n of
Td ) is t h e sa m e a s t h e ba sis for t h e n u ll spa ce fou n d in t h e la st sect ion . Th e
ot h er t wo eigen va lu es a r e t h e r esu lt of a pplyin g t h e qu a dr a t ic for m u la t o
x ^ 2 - 6 4 / 4 5 * x +2 5 3 / 2 1 6 0

wh ich is t h e qu a dr a t ic fa ct or in f a c t o r ( p o l y ( H) ) .
s y ms x
g = s i mp l e ( f a c t o r ( p o l y ( H) ) / x ) ;
s ol ve ( g)

Closed for m sym bolic expr ession s for t h e eigen va lu es a r e possible on ly wh en


t h e ch a r a ct er ist ic polyn om ia l ca n be expr essed a s a pr odu ct of r a t ion a l
polyn om ia ls of degr ee fou r or less. Th e Rosser m a t r ix is a cla ssic n u m er ica l
a n a lysis t est m a t r ix t h a t h a ppen s t o illu st r a t e t h is r equ ir em en t . Th e
st a t em en t
R = s y m( g a l l e r y ( ' r o s s e r ' ) )

gen er a t es
R =
[ 611 196 - 192 407 -8 - 52 - 49 29]
[ 196 899 113 - 192 - 71 - 43 -8 - 44]
[ - 192 113 899 196 61 49 8 52]
[ 407 - 192 196 611 8 44 59 - 23]
[ -8 - 71 61 8 411 - 599 208 208]
[ - 52 - 43 49 44 - 599 411 208 208]
[ - 49 -8 8 59 208 208 99 - 911]
[ 29 - 44 52 - 23 208 208 - 911 99]

Th e com m a n ds
p = p o l y ( R) ;
pr e t t y( f a c t or ( p) )

7-70
Linear Algebra

pr odu ce
2 2 2
x ( x - 1020) ( x - 1020 x + 100) ( x - 1040500) ( x - 1000)

Th e ch a r a ct er ist ic polyn om ia l (of degr ee 8) fa ct or s n icely in t o t h e pr odu ct of t wo


lin ea r t er m s a n d t h r ee qu a dr a t ic t er m s. We ca n see im m edia t ely t h a t fou r of
t h e eigen va lu es a r e 0, 1020, a n d a dou ble r oot a t 1000. Th e ot h er fou r r oot s a r e
obt a in ed fr om t h e r em a in in g qu a dr a t ics. Use
e i g ( R)

t o fin d a ll t h ese va lu es
[ 0]
[ 1020]
[ 5 1 0 +1 0 0 * 2 6 ^ ( 1 / 2 ) ]
[ 510- 100*26^ ( 1/ 2) ]
[ 10*10405^ ( 1/ 2) ]
[ - 10*10405^ ( 1/ 2) ]
[ 1000]
[ 1000]

Th e Rosser m a t r ix is n ot a t ypica l exa m ple; it is r a r e for a fu ll 8-by-8 m a t r ix t o


h a ve a ch a r a ct er ist ic polyn om ia l t h a t fa ct or s in t o su ch sim ple for m . If we
ch a n ge t h e t wo “cor n er ” elem en t s of R fr om 29 t o 30 wit h t h e com m a n ds
S = R; S( 1 , 8 ) = 3 0 ; S( 8 , 1 ) = 3 0 ;

a n d t h en t r y
p = p o l y ( S)

we fin d
p =
4 0 2 5 0 9 6 8 2 1 3 6 0 0 0 0 0 +5 1 2 6 4 0 0 8 5 4 0 9 4 8 0 0 0 * x -
1 0 8 2 6 9 9 3 8 8 4 1 1 1 6 6 0 0 0 * x ^ 2 +4 2 8 7 8 3 2 9 1 2 7 1 9 7 6 0 * x ^ - 3 -
5 3 2 7 8 3 1 9 1 8 5 6 8 * x ^ 4 +8 2 7 0 6 0 9 0 * x ^ 5 +5 0 7 9 9 4 1 * x ^ 6 -
4 0 4 0 * x ^ 7 +x ^ 8

We a lso fin d t h a t f a c t o r ( p ) is p it self. Th a t is, t h e ch a r a ct er ist ic polyn om ia l


ca n n ot be fa ct or ed over t h e r a t ion a ls.

7-71
7 Symbolic Math Toolbox

F or t h is m odified Rosser m a t r ix
F = e i g ( S)

r et u r n s
F =
[ - 1020. 0532142558915165931894252600]
[ - . 17053529728768998575200874607757]
[ . 21803980548301606860857564424981]
[ 999. 94691786044276755320289228602]
[ 1000. 1206982933841335712817075454]
[ 1019. 5243552632016358324933278291]
[ 1019. 9935501291629257348091808173]
[ 1020. 4201882015047278185457498840]

Not ice t h a t t h ese va lu es a r e close t o t h e eigen va lu es of t h e or igin a l Rosser


m a t r ix. F u r t h er , t h e n u m er ica l va lu es of F a r e a r esu lt of Ma ple’s floa t in g-poin t
a r it h m et ic. Con sequ en t ly, differ en t set t in gs of d i g i t s do n ot a lt er t h e n u m ber
of digit s t o t h e r igh t of t h e decim a l pla ce.
It is a lso possible t o t r y t o com pu t e eigen va lu es of sym bolic m a t r ices, bu t closed
for m solu t ion s a r e r a r e. Th e Given s t r a n sfor m a t ion is gen er a t ed a s t h e m a t r ix
expon en t ia l of t h e elem en t a r y m a t r ix

A = 0 1
–1 0

Th e Sym bolic Ma t h Toolbox com m a n ds


s y ms t
A = s y m( [ 0 1 ; - 1 0 ] ) ;
G = e x p m( t * A)

r et u r n
[ c os ( t ) , s i n( t ) ]
[ - s i n( t ) , c os ( t ) ]

Next , t h e com m a n d
g = e i g ( G)

pr odu ces

7-72
Linear Algebra

g =
[ c o s ( t ) +( c o s ( t ) ^ 2 - 1 ) ^ ( 1 / 2 ) ]
[ c os ( t ) - ( c os ( t ) ^ 2- 1) ^ ( 1/ 2) ]

We ca n u se s i mp l e t o sim plify t h is for m of g . In deed, r epea t ed a pplica t ion of


s i mp l e
f or j = 1: 4
[ g , h o w] = s i mp l e ( g )
e nd

pr odu ces t h e best r esu lt


g =
[ c o s ( t ) +( - s i n ( t ) ^ 2 ) ^ ( 1 / 2 ) ]
[ c os ( t ) - ( - s i n( t ) ^ 2) ^ ( 1/ 2) ]

how =
s i mp l i f y

g =
[ c o s ( t ) +i * s i n ( t ) ]
[ c os ( t ) - i *s i n( t ) ]

how =
r a d s i mp

g =
[ e xp( i *t ) ]
[ 1/ e xp( i *t ) ]

how =
c onve r t ( e xp)

g =
[ e xp( i *t ) ]
[ e xp( - i *t ) ]

how =
c o mb i n e

7-73
7 Symbolic Math Toolbox

Not ice t h e fir st a pplica t ion of s i mp l e u ses s i mp l i f y t o pr odu ce a su m of sin es


a n d cosin es. Next , s i mp l e in vokes r a d s i mp t o pr odu ce c o s ( t ) + i * s i n ( t ) for
t h e fir st eigen vect or . Th e t h ir d a pplica t ion of s i mp l e u ses c o n v e r t ( e x p ) t o
ch a n ge t h e sin es a n d cosin es t o com plex expon en t ia ls. Th e la st a pplica t ion of
s i mp l e u ses s i mp l i f y t o obt a in t h e fin a l for m .

Jorda n Ca nonica l Form


Th e J or da n ca n on ica l for m r esu lt s fr om a t t em pt s t o dia gon a lize a m a t r ix by a
sim ila r it y t r a n sfor m a t ion . F or a given m a t r ix A, fin d a n on sin gu la r m a t r ix V,
so t h a t i n v ( V) * A* V, or , m or e su ccin ct ly, J = V\ A* V, is “a s close t o dia gon a l a s
possible.” F or a lm ost a ll m a t r ices, t h e J or da n ca n on ica l for m is t h e dia gon a l
m a t r ix of eigen va lu es a n d t h e colu m n s of t h e t r a n sfor m a t ion m a t r ix a r e t h e
eigen vect or s. Th is a lwa ys h a ppen s if t h e m a t r ix is sym m et r ic or if it h a s
dist in ct eigen va lu es. Som e n on sym m et r ic m a t r ices wit h m u lt iple eigen va lu es
ca n n ot be dia gon a lized. Th e J or da n for m h a s t h e eigen va lu es on it s dia gon a l,
bu t som e of t h e su per dia gon a l elem en t s a r e on e, in st ea d of zer o. Th e st a t em en t
J = j o r d a n ( A)

com pu t es t h e J or da n ca n on ica l for m of A. Th e st a t em en t


[ V, J ] = j o r d a n ( A)

a lso com pu t es t h e sim ila r it y t r a n sfor m a t ion . Th e colu m n s of V a r e t h e


gen er a lized eigen vect or s of A.
Th e J or da n for m is ext r em ely sen sit ive t o per t u r ba t ion s. Alm ost a n y ch a n ge in
A ca u ses it s J or da n for m t o be dia gon a l. Th is m a kes it ver y difficu lt t o com pu t e
t h e J or da n for m r elia bly wit h floa t in g-poin t a r it h m et ic. It a lso im plies t h a t A
m u st be kn own exa ct ly (i.e., wit h ou t r ou n d-off er r or , et c.). It s elem en t s m u st be
in t eger s, or r a t ios of sm a ll in t eger s. In pa r t icu la r , t h e va r ia ble-pr ecision
ca lcu la t ion , j o r d a n ( v p a ( A) ) , is n ot a llowed.
F or exa m ple, let
A = s y m( [ 1 2 , 3 2 , 6 6 , 1 1 6 ; - 2 5 , - 7 6 , - 1 6 4 , - 2 9 4 ;
21, 66, 143, 256; - 6, - 19, - 41, - 73] )
A =
[ 12, 32, 66, 116]
[ - 25, - 76, - 164, - 294]
[ 21, 66, 143, 256]
[ - 6, - 19, - 41, - 73]

7-74
Linear Algebra

Th en
[ V, J ] = j o r d a n ( A)

pr odu ces
V =
[ 4, - 2, 4, 3]
[ - 6, 8, - 11, - 8]
[ 4, - 7, 10, 7]
[ - 1, 2, - 3, - 2]

J =
[ 1, 1, 0, 0]
[ 0, 1, 0, 0]
[ 0, 0, 2, 1]
[ 0, 0, 0, 2]

Th er efor e A h a s a dou ble eigen va lu e a t 1, wit h a sin gle J or da n block, a n d a


dou ble eigen va lu e a t 2, a lso wit h a sin gle J or da n block. Th e m a t r ix h a s on ly
t wo eigen vect or s, V( : , 1 ) a n d V( : , 3 ) . Th ey sa t isfy
A* V( : , 1 ) = 1 * V( : , 1 )
A* V( : , 3 ) = 2 * V( : , 3 )

Th e ot h er t wo colu m n s of V a r e gen er a lized eigen vect or s of gr a de 2. Th ey


sa t isfy
A* V( : , 2 ) = 1 * V( : , 2 ) + V( : , 1 )
A* V( : , 4 ) = 2 * V( : , 4 ) + V( : , 3 )

In m a t h em a t ica l n ot a t ion , wit h v j = v ( : , j ) , t h e colu m n s of V a n d eigen va lu es


sa t isfy t h e r ela t ion sh ips

( A – λ 2 I )v 4 = v 3

( A – λ 1 I )v 2 = v 1

7-75
7 Symbolic Math Toolbox

Singula r Va lue Decom position


On ly t h e va r ia ble-pr ecision n u m er ic com pu t a t ion of t h e sin gu la r va lu e
decom posit ion is a va ila ble in t h e t oolbox. On e r ea son for t h is is t h a t t h e
for m u la s t h a t r esu lt fr om sym bolic com pu t a t ion a r e u su a lly t oo lon g a n d
com plica t ed t o be of m u ch u se. If A is a sym bolic m a t r ix of floa t in g-poin t or
va r ia ble-pr ecision n u m ber s, t h en
S = s v d ( A)

com pu t es t h e sin gu la r va lu es of A t o a n a ccu r a cy det er m in ed by t h e cu r r en t


set t in g of d i g i t s . An d
[ U, S, V] = s v d ( A) ;

pr odu ces t wo or t h ogon a l m a t r ices, U a n d V, a n d a dia gon a l m a t r ix, S , so t h a t


A = U* S* V' ;

Let ’s look a t t h e n -by-n m a t r ix A wit h elem en t s defin ed by


A( i , j ) = 1 / ( i - j +1 / 2 )

F or n = 5 , t h e m a t r ix is
[ 2 -2 - 2/ 3 - 2/ 5 - 2/ 7]
[ 2/ 3 2 -2 - 2/ 3 - 2/ 5]
[ 2/ 5 2/ 3 2 -2 - 2/ 3]
[ 2/ 7 2/ 5 2/ 3 2 - 2]
[ 2/ 9 2/ 7 2/ 5 2/ 3 2]

It t u r n s ou t m a n y of t h e sin gu la r va lu es of t h ese m a t r ices a r e close t o π .


Th e m ost obviou s wa y of gen er a t in g t h is m a t r ix is
f o r i =1 : n
f o r j =1 : n
A( i , j ) = s y m( 1 / ( i - j +1 / 2 ) ) ;
e nd
e nd

Th e m ost efficien t wa y t o gen er a t e t h e m a t r ix is


[ J , I ] = me s h g r i d ( 1 : n ) ;
A = s y m( 1 . / ( I - J +1 / 2 ) ) ;

7-76
Linear Algebra

Sin ce t h e elem en t s of A a r e t h e r a t ios of sm a ll in t eger s, v p a ( A) pr odu ces a


va r ia ble-pr ecision r epr esen t a t ion , wh ich is a ccu r a t e t o d i g i t s pr ecision . H en ce
S = s v d ( v p a ( A) )

com pu t es t h e desir ed sin gu la r va lu es t o fu ll a ccu r a cy. Wit h n = 1 6 a n d


d i g i t s ( 3 0 ) , t h e r esu lt is
S =
[ 1. 20968137605668985332455685357 ]
[ 2. 69162158686066606774782763594 ]
[ 3. 07790297231119748658424727354 ]
[ 3. 13504054399744654843898901261 ]
[ 3. 14106044663470063805218371924 ]
[ 3. 14155754359918083691050658260 ]
[ 3. 14159075458605848728982577119 ]
[ 3. 14159256925492306470284863102 ]
[ 3. 14159265052654880815569479613 ]
[ 3. 14159265349961053143856838564 ]
[ 3. 14159265358767361712392612384 ]
[ 3. 14159265358975439206849907220 ]
[ 3. 14159265358979270342635559051 ]
[ 3. 14159265358979323325290142781 ]
[ 3. 14159265358979323843066846712 ]
[ 3. 14159265358979323846255035974 ]

Th er e a r e t wo wa ys t o com pa r e S wit h p i , t h e floa t in g-poin t r epr esen t a t ion of


π . In t h e vect or below, t h e fir st elem en t is com pu t ed by su bt r a ct ion wit h
va r ia ble-pr ecision a r it h m et ic a n d t h en con ver t ed t o a d o u b l e . Th e secon d
elem en t is com pu t ed wit h floa t in g-poin t a r it h m et ic.
f o r ma t s h o r t e
[ d o u b l e ( p i * o n e s ( 1 6 , 1 ) - S) p i - d o u b l e ( S) ]

Th e r esu lt s a r e
1. 9 3 1 9 e +0 0 1. 9 3 1 9 e +0 0
4. 4997e - 01 4. 4997e - 01
6. 3690e - 02 6. 3690e - 02
6. 5521e - 03 6. 5521e - 03
5. 3221e - 04 5. 3221e - 04
3. 5110e - 05 3. 5110e - 05
1. 8990e - 06 1. 8990e - 06

7-77
7 Symbolic Math Toolbox

8. 4335e - 08 8. 4335e - 08
3. 0632e - 09 3. 0632e - 09
9. 0183e - 11 9. 0183e - 11
2. 1196e - 12 2. 1196e - 12
3. 8846e - 14 3. 8636e - 14
5. 3504e - 16 4. 4409e - 16
5. 2097e - 18 0
3. 1975e - 20 0
9. 3024e - 23 0

Sin ce t h e r ela t ive a ccu r a cy of p i is p i * e p s , wh ich is 6 . 9 7 5 7 e - 1 6 , eit h er colu m n


con fir m s ou r su spicion t h a t fou r of t h e sin gu la r va lu es of t h e 1 6 -by-1 6 exa m ple
equ a l π t o floa t in g-poin t a ccu r a cy.

Eigenva lue Tra jectories


Th is exa m ple a pplies sever a l n u m er ic, sym bolic, a n d gr a ph ic t ech n iqu es t o
st u dy t h e beh a vior of m a t r ix eigen va lu es a s a pa r a m et er in t h e m a t r ix is
va r ied. Th is pa r t icu la r set t in g in volves n u m er ica l a n a lysis a n d per t u r ba t ion
t h eor y, bu t t h e t ech n iqu es illu st r a t ed a r e m or e widely a pplica ble.
In t h is exa m ple, we con sider a 3-by-3 m a t r ix A wh ose eigen va lu es a r e 1, 2, 3.
F ir st , we per t u r b A by a n ot h er m a t r ix E a n d pa r a m et er t : A → A + t E . As t
in cr ea ses fr om 0 t o 10 -6 , t h e eigen va lu es λ 1 = 1 , λ 2 = 2 , λ 3 = 3 ch a n ge t o
λ 1 ′ ≈ 1.5596 + 0.2726 i , λ 2 ′ ≈ 1.5596 – 0.2726 i , λ 3 ′ ≈ 2.8808 .

7-78
Linear Algebra

0.3 λ’(1)

0.2

0.1

λ(1) λ(2) λ(3)


0
λ’(3)

−0.1

−0.2

−0.3 λ’(2)

0 0.5 1 1.5 2 2.5 3 3.5

Th is, in t u r n , m ea n s t h a t for som e va lu e of t = τ, 0 < τ < 10 – 6 , t h e per t u r bed


m a t r ix A (t ) = A + t E h a s a dou ble eigen va lu e λ 1 = λ 2 .
Let ’s fin d t h e va lu e of t , ca lled τ , wh er e t h is h a ppen s.
Th e st a r t in g poin t is a MATLAB t est exa m ple, kn own a s g a l l e r y ( 3 ) .
A = ga l l e r y( 3)
A =
- 149 - 50 - 154
537 180 546
- 27 -9 - 25

Th is is a n exa m ple of a m a t r ix wh ose eigen va lu es a r e sen sit ive t o t h e effect s of


r ou n doff er r or s in t r odu ced du r in g t h eir com pu t a t ion . Th e a ct u a l com pu t ed
eigen va lu es m a y va r y fr om on e m a ch in e t o a n ot h er , bu t on a t ypica l
wor kst a t ion , t h e st a t em en t s

7-79
7 Symbolic Math Toolbox

f o r ma t l o n g
e = e i g ( A)

pr odu ce
e =
0. 99999999999642
2. 00000000000579
2. 99999999999780

Of cou r se, t h e exa m ple wa s cr ea t ed so t h a t it s eigen va lu es a r e a ct u a lly 1, 2, a n d


3. Not e t h a t t h r ee or fou r digit s h a ve been lost t o r ou n doff. Th is ca n be ea sily
ver ified wit h t h e t oolbox. Th e st a t em en t s
B = s y m( A) ;
e = e i g ( B) '
p = p o l y ( B)
f = f a c t or ( p)

pr odu ce
e =
[ 1, 2, 3]

p =
x ^ 3 - 6 * x ^ 2 +1 1 * x - 6

f =
( x- 1) *( x- 2) *( x- 3)

Ar e t h e eigen va lu es sen sit ive t o t h e per t u r ba t ion s ca u sed by r ou n doff er r or


beca u se t h ey a r e “close t oget h er ”? Or din a r ily, t h e va lu es 1, 2, a n d 3 wou ld be
r ega r ded a s “well sepa r a t ed.” Bu t , in t h is ca se, t h e sepa r a t ion sh ou ld be viewed
on t h e sca le of t h e or igin a l m a t r ix. If A wer e r epla ced by A/ 1 0 0 0 , t h e
eigen va lu es, wh ich wou ld be .001, .002, .003, wou ld “seem ” t o be closer
t oget h er .
Bu t eigen va lu e sen sit ivit y is m or e su bt le t h a n ju st “closen ess.” Wit h a ca r efu lly
ch osen per t u r ba t ion of t h e m a t r ix, it is possible t o m a ke t wo of it s eigen va lu es
coa lesce in t o a n a ct u a l dou ble r oot t h a t is ext r em ely sen sit ive t o r ou n doff a n d
ot h er er r or s.

7-80
Linear Algebra

On e good per t u r ba t ion dir ect ion ca n be obt a in ed fr om t h e ou t er pr odu ct of t h e


left a n d r igh t eigen vect or s a ssocia t ed wit h t h e m ost sen sit ive eigen va lu e. Th e
followin g st a t em en t cr ea t es
E = [ 130, - 390, 0; 43, - 129, 0; 133, - 399, 0]

t h e per t u r ba t ion m a t r ix
E =
130 - 390 0
43 - 129 0
133 - 399 0

Th e per t u r ba t ion ca n n ow be expr essed in t er m s of a sin gle, sca la r pa r a m et er


t . Th e st a t em en t s
s y ms x t
A = A+t * E

r epla ce A wit h t h e sym bolic r epr esen t a t ion of it s per t u r ba t ion .


A =
[ - 1 4 9 +1 3 0 * t , - 5 0 - 3 9 0 * t , - 1 5 4 ]
[ 5 3 7 +4 3 * t , 1 8 0 - 1 2 9 * t , 546]
[ - 2 7 +1 3 3 * t , - 9- 399*t , - 25]

Com pu t in g t h e ch a r a ct er ist ic polyn om ia l of t h is n ew A


p = p o l y ( A)

gives
p =
x ^ 3 - 6 * x ^ 2 +1 1 * x - t * x ^ 2 +4 9 2 5 1 2 * t * x - 6 - 1 2 2 1 2 7 1 * t

P r et t ypr in t in g
pr e t t y( c ol l e c t ( p, x) )

sh ows m or e clea r ly t h a t p is a cu bic in x wh ose coefficien t s va r y lin ea r ly wit h t .


3 2
x + ( - t - 6) x + ( 492512 t + 11) x - 6 - 1221271 t

It t u r n s ou t t h a t wh en t is va r ied over a ver y sm a ll in t er va l, fr om 0 t o 1.0e-6,


t h e desir ed dou ble r oot a ppea r s. Th is ca n best be seen gr a ph ica lly. Th e fir st

7-81
7 Symbolic Math Toolbox

figu r e sh ows plot s of p , con sider ed a s a fu n ct ion of x , for t h r ee differ en t va lu es


of t : t = 0, t = 0.5e-6, a n d t = 1.0e-6. F or ea ch va lu e, t h e eigen va lu es a r e
com pu t ed n u m er ica lly a n d a lso plot t ed.
x = . 8: . 01: 3. 2;
f or k = 0: 2
c = s y m2 p o l y ( s u b s ( p , t , k * 0 . 5 e - 6 ) ) ;
y = pol yva l ( c , x) ;
l a mb d a = e i g ( d o u b l e ( s u b s ( A, t , k * 0 . 5 e - 6 ) ) ) ;
s ubpl ot ( 3, 1, 3- k)
p l o t ( x , y , ' - ' , x , 0 * x , ' : ' , l a mb d a , 0 * l a mb d a , ' o ' )
a xi s ( [ . 8 3. 2 - . 5 . 5] )
t e x t ( 2 . 2 5 , . 3 5 , [ ' t = ' n u m2 s t r ( k * 0 . 5 e - 6 ) ] ) ;
e nd

0.5
t = 1e−006

−0.5
1 1.5 2 2.5 3

0.5
t = 5e−007

−0.5
1 1.5 2 2.5 3

0.5
t=0

−0.5
1 1.5 2 2.5 3

Th e bot t om su bplot sh ows t h e u n per t u r bed polyn om ia l, wit h it s t h r ee r oot s a t


1, 2, a n d 3. Th e m iddle su bplot sh ows t h e fir st t wo r oot s a ppr oa ch in g ea ch

7-82
Linear Algebra

ot h er . In t h e t op su bplot , t h ese t wo r oot s h a ve becom e com plex a n d on ly on e


r ea l r oot r em a in s.
Th e n ext st a t em en t s com pu t e a n d displa y t h e a ct u a l eigen va lu es
e = e i g ( A) ;
pr e t t y( e )

sh owin g t h a t e ( 2 ) a n d e ( 3 ) for m a com plex con ju ga t e pa ir .


[ 1/ 3 ]
[ 1 / 3 %1 - 3 %2 + 2 + 1 / 3 t ]
[ ]
[ 1/ 3 1/ 2 1/ 3 ]
[ - 1 / 6 %1 + 3 / 2 %2 + 2 + 1 / 3 t + 1 / 2 i 3 ( 1 / 3 %1 + 3 %2 ) ]
[ ]
[ 1/ 3 1/ 2 1/ 3 ]
[ - 1 / 6 %1 + 3 / 2 %2 + 2 + 1 / 3 t - 1 / 2 i 3 ( 1 / 3 %1 + 3 %2 ) ]

2 3
%1 : = 3 1 8 9 3 9 3 t - 2 2 1 6 2 8 6 t + t + 3 ( - 3 + 4432572 t
2 3
- 1052829647418 t + 358392752910068940 t
4 1/ 2
- 181922388795 t )

2
- 1/ 3 + 492508/ 3 t - 1/ 9 t
%2 : = - - - - - - - - - - - - - - - - - - - - - - - - - - -
1/ 3
%1

Next , t h e sym bolic r epr esen t a t ion s of t h e t h r ee eigen va lu es a r e eva lu a t ed a t


m a n y va lu es of t
t va l s = ( 2: - . 02: 0) ' * 1. e - 6;
r = s i z e ( t va l s , 1) ;
c = s i z e ( e , 1) ;
l a mb d a = z e r o s ( r , c ) ;
f or k = 1: c
l a mb d a ( : , k ) = d o u b l e ( s u b s ( e ( k ) , t , t v a l s ) ) ;
e nd

7-83
7 Symbolic Math Toolbox

p l o t ( l a mb d a , t v a l s )
x l a b e l ( ' \ l a mb d a ' ) ; y l a b e l ( ' t ' ) ;
t i t l e ( ' Ei g e n v a l u e Tr a n s i t i o n ' )

t o pr odu ce a plot of t h eir t r a ject or ies.

−6 Eigenvalue Transition
x 10
2

1.8

1.6

1.4

1.2

1
t

0.8

0.6

0.4

0.2

0
1 1.2 1.4 1.6 1.8 2 2.2 2.4 2.6 2.8 3
λ

Above t = 0 . 8 e - 6 , t h e gr a ph s of t wo of t h e eigen va lu es in t er sect , wh ile below


t = 0.8e - 6 , t wo r ea l r oot s becom e a com plex con ju ga t e pa ir . Wh a t is t h e pr ecise
va lu e of t t h a t m a r ks t h is t r a n sit ion ? Let τ den ot e t h is va lu e of t .
On e wa y t o fin d τ is ba sed on t h e fa ct t h a t , a t a dou ble r oot , bot h t h e fu n ct ion
a n d it s der iva t ive m u st va n ish . Th is r esu lt s in t wo polyn om ia l equ a t ion s t o be
solved for t wo u n kn own s. Th e st a t em en t
s ol = s ol ve ( p, di f f ( p, ' x' ) )

solves t h e pa ir of a lgebr a ic equ a t ion s p = 0 a n d d p / d x = 0 a n d pr odu ces

7-84
Linear Algebra

s ol =
t : [ 4 x 1 s y m]
x : [ 4 x 1 s y m]

F in d τ n ow by
t a u = doubl e ( s ol . t ( 2) )

wh ich r evea ls t h a t t h e secon d elem en t of s o l . t is t h e desir ed va lu e of τ .


f o r ma t s h o r t
t au =
7. 8379e - 07

Th er efor e, t h e secon d elem en t of s o l . x


s i g ma = d o u b l e ( s o l . x ( 2 ) )

is t h e dou ble eigen va lu e


s i g ma =
1. 5476

Let ’s ver ify t h a t t h is va lu e of τ does in deed pr odu ce a dou ble eigen va lu e a t


σ = 1.5476 . To a ch ieve t h is, su bst it u t e τ for t in t h e per t u r bed m a t r ix
A (t ) = A + t E a n d fin d t h e eigen va lu es of A (t ) . Th a t is,
e = e i g ( d o u b l e ( s u b s ( A, t , t a u ) ) )

e =

1. 5476
1. 5476
2. 9047

con fir m s t h a t σ = 1.5476 is a dou ble eigen va lu e of A (t ) for t = 7.8379e-07.

7-85
7 Symbolic Math Toolbox

Solving Equa tions


Solving Algebra ic Equa tions
If S is a sym bolic expr ession ,
s o l v e ( S)

a t t em pt s t o fin d va lu es of t h e sym bolic va r ia ble in S (a s det er m in ed by


f i n d s y m) for wh ich S is zer o. F or exa m ple,
s y ms a b c x
S = a *x^ 2 + b*x + c ;
s o l v e ( S)

u ses t h e fa m ilia r qu a dr a t ic for m u la t o pr odu ce


a ns =
[ 1 / 2 / a * ( - b +( b ^ 2 - 4 * a * c ) ^ ( 1 / 2 ) ) ]
[ 1/ 2/ a *( - b- ( b^ 2- 4*a *c ) ^ ( 1/ 2) ) ]

Th is is a sym bolic vect or wh ose elem en t s a r e t h e t wo solu t ion s.


If you wa n t t o solve for a specific va r ia ble, you m u st specify t h a t va r ia ble a s a n
a ddit ion a l a r gu m en t . F or exa m ple, if you wa n t t o solve S for b , u se t h e
com m a n d
b = s o l v e ( S, b )

wh ich r et u r n s
b =
- ( a * x ^ 2 +c ) / x

Not e t h a t t h ese exa m ples a ssu m e equ a t ion s of t h e for m f ( x ) = 0 . If you n eed
t o solve equ a t ion s of t h e for m f ( x ) = q ( x ) , you m u st u se qu ot ed st r in gs. In
pa r t icu la r , t h e com m a n d
s = s o l v e ( ' c o s ( 2 * x ) +s i n ( x ) =1 ' )

r et u r n s a vect or wit h fou r solu t ion s.

7-86
Solving Equations

s =
[ 0]
[ pi ]
[ 1/ 6*pi ]
[ 5/ 6*pi ]

Severa l Algebra ic Equa tions


Now let ’s look a t syst em s of equ a t ion s. Su ppose we h a ve t h e syst em
2 2
x y = 0

y
x – --- = α
2
a n d we wa n t t o solve for x a n d y. F ir st cr ea t e t h e n ecessa r y sym bolic object s.
s y ms x y a l p h a

Th er e a r e sever a l wa ys t o a ddr ess t h e ou t pu t of s o l v e . On e is t o u se a


t wo-ou t pu t ca ll
[ x, y] = s ol ve ( x^ 2*y^ 2, x- y/ 2- a l pha )

wh ich r et u r n s
x =
[ 0]
[ 0]
[ a l pha ]
[ a l pha ]

y =
[ - 2*a l pha ]
[ - 2*a l pha ]
[ 0]
[ 0]

Con sequ en t ly, t h e solu t ion vect or


v = [ x, y]

7-87
7 Symbolic Math Toolbox

a ppea r s t o h a ve r edu n da n t com pon en t s. Th is is du e t o t h e fir st equ a t ion


2 2
x y = 0 , wh ich h a s t wo solu t ion s in x a n d y: x = ± 0 , y = ± 0 . Ch a n gin g t h e
equ a t ion s t o
e q s 1 = ' x ^ 2 * y ^ 2 =1 , x - y / 2 - a l p h a '
[ x, y] = s ol ve ( e qs 1)

pr odu ces fou r dist in ct solu t ion s.


x =
[ 1/ 2*a l p h a +1 / 2*( a l p h a ^ 2 +2 ) ^ ( 1 / 2) ]
[ 1/ 2*a l pha - 1/ 2*( a l p h a ^ 2 +2 ) ^ ( 1 / 2) ]
[ 1/ 2*a l p h a +1 / 2*( a l pha ^ 2- 2) ^ ( 1/ 2) ]
[ 1/ 2*a l pha - 1/ 2*( a l pha ^ 2- 2) ^ ( 1/ 2) ]

y =
[ - al p h a +( a l p h a ^ 2 +2 ) ^ ( 1 / 2) ]
[ - al pha - ( a l p h a ^ 2 +2 ) ^ ( 1 / 2) ]
[ - al p h a +( a l pha ^ 2- 2) ^ ( 1/ 2) ]
[ - al pha - ( a l pha ^ 2- 2) ^ ( 1/ 2) ]

Sin ce we did n ot specify t h e depen den t va r ia bles, s o l v e u ses f i n d s y m t o


det er m in e t h e va r ia bles.
Th is wa y of a ssign in g ou t pu t fr om s o l v e is qu it e su ccessfu l for “sm a ll” syst em s.
P la in ly, if we h a d, sa y, a 10-by-10 syst em of equ a t ion s, t ypin g
[ x1, x2, x3, x4, x5, x6, x7, x8, x9, x10] = s ol ve ( . . . )

is bot h a wkwa r d a n d t im e con su m in g. To cir cu m ven t t h is difficu lt y, s o l v e ca n


r et u r n a st r u ct u r e wh ose fields a r e t h e solu t ion s. In pa r t icu la r , con sider t h e
syst em u ^ 2 - v ^ 2 = a ^ 2 , u + v = 1 , a ^ 2 - 2 * a = 3 . Th e com m a n d
S = s ol ve ( ' u^ 2- v^ 2 = a ^ 2' , ' u + v = 1' , ' a ^ 2- 2*a = 3' )

r et u r n s
S =
a : [ 2 x 1 s y m]
u : [ 2 x 1 s y m]
v : [ 2 x 1 s y m]

7-88
Solving Equations

Th e solu t ion s for a r eside in t h e “a -field” of S . Th a t is,


S. a

pr odu ces
a ns =
[ - 1]
[ 3]

Sim ila r com m en t s a pply t o t h e solu t ion s for u a n d v . Th e st r u ct u r e S ca n n ow


be m a n ipu la t ed by field a n d in dex t o a ccess a pa r t icu la r por t ion of t h e solu t ion .
F or exa m ple, if we wa n t t o exa m in e t h e secon d solu t ion , we ca n u se t h e
followin g st a t em en t
s 2 = [ S. a ( 2 ) , S. u ( 2 ) , S. v ( 2 ) ]

t o ext r a ct t h e secon d com pon en t of ea ch field.


s2 =
[ 3, 5, - 4]

Th e followin g st a t em en t
M = [ S. a , S. u , S. v ]

cr ea t es t h e solu t ion m a t r ix M
M=
[ - 1, 1, 0]
[ 3, 5, - 4]

wh ose r ows com pr ise t h e dist in ct solu t ion s of t h e syst em .


Lin ea r syst em s of sim u lt a n eou s equ a t ion s ca n a lso be solved u sin g m a t r ix
division . F or exa m ple,
cl ear u v x y
s y ms u v x y
S = s o l v e ( x +2 * y - u , 4 * x +5 * y - v ) ;
s o l = [ S. x ; S. y ]

and

7-89
7 Symbolic Math Toolbox

A = [ 1 2; 4 5] ;
b = [ u; v] ;
z = A\ b

r esu lt in
s ol =

[ - 5 / 3 * u +2 / 3 * v ]
[ 4/ 3*u- 1/ 3*v]

z =
[ - 5 / 3 * u +2 / 3 * v ]
[ 4/ 3*u- 1/ 3*v]

Th u s s a n d z pr odu ce t h e sa m e solu t ion , a lt h ou gh t h e r esu lt s a r e a ssign ed t o


differ en t va r ia bles.

Single Differentia l Equa tion


Th e fu n ct ion d s o l v e com pu t es sym bolic solu t ion s t o or din a r y differ en t ia l
equ a t ion s. Th e equ a t ion s a r e specified by sym bolic expr ession s con t a in in g t h e
let t er D t o den ot e differ en t ia t ion . Th e sym bols D2 , D3 , ... DN, cor r espon d t o t h e
secon d, t h ir d, ..., Nt h der iva t ive, r espect ively. Th u s, D2 y is t h e Sym bolic Ma t h
2 2
Toolbox equ iva len t of d y ⁄ d t . Th e depen den t va r ia bles a r e t h ose pr eceded by
D a n d t h e defa u lt in depen den t va r ia ble is t . Not e t h a t n a m es of sym bolic
va r ia bles sh ou ld n ot con t a in D. Th e in depen den t va r ia ble ca n be ch a n ged fr om
t t o som e ot h er sym bolic va r ia ble by in clu din g t h a t va r ia ble a s t h e la st in pu t
a r gu m en t .
In it ia l con dit ion s ca n be specified by a ddit ion a l equ a t ion s. If in it ia l con dit ion s
a r e n ot specified, t h e solu t ion s con t a in con st a n t s of in t egr a t ion , C1 , C2 , et c.
Th e ou t pu t fr om d s o l v e pa r a llels t h e ou t pu t fr om s o l v e . Th a t is, you ca n ca ll
d s o l v e wit h t h e n u m ber of ou t pu t va r ia bles equ a l t o t h e n u m ber of depen den t
va r ia bles or pla ce t h e ou t pu t in a st r u ct u r e wh ose fields con t a in t h e solu t ion s
of t h e differ en t ia l equ a t ion s.

Example 1
Th e followin g ca ll t o d s o l v e
d s o l v e ( ' Dy =1 +y ^ 2 ' )

7-90
Solving Equations

u ses y a s t h e depen den t va r ia ble a n d t a s t h e defa u lt in depen den t va r ia ble.


Th e ou t pu t of t h is com m a n d is
a ns =
t a n ( t +C1 )

To specify a n in it ia l con dit ion , u se


y = d s o l v e ( ' Dy =1 +y ^ 2 ' , ' y ( 0 ) =1 ' )

Th is pr odu ces
y =
t a n ( t +1 / 4 * p i )

Not ice t h a t y is in t h e MATLAB wor kspa ce, bu t t h e in depen den t va r ia ble t is


n ot . Th u s, t h e com m a n d d i f f ( y , t ) r et u r n s a n er r or . To pla ce t in t h e
wor kspa ce, t ype s y ms t .

Example 2
Non lin ea r equ a t ion s m a y h a ve m u lt iple solu t ion s, even wh en in it ia l con dit ion s
a r e given .
x = d s o l v e ( ' ( Dx ) ^ 2 +x ^ 2 =1 ' , ' x ( 0 ) =0 ' )

r esu lt s in
x =
[ - s i n( t ) ]
[ s i n( t ) ]

Example 3
H er e is a secon d or der differ en t ia l equ a t ion wit h t wo in it ia l con dit ion s. Th e
com m a n ds
y = d s o l v e ( ' D2 y =c o s ( 2 * x ) - y ' , ' y ( 0 ) =1 ' , ' Dy ( 0 ) =0 ' , ' x ' )
s i mp l i f y ( y )

pr odu ce
y =
- 2 / 3 * c o s ( x ) ^ 2 +1 / 3 +4 / 3 * c o s ( x )

7-91
7 Symbolic Math Toolbox

Th e key issu es in t h is exa m ple a r e t h e or der of t h e equ a t ion a n d t h e in it ia l


con dit ion s. To solve t h e or din a r y differ en t ia l equ a t ion
3
d u
= u
3
dx

u ( 0 ) = 1 , u ′ ( 0 ) = –1 , u ″ ( 0 ) = π
sim ply t ype
u = d s o l v e ( ' D3 u =u ' , ' u ( 0 ) =1 ' , ' Du ( 0 ) =- 1 ' , ' D2 u ( 0 ) = p i ' , ' x ' )
3 3
Use D3 u t o r epr esen t d u ⁄ d x a n d D2 u ( 0 ) for u ′′(0 ) .

Severa l Differentia l Equa tions


Th e fu n ct ion d s o l v e ca n a lso h a n dle sever a l or din a r y differ en t ia l equ a t ion s in
sever a l va r ia bles, wit h or wit h ou t in it ia l con dit ion s. F or exa m ple, h er e is a pa ir
of lin ea r , fir st or der equ a t ion s.
S = d s o l v e ( ' Df = 3 * f +4 * g ' , ' Dg = - 4 * f +3 * g ' )

Th e com pu t ed solu t ion s a r e r et u r n ed in t h e st r u ct u r e S . You ca n det er m in e t h e


va lu es of f a n d g by t ypin g
f = S. f
f =
e x p ( 3 * t ) * ( c o s ( 4 * t ) * C1 +s i n ( 4 * t ) * C2 )

g = S. g
g =
- e x p ( 3 * t ) * ( s i n ( 4 * t ) * C1 - c o s ( 4 * t ) * C2 )

If you pr efer t o r ecover f a n d g dir ect ly a s well a s in clu de in it ia l con dit ion s,
t ype
[ f , g ] = d s o l v e ( ' Df =3 * f +4 * g , Dg =- 4 * f +3 * g ' , ' f ( 0 ) = 0 , g ( 0 ) = 1 ' )

f =
e xp( 3*t ) *s i n( 4*t )

g =
e xp( 3*t ) *c os ( 4*t )

7-92
Solving Equations

Th is t a ble det a ils som e exa m ples a n d Sym bolic Ma t h Toolbox syn t a x. Not e t h a t
t h e fin a l en t r y in t h e t a ble is t h e Air y differ en t ia l equ a t ion wh ose solu t ion is
r efer r ed t o a s t h e Air y fu n ct ion .

Differentia l Equa tion M ATLAB Com m a nd

y = d s o l v e ( ' Dy +4 * y = e x p ( - t ) ' ,
dy –t
+ 4 y (t ) = e ' y( 0) = 1' )
dt

y (0 ) = 1

2 y = d s o l v e ( ' D2 y +4 * y = e x p ( - 2 * x ) ' ,
d y –2 x ' y ( 0 ) =0 ' , ' y ( p i ) = 0 ' , ' x ' )
+ 4 y(x) = e
2
dx

y (0 ) = 0, y (π)= 0

2 y = d s o l v e ( ' D2 y = x * y ' , ' y ( 0 ) = 0 ' ,


d y ' y( 3) = be s s e l k( 1/ 3, 2*s qr t ( 3) ) / pi ' ,
= xy(x)
2 ' x' )
dx

1
y ( 0 ) = 0 , y ( 3 ) = --- K 1 ( 2 3 )
π ---
3

(Th e Air y E qu a t ion )

Th e Air y fu n ct ion pla ys a n im por t a n t r ole in t h e m a t h em a t ica l m odelin g of t h e


disper sion of wa t er wa ves.

7-93
7 Symbolic Math Toolbox

7-94
A
MATLAB Qu ick Refer en ce

In tro d u c tio n . . . . . . . . . . . . . . . . . . . . A-2


Ge n e ra l P u rp o s e Co m m a n d s . . . . . . . . . . . . A-3
Op e ra to rs a n d S p e c ia l Ch a ra c te rs . . . . . . . . . . A-5
Lo g ic a l F u n c tio n s . . . . . . . . . . . . . . . . . A-5
La n g u a g e Co n s tru c ts a n d D e bu g g in g . . . . . . . . A-5
Ele m e n ta ry Ma tric e s a n d Ma trix Ma n ip u la tio n . . . . A-7
S p e c ia lize d Ma tric e s . . . . . . . . . . . . . . . . A-8
Ele m e n ta ry Ma th F u n c tio n s . . . . . . . . . . . . . A-8
S p e c ia lize d Ma th F u n c tio n s . . . . . . . . . . . . . A-9
Co o rd in a te S y s te m Co n v e rs io n . . . . . . . . . . . A-10
Ma trix F u n c tio n s - N u m e ric a l Lin e a r Alg e bra . . . . . A-10
D a ta An a ly s is a n d F o u rie r Tra n s fo rm F u n c tio n s . . . A-11
P o ly n o m ia l a n d In te rp o la tio n F u n c tio n s . . . . . . . A-12
F u n c tio n F u n c tio n s - N o n lin e a r N u m e ric a l Me th o d s . . A-13
S p a rs e Ma trix F u n c tio n s . . . . . . . . . . . . . . A-14
S o u n d P ro c e s s in g F u n c tio n s . . . . . . . . . . . . A-15
Ch a ra c te r S trin g F u n c tio n s . . . . . . . . . . . . . A-16
F ile I/O F u n c tio n s . . . . . . . . . . . . . . . . . A-17
B itw is e F u n c tio n s . . . . . . . . . . . . . . . . . A-17
S tru c tu re F u n c tio n s . . . . . . . . . . . . . . . . A-18
MATLAB Obje c t F u n c tio n s . . . . . . . . . . . . . A-18
MATLAB In te rfa c e to J a v a F u n c tio n s . . . . . . . . A-18
Ce ll Arra y F u n c tio n s . . . . . . . . . . . . . . . . A-19
Mu ltid im e n s io n a l Arra y F u n c tio n s . . . . . . . . . . A-19
D a ta Vis u a liza tio n . . . . . . . . . . . . . . . . . A-19
Gra p h ic a l U s e r In te rfa c e s . . . . . . . . . . . . . . A-24
S e ria l P o rt I/O . . . . . . . . . . . . . . . . . . . A-25
A

Introduction
Th is a ppen dix list s t h e MATLAB fu n ct ion s a s t h ey a r e gr ou ped in H elp by
su bject . E a ch t a ble con t a in s t h e fu n ct ion n a m es a n d br ief descr ipt ion s. F or
com plet e in for m a t ion a bou t a n y of t h ese fu n ct ion s, r efer t o H elp a n d eit h er :

• Select t h e fu n ct ion fr om t h e MATLAB F u n ct ion Refer en ce (F u n ct ion s by


Ca t egor y or Alph a bet ica l List of F u n ct ion s), or
• F r om t h e S e a rc h t a b in t h e H elp Na viga t or , select F u n c tio n N a m e a s
S e a rc h ty p e , t ype t h e fu n ct ion n a m e in t h e S e a rc h fo r field, a n d click Go .

N ote If you a r e viewin g t h is book fr om H elp, you ca n click on a n y fu n ct ion


n a m e a n d ju m p dir ect ly t o t h e cor r espon din g MATLAB fu n ct ion pa ge.

A-2
General Purpose Commands

Genera l Purpose Comma nds M a na ging Com m a nds a nd Functions (Continued)


r e ha s h Refr esh fu n ct ion a n d file syst em
Th is set of fu n ct ion s let s you st a r t a n d st op
ca ch es
MATLAB, wor k wit h files a n d t h e oper a t in g
syst em , con t r ol t h e com m a n d win dow, a n d m a n a ge r mp a t h Rem ove dir ect or ies fr om
MATLAB’s sea r ch pa t h
t h e en vir on m en t , va r ia bles, a n d t h e wor kspa ce.
s uppor t Open Ma t h Wor ks Tech n ica l
Su ppor t Web pa ge
M a na ging Comm a nds a nd Functions
t ype List file
a ddpa t h Add dir ect or ies t o MATLAB’s
sea r ch pa t h ve r Displa y ver sion in for m a t ion for
MATLAB, Sim u lin k, a n d
doc Displa y H TML docu m en t a t ion
t oolboxes
in H elp br owser
ve r s i on Get MATLAB ver sion n u m ber
doc opt Displa y loca t ion of h elp file
dir ect or y for UNIX pla t for m s we b Poin t H elp br owser or Web
br owser a t file or Web sit e
ge npa t h Gen er a t e a pa t h st r in g
wh a t List MATLAB-specific files in
he l p Display M-file help for
cu r r en t dir ect or y
MATLAB functions in the
Command Window wh a t s n e w Displa y RE ADME files for
MATLAB a n d t oolboxes
h e l p b r o ws e r Displa y H elp br owser for a ccess
t o a ll Ma t h Wor ks on lin e h elp wh i c h Loca t e fu n ct ion s a n d files
he l pde s k Displa y H elp br owser
M a na ging Va ria bles a nd the W ork spa ce
h e l p wi n Display M-file help and provide
access to M-file help for all
functions cl ear Rem ove it em s fr om t h e
wor kspa ce
l as t er r La st er r or m essa ge
di s p Displa y t ext or a r r a y
l a s t wa r n La st wa r n in g m essa ge
l e ngt h Len gt h of vect or
l i c e ns e Sh ow MATLAB licen se n u m ber
l oa d Ret r ieve va r ia bles fr om disk
l ookf or Search for specified keyword in
M-file help entries me mo r y H elp for m em or y lim it a t ion s
pa r t i a l pa t h Pa r t ia l pa t h n a m e ml o c k P r even t M-file clea r in g
pa t h Con t r ol MATLAB’s dir ect or y mu n l o c k Allow M-file clea r in g
sea r ch pa t h ope nva r Open wor kspa ce va r ia ble in
pa t ht ool Open the GUI for viewing and Ar r a y E dit or for gr a ph ica l
modifying MATLAB’s path edit in g
pr of i l e St a r t t h e M-file pr ofiler, a u t ilit y pa c k Con solida t e wor kspa ce m em or y
for debu ggin g a n d opt im izin g s a ve Sa ve wor kspa ce va r ia bles on
code disk
pr of r e por t Gen er a t e a pr ofile r epor t s a ve a s Sa ve figu r e or m odel u sin g
specified for m a t

A-3
A MATLAB Quick Reference

M a na ging Va ria bles a nd the W ork spa ce W ork ing w ith Files a nd the O pera ting
(Continued) Environment (Continued)
s i ze Ar r a y dim en sion s f i l e b r o ws e r Displa y Cu r r en t Dir ect or y
wh o , wh o s List t h e va r ia bles in t h e br owser , for viewin g files
wor kspa ce f ul l f i l e Bu ild fu ll filen a m e fr om pa r t s
wo r k s p a c e Displa y t h e Wor kspa ce br owser, i nf o Displa y con t a ct in for m a t ion or
a GUI for m a n a gin g t h e t oolbox Rea dm e files
wor kspa ce i n me m F u n ct ion s in m em or y
ls List dir ect or y on UNIX
Controlling the Comma nd W indow
ma t l a b r o o t Get r oot dir ect or y of MATLAB
cl c Clea r Com m a n d Win dow in st a lla t ion
e c ho E ch o M-files du r in g execu t ion mk d i r Ma ke n ew dir ect or y
f o r ma t Con t r ol t h e displa y for m a t for ope n Open files ba sed on ext en sion
ou t pu t
p wd Displa y cu r r en t dir ect or y
h o me Move cu r sor t o u pper left cor n er
of Com m a n d Win dow
t e mp d i r Ret u r n t h e n a m e of t h e syst em ’s
t em por a r y dir ect or y
mo r e Con t r ol pa ged ou t pu t for t h e
com m a n d win dow
t e mp n a me Un iqu e n a m e for t em por a r y file
undoc he c kout Un do pr eviou s ch eckou t fr om
sou r ce con t r ol syst em
W ork ing w ith Files a nd the O pera ting
Environm ent uni x E xecu t e a UNIX com m a n d a n d
r et u r n t h e r esu lt
be e p P r odu ce a beep sou n d
! E xecu t e oper a t in g syst em
cd Ch a n ge wor kin g dir ect or y
com m a n d
c he c ki n Ch eck file in t o sou r ce con t r ol
syst em Sta rting a nd Q uitting M ATLAB
c he c kout Ch eck file ou t of sou r ce con t r ol f i ni s h MATLAB t er m in a t ion M-file
syst em
e xi t Ter m in a t e MATLAB
c mo p t s Get n a m e of sou r ce con t r ol
syst em , a n d P VCS pr oject ma t l a b St a r t MATLAB (UNIX syst em s
filen a m e on ly)
c opyf i l e Copy file ma t l a b r c MATLAB st a r t u p M-file
c u s t o mv e r c t r l Allow cu st om sou r ce con t r ol qui t Ter m in a t e MATLAB
syst em s t a r t up MATLAB st a r t u p M-file
de l e t e Delet e files a n d gr a ph ics object s
di a r y Sa ve session t o a disk file
di r Displa y a dir ect or y list in g
e di t E dit a n M-file
f i l e pa r t s Get filen a m e pa r t s

A-4
Operators and Special Characters

O pera tors a nd Specia l Cha ra cters Logica l Functions


Th is set of fu n ct ion s per for m s logica l oper a t ion s
Th ese a r e t h e a ct u a l oper a t or s you u se t o en t er a n d su ch a s ch eckin g if a file or va r ia ble exist s a n d
m a n ipu la t e da t a , for exa m ple, m a t r ix t est in g if a ll elem en t s in a n a r r a y a r e n on zer o.
m u lt iplica t ion , a r r a y m u lt iplica t ion , a n d lin e “Oper a t or s a n d Specia l Ch a r a ct er s” con t a in s ot h er
con t in u a t ion . oper a t or s t h a t per for m logica l oper a t ion s.

O pera tors a nd Specia l Cha ra cters Logica l Functions


+ P lu s al l Test t o det er m in e if a ll elem en t s
- Min u s a r e n on zer o
* Ma t r ix m u lt iplica t ion a ny Test for a n y n on zer os
.* Ar r a y m u lt iplica t ion e xi s t Ch eck if a va r ia ble or file exist s
^ Ma t r ix power f i nd F in d in dices a n d va lu es of
.^ Ar r a y power n on zer o elem en t s
kr on Kr on ecker t en sor pr odu ct
i s* Det ect st a t e
\ Ba cksla sh or left division
i sa Det ect a n object of a given cla ss
/ Sla sh or r igh t division
i s k e y wo r d Test if st r in g is a MATLAB
keywor d
. / a nd . \ Ar r a y division , r igh t a n d left
i s v a r n a me Test if st r in g is a va lid va r ia ble
: Colon name
( ) Pa r en t h eses l ogi c a l Con ver t n u m er ic va lu es t o
[ ] Br a cket s logica l
{} Cu r ly br a ces mi s l o c k e d Tr u e if M-file ca n n ot be clea r ed
. Decim a l poin t
... Con t in u a t ion La ngua ge Constructs a nd Debugging
, Com m a
Th ese fu n ct ion s let you wor k wit h MATLAB a s a
; Sem icolon pr ogr a m m in g la n gu a ge. F or exa m ple, you ca n
% Com m en t con t r ol pr ogr a m flow, defin e globa l va r ia bles,
! E xcla m a t ion poin t per for m in t er a ct ive in pu t , a n d debu g you r code.
' Tr a n spose a n d qu ot e
.' Non con ju ga t ed t r a n spose M ATLAB a s a Progra m m ing La ngua ge
= Assign m en t
== E qu a lit y bui l t i n E xecu t e bu ilt in fu n ct ion fr om
over loa ded m et h od
< > Rela t ion a l oper a t or s
e va l In t er pr et st r in gs con t a in in g
& Logica l AND MATLAB expr ession s
| Logica l OR e va l c E va lu a t e MATLAB expr ession
~ Logica l NOT wit h ca pt u r e
xor Logica l E XCLUSIVE OR

A-5
A MATLAB Quick Reference

M ATLAB a s a Progra m m ing La ngua ge Control Flow (Continued)


(Continued) wa r n i n g Displa y wa r n in g m essa ge
e va l i n E va lu a t e expr ession in wh i l e Repea t st a t em en t s a n in defin it e
wor kspa ce n u m ber of t im es
f e va l F u n ct ion eva lu a t ion
f unc t i on F u n ct ion M-files Intera ctive Input
gl oba l Defin e globa l va r ia bles i nput Requ est u ser in pu t
na r gc hk Ch eck n u m ber of in pu t ke yboa r d In voke t h e keyboa r d in a n M-file
a r gu m en t s me n u Gen er a t e a m en u of ch oices for
pe r s i s t e nt Defin e per sist en t va r ia ble u ser in pu t
s c r i pt Scr ipt M-files pa us e H a lt execu t ion t em por a r ily

Control Flow O bject-O riented Progra mming


br e a k Ter m in a t e execu t ion of f o r loop cl as s Cr ea t e object or r et u r n cla ss of
or wh i l e loop object
cas e Ca se swit ch doubl e Con ver t t o dou ble pr ecision
cat ch Begin c a t c h block i nf e r i or t o In fer ior cla ss r ela t ion sh ip
c ont i nue Pa ss con t r ol t o t h e n ext it er a t ion i nl i ne Con st r u ct a n in lin e object
of f o r or wh i l e loop i n t 8 , i n t 1 6 , i n t 3 2 Con ver t t o sign ed in t eger
el s e Con dit ion a lly execu t e i sa Det ect a n object of a given cla ss
st a t em en t s
l oa dobj E xt en ds t h e l o a d fu n ct ion for
el s ei f Con dit ion a lly execu t e u ser object s
st a t em en t s
s a ve obj Sa ve filt er for object s
e nd Ter m in a t e f o r , wh i l e , s wi t c h ,
t r y, a n d i f st a t em en t s or
s i ngl e Con ver t t o sin gle pr ecision
in dica t e la st in dex s upe r i or t o Su per ior cla ss r ela t ion sh ip
e r r or Displa y er r or m essa ges ui nt 8, ui nt 16, Con ver t t o u n sign ed in t eger
ui nt 32
f or Repea t st a t em en t s a specific
n u m ber of t im es
if Con dit ion a lly execu t e Debugging
st a t em en t s dbc l e a r Clea r br ea kpoin t s
o t h e r wi s e Defa u lt pa r t of s wi t c h dbc ont Resu m e execu t ion
st a t em en t d b d o wn Ch a n ge loca l wor kspa ce con t ext
r e t ur n Ret u r n t o t h e in vokin g fu n ct ion d b me x E n a ble ME X-file debu ggin g
s wi t c h Swit ch a m on g sever a l ca ses dbqui t Qu it debu g m ode
ba sed on expr ession
dbs t a c k Displa y fu n ct ion ca ll st a ck
try Begin t r y block
dbs t a t us List a ll br ea kpoin t s

A-6
Elementary Matrices and Matrix Manipulation

Debugging (Continued) Elementa ry M a trices a nd Arra ys (Continued)


dbs t e p E xecu t e on e or m or e lin es fr om a z e r os Cr ea t e a n a r r a y of a ll zer os
br ea kpoin t : ( c ol on) Regu la r ly spa ced vect or
dbs t op Set br ea kpoin t s in a n M-file
fu n ct ion Specia l Va ria bles a nd Consta nts
dbt ype List M-file wit h lin e n u m ber s a ns Th e m ost r ecen t a n swer
dbup Ch a n ge loca l wor kspa ce con t ext c o mp u t e r Iden t ify t h e com pu t er on wh ich
MATLAB is r u n n in g
Function Ha ndles e ps F loa t in g-poin t r ela t ive a ccu r a cy
f unc t i on_ha ndl e MATLAB data type that is a i Im a gin a r y u n it
handle to a function
I nf In fin it y
f unc t i ons Return information about a
i n p u t n a me In pu t a r gu m en t n a m e
function handle
j Im a gin a r y u n it
f unc 2s t r Constructs a function name string
from a function handle Na N Not -a -Nu m ber
s t r 2f unc Constructs a function handle from na r gi n, na r gout Nu m ber of fu n ct ion a r gu m en t s
a function name string na r gout c hk Va lida t e n u m ber of ou t pu t
a r gu m en t s
Elem enta ry M a trices a nd M a trix pi Ra t io of a cir cle’s cir cu m fer en ce
M a nipula tion t o it s dia m et er
r e a l ma x La r gest posit ive floa t in g-poin t
Usin g t h ese fu n ct ion s you ca n m a n ipu la t e n u m ber
m a t r ices, a n d a ccess t im e, da t e, specia l va r ia bles,
r e a l mi n Sm a llest posit ive floa t in g-poin t
a n d con st a n t s, fu n ct ion s.
n u m ber
va r a r gi n, Pa ss or r et u r n va r ia ble n u m ber s
Elementa ry M a trices a nd Arra ys
va r a r gout of a r gu m en t s
bl kdi a g Con st r u ct a block dia gon a l
m a t r ix fr om in pu t a r gu m en t s
Time a nd Da tes
e ye Iden t it y m a t r ix
c a l e nda r Ca len da r
l i ns pa c e Gen er a t e lin ea r ly spa ced vect or s
c l oc k Cu r r en t t im e a s a da t e vect or
l ogs pa c e Gen er a t e loga r it h m ica lly spa ced
vect or s
c p u t i me E la psed CP U t im e
n u me l Nu m ber of elem en t s in a m a t r ix
da t e Cu r r en t da t e st r in g
or cell a r r a y da t e num Ser ia l da t e n u m ber
one s Cr ea t e a n a r r a y of a ll on es da t e s t r Da t e st r in g for m a t
r a nd Un ifor m ly dist r ibu t ed r a n dom da t e ve c Da t e com pon en t s
n u m ber s a n d a r r a ys e o md a y E n d of m on t h
r a ndn Nor m a lly dist r ibu t ed r a n dom e t i me E la psed t im e
n u m ber s a n d a r r a ys now Cu r r en t da t e a n d t im e

A-7
A MATLAB Quick Reference

Time a nd Da tes (Continued) Specia lized M a trices


t i c , t oc St opwa t ch t im er
Th ese fu n ct ion s let you wor k wit h m a t r ices su ch a s
we e k d a y Da y of t h e week H a da m a r d, H a n kel, H ilber t , a n d m a gic squ a r es.

M a trix M a nipula tion Specia lized M a trices


cat Con ca t en a t e a r r a ys c o mp a n Com pa n ion m a t r ix
di a g Dia gon a l m a t r ices a n d dia gon a ls ga l l e r y Test m a t r ices
of a m a t r ix
h a d a ma r d H a da m a r d m a t r ix
f l i pl r F lip m a t r ices left -r igh t
ha nke l H a n kel m a t r ix
f l i pud F lip m a t r ices u p-down
hi l b H ilber t m a t r ix
r e p ma t Replica t e a n d t ile a n a r r a y
i nvhi l b In ver se of t h e H ilber t m a t r ix
r e s ha pe Resh a pe a r r a y
ma g i c Ma gic squ a r e
r ot 90 Rot a t e m a t r ix 90 degr ees
pa s c a l Pa sca l m a t r ix
tril Lower t r ia n gu la r pa r t of a
m a t r ix
t oe pl i t z Toeplit z m a t r ix
wi l k i n s o n Wilkin son ’s eigen va lu e t est
triu Upper t r ia n gu la r pa r t of a
m a t r ix
m a t r ix
: ( c ol on) In dex in t o a r r a y, r ea r r a n ge
array Elementa ry M a th Functions
Th ese a r e m a n y of t h e st a n da r d m a t h em a t ica l
Vector Functions fu n ct ion s su ch a s t r igon om et r ic, h yper bolic,
c r os s Vector cross product loga r it h m ic, a n d com plex n u m ber m a n ipu la t ion .
dot Vector dot product
i nt e r s e c t Set intersection of two vectors Elementa ry M a th Functions
i s me mb e r Detect members of a set a bs Absolu t e va lu e a n d com plex
m a gn it u de
s e t di f f Return the set difference of two
vectors a c os , a c os h In ver se cosin e a n d in ver se
h yper bolic cosin e
s e t xor Set exclusive or of two vectors
a c ot , a c ot h In ver se cot a n gen t a n d in ver se
uni on Set union of two vectors h yper bolic cot a n gen t
uni que Unique elements of a vector acs c, acs ch In ver se coseca n t a n d in ver se
h yper bolic coseca n t
a ngl e P h a se a n gle
as ec, as ech In ver se seca n t a n d in ver se
h yper bolic seca n t
a s i n, a s i nh In ver se sin e a n d in ver se
h yper bolic sin e

A-8
Specialized Math Functions

Elementa ry M a th Functions (Continued) Specia lized M a th Functions


a t a n, a t a nh In ver se t a n gen t a n d in ver se
Th is set of fu n ct ion s in clu des Bessel, ellipt ic,
h yper bolic t a n gen t
ga m m a , fa ct or ia l, a n d ot h er s.
a t a n2 Fou r-qu a dr a n t in ver se t a n gen t
cei l Rou n d t owa r d in fin it y Specia lized M a th Functions
c o mp l e x Con st r u ct com plex da t a fr om ai r y Air y fu n ct ion s
r ea l a n d im a gin a r y com pon en t s
be s s e l h Bessel fu n ct ion s of t h e t h ir d
c onj Com plex con ju ga t e kin d (H a n kel fu n ct ion s)
c os , c os h Cosin e a n d h yper bolic cosin e be s s e l i , be s s e l k Modified Bessel fu n ct ion s
c ot , c ot h Cot a n gen t a n d h yper bolic be s s e l j , be s s e l y Bessel fu n ct ion s
cot a n gen t
be t a , be t a i nc , bet a , bet a in c, bet a ln
cs c, cs ch Coseca n t a n d h yper bolic be t a l n
coseca n t e l l i pj J a cobi ellipt ic fu n ct ion s
e xp E xpon en t ia l e l l i pke Com plet e ellipt ic in t egr a ls of t h e
fix Rou n d t owa r ds zer o fir st a n d secon d kin d
f l oor Rou n d t owa r ds m in u s in fin it y e r f , e r f c , e r f c x, E r r or fu n ct ion s
gc d Gr ea t est com m on divisor e r f i nv
i ma g Im a gin a r y pa r t of a com plex e xpi nt E xpon en t ia l in t egr a l
n u m ber f a c t or i a l Fa ct or ia l fu n ct ion
l cm Lea st com m on m u lt iple g a mma , g a mma i n c , Ga m m a fu n ct ion s
l og Na t u r a l loga r it h m g a mma l n

l og2 Ba se 2 loga r it h m a n d dissect l e ge ndr e Associa t ed Legen dr e fu n ct ion s


floa t in g-poin t n u m ber s in t o p o w2 Ba se 2 power a n d sca le
expon en t a n d m a n t issa floa t in g-poin t n u m ber s
l og10 Com m on (ba se 10) loga r it h m r at , r at s Ra t ion a l fr a ct ion a ppr oxim a t ion
mo d Modu lu s (sign ed r em a in der
a ft er division )
nc hoos e k Bin om ia l coefficien t or a ll
com bin a t ion s
r eal Rea l pa r t of com plex n u m ber
r em Rem a in der a ft er division
r ound Rou n d t o n ea r est in t eger
s ec, s ech Seca n t a n d h yper bolic seca n t
s i gn Sign u m fu n ct ion
s i n, s i nh Sin e a n d h yper bolic sin e
s qr t Squ a r e r oot
t a n, t a nh Ta n gen t a n d h yper bolic t a n gen t

A-9
A MATLAB Quick Reference

Coordina te System Conversion Linea r Equa tions


Usin g t h ese fu n ct ion s you ca n t r a n sfor m Ca r t esia n c hol Ch olesky fa ct or iza t ion
coor din a t es t o pola r , cylin dr ica l, or sph er ica l, a n d i nv Ma t r ix in ver se
vice ver sa . l s c ov Lea st squ a r es solu t ion in t h e
pr esen ce of kn own cova r ia n ce
Coordina te System Conversion lu LU m a t r ix fa ct or iza t ion
c a r t 2pol Tr a n sfor m Ca r t esia n l s qnonne g Non n ega t ive lea st squ a r es
coor din a t es t o pola r or mi n r e s Min im u m Residu a l Met h od
cylin dr ica l
pi nv Moor e-Pen r ose pseu doin ver se of
c a r t 2s ph Tr a n sfor m Ca r t esia n a m a t r ix
coor din a t es t o sph er ica l
qr Or t h ogon a l-t r ia n gu la r
pol 2c a r t Tr a n sfor m pola r or cylin dr ica l decom posit ion
coor din a t es t o Ca r t esia n
s y mml q Sym m et r ic LQ m et h od
s ph2c a r t Tr a n sfor m sph er ica l coor din a t es
t o Ca r t esia n
Eigenva lues a nd Singula r Va lues
M a trix Functions - N umerica l Linea r ba l a nc e Im pr ove a ccu r a cy of com pu t ed
eigen va lu es
Algebra
c df 2r df Con ver t com plex dia gon a l for m
Th ese fu n ct ion s let you per for m m a t r ix a n a lysis t o r ea l block dia gon a l for m
in clu din g m a t r ix det er m in a n t , r a n k, r edu ced r ow ei g E igen va lu es a n d eigen vect or s
ech elon for m , eigen va lu es, a n d in ver ses. gs vd Gen er a lized sin gu la r va lu e
decom posit ion
M a trix Ana lysis he s s H essen ber g for m of a m a t r ix
c ond Con dit ion n u m ber wit h r espect pol y Polyn om ia l wit h specified r oot s
t o in ver sion
qz QZ fa ct or iza t ion for gen er a lized
c onde i g Con dit ion n u m ber wit h r espect eigen va lu es
t o eigen va lu es
r s f 2c s f Con ver t r ea l Sch u r for m t o
de t Ma t r ix det er m in a n t com plex Sch u r for m
nor m Vect or a n d m a t r ix n or m s s c hur Sch u r decom posit ion
nul l Nu ll spa ce of a m a t r ix s vd Sin gu la r va lu e decom posit ion
or t h Ra n ge spa ce of a m a t r ix
r a nk Ra n k of a m a t r ix M a trix Functions
r c ond Ma t r ix r ecipr oca l con dit ion e xpm Ma t r ix expon en t ia l
n u m ber est im a t e f unm E va lu a t e gen er a l m a t r ix
r r e f , r r e f mo v i e Redu ced r ow ech elon for m fu n ct ion
s ubs pa c e An gle bet ween t wo su bspa ces l ogm Ma t r ix loga r it h m
t r ace Su m of dia gon a l elem en t s s qr t m Ma t r ix squ a r e r oot

A-10
Data Analysis and Fourier Transform Functions

Low Level Functions Ba sic O pera tions (Continued)


qr de l e t e Delet e colu m n fr om QR st d St a n da r d devia t ion
fa ct or iza t ion s um Su m of a r r a y elem en t s
qr i ns e r t In ser t colu m n in QR t r a pz Tr a pezoida l n u m er ica l
fa ct or iza t ion in t egr a t ion
va r Va r ia n ce
Da ta Ana lysis a nd Fourier Tra nsform
Functions Finite Differences
Usin g t h e da t a a n a lysis fu n ct ion s, you ca n fin d de l 2 Discr et e La pla cia n
per m u t a t ion s, pr im e n u m ber s, m ea n , m edia n , di f f Differ en ces a n d a ppr oxim a t e
va r ia n ce, cor r ela t ion , a n d per for m con volu t ion s der iva t ives
a n d ot h er st a n da r d a r r a y m a n ipu la t ion s. A set of gr a di e nt Nu m er ica l gr a dien t
vect or fu n ct ion s let s you oper a t e on vect or s t o fin d
cr oss pr odu ct , u n ion , a n d ot h er st a n da r d vect or Correla tion
m a n ipu la t ion s. Th e F ou r ier t r a n sfor m fu n ct ion s
c or r c oe f Cor r ela t ion coefficien t s
let you per for m discr et e F ou r ier t r a n sfor m a t ion s
in on e or m or e dim en sion s a n d t h eir in ver ses. c ov Cova r ia n ce m a t r ix

Ba sic O pera tions Filtering a nd Convolution


c u mp r o d Cu m u la t ive pr odu ct c onv Con volu t ion a n d polyn om ia l
c u ms u m Cu m u la t ive su m m u lt iplica t ion
c u mt r a p z Cu m u la t ive t r a pezoida l c onv2 Two-dim en sion a l con volu t ion
n u m er ica l in t egr a t ion de c onv Decon volu t ion a n d polyn om ia l
f a c t or P r im e fa ct or s division
i npol ygon Det ect poin t s in side a polygon a l f i l t er F ilt er da t a wit h a n in fin it e
r egion im pu lse r espon se (IIR) or fin it e
ma x Ma xim u m elem en t s of a n a r r a y im pu lse r espon se (F IR) filt er
me a n Aver a ge or m ea n va lu e of a r r a ys
f i l t er 2 Two-dim en sion a l digit a l
filt er in g
me d i a n Media n va lu e of a r r a ys
mi n Min im u m elem en t s of a n a r r a y Fourier Tra nsform s
p e r ms All possible per m u t a t ion s a bs Absolu t e va lu e a n d com plex
pol ya r e a Ar ea of polygon m a gn it u de
p r i me s Gen er a t e list of pr im e n u m ber s a ngl e P h a se a n gle
pr od P r odu ct of a r r a y elem en t s c pl xpa i r Sor t com plex n u m ber s in t o
r e c t i nt Rect a n gle in t er sect ion a r ea com plex con ju ga t e pa ir s
s or t Sor t elem en t s in a scen din g fft On e-dim en sion a l fa st Fou r ier
or der t r a n sfor m
s o r t r o ws Sor t r ows in a scen din g or der

A-11
A MATLAB Quick Reference

Fourier Tra nsforms (Continued)


fft2 Two-dim en sion a l fa st Fou r ier Polynom ia ls
t r a n sfor m c onv Con volu t ion a n d polyn om ia l
f f t s hi f t Sh ift DC com pon en t of fa st m u lt iplica t ion
Fou r ier t r a n sfor m t o cen t er of de c onv Decon volu t ion a n d polyn om ia l
spect r u m division
ifft In ver se on e-dim en sion a l fa st pol y Polyn om ia l wit h specified r oot s
Fou r ier t r a n sfor m pol yde r Polyn om ia l der iva t ive
ifft2 In ver se t wo-dim en sion a l fa st pol ye i g Polyn om ia l eigen va lu e pr oblem
Fou r ier t r a n sfor m pol yf i t Polyn om ia l cu r ve fit t in g
ifftn In ver se m u lt idim en sion a l fa st pol yi nt An a lyt ic polyn om ia l in t egr a t ion
Fou r ier t r a n sfor m
pol yva l Polyn om ia l eva lu a t ion
i f f t s hi f t In ver se F F T sh ift
pol yva l m Ma t r ix polyn om ia l eva lu a t ion
n e x t p o w2 Next power of t wo
r e s i due Con ver t bet ween pa r t ia l fr a ct ion
u n wr a p Cor r ect ph a se a n gles
expa n sion a n d polyn om ia l
coefficien t s
Vector Functions
r oot s Polyn om ia l r oot s
c r os s Vect or cr oss pr odu ct
i nt e r s e c t Set in t er sect ion of t wo vect or s Da ta Interpola tion
i s me mb e r Det ect m em ber s of a set c onvhul l Con vex h u ll
s e t di f f Ret u r n t h e set differ en ce of t wo c onvhul l n Mu lt idim en sion a l con vex h u ll
vect or
de l a una y Dela u n a y t r ia n gu la t ion
s e t xor Set exclu sive or of t wo vect or s
de l a una y3 3-D Dela u n a y t essella t ion
uni on Set u n ion of t wo vect or s
de l a una yn Mu lt idim en sion a l Dela u n a y
uni que Un iqu e elem en t s of a vect or t essella t ion
ds e a r c h Sea r ch for n ea r est poin t
Polynom ia l a nd Interpola tion
ds e a r c hn Mu lt idim en sion a l closest poin t
Functions sea r ch
Th ese fu n ct ion s let you oper a t e on polyn om ia ls gr i dda t a Da t a gr iddin g
su ch a s m u lt iply, divide, fin d der iva t ives, a n d gr i dda t a 3 Da t a gr iddin g a n d h yper su r fa ce
eva lu a t e. Th e da t a in t er pola t ion fu n ct ion s let you fit t in g for t h r ee-dim en sion a l
per for m in t er pola t ion in on e, t wo, t h r ee, a n d da t a
h igh er dim en sion s. gr i dda t a n Da t a gr iddin g a n d h yper su r fa ce
fit t in g (dim en sion >= 2)
i nt e r p1 On e-dim en sion a l da t a
in t er pola t ion (t a ble looku p)
i nt e r p2 Two-dim en sion a l da t a
in t er pola t ion (t a ble looku p)

A-12
Function Functions - N onlinear N umerical Methods

Da ta Interpola tion (Continued) Function Functions - N onlinea r N um erica l


i nt e r p3 Th r ee-dim en sion a l da t a M ethods (Continued)
in t er pola t ion (t a ble looku p) bvps e t Cr ea t e/a lt er BVP opt ion s
i nt e r pf t On e-dim en sion a l in t er pola t ion st r u ct u r e
u sin g t h e F F T m et h od bvpva l E va lu a t e t h e solu t ion com pu t ed
i nt e r pn Mu lt idim en sion a l da t a by b v p 4 c
in t er pola t ion (t a ble looku p) dbl qua d Nu m er ica l eva lu a t ion of dou ble
me s h g r i d Gen er a t e X a n d Y m a t r ices for in t egr a ls
t h r ee-dim en sion a l plot s f mi n b n d Min im ize a fu n ct ion of on e
ndgr i d Gen er a t e a r r a ys for va r ia ble
m u lt idim en sion a l fu n ct ion s a n d f mi n s e a r c h Min im ize a fu n ct ion of sever a l
in t er pola t ion va r ia bles
pc hi p P iecewise Cu bic H er m it e f zer o F in d zer o of a fu n ct ion of on e
In t er pola t in g Polyn om ia l va r ia ble
(P CH IP ) ode 45, ode 23, Solve initial value problems for
ppva l P iecewise polyn om ia l eva lu a t ion ode 113, ode 15s , ODEs
s pl i ne Cu bic splin e in t er pola t ion ode 23s , ode 23t ,
ode 23t b
t s ear ch Sea r ch for en closin g Dela u n a y
t r ia n gle
ode ge t E xt r a ct pa r a m et er s fr om ODE
opt ion s st r u ct u r e
t s e a r c hn Mu lt idim en sion a l closest
sim plex sea r ch
ode s e t Cr ea t e/a lt er ODE opt ion s
st r u ct u r e
vor onoi Vor on oi dia gr a m
o p t i mg e t Get opt im iza t ion opt ion s
vor onoi n Mu lt idim en sion a l Vor on oi st r u ct u r e pa r a m et er va lu es
dia gr a m s
o p t i ms e t Cr ea t e or edit opt im iza t ion
opt ion s pa r a m et er st r u ct u r e
Function Functions - N onlinea r
N umerica l M ethods pde pe Solve in it ia l-bou n da r y va lu e
pr oblem s
Usin g t h ese fu n ct ion s you ca n solve differ en t ia l pde va l E va lu a t e t h e solu t ion com pu t ed
equ a t ion s, per for m n u m er ica l eva lu a t ion of by p d e p e
in t egr a ls, a n d opt im ize fu n ct ion s. qua d Numerical evaluation of integrals,
adaptive Simpson quadrature
Function Functions - N onlinea r N umerica l qua dl Nu m er ica l eva lu a t ion of
M ethods in t egr a ls, a da pt ive Loba t t o
bvp4c Solve t wo-poin t bou n da r y va lu e qu a dr a t u r e
pr oblem s (BVP s) for or din a r y ve c t or i z e Vect or ize expr ession
differ en t ia l equ a t ion s (ODE s)
bvpge t E xt r a ct pa r a m et er s fr om BVP
opt ion s st r u ct u r e
bvpi ni t For m t h e in it ia l gu ess for b v p 4 c

A-13
A MATLAB Quick Reference

Spa rse M a trix Functions


V isua lizing Spa rse M a trices
Th ese fu n ct ion s a llow you t o oper a t e on a specia l s py Visu a lize spa r sit y pa t t er n
t ype of m a t r ix, spa r se. Usin g t h ese fu n ct ion s you
ca n con ver t fu ll t o spa r se, visu a lize, a n d oper a t e on
Reordering Algorithm s
t h ese m a t r ices.
c o l a md Colu m n a ppr oxim a t e m in im u m
degr ee per m u t a t ion
Elem enta ry Spa rse M a trices
c o l mmd Spa r se colu m n m in im u m degr ee
s pdi a gs E xt r a ct a n d cr ea t e spa r se ba n d
per m u t a t ion
a n d dia gon a l m a t r ices
c ol pe r m Spa r se colu m n per m u t a t ion
s pe ye Spa r se iden t it y m a t r ix
ba sed on n on zer o cou n t
s pr a nd Spa r se u n ifor m ly dist r ibu t ed
r a n dom m a t r ix
d mp e r m Du lm a ge-Men delsoh n
decom posit ion
s pr a ndn Spa r se n or m a lly dist r ibu t ed
r a n dom m a t r ix
r a ndpe r m Ra n dom per m u t a t ion
s y ma md Sym m et r ic a ppr oxim a t e
s pr a nds ym Spa r se sym m et r ic r a n dom
m in im u m degr ee per m u t a t ion
m a t r ix
s y mmmd Spa r se sym m et r ic m in im u m
degr ee or der in g
Full to Spa rse Conversion
s y mr c m Spa r se r ever se Cu t h ill-McKee
f i nd F in d in dices a n d va lu es of
or der in g
n on zer o elem en t s
f ul l Con ver t spa r se m a t r ix t o fu ll
N orm , Condition N um ber, a nd Ra nk
m a t r ix
c onde s t 1-n or m m a t r ix con dit ion n u m ber
s pa r s e Cr ea t e spa r se m a t r ix
est im a t e
s pc onve r t Im por t m a t r ix fr om spa r se
m a t r ix ext er n a l for m a t
n o r me s t 2-n or m est im a t e

W ork ing w ith N onzero Entries of Spa rse Spa rse Systems of Linea r Equa tions
M a trices bi c g BiCon ju ga t e Gr a dien t s m et h od
nnz Nu m ber of n on zer o m a t r ix bi c gs t a b BiCon ju ga t e Gr a dien t s
elem en t s St a bilized m et h od
nonz e r os Non zer o m a t r ix elem en t s c gs Con ju ga t e Gr a dien t s Squ a r ed
m et h od
n z ma x Am ou n t of st or a ge a lloca t ed for
n on zer o m a t r ix elem en t s c hol i nc Spa r se In com plet e Ch olesky a n d
Ch olesky-In fin it y fa ct or iza t ion s
s pa l l oc Alloca t e spa ce for spa r se m a t r ix
c hol upda t e Ra n k 1 u pda t e t o Ch olesky
s pf un Apply fu n ct ion t o n on zer o spa r se
fa ct or iza t ion
m a t r ix elem en t s
g mr e s Gen er a lized Min im u m Residu a l
s pone s Repla ce n on zer o spa r se m a t r ix
m et h od (wit h r est a r t s)
elem en t s wit h on es

A-14
Sound Processing Functions

Spa rse Systems of Linea r Equa tions (Continued) Sound Processing Functions
l s qr LSQR im plem en t a t ion of
Th e sou n d pr ocessin g fu n ct ion s let you con ver t
Con ju ga t e Gr a dien t s on t h e
n or m a l equ a t ion s
sign a ls, a n d r ea d a n d wr it e . a u a n d . wa v sou n d
files.
l ui nc In com plet e LU m a t r ix
fa ct or iza t ion s
Genera l Sound Functions
pc g P r econ dit ion ed Con ju ga t e
Gr a dien t s m et h od
l i n 2 mu Con ver t lin ea r a u dio sign a l t o
m u -la w
q mr Qu a si-Min im a l Residu a l m et h od
mu 2 l i n Con ver t m u -la w a u dio sign a l t o
qr Or t h ogon a l-t r ia n gu la r lin ea r
decom posit ion
s ound Con ver t vect or in t o sou n d
qr de l e t e Delet e colu m n fr om QR
fa ct or iza t ion
s ounds c Sca le da t a a n d pla y a s sou n d
qr i ns e r t In ser t colu m n in QR
fa ct or iza t ion SPARCsta tion-Specific Sound Functions
qr upda t e Ra n k 1 u pda t e t o QR a ur e a d Rea d NeXT/SUN (. a u ) sou n d file
fa ct or iza t ion a u wr i t e Wr it e NeXT/SUN (. a u ) sou n d
file
Spa rse Eigenva lues a nd Singula r Va lues
.WAV Sound Functions
e i gs F in d eigen va lu es a n d
eigen vect or s wa v p l a y P la y r ecor ded sou n d on a
P C-ba sed a u dio ou t pu t device
s vds F in d sin gu la r va lu es
wa v r e a d Rea d Micr osoft WAVE (. wa v )
sou n d file
M iscella neous
wa v r e c o r d Recor d sou n d u sin g a P C-ba sed
s p p a r ms Set pa r a m et er s for spa r se
a u dio in pu t device
m a t r ix r ou t in es
wa v wr i t e Wr it e Micr osoft WAVE (. wa v )
sou n d file

A-15
A MATLAB Quick Reference

Cha ra cter String Functions String M a nipula tion (Continued)


s y mv a r Det er m in e sym bolic va r ia bles in
Th is set of fu n ct ion s let s you m a n ipu la t e st r in gs
a n expr ession
su ch a s com pa r ison , con ca t en a t ion , sea r ch , a n d
con ver sion . t e xl a be l P r odu ce t h e TeX for m a t fr om a
ch a r a ct er st r in g

Genera l
uppe r Con ver t st r in g t o u pper ca se
a bs Absolu t e va lu e a n d com plex
m a gn it u de String to N umber Conversion
e va l In t er pr et st r in gs con t a in in g c ha r Cr ea t e ch a r a ct er a r r a y (st r in g)
MATLAB expr ession s i nt 2s t r In t eger t o st r in g con ver sion
r eal Rea l pa r t of com plex n u m ber ma t 2 s t r Con ver t a m a t r ix in t o a st r in g
s t r i ngs MATLAB st r in g h a n dlin g n u m2 s t r Nu m ber t o st r in g con ver sion
s pr i nt f Wr it e for m a t t ed da t a t o a st r in g
String to Function Ha ndle Conversion s s c a nf Rea d st r in g u n der for m a t
f unc 2s t r Con st r u ct s a fu n ct ion n a m e con t r ol
st r in g fr om a fu n ct ion h a n dle s t r 2doubl e Con ver t st r in g t o
s t r 2f unc Con st r u ct s a fu n ct ion h a n dle dou ble-pr ecision va lu e
fr om a fu n ct ion n a m e st r in g s t r 2 ma t St r in g t o m a t r ix con ver sion
s t r 2num St r in g t o n u m ber con ver sion
String M a nipula tion
de bl a nk St r ip t r a ilin g bla n ks fr om t h e Ra dix Conversion
en d of a st r in g
bi n2de c Bin a r y t o decim a l n u m ber
f i nds t r F in d on e st r in g wit h in a n ot h er con ver sion
l o we r Con ver t st r in g t o lower ca se de c 2bi n Decim a l t o bin a r y n u m ber
s t r cat St r in g con ca t en a t ion con ver sion
s t r c mp Com pa r e st r in gs de c 2he x Decim a l t o h exa decim a l n u m ber
s t r c mp i Com pa r e st r in gs ign or in g ca se con ver sion
s t r j us t J u st ify a ch a r a ct er a r r a y he x2de c H exa decim a l t o decim a l n u m ber
con ver sion
s t r ma t c h F in d possible m a t ch es for a
st r in g he x2num H exa decim a l t o dou ble n u m ber
con ver sion
s t r n c mp Com pa r e t h e fir st n ch a r a ct er s
of t wo st r in gs
s t r n c mp i Com pa r e t h e fir st n ch a r a ct er s
of st r in gs, ign or in g ca se
s t r r ep St r in g sea r ch a n d r epla ce
s t r t ok F ir st t oken in st r in g
s t r vc a t Ver t ica l con ca t en a t ion of st r in gs

A-16
File I/ O Functions

File I/ O Functions
Specia lized File I/ O
Th e file I/O fu n ct ion s a llow you t o open a n d close d l mr e a d Rea d a n ASCII delim it ed file
files, r ea d a n d wr it e for m a t t ed a n d u n for m a t t ed in t o a m a t r ix
da t a , oper a t e on files, a n d per for m ot h er
d l mwr i t e Wr it e a m a t r ix t o a n ASCII
specia lized file I/O su ch a s r ea din g a n d wr it in g delim it ed file
im a ges a n d spr ea dsh eet s.
hdf H DF in t er fa ce

File O pening a nd Closing


i mf i n f o Ret u r n in for m a t ion a bou t a
gr a ph ics file
f c l os e Close on e or m or e open files
i mr e a d Rea d im a ge fr om gr a ph ics file
f ope n Open a file or obt a in in for m a t ion
a bou t open files
i mwr i t e Wr it e a n im a ge t o a gr a ph ics file
s t r r ead Rea d for m a t t ed da t a fr om a
st r in g
Unforma tted I/ O t e xt r e a d Rea d for m a t t ed da t a fr om t ext
f r ead Rea d bin a r y da t a fr om file file
f wr i t e Wr it e bin a r y da t a t o a file wk 1 r e a d Rea d a Lot u s123 WK1
spr ea dsh eet file in t o a m a t r ix
Forma tted I/ O wk 1 wr i t e Wr it e a m a t r ix t o a Lot u s123
f ge t l Ret u r n t h e n ext lin e of a file a s a WK1 spr ea dsh eet file
st r in g wit h ou t lin e t er m in a t or (s)
f ge t s Ret u r n t h e n ext lin e of a file a s a Bitw ise Functions
st r in g wit h lin e t er m in a t or (s)
Th ese fu n ct ion s let you oper a t e a t t h e bit level
f pr i nt f Wr it e for m a t t ed da t a t o file
su ch a s sh ift in g a n d com plem en t in g.
f s c a nf Rea d for m a t t ed da t a fr om file
Bitw ise Functions
File Positioning bi t a nd Bit -wise AND
f e of Test for en d-of-file b i t c mp Com plem en t bit s
f e r r or Qu er y MATLAB a bou t er r or s in bi t or Bit -wise OR
file in pu t or ou t pu t
b i t ma x Ma xim u m floa t in g-poin t in t eger
f r e wi n d Rewin d a n open file
bi t s e t Set bit
f s eek Set file posit ion in dica t or
bi t s hi f t Bit -wise sh ift
f t el l Get file posit ion in dica t or
bi t ge t Get bit
bi t xor Bit -wise XOR
String Conversion
s pr i nt f Wr it e for m a t t ed da t a t o a st r in g
s s c a nf Rea d st r in g u n der for m a t
con t r ol

A-17
A MATLAB Quick Reference

Structure Functions M ATLAB Interfa ce to Ja va Functions


Th ese fu n ct ion s a llow you t o br in g J a va cla sses
St r u ct u r es a r e a r r a ys wh ose elem en t s ca n h old a n y in t o MATLAB, con st r u ct object s, a n d ca ll a n d sa ve
MATLAB da t a t ype su ch a s t ext , n u m er ic a r r a ys, m et h ods.
or ot h er st r u ct u r es. You a ccess st r u ct u r e elem en t s
by n a m e. Use t h e st r u ct u r e fu n ct ion s t o cr ea t e a n d Interfa ce to Ja va Functions
oper a t e on t h is a r r a y t ype. cl as s Cr ea t e object or r et u r n cla ss of
object
Structure Functions i mp o r t Add a package or class to the
de a l Dea l in pu t s t o ou t pu t s current Java import list
f i e l d n a me s F ield n a m es of a st r u ct u r e i sa Det ect a n object of a given cla ss
ge t f i e l d Get field of st r u ct u r e a r r a y i s j a va Test wh et h er a n object is a J a va
r mf i e l d Rem ove st r u ct u r e fields object
s et f i el d Set field of st r u ct u r e a r r a y j a v a Ar r a y Con st r u ct s a J a va a r r a y
s t r uc t Cr ea t e st r u ct u r e a r r a y j a v a Me t h o d In vokes a J a va m et h od
s t r uc t 2c e l l St r u ct u r e t o cell a r r a y j a v a Ob j e c t Con st r u ct s a J a va object
con ver sion me t h o d s Displa y m et h od n a m es
me t h o d s v i e w Displa y in for m a t ion on a ll
M ATLAB O bject Functions m et h ods im ple.m en t ed by a
cla ss
Usin g t h e object fu n ct ion s you ca n cr ea t e object s,
det ect object s of a given cla ss, a n d r et u r n t h e cla ss
of a n object .

O bject Functions
cl as s Cr ea t e object or r et u r n cla ss of
object
i sa Det ect a n object of a given cla ss
me t h o d s Displa y m et h od n a m es
me t h o d s v i e w Displa ys in for m a t ion on a ll
m et h ods im plem en t ed by a cla ss
s ubs a s gn Over loa ded m et h od for A(I)=B,
A{I}=B, a n d A.field=B
s ubs i nde x Over loa ded m et h od for X(A)
s ubs r e f Over loa ded m et h od for A(I), A{I}
a n d A.field

A-18
Cell Array Functions

Cell Arra y Functions M ultidim ensiona l Arra y Functions (Continued)


p e r mu t e Rea r r a n ge t h e dim en sion s of a
Cell a r r a ys a r e a r r a ys com pr ised of cells, wh ich
m u lt idim en sion a l a r r a y
ca n h old a n y MATLAB da t a t ype su ch a s t ext ,
n u m er ic a r r a ys, or ot h er cell a r r a ys. Un like r e s ha pe Resh a pe a r r a y
st r u ct u r es, you a ccess t h ese cells by n u m ber . Use s hi f t di m Sh ift dim en sion s
t h e cell a r r a y fu n ct ion s t o cr ea t e a n d oper a t e on s que e z e Rem ove sin glet on dim en sion s
t h ese a r r a ys. s ub2i nd Sin gle in dex fr om su bscr ipt s

Cell Arra y Functions Da ta Visua liza tion


cel l Cr ea t e cell a r r a y
Th is ext en sive set of fu n ct ion s gives you t h e a bilit y
c e l l f un Apply a fu n ct ion t o ea ch elem en t
t o cr ea t e ba sic gr a ph s su ch a s ba r , pie, pola r , a n d
in a cell a r r a y
t h r ee-dim en sion a l plot s, a n d a dva n ced gr a ph s
cel l s t r Cr ea t e cell a r r a y of st r in gs fr om su ch a s su r fa ce, m esh , con t ou r , a n d volu m e
ch a r a ct er a r r a y
visu a liza t ion plot s. In a ddit ion , you ca n u se t h ese
c e l l 2s t r uc t Cell a r r a y t o st r u ct u r e a r r a y fu n ct ion s t o con t r ol ligh t in g, color , view, a n d m a n y
con ver sion ot h er fin e m a n ipu la t ion s.
c e l l di s p Displa y cell a r r a y con t en t s
c e l l pl ot Gr a ph ica lly displa y t h e Ba sic Plots a nd Gra phs
st r u ct u r e of cell a r r a ys ba r Ver t ica l ba r ch a r t
n u m2 c e l l Con ver t a n u m er ic a r r a y in t o a ba r h H or izon t a l ba r ch a r t
cell a r r a y
hi s t P lot h ist ogr a m s
hi s t c H ist ogr a m cou n t
M ultidim ensiona l Arra y Functions
hol d H old cu r r en t gr a ph
Th ese fu n ct ion s pr ovide a m ech a n ism for wor kin g l ogl og P lot u sin g log-log sca les
wit h a r r a ys of dim en sion gr ea t er t h a n 2. pi e P ie plot
pl ot P lot vect or s or m a t r ices.
M ultidimensiona l Arra y Functions pol a r Pola r coor din a t e plot
cat Con ca t en a t e a r r a ys s e mi l o g x Sem i-log sca le plot
f l i pdi m F lip a r r a y a lon g a specified s e mi l o g y Sem i-log sca le plot
dim en sion
s ubpl ot Cr ea t e a xes in t iled posit ion s
i nd2s ub Su bscr ipt s fr om lin ea r in dex
i p e r mu t e In ver se per m u t e t h e dim en sion s
Three-Dim ensiona l Plotting
of a m u lt idim en sion a l a r r a y
ba r 3 Ver t ica l 3-D ba r ch a r t
ndgr i d Gen er a t e a r r a ys for
m u lt idim en sion a l fu n ct ion s a n d ba r 3h H or izon t a l 3-D ba r ch a r t
in t er pola t ion c o me t 3 3-D com et plot
n d i ms Nu m ber of a r r a y dim en sion s c yl i nde r Gen er a t e cylin der

A-19
A MATLAB Quick Reference

Three-Dimensiona l Plotting (Continued) Surfa ce, M esh, a nd Contour Plots (Continued)


fill3 Dr a w filled 3-D polygon s in me s h 3-D m esh wit h r efer en ce pla n e
3-spa ce pe a ks A sa m ple fu n ct ion of t wo
pl ot 3 P lot lin es a n d poin t s in 3-D va r ia bles
spa ce s ur f 3-D sh a ded su r fa ce gr a ph
qui ve r 3 Th r ee-dim en sion a l qu iver (or s ur f a c e Cr ea t e su r fa ce low-level object s
velocit y) plot
s ur f c Com bin a t ion su r f/con t ou r plot
s l i ce Volu m et r ic slice plot
s ur f l 3-D sh a ded su r fa ce wit h ligh t in g
s phe r e Gen er a t e sph er e
t r i me s h Tr ia n gu la r m esh plot
s t e m3 P lot discr et e su r fa ce da t a
t r i s ur f Tr ia n gu la r su r fa ce plot
wa t e r f a l l Wa t er fa ll plot
Volum e V isua liza tion
Plot Annota tion a nd Grids
c one pl ot P lot velocit y vect or s a s con es in
c l a be l Add con t ou r la bels t o a con t ou r 3-D vect or field
plot
c ont our s l i c e Dr a w con t ou r s in volu m e slice
da t e t i c k Da t e for m a t t ed t ick la bels pla n e
gr i d Gr id lin es for 2-D a n d 3-D plot s c ur l Com pu t e t h e cu r l a n d a n gu la r
gt e xt P la ce t ext on a 2-D gr a ph u sin g velocit y of a vect or field
a m ou se di ve r ge nc e Com pu t e t h e diver gen ce of a
l e ge nd Gr a ph legen d for lin es a n d vect or field
pa t ch es f l ow Gen er a t e sca la r volu m e da t a
pl ot e di t St a r t plot edit m ode t o edit a n d i n t e r p s t r e a ms p e e d In t er pola t e st r ea m lin e ver t ices
a n n ot a t e plot s fr om vect or-field m a gn it u des
pl ot yy P lot gr a ph s wit h Y t ick la bels on i s oc a ps Com pu t e isosu r fa ce en d-ca p
t h e left a n d r igh t geom et r y
title Tit les for 2-D a n d 3-D plot s i s oc ol or s Com pu t e t h e color s of isosu r fa ce
xl a be l X-a xis la bels for 2-D a n d 3-D ver t ices
plot s i s o n o r ma l s Com pu t e n or m a ls of isosu r fa ce
yl a be l Y-a xis la bels for 2-D a n d 3-D ver t ices
plot s i s os ur f a c e E xt r a ct isosu r fa ce da t a fr om
z l a be l Z-a xis la bels for 3-D plot s volu m e da t a
r e duc e pa t c h Redu ce t h e n u m ber of pa t ch
Surfa ce, M esh, a nd Contour Plots fa ces
c ont our Con t ou r (level cu r ves) plot r e d u c e v o l u me Redu ce n u m ber of elem en t s in
volu m e da t a set
c ont our c Con t ou r com pu t a t ion
s hr i nkf a c e s Redu ce t h e size of pa t ch fa ces
c ont our f F illed con t ou r plot
s l i ce Dr a w slice pla n es in volu m e
hi dde n Mesh h idden lin e r em ova l m ode
s mo o t h 3 Sm oot h 3-D da t a
me s h c Com bin a t ion m esh /con t ou r plot

A-20
Data Visualization

Volume V isua liza tion (Continued) Specia lized Plotting (Continued)


s t r e a m2 Com pu t e 2-D st r ea m lin e da t a e z me s h c E a sy t o u se com bin a t ion m esh /
s t r e a m3 Com pu t e 3-D st r ea m lin e da t a con t ou r plot t er
s t r e a ml i n e Dr a w st r ea m lin es fr om 2- or e z pl ot E a sy t o u se fu n ct ion plot t er
3-D vect or da t a e z pl ot 3 E a sy t o u se 3-D pa r a m et r ic
s t r e a mp a r t i c l e s Dr a w st r ea m pa r t icles fr om cu r ve plot t er
vect or volu m e da t a e z pol a r E a sy t o u se pola r coor din a t e
s t r e a mr i b b o n Dr a w st r ea m r ibbon s fr om plot t er
vect or volu m e da t a e z s ur f E a sy t o u se 3-D color ed su r fa ce
s t r e a ms l i c e Dr a w well-spa ced st r ea m lin es plot t er
fr om vect or volu m e da t a e z s ur f c E a sy t o u se com bin a t ion su r fa ce/
s t r e a mt u b e Dr a w st r ea m t u bes fr om vect or con t ou r plot t er
volu m e da t a f e a t he r Fea t h er plot
s ur f 2pa t c h Con ver t su r fa ce da t a t o pa t ch fill Dr a w filled 2-D polygon s
da t a f pl ot P lot a fu n ct ion
s u b v o l u me E xt r a ct su bset of volu m e da t a i npol ygon Tr u e for poin t s in side a
set polygon a l r egion
pa r e t o Pa r et o ch a r
Doma in Genera tion
pc ol or P seu docolor (ch ecker boa r d) plot
gr i dda t a Da t a gr iddin g a n d su r fa ce
fit t in g
pi e 3 3-D pie plot
p l o t ma t r i x Sca t t er plot m a t r ix
me s h g r i d Gen er a t ion of X a n d Y a r r a ys for
3-D plot s pol ya r e a Ar ea of polygon
qui ve r Qu iver (or velocit y) plot
Specia lized Plotting r i bbon Ribbon plot
ar ea Ar ea plot r os e P lot r ose or a n gle h ist ogr a m
box Axis box for 2-D a n d 3-D plot s s cat t er Sca t t er plot
c o me t Com et plot s cat t er 3 3-D sca t t er plot
c o mp a s s Com pa ss plot s t ai r s St a ir st ep gr a ph
c onvhul l Con vex h u ll s t em P lot discr et e sequ en ce da t a
de l a una y Dela u n a y t r ia n gu la t ion t s ear ch Sea r ch for en closin g Dela u n a y
ds e a r c h Sea r ch Dela u n a y t r ia n gu la t ion t r ia n gle
for n ea r est poin t vor onoi Vor on oi dia gr a m
e r r or ba r P lot gr a ph wit h er r or ba r s
e z c ont our E a sy t o u se con t ou r plot t er
e z c ont our f E a sy t o u se filled con t ou r plot t er
e z me s h E a sy t o u se 3-D m esh plot t er

A-21
A MATLAB Quick Reference

V iew Control Tra nspa rency


c a md o l l y Move ca m er a posit ion a n d t a r get a l pha Set or qu er y t r a n spa r en cy
c a ml o o k a t View specific object s pr oper t ies for object s in cu r r en t
a xes
c a mo r b i t Or bit a bou t ca m er a t a r get
a l p h a ma p Specify t h e figu r e a lph a m a p
c a mp a n Rot a t e ca m er a t a r get a bou t
ca m er a posit ion al i m Set or qu er y t h e a xes a lph a
lim it s
c a mp o s Set or get ca m er a posit ion
c a mp r o j Set or get pr oject ion t ype
Color O pera tions
c a mr o l l Rot a t e ca m er a a bou t viewin g
a xis
br i ght e n Br igh t en or da r ken color m a p
c a xi s P seu docolor a xis sca lin g
c a mt a r g e t Set or get ca m er a t a r get
c ol or ba r Displa y color ba r (color sca le)
c a mu p Set or get ca m er a u p-vect or
c ol or de f Set u p color defa u lt s
c a mv a Set or get ca m er a view a n gle
c o l o r ma p Set t h e color look-u p t a ble (list of
c a mz o o m Zoom ca m er a in or ou t
color m a ps)
da s pe c t Set or get da t a a spect r a t io
g r a y mo n Gr a ph ics figu r e defa u lt s set for
pba s pe c t Set or get plot box a spect r a t io gr a ysca le m on it or
vi e w 3-D gr a ph viewpoin t hs v2r gb H u e-sa t u r a t ion -va lu e t o
specifica t ion . r ed-gr een -blu e con ver sion
v i e wmt x Gen er a t e view t r a n sfor m a t ion r gb2hs v RGB t o H SV con ver sion
m a t r ices
r gbpl ot P lot color m a p
xl i m Set or get t h e cu r r en t x-a xis
lim it s
s ha di ng Color sh a din g m ode
yl i m Set or get t h e cu r r en t y-a xis
s p i n ma p Spin t h e color m a p
lim it s s ur f nor m 3-D su r fa ce n or m a ls
zl i m Set or get t h e cu r r en t z-a xis wh i t e b g Ch a n ge a xes ba ckgr ou n d color
lim it s for plot s

Lighting Colorm a ps
c a ml i g h t Cr ea t e or posit ion a ligh t a u t u mn Sh a des of r ed a n d yellow color
map
l i ght Ligh t object cr ea t ion fu n ct ion
bone Gr a y-sca le wit h a t in ge of blu e
l i ght a ngl e Sph er ica l posit ion of a ligh t
color m a p
l i ght i ng Ligh t in g m ode
c ont r a s t Gr a y color m a p t o en h a n ce
ma t e r i a l Ma t er ia l r eflect a n ce m ode im a ge con t r a st
c ool Sh a des of cya n a n d m a gen t a
color m a p
c oppe r Lin ea r copper-t on e color m a p

A-22
Data Visualization

Colorma ps (Continued) Ha ndle Gra phics, Genera l (Continued)


f l ag Alt er n a t in g r ed, wh it e, blu e, a n d ge t Get object pr oper t ies
bla ck color m a p i s ha ndl e Tr u e for gr a ph ics object s
gr a y Lin ea r gr a y-sca le color m a p r ot a t e Rot a t e object s a bou t specified
hot Bla ck-r ed-yellow-wh it e color or igin a n d dir ect ion
map s et Set object pr oper t ies
hs v H u e-sa t u r a t ion -va lu e (H SV)
color m a p W ork ing w ith Applica tion Da ta
j et Va r ia n t of H SV ge t a ppda t a Get va lu e of a pplica t ion da t a
l i ne s Lin e color color m a p i s a ppda t a Tr u e if a pplica t ion da t a exist s
pr i s m Color m a p of pr ism color s r ma p p d a t a Rem ove a pplica t ion da t a
s pr i ng Sh a des of m a gen t a a n d yellow s e t a ppda t a Specify a pplica t ion da t a
color m a p
s u mme r Sh a des of gr een a n d yellow Ha ndle Gra phics, O bject Crea tion
color m a p
a xe s Cr ea t e a xes object
wi n t e r Sh a des of blu e a n d gr een color
map
f i gur e Cr ea t e figu r e (gr a ph ) win dows
i ma g e Cr ea t e im a ge (2-D m a t r ix)
Printing l i ght Cr ea t e ligh t object (illu m in a t es
or i e nt H a r dcopy pa per or ien t a t ion pa t ch a n d su r fa ce)
pa ge s e t updl g Pa ge posit ion dia log box l i ne Cr ea t e lin e object (3-D polylin es)
pr i nt P r in t gr a ph or sa ve gr a ph t o file pa t c h Cr ea t e pa t ch object (polygon s)
pr i nt dl g P r in t dia log box r e c t a ngl e Cr ea t e r ect a n gle object (2-D
r ect a n gle)
pr i nt opt Con figu r e loca l pr in t er defa u lt s
s ur f a c e Cr ea t e su r fa ce (qu a dr ila t er a ls)
s a ve a s Sa ve figu r e t o gr a ph ic file
t e xt Cr ea t e t ext object (ch a r a ct er
st r in gs)
Ha ndle Gra phics, Genera l u i c o n t e x t me n u Cr ea t e con t ext m en u (pop-u p
a l l c hi l d F in d a ll ch ildr en of specified a ssocia t ed wit h object )
object s
c opyobj Ma ke a copy of a gr a ph ics object Ha ndle Gra phics, Figure W indow s
a n d it s ch ildr en c a pt ur e Scr een ca pt u r e of t h e cu r r en t
f i nda l l F in d a ll gr a ph ics object s figu r e
(in clu din g h idden h a n dles) cl c Clea r figu r e win dow
f i ndobj F in d object s wit h specified cl f Clea r figu r e
pr oper t y va lu es
c l os e Close specified win dow
gc bo Ret u r n object wh ose ca llba ck is
cu r r en t ly execu t in g
c l os e r e q Defa u lt close r equ est fu n ct ion
gc f Get cu r r en t figu r e h a n dle
gc o Ret u r n h a n dle of cu r r en t object

A-23
A MATLAB Quick Reference

Ha ndle Gra phics, Figure W indow s (Continued) Gra phica l User Interfa ces
n e wp l o t Gr a ph ics M-file pr ea m ble for
Th e gr a ph ica l u ser in t er fa ce fu n ct ion s let you bu ild
Ne x t Pl o t pr oper t y
you r own in t er fa ces for you r a pplica t ion s.
r ef r es h Refr esh figu r e
s a ve a s Sa ve figu r e or m odel t o desir ed Dia log Box es
ou t pu t for m a t
di a l og Cr ea t e a dia log box

Ha ndle Gra phics, Ax es


e r r or dl g Cr ea t e er r or dia log box
he l pdl g Displa y h elp dia log box
a xi s P lot a xis sca lin g a n d a ppea r a n ce
i nput dl g Cr ea t e in pu t dia log box
cl a Clea r a xes
l i s t dl g Cr ea t e list select ion dia log box
gc a Get cu r r en t a xes h a n dle
ms g b o x Cr ea t e m essa ge dia log box
O bject M a nipula tion pa ge dl g Displa y pa ge la you t dia log box
r es et Reset a xis or figu r e pr i nt dl g Displa y pr in t dia log box
r ot a t e 3d In t er a ct ively r ot a t e t h e view of a que s t dl g Cr ea t e qu est ion dia log box
3-D plot ui ge t f i l e Displa y dia log box t o r et r ieve
s e l e c t mo v e r e s i z e In t er a ct ively select , m ove, or n a m e of file for r ea din g
r esize object s ui put f i l e Displa y dia log box t o r et r ieve
n a m e of file for wr it in g
Intera ctive User Input ui s e t c ol or In t er a ct ively set a Co l o r Sp e c
u sin g a dia log box
gi nput Gr a ph ica l in pu t fr om a m ou se or
cu r sor ui s e t f ont In t er a ct ively set a fon t u sin g a
dia log box
z oom Zoom in a n d ou t on a 2-D plot
wa r n d l g Cr ea t e wa r n in g dia log box
Region of Interest
User Interfa ce Deploym ent
dr a gr e c t Dr a g XOR r ect a n gles wit h
m ou se gui da t a St or e or r et r ieve a pplica t ion
da t a
d r a wn o w Com plet e a n y pen din g dr a win g
gui ha ndl e s Cr ea t e a st r u ct u r e of h a n dles
r bbox Ru bber ba n d box
mo v e g u i Move GUI figu r e on scr een
ope nf i g Open or r a ise GUI figu r e

User Interfa ce Developm ent


gui de Open t h e GUI La you t E dit or
i ns pe c t Displa y P r oper t y In spect or

A-24
Serial Port I/ O

Seria l Port I/ O
User Interfa ce O bjects Th ese fu n ct ion s pr ovides dir ect a ccess t o
me n u Gen er a t e a m en u of ch oices for per iph er a l devices t h a t you con n ect t o you r
u ser in pu t com pu t er ’s ser ia l por t .
u i c o n t e x t me n u Cr ea t e con t ext m en u
Crea ting a Seria l Port O bject
ui c ont r ol Cr ea t e u ser in t er fa ce con t r ol
s er i al Cr ea t e a ser ia l por t object
u i me n u Cr ea t e u ser in t er fa ce m en u

W riting a nd Rea ding Da ta


O ther Functions
f ge t l Rea d on e lin e of t ext fr om t h e
dr a gr e c t Dr a g r ect a n gles wit h m ou se
device a n d disca r d t h e
f i ndf i gs Displa y off-scr een visible figu r e t er m in a t or
win dows
f ge t s Rea d on e lin e of t ext fr om t h e
gc bf Ret u r n h a n dle of figu r e device a n d in clu de t h e
con t a in in g ca llba ck object t er m in a t or
gc bo Ret u r n h a n dle of object wh ose f pr i nt f Wr it e t ext t o t h e device
ca llba ck is execu t in g
f r ead Rea d bin a r y da t a fr om t h e
r bbox Cr ea t e r u bber ba n d box for a r ea device
select ion
f s c a nf Rea d da t a fr om t h e device, a n d
s e l e c t mo v e r e s i z e Select , m ove, r esize, or copy a xes for m a t a s t ext
a n d u icon t r ol gr a ph ics object s
f wr i t e Wr it e bin a r y da t a t o t h e device
t e x t wr a p Ret u r n wr a pped st r in g m a t r ix
r e a da s ync Rea d da t a a syn ch r on ou sly fr om
for given u icon t r ol
t h e device
u i r e s u me Used wit h u i wa i t , con t r ols
s t opa s ync St op a syn ch r on ou s a n d wr it e
pr ogr a m execu t ion
oper a t ion s
u i wa i t Used wit h u i r e s u me , con t r ols
pr ogr a m execu t ion
Configuring a nd Returning Properties
wa i t b a r Displa y wa it ba r
ge t Ret u r n ser ia l por t object
wa i t f o r b u t t o n p r e s s Wa it for key/bu t t on pr ess over
pr oper t ies
figu r e
s et Con figu r e or displa y ser ia l por t
object pr oper t ies

Sta te Cha nge


f c l os e Discon n ect a ser ia l por t object
fr om t h e device
f ope n Con n ect a ser ia l por t object t o
t h e device
r e c or d Recor d da t a a n d even t
in for m a t ion t o a file

A-25
A MATLAB Quick Reference

Genera l Purpose
cl ear Rem ove a ser ia l por t object fr om
t h e MATLAB wor kspa ce
de l e t e Rem ove a ser ia l por t object fr om
m em or y
di s p Displa y ser ia l por t object
su m m a r y in for m a t ion
i ns t r a c t i on Displa y even t in for m a t ion wh en
a n even t occu r s
i ns t r f i nd Ret u r n ser ia l por t object s fr om
m em or y t o t h e MATLAB
wor kspa ce
i s va l i d Det er m in e if ser ia l por t object s
a r e va lid
l e ngt h Len gt h of ser ia l por t object a r r a y
l oa d Loa d ser ia l por t object s a n d
va r ia bles in t o t h e MATLAB
wor kspa ce
s a ve Sa ve ser ia l por t object s a n d
va r ia bles t o a MAT-file
s e r i a l br e a k Sen d a br ea k t o t h e device
con n ect ed t o t h e ser ia l por t
s i ze Size of ser ia l por t object a r r a y

A-26
B
Sym bolic Ma t h Toolbox
Qu ick Refer en ce
In tro d u c tio n . . . . . . . . . . . . . . . . . . . . B-2

Arith m e tic Op e ra tio n s . . . . . . . . . . . . . . . B-3

B a s ic Op e ra tio n s . . . . . . . . . . . . . . . . . . B-3

Ca lc u lu s . . . . . . . . . . . . . . . . . . . . . . B-3

Co n v e rs io n s . . . . . . . . . . . . . . . . . . . . B-3

In te g ra l Tra n s fo rm s . . . . . . . . . . . . . . . . B-3

Lin e a r Alg e bra . . . . . . . . . . . . . . . . . . . B-3

P e d a g o g ic a l a n d Gra p h ic a l Ap p lic a tio n s . . . . . . . B-4

S im p lific a tio n . . . . . . . . . . . . . . . . . . . B-4

S o lu tio n o f Equ a tio n s . . . . . . . . . . . . . . . . B-4

S p e c ia l F u n c tio n s . . . . . . . . . . . . . . . . . B-4

Va ria ble P re c is io n Arith m e tic . . . . . . . . . . . . B-4


B Symbolic Math Toolbox Quick Reference

Introduction
Th is a ppen dix list s t h e Sym bolic Ma t h Toolbox fu n ct ion s t h a t a r e a va ila ble in
t h e St u den t Ver sion of MATLAB & Sim u lin k. F or com plet e in for m a t ion a bou t
a n y of t h ese fu n ct ion s, u se H elp a n d select Refer en ce fr om t h e S y m bo lic Ma th
To o lbo x .

N ote All of t h e fu n ct ion s list ed in Sym bolic Ma t h Toolbox Refer en ce a r e


a va ila ble in t h e St u den t Ver sion of MATLAB & Sim u lin k except ma p l e ,
ma p l e i n i t , mf u n , mf u n l i s t , a n d mh e l p .

B -2
Symbolic Math Toolbox Quick Reference

Arithmetic O pera tions Ca lculus (Continued)


+ Addit ion s y ms u m Su m m a t ion of ser ies
- Su bt r a ct ion t a yl or Ta ylor ser ies expa n sion
* Mu lt iplica t ion
Conversions
.* Ar r a y m u lt iplica t ion
c ha r Con ver t s y m object t o st r in g
/ Righ t division
doubl e Con ver t sym bolic m a t r ix t o d o u b l e
./ Ar r a y r igh t division
pol y2s ym F u n ct ion ca lcu la t or
\ Left division
s y m2 p o l y Sym bolic polyn om ia l t o coefficien t
.\ Ar r a y left division vect or
^ Ma t r ix or sca la r r a ised t o a power
.^ Ar r a y r a ised t o a power Integra l Tra nsform s
‘ Com plex con ju ga t e t r a n spose f our i e r Fou r ier t r a n sfor m
.’ Rea l t r a n spose i f our i e r In ver se Fou r ier t r a n sfor m
i l a pl a c e In ver se La pla ce t r a n sfor m
Ba sic O pera tions
i z t r a ns In ver se z-t r a n sfor m
c c ode C code r epr esen t a t ion of a
sym bolic expr ession l a pl a c e La pla ce t r a n sfor m
c onj Com plex con ju ga t e z t r a ns z-t r a n sfor m
f i nds ym Det er m in e sym bolic va r ia bles
Linea r Algebra
f or t r a n For t r a n r epr esen t a t ion of a
sym bolic expr ession c ol s pa c e Ba sis for colu m n spa ce
i ma g Im a gin a r y pa r t of a com plex de t Det er m in a n t
n u m ber di a g Cr ea t e or ext r a ct dia gon a ls
l at ex La TeX r epr esen t a t ion of a ei g E igen va lu es a n d eigen vect or s
sym bolic expr ession e xpm Ma t r ix expon en t ia l
pr e t t y P r et t y pr in t a sym bolic expr ession i nv Ma t r ix in ver se
r eal Rea l pa r t of a n im a gin a r y n u m ber j or da n J or da n ca n on ica l for m
s ym Cr ea t e sym bolic object nul l Ba sis for n u ll spa ce
s y ms Sh or t cu t for cr ea t in g m u lt iple pol y Ch a r a ct er ist ic polyn om ia l
sym bolic object s
r a nk Ma t r ix r a n k
r r ef Redu ced r ow ech elon for m
Ca lculus
s vd Sin gu la r va lu e decom posit ion
di f f Differ en t ia t e
tril Lower t r ia n gle
i nt In t egr a t e
triu Upper t r ia n gle
j a c obi a n J a cobia n m a t r ix
l i mi t Lim it of a n expr ession

B -3
B Symbolic Math Toolbox Quick Reference

Peda gogica l a nd Gra phica l Applica tions Specia l Functions


e z c ont our Con t ou r plot t er c os i nt Cosin e in t egr a l, Ci(x)
λ( x )
e z c ont our f F illed con t ou r plot t er l a mb e r t w Solu t ion of λ ( x )e = x
e z me s h Mesh plot t er s i ni nt Sin e in t egr a l, Si(x)
e z me s h c Com bin ed m esh a n d con t ou r zet a Riem a n n z e t a fu n ct ion
plot t er
e z pl ot F u n ct ion plot t er Va ria ble Precision Arithm etic
e z pl ot E a sy-t o-u se fu n ct ion plot t er di gi t s Set va r ia ble pr ecision a ccu r a cy
e z pl ot 3 3-D cu r ve plot t er vpa Va r ia ble pr ecision a r it h m et ic
e z pol a r Pola r coor din a t e plot t er
e z s ur f Su r fa ce plot t er
e z s ur f c Com bin ed su r fa ce a n d con t ou r
plot t er
f unt ool F u n ct ion ca lcu la t or
r s u ms Riem a n n su m s
t a yl or t ool Ta ylor ser ies ca lcu la t or

Sim plifica tion


c ol l e c t Collect com m on t er m s
e xpa nd E xpa n d polyn om ia ls a n d
elem en t a r y fu n ct ion s
f a c t or Fa ct or
hor ne r Nest ed polyn om ia l r epr esen t a t ion
n u md e n Nu m er a t or a n d den om in a t or
s i mp l e Sea r ch for sh or t est for m
s i mp l i f y Sim plifica t ion
s ube xpr Rewr it e in t er m s of
su bexpr ession s

Solution of Equa tions


c o mp o s e F u n ct ion a l com posit ion
ds ol ve Solu t ion of differ en t ia l equ a t ion s
f i nve r s e F u n ct ion a l in ver se
s ol ve Solu t ion of a lgebr a ic equ a t ion s

B -4
Index

Symbols
: oper a t or 4-7 a xis
@s y m dir ect or y 7-15 la bels 5-12
\ 7-65, 7-66 t it les 5-12
a x i s 5-10

A
a bst r a ct fu n ct ion s 7-10 B
Air y differ en t ia l equ a t ion 7-93 ba cksla sh oper a t or 7-65
Air y fu n ct ion 7-93 Bessel fu n ct ion s
a lgor it h m s differ en t ia t in g 7-17
vect or izin g 6-23 in t egr a t in g 7-24
a n im a t ion 5-37 b e s s e l j 7-17
a n n ot a t in g plot s 5-14 b e s s e l k 7-93
a n s 4-4 bit m a p 5-25
Applica t ion P r ogr a m In t er fa ce (AP I) 1-13 bit wise fu n ct ion s A-17
Ar r a y E dit or 3-13 blockset 1-14
a r r a y oper a t or s 4-22 bookm a r kin g docu m en t a t ion 3-10
a r r a ys 4-18, 4-21 books
cell 6-9 MATLAB-r ela t ed 1-10
ch a r a ct er 6-11 br a n ch cu t 7-42
colu m n wise or ga n iza t ion 4-23 b r e a k 6-6
con ca t en a t in g 4-16
cr ea t in g in M-files 4-15
delet in g r ows a n d colu m n s 4-16 C
delet in g r ows or colu m n s 4-16 ca lcu lu s 7-16–7-42
elem en t s 4-10 c a s e 6-4
gen er a t in g wit h fu n ct ion s a n d oper a t or s 4-14 cell a r r a y fu n ct ion s A-19
list in g con t en t s 4-10 cell a r r a ys 6-9
loa din g fr om ext er n a l da t a files 4-15 c h a r 6-13
m u lt idim en sion a l 6-7 ch a r a ct er a r r a ys 6-11
n ot a t ion for elem en t s 4-10 ch a r a ct er st r in g fu n ct ion s A-16
pr ea lloca t in g 6-23 ch a r a ct er ist ic polyn om ia l 4-21, 7-69, 7-71
st r u ct u r e 6-14 cir cu la n t m a t r ix 7-10, 7-53
va r ia ble n a m es 4-10 c l e a r 7-26
a spect r a t io of a xes 5-11 clea r in g va r ia bles
a xes 5-10 in t h e Ma ple wor kspa ce 7-27

I-1
Index

in t h e MATLAB wor kspa ce 7-27 D


c o l l e c t 7-44 da t a a n a lysis a n d F ou r ier t r a n sfor m fu n ct ion s
colon oper a t or 4-7 A-11–A-12
color m a p 5-20 da t a visu a liza t ion A-19–A-24
color s debu ggin g com m a n ds A-6–A-7
lin es for plot t in g 5-4 debu ggin g M-files 3-14
c o l s p a c e 7-66 decim a l sym bolic expr ession s 7-8
colu m n spa ce 7-66 defa u lt sym bolic va r ia ble 7-11
Com m a n d H ist or y 3-7 defin it e in t egr a t ion 7-23
com m a n d lin e edit in g 4-30 delet in g a r r a y elem en t s 4-16
Com m a n d Win dow 3-6 dem on st r a t ion pr ogr a m s 6-27
com m a n ds dem os
gen er a l pu r pose A-3–A-4 MATLAB 1-6
c o mp . s o f t - s y s . ma t l a b 1-10 d e mo s 6-27
com plex n u m ber s, plot t in g 5-6 dem os, r u n n in g fr om t h e La u n ch P a d 3-8
com plex sym bolic va r ia bles 7-9 deskt op for MATLAB 3-4
con ca t en a t in g deskt op t ools 3-6
a r r a ys 4-16 det er m in a n t of m a t r ix 4-19
st r in gs 6-13 developm en t en vir on m en t 3-2
con ca t en a t ion 4-16 d i a g 4-5
con figu r in g a ddit ion a l pr odu ct s d i f f 7-16
on Lin u x 2-13 differ en t ia t ion 7-16–7-19
on P C 2-5 d i g i t s 7-8
con figu r in g t h e deskt op 3-5 discon t in u it ies 7-41
c o n j 7-9 displa y pa n e in H elp br owser 3-10
con st a n t s docu m en t a t ion 3-8
specia l 4-12 docu m en t a t ion pa t h
con t en t s in H elp br owser 3-10 set t in g on Lin u x 2-12
c o n t i n u e 6-5 d o u b l e 7-61
con t in u in g st a t em en t s on m u lt iple lin es 4-30 d s o l v e 7-90
con ver t in g sym bolic m a t r ices t o n u m er ic for m
7-8
coor din a t e syst em con ver sion A-10 E
cu r r en t dir ect or y 3-11 edit in g com m a n d lin es 4-30
Cu r r en t Dir ect or y br owser 3-11 edit in g plot s
in t er a ct ively 5-15
E dit or /Debu gger 3-14

I-2
Index

e i g 7-68 floa t in g-poin t a r it h m et ic 7-58


eigen va lu e 4-20 floa t in g-poin t n u m ber s 4-11
eigen va lu e t r a ject or ies 7-78–7-85 floa t in g-poin t sym bolic expr ession s 7-7
eigen va lu es 7-68–7-74, 7-79 flow con t r ol 6-2
com pu t in g 7-68 f o r 6-4
eigen vect or 4-20, 7-69 for m a t
elem en t a r y m a t h fu n ct ion s A-8–A-9 of ou t pu t displa y 4-28
elem en t a r y m a t r ices a n d m a t r ix m a n ipu la t ion f o r ma t 4-28, 7-58
A-7–A-8 f u n c t i o n 6-19
elem en t s of a r r a ys 4-10 fu n ct ion fu n ct ion s 6-24
en t er in g m a t r ices 4-3 n on lin ea r n u m er ica l m et h ods A-13
en vir on m en t 3-2 fu n ct ion h a n dles
e p s 7-8 defin ed 6-24
er a se m ode 5-37 u sin g 6-25
e v a l 6-22 fu n ct ion M-file 6-17, 6-18
execu t in g MATLAB 3-3 fu n ct ion of t wo va r ia bles 5-18
exit in g MATLAB 3-3 fu n ct ion s 4-11, 6-18
e x p a n d 7-44 bit wise A-17
expor t in g da t a 3-15 bu ilt -in 4-12
expr ession s 4-10, 4-13 cell a r r a y A-19
eva lu a t in g 6-22 ch a r a ct er st r in g A-16
ext er n a l pr ogr a m s, r u n n in g fr om MATLAB 3-7 coor din a t e syst em con ver sion A-10
e z p l o t 7-30 da t a a n a lysis a n d F ou r ier t r a n sfor m A-11–
A-12
da t a visu a liza t ion A-19–A-24
F elem en t a r y m a t h A-8–A-9
f a c t o r 7-45 elem en t a r y m a t r ices a n d m a t r ix m a n ipu la t ion
fa vor it es in H elp br owser 3-10 A-7–A-8
f i g u r e 5-9 file I/O A-17
figu r e win dows 5-9 fin din g specific 1-7
wit h m u lt iple plot s 5-9 fu n ct ion fu n ct ion s - n on lin ea r n u m er ica l
file I/O fu n ct ion s A-17 m et h ods A-13
f i n d 4-26 gen er a l pu r pose com m a n ds A-3–A-4
fin din g in a pa ge 3-10 gr a ph ica l u ser in t er fa ce con t r ol A-24–A-25
fin din g object h a n dles 5-33 J a va in t er fa ce A-18
f i n d s y m 7-13 la n gu a ge con st r u ct s a n d debu ggin g A-5–A-7
f l i p l r 4-5 logica l A-5

I-3
Index

m a t r ix fu n ct ion s - n u m er ica l lin ea r a lgebr a a ccessin g on Lin u x 2-14


A-10–A-11 a ccessin g on P C 2-6
m u lt idim en sion a l a r r a y A-19 h elp
object A-18 sou r ces of 1-8
oper a t or s a n d specia l ch a r a ct er s A-5 via Ma t h Wor ks Kn owledge Ba se 1-10
polyn om ia l a n d in t er pola t ion A-12–A-13 via Ma t h Wor ks Web sit e 1-10
ser ia l por t I/O A-25–A-26 via n ewsgr ou p 1-10
sou n d pr ocessin g A-15 H elp br owser 3-8
spa r se m a t r ix A-14–A-15 h elp fu n ct ion s 3-10
specia lized m a t h A-9 H elp Na viga t or 3-10
specia lized m a t r ices A-8 h ier a r ch y of gr a ph ics object s 5-29
st r u ct u r e A-18 H ilber t m a t r ix 7-8, 7-64
va r ia ble n u m ber of a r gu m en t s 6-19 h o l d 5-7
h o r n e r 7-45

G
Given s t r a n sfor m a t ion 7-63, 7-72 I
globa l va r ia bles 6-20 IE E E floa t in g-poin t a r it h m et ic 7-59
golden r a t io 7-6 i f 6-2
gr a ph ica l u ser in t er fa ce 5-35 im a ges 5-24
gr a ph ica l u ser in t er fa ce con t r ol A-24–A-25 im a gin a r y n u m ber 4-10
gr a ph ics Im por t Wiza r d 3-15
2-D 5-2 im por t in g da t a 3-15
files 5-26 in dex in H elp br owser 3-10
h a n dle gr a ph ics 5-28 in st a lla t ion pr ocedu r e
object s 5-28 select in g pr odu ct s on Lin u x 2-11
pr in t in g 5-26 in st a lla t ion r oot dir ect or y
gr ids 5-12 specifyin g 2-10
gu ide 5-35 in st a llin g MATLAB
on Lin u x 2-8
on P C 2-4
H in st a llin g t oolboxes
H a n dle Gr a ph ics 1-13, 5-28 on Lin u x 2-13
fin din g h a n dles 5-33 on P C 2-5
pr oper t ies 5-31 i n t 7-22
H elp in t egr a t ion 7-22–7-27
a ccessin g 1-8 defin it e 7-23

I-4
Index

wit h r ea l con st a n t s 7-24 M


m a ch in e epsilon 7-8
Ma cla u r in ser ies 7-28
J ma g i c 4-8
j a c o b i a n 7-18 m a gic squ a r e 4-4
J a cobia n m a t r ix 7-18 Ma ple 7-2
J a va in t er fa ce fu n ct ion s A-18 m a r ker s 5-5
j o r d a n 7-74 MAT-file 5-24
J or da n ca n on ica l for m 7-74–7-75 m a t h em a t ica l fu n ct ion libr a r y 1-13
m a t h em a t ica l fu n ct ion s
list in g a dva n ced 4-12
L list in g elem en t a r y 4-11
la n gu a ge con st r u ct s A-5–A-6 list in g m a t r ix 4-12
La u n ch P a d 3-8 Ma t h Wor ks Kn owledge Ba se 1-10
lea r n in g MATLAB 1-6 Ma t h Wor ks St or e
l e g e n d 5-3 pu r ch a sin g pr odu ct s fr om 1-10
legen d, a ddin g t o plot 5-3 Ma t h Wor ks Web sit e 1-10
libr a r y MATLAB
m a t h em a t ica l fu n ct ion 1-13 Applica t ion P r ogr a m In t er fa ce 1-13
licen se a gr eem en t 2-10 books 1-10
ligh t in g 5-21 ca llin g C su br ou t in e 2-3, 2-8
l i mi t 7-20 ca llin g F or t r a n su br ou t in e 2-3, 2-8
lim it s 7-20–7-21 dem os 1-6
t wo-sided 7-20 h ist or y 1-12
u n defin ed 7-21 h ow t o lea r n 1-6
lim it s, a xes 5-11 in st a llin g on Lin u x 2-8
lin e con t in u a t ion 4-30 in st a llin g on P C 2-4
lin e st yles of plot s 5-4 la n gu a ge 1-13
lin ea r a lgebr a 7-63–7-85 m a t h em a t ica l fu n ct ion libr a r y 1-13
Lin u x over view 1-12
set t in g on lin e docu m en t a t ion pa t h 2-12 st a r t in g on Lin u x 2-13
l o a d 4-15 su ppor t ed Lin u x com piler s 2-8
loa din g a r r a ys 4-15 su ppor t ed P C com piler s 2-3
loca l va r ia bles 6-19 ma t l a b com m a n d
log of fu n ct ion s u sed 3-7 set t in g u p sym bolic lin k 2-12
logica l fu n ct ion s A-5 m a t r ices 4-18
logica l vect or s 4-25 cr ea t in g 4-14

I-5
Index

en t er in g 4-3 Not ebook 3-15


m a t r ix 4-2 n u l l 7-66
a n t idia gon a l 4-5 n u ll spa ce 7-66
det er m in a n t 4-19 n u m ber s 4-10
m a in dia gon a l 4-5 floa t in g-poin t 4-11
sin gu la r 4-19 n u m er ic sym bolic expr ession s 7-7
swa ppin g colu m n s 4-8
sym m et r ic 4-18
t r a n spose 4-4 O
m a t r ix con dit ion n u m ber 7-66 object fu n ct ion s A-18
m a t r ix fu n ct ion s - n u m er ica l lin ea r a lgebr a A-10– object pr oper t ies 5-31
A-11 object s
m a t r ix m u lt iplica t ion 4-18 fin din g h a n dles 5-33
m esh plot 5-18 gr a ph ics 5-28
me x com m a n d on lin e docu m en t a t ion
set t in g u p sym bolic lin k 2-12 specifyin g viewin g opt ion s on Lin u x 2-12
ME X-file 2-3, 2-8 on lin e h elp, viewin g 3-8
su ppor t ed P C com piler s 2-3 oper a t or 4-11, A-5
M-file 1-12, 4-15, 6-17 colon 4-7
cr ea t in g 6-17, 7-15 ou t pu t
for cr ea t in g a r r a ys 4-15 con t r ollin g for m a t 4-28
fu n ct ion 6-17, 6-18 su ppr essin g 4-30
scr ipt 6-17 over la yin g plot s 5-7
M-file per for m a n ce 3-15
M-files 3-14
Micr osoft Wor d a n d a ccess t o MATLAB 3-15 P
m ovies 5-38 pa t h 3-12
m u lt idim en sion a l a r r a y fu n ct ion s A-19 per for m a n ce of M-files 3-15
m u lt idim en sion a l a r r a ys 6-7 p l o t 5-2
m u lt iple da t a set s, plot t in g 5-3 plot edit in g m ode
m u lt iple plot s per figu r e 5-9 over view 5-15
m u lt iva r ia t e da t a , or ga n izin g 4-23 plot s
edit in g 5-14
plot t in g
N a ddin g legen d 5-3
n ewsgr ou p 1-10 a ddin g plot s 5-7
n ewsgr ou p for MATLAB u ser s 3-11 a n n ot a t in g 5-14

I-6
Index

ba sic 5-2 r e a l pr oper t y 7-9


com plex da t a 5-6 r ea l sym bolic va r ia bles 7-9, 7-26
com plex n u m ber s 5-6 r efer en ce in for m a t ion , obt a in in g 1-7
con t ou r s 5-8 r egist er in g you r soft wa r e 1-11
lin e color s 5-4 r evision con t r ol syst em s, in t er fa cin g t o MATLAB
lin e st yles 5-4 3-15
lin es a n d m a r ker s 5-5 r oot dir ect or y
m esh a n d su r fa ce 5-18 specifyin g 2-10
m u lt iple da t a set s 5-3 Rosser m a t r ix 7-70, 7-71
m u lt iple plot s 5-9 r u n n in g fu n ct ion s 3-6
p o l y 7-69 r u n n in g MATLAB 3-3
polyn om ia l a n d in t er pola t ion fu n ct ion s A-12–
A-13
P ost Scr ipt 5-26 S
pr ea lloca t ion 6-23 sca la r expa n sion 4-24
pr efer en ces 3-5 scien t ific n ot a t ion 4-10
p r e t t y 7-28 scr een s
p r i n t 5-26 in st a lla t ion da t a 2-12
pr in t in g r oot dir ect or y 2-10
gr a ph ics 5-26 scr ipt M-file 6-17
pr odu ct scr ipt s 6-17
r egist r a t ion 1-11 sea r ch pa t h 3-12
pr ofession a l ver sion sea r ch in g docu m en t a t ion 3-10
differ en ces wit h St u den t Ver sion 1-3 select in g pr odu ct s t o be in st a lled on Lin u x 2-11
pr ofiler 3-15 sem icolon t o su ppr ess ou t pu t 4-30
P r oper t y E dit or ser ia l por t I/O fu n ct ion s A-25–A-26
in t er fa ce 5-16 sh u t t in g down MATLAB 3-3
s i mp l e 7-48
sim plifica t ion s 7-43–7-50
Q s i mp l i f y 7-47
qu it t in g MATLAB 3-3 Sim u lin k 1-14
sim u lt a n eou s differ en t ia l equ a t ion s
solvin g 7-92
R sim u lt a n eou s lin ea r equ a t ion s
r a t ion a l a r it h m et ic 7-59 solvin g syst em s of 7-65, 7-89
r a t ion a l sym bolic expr ession s 7-7 sin gu la r m a t r ix 4-19
Re a d Me file 2-5 sin gu la r va lu e decom posit ion 7-76–7-78

I-7
Index

s o l v e 7-86 wit h logica l vect or s 4-25


solvin g equ a t ion s 7-86–7-93 su bscr ipt s 4-6
a lgebr a ic 7-86–7-90 su bst it u t ion s 7-51–7-57
or din a r y differ en t ia l 7-90–7-93 s u m 4-4
sou n d pr ocessin g fu n ct ion s A-15 su m m a t ion
sou r ce con t r ol syst em s, in t er fa cin g t o MATLAB sym bolic 7-27
3-15 su ppor t
spa r se m a t r ix fu n ct ion s A-14–A-15 sou r ces of 1-8
specia l ch a r a ct er s A-5 su ppr essin g ou t pu t 4-30
specia l con st a n t s 4-12 su r fa ce plot 5-18
in fin it y 4-12 s v d 7-76
n ot -a -n u m ber 4-12 s wi t c h 6-4
specia lized gr a ph s 5-8 s y m 7-5, 7-6, 7-7, 7-8, 7-26
specia lized m a t h fu n ct ion s A-9 sym bolic expr ession s 7-86
specia lized m a t r ices A-8 cr ea t in g 7-6
specifyin g viewin g opt ion s on Lin u x 2-12 decim a l 7-8
sph er ica l coor din a t es 7-18 floa t in g-poin t 7-7
st a r t in g MATLAB 3-3 n u m er ic 7-7
s t a r t u p . m file 2-5, 2-13 r a t ion a l 7-7
St a t eflow 1-14 sym bolic lin ks
st a t em en t s set t in g u p 2-12
con t in u in g on m u lt iple lin es 4-30 sym bolic m a t h fu n ct ion s
execu t in g 6-22 cr ea t in g 7-14
st r in gs Sym bolic Ma t h Toolbox
con ca t en a t in g 6-13 dem o 7-5
st r u ct u r e fu n ct ion s A-18 lea r n in g 1-7
st r u ct u r es 6-14 Sym bolic Ma t h Toolbox fu n ct ion s
St u den t Ver sion a r it h m et ic oper a t ion s B-3
differ en ces wit h pr ofession a l ver sion 1-3 ba sic oper a t ion s B-3
MATLAB differ en ces 1-3 ca lcu lu s B-3
Sim u lin k differ en ces 1-4 con ver sion s B-3
Sym bolic Ma t h Toolbox differ en ces 1-4 in t egr a l t r a n sfor m s B-3
s u b e x p r 7-51 lin ea r a lgebr a B-3
su bexpr ession s 7-51–7-53 peda gogica l a n d gr a ph ica l a pplica t ion s B-4
s u b p l o t 5-9 sim plifica t ion B-4
s u b s 7-53 solu t ion of equ a t ion s B-4
su bscr ipt in g specia l fu n ct ion s B-4

I-8
Index

va r ia ble pr ecision a r it h m et ic B-4 U


sym bolic m a t r ix u n r e a l pr oper t y 7-9
com pu t in g eigen va lu e of 7-71 u ser in t er fa ce 5-35
con ver t in g t o n u m er ic for m 7-8 bu ildin g 5-35
cr ea t in g 7-10
differ en t ia t in g 7-17
sym bolic object s V
a bou t 7-5 va r ia ble-pr ecision a r it h m et ic 7-58–7-62
sym bolic su m m a t ion 7-27 va r ia ble-pr ecision n u m ber s 7-60
sym bolic va r ia bles va r ia bles 4-10
com plex 7-9 globa l 6-20
cr ea t in g 7-6 loca l 6-19
defa u lt 7-11 vect or 4-2
r ea l 7-9, 7-26 logica l 4-25
sym m et r ic m a t r ix 4-18 pr ea lloca t in g 6-23
s y ms 7-7 vect or iza t ion 6-23
s y ms u m 7-27 ver sion con t r ol syst em s, in t er fa cin g t o MATLAB
syst em r equ ir em en t s 3-15
Lin u x 2-7 viewin g docu m en t a t ion 3-10
P C 2-2 visibilit y of a xes 5-11
v p a 7-60

T
t a y l o r 7-28 W
Ta ylor ser ies 7-28 Web sit e 1-10
t ech n ica l su ppor t 1-11 wh i l e 6-5
t ext 6-11 win dows for plot t in g 5-9
TIF F 5-27 win dows in MATLAB 3-4
t it le wir efr a m e 5-18
figu r e 5-12 su r fa ce 5-18
t oolbox 1-12 Wor d a n d a ccess t o MATLAB 3-15
t oolboxes wor d pr ocessin g a ccess t o MATLAB 3-15
in st a llin g on Lin u x 2-13 wor kspa ce 3-12
in st a llin g on P C 2-5 Wor kspa ce br owser 3-12
t ools in t h e deskt op 3-6 www. ma t h wo r k s . c o m 1-10
t r a n s p o s e 4-4 www. ma t h wo r k s . c o m/ e d u c a t i o n 1-10
t r ou blesh oot in g 1-8 www. ma t h wo r k s . c o m/ s t o r e 1-10, 2-5, 2-13

I-9
Index

www. ma t h wo r k s . c o m/ s u p p o r t 1-8, 1-11


www. ma t h wo r k s . c o m/ s u p p o r t / b o o k s 1-10

X
xor er a se m ode 5-37

I-10

You might also like