Matlab 6
Matlab 6
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
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
F in d in g Re fe re n c e In fo rm a tio n . . . . . . . . . . . . . . . . . . . . . . . . 1-7
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
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
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
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
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
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
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
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
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
F in d in g Re fe re n c e In fo rm a tio n . . . . . . . . . . . 1-7
1-2
About the Student Version
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.
1-4
Obtaining Additional MathW orks Products
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
W ha t I W a nt W ha t I Should Do
(Lin u x ) E n t er t h e ma t l a b com m a n d.
1-6
Finding Reference Information
W ha t I W a nt W ha t I Should Do
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.
1-7
1 Introduction
W ha t I W a nt W ha t I Should Do
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 .
1-8
Troubleshooting and Other Resources
Tutorialsandreferencefor
MATLAB
Tutorialsandreferencefor
Simulink
Tutorialsandreferencefor
Stateflow
Tutorialsandreferencefor
Symbolic Math Toolbox
1-9
1 Introduction
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.
1-10
Troubleshooting and Other Resources
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.
1-11
1 Introduction
• 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
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
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
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).
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.
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
2-4
Installing on W indows
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 .
2-5
2 Installation
2-6
Installing 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 .
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).
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.
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
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 .
2-8
Installing on Linux
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
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
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 .
2-10
Installing on Linux
2-11
2 Installation
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.
2-12
Installing on Linux
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-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 .
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
MATLAB D e s k to p . . . . . . . . . . . . . . . . . 3-4
D e s k to p To o ls . . . . . . . . . . . . . . . . . . . 3-6
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
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
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
3-5
3 Development Environment
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.
3-7
3 Development Environment
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.
Demos-double-clicktodisplaythedemo
launcher for the product.
3-8
Desktop Tools
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:
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.
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 :
3-10
Desktop Tools
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 .
Double-click
a variable to
see and
change its
contentsin
the Array
Editor.
3-12
Desktop Tools
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.
Set breakpoints
where you want
execution to pause
soyoucanexamine
variables.
Holdthecursorover
a variable and its
current value
appears(known as
a datatip).
3-14
Other Development Environment Features
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
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:
4-3
4 Getting Started
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 ns =
16
10
7
1
and
s u m( d i a g ( A) )
pr odu ces
a ns =
34
4-5
4 Getting Started
s u m( d i a g ( f l i p l r ( A) ) )
a ns =
34
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
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
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 )
4-7
4 Getting Started
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
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
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
^ P ower
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
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.
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
4-12
Expressions
e ps = 1. e - 6
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
z e r os All zer os
one s All on es
F = 5*one s ( 3, 3)
F =
5 5 5
5 5 5
5 5 5
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
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
4-15
4 Getting Started
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
a ns =
260 260 260 260 260 260 260 260
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
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
• 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
a ns =
32 8 11 17
8 20 17 23
11 17 14 26
17 23 26 2
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
d =
0
R =
1 0 0 1
0 1 0 -3
0 0 1 3
0 0 0 0
4-19
4 Getting Started
e =
34. 0000
8. 0000
0. 0000
- 8. 0000
v =
1
1
1
1
A* v
a ns =
34
34
34
34
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
are
1 - 34 - 64 2176 0
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
+ Addit ion
- Su bt r a ct ion
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
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
mu =
75. 8 161. 8 3. 48
s i g ma =
5. 6303 25. 499 2. 2107
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
s u m( B)
a ns =
0 0 0 0
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
4-25
4 Getting Started
A =
0 3 2 13
5 0 11 0
0 0 7 0
0 0 0 0
4-26
More About Matrices and Arrays
a ns =
5 3 2 11 7 13
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
• 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
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
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
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
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 ) ;
4-30
Controlling Command W indow Input and Output
← Ctrl+b Move ba ck on e ch a r a ct er
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
Ha n d le Gra p h ic s . . . . . . . . . . . . . . . . . . 5-28
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.
5-2
Basic Plotting
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π
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.
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 .
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.
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
0.8
0.6
0.4
0.2
−0.2
−0.4
−0.6
−0.8
−1
−1 −0.5 0 0.5 1
5-7
5 Graphics
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)
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.
5-9
5 Graphics
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
Use t h e com m a n d
a xi s a ut o
5-11
5 Graphics
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 .
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
0.8
0.6
0.4
0.2
sin(t)
−0.2
−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.
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:
5-14
Editing Plots
Use these toolbar buttonsto add text, arrows, and linesto a graph.
Click thisbuttontostart plot
edit mode.
Double-click onanobject to
select it.
Accessobject-specific plot
edit functionsthrough
context-sensitive pop-up
menus.
5-15
5 Graphics
Use these buttonsto move back and forth among the graphicsobjectsyou have edited.
Click onatabtoviewagroup
of properties.
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
5-17
5 Graphics
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
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
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
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.
5-21
5 Graphics
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
5-24
Images
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
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
5-27
5 Graphics
• 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.
O bject Description
5-28
Handle Graphics
O bject Description
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
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 .
Function Purpose
de l e t e Delet e a n object
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
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.
5-31
5 Graphics
5-32
Handle Graphics
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.
5-33
5 Graphics
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
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
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:
a n d a t em per a t u r e or velocit y, su ch a s
s = . 02
5-37
5 Graphics
0.8
0.6
0.4
0.2
−0.2
−0.4
−0.6
−0.8
−1
−1 −0.5 0 0.5 1
5-38
Animations
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
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
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
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
6-2
Flow Control
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
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
6-4
Flow Control
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
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
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.
6-7
6 Programming with MATLAB
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 )
and
s u m( M, 2 )
F in a lly,
S = s u m( M, 3 )
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
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
6-11
6 Programming with MATLAB
Th e st a t em en t
s = c ha r ( a )
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 ' ]
Th e st a t em en t
v = [ s ; ' wo r l d ' ]
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. ' )
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. '
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+'
6-14
Other Data Structures
S =
n a me : ' Ed Pl u m'
s c or e : 83
g r a d e : ' B+'
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
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
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
An d
c h a r ( S. n a me )
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:
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
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
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 ) ;
6-19
6 Programming with MATLAB
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) ' ) ;
as
f oo( ' a ' , ' b' , ' c ' )
6-20
Scripts and Functions
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
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
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.
6-23
6 Programming with MATLAB
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 ;
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:
6-24
Scripts and Functions
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
6-25
6 Programming with MATLAB
p = f mi n s e a r c h ( @h u mp s , . 5 )
p =
0. 6370
a ns =
11. 2528
6-26
Demonstration Programs Included with MATLAB
6-27
6 Programming with MATLAB
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
6-29
6 Programming with MATLAB
6-30
Demonstration Programs Included with MATLAB
6-31
6 Programming with MATLAB
6-32
Demonstration Programs Included with MATLAB
6-33
6 Programming with MATLAB
6-34
7
Ge ttin g He lp . . . . . . . . . . . . . . . . . . . . 7-4
Ca lc u lu s . . . . . . . . . . . . . . . . . . . . . . 7-16
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
Sim plifica t ion Met h ods of sim plifyin g a lgebr a ic expr ession 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
7-2
Introduction
Section Covers
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
7-4
Getting Started
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
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
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 t h e r a t ion a l for m
1/ 10
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
a ns =
. 10000000000000000555111512312578
a ns =
. 1000000
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]
or m or e efficien t ly
s y ms x y r e a l
z = x + i *y
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.
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
wh ich r et u r n
A =
[ a , b, c ]
[ b, c , a ]
[ c, a, b ]
7-10
Getting Started
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
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.
7-11
7 Symbolic Math Toolbox
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 )
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 ) ;
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
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
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
y * ( 4 +3 * i ) + 6 * j y
s qr t ( pi *a l pha ) a l pha
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
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 )
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
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 )
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 )
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
∂( 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
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
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)
lim f ( x )
x→a
1 1 1
lim --- , lim --- , a n d lim ---
x→0 x x → 0- x x → 0+ x
7-20
Calculus
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
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
Integra tion
If f is a sym bolic expr ession , t h en
i nt ( f )
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
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 )
1 / ( 1 +u ^ 2 ) a t a n( u)
and
i nt ( f , v, a , b)
b b
∫a f ( x ) d x a n d ∫a f ( v ) d v
r espect ively.
7-23
7 Symbolic Math Toolbox
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 )
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
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
–∞
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 )
wh ich r et u r n s
a ns =
s i g n u m( k ) / k * p i ^ ( 1 / 2 )
7-26
Calculus
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
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) ;
7-28
Calculus
Function
Taylor
6
1
1 1.2 1.4 1.6 1.8 2 2.2 2.4 2.6 2.8 3
x
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 ) )
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
7-30
Calculus
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 )
−1
−2
−3
−4
−5
−6 −4 −2 0 2 4 6
x
7-31
7 Symbolic Math Toolbox
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) )
7-32
Calculus
0
0+ 2. 4381i
0- 2. 4381i
2. 4483
- 2. 4483
Zeros of f3
−1
−2
−3
−6 −4 −2 0 2 4 6
x
7-33
7 Symbolic Math Toolbox
by m u lt iples of 2 π
z r = [ z r - 2 * p i z r z r +2 * p i ] ;
Zeros of f3
−1
−2
−3
−6 −4 −2 0 2 4 6
x
7-34
Calculus
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
Th e r esu lt
a ns =
[ 0, 0]
7-36
Calculus
Th e a ct u a l m in im a a r e m1 , m2
a ns =
[ - 4, - 4]
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
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 )
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
r et u r n s
g =
- 8 / ( t a n ( 1 / 2 * x ) ^ 2 +9 )
1 8
–––––––––––– + –––––––––––––––
5 + 4 c os ( x) 2
t a n( 1/ 2 x) + 9
Th e r esu lt
F =
2/ 3*a t a n( 1/ 3*t a n( 1/ 2*x) )
7-39
7 Symbolic Math Toolbox
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
7-40
Calculus
atan(tan(x))
1.5
0.5
−0.5
−1
−1.5
−6 −4 −2 0 2 4 6
x
a n d plot t h e r esu lt .
e z p l o t ( F1 , [ - 6 . 2 8 , 6 . 2 8 ] )
7-41
7 Symbolic Math Toolbox
1.5
0.5
−0.5
−1
−1.5
−2
−2.5
−6 −4 −2 0 2 4 6
x
7-42
Simplifications and Substitutions
3 2
x - 6 x + 11 x - 6
( x - 1) ( x - 2) ( x - 3)
x ( x ( x - 6) + 11) - 6
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
collect
Th e st a t em en t
c ol l e c t ( f )
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 )
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 )
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 )
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
7-46
Simplifications and Substitutions
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 )
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
r et u r n s
f =
1
7-49
7 Symbolic Math Toolbox
r et u r n s
f =
1
how =
c o mb i n e
( 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)
r et u r n
2 +1 / a
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 )
7-51
7 Symbolic Math Toolbox
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)
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 ) ) ]
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 ]
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]
E =
[ S, 0, 0]
[ 0, - S, 0]
[ 0, 0 , b +c +a ]
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
a ns =
8
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
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
7-57
7 Symbolic Math Toolbox
u se va r ia ble-pr ecision a r it h m et ic t o r et u r n
. 8333333333333333333333333
7-58
Variable-Precision Arithmetic
7-59
7 Symbolic Math Toolbox
r et u r n s a m a t r ix
E =
2. 71828182845905 1. 41421356237310
1. 09861228866811 0. 21895918632809
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 )
r et u r n s
262537412640768744.
wh er ea s
vpa ( f , 25)
r et u r n s
262537412640768744. 0000000
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
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 ) ]
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)
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 n d t h en
I = s i mp l e ( I )
I =
[ 1, 0]
[ 0, 1]
7-64
Linear Algebra
gives
[ 1, 1/ 2, 1/ 3]
[ 1/ 2, 1/ 3, 1/ 4]
[ 1/ 3, 1/ 4, 1/ 5]
pr odu ces
[ 9, - 36, 30]
[ - 36, 192, - 180]
[ 30, - 180, 180]
and
d e t ( H)
yields
1/ 2160
7-65
7 Symbolic Math Toolbox
r et u r n s
[ 1. , . 5000000000000000, . 3333333333333333]
[ . 5000000000000000, . 3333333333333333, . 2500000000000000]
[ . 3333333333333333, . 2500000000000000, . 2000000000000000]
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]
wh ich gives
. 462962962962958e - 3
and
V\ b
wh ich is
[ 3. 000000000000041]
[ - 24. 00000000000021]
[ 30. 00000000000019]
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
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 )
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
C =
[ 0, 1]
[ 1, 0]
[ 6/ 5, - 3/ 10]
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) )
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)
7-68
Linear Algebra
[ 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) ]
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)
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)
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]
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
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]
A = 0 1
–1 0
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) ]
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
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]
( A – λ 2 I )v 4 = v 3
( A – λ 1 I )v 2 = v 1
7-75
7 Symbolic Math Toolbox
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]
7-76
Linear Algebra
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
7-78
Linear Algebra
0.3 λ’(1)
0.2
0.1
−0.1
−0.2
−0.3 λ’(2)
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
pr odu ce
e =
[ 1, 2, 3]
p =
x ^ 3 - 6 * x ^ 2 +1 1 * x - 6
f =
( x- 1) *( x- 2) *( x- 3)
7-80
Linear Algebra
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
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) )
7-81
7 Symbolic Math Toolbox
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
7-82
Linear Algebra
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
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 ' )
−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
λ
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) )
e =
1. 5476
1. 5476
2. 9047
7-85
7 Symbolic Math Toolbox
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 ' )
7-86
Solving Equations
s =
[ 0]
[ pi ]
[ 1/ 6*pi ]
[ 5/ 6*pi ]
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
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]
7-87
7 Symbolic Math Toolbox
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) ]
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
pr odu ces
a ns =
[ - 1]
[ 3]
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]
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]
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
Th is pr odu ces
y =
t a n ( t +1 / 4 * p i )
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
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 ) .
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 .
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
1
y ( 0 ) = 0 , y ( 3 ) = --- K 1 ( 2 3 )
π ---
3
7-93
7 Symbolic Math Toolbox
7-94
A
MATLAB Qu ick Refer en ce
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 :
A-2
General Purpose Commands
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
A-5
A MATLAB Quick Reference
A-6
Elementary Matrices and Matrix Manipulation
A-7
A MATLAB Quick Reference
A-8
Specialized Math Functions
A-9
A MATLAB Quick Reference
A-10
Data Analysis and Fourier Transform Functions
A-11
A MATLAB Quick Reference
A-12
Function Functions - N onlinear N umerical Methods
A-13
A MATLAB Quick Reference
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
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
A-17
A MATLAB Quick Reference
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
A-19
A MATLAB Quick Reference
A-20
Data Visualization
A-21
A MATLAB Quick Reference
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
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
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
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
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
S p e c ia l F u n c tio n s . . . . . . . . . . . . . . . . . B-4
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 .
B -2
Symbolic Math Toolbox Quick Reference
B -3
B Symbolic Math Toolbox Quick Reference
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
I-2
Index
I-3
Index
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
I-5
Index
I-6
Index
I-7
Index
I-8
Index
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
X
xor er a se m ode 5-37
I-10