MATLAB Student
MATLAB Student
STUDEN T VERSION
How to Conta ct The M a thW orks:
www. mat hwor ks . com Web
f t p. mat hwor ks . com Anonymous FTP server
comp. s of t - s y s . mat l ab Newsgroup
s uppor t @mat hwor ks . c om Techni cal support
s ugge s t @mat hwor ks . c om Product enhancement suggesti ons
bugs @mat hwor ks . c om Bug reports
doc @mat hwor ks . com Documentati on error reports
s ubs c r i be @mat hwor ks . com Subscri bi ng user regi strati on
s e r v i c e@mat hwor ks . c om Order status, l i cense renewal s, passcodes
i nf o@mat hwor ks . c om Sal es, pri ci ng, and general i nformati on
508-647-7000 Phone
508-647-7001 Fax
The MathWorks, I nc. Mai l
3 Appl e Hi l l Dri ve
Nati ck, MA 01760-2098
I SBN 0-9672195-1-5
LearningMATLAB
COPYRI GHT 1999 by The MathWorks, I nc.
The softwar e descr i bed i n thi s document i s fur ni shed under a l i cense agreement. The software may be used
or copi ed onl y under the terms of the l i cense agr eement. No par t of thi s manual may be photocopi ed or r epr o-
duced i n any for m wi thout pr i or wr i tten consent from The MathWor ks, I nc.
FEDERAL ACQUI SI TI ON: Thi s provi si on appl i es to al l acqui si ti ons of the Progr am and Documentati on by
or for the feder al gover nment of the Uni ted States. By accepti ng del i very of the Pr ogram, the government
her eby agr ees that thi s softwar e qual i fi es as "commerci al "computer softwar e wi thi n the meani ng of FAR
Par t 12.212, DFARS Par t 227.7202-1, DFARS Part 227.7202-3, DFARS Par t 252.227-7013, and DFARS Par t
252.227-7014. The ter ms and condi ti ons of The MathWorks, I nc. Softwar e Li cense Agr eement shal l pertai n
to the gover nments use and di scl osur e of the Pr ogram and Documentati on, and shal l super sede any
confl i cti ng contractual ter ms or condi ti ons. I f thi s l i cense fai l s to meet the gover nments mi ni mum needs or
i s i nconsi stent i n any r espect wi th federal pr ocur ement l aw, the gover nment agr ees to r eturn the Progr am
and Documentati on, unused, to MathWor ks.
MATLAB, Si mul i nk, Statefl ow, Handl e Gr aphi cs, and Real -Ti me Wor kshop ar e r egi stered trademar ks, and
Tar get Language Compi l er i s a tr ademark of The MathWor ks, I nc.
Other pr oduct or br and names are tr ademar ks or r egi stered tr ademar ks of thei r respecti ve hol der s.
Pri nti ng Hi story: August 1999 Fi r st pri nti ng New manual
IN TER N ET
@
E-M A IL
PH O N E
FA X
u
M A IL
i
Contents
1
Introduction
About theStudent Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2
Student Use Pol i cy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2
Di fferences Between the Student Versi on
and the Professi onal Versi on . . . . . . . . . . . . . . . . . . . . . . . . . . 1-3
ObtainingAdditional MathWorks Products . . . . . . . . . . . . . 1-5
Patches and Updates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-5
GettingStarted with MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . 1-6
FindingReferenceInformation . . . . . . . . . . . . . . . . . . . . . . . . 1-7
Troubleshootingand Other Resources . . . . . . . . . . . . . . . . . . 1-8
Documentati on Li brary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-8
Accessi ng the Onl i ne Documentati on . . . . . . . . . . . . . . . . . . 1-8
Usenet Newsgroup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-9
MathWorks Web Si te . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-9
MathWorks Educati on Web Si te . . . . . . . . . . . . . . . . . . . . . . . . 1-9
MATLAB Rel ated Books . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-9
MathWorks Store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-9
MathWorks Knowl edge Base . . . . . . . . . . . . . . . . . . . . . . . . . . 1-10
Techni cal Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-10
Student Versi on Support Pol i cy . . . . . . . . . . . . . . . . . . . . . . 1-10
Product Regi strati on . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-10
About MATLAB and Simulink . . . . . . . . . . . . . . . . . . . . . . . . . 1-11
What I s MATLAB? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-11
Tool boxes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-11
The MATLAB System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-12
What I s Si mul i nk? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-13
TheMATLAB Product Family . . . . . . . . . . . . . . . . . . . . . . . . . 1-14
ii Contents
2
Installation
Installingon a PC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2
System Requi rements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2
MATLAB and Si mul i nk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2
MEX-Fi l es . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3
I nstal l i ng MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-4
I nstal l i ng Addi ti onal Tool boxes . . . . . . . . . . . . . . . . . . . . . . . . . . 2-5
Accessi ng the Onl i ne Documentati on (Hel p Desk) . . . . . . . . . . 2-5
Installingon Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7
System Requi rements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7
MATLAB and Si mul i nk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7
MEX-Fi l es . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7
I nstal l i ng MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-8
I nstal l i ng the Software . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-8
Post I nstal l ati on Procedures . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-13
Successful I nstal l ati on . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-13
Unsuccessful I nstal l ati on . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-13
I nstal l i ng Addi ti onal Tool boxes . . . . . . . . . . . . . . . . . . . . . . . . . 2-14
Accessi ng the Onl i ne Documentati on (Hel p Desk) . . . . . . . . . 2-14
3
GettingStarted
StartingMATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-2
Matrices and Magic Squares . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-3
Enteri ng Matri ces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-4
sum, transpose, and di ag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-5
Subscri pts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-7
The Col on Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-8
The magi c Functi on . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-9
Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-11
Vari abl es . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-11
iii
Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-11
Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-12
Functi ons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-12
Expressi ons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-14
Workingwith Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-15
Generati ng Matri ces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-15
The l oad Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-16
M-Fi l es . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-16
Concatenati on . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-17
Del eti ng Rows and Col umns . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-18
MoreAbout Matrices and Arrays . . . . . . . . . . . . . . . . . . . . . . 3-19
Li near Al gebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-19
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-22
Mul ti vari ate Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-24
Scal ar Expansi on . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-25
Logi cal Subscri pti ng . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-26
The fi nd Functi on . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-27
TheCommand Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-29
The format Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-29
Suppressi ng Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-30
Long Command Li nes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-31
Command Li ne Edi ti ng . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-31
TheMATLAB Environment . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-33
The Workspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-33
save Commands . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-34
The Search Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-34
Di sk Fi l e Mani pul ati on . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-35
The di ary Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-35
Runni ng External Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-35
Help and OnlineDocumentation . . . . . . . . . . . . . . . . . . . . . . 3-37
The Hel p Desk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-37
Usi ng the Hel p Desk . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-38
Onl i ne Reference Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-38
The doc Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-38
iv Contents
Pri nti ng Onl i ne Reference Pages . . . . . . . . . . . . . . . . . . . . . 3-39
The hel p Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-39
The l ookfor Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-40
Li nk to the MathWorks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-41
4
Graphics
Basic Plotting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2
Creati ng a Pl ot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2
Mul ti pl e Data Sets i n One Graph . . . . . . . . . . . . . . . . . . . . . . 4-3
Speci fyi ng Li ne Styl es and Col ors . . . . . . . . . . . . . . . . . . . . . 4-4
Pl otti ng Li nes and Markers . . . . . . . . . . . . . . . . . . . . . . . . . . 4-5
I magi nary and Compl ex Data . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-6
Addi ng Pl ots to an Exi sti ng Graph . . . . . . . . . . . . . . . . . . . . . . . 4-7
Fi gure Wi ndows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-9
Mul ti pl e Pl ots i n One Fi gure . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-9
Control l i ng the Axes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-11
Setti ng Axi s Li mi ts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-11
Setti ng Axi s Aspect Rati o . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-11
Setti ng Axi s Vi si bi l i ty . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-12
Setti ng Gri d Li nes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-12
Axi s Label s and Ti tl es . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-12
Annotati ng Pl ots Usi ng the Pl ot Edi tor . . . . . . . . . . . . . . . . . . 4-13
Mesh and SurfacePlots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-15
Vi sual i zi ng Functi ons of Two Vari abl es . . . . . . . . . . . . . . . . . . 4-15
Exampl e Graphi ng the si nc Functi on . . . . . . . . . . . . . . . . 4-15
Exampl e Col ored Surface Pl ots . . . . . . . . . . . . . . . . . . . . . 4-16
Surface Pl ots wi th Li ghti ng . . . . . . . . . . . . . . . . . . . . . . . . . 4-17
Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-19
PrintingGraphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-21
Pri nti ng from the Menu . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-21
Exporti ng Fi gure to Graphi cs Fi l es . . . . . . . . . . . . . . . . . . . 4-21
Usi ng the Pri nt Command . . . . . . . . . . . . . . . . . . . . . . . . . . 4-21
v
HandleGraphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-23
Graphi cs Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-23
Object Hi erarchy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-24
Creati ng Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-25
Commands for Worki ng wi th Objects . . . . . . . . . . . . . . . . . . 4-25
Setti ng Object Properti es . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-26
Setti ng Properti es from Pl otti ng Commands . . . . . . . . . . . . 4-26
Setti ng Properti es of Exi sti ng Objects . . . . . . . . . . . . . . . . . 4-26
Setti ng Mul ti pl e Property Val ues . . . . . . . . . . . . . . . . . . . . . 4-27
Fi ndi ng the Handl es of Exi sti ng Objects . . . . . . . . . . . . . . . . . 4-28
Fi ndi ng Al l Objects of a Certai n Type . . . . . . . . . . . . . . . . . 4-29
Fi ndi ng Objects wi th a Parti cul ar Property . . . . . . . . . . . . . 4-29
Li mi ti ng the Scope of the Search . . . . . . . . . . . . . . . . . . . . . 4-29
Usi ng fi ndobj as an Argument . . . . . . . . . . . . . . . . . . . . . . . 4-29
Graphics User Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-30
Graphi cal User I nterface Desi gn Tool s . . . . . . . . . . . . . . . . . . . 4-30
Animations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-31
Erase Mode Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-31
Creati ng Movi es . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-32
5
Programmingwith MATLAB
FlowControl . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-2
i f . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-2
swi tch and case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-4
for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-4
whi l e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-5
break . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-5
Other Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-7
Mul ti di mensi onal Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-7
Cel l Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-9
Characters and Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-11
Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-14
vi Contents
Scripts and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-17
Scri pts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-17
Functi ons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-19
Gl obal Vari abl es . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-20
Passi ng Stri ng Arguments to Functi ons . . . . . . . . . . . . . . . . . . 5-21
Bui l di ng Stri ngs on the Fl y . . . . . . . . . . . . . . . . . . . . . . . . . . 5-21
A Cauti onary Note . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-22
The eval Functi on . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-22
Vectori zati on . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-23
Preal l ocati on . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-23
Functi on Functi ons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-24
Demonstration Programs Included with MATLAB . . . . . . 5-27
6
Symbolic Math Toolbox
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2
GettingHelp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-4
GettingStarted . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-5
Symbol i c Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-5
Creati ng Symbol i c Vari abl es and Expressi ons . . . . . . . . . . . . . . 6-6
Symbol i c and Numeri c Conversi ons . . . . . . . . . . . . . . . . . . . . . . 6-7
Constructi ng Real and Compl ex Vari abl es . . . . . . . . . . . . . . 6-9
Creati ng Abstract Functi ons . . . . . . . . . . . . . . . . . . . . . . . . . 6-10
Exampl e: Creati ng a Symbol i c Matri x . . . . . . . . . . . . . . . . . 6-10
The Defaul t Symbol i c Vari abl e . . . . . . . . . . . . . . . . . . . . . . . 6-12
Creati ng Symbol i c Math Functi ons . . . . . . . . . . . . . . . . . . . . . 6-14
Usi ng Symbol i c Expressi ons . . . . . . . . . . . . . . . . . . . . . . . . . 6-14
Creati ng an M-Fi l e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-15
Calculus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-16
Di fferenti ati on . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-16
Li mi ts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-20
I ntegrati on . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-22
vii
I ntegrati on wi th Real Constants . . . . . . . . . . . . . . . . . . . . . 6-24
Real Vari abl es vi a sym . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-26
Symbol i c Summati on . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-27
Tayl or Seri es . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-28
Extended Cal cul us Exampl e . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-30
Simplifications and Substitutions . . . . . . . . . . . . . . . . . . . . . 6-44
Si mpl i fi cati ons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-44
col l ect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-45
expand . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-46
horner . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-46
factor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-47
si mpl i fy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-49
si mpl e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-49
Substi tuti ons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-53
subexpr . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-53
subs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-56
Variable-Precision Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . 6-61
Overvi ew . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-61
Exampl e: Usi ng the Di fferent Ki nds of Ari thmeti c . . . . . . . . . 6-62
Rati onal Ari thmeti c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-62
Vari abl e-Preci si on Numbers . . . . . . . . . . . . . . . . . . . . . . . . . 6-63
Converti ng to Fl oati ng-Poi nt . . . . . . . . . . . . . . . . . . . . . . . . 6-64
Another Exampl e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-64
Linear Algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-66
Basi c Al gebrai c Operati ons . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-66
Li near Al gebrai c Operati ons . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-67
Ei genval ues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-71
Jordan Canoni cal Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-77
Si ngul ar Val ue Decomposi ti on . . . . . . . . . . . . . . . . . . . . . . . . . 6-79
Ei genval ue Trajectori es . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-81
SolvingEquations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-89
Sol vi ng Al gebrai c Equati ons . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-89
Several Al gebrai c Equati ons . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-90
Si ngl e Di fferenti al Equati on . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-93
Exampl e 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-94
Exampl e 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-94
viii Contents
Exampl e 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-94
Several Di fferenti al Equati ons . . . . . . . . . . . . . . . . . . . . . . . . . 6-95
A
MATLAB Quick Reference
B
Symbolic Math Toolbox Quick Reference
1
I ntroducti on
About theStudent Version . . . . . . . . . . . . . 1-2
ObtainingAdditional MathWorks Products . . . . . . 1-5
GettingStarted with MATLAB . . . . . . . . . . . . 1-6
FindingReferenceInformation . . . . . . . . . . . 1-7
Troubleshootingand Other Resources . . . . . . . . 1-8
About MATLAB and Simulink . . . . . . . . . . . . 1-11
TheMATLAB Product Family . . . . . . . . . . . . 1-14
1 Introduction
1-2
About the Student Version
The Student Versi on of MATLAB
& Si mul i nk
Blocksets
DSP
Fi xed-Poi nt
Nonl i near Control Desi gn
Power System
Contact The MathWorks or vi si t www. mat hwor ks . comfor an up-to-date product l i st.
Thi s fi gure shows how the MathWorks products fi t together.
The MATLAB Product Family
2
I nstal l ati on
Installingon a PC . . . . . . . . . . . . . . . . . 2-2
System Requi rements . . . . . . . . . . . . . . . . . 2-2
I nstal l i ng MATLAB . . . . . . . . . . . . . . . . . 2-4
I nstal l i ng Addi ti onal Tool boxes . . . . . . . . . . . . . 2-5
Accessi ng the Onl i ne Documentati on (Hel p Desk) . . . . . 2-5
Installingon Linux . . . . . . . . . . . . . . . . . 2-7
System Requi rements . . . . . . . . . . . . . . . . . 2-7
I nstal l i ng MATLAB . . . . . . . . . . . . . . . . . 2-8
Post I nstal l ati on Procedures . . . . . . . . . . . . . . 2-13
I nstal l i ng Addi ti onal Tool boxes . . . . . . . . . . . . . 2-14
Accessi ng the Onl i ne Documentati on (Hel p Desk) . . . . . 2-14
2 Installation
2-2
Installing on a PC
System Requirements
Note For the most up-to-date i nformati on about system requi rements, see
the system requi rements page, avai l abl e i n the Products area at the
MathWorks Web si te (www. mat hwor ks . com).
M ATLAB a nd Simulink
I ntel -based Penti um, Penti um Pro, or Penti um I I personal computer
Mi crosoft Wi ndows 95, Wi ndows 98, or Wi ndows NT 4.0 (wi th Servi ce
Pack 3)
CD-ROM dri ve (for i nstal l ati on and onl i ne documentati on)
16 MB RAM mi ni mum for Wi ndows 95 and 98; 16 MB RAM mi ni mum for
Wi ndows NT, 24 MB (or more) strongl y recommended
Di sk space vari es dependi ng on si ze of parti ti on. The MATLAB i nstal l er wi l l
i nform you of the hard di sk space requi rement for your parti cul ar parti ti on.
For exampl e:
- Parti ti on wi th a 512-byte cl uster si ze requi res 53 MB for MATLAB and
Si mul i nk
- Parti ti on wi th a 64K-byte cl uster si ze requi res 223 MB for MATLAB and
Si mul i nk
8-bi t graphi cs adapter and di spl ay (for 256 si mul taneous col ors)
Other recommended i tems i ncl ude:
Mi crosoft Wi ndows supported graphi cs accel erator card
Mi crosoft Wi ndows supported pri nter
Mi crosoft Wi ndows supported sound card
Netscape Navi gator 3.0 or hi gher or Mi crosoft I nternet Expl orer 4.0
(to vi ew the onl i ne documentati on)
Mi crosoft Word 7.0 (Offi ce 95), or 8.0 (Offi ce 97) (to run the MATLAB
Notebook)
Installing on a PC
2-3
Adobe Acrobat Reader i s requi red to vi ew and pri nt the MATLAB onl i ne
documentati on that i s i n PDF format. Adobe Acrobat Reader i s avai l abl e on the
MATLAB CD.
M EX- Files
MEX-fi l es are dynami cal l y l i nked subrouti nes that MATLAB can
automati cal l y l oad and execute. They provi de a mechani sm by whi ch you can
cal l your own C and Fortran subrouti nes from MATLAB as i f they were bui l t-i n
functi ons.
For More Information The Application ProgramI nterfaceGuidedescri bes
how to wri te MEX-fi l es and the Application ProgramI nterfaceReference
descri bes the col l ecti on of API functi ons. Both of these are avai l abl e from the
Hel p Desk.
I f you pl an to bui l d your own MEX-fi l es, one of the fol l owi ng i s requi red:
DEC Vi sual Fortran versi on 5.0 or 6.0
Mi crosoft Vi sual C/C++ versi on 4.2, 5.0, or 6.0
Borl and C++ versi on 5.0, 5.2, or 5.3
Watcom C/C++ versi on 10.6 or 11
Note For an up-to-date l i st of al l the compi l ers supported by MATLAB, see
the MathWorks Techni cal Support Departments Techni cal Notes at
www. mat hwor ks . com/s uppor t /t e ch- not es /#mex .
2 Installation
2-4
Installing MATLAB
Thi s l i st summari zes the steps i n the standard i nstal l ati on procedure. You can
perform the i nstal l ati on by si mpl y fol l owi ng the i nstructi ons i n the di al og
boxes presented by the i nstal l ati on program; i t wal ks you through thi s process.
1 Stop any vi rus protecti on software you have runni ng.
2 I nsert the MathWorks CD i nto your CD-ROM dri ve. The i nstal l ati on
program starts automati cal l y when the CD-ROM dri ve i s ready. You can
al so run s et up. ex e from the CD. Vi ew the Welcomescreen.
3 Revi ew the Student Use Pol i cy.
4 Revi ew the Software Li cense Agr eement.
5 Enter your name and school name.
6 To i nstal l the compl ete set of software (MATLAB, Si mul i nk, and the
Symbol i c Math Tool box), make sure al l of the components are sel ected i n the
Select MATLAB Components di al og box.
7 Speci fy the desti nati on di rectory, that i s, the di rectory where you want to
save the fi l es on your hard dri ve. To change di rectori es, use the Browse
button.
8 When the i nstal l ati on i s compl ete, veri fy the i nstal l ati on by starti ng
MATLAB and runni ng one of the demo programs.
9 Customi ze any MATLAB envi ronment opti ons, i f desi red. For exampl e, to
i ncl ude defaul t defi ni ti ons or any MATLAB expressi ons that you want
executed every ti me MATLAB i s i nvoked, create a fi l e named s t ar t up. mi n
the $MATLAB\t ool box \l ocal di rectory. MATLAB executes thi s fi l e each
ti me MATLAB i s i nvoked.
10 Perform any addi ti onal necessary confi gurati on by typi ng the appropri ate
command at the MATLAB command prompt. For exampl e, to confi gure the
MATLAB Notebook, type not ebook - s et up. To confi gure a compi l er to work
wi th the MATLAB Appl i cati on Program I nterface, type mex - s et up.
Installing on a PC
2-5
For More Information The MATLAB I nstallation Guidefor PC provi des
addi ti onal i nstal l ati on i nformati on. Thi s manual i s avai l abl e i n PDF form
from OnlineManuals on the Hel p Desk.
Installing Additional Toolboxes
To purchase addi ti onal tool boxes, vi si t the MathWorks Store at
(www. mat hwor ks . com/s t or e). Once you pur chase a tool box, i t i s downl oaded to
your computer.
When you downl oad a tool box, you recei ve an i nstal l ati on program for the
tool box. To i nstal l the tool box, run the i nstal l ati on program by doubl e-cl i cki ng
on i ts i con. After you successful l y i nstal l the tool box, al l of i ts functi onal i ty wi l l
be avai l abl e to you when you start MATLAB.
Note Some tool boxes have Re adMe fi l es associ ated wi th them. When you
downl oad the tool box, check to see i f there i s a ReadMe fi l e. These fi l es contai n
i mportant i nformati on about the tool box and possi bl y i nstal l ati on and
confi gurati on notes. To vi ew the ReadMe fi l e for a tool box, use the what s new
command.
Accessing the Online Documentation (Help Desk)
Access the onl i ne documentati on (Hel p Desk) di rectl y from your product CD:
1 Pl ace the CD i n your CD-ROM dri ve.
2 Sel ect Documentation (Help Desk) from the Help menu i n the MATLAB
command wi ndow. You can al so type he l pdes k at the MATLAB prompt.
2 Installation
2-6
The Hel p Desk, si mi l ar to thi s fi gure, appears i n your Web browser.
Installing on Linux
2-7
Installing on Linux
System Requirements
Note For the most up-to-date i nformati on about system requi rements, see
the system requi rements page, avai l abl e i n the products area at the
MathWorks Web si te (www. mat hwor ks . com).
M ATLAB a nd Simulink
I ntel -based Penti um, Penti um Pro, or Penti um I I personal computer
Li nux 2.0.34 kernel (Red Hat 4.2, 5.1, Debi an 2.0)
X Wi ndows (X11R6)
60 MB free di sk space for MATLAB & Si mul i nk
64 MB memory, addi ti onal memory strongl y recommended
64 MB swap space (recommended)
CD-ROM dri ve (for i nstal l ati on and onl i ne documentati on)
8-bi t gr aphi cs adapter and di spl ay (for 256 si mul taneous col or s)
Netscape Navi gator 3.0 or hi gher (to vi ew the onl i ne documentati on)
Adobe Acrobat Reader i s requi red to vi ew and pri nt the MATLAB onl i ne
documentati on that i s i n PDF format. Adobe Acrobat Reader i s avai l abl e on the
MATLAB CD.
M EX- Files
MEX-fi l es are dynami cal l y l i nked subrouti nes that MATLAB can
automati cal l y l oad and execute. They provi de a mechani sm by whi ch you can
cal l your own C and Fortran subrouti nes from MATLAB as i f they were bui l t-i n
functi ons.
2 Installation
2-8
For More Information The Application ProgramI nterfaceGuidedescri bes
how to wri te MEX-fi l es and the Application ProgramI nterfaceReference
descri bes the col l ecti on of API functi ons. Both of these are avai l abl e from the
Hel p Desk.
I f you pl an to bui l d your own MEX-fi l es, you need an ANSI C C compi l er (e.g.,
the GNU C compi l er, gcc).
Note For an up-to-date l i st of al l the compi l ers supported by MATLAB, see
the MathWorks Techni cal Support Departments Techni cal Notes at
www. mat hwor ks . com/s uppor t /t ech- not es /#mex .
Installing MATLAB
The fol l owi ng i nstructi ons descri be how to i nstal l the Student Versi on of
MATLAB & Si mul i nk on your computer.
Note I t i s recommended that you l og i n as r oot to perform your i nstal l ati on.
Insta lling the Softw a re
To i nstal l the Student Versi on:
1 I f your CD-ROM dri ve i s not accessi bl e to your operati ng system, you wi l l
need to create a di rectory to be the mount poi nt for i t.
mkdi r /c dr om
2 Pl ace the CD i nto the CD-ROM dri ve.
Installing on Linux
2-9
3 Execute the command to mount the CD-ROM dri ve on your system. For
exampl e,
# mount - t i s o9660 /dev /c dr om /cdr om
shoul d work on most systems. I f your /e t c/f s t ab fi l e has a l i ne si mi l ar to
/de v /cdr om /cdr om i s o9660 noaut o, r o, us er , ex ec 0 0
then nonroot users can mount the CD-ROM usi ng the si mpl i fi ed command
$ mount /cdr om
Note I f the ex ec opti on i s mi ssi ng (as i t often i s by defaul t, for securi ty
reasons), you wi l l recei ve a Permi ssi on deni ed error when attempti ng to run
the i nstal l scri pt. To remedy thi s, ei ther use the ful l mount command shown
above (as r oot ) or add the ex ec opti on to the fi l e /et c/f s t ab.
4 Move to the i nstal l ati on l ocati on usi ng the c d command. For exampl e, i f you
are goi ng to i nstal l i nto the l ocati on /us r /l ocal /mat l ab5, use the
commands
cd /us r /l ocal
mkdi r mat l ab5
cd mat l ab5
Subsequent i nstructi ons i n thi s secti on refer to thi s di rectory as $MATLAB.
5 Copy the l i cense fi l e, l i cens e. dat , from the CD to $MATLAB.
6 Run the CD i nstal l scri pt.
/cdr om/i ns t al l _l nx 86. s h
The wel come screen appears. Sel ect OK to proceed wi th the i nstal l ati on.
Note I f you need addi ti onal hel p on any step duri ng thi s i nstal l ati on process,
cl i ck the Help button at the bottom of the di al og box.
2 Installation
2-10
7 Accept or reject the software l i censi ng agreement di spl ayed. I f you accept
the terms of the agreement, you may proceed wi th the i nstal l ati on.
8 The MATLAB Root Directory screen i s di spl ayed. Sel ect OK i f the
pathname for the MATLAB root di rectory i s correct; otherwi se, change i t to
the desi red l ocati on.
9 The system di spl ays your l i cense fi l e. Press OK.
Installing on Linux
2-11
10 The i nstal l ati on program di spl ays the Product Installation Options
screen, whi ch i s si mi l ar to thi s.
The products you are l i censed to i nstal l are l i sted i n the Itemstoinstall l i st
box. The ri ght l i st box di spl ays the products that you do not want to i nstal l .
To i nstal l the compl ete Student Versi on of MATLAB & Si mul i nk, you must
i nstal l al l the products for whi ch you are l i censed (MATLAB, MATLAB
Tool box, MATLAB Kernel , Si mul i nk, Symbol i c Math, Symbol i c Math
Li brary, and GhostScri pt). Sel ect OK.
2 Installation
2-12
11 The i nstal l ati on program di spl ays the Installation Data screen.
Speci fy the di rectory l ocati on i n your fi l e system for symbol i c l i nks to the
mat l ab, mat l abdoc, and mex scri pts. Choose a di rectory such as
/us r /l ocal /bi n. You must be l ogged i n as r oot to do thi s.
I n the MATLAB Li cense No. fi el d, enter s t udent .
Sel ect OK to conti nue.
12 The Begin Installation screen i s di spl ayed. Sel ect OK to start the
i nstal l ati on. After the i nstal l ati on i s compl ete, the Installation Complete
screen i s di spl ayed, assumi ng your i nstal l ati on i s successful . Sel ect Exit to
exi t from the setup program.
13 I f desi red, customi ze any MATLAB envi ronment opti ons. For exampl e, to
i ncl ude defaul t defi ni ti ons or any MATLAB expressi ons that you want
executed every ti me MATLAB i s i nvoked, create a fi l e named s t ar t up. mi n
the $MATLAB/t ool box /l ocal di rectory. MATLAB executes thi s fi l e each
ti me MATLAB i s i nvoked.
14 You must edi t the docopt . mM-fi l e l ocated i n the $MATLAB/t ool box /l ocal
di rectory to speci fy the path to the onl i ne documentati on (Hel p Desk). For
exampl e, i f /cdr omi s the path to your CD-ROM dri ve, then you woul d use
Installing on Linux
2-13
/cdr om/he l p. To set the path usi ng thi s exampl e, change the l i nes i n the
i f i s uni x bl ock i n the docopt . mfi l e to
i f i s uni x % UNI X
% doccmd = ' ' ;
% opt i ons = ' ' ;
docpat h = ' /cdr om/hel p' ;
The docopt . mfi l e al so al l ows you to speci fy an al ternati ve Web browser or
addi ti onal i ni ti al browser opti ons. I t i s confi gured for Netscape Navi gator.
15 Start MATLAB by enteri ng the mat l ab command. I f you di d not set up
symbol i c l i nks i n a di rectory on your path, type $MATLAB/bi n/mat l ab.
Post Installation Procedures
Successful Insta lla tion
I f you want to use the MATLAB Appl i cati on Program I nterface, you must
confi gure the mex scri pt to work wi th your compi l er. Al so, some tool boxes may
requi re some addi ti onal confi gurati on. For more i nformati on, see I nstal l i ng
Addi ti onal Tool boxes l ater i n thi s secti on.
Unsuccessful I nsta lla tion
I f MATLAB does not execute correctl y after i nstal l ati on:
1 Check the MATLAB Known Softwareand Documentation Problems
document for the l atest i nformati on concerni ng i nstal l ati on. Thi s document
i s accessi bl e from the Hel p Desk.
2 Repeat the i nstal l ati on procedure from the begi nni ng but run the CD i nstal l
scri pt usi ng the - t opti on.
/cdr om/i ns t al l _l nx 86. s h - t
For More Information The MATLAB I nstallation Guidefor UNI X provi des
addi ti onal i nstal l ati on i nformati on. Thi s manual i s avai l abl e i n PDF form
from OnlineManuals on the Hel p Desk.
2 Installation
2-14
Installing Additional Toolboxes
To purchase addi ti onal tool boxes, vi si t the MathWorks Store at
(www. mat hwor ks . com/s t or e ). Once you purchase a tool box, i t i s downl oaded to
your computer. When you downl oad a tool box on Li nux, you recei ve a tar fi l e (a
standard, compressed formatted fi l e).
To i nstal l the tool box, you must:
1 Pl ace the tar fi l e i n $MATLAB and un-tar i t.
t ar - x f f i l ename
2 Run i ns t al l _mat l ab.
After you successful l y i nstal l the tool box, al l of i ts functi onal i ty wi l l be
avai l abl e to you when you start MATLAB.
Note Some tool boxes have ReadMe fi l es associ ated wi th them. When you
downl oad the tool box, check to see i f there i s a ReadMe fi l e. These fi l es contai n
i mportant i nformati on about the tool box and possi bl y i nstal l ati on and
confi gurati on notes. To vi ew the ReadMe fi l e for a tool box, use the what s new
command.
Accessing the Online Documentation (Help Desk)
Access the onl i ne documentati on (Hel p Desk) di rectl y from your product CD:
1 Pl ace the CD i n your CD-ROM dri ve and mount i t.
2 Type hel pdes k at the MATLAB prompt.
Installing on Linux
2-15
The Hel p Desk, si mi l ar to thi s fi gure, appears i n your Web browser.
2 Installation
2-16
3
Getti ng Started
StartingMATLAB . . . . . . . . . . . . . . . . . 3-2
Matrices and Magic Squares . . . . . . . . . . . . . 3-3
Expressions . . . . . . . . . . . . . . . . . . . . 3-11
Workingwith Matrices . . . . . . . . . . . . . . . 3-15
MoreAbout Matrices and Arrays . . . . . . . . . . . 3-19
TheCommand Window . . . . . . . . . . . . . . . 3-29
TheMATLAB Environment . . . . . . . . . . . . . 3-33
Help and OnlineDocumentation . . . . . . . . . . . 3-37
3 G etting Started
3-2
Starting MATLAB
Thi s book i s i ntended to hel p you start l earni ng MATLAB. I t contai ns a
number of exampl es, so you shoul d run MATLAB and fol l ow al ong.
To run MATLAB on a PC, doubl e-cl i ck on the MATLAB i con. To run MATLAB
on a Li nux system, type mat l ab at the operati ng system prompt. To qui t
MATLAB at any ti me, type qui t at the MATLAB prompt.
I f you feel you need more assi stance, you can:
Access the Hel p Desk by typi ng hel pdes k at the MATLAB prompt.
Type hel p at the MATLAB prompt.
Pul l down the Help menu on a PC.
For more i nformati on about hel p and onl i ne documentati on, see Hel p and
Onl i ne Documentati on l ater i n thi s chapter. Al so, Chapter 1 provi des
addi ti onal hel p resources.
M atrices and M agic Squares
3-3
Matrices and Magic Squares
The best way for you to get started wi th MATLAB i s to l earn how to handl e
matri ces. Thi s secti on shows you how to do that. I n MATLAB, a matri x i s a
rectangul ar array of numbers. Speci al meani ng i s someti mes attached to
1-by-1 matri ces, whi ch are scal ars, and to matri ces wi th onl y one row or
col umn, whi ch are vectors. MATLAB has other ways of stori ng both numeri c
and nonnumeri c data, but i n the begi nni ng, i t i s usual l y best to thi nk of
everythi ng as a matri x. The operati ons i n MATLAB are desi gned to be as
natural as possi bl e. Where other programmi ng l anguages work wi th numbers
one at a ti me, MATLAB al l ows you to work wi th enti re matri ces qui ckl y and
easi l y.
3 G etting Started
3-4
A good exampl e matri x, used
throughout thi s book, appears
i n the Renai ssance engravi ng
Mel anchol i a I by the German
arti st and amateur
mathemati ci an Al brecht Drer.
Thi s i mage i s fi l l ed wi th
mathemati cal symbol i sm, and i f
you l ook careful l y, you wi l l see a
matri x i n the upper ri ght
corner. Thi s matri x i s known as
a magi c square and was
bel i eved by many i n Drers
ti me to have genui nel y magi cal
properti es. I t does tur n out to
have some fasci nati ng
characteri sti cs worth expl ori ng.
Entering Matrices
You can enter matri ces i nto MATLAB i n several di fferent ways:
Enter an expl i ci t l i st of el ements.
Load matri ces from external data fi l es.
Generate matri ces usi ng bui l t-i n functi ons.
Create matri ces wi th your own functi ons i n M-fi l es.
Start by enteri ng Drers matri x as a l i st of i ts el ements. You have onl y to
fol l ow a few basi c conventi ons:
Separate the el ements of a row wi th bl anks or commas.
Use a semi col on, ; , to i ndi cate the end of each row.
Surround the enti re l i st of el ements wi th square brackets, [ ] .
To enter Drers matri x, si mpl y type
A = [ 16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]
M atrices and M agic Squares
3-5
MATLAB di spl ays the matri x you just entered,
A =
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
Thi s exactl y matches the numbers i n the engravi ng. Once you have entered the
matri x, i t i s automati cal l y remembered i n the MATLAB workspace. You can
refer to i t si mpl y as A. Now that you have A i n the workspace, take a l ook at
what makes i t so i nteresti ng. Why i s i t magi c?
sum, transpose, and diag
Youre probabl y al ready aware that the speci al properti es of a magi c square
have to do wi th the vari ous ways of summi ng i ts el ements. I f you take the sum
al ong any row or col umn, or al ong ei ther of the two mai n di agonal s, you wi l l
al ways get the same number. Lets veri fy that usi ng MATLAB. The fi rst
statement to try i s
s um( A)
MATLAB repl i es wi th
ans =
34 34 34 34
When you dont speci fy an output vari abl e, MATLAB uses the vari abl e ans ,
short for answer , to store the resul ts of a cal cul ati on. You have computed a row
vector contai ni ng the sums of the col umns of A. Sure enough, each of the
col umns has the same sum, the magi c sum, 34.
How about the row sums? MATLAB has a preference for worki ng wi th the
col umns of a matri x, so the easi est way to get the row sums i s to transpose the
matri x, compute the col umn sums of the transpose, and then transpose the
resul t. The transpose operati on i s denoted by an apostrophe or si ngl e quote, ' .
I t fl i ps a matri x about i ts mai n di agonal and i t turns a row vector i nto a col umn
vector. So
A'
3 G etting Started
3-6
produces
ans =
16 5 9 4
3 10 6 15
2 11 7 14
13 8 12 1
And
s um( A' ) '
produces a col umn vector contai ni ng the row sums
ans =
34
34
34
34
The sum of the el ements on the mai n di agonal i s easi l y obtai ned wi th the hel p
of the di ag functi on, whi ch pi cks off that di agonal .
di ag( A)
produces
ans =
16
10
7
1
and
s um( di ag( A) )
produces
ans =
34
M atrices and M agic Squares
3-7
The other di agonal , the so-cal l ed antidiagonal, i s not so i mportant
mathemati cal l y, so MATLAB does not have a ready-made functi on for i t. But a
functi on ori gi nal l y i ntended for use i n graphi cs, f l i pl r , fl i ps a matri x from l eft
to ri ght.
s um( di ag( f l i pl r ( A) ) )
ans =
34
You have veri fi ed that the matri x i n Drers engravi ng i s i ndeed a magi c
square and, i n the process, have sampl ed a few MATLAB matri x operati ons.
The fol l owi ng secti ons conti nue to use thi s matri x to i l l ustrate addi ti onal
MATLAB capabi l i ti es.
Subscripts
The el ement i n row i and col umn j of A i s denoted by A( i , j ) . For exampl e,
A( 4, 2) i s the number i n the fourth row and second col umn. For our magi c
square, A( 4, 2) i s 15. So i t i s possi bl e to compute the sum of the el ements i n the
fourth col umn of A by typi ng
A( 1, 4) + A( 2, 4) + A( 3, 4) + A( 4, 4)
Thi s produces
ans =
34
but i s not the most el egant way of summi ng a si ngl e col umn.
I t i s al so possi bl e to refer to the el ements of a matri x wi th a si ngl e subscri pt,
A( k) . Thi s i s the usual way of referenci ng row and col umn vectors. But i t can
al so appl y to a ful l y two-di mensi onal matri x, i n whi ch case the array i s
regarded as one l ong col umn vector formed from the col umns of the ori gi nal
matri x. So, for our magi c square, A( 8) i s another way of referri ng to the val ue
15 stor ed i n A( 4, 2) .
I f you try to use the val ue of an el ement outsi de of the matri x, i t i s an error.
t = A( 4, 5)
I ndex ex ceeds mat r i x di me ns i ons .
3 G etting Started
3-8
On the other hand, i f you store a val ue i n an el ement outsi de of the matri x, the
si ze i ncreases to accommodate the newcomer.
X = A;
X( 4, 5) = 17
X =
16 3 2 13 0
5 10 11 8 0
9 6 7 12 0
4 15 14 1 17
The Colon Operator
The col on, : , i s one of MATLABs most i mportant operators. I t occurs i n several
di fferent forms. The expressi on
1: 10
i s a row vector contai ni ng the i ntegers from 1 to 10
1 2 3 4 5 6 7 8 9 10
To obtai n nonuni t spaci ng, speci fy an i ncrement. For exampl e,
100: - 7: 50
i s
100 93 86 79 72 65 58 51
and
0: pi /4: pi
i s
0 0. 7854 1. 5708 2. 3562 3. 1416
Subscri pt expressi ons i nvol vi ng col ons refer to porti ons of a matri x.
A( 1: k, j )
i s the fi rst k el ements of the j th col umn of A. So
s um( A( 1: 4, 4) )
M atrices and M agic Squares
3-9
computes the sum of the fourth col umn. But there i s a better way. The col on by
i tsel f refers to all the el ements i n a row or col umn of a matri x and the keyword
end refers to the last row or col umn. So
s um( A( : , end) )
computes the sum of the el ements i n the l ast col umn of A.
ans =
34
Why i s the magi c sum for a 4-by-4 square equal to 34? I f the i ntegers from 1 to
16 are sorted i nto four groups wi th equal sums, that sum must be
s um( 1: 16) /4
whi ch, of course, i s
ans =
34
Usi ng t he Symbol i c Mat h
Tool box, you can di scover
t hat t he magi c sum f or an
n-by-n magi c square i s
( n
3
+ n) / 2 .
The magic Function
MATLAB actual l y has a bui l t-i n functi on that creates magi c squares of al most
any si ze. Not surpri si ngl y, thi s functi on i s named magi c .
B = magi c( 4)
B =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
Thi s matri x i s al most the same as the one i n the Drer engravi ng and has al l
the same magi c properti es; the onl y di fference i s that the two mi ddl e col umns
are exchanged. To make thi s B i nto Drers A, swap the two mi ddl e col umns.
A = B( : , [ 1 3 2 4] )
3 G etting Started
3-10
Thi s says for each of the rows of matri x B, reorder the el ements i n the order 1,
3, 2, 4. I t produces
A =
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
Why woul d Drer go to the troubl e of rearrangi ng the col umns when he coul d
have used MATLABs orderi ng? No doubt he wanted to i ncl ude the date of the
engravi ng, 1514, at the bottom of hi s magi c square.
For More Information UsingMATLAB provi des comprehensi ve materi al on
the MATLAB l anguage, envi ronment, mathemati cal topi cs, and programmi ng
i n MATLAB. Access UsingMATLAB from the Hel p Desk.
Expressions
3-11
Expressions
Li ke most other programmi ng l anguages, MATLAB provi des mathemati cal
expressions, but unl i ke most programmi ng l anguages, these expressi ons
i nvol ve enti re matri ces. The bui l di ng bl ocks of expressi ons are:
Vari abl es
Numbers
Operators
Functi ons
Variables
MATLAB does not requi re any type decl arati ons or di mensi on statements.
When MATLAB encounters a new vari abl e name, i t automati cal l y creates the
vari abl e and al l ocates the appropri ate amount of storage. I f the vari abl e
al ready exi sts, MATLAB changes i ts contents and, i f necessary, al l ocates new
storage. For exampl e,
num_s t udent s = 25
creates a 1-by-1 matri x named num_s t udent s and stores the val ue 25 i n i ts
si ngl e el ement.
Vari abl e names consi st of a l etter, fol l owed by any number of l etters, di gi ts, or
underscores. MATLAB uses onl y the fi rst 31 characters of a vari abl e name.
MATLAB i s case sensi ti ve; i t di sti ngui shes between uppercase and l owercase
l etters. A and a are not the same vari abl e. To vi ew the matri x assi gned to any
vari abl e, si mpl y enter the vari abl e name.
Numbers
MATLAB uses conventi onal deci mal notati on, wi th an opti onal deci mal poi nt
and l eadi ng pl us or mi nus si gn, for numbers. Scientificnotation uses the l etter
e to speci fy a power-of-ten scal e factor. I maginarynumbers use ei ther i or j as
a suffi x. Some exampl es of l egal numbers are
3 - 99 0. 0001
9. 6397238 1. 60210e- 20 6. 02252e23
1i - 3. 14159j 3e5i
3 G etting Started
3-12
Al l numbers are stored i nternal l y usi ng the longformat speci fi ed by the I EEE
fl oati ng-poi nt standard. Fl oati ng-poi nt numbers have a fi ni te precision of
roughl y 16 si gni fi cant deci mal di gi ts and a fi ni te rangeof roughl y 10
-308
to
10
+308
.
Operators
Expressi ons use fami l i ar ari thmeti c operators and precedence rul es.
Functions
MATLAB provi des a l arge number of standard el ementary mathemati cal
functi ons, i ncl udi ng abs , s qr t , e x p, and s i n. Taki ng the square root or
l ogari thm of a negati ve number i s not an error; the appropri ate compl ex resul t
i s produced automati cal l y. MATLAB al so provi des many more advanced
mathemati cal functi ons, i ncl udi ng Bessel and gamma functi ons. Most of these
functi ons accept compl ex arguments. For a l i st of the el ementary mathemati cal
functi ons, type
hel p el f un
+ Addi ti on
- Subtracti on
* Mul ti pl i cati on
/ Di vi si on
\ Left di vi si on (descri bed i n Matri ces and Li near
Al gebra i n UsingMATLAB)
^ Power
' Compl ex conjugate transpose
( ) Speci fy eval uati on order
Expressions
3-13
For a l i st of more advanced mathemati cal and matri x functi ons, type
hel p s pecf un
hel p el mat
For More Information Appendi x A, MATLAB Qui ck Reference, contai ns
bri ef descri pti ons of the MATLAB functi ons. Use the Hel p Desk to access
compl ete descri pti ons of al l the MATLAB functi ons by Subject or by I ndex.
Some of the functi ons, l i ke s qr t and s i n, are built-in. They are part of the
MATLAB core so they are very effi ci ent, but the computati onal detai l s are not
readi l y accessi bl e. Other functi ons, l i ke gamma and s i nh, are i mpl emented i n
M-fi l es. You can see the code and even modi fy i t i f you want.
Several speci al functi ons provi de val ues of useful constants.
I nfi ni ty i s generated by di vi di ng a nonzero val ue by zero, or by eval uati ng wel l
defi ned mathemati cal expressi ons that overflow, i .e., exceed r eal max .
Not-a-number i s generated by tryi ng to eval uate expressi ons l i ke 0/0 or
I nf - I nf that do not have wel l defi ned mathemati cal val ues.
The functi on names are not reserved. I t i s possi bl e to overwri te any of them
wi th a new vari abl e, such as
eps = 1. e- 6
pi 3.14159265
i I magi nary uni t, -1
j Same as i
e ps Fl oati ng-poi nt rel ati ve preci si on, 2
-52
r eal mi n Smal l est fl oati ng-poi nt number, 2
-1022
r eal max Largest fl oati ng-poi nt number, (2-)2
1023
I nf I nfi ni ty
NaN Not-a-number
3 G etting Started
3-14
and then use that val ue i n subsequent cal cul ati ons. The ori gi nal functi on can
be restored wi th
cl ear eps
Expressions
You have al ready seen several exampl es of MATLAB expressi ons. Here are a
few more exampl es, and the resul ti ng val ues.
r ho = ( 1+s qr t ( 5) ) /2
r ho =
1. 6180
a = abs ( 3+4i )
a =
5
z = s qr t ( bes s el k( 4/3, r ho- i ) )
z =
0. 3730+ 0. 3214i
huge = e x p( l og( r eal max ) )
huge =
1. 7977e+308
t oobi g = pi *huge
t oobi g =
I nf
W orking w ith M atrices
3-15
Working with Matrices
Thi s secti on i ntroduces you to other ways of creati ng matri ces.
Generating Matrices
MATLAB provi des four functi ons that generate basi c matri ces.
Here are some exampl es.
Z = z er os ( 2, 4)
Z =
0 0 0 0
0 0 0 0
F = 5*ones ( 3, 3)
F =
5 5 5
5 5 5
5 5 5
N = f i x ( 10*r and( 1, 10) )
N =
4 9 4 4 8 5 2 6 8 0
R = r andn( 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
z er os Al l zeros
ones Al l ones
r and Uni forml y di stri buted random el ements
r andn Normal l y di stri buted random el ements
3 G etting Started
3-16
The load Command
The l oad command reads bi nary fi l es contai ni ng matri ces generated by earl i er
MATLAB sessi ons, or reads text fi l es contai ni ng numeri c data. The text fi l e
shoul d be organi zed as a rectangul ar tabl e of numbers, separated by bl anks,
wi th one row per l i ne, and an equal number of el ements i n each row. For
exampl e, outsi de of MATLAB, create a text fi l e contai ni ng these four l i nes.
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
Store the fi l e under the name magi k. dat . Then the command
l oad magi k. dat
reads the fi l e and creates a vari abl e, magi k, contai ni ng our exampl e matri x.
M-Files
You can create your own matri ces usi ng M-files, whi ch are text fi l es contai ni ng
MATLAB code. Just create a fi l e contai ni ng the same statements you woul d
type at the MATLAB command l i ne. Save the fi l e under a name that ends i n . m.
Note To access a text edi tor on a PC, choose Open or New from the File
menu or press the appropri ate button on the tool bar. To access a text edi tor
under Li nux, use the ! symbol fol l owed by whatever command you woul d
ordi nari l y use at your operati ng system prompt.
For exampl e, create a fi l e contai ni ng these fi ve l i nes.
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 ] ;
W orking w ith M atrices
3-17
Store the fi l e under the name magi k. m. Then the statement
magi k
reads the fi l e and creates a vari abl e, A, contai ni ng our exampl e matri x.
Concatenation
Concatenation i s the pr ocess of joi ni ng smal l matri ces to make bi gger ones. I n
fact, you made your fi rst matri x by concatenati ng i ts i ndi vi dual el ements. The
pai r of square brackets, [ ] , i s the concatenati on operator. For an exampl e, start
wi th the 4-by-4 magi c square, A, and form
B = [ A A+32; A+48 A+16]
The resul t i s an 8-by-8 matri x, obtai ned by joi ni ng the four submatri ces.
B =
16 3 2 13 48 35 34 45
5 10 11 8 37 42 43 40
9 6 7 12 41 38 39 44
4 15 14 1 36 47 46 33
64 51 50 61 32 19 18 29
53 58 59 56 21 26 27 24
57 54 55 60 25 22 23 28
52 63 62 49 20 31 30 17
Thi s matri x i s hal f way to bei ng another magi c square. I ts el ements are a
rearrangement of the i ntegers 1: 64. I ts col umn sums are the correct val ue for
an 8-by-8 magi c square.
s um( B)
ans =
260 260 260 260 260 260 260 260
But i ts row sums, s um( B' ) ' , are not al l the same. Further mani pul ati on i s
necessary to make thi s a val i d 8-by-8 magi c square.
3 G etting Started
3-18
Deleting Rows and Columns
You can del ete rows and col umns from a matri x usi ng just a pai r of square
brackets. Start wi th
X = A;
Then, to del ete the second col umn of X, use
X( : , 2) = [ ]
Thi s changes X to
X =
16 2 13
5 11 8
9 7 12
4 14 1
I f you del ete a si ngl e el ement from a matri x, the resul t i snt a matri x anymore.
So, expressi ons l i ke
X( 1, 2) = [ ]
resul t i n an error. However, usi ng a si ngl e subscri pt del etes a si ngl e el ement,
or sequence of el ements, and reshapes the remai ni ng el ements i nto a row
vector. So
X( 2: 2: 10) = [ ]
resul ts i n
X =
16 9 2 7 13 12 1
M ore A bout M atrices and A rrays
3-19
More About Matrices and Arrays
Thi s secti ons shows you more about worki ng wi th matri ces and arrays,
focusi ng on:
Li near al gebra
Arrays
Mul ti vari ate data
Linear Algebra
I nformal l y, the terms matrix and array are often used i nterchangeabl y. More
preci sel y, a matrixi s a two-di mensi onal numeri c array that represents a linear
transformation. The mathemati cal operati ons defi ned on matri ces are the
subject of linear algebra.
Drers magi c square
A =
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
provi des several exampl es that gi ve a taste of MATLAB matri x operati ons.
Youve al ready seen the matri x transpose, A'. Addi ng a matri x to i ts transpose
produces a symmetric matri x.
A + A'
ans =
32 8 11 17
8 20 17 23
11 17 14 26
17 23 26 2
For More Information Al l of the MATLAB math functi ons are descri bed i n
the MATLAB Function Reference, whi ch i s accessi bl e from the Hel p Desk.
3 G etting Started
3-20
The mul ti pl i cati on symbol , *, denotes the matrixmul ti pl i cati on i nvol vi ng i nner
products between rows and col umns. Mul ti pl yi ng the transpose of a matri x by
the ori gi nal matri x al so produces a symmetri c matri x.
A' *A
ans =
378 212 206 360
212 370 368 206
206 368 370 212
360 206 212 378
The determi nant of thi s parti cul ar matri x happens to be zero, i ndi cati ng that
the matri x i s singular.
d = det ( A)
d =
0
The reduced row echel on form of A i s not the i denti ty.
R = r r ef ( A)
R =
1 0 0 1
0 1 0 - 3
0 0 1 3
0 0 0 0
Si nce the matri x i s si ngul ar, i t does not have an i nverse. I f you try to compute
the i nverse wi th
X = i nv ( A)
you wi l l get a warni ng message
War ni ng: Mat r i x i s c l os e t o s i ngul ar or badl y s cal ed.
Res ul t s may be i naccur at e. RCOND = 1. 175530e - 017.
Roundoff error has prevented the matri x i nversi on al gori thm from detecti ng
exact si ngul ari ty. But the val ue of r cond, whi ch stands for reciprocal condition
estimate, i s on the order of eps , the fl oati ng-poi nt rel ati ve preci si on, so the
computed i nverse i s unl i kel y to be of much use.
M ore A bout M atrices and A rrays
3-21
The ei genval ues of the magi c square are i nteresti ng.
e = ei g( A)
e =
34. 0000
8. 0000
0. 0000
- 8. 0000
One of the ei genval ues i s zero, whi ch i s another consequence of si ngul ari ty.
The l argest ei genval ue i s 34, the magi c sum. Thats because the vector of al l
ones i s an ei genvector.
v = ones ( 4, 1)
v =
1
1
1
1
A*v
ans =
34
34
34
34
When a magi c square i s scal ed by i ts magi c sum,
P = A/34
the resul t i s a doublystochasticmatri x whose row and col umn sums are al l one.
P =
0. 4706 0. 0882 0. 0588 0. 3824
0. 1471 0. 2941 0. 3235 0. 2353
0. 2647 0. 1765 0. 2059 0. 3529
0. 1176 0. 4412 0. 4118 0. 0294
3 G etting Started
3-22
Such matri ces represent the transi ti on probabi l i ti es i n a Markov process.
Repeated powers of the matri x represent repeated steps of the process. For our
exampl e, the fi fth power
P^5
i s
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
Thi s shows that as k approaches i nfi ni ty, al l the el ements i n the kth power, P
k
,
approach
1
/4.
Fi nal l y, the coeffi ci ents i n the characteri sti c pol ynomi al
pol y ( A)
are
1 - 34 - 64 2176 0
Thi s i ndi cates that the characteri sti c pol ynomi al
det( A - I )
i s
4
- 34
3
- 64
2
+ 2176
The constant term i s zero, because the matri x i s si ngul ar, and the coeffi ci ent of
the cubi c term i s -34, because the matri x i s magi c!
Arrays
When they are taken away fr om the wor l d of l i near al gebr a, matr i ces become
two di mensi onal numeri c arrays. Ari thmeti c operati ons on arrays are done
el ement-by-el ement. Thi s means that addi ti on and subtracti on are the same
for arrays and matri ces, but that mul ti pl i cati ve operati ons are di fferent.
MATLAB uses a dot, or deci mal poi nt, as part of the notati on for mul ti pl i cati ve
array operati ons.
M ore A bout M atrices and A rrays
3-23
The l i st of operators i ncl udes:
I f the Drer magi c square i s mul ti pl i ed by i tsel f wi th array mul ti pl i cati on
A. *A
the resul t i s an array contai ni ng the squares of the i ntegers from 1 to 16, i n an
unusual order.
ans =
256 9 4 169
25 100 121 64
81 36 49 144
16 225 196 1
Array operati ons are useful for bui l di ng tabl es. Suppose n i s the col umn vector
n = ( 0: 9) ' ;
Then
pows = [ n n. ^2 2. ^n]
+ Addi ti on
- Subtracti on
. * El ement-by-el ement mul ti pl i cati on
. / El ement-by-el ement di vi si on
. \ El ement-by-el ement l eft di vi si on
. ^ El ement-by-el ement power
. ' Unconjugated array transpose
3 G etting Started
3-24
bui l ds a tabl e of squares and powers of two.
pows =
0 0 1
1 1 2
2 4 4
3 9 8
4 16 16
5 25 32
6 36 64
7 49 128
8 64 256
9 81 512
The el ementary math functi ons operate on arrays el ement by el ement. So
f or mat s hor t g
x = ( 1: 0. 1: 2) ' ;
l ogs = [ x l og10( x ) ]
bui l ds a tabl e of l ogari thms.
l ogs =
1. 0 0
1. 1 0. 04139
1. 2 0. 07918
1. 3 0. 11394
1. 4 0. 14613
1. 5 0. 17609
1. 6 0. 20412
1. 7 0. 23045
1. 8 0. 25527
1. 9 0. 27875
2. 0 0. 30103
Multivariate Data
MATLAB uses col umn-ori ented anal ysi s for mul ti vari ate stati sti cal data. Each
col umn i n a data set represents a vari abl e and each row an observati on. The
( i , j ) th el ement i s the i th observati on of the j th vari abl e.
M ore A bout M atrices and A rrays
3-25
As an exampl e, consi der a data set wi th three vari abl es:
Heart rate
Wei ght
Hours of exerci se per week
For fi ve observati ons, the resul ti ng array mi ght l ook l i ke
D =
72 134 3. 2
81 201 3. 5
69 156 7. 1
82 148 2. 4
75 170 1. 2
The fi rst row contai ns the heart rate, wei ght, and exerci se hours for pati ent 1,
the second row contai ns the data for pati ent 2, and so on. Now you can appl y
many of MATLABs data anal ysi s functi ons to thi s data set. For exampl e, to
obtai n the mean and standard devi ati on of each col umn:
mu = mean( D) , s i gma = s t d( D)
mu =
75. 8 161. 8 3. 48
s i gma =
5. 6303 25. 499 2. 2107
For a l i st of the data anal ysi s functi ons avai l abl e i n MATLAB, type
hel p dat af un
I f you have access to the Stati sti cs Tool box, type
hel p s t at s
Scalar Expansion
Matri ces and scal ars can be combi ned i n several di fferent ways. For exampl e,
a scal ar i s subtracted from a matri x by subtracti ng i t from each el ement. The
average val ue of the el ements i n our magi c square i s 8.5, so
B = A - 8. 5
3 G etting Started
3-26
forms a matri x whose col umn sums are zero.
B =
7. 5 - 5. 5 - 6. 5 4. 5
- 3. 5 1. 5 2. 5 - 0. 5
0. 5 - 2. 5 - 1. 5 3. 5
- 4. 5 6. 5 5. 5 - 7. 5
s um( B)
ans =
0 0 0 0
Wi th scal ar expansi on, MATLAB assi gns a speci fi ed scal ar to al l i ndi ces i n a
range. For exampl e,
B( 1: 2, 2: 3) = 0
zeros out a porti on of B
B =
7. 5 0 0 4. 5
- 3. 5 0 0 - 0. 5
0. 5 - 2. 5 - 1. 5 3. 5
- 4. 5 6. 5 5. 5 - 7. 5
Logical Subscripting
The l ogi cal vectors created from l ogi cal and rel ati onal operati ons can be used
to reference subarrays. Suppose Xi s an ordi nary matri x and L i s a matri x of the
same si ze that i s the resul t of some l ogi cal operati on. Then X( L) speci fi es the
el ements of X where the el ements of L are nonzero.
Thi s ki nd of subscri pti ng can be done i n one step by speci fyi ng the l ogi cal
operati on as the subscri pti ng expressi on. Suppose you have the fol l owi ng set of
data.
x =
2. 1 1. 7 1. 6 1. 5 NaN 1. 9 1. 8 1. 5 5. 1 1. 8 1. 4 2. 2 1. 6 1. 8
The NaNi s a marker for a mi ssi ng observati on, such as a fai l ure to respond to
an i tem on a questi onnai re. To remove the mi ssi ng data wi th l ogi cal i ndexi ng,
M ore A bout M atrices and A rrays
3-27
use f i ni t e( x ) , whi ch i s true for al l fi ni te numeri cal val ues and fal se for NaN
and I nf .
x = x ( f i ni t e( x ) )
x =
2. 1 1. 7 1. 6 1. 5 1. 9 1. 8 1. 5 5. 1 1. 8 1. 4 2. 2 1. 6 1. 8
Now ther e i s one obser vati on, 5. 1, whi ch seems to be very di fferent from the
others. I t i s an outlier. The fol l owi ng statement removes outl i ers, i n thi s case
those el ements more than three standard devi ati ons from the mean.
x = x ( abs ( x - me an( x ) ) <= 3*s t d( x ) )
x =
2. 1 1. 7 1. 6 1. 5 1. 9 1. 8 1. 5 1. 8 1. 4 2. 2 1. 6 1. 8
For another exampl e, hi ghl i ght the l ocati on of the pri me numbers i n Drers
magi c square by usi ng l ogi cal i ndexi ng and scal ar expansi on to set the
nonpri mes to 0.
A( ~i s pr i me( A) ) = 0
A =
0 3 2 13
5 0 11 0
0 0 7 0
0 0 0 0
The find Function
The f i nd functi on determi nes the i ndi ces of array el ements that meet a gi ven
l ogi cal condi ti on. I n i ts si mpl est form, f i nd returns a col umn vector of i ndi ces.
Transpose that vector to obtai n a row vector of i ndi ces. For exampl e,
k = f i nd( i s pr i me( A) ) '
pi cks out the l ocati ons, usi ng one-di mensi onal i ndexi ng, of the pri mes i n the
magi c square.
k =
2 5 9 10 11 13
3 G etting Started
3-28
Di spl ay those pri mes, as a row vector i n the order determi ned by k, wi th
A( k)
ans =
5 3 2 11 7 13
When you use k as a l eft-hand-si de i ndex i n an assi gnment statement, the
matri x structure i s preserved.
A( k) = NaN
A =
16 NaN NaN NaN
NaN 10 NaN 8
9 6 NaN 12
4 15 14 1
The C om m and W indow
3-29
The Command Window
So far, you have been usi ng the MATLAB command l i ne, typi ng commands and
expressi ons, and seei ng the resul ts pri nted i n the command wi ndow. Thi s
secti on descri bes a few ways of al teri ng the appearance of the command
wi ndow. I f your system al l ows you to sel ect the command wi ndow font or
typeface, we recommend you use a fi xed wi dth font, such as Fi xedsys or
Couri er, to provi de proper spaci ng.
The format Command
The f or mat command control s the numeri c format of the val ues di spl ayed by
MATLAB. The command affects onl y how numbers are di spl ayed, not how
MATLAB computes or saves them. Here are the di fferent formats, together
wi th the resul ti ng output produced from a vector x wi th components of
di fferent magni tudes.
x = [ 4/3 1. 2345e- 6]
f or mat s hor t
1. 3333 0. 0000
f or mat s hor t e
1. 3333e+000 1. 2345e- 006
f or mat s hor t g
1. 3333 1. 2345e- 006
f or mat l ong
1. 33333333333333 0. 00000123450000
f or mat l ong e
1. 333333333333333e+000 1. 234500000000000e- 006
3 G etting Started
3-30
f or mat l ong g
1. 33333333333333 1. 2345e- 006
f or mat bank
1. 33 0. 00
f or mat r at
4/3 1/810045
f or mat hex
3f f 5555555555555 3eb4b6231abf d271
I f the l argest el ement of a matri x i s l arger than 10
3
or smal l er than 10
-3
,
MATLAB appl i es a common scal e factor for the short and l ong formats.
I n addi ti on to the f or mat commands shown above
f or mat c ompact
suppresses many of the bl ank l i nes that appear i n the output. Thi s l ets you
vi ew more i nformati on on a screen or wi ndow. I f you want more control over
the output format, use the s pr i nt f and f pr i nt f functi ons.
Suppressing Output
I f you si mpl y type a statement and press Return or Enter, MATLAB
automati cal l y di spl ays the resul ts on screen. However, i f you end the l i ne wi th
a semi col on, MATLAB performs the computati on but does not di spl ay any
output. Thi s i s parti cul arl y useful when you generate l arge matri ces. For
exampl e,
A = magi c( 100) ;
The C om m and W indow
3-31
Long Command Lines
I f a statement does not fi t on one l i ne, use three peri ods, . . . , fol l owed by
Return or Enter to i ndi cate that the statement conti nues on the next l i ne. For
exampl e,
s = 1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 + 1/7 . . .
- 1/8 + 1/9 - 1/10 + 1/11 - 1/12;
Bl ank spaces around the =, +, and - si gns are opti onal , but they i mprove
readabi l i ty.
Command Line Editing
Vari ous arrow and control keys on your keyboard al l ow you to recal l , edi t, and
reuse commands you have typed earl i er. For exampl e, suppose you mi stakenl y
enter
r ho = ( 1 + s qt ( 5) ) /2
You have mi sspel l ed s qr t . MATLAB responds wi th
Undef i ned f unc t i on or v ar i abl e ' s qt ' .
I nstead of retypi ng the enti re l i ne, si mpl y press the key. The mi sspel l ed
command i s redi spl ayed. Use the key to move the cursor over and i nsert the
mi ssi ng r . Repeated use of the key recal l s earl i er l i nes. Typi ng a few
characters and then the key fi nds a previ ous l i ne that begi ns wi th those
characters.
The l i st of avai l abl e command l i ne edi ti ng keys i s di fferent on di fferent
computers. Experi ment to see whi ch of the fol l owi ng keys i s avai l abl e on your
machi ne. (Many of these keys wi l l be fami l i ar to users of the EMACS edi tor.)
Ctrl-p Recal l previ ous l i ne
Ctrl-n Recal l next l i ne
Ctrl-b Move back one character
Ctrl-f Move forward one character
Ctrl- Ctrl-r Move ri ght one word
3 G etting Started
3-32
Ctrl- Ctrl-l Move l eft one wor d
Home Ctrl-a Move to begi nni ng of l i ne
End Ctrl-e Move to end of l i ne
Esc Ctrl-u Cl ear l i ne
Del Ctrl-d Del ete character at cursor
Backspace Ctrl-h Del ete character before cursor
Ctrl-k Del ete to end of l i ne
The M A TLA B Environm ent
3-33
The MATLAB Environment
The MATLAB envi ronment i ncl udes both the set of vari abl es bui l t up duri ng a
MATLAB sessi on and the set of di sk fi l es contai ni ng programs and data that
per si st between sessi ons.
The Workspace
The workspacei s the area of memory accessi bl e from the MATLAB command
l i ne. Two commands, who and whos , show the current contents of the
workspace. The who command gi ves a short l i st, whi l e whos al so gi ves si ze and
storage i nformati on.
Here i s the output produced by whos on a workspace contai ni ng resul ts from
some of the exampl es i n thi s book. I t shows several di fferent MATLAB data
structures. As an exerci se, you mi ght see i f you can match each of the vari abl es
wi th the code segment i n thi s book that generates i t.
whos
Name Si z e By t es Cl as s
A 4x 4 128 doubl e ar r ay
D 5x 3 120 doubl e ar r ay
M 10x 1 3816 cel l ar r ay
S 1x 3 442 s t r uc t ar r ay
h 1x 11 22 char ar r ay
n 1x 1 8 doubl e ar r ay
s 1x 5 10 char ar r ay
v 2x 5 20 char ar r ay
Gr and t ot al i s 471 el ement s us i ng 4566 by t es .
To del ete al l the exi sti ng vari abl es from the workspace, enter
cl e ar
3 G etting Started
3-34
save Commands
The s av e commands preserve the contents of the workspace i n a MAT-fi l e that
can be read wi th the l oad command i n a l ater MATLAB sessi on. For exampl e,
s av e Augus t 17t h
saves the enti re workspace contents i n the fi l e Augus t 17t h. mat . I f desi red, you
can save onl y certai n vari abl es by speci fyi ng the vari abl e names after the
fi l ename.
Ordi nari l y, the vari abl es are saved i n a bi nary format that can be read qui ckl y
(and accuratel y) by MATLAB. I f you want to access these fi l es outsi de of
MATLAB, you may want to speci fy an al ternati ve format.
When you save workspace contents i n text format, you shoul d save onl y one
vari abl e at a ti me. I f you save more than one vari abl e, MATLAB wi l l create the
text fi l e, but you wi l l be unabl e to l oad i t easi l y back i nto MATLAB.
The Search Path
MATLAB uses a search path, an ordered l i st of di rectori es, to determi ne how
to execute the functi ons you cal l . When you cal l a standard functi on, MATLAB
executes the fi rst M-fi l e functi on on the path that has the speci fi ed name. You
can overri de thi s behavi or usi ng speci al pri vate di rectori es and subfuncti ons.
The command
pat h
shows the search path on any pl atform. On PCs, choose Set Pathfr om the File
menu to vi ew or modi fy the path.
- as ci i Use 8-di gi t text format.
- as ci i - doubl e Use 16-di gi t text format.
- as ci i - doubl e - t abs Del i mi t array el ements wi th tabs.
- v 4 Create a fi l e for MATLAB versi on 4.
- append Append data to an exi sti ng MAT-fi l e.
The M A TLA B Environm ent
3-35
Disk File Manipulation
The commands di r , t y pe, de l et e, and cd i mpl ement a set of generi c operati ng
system commands for mani pul ati ng fi l es. Thi s tabl e i ndi cates how these
commands map to other operati ng systems.
For most of these commands, you can use pathnames, wi l dcards, and dri ve
desi gnators i n the usual way.
The diary Command
The di ar y command creates a di ary of your MATLAB sessi on i n a di sk fi l e. You
can vi ew and edi t the resul ti ng text fi l e usi ng any word processor. To create a
fi l e cal l ed di ar y that contai ns al l the commands you enter, as wel l as
MATLABs pri nted output (but not the graphi cs output), enter
di ar y
To save the MATLAB sessi on i n a fi l e wi th a parti cul ar name, use
di ar y f i l ename
To stop recordi ng the sessi on, use
di ar y of f
Running External Programs
The excl amati on poi nt character ! i s a shel l escape and i ndi cates that the rest
of the i nput l i ne i s a command to the operati ng system. Thi s i s qui te useful for
i nvoki ng uti l i ti es or runni ng other programs wi thout qui tti ng MATLAB. On
Li nux, for exampl e,
! emacs magi k. m
MATLAB MS-DOS Linux
di r di r l s
t y pe t y pe cat
del et e del or e r as e r m
c d chdi r cd
3 G etting Started
3-36
i nvokes an edi tor cal l ed e macs for a fi l e named magi k. m. When you qui t the
external program, the operati ng system returns control to MATLAB.
H elp and O nline D ocum entation
3-37
Help and Online Documentation
There are several di fferent ways to access onl i ne i nformati on about MATLAB
functi ons:
The MATLAB Hel p Desk
Onl i ne reference pages
The hel p command
Li nk to The MathWorks, I nc.
The Help Desk
The MATLAB Hel p Desk provi des access to a wi de range of hel p and reference
i nformati on stored on CD. Many of the underl yi ng documents use HyperText
Markup Language (HTML) and are accessed wi th an I nternet Web browser
such as Netscape or Mi crosoft Expl orer. The Hel p Desk process can be started
on PCs by sel ecti ng the Help Desk opti on under the Help menu, or, on al l
computers, by typi ng
hel pdes k
Al l of MATLABs operators and functi ons have onl i ne reference pages i n HTML
format, whi ch you can reach from the Hel p Desk. These pages provi de more
detai l s and exampl es than the basi c hel p entri es. HTML versi ons of other
documents, i ncl udi ng thi s manual , are al so avai l abl e. A search engi ne, runni ng
on your own machi ne, can query al l the onl i ne reference materi al .
3 G etting Started
3-38
Using the Help Desk
When you access the Hel p Desk, you see i ts entry screen.
Online Reference Pages
The doc Comma nd
I f you know the name of a speci fi c functi on, you can vi ew i ts reference page
di rectl y. For exampl e, to get the reference page for the ev al functi on, type
doc ev al
MATLAB Function
Reference pages
Introduction to
MATLAB
In-depth instruction
on MATLAB
In-depth instruction
on MATLAB graphics
A particular MATLAB
Function Reference
Search all documents
for particular text
Access all documents
in PDF format
Simulink instruction
and reference pages
In-depth instruction
on Simulink blocks
page
Access all toolbox
documentation
Access other product
documentation
Find answers to your
questions (WWW)
Contact the
MathWorks (WWW)
Symbolic Math Toolbox
reference pages
H elp and O nline D ocum entation
3-39
The doc command starts your Web browser, i f i t i s not al ready runni ng.
Pr inting O nline Refer ence Pa ges
Ver si ons of the onl i ne reference pages, as wel l as the rest of the MATLAB
documentati on set, are al so avai l abl e i n Portabl e Document Format (PDF)
through the Hel p Desk. These pages are processed by Adobes Acrobat reader.
They reproduce the l ook and feel of the pri nted page, compl ete wi th fonts,
graphi cs, formatti ng, and i mages. Thi s i s the best way to get pri nted copi es of
reference materi al . To access the PDF versi ons of the books, sel ect Online
Manuals fr om the Hel p Desk and then choose the desi r ed book.
The help Command
The hel p command i s the most basi c way to determi ne the syntax and behavi or
of a parti cul ar functi on. I nformati on i s di spl ayed di rectl y i n the command
wi ndow. For exampl e,
hel p magi c
pri nts
MAGI C Magi c s quar e.
MAGI C( N) i s an N- by - N mat r i x cons t r uct ed f r om
t he i nt ege r s 1 t hr ough N^2 wi t h e qual r ow,
col umn, and di agonal s ums .
Pr oduces v al i d magi c s quar es f or N = 1, 3, 4, 5. . . .
Note MATLAB onl i ne hel p entri es use uppercase characters for the functi on
and vari abl e names to make them stand out from the rest of the text. When
typi ng functi on names, however, al ways use the correspondi ng l owercase
characters because MATLAB i s case sensi ti ve and al l functi on names are
actual l y i n l owercase.
Al l the MATLAB functi ons are organi zed i nto l ogi cal groups, and MATLABs
di rectory structure i s based on thi s groupi ng. For exampl e, al l the l i near
3 G etting Started
3-40
al gebra functi ons resi de i n the mat f un di rectory. To l i st the names of al l the
functi ons i n that di rectory, wi th a bri ef descri pti on of each
hel p mat f un
Mat r i x f unct i ons - numer i cal l i near al gebr a.
Mat r i x anal y s i s .
nor m - Mat r i x or v ect or nor m.
nor mes t - Es t i mat e t he mat r i x 2- nor m
. . .
The command
hel p
by i tsel f l i sts al l the di rectori es, wi th a descri pti on of the functi on category each
represents.
mat l ab/gener al
mat l ab/ops
. . .
The lookfor Comma nd
The l ookf or command al l ows you to search for functi ons based on a keyword.
I t searches through the fi rst l i ne of hel p text, whi ch i s known as the H1 l i ne,
for each MATLAB functi on, and returns the H1 l i nes contai ni ng a speci fi ed
keyword. For exampl e, MATLAB does not have a functi on named i nv er s e . So
the response from
hel p i nv er s e
i s
i nv er s e. m not f ound.
But
l ookf or i nv er s e
H elp and O nline D ocum entation
3-41
fi nds over a dozen matches. Dependi ng on whi ch tool boxes you have i nstal l ed,
you wi l l fi nd entri es l i ke
I NVHI LB I nv er s e Hi l ber t mat r i x .
ACOSH I nv er s e hy per bol i c cos i ne.
ERFI NV I nv er s e of t he er r or f unct i on.
I NV Mat r i x i nv er s e.
PI NV Ps eudoi nv er s e.
I FFT I nv er s e di s cr et e Four i er t r ans f or m.
I FFT2 Two- di mens i onal i nv er s e di s cr et e Four i er t r ans f or m.
I CCEPS I nv er s e compl ex c eps t r um.
I DCT I nv er s e di s cr et e cos i ne t r ans f or m.
Addi ng - al l to the l ookf or command, as i n
l ookf or - al l
searches the enti re hel p entry, not just the H1 l i ne.
Link to the MathWorks
I f your computer i s connected to the I nternet, the Hel p Desk provi des a
connecti on to The MathWorks, the home of MATLAB. You can al so use the
Sol uti on Search Engi ne at The MathWorks Web si te to query an up-to-date
data base of techni cal support i nformati on.
3 G etting Started
3-42
4
Graphi cs
Basic Plotting . . . . . . . . . . . . . . . . . . . 4-2
Creati ng a Pl ot . . . . . . . . . . . . . . . . . . . 4-2
I magi nary and Compl ex Data . . . . . . . . . . . . . 4-6
Addi ng Pl ots to an Exi sti ng Graph . . . . . . . . . . . 4-7
Fi gure Wi ndows . . . . . . . . . . . . . . . . . . . 4-9
Mul ti pl e Pl ots i n One Fi gure . . . . . . . . . . . . . . 4-9
Control l i ng the Axes . . . . . . . . . . . . . . . . . 4-11
Axi s Label s and Ti tl es . . . . . . . . . . . . . . . . 4-12
Annotati ng Pl ots Usi ng the Pl ot Edi tor . . . . . . . . . . 4-13
Mesh and SurfacePlots . . . . . . . . . . . . . . . 4-15
Vi sual i zi ng Functi ons of Two Vari abl es . . . . . . . . . 4-15
Images . . . . . . . . . . . . . . . . . . . . . . 4-19
PrintingGraphics . . . . . . . . . . . . . . . . . 4-21
HandleGraphics . . . . . . . . . . . . . . . . . . 4-23
Graphi cs Objects . . . . . . . . . . . . . . . . . . . 4-23
Setti ng Object Properti es . . . . . . . . . . . . . . . 4-26
Fi ndi ng the Handl es of Exi sti ng Objects . . . . . . . . . 4-28
Graphics User Interfaces . . . . . . . . . . . . . . 4-30
Graphi cal User I nterface Desi gn Tool s . . . . . . . . . . 4-30
Animations . . . . . . . . . . . . . . . . . . . . 4-31
Erase Mode Method . . . . . . . . . . . . . . . . . 4-31
Creati ng Movi es . . . . . . . . . . . . . . . . . . . 4-32
4 G raphics
4-2
Basic Plotting
MATLAB has extensi ve faci l i ti es for di spl ayi ng vectors and matri ces as
graphs, as wel l as annotati ng and pri nti ng these graphs. Thi s secti on descri bes
a few of the most i mportant graphi cs functi ons and provi des exampl es of some
typi cal appl i cati ons.
For More InformationUsingMATLAB Graphicsprovi des i n-depth coverage
of MATLAB graphi cs and vi sual i zati on tool s. Access UsingMATLAB Graphics
from the Hel p Desk.
Creating a Plot
The pl ot functi on has di fferent forms, dependi ng on the i nput arguments. I f y
i s a vector, pl ot ( y ) produces a pi ecewi se l i near graph of the el ements of y
versus the i ndex of the el ements of y . I f you speci fy two vectors as arguments,
pl ot ( x , y ) produces a graph of y versus x .
For exampl e, these statements use the col on operator to create a vector of x
val ues rangi ng from zero to 2, compute the si ne of these val ues, and pl ot the
resul t.
x = 0: pi /100: 2*pi ;
y = s i n( x ) ;
pl ot ( x , y )
Now l abel the axes and add a ti tl e. The characters \pi create the symbol .
x l abel ( ' x = 0: 2\pi ' )
y l abel ( ' Si ne of x ' )
t i t l e( ' Pl ot of t he Si ne Funct i on' , ' Font Si z e' , 12)
Basic Plotting
4-3
M ultiple Da ta Sets in O ne Gra ph
Mul ti pl e x -y pai r arguments create mul ti pl e graphs wi th a si ngl e cal l to pl ot .
MATLAB automati cal l y cycl es through a predefi ned (but user settabl e) l i st of
col ors to al l ow di scri mi nati on between each set of data. For exampl e, these
statements pl ot three rel ated functi ons of x , each curve i n a separate
di sti ngui shi ng col or.
y 2 = s i n( x - . 25) ;
y 3 = s i n( x - . 5) ;
pl ot ( x , y , x , y 2, x , y 3)
The l e gend command provi des an easy way to i denti fy the i ndi vi dual pl ots.
l egend( ' s i n( x ) ' , ' s i n( x - . 25) ' , ' s i n( x - . 5) ' )
0 1 2 3 4 5 6 7
1
0.8
0.6
0.4
0.2
0
0.2
0.4
0.6
0.8
1
x = 0:2
S
i
n
e
o
f
x
Plot of the Sine Function
4 G raphics
4-4
For More Information See Defi ni ng the Col or of Li nes for Pl otti ng i n the
Axes Properti es chapter of UsingMATLAB Graphics. Access UsingMATLAB
Graphics from the Hel p Desk.
Specifying Line Styles a nd Colors
I t i s possi bl e to speci fy col or, l i ne styl es, and markers (such as pl us si gns or
ci rcl es) wi th the syntax
pl ot ( x , y , ' col or _st yl e_mar ker ' )
col or _st yl e_mar ker i s a stri ng contai ni ng from one to four characters
(encl osed i n si ngl e quotati on marks) constructed from a col or, a l i ne styl e, and
a marker type:
0 1 2 3 4 5 6 7
1
0.8
0.6
0.4
0.2
0
0.2
0.4
0.6
0.8
1
sin(x)
sin(x.25)
sin(x.5)
Basic Plotting
4-5
Col or stri ngs are ' c' , ' m' , ' y ' , ' r ' , ' g' , ' b' , ' w' , and ' k' . These correspond
to cyan, magenta, yel l ow, red, green, bl ue, whi te, and bl ack.
Li nestyl e stri ngs are ' - ' for sol i d, ' - - ' for dashed, ' : ' for dotted, ' - . ' for
dash-dot, and ' none' for no l i ne.
The marker types are ' +' , ' o' , ' *' , and ' x ' and the fi l l ed marker types ' s '
for square, ' d' for di amond, ' ^' for up tri angl e, ' v ' for down tri angl e, ' >'
for ri ght tri angl e, ' <' for l eft tri angl e, ' p' for pentagram, ' h' for hexagram,
and none for no marker.
Plotting Lines a nd M a rkers
I f you speci fy a marker type but not a l i nestyl e, MATLAB draws onl y the
marker. For exampl e,
pl ot ( x , y , ' ks ' )
pl ots bl ack squares at each data poi nt, but does not connect the markers wi th
a l i ne.
The statement
pl ot ( x , y , ' r : +' )
pl ots a red dotted l i ne and pl aces pl us si gn markers at each data poi nt. You
may want to use fewer data poi nts to pl ot the markers than you use to pl ot the
l i nes. Thi s exampl e pl ots the data twi ce usi ng a di fferent number of poi nts for
the dotted l i ne and mar ker pl ots.
x 1 = 0: pi /100: 2*pi ;
x 2 = 0: pi /10: 2*pi ;
pl ot ( x 1, s i n( x 1) , ' r : ' , x 2, s i n( x 2) , ' r +' )
4 G raphics
4-6
For More Information See the Basi c Pl otti ng chapter of UsingMATLAB
Graphics for more exampl es of pl otti ng opti ons. Access UsingMATLAB
Graphics from the Hel p Desk.
Imaginary and Complex Data
When the arguments to pl ot are compl ex, the i magi nary part i s i gnored except
when pl ot i s gi ven a si ngl e compl ex argument. For thi s speci al case, the
command i s a shortcut for a pl ot of the real part versus the i magi nary part.
Therefore,
pl ot ( Z)
where Z i s a compl ex vector or matri x, i s equi val ent to
pl ot ( r eal ( Z) , i mag( Z) )
0 1 2 3 4 5 6 7
1
0.8
0.6
0.4
0.2
0
0.2
0.4
0.6
0.8
1
Basic Plotting
4-7
For exampl e,
t = 0: pi /10: 2*pi ;
pl ot ( ex p( i *t ) , ' - o' )
ax i s equal
draws a 20-si ded pol ygon wi th l i ttl e ci rcl es at the verti ces. The command,
ax i s equal , makes the i ndi vi dual ti ck mark i ncrements on the x- and y-axes
the same l ength, whi ch makes thi s pl ot more ci rcul ar i n appearance.
Adding Plots to an Existing Graph
The hol d command enabl es you to add pl ots to an exi sti ng graph. When you
type
hol d on
MATLAB does not repl ace the exi sti ng graph when you i ssue another pl otti ng
command; i t adds the new data to the current graph, rescal i ng the axes i f
necessary.
1 0.5 0 0.5 1
1
0.8
0.6
0.4
0.2
0
0.2
0.4
0.6
0.8
1
4 G raphics
4-8
For exampl e, these statements fi rst create a contour pl ot of the peaks functi on,
then superi mpose a pseudocol or pl ot of the same functi on.
[ x , y , z ] = peaks ;
cont our ( x , y , z , 20, ' k' )
hol d on
pcol or ( x , y , z )
s hadi ng i nt er p
hol d of f
The hol d on command causes the pcol or pl ot to be combi ned wi th the cont our
pl ot i n one fi gure.
For More Information See the Speci al i zed Graphs chapter i n Using
MATLAB Graphics for i nformati on on a vari ety of graph types. Access Using
MATLAB Graphics from the Hel p Desk.
Basic Plotting
4-9
Figure Windows
Graphi ng functi ons automati cal l y open a new fi gure wi ndow i f there are no
fi gure wi ndows al ready on the screen. I f a fi gure wi ndow exi sts, MATLAB uses
that wi ndow for graphi cs output. I f there are mul ti pl e fi gure wi ndows open,
MATLAB targets the one that i s desi gnated the current fi gure (the l ast fi gure
used or cl i cked i n).
To make an exi sti ng fi gure wi ndow the current fi gure, you can cl i ck the mouse
whi l e the poi nter i s i n that wi ndow or you can type
f i gur e( n)
where n i s the number i n the fi gure ti tl e bar. The resul ts of subsequent
graphi cs commands are di spl ayed i n thi s wi ndow.
To open a new fi gure wi ndow and make i t the current fi gure, type
f i gur e
For More Information See the Fi gure Properti es chapter i n Using
MATLAB Graphics and the reference page for the f i gur e command. Access
UsingMATLAB Graphics and the f i gur e reference page from the Hel p Desk.
Multiple Plots in One Figure
The s ubpl ot command enabl es you to di spl ay mul ti pl e pl ots i n the same
wi ndow or pri nt them on the same pi ece of paper. Typi ng
s ubpl ot ( m, n, p)
parti ti ons the fi gure wi ndow i nto an m-by-n matri x of smal l subpl ots and sel ects
the pth subpl ot for the current pl ot. The pl ots are numbered al ong fi rst the top
4 G raphics
4-10
row of the fi gure wi ndow, then the second row, and so on. For exampl e, these
statements pl ot data i n four di fferent subregi ons of the fi gure wi ndow.
t = 0: pi /10: 2*pi ;
[ X, Y, Z] = cy l i nder ( 4*cos ( t ) ) ;
s ubpl ot ( 2, 2, 1) ; mes h( X)
s ubpl ot ( 2, 2, 2) ; mes h( Y)
s ubpl ot ( 2, 2, 3) ; mes h( Z)
s ubpl ot ( 2, 2, 4) ; mes h( X, Y, Z)
0
20
40
0
20
40
5
0
5
0
20
40
0
20
40
5
0
5
0
20
40
0
20
40
0
0.5
1
5
0
5
5
0
5
0
0.5
1
Basic Plotting
4-11
Controlling the Axes
The ax i s command supports a number of opti ons for setti ng the scal i ng,
ori entati on, and aspect rati o of pl ots.
Setting Ax is Limits
By defaul t, MATLAB fi nds the maxi ma and mi ni ma of the data to choose the
axi s l i mi ts to span thi s range. The ax i s command enabl es you to speci fy your
own l i mi ts
ax i s ( [ x mi n x max y mi n y max ] )
or for three-di mensi onal graphs,
ax i s ( [ x mi n x max y mi n y max z mi n z max ] )
Use the command
ax i s aut o
to re-enabl e MATLABs automati c l i mi t sel ecti on.
Setting Ax is Aspect Ra tio
ax i s al so enabl es you to speci fy a number of predefi ned modes. For exampl e,
ax i s s quar e
makes the x-axes and y-axes the same l ength.
ax i s equal
makes the i ndi vi dual ti ck mark i ncrements on the x- and y-axes the same
l ength. Thi s means
pl ot ( ex p( i *[ 0: pi /10: 2*pi ] ) )
fol l owed by ei ther ax i s s quar e or ax i s equal turns the oval i nto a proper
ci rcl e.
ax i s aut o nor mal
returns the axi s scal i ng to i ts defaul t, automati c mode.
4 G raphics
4-12
Setting Ax is Visibility
You can use the ax i s command to make the axi s vi si bl e or i nvi si bl e.
ax i s on
makes the axi s vi si bl e. Thi s i s the defaul t.
ax i s of f
makes the axi s i nvi si bl e.
Setting Gr id Lines
The gr i d command toggl es gri d l i nes on and off. The statement
gr i d on
turns the gri d l i nes on and
gr i d of f
turns them back off agai n.
For More Information See the ax i s and ax es reference pages and the Axes
Properti es chapter i n UsingMATLAB Graphics. Access these reference pages
and UsingMATLAB Graphics from the Hel p Desk.
Axis Labels and Titles
The x l abel , y l abel , and z l abel commands add x-, y-, and z-axi s l abel s. The
t i t l e command adds a ti tl e at the top of the fi gure and the t ex t functi on
i nserts text anywhere i n the fi gure. A subset of TeX notati on produces Greek
l etters.
t = - pi : pi /100: pi ;
y = s i n( t ) ;
pl ot ( t , y )
ax i s ( [ - pi pi - 1 1] )
x l abel ( ' - \pi \l eq {\i t t } \l eq \pi ' )
y l abel ( ' s i n( t ) ' )
t i t l e( ' Gr aph of t he s i ne f unct i on' )
t ex t ( 1, - 1/3, ' {\i t Not e t he odd s y mmet r y . }' )
Basic Plotting
4-13
For More Information See the Label i ng Graphs chapter i n Using
MATLAB Graphics for more i nformati on. Access UsingMATLAB Graphics
from the Hel p Desk.
Annotating Plots Using the Plot Editor
After creati ng a pl ot, you can make changes to i t and annotate i t wi th the Pl ot
Edi tor, whi ch i s an easy-to-use graphi cal i nterface. The fol l owi ng i l l ustrati on
3 2 1 0 1 2 3
1
0.8
0.6
0.4
0.2
0
0.2
0.4
0.6
0.8
1
t
s
i
n
(
t
)
Graph of the sine function
Note the odd symmetry.
4 G raphics
4-14
shows the pl ot i n a fi gure wi ndow and l abel s the mai n features of the fi gure
wi ndow and the Pl ot Edi tor.
To save a fi gure, sel ect Savefrom the Filemenu. To save i t usi ng a graphi cs
format, such as TI FF, for use wi th other appl i cati ons, sel ect Export from the
Filemenu. You can al so save from the command l i ne use the s av eas
command, i ncl udi ng any opti ons to save the fi gure i n a di fferent format.
UsetheTools
Menu to accessPlot
Editor features
Click thisbutton to
start Plot Editor
mode
Annotate
theplot
Get helpfor
thePlot
Editor
Zoomandrotatethe
plot
M esh and Surface Plots
4-15
Mesh and Surface Plots
MATLAB defi nes a surface by the z-coordi nates of poi nts above a gri d i n the x-y
pl ane, usi ng strai ght l i nes to connect adjacent poi nts. The mes h and s ur f
pl otti ng functi ons di spl ay surfaces i n three di mensi ons. mes h produces
wi reframe surfaces that col or onl y the l i nes connecti ng the defi ni ng poi nts.
s ur f di spl ays both the connecti ng l i nes and the faces of the surface i n col or.
Visualizing Functions of Two Variables
To di spl ay a functi on of two vari abl es, z = f (x,y):
Generate X and Y matri ces consi sti ng of repeated rows and col umns,
respecti vel y, over the domai n of the functi on.
Use X and Y to eval uate and graph the functi on.
The me s hgr i d functi on transfor ms the domai n speci fi ed by a si ngl e vector or
two vectors x and y i nto matri ces Xand Yfor use i n eval uati ng functi ons of two
vari abl es. The rows of X are copi es of the vector x and the col umns of Y are
copi es of the vector y .
Ex a mple Gr a phing the sinc Function
Thi s exampl e eval uates and graphs the two-di mensi onal sincfuncti on, si n(r)/r,
between the xand ydi recti ons. R i s the di stance from ori gi n, whi ch i s at the
center of the matri x. Addi ng eps (a MATLAB command that returns the
smal l est fl oati ng-poi nt number on your system) avoi ds the i ndetermi nate 0/0
at the ori gi n.
[ X, Y] = mes hgr i d( - 8: . 5: 8) ;
R = s qr t ( X. ^2 + Y. ^2) + e ps ;
Z = s i n( R) . /R;
mes h( X, Y, Z, ' EdgeCol or ' , ' bl ack' )
4 G raphics
4-16
By defaul t, MATLAB col ors the mesh usi ng the current col ormap. However,
thi s exampl e uses a si ngl e-col ored mesh by speci fyi ng the EdgeCol or surface
property. See the s ur f ace reference page for a l i st of al l surface properti es.
You can create a transparent mesh by di sabl i ng hi dden l i ne removal .
hi dden of f
See the hi dden reference page for more i nformati on on thi s opti on.
Ex a mple Colored Sur fa ce Plots
A surface pl ot i s si mi l ar to a mesh pl ot except the rectangul ar faces of the
surface are col ored. The col or of the faces i s determi ned by the val ues of Z and
the col ormap (a c ol or map i s an ordered l i st of col ors). These statements graph
the si nc functi on as a surface pl ot, sel ect a col ormap, and add a col or bar to
show the mappi ng of data to col or.
s ur f ( X, Y, Z)
col or map hs v
col or bar
10
5
0
5
10
10
5
0
5
10
0.5
0
0.5
1
M esh and Surface Plots
4-17
See the col or map reference page for i nformati on on col ormaps.
For More Information See the Creati ng 3-D Graphs chapter i n Using
MATLAB Graphics for more i nformati on on surface pl ots. Access Using
MATLAB Graphics from the Hel p Desk.
Sur fa ce Plots w ith Lighting
Li ghti ng i s the techni que of i l l umi nati ng an object wi th a di recti onal l i ght
source. I n certai n cases, thi s techni que can make subtl e di fferences i n surface
shape easi er to see. Li ghti ng can al so be used to add real i sm to
three-di mensi onal graphs.
Thi s exampl e uses the same surface as the previ ous exampl es, but col ors i t red
and removes the mesh l i nes. A l i ght object i s then added to the l eft of the
camera (that i s the l ocati on i n space from where you are vi ewi ng the surface).
0.2
0
0.2
0.4
0.6
0.8
1
10
5
0
5
10
10
5
0
5
10
0.5
0
0.5
1
4 G raphics
4-18
After addi ng the l i ght and setti ng the l i ghti ng method to phong, use the v i ew
command to change the vi ew poi nt so you are l ooki ng at the surface from a
di fferent poi nt i n space (an azi muth of -15 and an el evati on of 65 degrees).
Fi nal l y, zoom i n on the surface usi ng the tool bar zoom mode.
s ur f ( X, Y, Z, ' FaceCol or ' , ' r ed' , ' EdgeCol or ' , ' none' ) ;
caml i ght l ef t ; l i ght i ng phong
v i ew( - 15, 65)
For More Information See the Li ghti ng as a Vi sual i zati on Tool and
Defi ni ng the Vi ew chapters i n UsingMATLAB Graphics for i nformati on on
these techni ques. Access UsingMATLAB Graphics fr om the Hel p Desk.
Im ages
4-19
Images
Two-di mensi onal arrays can be di spl ayed as images, where the array el ements
determi ne bri ghtness or col or of the i mages. For exampl e, the statements
l oad dur er
whos
Name Si z e By t es Cl as s
X 648x 509 2638656 doubl e ar r ay
c apt i on 2x 28 112 c har ar r ay
map 128x 3 3072 doubl e ar r ay
l oad the fi l e dur er . mat , addi ng three vari abl es to the workspace. The matri x X
i s a 648-by-509 matri x and map i s a 128-by-3 matri x that i s the col ormap for thi s
i mage.
NoteMAT-fi l es, such as dur er . mat , are bi nary fi l es that can be created on one
pl atform and l ater read by MATLAB on a di fferent pl atform.
The el ements of X are i ntegers between 1 and 128, whi ch serve as i ndi ces i nto
the col ormap, map. Then
i mage( X)
col or map( map)
ax i s i mage
reproduces Drers etchi ng shown at the begi nni ng of thi s book. A hi gh
resol uti on scan of the magi c square i n the upper ri ght corner i s avai l abl e i n
another fi l e. Type
l oad det ai l
and then use the uparrow key on your keyboard to reexecute the i mage,
col or map, and ax i s commands. The statement
col or map( hot )
adds some twenti eth century col ori zati on to the si xteenth century etchi ng. The
functi on hot generates a col ormap contai ni ng shades of reds, oranges, and
4 G raphics
4-20
yel l ows. Typi cal l y a gi ven i mage matri x has a speci fi c col ormap associ ated wi th
i t. See the col or map reference page for a l i st of other predefi ned col ormaps.
For More Information See the Di spl ayi ng Bi t-Mapped I mages chapter i n
UsingMATLAB Graphics for i nformati on the i mage processi ng capabi l i ti es of
MATLAB. Access UsingMATLAB Graphics from the Hel p Desk.
Printing G raphics
4-21
Printing Graphics
You can pri nt a MATLAB fi gure di rectl y on a pri nter connected to your
computer or you can export the fi gure to one of the standard graphi c fi l e
formats supported by MATLAB. There are two ways to pri nt and export
fi gures:
Usi ng the Print opti on under the Filemenu
Usi ng the pr i nt command
Pr inting fr om the M enu
There are four menu opti ons under the Filemenu that pertai n to pri nti ng:
The PageSetup opti on di spl ays a di al og box that enabl es you to adjust
characteri sti cs of the fi gure on the pri nted page.
The Print Setupopti on di spl ays a di al og box that sets pri nti ng defaul ts, but
does not actual l y pri nt the fi gure.
The Print Previewopti on enabl es you to vi ew the fi gure the way i t wi l l l ook
on the pri nted page.
The Print opti on di spl ays a di al og box that l ets you sel ect standard pri nti ng
opti ons and pri nt the fi gure.
Gener al l y, use Print Previewto determi ne whether the pri nted output i s what
you want. I f not, use the PageSetup di al og box to change the output setti ngs.
The PageSetup di al og box Helpbutton di spl ays i nformati on on how to set up
the page.
Ex por ting Figur e to Gra phics Files
The Export opti on under the Filemenu enabl es you to export the fi gure to a
vari ety of standard graphi cs fi l e formats.
Using the Print Comma nd
The pr i nt command provi des more fl exi bi l i ty i n the type of output sent to the
pri nter and al l ows you to control pri nti ng from M-fi l es. The resul t can be sent
di rectl y to your defaul t pri nter or stored i n a speci fi ed fi l e. A wi de vari ety of
output formats, i ncl udi ng TI FF, JPEG, and PostScri pt, i s avai l abl e.
For exampl e, thi s statement saves the contents of the current fi gure wi ndow as
col or Encapsul ated Level 2 PostScri pt i n the fi l e cal l ed magi cs quar e. eps . I t
4 G raphics
4-22
al so i ncl udes a TI FF previ ew, whi ch enabl es most word processors to di spl ay
the pi cture
pr i nt - deps c2 - t i f f magi cs quar e . eps
To save the same fi gure as a TI FF fi l e wi th a resol uti on of 200 dpi , use the
command
pr i nt - dt i f f - r 200 magi cs quar e. t i f f
I f you type pr i nt on the command l i ne,
pr i nt
MATLAB pri nts the current fi gure on your defaul t pri nter.
For More Information See the pr i nt command reference page and the
Pri nti ng MATLAB Graphi cs chapter i n UsingMATLAB Graphics for more
i nformati on on pri nti ng. Access thi s i nformati on from the Hel p Desk.
H andle G raphics
4-23
Handle Graphics
When you use a pl otti ng command, MATLAB creates the graph usi ng vari ous
graphi cs objects, such as l i nes, text, and surfaces (see Tabl e 4-1 for a compl ete
l i st). Al l graphi cs objects have properti es that control the appearance and
behavi or of the object. MATLAB enabl es you to quer y the val ue of each
pr oper ty and set the val ue of most properti es.
Whenever MATLAB creates a graphi cs object, i t assi gns an i denti fi er (cal l ed a
handl e) to the object. You can use thi s handl e to access the objects properti es.
Handl e Graphi cs i s useful i f you want to:
Modi fy the appearance of graphs.
Create custom pl otti ng commands by wri ti ng M-fi l es that create and
mani pul ate objects di rectl y.
The materi al i n thi s manual concentrates on modi fyi ng the appearance of
graphs. See the Handl e Graphi cs chapter i n UsingMATLAB Graphics for
more i nformati on on programmi ng wi th Handl e Graphi cs.
Graphics Objects
Graphi cs objects are the basi c el ements used to di spl ay graphi cs and user
i nterface el ements. Tabl e 4-1 l i sts the graphi cs objects.
Table 4-1: Handle Graphics Objects
Object Description
Root Top of the hi erarchy correspondi ng to the computer
screen
Fi gure Wi ndow used to di spl ay graphi cs and user i nterfaces
Ui control User i nterface control that executes a functi on i n
response to user i nteracti on
Ui menu User-defi ned fi gure wi ndow menu
Ui contextmenu Pop-up menu i nvoked by r i ght cl i cki ng on a graphi cs
object
4 G raphics
4-24
O bject Hier a r chy
The objects are organi zed i n a tree structured hi erarchy refl ecti ng thei r
i nterdependence. For exampl e, l i ne objects requi re axes objects as a frame of
reference. I n turn, axes objects exi st onl y wi thi n fi gure objects. Thi s di agram
i l l ustrates the tree structure.
Axes Axes for di spl ayi ng graphs i n a fi gure
I mage Two-di mensi onal pi xel -based pi cture
Li ght Li ght sources that affect the col ori ng of patch and
surface objects
Li ne Li ne used by functi ons such as pl ot , pl ot 3, s emi l ogx
Patch Fi l l ed pol ygon wi th edges
Rectangl e Two-di mensi onal shape varyi ng from rectangl es to
oval s
Surface Three-di mensi onal representati on of matri x data
created by pl otti ng the val ue of the data as hei ghts
above the x-y pl ane
Text Character stri ng
Table 4-1: Handle Graphics Objects (Continued)
Object Description
Uimenu
Line
Axes Uicontrol
Image
Figure
Uicontextmenu
Light Surface Patch Text
Root
Rectangle
H andle G raphics
4-25
Crea ting O bjects
Each object has an associ ated functi on that creates the object. These functi ons
have the same name as the objects they create. For exampl e, the t ex t functi on
creates text objects, the f i gur e functi on creates fi gure objects, and so on.
MATLABs hi gh-l evel graphi cs functi ons (l i ke pl ot and s ur f ) cal l the
appropri ate l ow-l evel functi on to draw thei r respecti ve graphi cs.
For More Information See the object creati on functi on reference page for
more i nformati on about the object and a descri pti on of the objects properti es.
Comma nds for Working w ith O bjects
Thi s tabl e l i sts commands commonl y used when worki ng wi th objects.
For More Information See MATLAB Functi ons i n the Hel p Desk for a
descri pti on of each of these functi ons.
Function Purpose
c opy obj Copy graphi cs object
del et e Del ete an object
f i ndobj Fi nd the handl e of objects havi ng speci fi ed property val ues
gca Return the handl e of the current axes
gcf Return the handl e of the current fi gure
gco Return the handl e of the current object
get Query the val ue of an objects properti es
s et Set the val ue of an objects properti es
4 G raphics
4-26
Setting Object Properties
Al l object properti es have defaul t val ues. However, you may fi nd i t useful to
change the setti ngs of some properti es to customi ze your graph. There are two
ways to set object properti es:
Speci fy val ues for properti es when you create the object.
Set the property val ue on an object that al ready exi sts.
You can speci fy object property val ues as arguments to object creati on
functi ons as wel l as wi th pl otti ng functi on, such as pl ot , mes h, and s ur f . You
can use the s et command to modi fy the property val ues of exi sti ng objects.
For More Information See Handl e Graphi cs Properti es i n the Hel p Desk for
a descri pti on of al l object properti es.
Setting Pr oper ties from Plotting Comma nds
Pl otti ng commands that create l i nes or surfaces enabl e you to speci fy property
name/property val ue pai rs as arguments. For exampl e, the command
pl ot ( x , y , ' Li neWi dt h' , 1. 5)
pl ots the data i n the vari abl es x and y usi ng l i nes havi ng a Li neWi dt h property
set to 1.5 poi nts (one poi nt = 1/72 i nch). You can set any l i ne object property
thi s way.
Setting Pr oper ties of Ex isting O bjects
Many pl otti ng commands can al so return the handl es of the objects created so
you can modi fy the objects usi ng the s et command. For exampl e, these
statements pl ot a fi ve-by-fi ve matri x (creati ng fi ve l i nes, one per col umn) and
then set the Mar ker to a square and the Mar ker FaceCol or to green.
h = pl ot ( magi c( 5) ) ;
s et ( h, ' Mar ker ' , ' s ' , Mar ker FaceCol or ' , ' g' )
I n thi s case, h i s a vector contai ni ng fi ve handl es, one for each of the fi ve l i nes
i n the pl ot. The s et statement sets the Mar ker and Mar ker FaceCol or properti es
of al l l i nes to the same val ues.
H andle G raphics
4-27
Setting M ultiple Proper ty Va lues
I f you want to set the properti es of each l i ne to a di fferent val ue, you can use
cel l arrays to store al l the data and pass i t to the s et command. For exampl e,
create a pl ot and save the l i ne handl es.
h = pl ot ( magi c ( 5) ) ;
Suppose you want to add di fferent markers to each l i ne and col or the markers
face col or to the same col or as the l i ne. You need to defi ne two cel l arrays one
contai ni ng the property names and the other contai ni ng the desi red val ues of
the properti es.
The pr op_name cel l array contai ns two el ements.
pr op_name( 1) = {' Mar ker ' };
pr op_name( 2) = {' Mar ker FaceCol or ' };
The pr op_v al ues cel l array contai ns 10 val ues fi ve val ues for the Mar ker
property and fi ve val ues for the Mar ker FaceCol or property. Noti ce that
pr op_v al ues i s a two-di mensi onal cel l array. The fi rst di mensi on i ndi cates
whi ch handl e i n h the val ues appl y to and the second di mensi on i ndi cates
whi ch property the val ue i s assi gned to.
pr op_v al ues ( 1, 1) = {' s ' };
pr op_v al ues ( 1, 2) = {get ( h( 1) , ' Col or ' ) };
pr op_v al ues ( 2, 1) = {' d' };
pr op_v al ues ( 2, 2) = {get ( h( 2) , ' Col or ' ) };
pr op_v al ues ( 3, 1) = {' o' };
pr op_v al ues ( 3, 2) = {get ( h( 3) , ' Col or ' ) };
pr op_v al ues ( 4, 1) = {' p' };
pr op_v al ues ( 4, 2) = {get ( h( 4) , ' Col or ' ) };
pr op_v al ues ( 5, 1) = {' h' };
pr op_v al ues ( 5, 2) = {get ( h( 5) , ' Col or ' ) };
The Mar ker Fac eCol or i s al ways assi gned the val ue of the correspondi ng l i nes
col or (obtai ned by getti ng the l i nes Col or property wi th the ge t command).
After defi ni ng the cel l arrays, cal l s et to speci fy the new property val ues.
s et ( h, pr op_name, pr op_v al ues )
4 G raphics
4-28
For More Information See the Structures and Cel l Arrays chapter i n
UsingMATLAB for i nformati on on cel l arrays. Access UsingMATLAB from
the Hel p Desk.
Finding the Handles of Existing Objects
The f i ndobj command enabl es you to obtai n the handl es of graphi cs objects by
searchi ng for objects wi th parti cul ar property val ues. Wi th f i ndobj you can
speci fy the val ue of any combi nati on of properti es, whi ch makes i t easy to pi ck
one object out of many. For exampl e, you may want to fi nd the bl ue l i ne wi th
square marker havi ng bl ue face col or.
You can al so speci fy whi ch fi gures or axes to search, i f there i s more than one.
The fol l owi ng secti ons provi de exampl es i l l ustrati ng how to use f i ndobj .
1 1.5 2 2.5 3 3.5 4 4.5 5
0
5
10
15
20
25
H andle G raphics
4-29
Finding All O bjects of a Cer ta in Type
Si nce al l objects have a Ty pe property that i denti fi es the type of object, you can
fi nd the handl es of al l occurrences of a parti cul ar type of object. For exampl e,
h = f i ndobj ( ' Ty pe' , ' l i ne' ) ;
fi nds the handl es of al l l i ne objects.
Finding O bjects w ith a Pa r ticula r Proper ty
You can speci fy mul ti pl e properti es to narrow the search. For exampl e,
h = f i ndobj ( ' Ty pe' , ' l i ne' , ' Col or ' , ' r ' , ' Li neSt y l e ' , ' : ' ) ;
fi nds the handl es of al l red, dotted l i nes.
Limiting the Scope of the Sea r ch
You can speci fy the starti ng poi nt i n the object hi erarchy by passi ng the handl e
of the starti ng fi gure or axes as the fi rst argument. For exampl e,
h = f i ndobj ( gc a, ' Ty pe' , ' t ex t ' , ' St r i ng' , ' \pi /2' ) ;
fi nds the stri ng /2 onl y wi thi n the current axes.
Using findobj a s a n Argument
Si nce f i ndobj returns the handl es i t fi nds, you can use i t i n pl ace of the handl e
argument. For exampl e,
s et ( f i ndobj ( ' Ty pe' , ' l i ne' , ' Col or ' , ' r e d' ) , ' Li neSt y l e' , ' : ' )
fi nds al l red l i nes and sets thei r l i ne styl e to dotted.
For More Information See the Accessi ng Object Handl es secti on of the
Handl e Graphi cs chapter i n UsingMATLAB Graphics for more i nformati on.
Access UsingMATLAB Graphics fr om the Hel p Desk.
4 G raphics
4-30
Graphics User Interfaces
Here i s a si mpl e exampl e i l l ustrati ng how to use Handl e Graphi cs to bui l d user
i nterfaces. The statement
b = ui cont r ol ( ' St y l e ' , ' pus hbut t on' , . . .
' Uni t s ' , ' nor mal i z ed' , . . .
' Pos i t i on' , [ . 5 . 5 . 2 . 1] , . . .
' St r i ng' , ' cl i c k her e' ) ;
creates a pushbutton i n the center of a fi gure wi ndow and returns a handl e to
the new object. But, so far, cl i cki ng on the button does nothi ng. The statement
s = ' s et ( b, ' ' Pos i t i on' ' , [ . 8*r and . 9*r and . 2 . 1] ) ' ;
creates a stri ng contai ni ng a command that al ters the pushbuttons posi ti on.
Repeated executi on of
ev al ( s )
moves the button to random posi ti ons. Fi nal l y,
s et ( b, ' Cal l back' , s )
i nstal l s s as the buttons cal l back acti on, so every ti me you cl i ck on the button,
i t moves to a new posi ti on.
Graphical User Interface Design Tools
MATLAB provi des GUI Desi gn Envi ronment (GUI DE) tool s that si mpl i fy the
creati on of graphi cal user i nterfaces. To di spl ay the GUI DE control panel , i ssue
the gui de command.
For More Information Type hel p gui de at the MATLAB command l i ne.
A nim ations
4-31
Animations
MATLAB provi des two ways of generati ng movi ng, ani mated graphi cs:
Conti nual l y erase and then redraw the objects on the screen, maki ng
i ncremental changes wi th each redraw.
Save a number of di fferent pi ctures and then pl ay them back as a movi e.
Erase Mode Method
Usi ng the Er as e Mode property i s appropri ate for l ong sequences of si mpl e pl ots
where the change from frame to frame i s mi ni mal . Here i s an exampl e showi ng
si mul ated Browni an moti on. Speci fy a number of poi nts, such as
n = 20
and a temperature or vel oci ty, such as
s = . 02
The best val ues for these two parameters depend upon the speed of your
parti cul ar computer. Generate n random poi nts wi th (x,y) coordi nates between
-
1
/2 and +
1
/2.
x = r and( n, 1) - 0. 5;
y = r and( n, 1) - 0. 5;
Pl ot the poi nts i n a square wi th si des at -1 and +1. Save the handl e for the
vector of poi nts and set i ts Er as eMode to x or . Thi s tel l s the MATLAB graphi cs
system not to redraw the enti re pl ot when the coordi nates of one poi nt are
changed, but to restore the background col or i n the vi ci ni ty of the poi nt usi ng
an excl usi ve or operati on.
h = pl ot ( x , y , ' . ' ) ;
ax i s ( [ - 1 1 - 1 1] )
ax i s s quar e
gr i d of f
s et ( h, ' Er as eMode' , ' x or ' , ' Mar ker Si z e' , 18)
Now begi n the ani mati on. Here i s an i nfi ni te whi l e l oop, whi ch you can
eventual l y exi t by typi ng Ctrl-c. Each ti me through the l oop, add a smal l
amount of normal l y di stri buted random noi se to the coordi nates of the poi nts.
4 G raphics
4-32
Then, i nstead of creati ng an enti rel y new pl ot, si mpl y change the XDat a and
YDat a properti es of the ori gi nal pl ot.
whi l e 1
dr awnow
x = x + s *r andn( n, 1) ;
y = y + s *r andn( n, 1) ;
s et ( h, ' XDat a' , x , ' YDat a' , y )
end
How l ong does i t take for one of the poi nts to get outsi de of the square? How
l ong before al l of the poi nts are outsi de the square?
Creating Movies
I f you i ncrease the number of poi nts i n the Browni an moti on exampl e to
somethi ng l i ke n = 300 and s = . 02, the moti on i s no l onger very fl ui d; i t takes
too much ti me to draw each ti me step. I t becomes more effecti ve to save a
predetermi ned number of frames as bi tmaps and to pl ay them back as a movie.
1 0.5 0 0.5 1
1
0.8
0.6
0.4
0.2
0
0.2
0.4
0.6
0.8
1
A nim ations
4-33
Fi rst, deci de on the number of frames, say
nf r ames = 50;
Next, set up the fi rst pl ot as before, except usi ng the defaul t Er as eMode
(nor mal ).
x = r and( n, 1) - 0. 5;
y = r and( n, 1) - 0. 5;
h = pl ot ( x , y , ' . ' ) ;
s et ( h, ' Mar ker Si z e' , 18) ;
ax i s ( [ - 1 1 - 1 1] )
ax i s s quar e
gr i d of f
Generate the movi e and use get f r ame to capture each frame.
f or k = 1: nf r ames
x = x + s *r andn( n, 1) ;
y = y + s *r andn( n, 1) ;
s et ( h, ' XDat a' , x , ' YDat a' , y )
M( k) = get f r ame;
end
Fi nal l y, pl ay the movi e 30 ti mes.
mov i e( M, 30)
4 G raphics
4-34
5
Programmi ng
wi th MATLAB
FlowControl . . . . . . . . . . . . . . . . . . . . 5-2
i f . . . . . . . . . . . . . . . . . . . . . . . . . 5-2
swi tch and case . . . . . . . . . . . . . . . . . . . 5-4
for . . . . . . . . . . . . . . . . . . . . . . . . . 5-4
whi l e . . . . . . . . . . . . . . . . . . . . . . . . 5-5
break . . . . . . . . . . . . . . . . . . . . . . . 5-5
Other Data Structures . . . . . . . . . . . . . . . 5-7
Mul ti di mensi onal Arrays . . . . . . . . . . . . . . . 5-7
Cel l Arrays . . . . . . . . . . . . . . . . . . . . . 5-9
Characters and Text . . . . . . . . . . . . . . . . . 5-11
Structures . . . . . . . . . . . . . . . . . . . . . 5-14
Scripts and Functions . . . . . . . . . . . . . . . 5-17
Scri pts . . . . . . . . . . . . . . . . . . . . . . . 5-17
Functi ons . . . . . . . . . . . . . . . . . . . . . . 5-19
Gl obal Vari abl es . . . . . . . . . . . . . . . . . . . 5-20
Passi ng Stri ng Arguments to Functi ons . . . . . . . . . 5-21
The eval Functi on . . . . . . . . . . . . . . . . . . 5-22
Vectori zati on . . . . . . . . . . . . . . . . . . . . 5-23
Preal l ocati on . . . . . . . . . . . . . . . . . . . . 5-23
Functi on Functi ons . . . . . . . . . . . . . . . . . . 5-24
Demonstration Programs Included with MATLAB . . . 5-27
5 Program m ing w ith M A TLA B
5-2
Flow Control
MATLAB has several fl ow control constructs:
i f statements
s wi t ch statements
f or l oops
whi l e l oops
br eak statements
For More InformationUsingMATLAB di scusses programmi ng i n MATLAB.
Access UsingMATLAB from the Hel p Desk.
if
The i f statement eval uates a l ogi cal expressi on and executes a group of
statements when the expressi on i s true. The opti onal el s ei f and el s e
keywords provi de for the executi on of al ternate groups of statements. An end
keyword, whi ch matches the i f , termi nates the l ast group of statements. The
groups of statements are del i neated by the four keywords no braces or
brackets are i nvol ved.
MATLABs al gori thm for generati ng a magi c square of order n i nvol ves three
di fferent cases: when n i s odd, when n i s even but not di vi si bl e by 4, or when n
i s di vi si bl e by 4. Thi s i s descri bed by
i f r em( n, 2) ~= 0
M = odd_magi c( n)
el s ei f r em( n, 4) ~= 0
M = s i ngl e_ev en_magi c( n)
el s e
M = doubl e_ev en_magi c( n)
end
I n thi s exampl e, the three cases are mutual l y excl usi ve, but i f they werent, the
fi rst truecondi ti on woul d be executed.
Flow C ontrol
5-3
I t i s i mportant to understand how rel ati onal operators and i f statements work
wi th matri ces. When you want to check for equal i ty between two vari abl es, you
mi ght use
i f A == B, . . .
Thi s i s l egal MATLAB code, and does what you expect when Aand Bare scal ars.
But when A and B are matri ces, A == B does not test if they are equal , i t tests
wherethey are equal ; the resul t i s another matri x of 0s and 1s showi ng
el ement-by-el ement equal i ty. I n fact, i f A and B ar e not the same si ze, then
A == B i s an error.
The proper way to check for equal i ty between two vari abl es i s to use the
i s equal functi on,
i f i s equal ( A, B) , . . .
Here i s another exampl e to emphasi ze thi s poi nt. I f A and B are scal ars, the
fol l owi ng program wi l l never reach the unexpected si tuati on. But for most
pai rs of matri ces, i ncl udi ng our magi c squares wi th i nterchanged col umns,
none of the matri x condi ti ons A > B, A < B or A == B i s true for all el ements
and so the el s e cl ause i s executed.
i f A > B
' gr eat er '
el s ei f A < B
' l es s '
el s ei f A == B
' equal '
el s e
er r or ( ' Unex pect ed s i t uat i on' )
end
Several functi ons are hel pful for reduci ng the resul ts of matri x compari sons to
scal ar condi ti ons for use wi th i f , i ncl udi ng
i s e qual
i s e mpt y
al l
any
5 Program m ing w ith M A TLA B
5-4
switch and case
The s wi t ch statement executes groups of statements based on the val ue of a
vari abl e or expressi on. The keywords cas e and ot her wi s e del i neate the
groups. Onl y the fi rst matchi ng case i s executed. There must al ways be an end
to match the s wi t ch.
The l ogi c of the magi c squares al gori thm can al so be descri bed by
s wi t ch ( r em( n, 4) ==0) + ( r em( n, 2) ==0)
cas e 0
M = odd_magi c ( n)
cas e 1
M = s i ngl e_ev en_magi c( n)
cas e 2
M = doubl e_ev en_magi c( n)
ot he r wi s e
e r r or ( ' Thi s i s i mpos s i bl e' )
end
Note for C Programmers Unl i ke the C l anguage s wi t ch statement,
MATLABs s wi t ch does not fal l through. I f the fi rst case statement i s true, the
other case statements do not execute. So, br eak statements are not requi red.
for
The f or l oop repeats a group of statements a fi xed, predetermi ned number of
ti mes. A matchi ng end del i neates the statements.
f or n = 3: 32
r ( n) = r ank( magi c ( n) ) ;
end
r
The semi col on termi nati ng the i nner statement suppresses repeated pri nti ng,
and the r after the l oop di spl ays the fi nal resul t.
Flow C ontrol
5-5
I t i s a good i dea to i ndent the l oops for readabi l i ty, especi al l y when they are
nested.
f or i = 1: m
f or j = 1: n
H( i , j ) = 1/( i +j ) ;
end
end
while
The whi l e l oop repeats a group of statements an i ndefi ni te number of ti mes
under control of a l ogi cal condi ti on. A matchi ng end del i neates the statements.
Here i s a compl ete program, i l l ustrati ng whi l e , i f , el s e , and end, that uses
i nterval bi secti on to fi nd a zero of a pol ynomi al .
a = 0; f a = - I nf ;
b = 3; f b = I nf ;
whi l e b- a > eps *b
x = ( a+b) /2;
f x = x ^3- 2*x - 5;
i f s i gn( f x ) == s i gn( f a)
a = x ; f a = f x ;
el s e
b = x ; f b = f x ;
end
end
x
The resul t i s a root of the pol ynomi al x
3
- 2x- 5, namel y
x =
2. 09455148154233
The cauti ons i nvol vi ng matri x compari sons that are di scussed i n the secti on on
the i f statement al so appl y to the whi l e statement.
break
The br eak statement l ets you exi t earl y from a f or or whi l e l oop. I n nested
l oops, br e ak exi ts fr om the i nner most l oop onl y.
5 Program m ing w ith M A TLA B
5-6
Here i s an i mprovement on the exampl e from the previ ous secti on. Why i s thi s
use of br eak a good i dea?
a = 0; f a = - I nf ;
b = 3; f b = I nf ;
whi l e b- a > eps *b
x = ( a+b) /2;
f x = x ^3- 2*x - 5;
i f f x == 0
br eak
el s ei f s i gn( f x ) == s i gn( f a)
a = x ; f a = f x ;
el s e
b = x ; f b = f x ;
end
end
x
O ther D ata Structures
5-7
Other Data Structures
Thi s secti on i ntroduces you to some other data structures i n MATLAB,
i ncl udi ng:
Mul ti di mensi onal arrays
Cel l arrays
Characters and text
Structures
For More Information For a compl ete di scussi on of MATLABs data
structures, see UsingMATLAB, whi ch i s accessi bl e from the Hel p Desk.
Multidimensional Arrays
Mul ti di mensi onal arrays i n MATLAB are arrays wi th more than two
subscri pts. They can be created by cal l i ng z er os , ones , r and, or r andn wi th
more than two arguments. For exampl e,
R = r andn( 3, 4, 5) ;
creates a 3-by-4-by-5 array wi th a total of 3x4x5 = 60 normal l y di stri buted
random el ements.
A three-di mensi onal array mi ght represent three-di mensi onal physi cal data,
say the temperature i n a room, sampl ed on a rectangul ar gri d. Or, i t mi ght
represent a sequence of matri ces, A
(k)
, or sampl es of a ti me-dependent matri x,
A(t). I n these l atter cases, the (i, j)th el ement of the kth matri x, or the t
k
th
matri x, i s denoted by A( i , j , k) .
MATLABs and Drers versi ons of the magi c square of order 4 di ffer by an
i nterchange of two col umns. Many di fferent magi c squares can be generated by
i nterchangi ng col umns. The statement
p = per ms ( 1: 4) ;
5 Program m ing w ith M A TLA B
5-8
generates the 4! = 24 permutati ons of 1: 4. The kth permutati on i s the row
vector, p( k, : ) . Then
A = magi c( 4) ;
M = z er os ( 4, 4, 24) ;
f or k = 1: 24
M( : , : , k) = A( : , p( k, : ) ) ;
end
stores the sequence of 24 magi c squares i n a three-di mensi onal array, M. The
si ze of Mi s
s i z e( M)
ans =
4 4 24
I t turns out that the 22nd matri x i n the sequence i s Drers:
M( : , : , 22)
ans =
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
13 2 3 16
8 11 10 5
12 7 6 9
1 14 15 4
3 13 2 16
10 8 11 5
6 12 7 9
15 1 14 4
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
13 3 2 16
8 10 11 5
12 6 7 9
1 15 14 4
.
.
.
O ther D ata Structures
5-9
The statement
s um( M, d)
computes sums by varyi ng the dth subscri pt. So
s um( M, 1)
i s a 1-by-4-by-24 array contai ni ng 24 copi es of the row vector
34 34 34 34
and
s um( M, 2)
i s a 4-by-1-by-24 array contai ni ng 24 copi es of the col umn vector
34
34
34
34
Fi nal l y,
S = s um( M, 3)
adds the 24 matri ces i n the sequence. The resul t has si ze 4-by-4-by-1, so i t l ooks
l i ke a 4-by-4 array,
S =
204 204 204 204
204 204 204 204
204 204 204 204
204 204 204 204
Cell Arrays
Cel l arrays i n MATLAB are mul ti di mensi onal arrays whose el ements are
copi es of other arrays. A cel l array of empty matri ces can be created wi th the
cel l functi on. But, more often, cel l arrays are created by encl osi ng a
mi scel l aneous col l ecti on of thi ngs i n curl y braces, {}. The curl y braces are al so
used wi th subscri pts to access the contents of vari ous cel l s. For exampl e,
C = {A s um( A) pr od( pr od( A) ) }
5 Program m ing w ith M A TLA B
5-10
produces a 1-by-3 cel l array. The three cel l s contai n the magi c square, the row
vector of col umn sums, and the product of al l i ts el ements. When Ci s di spl ayed,
you see
C =
[ 4x 4 doubl e] [ 1x 4 doubl e ] [ 20922789888000]
Thi s i s because the fi rst two cel l s are too l arge to pri nt i n thi s l i mi ted space, but
the thi rd cel l contai ns onl y a si ngl e number, 16!, so there i s room to pri nt i t.
Here are two i mportant poi nts to remember. Fi rst, to retri eve the contents of
one of the cel l s, use subscri pts i n curl y braces. For exampl e, C{1} retri eves the
magi c square and C{3} i s 16!. Second, cel l arrays contai n copiesof other arrays,
not pointers to those arrays. I f you subsequentl y change A, nothi ng happens to
C.
Three-di mensi onal arrays can be used to store a sequence of matri ces of the
samesi ze. Cel l arrays can be used to store a sequence of matri ces of different
si zes. For exampl e,
M = cel l ( 8, 1) ;
f or n = 1: 8
M{n} = magi c( n) ;
end
M
produces a sequence of magi c squares of di fferent order,
M =
[ 1]
[ 2x 2 doubl e]
[ 3x 3 doubl e]
[ 4x 4 doubl e]
[ 5x 5 doubl e]
[ 6x 6 doubl e]
[ 7x 7 doubl e]
[ 8x 8 doubl e]
O ther D ata Structures
5-11
You can retri eve our ol d fri end wi th
M{4}
Characters and Text
Enter text i nto MATLAB usi ng si ngl e quotes. For exampl e,
s = ' Hel l o'
The resul t i s not the same ki nd of numeri c matri x or array we have been
deal i ng wi th up to now. I t i s a 1-by-5 character array.
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
.
.
.
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
1 3
4 2
8 1 6
3 5 7
4 9 2
1
5 Program m ing w ith M A TLA B
5-12
I nternal l y, the characters are stored as numbers, but not i n fl oati ng-poi nt
format. The statement
a = doubl e( s )
converts the character array to a numeri c matri x contai ni ng fl oati ng-poi nt
representati ons of the ASCI I codes for each character. The resul t i s
a =
72 101 108 108 111
The statement
s = char ( a)
reverses the conversi on.
Converti ng numbers to characters makes i t possi bl e to i nvesti gate the vari ous
fonts avai l abl e on your computer. The pri ntabl e characters i n the basi c ASCI I
character set are represented by the i ntegers 32: 127. (The i ntegers l ess than
32 represent nonpri ntabl e control characters.) These i ntegers are arranged i n
an appropri ate 6-by-16 array wi th
F = r es hape( 32: 127, 16, 6) ' ;
The pri ntabl e characters i n the extended ASCI I character set are represented
by F+128. When these i ntegers are i nterpreted as characters, the resul t
depends on the font currentl y bei ng used. Type the statements
char ( F)
char ( F+128)
and then vary the font bei ng used for the MATLAB command wi ndow. On a PC,
sel ect Preferences under the Filemenu. Be sure to try the Symbol and
O ther D ata Structures
5-13
Wingdings fonts, i f you have them on your computer. Here i s one exampl e of
the ki nd of output you mi ght obtai n.
! "#$%&' ( ) *+, - . /
0123456789: ; <=>?
@ABCDEFGHI JKLMNO
PQRSTUVWXYZ[ \] ^_
`abcdef ghi j kl mno
pqr s t uv wx y z {| }~-
- ??
``
Concatenati on wi th square brackets joi ns text vari abl es together i nto l arger
stri ngs. The statement
h = [ s , ' wor l d' ]
joi ns the stri ngs hori zontal l y and produces
h =
Hel l o wor l d
The statement
v = [ s ; ' wor l d' ]
joi ns the stri ngs verti cal l y and produces
v =
Hel l o
wor l d
Note that a bl ank has to be i nserted before the ' w' i n h and that both words i n
v have to have the same l ength. The resul ti ng arrays are both character arrays;
h i s 1-by-11 and v i s 2-by-5.
To mani pul ate a body of text contai ni ng l i nes of di fferent l engths, you have two
choi ces a padded character array or a cel l array of stri ngs. The char functi on
accepts any number of l i nes, adds bl anks to each l i ne to make them al l the
5 Program m ing w ith M A TLA B
5-14
same l ength, and forms a character array wi th each l i ne i n a separate row. For
exampl e,
S = char ( ' A' , ' r ol l i ng' , ' s t one' , ' gat her s ' , ' moment um. ' )
produces a 5-by-9 character array
S =
A
r ol l i ng
s t one
gat her s
moment um.
There are enough bl anks i n each of the fi rst four rows of S to make al l the rows
the same l ength. Al ternati vel y, you can store the text i n a cel l array. For
exampl e,
C = {' A' ; ' r ol l i ng' ; ' s t one' ; ' gat her s ' ; ' mome nt um. ' }
i s a 5-by-1 cel l array
C =
' A'
' r ol l i ng'
' s t one'
' gat her s '
' moment um. '
You can convert a padded character array to a cel l array of stri ngs wi th
C = cel l s t r ( S)
and reverse the process wi th
S = char ( C)
Structures
Structures are mul ti di mensi onal MATLAB arrays wi th el ements accessed by
textual field designators. For exampl e,
S. name = ' Ed Pl um' ;
S. s cor e = 83;
S. gr ade = ' B+'
O ther D ata Structures
5-15
creates a scal ar structure wi th three fi el ds.
S =
name: ' Ed Pl um'
s cor e: 83
gr ade: ' B+'
Li ke everythi ng el se i n MATLAB, structures are arrays, so you can i nsert
addi ti onal el ements. I n thi s case, each el ement of the array i s a structure wi th
several fi el ds. The fi el ds can be added one at a ti me,
S( 2) . name = ' Toni Mi l l er ' ;
S( 2) . s cor e = 91;
S( 2) . gr ade = ' A- ' ;
or, an enti re el ement can be added wi th a si ngl e statement.
S( 3) = s t r uct ( ' name' , ' Jer r y Gar ci a' , . . .
' s cor e' , 70, ' gr ade' , ' C' )
Now the structure i s l arge enough that onl y a summary i s pri nted.
S =
1x 3 s t r uct ar r ay wi t h f i e l ds :
name
s cor e
gr ade
There are several ways to reassembl e the vari ous fi el ds i nto other MATLAB
ar r ays. They ar e al l based on the notati on of a commaseparated list. I f you type
S. s cor e
i t i s the same as typi ng
S( 1) . s cor e, S( 2) . s cor e, S( 3) . s cor e
Thi s i s a comma separated l i st. Wi thout any other punctuati on, i t i s not very
useful . I t assi gns the three scores, one at a ti me, to the defaul t vari abl e ans and
duti ful l y pri nts out the resul t of each assi gnment. But when you encl ose the
expressi on i n square brackets,
[ S. s cor e]
5 Program m ing w ith M A TLA B
5-16
i t i s the same as
[ S( 1) . s c or e, S( 2) . s c or e, S( 3) . s cor e]
whi ch produces a numeri c row vector contai ni ng al l of the scores.
ans =
83 91 70
Si mi l arl y, typi ng
S. name
just assi gns the names, one at ti me, to ans . But encl osi ng the expressi on i n
curl y braces,
{S. name}
creates a 1-by-3 cel l array contai ni ng the three names.
ans =
' Ed Pl um' ' Toni Mi l l er ' ' Jer r y Gar ci a'
And
char ( S. name)
cal l s the char functi on wi th three arguments to create a character array from
the name fi el ds,
ans =
Ed Pl um
Toni Mi l l er
Jer r y Gar ci a
Scripts and Functions
5-17
Scripts and Functions
MATLAB i s a powerful programmi ng l anguage as wel l as an i nteracti ve
computati onal envi ronment. Fi l es that contai n code i n the MATLAB l anguage
are cal l ed M-fi l es. You create M-fi l es usi ng a text edi tor, then use them as you
woul d any other MATLAB functi on or command.
There are two ki nds of M-fi l es:
Scri pts, whi ch do not accept i nput arguments or return output arguments.
They operate on data i n the workspace.
Functi ons, whi ch can accept i nput arguments and return output arguments.
I nternal vari abl es are l ocal to the functi on.
I f youre a new MATLAB programmer, just create the M-fi l es that you want to
try out i n the current di rectory. As you devel op more of your own M-fi l es, you
wi l l want to organi ze them i nto other di rectori es and personal tool boxes that
you can add to MATLABs search path.
I f you dupl i cate functi on names, MATLAB executes the one that occurs fi rst i n
the search path.
To vi ew the contents of an M-fi l e, for exampl e, my f unct i on. m, use
t y pe my f unct i on
Scripts
When you i nvoke a script, MATLAB si mpl y executes the commands found i n
the fi l e. Scri pts can operate on exi sti ng data i n the workspace, or they can
create new data on whi ch to operate. Al though scri pts do not return output
arguments, any vari abl es that they create remai n i n the workspace, to be used
i n subsequent computati ons. I n addi ti on, scri pts can produce graphi cal output
usi ng functi ons l i ke pl ot .
5 Program m ing w ith M A TLA B
5-18
For exampl e, create a fi l e cal l ed magi cr ank. mthat contai ns these MATLAB
commands.
% I nv es t i gat e t he r ank of magi c s quar es
r = z er os ( 1, 32) ;
f or n = 3: 32
r ( n) = r ank( magi c ( n) ) ;
end
r
bar ( r )
Typi ng the statement
magi cr ank
causes MATLAB to execute the commands, compute the rank of the fi rst 30
magi c squares, and pl ot a bar graph of the resul t. After executi on of the fi l e i s
compl ete, the vari abl es n and r remai n i n the workspace.
0 5 10 15 20 25 30 35
0
5
10
15
20
25
30
35
Scripts and Functions
5-19
Functions
Functi ons are M-fi l es that can accept i nput arguments and return output
arguments. The name of the M-fi l e and of the functi on shoul d be the same.
Functi ons operate on vari abl es wi thi n thei r own workspace, separate from the
workspace you access at the MATLAB command prompt.
A good exampl e i s provi ded by r ank. The M-fi l e r ank. mi s avai l abl e i n the
di rectory
t ool box /mat l ab/mat f un
You can see the fi l e wi th
t y pe r ank
Here i s the fi l e.
f unct i on r = r ank( A, t ol )
% RANK Mat r i x r ank.
% RANK( A) pr ov i des an e s t i mat e of t he number of l i near l y
% i ndependent r ows or c ol umns of a mat r i x A.
% RANK( A, t ol ) i s t he number of s i ngul ar v al ues of A
% t hat ar e l ar ger t han t ol .
% RANK( A) us es t he def aul t t ol = max ( s i z e( A) ) * nor m( A) * eps .
s = s v d( A) ;
i f nar gi n==1
t ol = max ( s i z e( A) ' ) * max ( s ) * eps ;
end
r = s um( s > t ol ) ;
The fi rst l i ne of a functi on M-fi l e starts wi th the keyword f unct i on. I t gi ves the
functi on name and order of arguments. I n thi s case, there are up to two i nput
arguments and one output argument.
The next several l i nes, up to the fi rst bl ank or executabl e l i ne, are comment
l i nes that provi de the hel p text. These l i nes are pri nted when you type
hel p r ank
The fi rst l i ne of the hel p text i s the H1 l i ne, whi ch MATLAB di spl ays when you
use the l ookf or command or request hel p on a di rectory.
5 Program m ing w ith M A TLA B
5-20
The rest of the fi l e i s the executabl e MATLAB code defi ni ng the functi on. The
vari abl e s i ntroduced i n the body of the functi on, as wel l as the vari abl es on the
fi rst l i ne, r , A and t ol , are al l local to the functi on; they are separate from any
vari abl es i n the MATLAB workspace.
Thi s exampl e i l l ustrates one aspect of MATLAB functi ons that i s not ordi nari l y
found i n other programmi ng l anguages a vari abl e number of arguments. The
r ank functi on can be used i n several di fferent ways.
r ank( A)
r = r ank( A)
r = r ank( A, 1. e- 6)
Many M-fi l es work thi s way. I f no output argument i s suppl i ed, the resul t i s
stored i n ans . I f the second i nput argument i s not suppl i ed, the functi on
computes a defaul t val ue. Wi thi n the body of the functi on, two quanti ti es
named nar gi n and nar gout are avai l abl e whi ch tel l you the number of i nput
and output arguments i nvol ved i n each parti cul ar use of the functi on. The r ank
functi on uses nar gi n, but does not need to use nar gout .
Global Variables
I f you want more than one functi on to share a si ngl e copy of a vari abl e, si mpl y
decl are the vari abl e as gl obal i n al l the functi ons. Do the same thi ng at the
command l i ne i f you want the base workspace to access the vari abl e. The gl obal
decl arati on must occur before the vari abl e i s actual l y used i n a functi on.
Al though i t i s not requi red, usi ng capi tal l etters for the names of gl obal
vari abl es hel ps di sti ngui sh them from other vari abl es. For exampl e, create an
M-fi l e cal l ed f al l i ng. m.
f unct i on h = f al l i ng( t )
gl obal GRAVI TY
h = 1/2*GRAVI TY*t . ^2;
Then i nteracti vel y enter the statements
gl obal GRAVI TY
GRAVI TY = 32;
y = f al l i ng( ( 0: . 1: 5) ' ) ;
The two gl obal statements make the val ue assi gned to GRAVI TY at the
command prompt avai l abl e i nsi de the functi on. You can then modi fy GRAVI TY
i nteracti vel y and obtai n new sol uti ons wi thout edi ti ng any fi l es.
Scripts and Functions
5-21
Passing String Arguments to Functions
You can wri te MATLAB functi ons that accept stri ng arguments wi thout the
parentheses and quotes. That i s, MATLAB i nterprets
f oo a b c
as
f oo( ' a' , ' b' , ' c ' )
However, when usi ng the unquoted form, MATLAB cannot return output
arguments. For exampl e,
l egend appl es or anges
creates a l egend on a pl ot usi ng the stri ngs appl e s and or anges as l abel s. I f you
want the l egend command to return i ts output arguments, then you must use
the quoted form.
[ l e gh, obj h] = l egend( ' appl es ' , ' or ange s ' ) ;
I n addi ti on, you cannot use the unquoted form i f any of the arguments are not
stri ngs.
Building Str ings on the Fly
The quoted form enabl es you to construct stri ng arguments wi thi n the code.
The fol l owi ng exampl e processes mul ti pl e data fi l es, Augus t 1. dat ,
Augus t 2. dat , and so on. I t uses the functi on i nt 2s t r , whi ch converts an
i nteger to a character, to bui l d the fi l ename.
f or d = 1: 31
s = [ ' Augus t ' i nt 2s t r ( d) ' . dat ' ] ;
l oad( s )
% Code t o pr oces s t he cont ent s of t he d- t h f i l e
end
5 Program m ing w ith M A TLA B
5-22
A Ca utiona r y N ote
Whi l e the unquoted syntax i s conveni ent, i t can be used i ncorrectl y wi thout
causi ng MATLAB to generate an error. For exampl e, gi ven a matri x A,
A =
0 - 6 - 1
6 2 - 16
- 5 20 - 10
The ei g command returns the ei genval ues of A.
ei g( A)
ans =
- 3. 0710
- 2. 4645+17. 6008i
- 2. 4645- 17. 6008i
The fol l owi ng statement i s not al l owed because A i s not a stri ng, however
MATLAB does not generate an error.
ei g A
ans =
65
MATLAB actual l y takes the ei genval ues of ASCI I numeri c equi val ent of the
l etter A (whi ch i s the number 65).
The eval Function
The ev al functi on works wi th text vari abl es to i mpl ement a powerful text
macro faci l i ty. The expressi on or statement
ev al ( s )
uses the MATLAB i nterpreter to eval uate the expressi on or execute the
statement contai ned i n the text str i ng s .
Scripts and Functions
5-23
The exampl e of the pr evi ous secti on coul d al so be done wi th the fol l owi ng code,
al though thi s woul d be somewhat l ess effi ci ent because i t i nvol ves the ful l
i nterpreter, not just a functi on cal l .
f or d = 1: 31
s = [ ' l oad Augus t ' i nt 2s t r ( d) ' . dat ' ] ;
ev al ( s )
% Pr oces s t he cont ent s of t he d- t h f i l e
end
Vectorization
To obtai n the most speed out of MATLAB, i ts i mportant to vectori ze the
al gori thms i n your M-fi l es. Where other programmi ng l anguages mi ght use f or
or DOl oops, MATLAB can use vector or matri x operati ons. A si mpl e exampl e
i nvol ves creati ng a tabl e of l ogari thms.
x = 0;
f or k = 1: 1001
y ( k) = l og10( x ) ;
x = x + . 01;
end
Experi enced MATLAB users
l i k e t o say Li f e i s t oo short
t o spend w ri t i ng f or l oops.
A vectori zed versi on of the same code i s
x = 0: . 01: 10;
y = l og10( x ) ;
For more compl i cated code, vectori zati on opti ons are not al ways so obvi ous.
When speed i s i mportant, however, you shoul d al ways l ook for ways to
vectori ze your al gori thms.
Preallocation
I f you cant vectori ze a pi ece of code, you can make your f or l oops go faster by
preal l ocati ng any vectors or arrays i n whi ch output resul ts are stored. For
exampl e, thi s code uses the functi on z e r os to preal l ocate the vector created i n
the f or l oop. Thi s makes the f or l oop execute si gni fi cantl y faster.
r = z er os ( 32, 1) ;
f or n = 1: 32
r ( n) = r ank( magi c( n) ) ;
end
5 Program m ing w ith M A TLA B
5-24
Wi thout the preal l ocati on i n the previ ous exampl e, the MATLAB i nterpreter
enl arges the r vector by one el ement each ti me through the l oop. Vector
preal l ocati on el i mi nates thi s step and resul ts i n faster executi on.
Function Functions
A cl ass of functi ons, cal l ed functi on functi ons, works wi th nonl i near functi ons
of a scal ar vari abl e. That i s, one functi on works on another functi on. The
functi on functi ons i ncl ude:
Zero fi ndi ng
Opti mi zati on
Quadrature
Ordi nary di fferenti al equati ons
MATLAB r epresents the nonl i near functi on by a functi on M-fi l e. For exampl e,
here i s a si mpl i fi ed versi on of the functi on humps from the mat l ab/demos
di rectory.
f unct i on y = humps ( x )
y = 1. /( ( x - . 3) . ^2 + . 01) + 1. /( ( x - . 9) . ^2 + . 04) - 6;
Eval uate thi s functi on at a set of poi nts i n the i nterval 0 x 1 wi th
x = 0: . 002: 1;
y = humps ( x ) ;
Then pl ot the functi on wi th
pl ot ( x , y )
Scripts and Functions
5-25
The graph shows that the functi on has a l ocal mi ni mum near x= 0.6. The
functi on f mi ns fi nds the minimizer, the val ue of xwhere the functi on takes on
thi s mi ni mum. The fi rst argument to f mi ns i s the name of the functi on bei ng
mi ni mi zed and the second argument i s a rough guess at the l ocati on of the
mi ni mum.
p = f mi ns ( ' humps ' , . 5)
p =
0. 6370
To eval uate the functi on at the mi ni mi zer,
humps ( p)
ans =
11. 2528
Numeri cal anal ysts use the terms quadratureand integration to di sti ngui sh
between numeri cal approxi mati on of defi ni te i ntegral s and numeri cal
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
0
10
20
30
40
50
60
70
80
90
100
5 Program m ing w ith M A TLA B
5-26
i ntegrati on of ordi nary di fferenti al equati ons. MATLABs quadrature routi nes
are quad and quad8. The statement
Q = quad8( ' humps ' , 0, 1)
computes the area under the curve i n the graph and produces
Q =
29. 8583
Fi nal l y, the graph shows that the functi on i s never zero on thi s i nterval . So, i f
you search for a zero wi th
z = f z er o( ' humps ' , . 5)
you wi l l fi nd one outsi de of the i nterval
z =
- 0. 1316
D em onstration Program s Included w ith M A TLA B
5-27
Demonstration Programs Included with MATLAB
Thi s secti on i ncl udes i nformati on on many of the demonstrati on programs that
are i ncl uded wi th MATLAB.
For More Information The MathWorks Web si te (www. mat hwor ks . c om)
contai ns numerous M-fi l es that have been wri tten by users and MathWorks
staff. These are accessi bl e by sel ecti ng Download M-Files. Al so, Techni cal
Notes, whi ch i s accessi bl e from our Techni cal Support Web si te
(www. mat hwor ks . com/s uppor t ), contai ns numerous exampl es on graphi cs,
mathemati cs, API , Si mul i nk, and others.
There are many programs i ncl uded wi th MATLAB that hi ghl i ght vari ous
features and functi ons. For a compl ete l i st of the demos, at the command
prompt type
hel p demos
To vi ew a speci fi c fi l e, for exampl e, ai r f oi l , type
edi t ai r f oi l
To run a demonstrati on, type the fi l ename at the command prompt. For
exampl e, to run the ai rfoi l demonstrati on, type
ai r f oi l
Note Many of the demonstrati ons use mul ti pl e wi ndows and requi re you to
press a key i n the MATLAB command wi ndow to conti nue through the
demonstrati on.
5 Program m ing w ith M A TLA B
5-28
These are the current demonstrati on programs.
Table 5-1: MATLAB Demonstration Programs
Matrices
ai r f oi l Graphi cal demonstrati on of sparse matri x from NASA
ai rfoi l .
bucky de m Connecti vi ty graph of the Buckmi nster Ful l er geodesi c
dome.
del s qde mo Fi ni te di fference Lapl aci an on vari ous domai ns.
ei gmov i e Symmetri c ei genval ue movi e.
ei gs how Graphi cal demonstrati on of matri x ei genval ues.
i nt r o I ntroducti on to basi c matri x operati ons i n MATLAB.
i nv er t e r Demonstrati on of the i nversi on of a l arge matri x.
mat mani p I ntroducti on to matri x mani pul ati on.
r r ef mov i e Computati on of reduced row echel on form.
s epdemo Separators for a fi ni te el ement mesh.
s par s i t y Demonstrati on of the effect of sparsi ty orderi ngs.
s v ds how Graphi cal demonstrati on of matri x si ngul ar val ues.
Numerics
bench MATLAB benchmark.
cens us Predi cti on of the U.S. popul ati on i n the year 2000.
e2pi Two-di mensi onal , vi sual sol uti on to the probl em
Whi ch i s greater, e
or
e
?
f f t demo Use of the FFT functi on for spectral anal ysi s.
f i t demo Nonl i near curve fi t wi th si mpl ex al gori thm.
f pl ot de mo Demonstrati on of pl otti ng a functi on.
D em onstration Program s Included w ith M A TLA B
5-29
f unf uns Demonstrati on of functi ons operati ng on other
functi ons.
l ot kademo Exampl e of ordi nary di fferenti al equati on sol uti on.
quaddemo Adapti ve quadrature.
quake Loma Pri eta earthquake.
s pl i ne2d Demonstrati on of gi nput and s pl i ne i n two
di mensi ons.
s uns pot s Demonstrati on of the fast Fouri er transform (FFT)
functi on i n MATLAB used to anal yze the vari ati ons i n
sunspot acti vi ty.
z er odemo Zero fi ndi ng wi th f z er o.
Visualization
c ol or menu Demonstrati on of addi ng a col ormap to the current
fi gure.
c pl x demo Maps of functi ons of a compl ex vari abl e.
e ar t hmap Graphi cal demonstrati ons of earths topography.
gr af 2d Two-di mensi onal XY pl ots i n MATLAB.
gr af 2d2 Three-di mensi onal XYZ pl ots i n MATLAB.
gr af cpl x Demonstrati on of compl ex functi on pl ots i n MATLAB.
i magedemo Demonstrati on of MATLABs i mage capabi l i ty.
i mageex t Demonstrati on of changi ng and rotati ng i mage
col ormaps.
l or enz Gr aphi cal demonstr ati on of the or bi t around the
Lorenz chaoti c attractor.
penny Several vi ews of the penny data.
v i bes Vi brati ng L-shaped membrane movi e.
Table 5-1: MATLAB Demonstration Programs (Continued)
5 Program m ing w ith M A TLA B
5-30
x f our i e r Graphi cal demonstrati on of Fouri er seri es expansi on.
x pkl ei n Kl ei n bottl e demo.
x ps ound Demonstrati on of MATLABs sound capabi l i ty.
Language
gr af 3d Demonstrati on of Handl e Graphi cs for surface pl ots.
hndl ax i s Demonstrati on of Handl e Graphi cs for axes.
hndl gr af Demonstrati on of Handl e Graphi cs for l i ne pl ots.
x pl ang I ntroducti on to the MATLAB l anguage.
ODE Suite
a2ode Sti ff probl em, l i near wi th real ei genval ues.
a3ode Sti ff probl em, l i near wi th real ei genval ues.
b5ode Sti ff probl em, l i near wi th compl ex ei genval ues.
bal l ode Equati ons of moti on for a bounci ng bal l used by
BALLDEMO.
bes s l ode Bessel s equati on of order 0 used by BESSLDEMO.
br us s ode Sti ff probl em, model l i ng a chemi cal reacti on
(Brussel ator).
bui ode Sti ff probl em, anal yti cal sol uti on due to Bui .
chm6ode Sti ff probl em CHM6 from Enri ght and Hul l .
chm7ode Sti ff probl em CHM7 from Enri ght and Hul l .
chm9ode Sti ff probl em CHM9 from Enri ght and Hul l .
d1ode Sti ff probl em, nonl i near wi th real ei genval ues.
f em1ode Sti ff probl em wi th a ti me-dependent mass matri x.
f em2ode Sti ff probl em wi th a ti me-i ndependent mass matri x.
Table 5-1: MATLAB Demonstration Programs (Continued)
D em onstration Program s Included w ith M A TLA B
5-31
gear ode Sti ff probl em due to Gear as quoted by van der
Houwen.
hb1ode Sti ff probl em 1 of Hi ndmarsh and Byrne.
hb2ode Sti ff probl em 2 of Hi ndmarsh and Byrne.
hb3ode Sti ff probl em 3 of Hi ndmarsh and Byrne.
odedemo Demonstrati on of the ODE sui te i ntegrators.
or bi t ode Restri cted 3 body probl em used by ORBI TDEMO.
or bt 2ode Nonsti ff probl em D5 of Hul l et al .
r i gi dode Eul er equati ons of a ri gi d body wi thout external forces.
s t i code Spri ng-dri ven mass stuck to surface, used by STI CDEMO.
v dpode Parameteri zabl e van der Pol equati on (sti ff for l arge ).
Gallery
c r ul l er Graphi cal demonstrati on of a crul l er.
kl ei n1 Graphi cal demonstrati on of a Kl ei n bottl e.
knot Tube surroundi ng a three-di mensi onal knot.
l ogo Graphi cal demonstrati on of the MATLAB L-shaped
membrane l ogo.
modes Graphi cal demonstrati on of 12 modes of the L-shaped
membrane.
qui v demo Graphi cal demonstrati on of the qui ver functi on.
s phar m2 Graphi cal demonstrati on of spheri cal surface
harmoni c.
t or i 4 Graphi cal demonstrati on of four-l i nked, unknotted tori .
Table 5-1: MATLAB Demonstration Programs (Continued)
5 Program m ing w ith M A TLA B
5-32
Games
bbl wr ap Bubbl ewrap.
l i f e Conways Game of Li fe.
s oma Soma cube.
x pbombs Mi nesweeper game.
Miscellaneous
codec Al phabet transposi ti on coder/decoder.
cr ul s pi n Spi nni ng crul l er movi e.
l ogos pi n Movi e of the MathWorks l ogo spi nni ng.
makev as e Demonstrati on of a surface of revol uti on.
quat demo Quaterni on rotati on.
s pi nner Col orful l i nes spi nni ng through space.
t r av el Travel i ng sal esman probl em.
t r us s Ani mati on of a bendi ng bri dge truss.
wr l dt r v Great ci rcl e fl i ght routes around the gl obe.
x phi de Vi sual percepti on of objects i n moti on.
x pquad Superquadri cs pl otti ng demonstrati on.
Helper Functions
bucky Graph of the Buckmi nster Ful l er geodesi c dome.
cmdl nbgn Set up for command l i ne demos.
cmdl nend Cl ean up after command l i ne demos.
cmdl nwi n Demo gateway routi ne for runni ng command l i ne
demos.
Table 5-1: MATLAB Demonstration Programs (Continued)
D em onstration Program s Included w ith M A TLA B
5-33
f i nddemo Command that fi nds avai l abl e demos for i ndi vi dual
tool boxes.
hel pf un Uti l i ty functi on for di spl ayi ng hel p text conveni entl y.
membr ane The MathWorks l ogo.
peaks Sampl e functi on of two vari abl es.
pl t mat Command that di spl ays a matri x i n a fi gure wi ndow.
Table 5-1: MATLAB Demonstration Programs (Continued)
5 Program m ing w ith M A TLA B
5-34
6
Symbol i c
Math Tool box
Introduction . . . . . . . . . . . . . . . . . . . . 6-2
GettingHelp . . . . . . . . . . . . . . . . . . . . 6-4
GettingStarted . . . . . . . . . . . . . . . . . . 6-5
Calculus . . . . . . . . . . . . . . . . . . . . . . 6-16
Simplifications and Substitutions . . . . . . . . . . 6-44
Variable-Precision Arithmetic . . . . . . . . . . . . 6-61
Linear Algebra . . . . . . . . . . . . . . . . . . . 6-66
SolvingEquations . . . . . . . . . . . . . . . . . 6-89
6 Sym bolic M ath Toolbox
6-2
Introduction
The Symbol i c Math Tool box i ncorporates symbol i c computati on i nto
MATLABs numeri c envi ronment. Thi s tool box suppl ements MATLABs
numeri c and graphi cal faci l i ti es wi th several other types of mathemati cal
computati on.
The computati onal engi ne underl yi ng the tool boxes i s the kernel of Mapl e, a
system devel oped pri mari l y at the Uni versi ty of Waterl oo, Canada, and, more
recentl y, at the Ei dgenssi che Techni sche Hochschul e, Zri ch, Swi tzerl and.
Mapl e i s marketed and supported by Waterl oo Mapl e, I nc.
Thi s versi on of the Symbol i c Math Tool box i s desi gned to work wi th
MATLAB 5.3 and Mapl e V Rel ease 5.
The Symbol i c Math Tool box i s a col l ecti on of more than one-hundred MATLAB
functi ons that provi de access to the Mapl e kernel usi ng a syntax and styl e that
i s a natural extensi on of the MATLAB l anguage. The tool box al so al l ows you to
access functi ons i n Mapl es l i near al gebra package. Wi th thi s tool box, you can
wri te your own M-fi l es to access Mapl e functi ons and the Mapl e workspace.
Facility Covers
Cal cul us Di fferenti ati on, i ntegrati on, l i mi ts, summati on,
and Tayl or seri es
Li near Al gebra I nverses, determi nants, ei genval ues, si ngul ar
val ue decomposi ti on, and canoni cal forms of
symbol i c matri ces
Si mpl i fi cati on Methods of si mpl i fyi ng al gebrai c expressi ons
Sol uti on of
Equati ons
Symbol i c and numeri cal sol uti ons to al gebrai c and
di ffer enti al equati ons
Transforms Fouri er, Lapl ace, z-transform, and correspondi ng
i nverse transforms
Vari abl e-Preci si on
Ari thmeti c
Numeri cal eval uati on of mathemati cal expressi ons
to any speci fi ed accuracy
Introduction
6-3
The fol l owi ng secti ons of thi s tutori al provi de expl anati on and exampl es on
how to use the tool box.
For More InformationYou can access compl ete reference i nformati on for the
Symbol i c Math Tool box functi ons from the Hel p Desk. Al so, you can pri nt the
PDF ver si on of the Symbolic Math Toolbox Users Guide(tutori al and
reference i nformati on) by sel ecti ng Symbolic Math Toolbox Users Guide
from OnlineManuals on the Hel p Desk.
Section Covers
Getti ng Hel p How to get onl i ne hel p for Symbol i c Math
Tool box functi ons
Getti ng Started Basi c symbol i c math operati ons
Cal cul us How to di fferenti ate and i ntegrate symbol i c
expressi ons
Si mpl i fi cati ons and
Substi tuti ons
How to si mpl i fy and substi tute val ues i nto
expressi ons
Vari abl e-Preci si on
Ari thmeti c
How to control the preci si on of
computati ons
Li near Al gebra Exampl es usi ng the tool box functi ons
Sol vi ng Equati ons How to sol ve symbol i c equati ons
6 Sym bolic M ath Toolbox
6-4
Getting Help
There are several ways to fi nd i nformati on on usi ng Symbol i c Math Tool box
functi ons. One, of course, i s to read thi s chapter! Another i s to use the Hel p
Desk, whi ch contai ns reference i nformati on for al l the functi ons. You can al so
use MATLABs command l i ne hel p system. General l y, you can obtai n hel p on
MATLAB functi ons si mpl y by typi ng
hel p f unct i on
where f unct i on i s the name of the MATLAB functi on for whi ch you need hel p.
Thi s i s not suffi ci ent, however, for some Symbol i c Math Tool box functi ons. The
reason? The Symbol i c Math Tool box overl oads many of MATLABs numeri c
functi ons. That i s, i t provi des symbol i c-speci fi c i mpl ementati ons of the
functi ons, usi ng the same functi on name. To obtai n hel p for the symbol i c
versi on of an overl oaded functi on, type
hel p s y m/f unct i on
where f unct i on i s the overl oaded functi ons name. For exampl e, to obtai n hel p
on the symbol i c versi on of the overl oaded functi on, di f f , type
hel p s y m/di f f
To obtai n i nformati on on the numeri c versi on, on the other hand, si mpl y type
hel p di f f
How can you tel l whether a functi on i s overl oaded? The hel p for the numeri c
versi on tel l s you so. For exampl e, the hel p for the di f f functi on contai ns the
secti on
Ov er l oaded met hods
hel p char /di f f . m
hel p s y m/di f f . m
Thi s tel l s you that there are two other di f f commands that operate on
expressi ons of cl ass char and cl ass s y m, respecti vel y. See the next secti on for
i nformati on on cl ass s y m. For more i nformati on on overl oaded commands, see
the UsingMATLAB gui de, whi ch i s accessi bl e from the Hel p Desk.
G etting Started
6-5
Getting Started
Thi s secti on descri bes how to create and use symbol i c objects. I t al so descri bes
the defaul t symbol i c vari abl e. I f you are fami l i ar wi th versi on 1 of the Symbol i c
Math Tool box, pl ease note that versi on 2 uses substanti al l y di fferent and
si mpl er syntax.
To get a qui ck onl i ne i ntroducti on to the Symbol i c Math Tool box, type demos at
the MATLAB command l i ne. MATLAB di spl ays the MATLAB Demos di al og
box. Sel ect Symbolic Math (i n the l eft l i st box) and then Introduction (i n the
ri ght l i st box).
Symbolic Objects
The Symbol i c Math Tool box defi nes a new MATLAB data type cal l ed a
symbol i c object or s y m(see UsingMATLAB for an i ntroducti on to MATLAB
cl asses and objects). I nternal l y, a symbol i c object i s a data structure that stores
a stri ng representati on of the symbol . The Symbol i c Math Tool box uses
symbol i c objects to represent symbol i c vari abl es, expressi ons, and matri ces.
6 Sym bolic M ath Toolbox
6-6
Creating Symbolic Variables and Expressions
The s y mcommand l ets you construct symbol i c vari abl es and expressi ons. For
exampl e, the commands
x = s y m( ' x ' )
a = s y m( ' al pha' )
create a symbol i c vari abl e x that pri nts as x and a symbol i c vari abl e a that
pri nts as al pha.
Suppose you want to use a symbol i c vari abl e to represent the gol den rati o
The command
r ho = s y m( ' ( 1 + s qr t ( 5) ) /2' )
achi eves thi s goal . Now you can perform vari ous mathemati cal operati ons on
r ho. For exampl e,
f = r ho^2 - r ho - 1
returns
f =
( 1/2+1/2*5^( 1/2) ) ^2- 3/2- 1/2*5^( 1/2)
Then
s i mpl i f y ( f )
returns
0
Now suppose you want to study the quadrati c functi on f = ax
2
+ bx + c. The
statement
f = s y m( ' a*x ^2 + b*x + c' )
assi gns the symbol i c expressi on ax
2
+ bx + c to the vari abl e f . Observe that i n
thi s case, the Symbol i c Math Tool box does not create vari abl es correspondi ng
to the terms of the expressi on, a, b, c, and x. To perform symbol i c math
1 5 +
2
----------------- =
G etting Started
6-7
operati ons (e.g., i ntegrati on, di fferenti ati on, substi tuti on, etc.) on f , you need
to create the vari abl es expl i ci tl y. You can do thi s by typi ng
a = s y m( ' a' )
b = s y m( ' b' )
c = s y m( ' c' )
x = s y m( ' x ' )
or si mpl y
s y ms a b c x
I n general , you can use s y mor s y ms to create symbol i c vari abl es. We
recommend you use s y ms because i t requi res l ess typi ng.
Symbolic and Numeric Conversions
Consi der the ordi nary MATLAB quanti ty
t = 0. 1
The s y mfuncti on has four opti ons for returni ng a symbol i c representati on of
the numeri c val ue stored i n t . The ' f ' opti on
s y m( t , ' f ' )
returns a symbol i c fl oati ng-poi nt representati on
' 1. 999999999999a' *2^( - 4)
The ' r ' opti on
s y m( t , ' r ' )
returns the rati onal form
1/10
Thi s i s the defaul t setti ng for s y m. That i s, cal l i ng s y mwi thout a second
argument i s the same as usi ng s y mwi th the ' r ' opti on.
s y m( t )
ans =
1/10
6 Sym bolic M ath Toolbox
6-8
The thi rd opti on ' e' returns the rati onal form of t pl us the di fference between
the theoreti cal rati onal expressi on for t and i ts actual (machi ne) fl oati ng-poi nt
val ue i n terms of eps (the fl oati ng-poi nt rel ati ve accuracy).
s y m( t , ' e ' )
ans =
1/10+eps /40
The fourth opti on ' d' returns the deci mal expansi on of t up to the number of
si gni fi cant di gi ts speci fi ed by di gi t s .
s y m( t , ' d' )
ans =
. 10000000000000000555111512312578
The defaul t val ue of di gi t s i s 32 (hence, s y m( t , ' d' ) returns a number wi th 32
si gni fi cant di gi ts), but i f you prefer a shorter representati on, use the di gi t s
command as fol l ows.
di gi t s ( 7)
s y m( t , ' d' )
ans =
. 1000000
A parti cul arl y effecti ve use of s y mi s to convert a matri x from numeri c to
symbol i c form. The command
A = hi l b( 3)
generates the 3-by-3 Hi l bert matri x.
A =
1. 0000 0. 5000 0. 3333
0. 5000 0. 3333 0. 2500
0. 3333 0. 2500 0. 2000
By appl yi ng s y mto A
A = s y m( A)
G etting Started
6-9
you can obtai n the (i nfi ni tel y preci se) symbol i c form of the 3-by-3 Hi l bert
matri x.
A =
[ 1, 1/2, 1/3]
[ 1/2, 1/3, 1/4]
[ 1/3, 1/4, 1/5]
Constr ucting Rea l a nd Complex Va ria bles
The s y mcommand al l ows you to speci fy the mathemati cal properti es of
symbol i c vari abl es by usi ng the ' r eal ' opti on. That i s, the statements
x = s y m( ' x ' , ' r eal ' ) ; y = s y m( ' y ' , ' r eal ' ) ;
or more effi ci entl y
s y ms x y r eal
z = x + i *y
create symbol i c vari abl es x and y that have the added mathemati cal property
of bei ng real vari abl es. Speci fi cal l y thi s means that the expressi on
f = x ^2 + y ^2
i s stri ctl y nonnegati ve. Hence, z i s a (formal ) compl ex vari abl e and can be
mani pul ated as such. Thus, the commands
conj ( x ) , conj ( z ) , ex pand( z *conj ( z ) )
return the compl ex conjugates of the vari abl es
x , x - i *y , x ^2 + y ^2
The conj command i s the compl ex conjugate operator for the tool box. I f
conj ( x ) == x returns 1, then x i s a real vari abl e.
To cl ear x of i ts real property, you must type
s y ms x unr eal
or
x = s y m( ' x ' , ' unr eal ' )
6 Sym bolic M ath Toolbox
6-10
The command
cl ear x
does not make x a nonreal vari abl e.
Cr ea ting Abstr a ct Functions
I f you want to create an abstract (i .e., i ndetermi nant) functi on f(x), type
f = s y m( ' f ( x ) ' )
Then f acts l i ke f(x) and can be mani pul ated by the tool box commands. To
construct the fi rst di fference rati o, for exampl e, type
df = ( s ubs ( f , ' x ' , ' x +h' ) - f ) /' h'
or
s y ms x h
df = ( s ubs ( f , x , x +h) - f ) /h
whi ch returns
df =
( f ( x +h) - f ( x ) ) /h
Thi s appl i cati on of s y mi s useful when computi ng Fouri er, Lapl ace, and
z-transforms.
Ex a mple: Cr ea ting a Symbolic M a tr ix
A ci rcul ant matri x has the property that each row i s obtai ned from the previ ous
one by cycl i cal l y permuti ng the entri es one step forward. We create the
ci rcul ant matri x A whose el ements are a, b, and c, usi ng the commands
s y ms a b c
A = [ a b c; b c a; c a b]
whi ch return
A =
[ a, b, c ]
[ b, c, a ]
[ c, a, b ]
G etting Started
6-11
Si nce Ai s ci rcul ant, the sum over each row and col umn i s the same. Lets check
thi s for the fi rst row and second col umn. The command
s um( A( 1, : ) )
returns
ans =
a+b+c
The command
s um( A( 1, : ) ) == s um( A( : , 2) ) % Thi s i s a l ogi cal t es t .
returns
ans =
1
Now repl ace the (2,3) entry of A wi th bet a and the vari abl e b wi th al pha. The
commands
s y ms al pha bet a;
A( 2, 3) = bet a;
A = s ubs ( A, b, al pha)
return
A =
[ a, al pha, c]
[ al pha, c , bet a]
[ c, a, al pha]
From thi s exampl e, you can see that usi ng symbol i c objects i s very si mi l ar to
usi ng regul ar MATLAB numeri c objects.
6 Sym bolic M ath Toolbox
6-12
The Defa ult Symbolic Va ria ble
When mani pul ati ng mathemati cal functi ons, the choi ce of the i ndependent
vari abl e i s often cl ear from context. For exampl e, consi der the expressi ons i n
the tabl e bel ow.
I f we ask for the deri vati ves of these expr essi ons, wi thout speci fyi ng the
i ndependent vari abl e, then by mathemati cal conventi on we obtai n f' =nx
n
,
g' =acos(at +b), and h' =J
v
(z)(v/z) - J
v+1
(z). Lets assume that the i ndependent
vari abl es i n these three expressi ons are x, t, and z, respecti vel y. The other
symbol s, n, a, b, and v, are usual l y regarded as constants or parameters. I f,
however, we wanted to di fferenti ate the fi rst expressi on wi th respect to n, for
exampl e, we coul d wri te
or
to get x
n
l n x.
By mathemati cal conventi on, i ndependent vari abl es are often l ower-case
l etters found near the end of the Lati n al phabet (e.g., x, y, or z). Thi s i s the i dea
behi nd f i nds y m, a uti l i ty functi on i n the tool box used to determi ne defaul t
symbol i c vari abl es. Defaul t symbol i c vari abl es are uti l i zed by the cal cul us,
si mpl i fi cati on, equati on-sol vi ng, and transform functi ons. To appl y thi s uti l i ty
to the exampl e di scussed above, type
s y ms a b n nu t x z
f = x ^n; g = s i n( a*t + b) ; h = bes s el j ( nu, z ) ;
Thi s creates the symbol i c expressi ons f , g, and h to match the exampl e. To
di fferenti ate these expressi ons, we use di f f .
di f f ( f )
Mathematical Function MATLAB Command
f = x
n
f = x ^n
g= si n(at+b) g = s i n( a*t + b)
h = J
(z) h = be s s el j ( nu, z )
d
dn
-------f x ( )
d
dn
-------x
n
G etting Started
6-13
returns
ans =
x ^n*n/x
See the secti on Di fferenti ati on for a more detai l ed di scussi on of
di fferenti ati on and the di f f command.
Here, as above, we di d not speci fy the vari abl e wi th respect to di fferenti ati on.
How di d the tool box determi ne that we wanted to di fferenti ate wi th respect to
x ? The answer i s the f i nds y mcommand
f i nds y m( f , 1)
whi ch returns
ans =
x
Si mi l arl y, f i nds y m( g, 1) and f i nds y m( h, 1) return t and z , respecti vel y. Here
the second argument of f i nds y mdenotes the number of symbol i c vari abl es we
want to fi nd i n the symbol i c object f , usi ng the f i nds y mrul e (see bel ow). The
absence of a second argument i n f i nds y mresul ts i n a l i st of al l symbol i c
var i abl es i n a gi ven symbol i c expr essi on. We see thi s demonstr ated bel ow. The
command
f i nds y m( g)
returns the resul t
ans =
a, b, t
findsym Rule The defaul t symbol i c vari abl e i n a symbol i c expressi on i s the
l etter that i s cl osest to ' x ' al phabeti cal l y. I f there are two equal l y cl ose, the
l etter l ater i n the al phabet i s chosen.
6 Sym bolic M ath Toolbox
6-14
Here are some exampl es.
Creating Symbolic Math Functions
There are two ways to create functi ons:
Use symbol i c expressi ons
Create an M-fi l e
Using Symbolic Ex pressions
The sequence of commands
s y ms x y z
r = s qr t ( x ^2 + y ^2 + z ^2)
t = at an( y /x )
f = s i n( x *y ) /( x *y )
generates the symbol i c expressi ons r , t , and f . You can use di f f , i nt , s ubs ,
and other Symbol i c Math Tool box functi ons to mani pul ate such expressi ons.
Expression Variable Returned By findsym
x ^n x
s i n( a*t +b) t
bes s el j ( nu, z ) z
w*y + v *z y
ex p( i *t het a) t het a
l og( al pha*x 1) x 1
y *( 4+3*i ) + 6*j y
s qr t ( pi *al pha) al pha
G etting Started
6-15
Crea ting a n M - File
M-fi l es permi t a more general use of functi ons. Suppose, for exampl e, you want
to create the s i nc functi on s i n( x ) /x . To do thi s, create an M-fi l e i n the @s y m
di rectory.
f unct i on z = s i nc( x )
%SI NC The s y mbol i c s i nc f unct i on
% s i n( x ) /x . Thi s f unc t i on
% accept s a s y m as t he i nput ar gument .
i f i s equal ( x , s y m( 0) )
z = 1;
el s e
z = s i n( x ) /x ;
end
You can extend such exampl es to functi ons of several var i abl es. For a mor e
detai l ed di scussi on on object-ori ented programmi ng, see the UsingMATLAB
gui de.
6 Sym bolic M ath Toolbox
6-16
Calculus
The Symbol i c Math Tool box provi des functi ons to do the basi c operati ons of
cal cul us; di fferenti ati on, l i mi ts, i ntegrati on, summati on, and Tayl or seri es
expansi on. The fol l owi ng secti ons outl i ne these functi ons.
Differentiation
Lets create a symbol i c expressi on.
s y ms a x
f = s i n( a*x )
Then
di f f ( f )
di fferenti ates f wi th respect to i ts symbol i c vari abl e (i n thi s case x ), as
determi ned by f i nds y m.
ans =
cos ( a*x ) *a
To di fferenti ate wi th respect to the vari abl e a, type
di f f ( f , a)
whi ch returns df/ da
ans =
cos ( a*x ) *x
To cal cul ate the second deri vati ves wi th respect to x and a, respecti vel y, type
di f f ( f , 2)
or
di f f ( f , x , 2)
whi ch return
ans =
- s i n( a*x ) *a^2
C alculus
6-17
and
di f f ( f , a, 2)
whi ch returns
ans =
- s i n( a*x ) *x ^2
Defi ne a, b, x , n, t , and t het a i n the MATLAB workspace, usi ng the s y m
command. The tabl e bel ow i l l ustrates the di f f command.
To di fferenti ate the Bessel functi on of the fi rst ki nd, bes s el j ( nu, z ) , wi th
respect to z , type
s y ms nu z
b = bes s el j ( nu, z ) ;
db = di f f ( b)
whi ch returns
db =
- be s s el j ( nu+1, z ) +nu/z *bes s el j ( nu, z )
The di f f functi on can al so take a symbol i c matri x as i ts i nput. I n thi s case, the
di fferenti ati on i s done el ement-by-el ement. Consi der the exampl e
s y ms a x
A = [ cos ( a*x ) , s i n( a*x ) ; - s i n( a*x ) , cos ( a*x ) ]
whi ch returns
A =
[ cos ( a*x ) , s i n( a*x ) ]
[ - s i n( a*x ) , cos ( a*x ) ]
f diff(f)
x ^n x ^n*n/x
s i n( a*t +b) a*cos ( a*t +b)
e x p( i *t het a) i *ex p( i *t het a)
6 Sym bolic M ath Toolbox
6-18
The command
di f f ( A)
returns
ans =
[ - s i n( a*x ) *a, cos ( a*x ) *a]
[ - cos ( a*x ) *a, - s i n( a*x ) *a]
You can al so perform di fferenti ati on of a col umn vector wi th respect to a row
vector. Consi der the transformati on from Eucl i dean (x, y, z)to spheri cal (r, , )
coordi nates as gi ven by x = r cos cos , y= r cos si n , and z = r si n . Note
that corresponds to el evati on or l ati tude whi l e denotes azi muth or
l ongi tude.
To cal cul ate the Jacobi an matri x, J , of thi s transformati on, use the j acobi an
functi on. The mathemati cal notati on for J i s
=
For the purposes of tool box syntax, we use l for and f for . The commands
s y ms r l f
x = r *cos ( l ) *cos ( f ) ; y = r *cos ( l ) *s i n( f ) ; z = r *s i n( l ) ;
J = j acobi an( [ x ; y ; z ] , [ r l f ] )
z
y
x
( x, y, z)
r
J
x y x , , ( )
r , , ( )
-----------------------
C alculus
6-19
return the Jacobi an
J =
[ cos ( l ) *cos ( f ) , - r *s i n( l ) *cos ( f ) , - r *cos ( l ) *s i n( f ) ]
[ cos ( l ) *s i n( f ) , - r *s i n( l ) *s i n( f ) , r *cos ( l ) *cos ( f ) ]
[ s i n( l ) , r *cos ( l ) , 0]
and the command
det J = s i mpl e( det ( J) )
returns
det J =
- cos ( l ) *r ^2
Noti ce that the fi rst argument of the j acobi an functi on must be a col umn
vector and the second argument a row vector. Moreover, si nce the determi nant
of the Jacobi an i s a rather compl i cated tri gonometri c expressi on, we used the
s i mpl e command to make tri gonometri c substi tuti ons and reducti ons
(si mpl i fi cati ons). The secti on Si mpl i fi cati ons and Substi tuti ons di scusses
si mpl i fi cati on i n more detai l .
A tabl e summari zi ng di f f and j ac obi an fol l ows.
Mathematical Operator MATLAB Command
di f f ( f ) or di f f ( f , x )
di f f ( f , a)
di f f ( f , b, 2)
J = j ac obi an( [ r : t ] , [ u, v ] )
x d
df
a d
df
b
2
2
d
d f
J
r t , ( )
u v , ( )
----------------- =
6 Sym bolic M ath Toolbox
6-20
Limits
The fundamental i dea i n cal cul us i s to make cal cul ati ons on functi ons as a
vari abl e gets cl ose to or approaches a certai n val ue. Recal l that the defi ni ti on
of the deri vati ve i s gi ven by a l i mi t
provi ded thi s l i mi t exi sts. The Symbol i c Math Tool box al l ows you to compute
the l i mi ts of functi ons i n a di rect manner. The commands
s y ms h n x
l i mi t ( ( cos ( x +h) - c os ( x ) ) /h, h, 0 )
whi ch return
ans =
- s i n( x )
and
l i mi t ( ( 1 + x /n) ^n, n, i nf )
whi ch returns
ans =
ex p( x )
i l l ustrate two of the most i mportant l i mi ts i n mathemati cs: the deri vati ve (i n
thi s case of cos x) and the exponenti al functi on. Whi l e many l i mi ts
are two si ded (that i s, the resul t i s the same whether the approach i s from the
ri ght or l eft of a), l i mi ts at the si ngul ari ti es of f(x) are not. Hence, the three
l i mi ts,
, , and
yi el d the three di sti nct resul ts: undefi ned, - , and + , respecti vel y.
f x ( )
f x h + ( ) f x ( )
h
----------------------------------
h 0
l i m =
f x ( )
x a
l i m
1
x
---
x 0
l i m
1
x
---
x 0-
l i m
1
x
---
x 0+
l i m
C alculus
6-21
I n the case of undefi ned l i mi ts, the Symbol i c Math Tool box returns NaN(not a
number). The command
l i mi t ( 1/x , x , 0)
or
l i mi t ( 1/x )
returns
ans =
NaN
The command
l i mi t ( 1/x , x , 0, ' l ef t ' )
returns
ans =
- i nf
whi l e the command
l i mi t ( 1/x , x , 0, ' r i ght ' )
returns
ans =
i nf
Observe that the defaul t case, l i mi t ( f ) i s the same as l i mi t ( f , x , 0) . Expl ore
the opti ons for the l i mi t command i n thi s tabl e. Here, we assume that f i s a
functi on of the symbol i c object x .
Mathematical Operation MATLAB Command
l i mi t ( f )
l i mi t ( f , x , a) or
l i mi t ( f , a)
f x ( )
x 0
l i m
f x ( )
x a
l i m
6 Sym bolic M ath Toolbox
6-22
Integration
I f f i s a symbol i c expressi on, then
i nt ( f )
attempts to fi nd another symbol i c expressi on, F, so that di f f ( F) = f . That i s,
i nt ( f ) returns the i ndefi ni te i ntegral or anti deri vati ve of f (provi ded one exi sts
i n cl osed form). Si mi l ar to di fferenti ati on,
i nt ( f , v )
uses the symbol i c object v as the vari abl e of i ntegrati on, rather than the
vari abl e determi ned by f i nds y m. See how i nt works by l ooki ng at thi s tabl e.
l i mi t ( f , x , a, ' l ef t ' )
l i mi t ( f , x , a, ' r i ght ' )
Mathematical Operation MATLAB Command
i nt ( x ^n) or
i nt ( x ^n, x )
i nt ( s i n( 2*x ) , 0, pi /2) or
i nt ( s i n( 2*x ) , x , 0, pi /2)
g = cos ( a*t + b)
i nt ( g) or
i nt ( g, t )
i nt ( be s s el j ( 1, z ) ) or
i nt ( be s s el j ( 1, z ) , z )
Mathematical Operation MATLAB Command
f x ( )
x a-
l i m
f x ( )
x a+
l i m
x
n
x d
x
n 1 +
n 1 +
------------- =
2x ( ) si n x d
0
2
1 =
g at b + ( ) cos =
g t ( ) t d
at b + ( ) si n a =
J
1
z ( )
dz J
0
z ( ) =
C alculus
6-23
I n contrast to di fferenti ati on, symbol i c i ntegrati on i s a more compl i cated task.
A number of di ffi cul ti es can ari se i n computi ng the i ntegral . The
anti deri vati ve, F, may not exi st i n cl osed form; i t may defi ne an unfami l i ar
functi on; i t may exi st, but the software cant fi nd the anti deri vati ve; the
software coul d fi nd i t on a l arger computer, but runs out of ti me or memory on
the avai l abl e machi ne. Neverthel ess, i n many cases, MATLAB can perform
symbol i c i ntegrati on successful l y. For exampl e, create the symbol i c vari abl es
s y ms a b t het a x y n x 1 u
Thi s tabl e i l l ustrates i ntegrati on of expressi ons contai ni ng those vari abl es.
The l ast exampl e shows what happens i f the tool box cant fi nd the
anti deri vati ve; i t si mpl y returns the command, i ncl udi ng the vari abl e of
i ntegrati on, uneval uated.
Defi ni te i ntegrati on i s al so possi bl e. The commands
i nt ( f , a, b)
and
i nt ( f , v , a, b)
are used to fi nd a symbol i c expr essi on for
and
respecti vel y.
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 het a+b) - cos ( a*t het a+b) /a
e x p( - x 1^2) 1/2*pi ^( 1/2) *er f ( x 1)
1/( 1+u^2) at an( u)
f x ( )
a
b
dx f v ( ) v d
a
b
dx
C alculus
6-27
you onl y cl ear k i n the MATLAB workspace. To ensure that k has no formal
properti es (that i s, to ensure k i s a purel y formal vari abl e), type
s y ms k unr eal
Thi s vari ati on of the s y ms command cl ears k i n the Mapl e workspace. You can
al so decl are a sequence of symbol i c vari abl es w, y, x, z to be real , usi ng
s y ms w x y z r eal
I n thi s case, al l of the vari abl es i n between the words s y ms and r eal are
assi gned the property r eal . That i s, they are real vari abl es i n the Mapl e
workspace.
Symbolic Summation
You can compute symbol i c summati ons, when they exi st, by usi ng the s y ms um
command. For exampl e, the p-seri es
adds to
2
/6, whi l e the geometri c seri es 1 + x+ x
2
+ ... adds to 1/(1-x), provi ded
| x| < 1. Three summati ons are demonstrated bel ow.
s y ms x k
s 1 = s y ms um( 1/k^2, 1, i nf )
s 2 = s y ms um( x ^k, k, 0, i nf )
s 1 =
1/6*pi ^2
s 2 =
- 1/( x - 1)
1
1
2
2
------
1
3
2
------ + + +
6 Sym bolic M ath Toolbox
6-28
Taylor Series
The statements
s y ms x
f = 1/( 5+4*cos ( x ) )
T = t ay l or ( f , 8)
return
T =
1/9+2/81*x ^2+5/1458*x ^4+49/131220*x ^6
whi ch i s al l the terms up to, but not i ncl udi ng, order ei ght (O(x
8
)) i n the Tayl or
seri es for f(x).
Techni cal l y, Ti s a Macl auri n seri es, si nce i ts basepoi nt i s a = 0.
The command
pr et t y ( T)
pri nts Ti n a format resembl i ng typeset mathemati cs.
2 4 49 6
1/9 + 2/81 x + 5/1458 x + - - - - - - x
131220
These commands
s y ms x
g = ex p( x *s i n( x ) )
t = t ay l or ( g, 12, 2)
generate the fi rst 12 nonzero terms of the Tayl or seri es for g about x = 2.
x a ( )
n
n 0 =
f
n ( )
a ( )
n!
-----------------
C alculus
6-29
Lets pl ot these functi ons together to see how wel l thi s Tayl or approxi mati on
compares to the actual functi on g.
x d = 1: 0. 05: 3; y d = s ubs ( g, x , x d) ;
ez pl ot ( t , [ 1, 3] ) ; hol d on;
pl ot ( x d, y d, ' r - . ' )
t i t l e( ' Tay l or appr ox i mat i on v s . act ual f unct i on' ) ;
l egend( ' Funct i on' , ' Tay l or ' )
Speci al thanks to Professor Gunnar Bckstrm of UMEA i n Sweden for thi s
exampl e.
1 1.2 1.4 1.6 1.8 2 2.2 2.4 2.6 2.8 3
1
2
3
4
5
6
x
Taylor approximation vs. actual function
Function
Taylor
6 Sym bolic M ath Toolbox
6-30
Extended Calculus Example
The functi on
provi des a starti ng poi nt for i l l ustrati ng several cal cul us operati ons i n the
tool box. I t i s al so an i nteresti ng functi on i n i ts own ri ght. The statements
s y ms x
f = 1/( 5+4*cos ( x ) )
store the symbol i c expressi on defi ni ng the functi on i n f .
The functi on ez pl ot ( f ) produces the pl ot of f(x) as shown bel ow.
The ez pl ot functi on tri es to make reasonabl e choi ces for the range of the x-axi s
and for the resul ti ng scal e of the y-axi s. I ts choi ces can be overri dden by an
addi ti onal i nput argument, or by subsequent ax i s commands. The defaul t
f x ( )
1
5 4 x ( ) cos +
------------------------------ =
6 4 2 0 2 4 6
0.1
0.2
0.3
0.4
0.5
0.6
0.7
0.8
0.9
1
x
1/(5+4*cos(x))
C alculus
6-31
domai n for a functi on di spl ayed by e z pl ot i s -2 x 2. To produce a graph
of f(x) for a x b, type
ez pl ot ( f , [ a b] )
Lets now l ook at the second deri vati ve of the functi on f .
f 2 = di f f ( f , 2)
f 2 =
32/( 5+4*cos ( x ) ) ^3*s i n( x ) ^2+4/( 5+4*cos ( x ) ) ^2*cos ( x )
Equi val entl y, we can type f 2 = di f f ( f , x , 2) . The defaul t scal i ng i n ez pl ot
cuts off part of f 2s graph. Set the axes l i mi ts manual l y to see the enti re
functi on.
ez pl ot ( f 2)
ax i s ( [ - 2*pi 2*pi - 5 2] )
6 4 2 0 2 4 6
5
4
3
2
1
0
1
2
x
32/(5+4*cos(x))^3*sin(x)^2+4/(5+4*cos(x))^2*cos(x)
6 Sym bolic M ath Toolbox
6-32
From the graph, i t appears that the val ues of f''(x) l i e between -4 and 1. As i t
turns out, thi s i s not true. We can cal cul ate the exact range for f (i .e., compute
i ts actual maxi mum and mi ni mum).
The actual maxi ma and mi ni ma of f''(x) occur at the zeros of f'''(x). The
statements
f 3 = di f f ( f 2) ;
pr et t y ( f 3)
compute f'''(x) and di spl ay i t i n a more readabl e format.
3
s i n( x ) s i n( x ) cos ( x ) s i n( x )
384 - - - - - - - - - - - - - - - + 96 - - - - - - - - - - - - - - - - 4 - - - - - - - - - - - - - - -
4 3 2
( 5 + 4 cos ( x ) ) ( 5 + 4 cos ( x ) ) ( 5 + 4 cos ( x ) )
We can si mpl i fy thi s expressi on usi ng the statements
f 3 = s i mpl e( f 3) ;
pr et t y ( f 3)
2 2
s i n( x ) ( 96 s i n( x ) + 80 cos ( x ) + 80 cos ( x ) - 25)
4 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
4
( 5 + 4 cos ( x ) )
Now use the s ol v e functi on to fi nd the zeros of f'''(x).
z = s ol v e( f 3)
returns a 5-by-1 symbol i c matri x
z =
[ 0]
[ at an( ( - 255- 60*19^( 1/2) ) ^( 1/2) , 10+3*19^( 1/2) ) ]
[ at an( - ( - 255- 60*19^( 1/2) ) ^( 1/2) , 10+3*19^( 1/2) ) ]
[ at an( ( - 255+60*19^( 1/2) ) ^( 1/2) /( 10- 3*19^( 1/2) ) ) +pi ]
[ - at an( ( - 255+60*19^( 1/2) ) ^( 1/2) /( 10- 3*19^( 1/2) ) ) - pi ]
C alculus
6-33
each of whose entri es i s a zero of f'''(x). The command
f or mat ; % Def aul t f or mat of 5 di gi t s
z r = doubl e( z )
converts the zeros to doubl e form.
z r =
0
0+ 2. 4381i
0- 2. 4381i
2. 4483
- 2. 4483
So far, we have found three real zeros and two compl ex zeros. However, a graph
of f 3 shows that we have not yet found al l i ts zeros.
ez pl ot ( f 3)
hol d on;
pl ot ( z r , 0*z r , ' r o' )
pl ot ( [ - 2*pi , 2*pi ] , [ 0, 0] , ' g- . ' ) ;
t i t l e( ' Zer os of f 3' )
6 Sym bolic M ath Toolbox
6-34
Thi s occurs because f'''(x) contai ns a factor of si n(x), whi ch i s zero at i nteger
mul ti pl es of . The functi on, s ol v e( s i n( x ) ) , however , onl y reports the zer o at
x=0.
We can obtai n a compl ete l i st of the real zeros by transl ati ng z r
z r = [ 0 z r ( 4) pi 2*pi - z r ( 4) ]
by mul ti pl es of 2
z r = [ z r - 2*pi z r z r +2*pi ] ;
6 4 2 0 2 4 6
3
2
1
0
1
2
3
x
Zeros of f3
C alculus
6-35
Now l ets pl ot the transformed z r on our graph for a compl ete pi cture of the
zeros of f 3.
pl ot ( z r , 0*z r , ' kX' )
The fi rst zero of f'''(x) found by s ol v e i s at x= 0. We substi tute 0 for the
symbol i c vari abl e i n f 2
f 20 = s ubs ( f 2, x , 0)
to compute the correspondi ng val ue of f''(0).
f 20 =
0. 0494
6 4 2 0 2 4 6
3
2
1
0
1
2
3
x
Zeros of f3
6 Sym bolic M ath Toolbox
6-36
A l ook at the graph of f''(x) shows that thi s i s onl y a l ocal mi ni mum, whi ch we
demonstrate by repl otti ng f 2.
cl f
ez pl ot ( f 2)
ax i s ( [ - 2*pi 2*pi - 4. 25 1. 25] )
y l abel ( ' f 2' ) ;
t i t l e( ' Pl ot of f 2 = f ' ' ' ' ( x ) ' )
hol d on
pl ot ( 0, doubl e( f 20) , ' r o' )
t ex t ( - 1, - 0. 25, ' Local mi ni mum' )
The resul ti ng pl ot
i ndi cates that the gl obal mi ni ma occur near x = - and x= . We can
demonstrate that they occur exactl y at x= , usi ng the fol l owi ng sequence of
commands. Fi rst we try substi tuti ng - and i nto f'''(x).
s i mpl e( [ s ubs ( f 3, x , - s y m( pi ) ) , s ubs ( f 3, x , s y m( pi ) ) ] )
6 4 2 0 2 4 6
4
3.5
3
2.5
2
1.5
1
0.5
0
0.5
1
x
Plot of f2 = f(x)
f
2
Local minimum
C alculus
6-37
The resul t
ans =
[ 0, 0]
shows that - and happen to be cri ti cal poi nts of f'''(x). We can see that - and
are gl obal mi ni ma by pl otti ng f 2( - pi ) and f 2( pi ) agai nst f 2( x ) .
m1 = doubl e( s ubs ( f 2, x , - pi ) ) ; m2 = doubl e( s ubs ( f 2, x , pi ) ) ;
pl ot ( - pi , m1, ' go' , pi , m2, ' go' )
t ex t ( - 1, - 4, ' Gl obal mi ni ma' )
The actual mi ni ma are m1, m2
ans =
[ - 4, - 4]
as shown i n the fol l owi ng pl ot.
The foregoi ng anal ysi s confi rms part of our ori gi nal guess that the range of
f''(x) i s [ - 4, 1] . We can confi rm the other part by exami ni ng the fourth zero of
6 4 2 0 2 4 6
4
3.5
3
2.5
2
1.5
1
0.5
0
0.5
1
x
Plot of f2 = f(x)
f
2
Local minimum
Global minima
6 Sym bolic M ath Toolbox
6-38
f'''(x) found by s ol v e. Fi rst extract the fourth zero from z and assi gn i t to a
separate vari abl e
s = z ( 4)
to obtai n
s =
at an( ( - 255+60*19^( 1/2) ) ^( 1/2) /( 10- 3*19^( 1/2) ) ) +pi
Executi ng
s d = doubl e( s )
di spl ays the zeros correspondi ng numeri c val ue.
s d =
2. 4483
Pl otti ng the poi nt ( s , f 2( s ) ) agai nst f 2, usi ng
M1 = doubl e( s ubs ( f 2, x , s ) ) ;
pl ot ( s d, M1, ' ko' )
t ex t ( - 1, 1, ' Gl obal max i mum' )
C alculus
6-39
vi sual l y confi rms that s i s a maxi mum.
The maxi mum i s M1 = 1. 0051.
Therefore, our guess that the maxi mum of f''(x) i s [-4, 1] was cl ose, but
i ncorrect. The actual range i s [-4, 1.0051].
Now, l ets see i f i ntegrati ng f''(x) twi ce wi th respect to x recovers our ori gi nal
functi on f(x) = 1/(5 + 4 cos x). The command
g = i nt ( i nt ( f 2) )
returns
g =
- 8/( t an( 1/2*x ) ^2+9)
6 4 2 0 2 4 6
4
3.5
3
2.5
2
1.5
1
0.5
0
0.5
1
x
Plot of f2 = f(x)
f
2
Local minimum
Global minima
Global maximum
6 Sym bolic M ath Toolbox
6-40
Thi s i s certai nl y not the ori gi nal expressi on for f(x). Lets l ook at the di fference
f(x)- g(x).
d = f - g
pr et t y ( d)
1 8
+
5 + 4 cos ( x ) 2
t an( 1/2 x ) + 9
We can si mpl i fy thi s usi ng s i mpl e( d) or s i mpl i f y ( d) . Ei ther command
produces
ans =
1
Thi s i l l ustrates the concept that di fferenti ati ng f(x) twi ce, then i ntegrati ng the
resul t twi ce, produces a functi on that may di ffer from f(x) by a l i near functi on
of x.
Fi nal l y, i ntegrate f(x) once more.
F = i nt ( f )
The resul t
F =
2/3*at an( 1/3*t an( 1/2*x ) )
i nvol ves the arctangent functi on.
C alculus
6-41
Though F(x) i s the anti deri vati ve of a conti nuous functi on, i t i s i tsel f
di sconti nuous as the fol l owi ng pl ot shows.
ez pl ot ( F)
Note that F(x) has jumps at x = . Thi s occurs because tan x i s si ngul ar at
x = .
6 4 2 0 2 4 6
1
0.8
0.6
0.4
0.2
0
0.2
0.4
0.6
0.8
1
x
2/3*atan(1/3*tan(1/2*x))
6 Sym bolic M ath Toolbox
6-42
I n fact, as
ez pl ot ( at an( t an( x ) ) )
shows, the numeri cal val ue of at an( t an( x ) ) di ffers from x by a pi ecewi se
constant functi on that has jumps at odd mul ti pl es of /2.
To obtai n a representati on of F(x) that does not have jumps at these poi nts, we
must i ntroduce a second functi on, J (x), that compensates for the
di sconti nui ti es. Then we add the appropri ate mul ti pl e of J (x) to F(x)
J = s y m( ' r ound( x /( 2*pi ) ) ' ) ;
c = s y m( ' 2/3*pi ' ) ;
F1 = F+c *J
F1 =
2/3*at an( 1/3*t an( 1/2*x ) ) +2/3*pi *r ound( 1/2*x /pi )
6 4 2 0 2 4 6
1.5
1
0.5
0
0.5
1
1.5
x
atan(tan(x))
C alculus
6-43
and pl ot the resul t.
ez pl ot ( F1, [ - 6. 28, 6. 28] )
Thi s representati on does have a conti nuous graph.
Noti ce that we use the domai n [-6.28, 6.28] i n ez pl ot rather than the defaul t
domai n [-2, 2]. The reason for thi s i s to pr event an eval uati on of
F1 = 2/3 atan(1/3 tan 1/2 x) at the si ngul ar poi nts x = - and x = where the
jumps i n F and J do not cancel out one another. The proper handl i ng of branch
cut di sconti nui ti es i n mul ti val ued functi ons l i ke arctan xi s a deep and di ffi cul t
probl em i n symbol i c computati on. Al though MATLAB and Mapl e cannot do
thi s enti rel y automati cal l y, they do provi de the tool s for i nvesti gati ng such
questi ons.
6 4 2 0 2 4 6
2.5
2
1.5
1
0.5
0
0.5
1
1.5
2
2.5
x
2/3*atan(1/3*tan(1/2*x))+2/3*pi*round(1/2*x/pi)
6 Sym bolic M ath Toolbox
6-44
Simplifications and Substitutions
There are several functi ons that si mpl i fy symbol i c expressi ons and are used to
perform symbol i c substi tuti ons.
Simplifications
Here are three di fferent symbol i c expressi ons.
s y ms x
f = x ^3- 6*x ^2+11*x - 6
g = ( x - 1) *( x - 2) *( x - 3)
h = x *( x *( x - 6) +11) - 6
Here are thei r prettypri nted forms, generated by
pr et t y ( f ) , pr et t y ( g) , pr et t y ( h)
3 2
x - 6 x + 11 x - 6
( x - 1) ( x - 2) ( x - 3)
x ( x ( x - 6) + 11) - 6
These expressi ons are three di fferent representati ons of the same
mathemati cal functi on, a cubi c pol ynomi al i n x .
Each of the three forms i s preferabl e to the others i n di fferent si tuati ons. The
fi rst form, f , i s the most commonl y used representati on of a pol ynomi al . I t i s
si mpl y a l i near combi nati on of the powers of x . The second form, g, i s the
factored form. I t di spl ays the roots of the pol ynomi al and i s the most accurate
for numer i cal eval uati on near the r oots. But, i f a pol ynomi al does not have such
si mpl e roots, i ts factored form may not be so conveni ent. The thi rd form, h, i s
the Horner, or nested, representati on. For numeri cal eval uati on, i t i nvol ves the
fewest ari thmeti c operati ons and i s the most accurate for some other ranges of
x .
The symbol i c si mpl i fi cati on probl em i nvol ves the veri fi cati on that these three
expressi ons represent the same functi on. I t al so i nvol ves a l ess cl earl y defi ned
objecti ve whi ch of these representati ons i s the si mpl est?
Sim plifications and Substitutions
6-45
Thi s tool box provi des several functi ons that appl y vari ous al gebrai c and
tri gonometri c i denti ti es to transform one representati on of a functi on i nto
another, possi bl y si mpl er, representati on. These functi ons are col l ect ,
ex pand, hor ner , f act or , s i mpl i f y , and s i mpl e.
collect
The statement
col l ect ( f )
vi ews f as a pol ynomi al i n i ts symbol i c vari abl e, say x , and col l ects al l the
coeffi ci ents wi th the same power of x . A second argument can speci fy the
vari abl e i n whi ch to col l ect terms i f there i s more than one candi date. Here are
a few exampl es.
f collect(f)
( x - 1) *( x - 2) *( x - 3) x ^3- 6*x ^2+11*x - 6
x *( x *( x - 6) +11) - 6 x ^3- 6*x ^2+11*x - 6
( 1+x ) *t + x *t 2*x *t +t
6 Sym bolic M ath Toolbox
6-46
ex pa nd
The statement
ex pand( f )
di stri butes products over sums and appl i es other i denti ti es i nvol vi ng functi ons
of sums. For exampl e,
hor ner
The statement
hor ner ( f )
transforms a symbol i c pol ynomi al f i nto i ts Horner, or nested, representati on.
For exampl e,
f expand(f)
a( x + y ) ax + ay
( x - 1) ( x - 2) ( x - 3) x ^3- 6x ^2+11x - 6
x ( x( x - 6) +11) - 6 x ^3- 6x ^2+11x - 6
ex p( a+b) ex p( a) ex p( b)
cos ( x +y ) cos ( x ) *cos ( y ) - s i n( x ) *s i n( y )
cos ( 3acos ( x ) ) 4x ^3 - 3x
f horner(f)
x ^3- 6x ^2+11x - 6 - 6+( 11+( - 6+x ) *x ) *x
1. 1+2. 2x +3. 3x ^2 11/10+( 11/5+33/10*x ) *x
Sim plifications and Substitutions
6-47
fa ctor
I f f i s a pol ynomi al wi th r ati onal coeffi ci ents, the statement
f ac t or ( f )
expresses f as a product of pol ynomi al s of l ower degree wi th rati onal
coeffi ci ents. I f f cannot be factored over the rati onal numbers, the resul t i s f
i tsel f. For exampl e,
Here i s another exampl e i nvol vi ng f act or . I t factors pol ynomi al s of the form
x ^n + 1. Thi s code
s y ms x ;
n = ( 1: 9) ' ;
p = x . ^n + 1;
f = f act or ( p) ;
[ p, f ]
returns a matri x wi th the pol ynomi al s i n i ts fi rst col umn and thei r factored
forms i n i ts second.
[ x +1, x +1 ]
[ x ^2+1, x ^2+1 ]
[ x ^3+1, ( x +1) *( x ^2- x +1) ]
[ x ^4+1, x ^4+1 ]
[ x ^5+1, ( x +1) *( x ^4- x ^3+x ^2- x +1) ]
[ x ^6+1, ( x ^2+1) *( x ^4- x ^2+1) ]
[ x ^7+1, ( x +1) *( 1- x +x ^2- x ^3+x ^4- x ^5+x ^6) ]
[ x ^8+1, x ^8+1 ]
[ x ^9+1, ( x +1) *( x ^2- x +1) *( x ^6- x ^3+1) ]
f factor(f)
x ^3- 6x ^2+11x - 6 ( x - 1) ( x - 2) ( x - 3)
x ^3- 6x ^2+11x - 5 x ^3- 6x ^2+11x - 5
x ^6+1 ( x ^2+1) ( x ^4- x ^2+1)
6 Sym bolic M ath Toolbox
6-48
As an asi de at thi s poi nt, we menti on that f act or can al so factor symbol i c
objects contai ni ng i ntegers. Thi s i s an al ternati ve to usi ng the f act or functi on
i n MATLABs s pecf un di rectory. For exampl e, the fol l owi ng code segment
N = s y m( 1) ;
f or k = 2: 11
N( k) = 10*N( k- 1) +1;
end
[ N' f act or ( N' ) ]
di spl ays the factors of symbol i c i ntegers consi sti ng of 1s.
[ 1, 1]
[ 11, ( 11) ]
[ 111, ( 3) *( 37) ]
[ 1111, ( 11) *( 101) ]
[ 11111, ( 41) *( 271) ]
[ 111111, ( 3) *( 7) *( 11) *( 13) *( 37) ]
[ 1111111, ( 239) *( 4649) ]
[ 11111111, ( 11) *( 73) *( 101) *( 137) ]
[ 111111111, ( 3) ^2*( 37) *( 333667) ]
[ 1111111111, ( 11) *( 41) *( 271) *( 9091) ]
[ 11111111111, ( 513239) *( 21649) ]
Sim plifications and Substitutions
6-49
simplify
The s i mpl i f y functi on i s a powerful , general purpose tool that appl i es a
number of al gebrai c i denti ti es i nvol vi ng sums, i ntegral powers, square roots
and other fracti onal powers, as wel l as a number of functi onal i denti ti es
i nvol vi ng tri g functi ons, exponenti al and l og functi ons, Bessel functi ons,
hypergeometri c functi ons, and the gamma functi on. Here are some exampl es.
simple
The s i mpl e functi on has the unorthodox mathemati cal goal of fi ndi ng a
si mpl i fi cati on of an expressi on that has the fewest number of characters. Of
course, there i s l i ttl e mathemati cal justi fi cati on for cl ai mi ng that one
expressi on i s si mpl er than another just because i ts ASCI I representati on i s
shorter, but thi s often proves sati sfactory i n practi ce.
The s i mpl e functi on achi eves i ts goal by i ndependentl y appl yi ng s i mpl i f y ,
col l e ct , f act or , and other si mpl i fi cati on functi ons to an expressi on and
keepi ng track of the l engths of the resul ts. The s i mpl e functi on then returns
the shortest resul t.
The s i mpl e functi on has several forms, each returni ng di fferent output. The
form
s i mpl e( f )
f simplify(f)
x ( x ( x - 6) +11) - 6 x ^3- 6x ^2+11x - 6
( 1- x ^2) /( 1- x ) x +1
( 1/a^3+6/a^2+12/a+8) ^( 1/3) ( ( 2*a+1) ^3/a^3) ^( 1/3)
s y ms x y pos i t i v e
l og( xy ) l og( x ) +l og( y )
e x p( x ) ex p( y ) ex p( x +y )
bes s el j ( 2, x ) + bes s el j ( 0, x ) 2/x *bes s el j ( 1, x )
gamma( x +1) - x *gamma( x ) 0
c os ( x ) ^2 + s i n( x ) ^2 1
6 Sym bolic M ath Toolbox
6-50
di spl ays each tri al si mpl i fi cati on and the si mpl i fi cati on functi on that produced
i t i n the MATLAB command wi ndow. The s i mpl e functi on then returns the
shortest resul t. For exampl e, the command
s i mpl e( c os ( x ) ^2 + s i n( x ) ^2)
di spl ays the fol l owi ng al ternati ve si mpl i fi cati ons i n the MATLAB command
wi ndow
s i mpl i f y :
1
r ads i mp:
cos ( x ) ^2+s i n( x ) ^2
combi ne( t r i g) :
1
f act or :
cos ( x ) ^2+s i n( x ) ^2
ex pand:
cos ( x ) ^2+s i n( x ) ^2
conv er t ( ex p) :
( 1/2*ex p( i *x ) +1/2/ex p( i *x ) ) ^2- 1/4*( ex p( i *x ) - 1/ex p( i *x ) ) ^2
conv er t ( s i ncos ) :
cos ( x ) ^2+s i n( x ) ^2
conv er t ( t an) :
( 1- t an( 1/2*x ) ^2) ^2/( 1+t an( 1/2*x ) ^2) ^2+4*t an( 1/2*x ) ^2/
( 1+t an( 1/2*x ) ^2) ^2
col l ect ( x ) :
cos ( x ) ^2+s i n( x ) ^2
and returns
ans =
1
Sim plifications and Substitutions
6-51
Thi s form i s useful when you want to check, for exampl e, whether the shortest
form i s i ndeed the si mpl est. I f you are not i nterested i n how s i mpl e achi eves
i ts resul t, use the form
f = s i mpl e( f )
Thi s form si mpl y returns the shortest expressi on found. For exampl e, the
statement
f = s i mpl e( cos ( x ) ^2+s i n( x ) ^2)
returns
f =
1
I f you want to know whi ch si mpl i fi cati on returned the shortest resul t, use the
mul ti pl e output form.
[ F, how] = s i mpl e( f )
Thi s form returns the shortest resul t i n the fi rst vari abl e and the si mpl i fi cati on
method used to achi eve the resul t i n the second vari abl e. For exampl e, the
statement
[ f , how] = s i mpl e( cos ( x ) ^2+s i n( x ) ^2)
returns
f =
1
how =
combi ne
The s i mpl e functi on someti mes i mproves on the resul t returned by s i mpl i f y ,
one of the si mpl i fi cati ons that i t tri es. For exampl e, when appl i ed to the
6 Sym bolic M ath Toolbox
6-52
exampl es gi ven for s i mpl i f y , s i mpl e returns a si mpl er (or at l east shorter)
resul t i n two cases.
I n some cases, i t i s advantageous to appl y s i mpl e twi ce to obtai n the effect of
two di fferent si mpl i fi cati on functi ons. For exampl e, the statements
f = ( 1/a^3+6/a^2+12/a+8) ^( 1/3) ;
s i mpl e( s i mpl e( f ) )
return
1/a+2
The fi rst appl i cati on, s i mpl e( f ) , uses r ads i mp to produce ( 2*a+1) /a; the
second appl i cati on uses combi ne( t r i g) to transform thi s to 1/a+2.
The s i mpl e functi on i s parti cul arl y effecti ve on expressi ons i nvol vi ng
tri gonometri c functi ons. Here are some exampl es.
f simplify(f) simple(f)
( 1/a^3+6/a^2+12/a+8) ^( 1/3) ( ( 2*a+1) ^3/a^3) ^( 1/3) ( 2*a+1) /a
s y ms x y pos i t i v e
l og( x y ) l og( x ) +l og( y ) l og( x *y )
f simple(f)
cos ( x ) ^2+s i n( x ) ^2 1
2c os ( x ) ^2- s i n( x ) ^2 3cos ( x ) ^2- 1
cos ( x ) ^2- s i n( x ) ^2 cos ( 2x )
cos ( x ) +( - s i n( x ) ^2) ^( 1/2) cos ( x ) +i s i n( x )
cos ( x ) +i s i n( x ) ex p( i x )
cos ( 3acos ( x ) ) 4x ^3- 3x
Sim plifications and Substitutions
6-53
Substitutions
There are two functi ons for symbol i c substi tuti on: s ubex pr and s ubs .
subex pr
These commands
s y ms a x
s = s ol v e( x ^3+a*x +1)
sol ve the equati on x ^3+a*x +1 = 0 f or x .
s =
[ 1/6*( - 108+12*( 12*a^3+81) ^( 1/2) ) ^( 1/3) - 2*a/
( - 108+12*( 12*a^3+81) ^( 1/2) ) ^( 1/3) ]
[ - 1/12*( - 108+12*( 12*a^3+81) ^( 1/2) ) ^( 1/3) +a/
( - 108+12*( 12*a^3+81) ^( 1/2) ) ^( 1/3) +1/2*i *3^( 1/2) *( 1/
6*( - 108+12*( 12*a^3+81) ^( 1/2) ) ^( 1/3) +2*a/
( - 108+12*( 12*a^3+81) ^( 1/2) ) ^( 1/3) ) ]
[ - 1/12*( - 108+12*( 12*a^3+81) ^( 1/2) ) ^( 1/3) +a/
( - 108+12*( 12*a^3+81) ^( 1/2) ) ^( 1/3) - 1/2*i *3^( 1/2) *( 1/
6*( - 108+12*( 12*a^3+81) ^( 1/2) ) ^( 1/3) +2*a/
( - 108+12*( 12*a^3+81) ^( 1/2) ) ^( 1/3) ) ]
6 Sym bolic M ath Toolbox
6-54
Use the pretty functi on to di spl ay s i n a more readabl e form.
pr et t y ( s )
s =
[ 1/3 a ]
[ 1/6 %1 - 2 - - - - - ]
[ 1/3 ]
[ %1 ]
[ ]
[ 1/3 a 1/2 / 1/3 a \]
[ - 1/12 %1 + - - - - - + 1/2 i 3 | 1/6 %1 + 2 - - - - - | ]
[ 1/3 | 1/3| ]
[ %1 \ %1 /]
[ ]
[ 1/3 a 1/2 / 1/3 a \]
[ - 1/12 %1 + - - - - - - 1/2 i 3 | 1/6 %1 + 2 - - - - - | ]
[ 1/3 | 1/3| ]
[ %1 \ %1 /]
3 1/2
%1 : = - 108 + 12 ( 12 a + 81)
The pr et t y command i nheri ts the %n (n, an i nteger) notati on from Mapl e to
denote subexpressi ons that occur mul ti pl e ti mes i n the symbol i c object. The
s ubex pr functi on al l ows you to save these common subexpressi ons as wel l as
the symbol i c object rewri tten i n terms of the subexpressi ons. The
subexpressi ons are saved i n a col umn vector cal l ed s i gma.
Conti nui ng wi th the exampl e
r = s ube x pr ( s )
Sim plifications and Substitutions
6-55
returns
s i gma =
- 108+12*( 12*a^3+81) ^( 1/2)
r =
[ 1/6*s i gma^( 1/3) - 2*a/s i gma^( 1/3) ]
[ - 1/12*s i gma^( 1/3) +a/s i gma^( 1/3) +1/2*i *3^( 1/2) *( 1/6*s i gma^
( 1/3) +2*a/s i gma^( 1/3) ) ]
[ - 1/12*s i gma^( 1/3) +a/s i gma^( 1/3) - 1/2*i *3^( 1/2) *( 1/6*s i gma^
( 1/3) +2*a/s i gma^( 1/3) ) ]
Noti ce that s ube x pr creates the vari abl e s i gma i n the MATLAB workspace.
You can veri fy thi s by typi ng whos , or the command
s i gma
whi ch returns
s i gma =
- 108+12*( 12*a^3+81) ^( 1/2)
6 Sym bolic M ath Toolbox
6-56
subs
Lets fi nd the ei genval ues and ei genvectors of a ci rcul ant matri x A.
s y ms a b c
A = [ a b c; b c a; c a b] ;
[ v , E] = ei 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]
Suppose we want to repl ace the rather l engthy expressi on
( b^2- b*a- c*b- c*a+a^2+c^2) ^( 1/2)
throughout v and E. We fi rst use s ubex pr
v = s ube x pr ( v , ' S' )
whi ch returns
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]
Sim plifications and Substitutions
6-57
Next, substi tute the symbol S i nto E wi th
E = s ubs ( E, S, ' S' )
E =
[ S, 0, 0]
[ 0, - S, 0]
[ 0, 0, b+c+a]
Now suppose we want to eval uate v at a = 10. We can do thi s usi ng the s ubs
command.
s ubs ( v , a, 10)
Thi s repl aces al l occurrences of a i n v wi th 10.
[ - ( 10+S- b) /( 10- c) , - ( 10- S- b) /( 10- c) , 1]
[ - ( b- c- S) /( 10- c) , - ( b- c+S) /( 10- c) , 1]
[ 1, 1, 1]
Noti ce, however, that the symbol i c expressi on represented by S i s unaffected by
thi s substi tuti on. That i s, the symbol a i n S i s not repl aced by 10. The s ubs
command i s al so a useful functi on for substi tuti ng i n a vari ety of val ues for
several vari abl es i n a parti cul ar expressi on. Lets l ook at S. Suppose that i n
addi ti on to substi tuti ng a = 10, we al so want to substi tute the val ues for 2 and
10 for b and c, respecti vel y. The way to do thi s i s to set val ues for a, b, and c i n
the workspace. Then s ubs eval uates i ts i nput usi ng the exi sti ng symbol i c and
doubl e vari abl es i n the current workspace. I n our exampl e, we fi rst set
a = 10; b = 2; c = 10;
s ubs ( S)
ans =
8
6 Sym bolic M ath Toolbox
6-58
To l ook at the contents of our workspace, type whos , whi ch gi ves
Name Si z e By t es Cl as s
A 3x 3 878 s y m obj ect
E 3x 3 888 s y m obj ect
S 1x 1 186 s y m obj ect
a 1x 1 8 doubl e ar r ay
ans 1x 1 140 s y m obj ect
b 1x 1 8 doubl e ar r ay
c 1x 1 8 doubl e ar r ay
v 3x 3 982 s y m obj ect
a, b, and c are now vari abl es of cl ass doubl e whi l e A, E, S, and v remai n symbol i c
expressi ons (cl ass s y m).
I f you want to preserve a, b, and c as symbol i c vari abl es, but sti l l al ter thei r
val ue wi thi n S, use thi s procedure.
s y ms a b c
s ubs ( S, {a, b, c}, {10, 2, 10})
ans =
8
Typi ng whos reveal s that a, b, and c remai n 1-by-1 s y mobjects.
The s ubs command can be combi ned wi th doubl e to eval uate a symbol i c
expressi on numeri cal l y. Suppose we have
s y ms t
M = ( 1- t ^2) *ex p( - 1/2*t ^2) ;
P = ( 1- t ^2) *s ech( t ) ;
and want to see how Mand P di ffer graphi cal l y.
Sim plifications and Substitutions
6-59
One approach i s to type
ez pl ot ( M) ; hol d on; ez pl ot ( P)
but thi s pl ot
does not readi l y hel p us i denti fy the curves.
6 4 2 0 2 4 6
0.8
0.6
0.4
0.2
0
0.2
0.4
0.6
0.8
1
t
(1t^2)*sech(t)
6 Sym bolic M ath Toolbox
6-60
I nstead, combi ne s ubs , doubl e , and pl ot
T = - 6: 0. 05: 6;
MT = doubl e( s ubs ( M, t , T) ) ;
PT = doubl e( s ubs ( P, t , T) ) ;
pl ot ( T, MT, ' b' , T, PT, ' r - . ' )
t i t l e( ' ' )
l egend( ' M' , ' P' )
x l abel ( ' t ' ) ; gr i d
to produce a mul ti col ored graph that i ndi cates the di fference between Mand P.
Fi nal l y the use of s ubs wi th stri ngs greatl y faci l i tates the sol uti on of probl ems
i nvol vi ng the Fouri er, Lapl ace, or z-transforms.
6 4 2 0 2 4 6
1
0.8
0.6
0.4
0.2
0
0.2
0.4
0.6
0.8
1
t
M
P
Variable-Precision A rithm etic
6-61
Variable-Precision Arithmetic
Overview
There are three di fferent ki nds of ari thmeti c operati ons i n thi s tool box.
Numeri c MATLABs fl oati ng-poi nt ari thmeti c
Rati onal Mapl es exact symbol i c ari thmeti c
VPA Mapl es vari abl e-preci si on ari thmeti c
For exampl e, the MATLAB statements
f or mat l ong
1/2+1/3
use numeri c computati on to produce
0. 83333333333333
Wi th the Symbol i c Math Tool box, the statement
s y m( 1/2) +1/3
uses symbol i c computati on to yi el d
5/6
And, al so wi th the tool box, the statements
di gi t s ( 25)
v pa( ' 1/2+1/3' )
use vari abl e-preci si on ari thmeti c to return
. 8333333333333333333333333
The fl oati ng-poi nt operati ons used by numeri c ari thmeti c are the fastest of the
three, and requi re the l east computer memory, but the resul ts are not exact.
The number of di gi ts i n the pri nted output of MATLABs doubl e quanti ti es i s
control l ed by the f or mat statement, but the i nternal representati on i s al ways
the ei ght-byte fl oati ng-poi nt representati on provi ded by the parti cul ar
computer hardware.
I n the computati on of the numeri c resul t above, there are actual l y three
roundoff errors, one i n the di vi si on of 1 by 3, one i n the addi ti on of 1/2 to the
6 Sym bolic M ath Toolbox
6-62
resul t of the di vi si on, and one i n the bi nary to deci mal conversi on for the
pri nted output. On computers that use I EEE fl oati ng-poi nt standard
ari thmeti c, the resul ti ng i nternal val ue i s the bi nary expansi on of 5/6,
truncated to 53 bi ts. Thi s i s approxi matel y 16 deci mal di gi ts. But, i n thi s
parti cul ar case, the pri nted output shows onl y 15 di gi ts.
The symbol i c operati ons used by rati onal ari thmeti c are potenti al l y the most
expensi ve of the three, i n terms of both computer ti me and memory. The resul ts
are exact, as l ong as enough ti me and memory are avai l abl e to compl ete the
computati ons.
Vari abl e-preci si on ari thmeti c fal l s i n between the other two i n terms of both
cost and accuracy. A gl obal parameter, set by the functi on di gi t s , control s the
number of si gni fi cant deci mal di gi ts. I ncreasi ng the number of di gi ts i ncreases
the accuracy, but al so i ncreases both the ti me and memory requi rements. The
defaul t val ue of di gi t s i s 32, correspondi ng roughl y to fl oati ng-poi nt accuracy.
The Mapl e documentati on uses the term hardware fl oati ng-poi nt for what we
are cal l i ng numeri c or fl oati ng-poi nt and uses the term fl oati ng-poi nt
ari thmeti c for what we are cal l i ng vari abl e-preci si on ari thmeti c.
Example: Using the Different Kinds of Arithmetic
Ra tiona l Arithmetic
By defaul t, the Symbol i c Math Tool box uses rati onal ari thmeti c operati ons, i .e.,
Mapl es exact symbol i c ari thmeti c. Rati onal ari thmeti c i s i nvoked when you
create symbol i c vari abl es usi ng the s y mfuncti on.
The s y mfuncti on converts a doubl e matri x to i ts symbol i c form. For exampl e, i f
the doubl e matri x i s
A =
1. 1000 1. 2000 1. 3000
2. 1000 2. 2000 2. 3000
3. 1000 3. 2000 3. 3000
i ts symbol i c form, S = s y m( A) , i s
S =
[ 11/10, 6/5, 13/10]
[ 21/10, 11/5, 23/10]
[ 31/10, 16/5, 33/10]
Variable-Precision A rithm etic
6-63
For thi s matri x A, i t i s possi bl e to di scover that the el ements are the rati os of
smal l i ntegers, so the symbol i c representati on i s formed from those i ntegers.
On the other hand, the statement
E = [ ex p( 1) s qr t ( 2) ; l og( 3) r and]
returns a matri x
E =
2. 71828182845905 1. 41421356237310
1. 09861228866811 0. 21895918632809
whose el ements are not the rati os of smal l i ntegers, so s y m( E) reproduces the
fl oati ng-poi nt representati on i n a symbol i c form.
[ 3060513257434037*2^( - 50) , 3184525836262886*2^( - 51) ]
[ 2473854946935174*2^( - 51) , 3944418039826132*2^( - 54) ]
Va ria ble- Precision N umbers
Vari abl e-preci si on numbers are di sti ngui shed from the exact rati onal
representati on by the presence of a deci mal poi nt. A power of 10 scal e factor,
denoted by ' e' , i s al l owed. To use vari abl e-preci si on i nstead of rati onal
ari thmeti c, create your vari abl es usi ng the v pa functi on.
For matri ces wi th purel y doubl e entri es, the v pa functi on generates the
representati on that i s used wi th vari abl e-preci si on ari thmeti c. Conti nui ng on
wi th our exampl e, and usi ng di gi t s ( 4) , appl yi ng v pa to the matri x S
v pa( S)
generates the output
S =
[ 1. 100, 1. 200, 1. 300]
[ 2. 100, 2. 200, 2. 300]
[ 3. 100, 3. 200, 3. 300]
and wi th di gi t s ( 25)
F = v pa( E)
6 Sym bolic M ath Toolbox
6-64
generates
F =
[ 2. 718281828459045534884808, 1. 414213562373094923430017]
[ 1. 098612288668110004152823, . 2189591863280899719512718]
Conver ting to Floa ting- Point
To convert a rati onal or vari abl e-preci si on number to i ts MATLAB
fl oati ng-poi nt representati on, use the doubl e functi on.
I n our exampl e, both doubl e( s y m( E) ) and doubl e( v pa( E) ) return E.
Another Example
The next exampl e i s perhaps more i nteresti ng. Start wi th the symbol i c
expressi on
f = s y m( ' ex p( pi *s qr t ( 163) ) ' )
The statement
doubl e( f )
produces the pri nted fl oati ng-poi nt val ue
2. 625374126407687e+17
Usi ng the second argument of v pa to speci fy the number of di gi ts,
v pa( f , 18)
returns
262537412640768744.
whereas
v pa( f , 25)
returns
262537412640768744. 0000000
We suspect that f mi ght actual l y have an i nteger val ue. Thi s suspi ci on i s
rei nforced by the 30 di gi t val ue, v pa( f , 30)
262537412640768743. 999999999999
Variable-Precision A rithm etic
6-65
Fi nal l y, the 40 di gi t val ue, v pa( f , 40)
262537412640768743. 9999999999992500725944
shows that f i s very cl ose to, but not exactl y equal to, an i nteger.
6 Sym bolic M ath Toolbox
6-66
Linear Algebra
Basic Algebraic Operations
Basi c al gebr ai c oper ati ons on symbol i c objects ar e the same as oper ati ons on
MATLAB objects of cl ass doubl e. Thi s i s i l l ustrated i n the fol l owi ng exampl e.
The Gi vens transformati on produces a pl ane rotati on through the angl e t . The
statements
s y ms t ;
G = [ cos ( t ) s i n( t ) ; - s i n( t ) cos ( t ) ]
create thi s transformati on matri x.
G =
[ cos ( t ) , s i n( t ) ]
[ - s i n( t ) , cos ( t ) ]
Appl yi ng the Gi vens transformati on twi ce shoul d si mpl y be a rotati on through
twi ce the angl e. The correspondi ng matri x can be computed by mul ti pl yi ng G
by i tsel f or by rai si ng Gto the second power. Both
A = G*G
and
A = G^2
produce
A =
[ cos ( t ) ^2- s i n( t ) ^2, 2*cos ( t ) *s i n( t ) ]
[ - 2*cos ( t ) *s i n( t ) , cos ( t ) ^2- s i n( t ) ^2]
The s i mpl e functi on
A = s i mpl e( A)
Linear A lgebra
6-67
uses a tri gonometri c i denti ty to return the expected form by tryi ng several
di fferent i denti ti es and pi cki ng the one that produces the shortest
representati on.
A =
[ c os ( 2*t ) , s i n( 2*t ) ]
[ - s i n( 2*t ) , cos ( 2*t ) ]
A Gi vens rotati on i s an orthogonal matri x, so i ts transpose i s i ts i nverse.
Confi rmi ng thi s by
I = G. ' *G
whi ch produces
I =
[ cos ( t ) ^2+s i n( t ) ^2, 0]
[ 0, cos ( t ) ^2+s i n( t ) ^2]
and then
I = s i mpl e( I )
I =
[ 1, 0]
[ 0, 1]
Linear Algebraic Operations
Lets do several basi c l i near al gebrai c operati ons.
The command
H = hi l b( 3)
generates the 3-by-3 Hi l bert matri x. Wi th f or mat s hor t , MATLAB pri nts
H =
1. 0000 0. 5000 0. 3333
0. 5000 0. 3333 0. 2500
0. 3333 0. 2500 0. 2000
6 Sym bolic M ath Toolbox
6-68
The computed el ements of Hare fl oati ng-poi nt numbers that are the rati os of
smal l i ntegers. I ndeed, Hi s a MATLAB array of cl ass doubl e. Converti ng Hto
a symbol i c matri x
H = s y m( H)
gi ves
[ 1, 1/2, 1/3]
[ 1/2, 1/3, 1/4]
[ 1/3, 1/4, 1/5]
Thi s al l ows subsequent symbol i c operati ons on Hto produce resul ts that
correspond to the i nfi ni tel y preci se Hi l bert matri x, s y m( hi l b( 3) ) , not i ts
fl oati ng-poi nt approxi mati on, hi l b( 3) . Therefore,
i nv ( H)
produces
[ 9, - 36, 30]
[ - 36, 192, - 180]
[ 30, - 180, 180]
and
det ( H)
yi el ds
1/2160
We can use the backsl ash oper ator to sol ve a system of si mul taneous l i near
equati ons. The commands
b = [ 1 1 1] '
x = H\b % Sol v e Hx = b
produce the sol uti on
[ 3]
[ - 24]
[ 30]
Linear A lgebra
6-69
Al l three of these resul ts, the i nverse, the determi nant, and the sol uti on to the
l i near system, are the exact resul ts correspondi ng to the i nfi ni tel y preci se,
rati onal , Hi l bert matri x. On the other hand, usi ng di gi t s ( 16) , the command
V = v pa( hi l b( 3) )
returns
[ 1. , . 5000000000000000, . 3333333333333333]
[ . 5000000000000000, . 3333333333333333, . 2500000000000000]
[ . 3333333333333333, . 2500000000000000, . 2000000000000000]
The deci mal poi nts i n the representati on of the i ndi vi dual el ements are the
si gnal to use vari abl e-preci si on ari thmeti c. The resul t of each ari thmeti c
operati on i s rounded to 16 si gni fi cant deci mal di gi ts. When i nverti ng the
matri x, these errors are magni fi ed by the matri x condi ti on number, whi ch for
hi l b( 3) i s about 500. Consequentl y,
i nv ( V)
whi ch returns
[ 9. 000000000000082, - 36. 00000000000039, 30. 00000000000035]
[ - 36. 00000000000039, 192. 0000000000021, - 180. 0000000000019]
[ 30. 00000000000035, - 180. 0000000000019, 180. 0000000000019]
shows the l oss of two di gi ts. So does
det ( V)
whi ch gi ves
. 462962962962958e- 3
and
V\b
whi ch i s
[ 3. 000000000000041]
[ - 24. 00000000000021]
[ 30. 00000000000019]
Si nce Hi s nonsi ngul ar, the nul l space of H
nul l ( H)
6 Sym bolic M ath Toolbox
6-70
and the col umn space of H
col s pace ( H)
produce an empty matri x and a permutati on of the i denti ty matri x,
respecti vel y. To make a more i nteresti ng exampl e, l ets try to fi nd a val ue s for
H( 1, 1) that makes Hsi ngul ar. The commands
s y ms s
H( 1, 1) = s
Z = det ( H)
s ol = s ol v e( Z)
produce
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
Then
H = s ubs ( H, s , s ol )
substi tutes the computed val ue of s ol for s i n Hto gi ve
H =
[ 8/9, 1/2, 1/3]
[ 1/2, 1/3, 1/4]
[ 1/3, 1/4, 1/5]
Now, the command
det ( H)
returns
ans =
0
Linear A lgebra
6-71
and
i nv ( H)
produces an error message
??? er r or us i ng ==> i nv
Er r or , ( i n i nv er s e) s i ngul ar mat r i x
because Hi s si ngul ar. For thi s matri x, Z = nul l ( H) and C = col s pace( H) are
nontri vi al .
Z =
[ 1]
[ - 4]
[ 10/3]
C =
[ 0, 1]
[ 1, 0]
[ 6/5, - 3/10]
I t shoul d be poi nted out that even though Hi s si ngul ar, v pa( H) i s not. For any
i nteger val ue d, setti ng
di gi t s ( d)
and then computi ng
det ( v pa( H) )
i nv ( v pa( H) )
resul ts i n a determi nant of si ze 10^( - d) and an i nverse wi th el ements on the
order of 10^d.
Eigenvalues
The symbol i c ei genval ues of a square matri x Aor the symbol i c ei genval ues and
ei genvectors of A are computed, respecti vel y, usi ng the commands
E = ei g( A)
[ V, E] = ei g( A)
6 Sym bolic M ath Toolbox
6-72
The vari abl e-preci si on counterparts are
E = ei g( v pa( A) )
[ V, E] = ei g( v pa( A) )
The ei genval ues of A are the zeros of the characteri sti c pol ynomi al of A,
det ( A- x *I ) , whi ch i s computed by
pol y ( A)
The matri x Hfr om the l ast secti on pr ovi des our fi rst exampl e.
H =
[ 8/9, 1/2, 1/3]
[ 1/2, 1/3, 1/4]
[ 1/3, 1/4, 1/5]
The matri x i s si ngul ar, so one of i ts ei genval ues must be zero. The statement
[ T, E] = ei g( H)
produces the matri ces Tand E. The col umns of Tare the ei genvectors of H.
T =
[ 1, 28/153+2/153*12589^( 1/2) , 28/153- 2/153*12589^( 12) ]
[ - 4, 1, 1]
[ 10/3, 92/255- 1/255*12589^( 1/2) , 292/255+1/255*12589^( 12) ]
Si mi l arl y, the di agonal el ements of E are the ei genval ues of H.
E =
[ 0, 0, 0]
[ 0, 32/45+1/180*12589^( 1/2) , 0]
[ 0, 0, 32/45- 1/180*12589^( 1/2) ]
I t may be easi er to understand the structure of the matri ces of ei genvectors, T,
and ei genval ues, E, i f we convert Tand E to deci mal notati on. We proceed as
fol l ows. The commands
Td = doubl e( T)
Ed = doubl e( E)
Linear A lgebra
6-73
return
Td =
1. 0000 1. 6497 - 1. 2837
- 4. 0000 1. 0000 1. 0000
3. 3333 0. 7051 1. 5851
Ed =
0 0 0
0 1. 3344 0
0 0 0. 0878
The fi rst ei genval ue i s zero. The correspondi ng ei genvector (the fi rst col umn of
Td) i s the same as the basi s for the nul l space found i n the l ast secti on. The
other two ei genval ues are the resul t of appl yi ng the quadrati c formul a to
x ^2- 64/45*x +253/2160
whi ch i s the quadrati c factor i n f act or ( pol y ( H) ) .
s y ms x
g = s i mpl e( f ac t or ( pol y ( H) ) /x ) ;
s ol v e( g)
Cl osed for m symbol i c expr essi ons for the ei genval ues ar e possi bl e onl y when
the characteri sti c pol ynomi al can be expressed as a product of rati onal
pol ynomi al s of degree four or l ess. The Rosser matri x i s a cl assi c numeri cal
anal ysi s test matri x that happens to i l l ustrate thi s requi rement. The
statement
R = s y m( gal l er y ( ' r os s er ' ) )
generates
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]
6 Sym bolic M ath Toolbox
6-74
The commands
p = pol y ( R) ;
pr et t y ( f act or ( p) )
produce
2 2 2
x ( x - 1020) ( x - 1020 x + 100) ( x - 1040500) ( x - 1000)
The characteri sti c pol ynomi al (of degree 8)factors ni cel y i nto the product of two
l i near terms and three quadrati c terms. We can see i mmedi atel y that four of
the ei genval ues are 0, 1020, and a doubl e root at 1000. The other four roots are
obtai ned from the remai ni ng quadrati cs. Use
ei g( R)
to fi nd al l these val ues
[ 0]
[ 1020]
[ 510+100*26^( 1/2) ]
[ 510- 100*26^( 1/2) ]
[ 10*10405^( 1/2) ]
[ - 10*10405^( 1/2) ]
[ 1000]
[ 1000]
The Rosser matri x i s not a typi cal exampl e; i t i s rare for a ful l 8-by-8 matri x to
have a characteri sti c pol ynomi al that factors i nto such si mpl e form. I f we
change the two corner el ements of R from 29 to 30 wi th the commands
S = R; S( 1, 8) = 30; S( 8, 1) = 30;
and then try
p = pol y ( S)
we fi nd
p =
40250968213600000+51264008540948000*x -
1082699388411166000*x ^2+4287832912719760*x ^- 3-
5327831918568*x ^4+82706090*x ^5+5079941*x ^6-
4040*x ^7+x ^8
Linear A lgebra
6-75
We al so fi nd that f act or ( p) i s p i tsel f. That i s, the characteri sti c pol ynomi al
cannot be factored over the rati onal s.
For thi s modi fi ed Rosser matri x
F = ei g( S)
returns
F =
[ - 1020. 0532142558915165931894252600]
[ - . 17053529728768998575200874607757]
[ . 21803980548301606860857564424981]
[ 999. 94691786044276755320289228602]
[ 1000. 1206982933841335712817075454]
[ 1019. 5243552632016358324933278291]
[ 1019. 9935501291629257348091808173]
[ 1020. 4201882015047278185457498840]
Noti ce that these val ues are cl ose to the ei genval ues of the ori gi nal Rosser
matri x. Further, the numeri cal val ues of F are a resul t of Mapl es fl oati ng-poi nt
ari thmeti c. Consequentl y, di fferent setti ngs of di gi t s do not al ter the number
of di gi ts to the ri ght of the deci mal pl ace.
I t i s al so possi bl e to try to compute ei genval ues of symbol i c matri ces, but cl osed
form sol uti ons are rare. The Gi vens transformati on i s generated as the matri x
exponenti al of the el ementary matri x
The Symbol i c Math Tool box commands
s y ms t
A = s y m( [ 0 1; - 1 0] ) ;
G = ex pm( t *A)
return
[ cos ( t ) , s i n( t ) ]
[ - s i n( t ) , cos ( t ) ]
A
0 1
1 0
=
6 Sym bolic M ath Toolbox
6-76
Next, the command
g = ei g( G)
produces
g =
[ cos ( t ) +( cos ( t ) ^2- 1) ^( 1/2) ]
[ cos ( t ) - ( cos ( t ) ^2- 1) ^( 1/2) ]
We can use s i mpl e to si mpl i fy thi s form of g. I ndeed, repeated appl i cati on of
s i mpl e
f or j = 1: 4
[ g, how] = s i mpl e( g)
end
produces the best resul t
g =
[ cos ( t ) +( - s i n( t ) ^2) ^( 1/2) ]
[ cos ( t ) - ( - s i n( t ) ^2) ^( 1/2) ]
how =
s i mpl i f y
g =
[ cos ( t ) +i *s i n( t ) ]
[ cos ( t ) - i *s i n( t ) ]
how =
r ads i mp
g =
[ ex p( i *t ) ]
[ 1/ex p( i *t ) ]
how =
conv er t ( ex p)
g =
[ ex p( i *t ) ]
[ ex p( - i *t ) ]
Linear A lgebra
6-77
how =
combi ne
Noti ce the fi rst appl i cati on of s i mpl e uses s i mpl i f y to produce a sum of si nes
and cosi nes. Next, s i mpl e i nvokes r ads i mp to produce cos ( t ) + i *s i n( t ) for
the fi rst ei genvector. The thi rd appl i cati on of s i mpl e uses conv er t ( ex p) to
change the si nes and cosi nes to compl ex exponenti al s. The l ast appl i cati on of
s i mpl e uses s i mpl i f y to obtai n the fi nal form.
J ordan Canonical Form
The Jordan canoni cal form resul ts from attempts to di agonal i ze a matri x by a
si mi l ari ty transformati on. For a gi ven matri x A, fi nd a nonsi ngul ar matri x V,
so that i nv ( V) *A*V, or, more succi nctl y, J = V\A*V, i s as cl ose to di agonal as
possi bl e. For al most al l matri ces, the Jordan canoni cal form i s the di agonal
matri x of ei genval ues and the col umns of the transformati on matri x are the
ei genvectors. Thi s al ways happens i f the matri x i s symmetri c or i f i t has
di sti nct ei genval ues. Some nonsymmetri c matri ces wi th mul ti pl e ei genval ues
cannot be di agonal i zed. The Jordan form has the ei genval ues on i ts di agonal ,
but some of the superdi agonal el ements are one, i nstead of zero. The statement
J = j or dan( A)
computes the Jordan canoni cal form of A. The statement
[ V, J] = j or dan( A)
al so computes the si mi l ari ty transformati on. The col umns of V are the
general i zed ei genvectors of A.
The Jordan form i s extremel y sensi ti ve to perturbati ons. Al most any change i n
Acauses i ts Jordan form to be di agonal . Thi s makes i t very di ffi cul t to compute
the Jordan form rel i abl y wi th fl oati ng-poi nt ari thmeti c. I t al so i mpl i es that A
must be known exactl y (i .e., wi thout round-off error, etc.). I ts el ements must be
i ntegers, or rati os of smal l i ntegers. I n parti cul ar, the vari abl e-preci si on
cal cul ati on, j or dan( v pa( A) ) , i s not al l owed.
6 Sym bolic M ath Toolbox
6-78
For exampl e, l et
A = s y m( [ 12, 32, 66, 116; - 25, - 76, - 164, - 294;
21, 66, 143, 256; - 6, - 19, - 41, - 73] )
A =
[ 12, 32, 66, 116]
[ - 25, - 76, - 164, - 294]
[ 21, 66, 143, 256]
[ - 6, - 19, - 41, - 73]
Then
[ V, J] = j or dan( A)
produces
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]
Therefore A has a doubl e ei genval ue at 1, wi th a si ngl e Jordan bl ock, and a
doubl e ei genval ue at 2, al so wi th a si ngl e Jordan bl ock. The matri x has onl y
two ei genvectors, V( : , 1) and V( : , 3) . They sati sfy
A*V( : , 1) = 1*V( : , 1)
A*V( : , 3) = 2*V( : , 3)
The other two col umns of V are general i zed ei genvectors of grade 2. They
sati sfy
A*V( : , 2) = 1*V( : , 2) + V( : , 1)
A*V( : , 4) = 2*V( : , 4) + V( : , 3)
I n mathemati cal notati on, wi th v
j
= v ( : , j ) , the col umns of V and ei genval ues
sati sfy the rel ati onshi ps
Linear A lgebra
6-79
Singular Value Decomposition
Onl y the vari abl e-preci si on numeri c computati on of the si ngul ar val ue
decomposi ti on i s avai l abl e i n the tool box. One reason for thi s i s that the
formul as that resul t from symbol i c computati on are usual l y too l ong and
compl i cated to be of much use. I f A i s a symbol i c matri x of fl oati ng-poi nt or
vari abl e-preci si on numbers, then
S = s v d( A)
computes the si ngul ar val ues of A to an accuracy determi ned by the current
setti ng of di gi t s . And
[ U, S, V] = s v d( A) ;
produces two orthogonal matri ces, U and V, and a di agonal matri x, S, so that
A = U*S*V' ;
Lets l ook at the n-by-n matri x A wi th el ements defi ned by
A( i , j ) = 1/( i - j +1/2)
For n = 5, the matri x i s
[ 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]
I t turns out many of the si ngul ar val ues of these matri ces are cl ose to .
The most obvi ous way of generati ng thi s matri x i s
f or i =1: n
f or j =1: n
A( i , j ) = s y m( 1/( i - j +1/2) ) ;
end
end
A
2
I ( )v
4
v
3
=
A
1
I ( )v
2
v
1
=
6 Sym bolic M ath Toolbox
6-80
The most effi ci ent way to generate the matri x i s
[ J, I ] = mes hgr i d( 1: n) ;
A = s y m( 1. /( I - J+1/2) ) ;
Si nce the el ements of A are the rati os of smal l i ntegers, v pa( A) produces a
vari abl e-preci si on representati on, whi ch i s accurate to di gi t s preci si on. Hence
S = s v d( v pa( A) )
computes the desi red si ngul ar val ues to ful l accuracy. Wi th n = 16 and
di gi t s ( 30) , the resul t i s
S =
[ 1. 20968137605668985332455685357 ]
[ 2. 69162158686066606774782763594 ]
[ 3. 07790297231119748658424727354 ]
[ 3. 13504054399744654843898901261 ]
[ 3. 14106044663470063805218371924 ]
[ 3. 14155754359918083691050658260 ]
[ 3. 14159075458605848728982577119 ]
[ 3. 14159256925492306470284863102 ]
[ 3. 14159265052654880815569479613 ]
[ 3. 14159265349961053143856838564 ]
[ 3. 14159265358767361712392612384 ]
[ 3. 14159265358975439206849907220 ]
[ 3. 14159265358979270342635559051 ]
[ 3. 14159265358979323325290142781 ]
[ 3. 14159265358979323843066846712 ]
[ 3. 14159265358979323846255035974 ]
There are two ways to compare S wi th pi , the fl oati ng-poi nt representati on of
. I n the vector bel ow, the fi rst el ement i s computed by subtr acti on wi th
vari abl e-preci si on ari thmeti c and then converted to a doubl e. The second
el ement i s computed wi th fl oati ng-poi nt ari thmeti c.
f or mat s hor t e
[ doubl e( pi *ones ( 16, 1) - S) pi - doubl e( S) ]
Linear A lgebra
6-81
The resul ts are
1. 9319e+00 1. 9319e+00
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
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
Si nce the rel ati ve accuracy of pi i s pi *eps , whi ch i s 6. 9757e - 16, ei ther col umn
confi rms our suspi ci on that four of the si ngul ar val ues of the 16-by-16 exampl e
equal to fl oati ng-poi nt accuracy.
Eigenvalue Trajectories
Thi s exampl e appl i es several numeri c, symbol i c, and graphi c techni ques to
study the behavi or of matri x ei genval ues as a parameter i n the matri x i s
vari ed. Thi s parti cul ar setti ng i nvol ves numeri cal anal ysi s and perturbati on
theory, but the techni ques i l l ustrated are more wi del y appl i cabl e.
I n thi s exampl e, we consi der a 3-by-3 matri x A whose ei genval ues ar e 1, 2, 3.
Fi rst, we perturb A by another matri x E and parameter t: A A + tE. As t
6 Sym bolic M ath Toolbox
6-82
i ncr eases from 0 to 10
-6
, the ei genval ues
1
= 1,
2
= 2,
3
= 3 change to
1
' 1.5596 + 0.2726i ,
2
' 1.5596 - 0.2726i ,
3
' 2.8808.
Thi s, i n turn, means that for some val ue of t = , 0 < < 10
-6
, the perturbed
matri x A(t) = A + tE has a doubl e ei genval ue
1
=
2
.
Lets fi nd the val ue of t , cal l ed , where thi s happens.
The starti ng poi nt i s a MATLAB test exampl e, known as gal l e r y ( 3) .
A = gal l er y ( 3)
A =
- 149 - 50 - 154
537 180 546
- 27 - 9 - 25
Thi s i s an exampl e of a matri x whose ei genval ues are sensi ti ve to the effects of
roundoff errors i ntroduced duri ng thei r computati on. The actual computed
0 0.5 1 1.5 2 2.5 3 3.5
0.3
0.2
0.1
0
0.1
0.2
0.3
(1) (2) (3)
(1)
(2)
(3)
Linear A lgebra
6-83
ei genval ues may vary from one machi ne to another, but on a typi cal
workstati on, the statements
f or mat l ong
e = ei g( A)
produce
e =
0. 99999999999642
2. 00000000000579
2. 99999999999780
Of course, the exampl e was created so that i ts ei genval ues are actual l y 1, 2, and
3. Note that three or four di gi ts have been l ost to roundoff. Thi s can be easi l y
ver i fi ed wi th the tool box. The statements
B = s y m( A) ;
e = ei g( B) '
p = pol y ( B)
f = f act or ( p)
produce
e =
[ 1, 2, 3]
p =
x ^3- 6*x ^2+11*x - 6
f =
( x - 1) *( x - 2) *( x - 3)
Are the ei genval ues sensi ti ve to the perturbati ons caused by roundoff error
because they are cl ose together? Ordi nari l y, the val ues 1, 2, and 3 woul d be
regarded as wel l separated. But, i n thi s case, the separati on shoul d be vi ewed
on the scal e of the ori gi nal matri x. I f A were repl aced by A/1000, the
ei genval ues, whi ch woul d be .001, .002, .003, woul d seem to be cl oser
together.
But ei genval ue sensi ti vi ty i s more subtl e than just cl oseness. Wi th a careful l y
chosen perturbati on of the matri x, i t i s possi bl e to make two of i ts ei genval ues
6 Sym bolic M ath Toolbox
6-84
coal esce i nto an actual doubl e root that i s extremel y sensi ti ve to roundoff and
other errors.
One good perturbati on di recti on can be obtai ned from the outer product of the
l eft and ri ght ei genvectors associ ated wi th the most sensi ti ve ei genval ue. The
fol l owi ng statement creates
E = [ 130, - 390, 0; 43, - 129, 0; 133, - 399, 0]
the perturbati on matri x
E =
130 - 390 0
43 - 129 0
133 - 399 0
The perturbati on can now be expressed i n terms of a si ngl e, scal ar parameter
t . The statements
s y ms x t
A = A+t *E
repl ace A wi th the symbol i c representati on of i ts perturbati on.
A =
[ - 149+130*t , - 50- 390*t , - 154]
[ 537+43*t , 180- 129*t , 546]
[ - 27+133*t , - 9- 399*t , - 25]
Computi ng the characteri sti c pol ynomi al of thi s new A
p = pol y ( A)
gi ves
p =
x ^3- 6*x ^2+11*x - t *x ^2+492512*t *x - 6- 1221271*t
Prettypri nti ng
pr et t y ( c ol l ect ( p, x ) )
shows more cl earl y that p i s a cubi c i n x whose coeffi ci ents vary l i nearl y wi th t .
3 2
x + ( - t - 6) x + ( 492512 t + 11) x - 6 - 1221271 t
Linear A lgebra
6-85
I t turns out that when t i s vari ed over a very smal l i nterval , from 0 to 1.0e-6,
the desi red doubl e root appears. Thi s can best be seen graphi cal l y. The fi rst
fi gure shows pl ots of p, consi dered as a functi on of x , for three di fferent val ues
of t : t = 0, t = 0.5e-6, and t = 1.0e-6. For each val ue, the ei genval ues are
computed numeri cal l y and al so pl otted.
x = . 8: . 01: 3. 2;
f or k = 0: 2
c = s y m2pol y ( s ubs ( p, t , k*0. 5e- 6) ) ;
y = pol y v al ( c, x ) ;
l ambda = ei g( doubl e( s ubs ( A, t , k*0. 5e - 6) ) ) ;
s ubpl ot ( 3, 1, 3- k)
pl ot ( x , y , ' - ' , x , 0*x , ' : ' , l ambda, 0*l ambda, ' o' )
ax i s ( [ . 8 3. 2 - . 5 . 5] )
t ex t ( 2. 25, . 35, [ ' t = ' num2s t r ( k*0. 5e- 6 ) ] ) ;
end
The bottom subpl ot shows the unperturbed pol ynomi al , wi th i ts three roots at
1, 2, and 3. The mi ddl e subpl ot shows the fi rst two roots approachi ng each
1 1.5 2 2.5 3
0.5
0
0.5
t = 0
1 1.5 2 2.5 3
0.5
0
0.5
t = 5e07
1 1.5 2 2.5 3
0.5
0
0.5
t = 1e06
6 Sym bolic M ath Toolbox
6-86
other. I n the top subpl ot, these two roots have become compl ex and onl y one
real root remai ns.
The next statements compute and di spl ay the actual ei genval ues
e = ei g( A) ;
pr et t y ( e )
showi ng that e( 2) and e( 3) form a compl ex conjugate pai r.
[ 1/3 ]
[ 1/3 %1 - 3 %2 + 2 + 1/3 t ]
[ ]
[ 1/3 1/2 1/3 ]
[ - 1/6 %1 + 3/2 %2 + 2 + 1/3 t + 1/2 i 3 ( 1/3 %1 + 3 %2) ]
[ ]
[ 1/3 1/2 1/3 ]
[ - 1/6 %1 + 3/2 %2 + 2 + 1/3 t - 1/2 i 3 ( 1/3 %1 + 3 %2) ]
2 3
%1 : = 3189393 t - 2216286 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
Linear A lgebra
6-87
Next, the symbol i c representati ons of the three ei genval ues are eval uated at
many val ues of t
t v al s = ( 2: - . 02: 0) ' * 1. e - 6;
r = s i z e( t v al s , 1) ;
c = s i z e( e, 1) ;
l ambda = z er os ( r , c) ;
f or k = 1: c
l ambda( : , k) = doubl e( s ubs ( e( k) , t , t v al s ) ) ;
end
pl ot ( l ambda, t v al s )
x l abel ( ' \l ambda' ) ; y l abel ( ' t ' ) ;
t i t l e( ' Ei genv al ue Tr ans i t i on' )
to produce a pl ot of thei r trajectori es.
Above t = 0. 8e
-6
, the graphs of two of the ei genval ues i ntersect, whi l e bel ow
t = 0.8e
-6
, two real roots become a compl ex conjugate pai r. What i s the preci se
val ue of t that marks thi s transi ti on? Let denote thi s val ue of t .
1 1.2 1.4 1.6 1.8 2 2.2 2.4 2.6 2.8 3
0
0.2
0.4
0.6
0.8
1
1.2
1.4
1.6
1.8
2
x 10
6
t
Eigenvalue Transition
6 Sym bolic M ath Toolbox
6-88
One way to fi nd i s based on the fact that, at a doubl e root, both the functi on
and i ts deri vati ve must vani sh. Thi s resul ts i n two pol ynomi al equati ons to be
sol ved for two unknowns. The statement
s ol = s ol v e( p, di f f ( p, ' x ' ) )
sol ves the pai r of al gebrai c equati ons p = 0 and dp/dx = 0 and produces
s ol =
t : [ 4x 1 s y m]
x : [ 4x 1 s y m]
Fi nd now by
t au = doubl e( s ol . t ( 2) )
whi ch reveal s that the second el ement of s ol . t i s the desi red val ue of .
f or mat s hor t
t au =
7. 8379e- 07
Therefore, the second el ement of s ol . x
s i gma = doubl e( s ol . x ( 2) )
i s the doubl e ei genval ue
s i gma =
1. 5476
Lets veri fy that thi s val ue of does i ndeed produce a doubl e ei genval ue at
= 1.5476. To achi eve thi s, substi tute for t i n the perturbed matri x
A(t) = A + tE and fi nd the ei genval ues of A(t). That i s,
e = ei g( doubl e( s ubs ( A, t , t au) ) )
e =
1. 5476
1. 5476
2. 9047
confi rms that = 1.5476 i s a doubl e ei genval ue of A(t) for t = 7.8379e-07.
Solving Equations
6-89
Solving Equations
Solving Algebraic Equations
I f S i s a symbol i c expressi on,
s ol v e( S)
attempts to fi nd val ues of the symbol i c vari abl e i n S (as determi ned by
f i nds y m) for whi ch S i s zer o. For exampl e,
s y ms a b c x
S = a*x ^2 + b*x + c;
s ol v e( S)
uses the fami l i ar quadrati c formul a to produce
ans =
[ 1/2/a*( - b+( b^2- 4*a*c) ^( 1/2) ) ]
[ 1/2/a*( - b- ( b^2- 4*a*c) ^( 1/2) ) ]
Thi s i s a symbol i c vector whose el ements are the two sol uti ons.
I f you want to sol ve for a speci fi c vari abl e, you must speci fy that vari abl e as an
addi ti onal argument. For exampl e, i f you want to sol ve S for b, use the
command
b = s ol v e( S, b)
whi ch returns
b =
- ( a*x ^2+c) /x
Note that these exampl es assume equati ons of the form f(x) = 0. I f you need to
sol ve equati ons of the form f(x) = q(x), you must use quoted stri ngs. I n
parti cul ar, the command
s = s ol v e( ' cos ( 2*x ) +s i n( x ) =1' )
6 Sym bolic M ath Toolbox
6-90
returns a vector wi th four sol uti ons.
s =
[ 0]
[ pi ]
[ 1/6*pi ]
[ 5/6*pi ]
Several Algebraic Equations
Now l et's l ook at systems of equati ons. Suppose we have the system
and we want to sol ve for xand y. Fi rst create the necessary symbol i c objects.
s y ms x y al pha
There are several ways to address the output of s ol v e. One i s to use a
two-output cal l
[ x , y ] = s ol v e( x ^2*y ^2, x - y /2- al pha)
whi ch returns
x =
[ 0]
[ 0]
[ al pha]
[ al pha]
y =
[ - 2*al pha]
[ - 2*al pha]
[ 0]
[ 0]
Consequentl y, the sol uti on vector
v = [ x , y ]
x
2
y
2
0 =
x
y
2
--- =
Solving Equations
6-91
appears to have redundant components. Thi s i s due to the fi rst equati on
x
2
y
2
= 0, whi ch has two sol uti ons i n x and y: x = 0, y = 0. Changi ng the
equati ons to
eqs 1 = ' x ^2*y ^2=1, x - y /2- al pha'
[ x , y ] = s ol v e( eqs 1)
produces four di sti nct sol uti ons.
x =
[ 1/2*al pha+1/2*( al pha^2+2) ^( 1/2) ]
[ 1/2*al pha- 1/2*( al pha^2+2) ^( 1/2) ]
[ 1/2*al pha+1/2*( al pha^2- 2) ^( 1/2) ]
[ 1/2*al pha- 1/2*( al pha^2- 2) ^( 1/2) ]
y =
[ - al pha+( al pha^2+2) ^( 1/2) ]
[ - al pha- ( al pha^2+2) ^( 1/2) ]
[ - al pha+( al pha^2- 2) ^( 1/2) ]
[ - al pha- ( al pha^2- 2) ^( 1/2) ]
Si nce we di d not speci fy the dependent vari abl es, s ol v e uses f i nds y mto
determi ne the vari abl es.
Thi s way of assi gni ng output from s ol v e i s qui te successful for smal l systems.
Pl ai nl y, i f we had, say, a 10-by-10 system of equati ons, typi ng
[ x 1, x 2, x 3, x 4, x 5, x 6, x 7, x 8, x 9, x 10] = s ol v e( . . . )
i s both awkward and ti me consumi ng. To ci rcumvent thi s di ffi cul ty, s ol v e can
return a structure whose fi el ds are the sol uti ons. I n parti cul ar, consi der the
system u^2- v ^2 = a^2, u + v = 1, a^2- 2*a = 3. The command
S = s ol v e( ' u^2- v ^2 = a^2' , ' u + v = 1' , ' a^2- 2*a = 3' )
returns
S =
a: [ 2x 1 s y m]
u: [ 2x 1 s y m]
v : [ 2x 1 s y m]
6 Sym bolic M ath Toolbox
6-92
The sol uti ons for a resi de i n the a-fi el d of S. That i s,
S. a
produces
ans =
[ - 1]
[ 3]
Si mi l ar comments appl y to the sol uti ons for u and v . The structure S can now
be mani pul ated by fi el d and i ndex to access a parti cul ar porti on of the sol uti on.
For exampl e, i f we want to exami ne the second sol uti on, we can use the
fol l owi ng statement
s 2 = [ S. a( 2) , S. u( 2) , S. v ( 2) ]
to extract the second component of each fi el d.
s 2 =
[ 3, 5, - 4]
The fol l owi ng statement
M = [ S. a, S. u, S. v ]
creates the sol uti on matri x M
M =
[ - 1, 1, 0]
[ 3, 5, - 4]
whose rows compri se the di sti nct sol uti ons of the system.
Li near systems of si mul taneous equati ons can al so be sol ved usi ng matri x
di vi si on. For exampl e,
cl ear u v x y
s y ms u v x y
S = s ol v e( x +2*y - u, 4*x +5*y - v ) ;
s ol = [ S. x ; S. y ]
Solving Equations
6-93
and
A = [ 1 2; 4 5] ;
b = [ u; v ] ;
z = A\b
resul t i n
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 ]
Thus s and z produce the same sol uti on, al though the resul ts are assi gned to
di fferent vari abl es.
Single Differential Equation
The functi on ds ol v e computes symbol i c sol uti ons to ordi nary di fferenti al
equati ons. The equati ons are speci fi ed by symbol i c expressi ons contai ni ng the
l etter Dto denote di fferenti ati on. The symbol s D2, D3, ... DN, correspond to the
second, thi rd, ..., Nth deri vati ve, respecti vel y. Thus, D2y i s the Symbol i c Math
Tool box equi val ent of d
2
y/dt
2
. The dependent vari abl es are those preceded by D
and the defaul t i ndependent vari abl e i s t . Note that names of symbol i c
vari abl es shoul d not contai n D. The i ndependent vari abl e can be changed from
t to some other symbol i c vari abl e by i ncl udi ng that vari abl e as the l ast i nput
argument.
I ni ti al condi ti ons can be speci fi ed by addi ti onal equati ons. I f i ni ti al condi ti ons
are not speci fi ed, the sol uti ons contai n constants of i ntegrati on, C1, C2, etc.
The output from ds ol v e paral l el s the output from s ol v e. That i s, you can cal l
ds ol v e wi th the number of output vari abl es equal to the number of dependent
vari abl es or pl ace the output i n a structure whose fi el ds contai n the sol uti ons
of the di fferenti al equati ons.
6 Sym bolic M ath Toolbox
6-94
Ex a mple 1
The fol l owi ng cal l to ds ol v e
ds ol v e( ' Dy =1+y ^2' )
uses y as the dependent vari abl e and t as the defaul t i ndependent vari abl e.
The output of thi s command i s
ans =
t an( t +C1)
To speci fy an i ni ti al condi ti on, use
y = ds ol v e( ' Dy =1+y ^2' , ' y ( 0) =1' )
Thi s produces
y =
t an( t +1/4*pi )
Noti ce that y i s i n the MATLAB workspace, but the i ndependent vari abl e t i s
not. Thus, the command di f f ( y , t ) returns an error. To pl ace t i n the
workspace, type s y ms t .
Ex a mple 2
Nonl i near equati ons may have mul ti pl e sol uti ons, even when i ni ti al condi ti ons
are gi ven.
x = ds ol v e( ' ( Dx ) ^2+x ^2=1' , ' x ( 0) =0' )
resul ts i n
x =
[ - s i n( t ) ]
[ s i n( t ) ]
Ex a mple 3
Here i s a second order di fferenti al equati on wi th two i ni ti al condi ti ons. The
commands
y = ds ol v e( ' D2y =cos ( 2*x ) - y ' , ' y ( 0) =1' , ' Dy ( 0) =0' , ' x ' )
s i mpl i f y ( y )
Solving Equations
6-95
produce
y =
- 2/3*cos ( x ) ^2+1/3+4/3*cos ( x )
The key i ssues i n thi s exampl e are the order of the equati on and the i ni ti al
condi ti ons. To sol ve the ordi nary di fferenti al equati on
u(0) = 1, u'(0) = -1, u''(0) =
si mpl y type
u = ds ol v e( ' D3u=u' , ' u( 0) =1' , ' Du( 0) =- 1' , ' D2u( 0) = pi ' , ' x ' )
Use D3u to represent d
3
u/dx
3
and D2u( 0) for u''(0).
Several Differential Equations
The functi on ds ol v e can al so handl e several ordi nary di fferenti al equati ons i n
several vari abl es, wi th or wi thout i ni ti al condi ti ons. For exampl e, here i s a pai r
of l i near, fi rst order equati ons.
S = ds ol v e( ' Df = 3*f +4*g' , ' Dg = - 4*f +3*g' )
The computed sol uti ons are returned i n the structure S. You can determi ne the
val ues of f and g by typi ng
f = S. f
f =
C2*ex p( 3*t ) *s i n( 4*t ) - C1*e x p( 3*t ) *cos ( 4*t )
g = S. g
g =
C1*ex p( 3*t ) *s i n( 4*t ) +C2*e x p( 3*t ) *cos ( 4*t )
x
3
3
d
d u
u =
6 Sym bolic M ath Toolbox
6-96
I f you prefer to recover f and g di rectl y as wel l as i ncl ude i ni ti al condi ti ons,
type
[ f , g] = ds ol v e( ' Df =3*f +4*g, Dg =- 4*f +3*g' , ' f ( 0) = 0, g( 0) = 1' )
f =
ex p( 3*t ) *s i n( 4*t )
g =
ex p( 3*t ) *cos ( 4*t )
Thi s tabl e detai l s some exampl es and Symbol i c Math Tool box syntax. Note that
the fi nal entry i n the tabl e i s the Ai ry di fferenti al equati on whose sol uti on i s
referred to as the Ai ry functi on.
The Ai ry functi on pl ays an i mportant rol e i n the mathemati cal model i ng of the
di spersi on of water waves.
Differential Equation MATLAB Command
y = ds ol v e( ' Dy +4*y = ex p( - t ) ' ,
' y ( 0) = 1' )
y = ds ol v e( ' D2y +4*y = ex p( - 2*x ) ' ,
' y ( 0) =0' , ' y ( pi ) = 0' , ' x ' )
(The Ai ry Equati on)
y = ds ol v e( ' D2y = x *y ' , ' y ( 0) = 0' ,
' y ( 3) = bes s el k( 1/3, 2*s qr t ( 3) ) /pi ' ,
' x ' )
t d
dy
4y t ( ) + e
t
=
y 0 ( ) 1 =
x
2
2
d
d y
4y x ( ) + e
2x
=
y 0 ( ) 0 y ( ) 0 = , =
x
2
2
d
d y
xy x ( ) =
y 0 ( ) 0 y 3 ( )
1
---K
1
3
---
2 3 ( ) = , =
A
MATLAB Qui ck Reference
Introduction . . . . . . . . . . . . . . . . . . . . A-2
General PurposeCommands . . . . . . . . . . . . A-3
Operators and Special Characters . . . . . . . . . . A-4
Logical Functions . . . . . . . . . . . . . . . . . A-5
LanguageConstructs and Debugging . . . . . . . . A-5
Elementary Matrices and Matrix Manipulation . . . . A-6
Specialized Matrices . . . . . . . . . . . . . . . . A-7
Elementary Math Functions . . . . . . . . . . . . . A-7
Specialized Math Functions . . . . . . . . . . . . . A-8
CoordinateSystemConversion . . . . . . . . . . . A-9
Matrix Functions - Numerical Linear Algebra . . . . . A-9
Data Analysis and Fourier TransformFunctions . . . A-10
Polynomial and Interpolation Functions . . . . . . . A-11
Function Functions - Nonlinear Numerical Methods . . A-12
SparseMatrix Functions . . . . . . . . . . . . . . A-12
Sound ProcessingFunctions . . . . . . . . . . . . A-13
Character StringFunctions . . . . . . . . . . . . . A-14
Low-Level FileI/O Functions . . . . . . . . . . . . A-14
BitwiseFunctions . . . . . . . . . . . . . . . . . A-15
StructureFunctions . . . . . . . . . . . . . . . . A-15
Object Functions . . . . . . . . . . . . . . . . . . A-16
Cell Array Functions . . . . . . . . . . . . . . . . A-16
Multidimensional Array Functions . . . . . . . . . . A-16
Plottingand Data Visualization . . . . . . . . . . . A-16
Graphical User InterfaceCreation . . . . . . . . . . A-20
A M A TLA B Q uick Reference
A-2
Introduction
Thi s appendi x l i sts the MATLAB functi ons as they are grouped i n the Hel p
Desk by subject. Each tabl e contai ns the functi on names and bri ef descri pti ons.
For compl ete i nformati on about any of these functi ons, refer to the Hel p Desk
and ei ther:
Sel ect the functi on from the MATLAB Functi ons l i st (By Subject or
By I ndex), or
Type the functi on name i n the Go to MATLAB function fi el d and cl i ck Go.
Note I f you are vi ewi ng thi s book from the Hel p Desk, you can cl i ck on any
functi on name and jump di rectl y to the correspondi ng MATLAB functi on
page.
G eneral Purpose C om m ands
A-3
A
General Purpose Commands
Thi s set of functi ons l ets you start and stop
MATLAB, work wi th fi l es and the operati ng
system, control the command wi ndow, and manage
the envi ronment, vari abl es, and the workspace.
Managing Commands and Functions
addpat h Add di rectori es to MATLABs
search path
doc Di spl ay HTML documentati on
i n Web br owser
docopt Di spl ay l ocati on of hel p fi l e
di r ectory for UNI X pl atforms
hel p Onl i ne hel p for MATLAB
functi ons and M-fi l es
hel pdes k Di spl ay Hel p Desk page i n Web
br owser, gi vi ng access to
extensi ve hel p
hel pwi n Di spl ay Hel p Wi ndow, provi di ng
access to hel p for al l commands
l as t er r Last er ror message
l as t war n Last warni ng message
l ookf or Keyword search through al l hel p
entri es
par t i al pat h Parti al pathname
pat h Control MATLABs di r ectory
search path
pat ht ool Start Path Browser, a GUI for
vi ewi ng and modi fyi ng
MATLABs path
pr of i l e Start the M-fi l e pr ofi l er, a uti l i ty
for debuggi ng and opti mi zi ng
code
pr of r epor t Generate a pr ofi l e report
r mpat h Remove di r ectori es from
MATLABs search path
t y pe Li st fi l e
v er Di spl ay versi on i nformati on for
MATLAB, Si mul i nk, and
tool boxes
v er s i on MATLAB versi on number
web Poi nt Web browser at fi l e or Web
si te
what Di rectory l i sti ng of M-fi l es,
MAT-fi l es, and MEX-fi l es
what s ne w Di spl ay README fi l es for
MATLAB and tool boxes
whi ch Locate functi ons and fi l es
Managing Variables and the Workspace
cl ear Remove i tems from memor y
di s p Di spl ay text or array
l engt h Length of vector
l oad Retr i eve vari abl es from di sk
ml ock Prevent M-fi l e cl ear i ng
munl ock Al l ow M-fi l e cl eari ng
openv ar Open workspace vari abl e i n
Array Edi tor for gr aphi cal
edi ti ng
pack Consol i date workspace memory
s av e Save workspace var i abl es on
di sk
s av eas Save fi gure or model usi ng
speci fi ed format
s i z e Array di mensi ons
who, whos Li st di rectory of vari abl es i n
memory
wor ks pace Di spl ay the Workspace Browser,
a GUI for managi ng the
workspace
Managing Commands and Functions
(Continued)
A M A TLA B Q uick Reference
A-4
Operators and Special Characters
These are the actual operators you use to enter and
mani pul ate data, for exampl e, matri x
mul ti pl i cati on, array mul ti pl i cati on, and l i ne
conti nuati on.
Controlling the Command Window
cl c Cl ear command wi ndow
echo Echo M-fi l es duri ng executi on
f or mat Control the output di spl ay
format
home Move the cursor to the home
posi ti on
mor e Control paged output for the
command wi ndow
Working with Files and the Operating
Environment
cd Change worki ng di rector y
copy f i l e Copy fi l e
del et e Del ete fi l es and graphi cs objects
di ar y Save sessi on i n a di sk fi l e
di r Di rectory l i sti ng
edi t Edi t an M-fi l e
f i l epar t s Fi l ename parts
f ul l f i l e Bui l d ful l fi l ename from parts
i nmem Functi ons i n memory
l s Li st di rectory on UNI X
mat l abr oot Root di r ectory of MATLAB
i nstal l ati on
mkdi r Make di r ectory
open Open fi l es based on extensi on
pwd Di spl ay current di rectory
t empdi r Return the name of the systems
tempor ar y di rectory
t empname Uni que name for temporary fi l e
! Execute operati ng system
command
Starting and Quitting MATLAB
mat l abr c MATLAB startup M-fi l e
qui t Termi nate MATLAB
s t ar t up MATLAB startup M-fi l e
Operators and Special Characters
+ Pl us
- Mi nus
* Matr i x mul ti pl i cati on
. * Array mul ti pl i cati on
^ Matr i x power
. ^ Array power
kr on Kronecker tensor product
\ Backsl ash or l eft di vi si on
/ Sl ash or ri ght di vi si on
. / and . \ Array di vi si on, ri ght and l eft
: Col on
( ) Parentheses
[ ] Brackets
{} Curl y braces
. Deci mal poi nt
. . . Conti nuati on
, Comma
; Semi col on
% Comment
! Excl amati on poi nt
' Transpose and quote
. ' Nonconjugated transpose
= Assi gnment
Logical Functions
A-5
Logical Functions
Thi s set of functi ons performs l ogi cal operati ons
such as checki ng i f a fi l e or vari abl e exi sts and
testi ng i f al l el ements i n an array are nonzero.
Operators and Speci al Characters contai ns other
operators that perform l ogi cal operati ons.
Language Constructs and Debugging
These functi ons l et you work wi th MATLAB as a
programmi ng l anguage. For exampl e, you can
control program fl ow, defi ne gl obal vari abl es,
perform i nteracti ve i nput, and debug your code.
== Equal i ty
< > Rel ati onal oper ators
& Logi cal and
| Logi cal or
~ Logi cal not
x or Logi cal excl usi ve or
Logical Functions
al l Test to determi ne i f al l el ements
ar e nonzer o
any Test for any nonzeros
ex i s t Check i f a vari abl e or fi l e exi sts
f i nd Fi nd i ndi ces and val ues of
nonzero el ements
i s * Detect state
i s a Detect an object of a gi ven cl ass
l ogi cal Convert numeri c val ues to
l ogi cal
mi s l ocked True i f M-fi l e cannot be cl eared
Operators and Special Characters (Continued)
MATLAB as a Programming Language
bui l t i n Execute bui l ti n functi on fr om
overl oaded method
ev al I nterpret str i ngs contai ni ng
MATLAB expressi ons
ev al c Eval uate MATLAB expressi on
wi th capture
ev al i n Eval uate expressi on i n
workspace
f ev al Functi on eval uati on
f unct i on Functi on M-fi l es
gl obal Defi ne gl obal vari abl es
nar gchk Check number of i nput
ar guments
per s i s t ent Defi ne persi stent vari abl e
s cr i pt Scr i pt M-fi l es
Control Flow
br eak Termi nate executi on of f or l oop
or whi l e l oop
cas e Case swi tch
cat ch Begi n cat ch bl ock
el s e Condi ti onal l y execute
statements
el s ei f Condi ti onal l y execute
statements
end Termi nate f or , whi l e, s wi t ch,
t r y, and i f statements or
i ndi cate l ast
er r or Di spl ay error messages
f or Repeat statements a speci fi c
number of ti mes
i f Condi ti onal l y execute
statements
ot her wi s e Defaul t part of s wi t ch
statement
r et ur n Retur n to the i nvoki ng functi on
A M A TLA B Q uick Reference
A-6
Elementary Matrices and Matrix
Manipulation
Usi ng these functi ons you can mani pul ate
matri ces, and access ti me, date, speci al vari abl es,
and constants, functi ons.
s wi t ch Swi tch among several cases
based on expressi on
t r y Begi n t r y bl ock
war ni ng Di spl ay war ni ng message
whi l e Repeat statements an i ndefi ni te
number of ti mes
Interactive Input
i nput Request user i nput
key boar d I nvoke the keyboard i n an M-fi l e
menu Generate a menu of choi ces for
user i nput
paus e Hal t executi on temporari l y
Object-Oriented Programming
cl as s Create object or return cl ass of
object
doubl e Convert to doubl e preci si on
i nf er i or t o I nferi or cl ass rel ati onshi p
i nl i ne Construct an i nl i ne object
i nt 8, i nt 16, i nt 32 Convert to si gned i nteger
i s a Detect an object of a gi ven cl ass
l oadobj Extends the l oad functi on for
user objects
s av eobj Save fi l ter for objects
s i ngl e Convert to si ngl e pr eci si on
s uper i or t o Superi or cl ass rel ati onshi p
ui nt 8, ui nt 16,
ui nt 32
Convert to unsi gned i nteger
Debugging
dbcl ear Cl ear br eakpoi nts
dbcont Resume executi on
dbdown Change l ocal workspace context
dbmex Enabl e MEX-fi l e debuggi ng
dbqui t Qui t debug mode
Control Flow (Continued)
dbs t ack Di spl ay functi on cal l stack
dbs t at us Li st al l breakpoi nts
dbs t ep Execute one or more l i nes from a
br eakpoi nt
dbs t op Set breakpoi nts i n an M-fi l e
functi on
dbt y pe Li st M-fi l e wi th l i ne numbers
dbup Change l ocal workspace context
Elementary Matrices and Arrays
bl kdi ag Construct a bl ock di agonal
matri x from i nput arguments
ey e I denti ty matri x
l i ns pac e Generate l i near l y spaced vectors
l ogs pac e Generate l ogar i thmi cal l y spaced
vector s
ones Create an array of al l ones
r and Uni forml y di str i buted random
numbers and arrays
r andn Normal l y di stri buted random
numbers and arrays
z er os Create an array of al l zer os
: ( col on) Regul arl y spaced vector
Special Variables and Constants
ans The most r ecent answer
comput e r I denti fy the computer on whi ch
MATLAB i s runni ng
eps Fl oati ng-poi nt rel ati ve accuracy
f l ops Count fl oati ng-poi nt operati ons
i I magi nary uni t
Debugging (Continued)
Specialized M atrices
A-7
Specialized Matrices
These functi ons l et you work wi th matri ces such as
Hadamard, Hankel , Hi l bert, and magi c squares.
Elementary Math Functions
These are many of the standard mathemati cal
functi ons such as tri gonometri c, hyperbol i c,
l ogari thmi c, and compl ex number mani pul ati on.
I nf I nfi ni ty
i nput name I nput ar gument name
j I magi nary uni t
NaN Not-a-Number
nar gi n, nar gout Number of functi on arguments
pi Rati o of a ci rcl es ci rcumference
to i ts di ameter,
r eal max Lar gest posi ti ve fl oati ng-poi nt
number
r eal mi n Smal l est posi ti ve fl oati ng-poi nt
number
v ar ar gi n,
v ar ar gout
Pass or retur n var i abl e numbers
of arguments
Time and Dates
cal endar Cal endar
cl ock Current ti me as a date vector
cput i me El apsed CPU ti me
dat e Current date stri ng
dat enum Seri al date number
dat es t r Date stri ng format
dat ev ec Date components
eomday End of month
et i me El apsed ti me
now Current date and ti me
t i c, t oc Stopwatch ti mer
weekday Day of the week
Matrix Manipulation
cat Concatenate arr ays
di ag Di agonal matri ces and di agonal s
of a matri x
f l i pl r Fl i p matri ces l eft-ri ght
f l i pud Fl i p matri ces up-down
r epmat Repl i cate and ti l e an ar ray
r es hape Reshape ar ray
Special Variables and Constants (Continued)
r ot 90 Rotate matri x 90 degrees
t r i l Lower tri angul ar par t of a
matri x
t r i u Upper tri angul ar part of a
matri x
: ( col on) I ndex i nto array, rearr ange
ar ray
Specialized Matrices
compan Compani on matri x
gal l er y Test matri ces
hadamar d Hadamard matri x
hankel Hankel matr i x
hi l b Hi l ber t matri x
i nv hi l b I nverse of the Hi l bert matri x
magi c Magi c square
pas cal Pascal matri x
t oepl i t z Toepl i tz matri x
wi l ki ns on Wi l ki nsons ei genval ue test
matri x
Elementary Math Functions
abs Absol ute val ue and compl ex
magni tude
acos , acos h I nverse cosi ne and i nverse
hyper bol i c cosi ne
Matrix Manipulation (Continued)
A M A TLA B Q uick Reference
A-8
Specialized Math Functions
Thi s set of functi ons i ncl udes Bessel , el l i pti c,
gamma, factori al , and others.
acot , acot h I nverse cotangent and i nverse
hyperbol i c cotangent
acs c, acs ch I nverse cosecant and i nverse
hyperbol i c cosecant
angl e Phase angl e
as ec, as ech I nverse secant and i nverse
hyperbol i c secant
as i n, as i nh I nverse si ne and i nver se
hyperbol i c si ne
at an, at anh I nverse tangent and i nverse
hyperbol i c tangent
at an2 Four-quadr ant i nverse tangent
cei l Round toward i nfi ni ty
compl ex Construct compl ex data from
real and i magi nary components
conj Compl ex conjugate
cos , cos h Cosi ne and hyper bol i c cosi ne
cot , cot h Cotangent and hyperbol i c
cotangent
cs c, cs ch Cosecant and hyperbol i c
cosecant
ex p Exponenti al
f i x Round towards zero
f l oor Round towards mi nus i nfi ni ty
gcd Greatest common di vi sor
i mag I magi nary part of a compl ex
number
l cm Least common mul ti pl e
l og Natur al l ogari thm
l og2 Base 2 l ogari thm and di ssect
fl oati ng-poi nt numbers i nto
exponent and
l og10 Common (base 10) l ogari thm
mod Modul us (si gned remai nder
after di vi si on)
nchoos e k Bi nomi al coeffi ci ent or al l
combi nati ons
Elementary Math Functions (Continued)
r eal Real part of compl ex number
r em Remai nder after di vi si on
r ound Round to nearest i nteger
s ec, s e ch Secant and hyperbol i c secant
s i gn Si gnum functi on
s i n, s i nh Si ne and hyper bol i c si ne
s qr t Square root
t an, t anh Tangent and hyperbol i c tangent
Specialized Math Functions
ai r y Ai r y functi ons
bes s el h Bessel functi ons of the thi rd
ki nd (Hankel functi ons)
bes s el i , bes s el k Modi fi ed Bessel functi ons
bes s el j , bes s el y Bessel functi ons
bet a, bet ai nc,
bet al n
beta, betai nc, betal n
el l i pj Jacobi el l i pti c functi ons
el l i pke Compl ete el l i pti c i ntegral s of the
fi r st and second ki nd
er f , er f c, er f cx ,
er f i nv
Error functi ons
ex pi nt Exponenti al i ntegr al
f act or i al Factor i al functi on
gamma, gammai nc ,
gammal n
Gamma functi ons
l egendr e Associ ated Legendr e functi ons
pow2 Base 2 power and scal e
fl oati ng-poi nt numbers
r at , r at s Rati onal fracti on approxi mati on
Elementary Math Functions (Continued)
C oordinate System C onversion
A-9
Coordinate System Conversion
Usi ng these functi ons you can transform
Cartesi an coordi nates to pol ar, cyl i ndri cal , or
spheri cal , and vi ce versa.
Matrix Functions - Numerical Linear
Algebra
These functi ons l et you perform matri x anal ysi s
i ncl udi ng matri x determi nant, rank, reduced row
echel on form, ei genval ues, and i nverses.
Coordinate System Conversion
car t 2pol Transfor m Cartesi an
coordi nates to pol ar or
cyl i ndri cal
car t 2s ph Transfor m Cartesi an
coordi nates to spheri cal
pol 2car t Transfor m pol ar or cyl i ndri cal
coordi nates to Car tesi an
s ph2car t Transfor m spher i cal coordi nates
to Cartesi an
Matrix Analysis
cond Condi ti on number wi th r espect
to i nversi on
condei g Condi ti on number wi th r espect
to ei genval ues
det Matr i x determi nant
nor m Vector and matri x nor ms
nul l Nul l space of a matr i x
or t h Range space of a matri x
r ank Rank of a matri x
r cond Matr i x reci procal condi ti on
number esti mate
r r ef , r r ef mov i e Reduced row echel on form
s ubs pac e Angl e between two subspaces
t r ace Sum of di agonal el ements
Linear Equations
chol Chol esky factori zati on
i nv Matr i x i nverse
l s cov Least squares sol uti on i n the
presence of known covari ance
l u LU matri x factor i zati on
l s qnonneg Nonnegati ve l east squar es
pi nv Moor e-Penr ose pseudoi nverse of
a matri x
qr Orthogonal -tri angul ar
decomposi ti on
Eigenvalues and Singular Values
bal ance I mprove accuracy of computed
ei genval ues
cdf 2r df Convert compl ex di agonal for m
to real bl ock di agonal form
ei g Ei genval ues and ei genvector s
gs v d General i zed si ngul ar val ue
decomposi ti on
hes s Hessenberg form of a matri x
pol y Pol ynomi al wi th speci fi ed roots
qz QZ factori zati on for general i zed
ei genval ues
r s f 2cs f Convert real Schur form to
compl ex Schur form
s chur Schur decomposi ti on
s v d Si ngul ar val ue decomposi ti on
Matrix Functions
ex pm Matr i x exponenti al
f unm Eval uate functi ons of a matr i x
l ogm Matr i x l ogari thm
s qr t m Matr i x square r oot
A M A TLA B Q uick Reference
A-10
Data Analysis and Fourier Transform
Functions
Usi ng the data anal ysi s functi ons, you can fi nd
permutati ons, pri me numbers, mean, medi an,
vari ance, correl ati on, and perform convol uti ons
and other standard array mani pul ati ons. A set of
vector functi ons l ets you operate on vectors to fi nd
cross product, uni on, and other standard vector
mani pul ati ons. The Fouri er transform functi ons
l et you perform di screte Fouri er transformati ons i n
one or more di mensi ons and thei r i nverses.
Low Level Functions
qr del et e Del ete col umn from QR
factori zati on
qr i ns er t I nsert col umn i n QR
factori zati on
Basic Operations
conv hul l Convex hul l
cumpr od Cumul ati ve pr oduct
cums um Cumul ati ve sum
cumt r apz Cumul ati ve trapezoi dal
numer i cal i ntegrati on
del aunay Del aunay tr i angul ati on
ds ear ch Search for nearest poi nt
f act or Pri me factors
i npol y gon Detect poi nts i nsi de a pol ygonal
regi on
max Maxi mum el ements of an array
mean Average or mean val ue of arrays
medi an Medi an val ue of arr ays
mi n Mi ni mum el ements of an arr ay
per ms Al l possi bl e permutati ons
pol y ar e a Area of pol ygon
pr i mes Generate l i st of pri me numbers
pr od Product of ar ray el ements
s or t Sor t el ements i n ascendi ng
or der
s or t r ows Sor t rows i n ascendi ng order
s t d Standard devi ati on
s um Sum of arr ay el ements
t r apz Trapezoi dal numeri cal
i ntegrati on
t s ear ch Search for encl osi ng Del aunay
tri angl e
v ar Vari ance
v or onoi Voronoi di agram
Finite Differences
del 2 Di screte Lapl aci an
di f f Di fferences and approxi mate
der i vati ves
gr adi ent Numeri cal gradi ent
Correlation
cor r coe f Cor rel ati on coeffi ci ents
cov Covari ance matr i x
Filtering and Convolution
conv Convol uti on and pol ynomi al
mul ti pl i cati on
conv 2 Two-di mensi onal convol uti on
deconv Deconvol uti on and pol ynomi al
di vi si on
f i l t er Fi l ter data wi th an i nfi ni te
i mpul se r esponse (I I R) or fi ni te
i mpul se r esponse
f i l t er 2 Two-di mensi onal di gi tal
fi l teri ng
Basic Operations (Continued)
Polynom ial and Interpolation Functions
A-11
Polynomial and Interpolation
Functions
These functi ons l et you operate on pol ynomi al s
such as mul ti pl y, di vi de, fi nd deri vati ves, and
eval uate. The data i nterpol ati on functi ons l et you
per for m i nter pol ati on i n one, two, three, and
hi gher di mensi ons.
Fourier Transforms
abs Absol ute val ue and compl ex
magni tude
angl e Phase angl e
cpl x pai r Sort compl ex numbers i nto
compl ex conjugate pai rs
f f t One-di mensi onal fast Four i er
transform
f f t 2 Two-di mensi onal fast Fouri er
transform
f f t s hi f t Shi ft DC component of fast
Fouri er tr ansform to center of
spectr um
i f f t I nverse one-di mensi onal fast
Fouri er tr ansform
i f f t 2 I nverse two-di mensi onal fast
Fouri er tr ansform
i f f t n I nverse mul ti di mensi onal fast
Fouri er tr ansform
i f f t s hi f t I nverse FFT shi ft
nex t pow2 Next power of two
unwr ap Cor rect phase angl es
Vector Functions
cr os s Vector cross product
i nt er s e ct Set i nter secti on of two vectors
i s membe r Detect members of a set
s et di f f Return the set di fference of two
vector
s et x or Set excl usi ve or of two vectors
uni on Set uni on of two vectors
uni que Uni que el ements of a vector
Polynomials
conv Convol uti on and pol ynomi al
mul ti pl i cati on
deconv Deconvol uti on and pol ynomi al
di vi si on
pol y Pol ynomi al wi th speci fi ed roots
pol y der Pol ynomi al deri vati ve
pol y ei g Pol ynomi al ei genval ue probl em
pol y f i t Pol ynomi al curve fi tti ng
pol y v al Pol ynomi al eval uati on
pol y v al m Matr i x pol ynomi al eval uati on
r es i due Convert between par ti al fracti on
expansi on and pol ynomi al
coeffi ci ents
r oot s Pol ynomi al roots
Data Interpolation
gr i ddat a Data gri ddi ng
i nt er p1 One-di mensi onal data
i nterpol ati on (tabl e l ookup)
i nt er p2 Two-di mensi onal data
i nterpol ati on (tabl e l ookup)
i nt er p3 Three-di mensi onal data
i nterpol ati on (tabl e l ookup)
i nt er pf t One-di mensi onal i nterpol ati on
usi ng the FFT method
i nt er pn Mul ti di mensi onal data
i nterpol ati on (tabl e l ookup)
mes hgr i d Generate X and Y matri ces for
three-di mensi onal pl ots
ndgr i d Generate arrays for
mul ti di mensi onal functi ons and
i nterpol ati on
s pl i ne Cubi c spl i ne i nterpol ati on
A M A TLA B Q uick Reference
A-12
Function Functions - Nonlinear
Numerical Methods
Usi ng these functi ons you can sol ve di fferenti al
equati ons, perform numeri cal eval uati on of
i ntegral s, and opti mi ze functi ons.
Sparse Matrix Functions
These functi ons al l ow you to operate on a speci al
type of matri x, sparse. Usi ng these functi ons you
can convert ful l to sparse, vi sual i ze, and operate on
these matri ces.
Function Functions - Nonlinear Numerical
Methods
dbl quad Numeri cal doubl e i ntegrati on
f mi nbnd Mi ni mi ze a functi on of one
vari abl e
f mi ns ear ch Mi ni mi ze a functi on of several
vari abl es
f z er o Zero of a functi on of one vari abl e
ode45, ode23,
ode113, ode15s ,
ode23s , ode23t ,
ode23t b
Sol ve di fferenti al equati ons
odef i l e Defi ne a di ffer enti al equati on
probl em for ODE sol vers
odeget Extract pr oper ti es fr om opti ons
structure created wi th odeset
odes et Create or al ter opti ons structure
for i nput to ODE sol vers
quad, quad8 Numeri cal eval uati on of
i ntegral s
v ect or i z e Vectori ze expr essi on
Elementary Sparse Matrices
s pdi ags Extract and create sparse band
and di agonal matri ces
s pey e Sparse i denti ty matri x
s pr and Sparse uni for ml y di stri buted
random matri x
s pr andn Sparse normal l y di stri buted
random matri x
s pr ands y m Sparse symmetri c r andom
matri x
Full to Sparse Conversion
f i nd Fi nd i ndi ces and val ues of
nonzer o el ements
f ul l Convert spar se matri x to ful l
matri x
s par s e Create sparse matri x
s pconv e r t I mpor t matri x from sparse
matri x exter nal format
Working with Nonzero Entries of Sparse
Matrices
nnz Number of nonzero matri x
el ements
nonz er os Nonzero matr i x el ements
nz max Amount of storage al l ocated for
nonzer o matri x el ements
s pal l oc Al l ocate space for spar se matri x
s pf un Appl y functi on to nonzero sparse
matri x el ements
s pones Repl ace nonzero spar se matri x
el ements wi th ones
Visualizing Sparse Matrices
s py Vi sual i ze sparsi ty pattern
Reordering Algorithms
col mmd Sparse col umn mi ni mum degree
per mutati on
col per m Sparse col umn permutati on
based on nonzero count
Elementary Sparse Matrices (Continued)
Sound Processing Functions
A-13
Sound Processing Functions
The sound processi ng functi ons l et you convert
si gnal s, and read and wri te . au and . wav sound
fi l es.
dmper m Dul mage-Mendel sohn
decomposi ti on
r andper m Random permutati on
s y mmmd Sparse symmetri c mi ni mum
degr ee orderi ng
s y mr cm Sparse reverse Cuthi l l -McKee
orderi ng
Norm, Condition Number, and Rank
condes t 1-norm matr i x condi ti on number
esti mate
nor mes t 2-norm esti mate
Sparse Systems of Linear Equations
bi cg Bi Conjugate Gradi ents method
bi cgs t ab Bi Conjugate Gradi ents
Stabi l i zed method
cgs Conjugate Gradi ents Squared
method
chol i nc Sparse I ncompl ete Chol esky and
Chol esky-I nfi ni ty factori zati ons
chol updat e Rank 1 update to Chol esky
factori zati on
gmr es General i zed Mi ni mum Resi dual
method (wi th r estarts)
l ui nc I ncompl ete LU matri x
factori zati ons
pcg Precondi ti oned Conjugate
Gradi ents method
qmr Quasi -Mi ni mal Resi dual method
qr Orthogonal -tri angul ar
decomposi ti on
qr del et e Del ete col umn from QR
factori zati on
qr i ns er t I nsert col umn i n QR
factori zati on
qr updat e Rank 1 update to QR
factori zati on
Reordering Algorithms (Continued)
Sparse Eigenvalues and Singular Values
ei gs Fi nd ei genval ues and
ei genvectors
s v ds Fi nd si ngul ar val ues
Miscellaneous
s ppar ms Set parameters for sparse
matri x routi nes
General Sound Functions
l i n2mu Convert l i near audi o si gnal to
mu-l aw
mu2l i n Convert mu-l aw audi o si gnal to
l i near
s ound Convert vector i nto sound
s ounds c Scal e data and pl ay as sound
SPARCstation-Specific Sound Functions
aur ead Read NeXT/SUN (. au) sound fi l e
auwr i t e Wr i te NeXT/SUN (. au) sound
fi l e
.WAV Sound Functions
wav r ead Read Mi cr osoft WAVE (. wav )
sound fi l e
wav wr i t e Wr i te Mi crosoft WAVE (. wav )
sound fi l e
A M A TLA B Q uick Reference
A-14
Character String Functions
Thi s set of functi ons l ets you mani pul ate stri ngs
such as compari son, concatenati on, search, and
conversi on.
Low-Level File I/ O Functions
The l ow-l evel fi l e I /O functi ons al l ow you to open
and cl ose fi l es, read and wri te formatted and
unformatted data, operate on fi l es, and perform
other speci al i zed fi l e I /O such as readi ng and
wri ti ng i mages and spreadsheets.
General
abs Absol ute val ue and compl ex
magni tude
ev al I nterpret str i ngs contai ni ng
MATLAB expressi ons
r eal Real part of compl ex number
s t r i ngs MATLAB stri ng handl i ng
String Manipulation
debl ank Stri p trai l i ng bl anks from the
end of a str i ng
f i nds t r Fi nd one str i ng wi thi n another
l ower Convert stri ng to l ower case
s t r cat Stri ng concatenati on
s t r cmp Compare stri ngs
s t r cmpi Compare stri ngs i gnori ng case
s t r j us t Justi fy a char acter array
s t r mat c h Fi nd possi bl e matches for a
stri ng
s t r ncmp Compare the fi rst n char acters
of two str i ngs
s t r r ep Stri ng search and r epl ace
s t r t ok Fi rst token i n str i ng
s t r v cat Verti cal concatenati on of stri ngs
s y mv ar Determi ne symbol i c vari abl es i n
an expressi on
t ex l abe l Produce the TeX format fr om a
character stri ng
upper Convert stri ng to upper case
String to Number Conversion
char Create character arr ay (str i ng)
i nt 2s t r I nteger to stri ng conver si on
mat 2s t r Convert a matri x i nto a stri ng
num2s t r Number to stri ng conver si on
s pr i nt f Wr i te for matted data to a stri ng
s s canf Read stri ng under format
contr ol
s t r 2doubl e Convert stri ng to
doubl e-preci si on val ue
s t r 2num Stri ng to number conversi on
Radix Conversion
bi n2dec Bi nary to deci mal number
conversi on
dec2bi n Deci mal to bi nary number
conversi on
dec2hex Deci mal to hexadeci mal number
conversi on
hex 2dec I EEE hexadeci mal to deci mal
number conversi on
hex 2num Hexadeci mal to doubl e number
conversi on
File Opening and Closing
f cl os e Cl ose one or more open fi l es
f open Open a fi l e or obtai n i nformati on
about open fi l es
Bitw ise Functions
A-15
Bitwise Functions
These functi ons l et you operate at the bi t l evel
such as shi fti ng and compl ementi ng.
Structure Functions
Structures are arrays whose el ements can hol d
any MATLAB data type such as text, numeri c
arrays, or other structures. You access structure
el ements by name. Use the structure functi ons to
create and operate on thi s array type.
Unformatted I/ O
f r ead Read bi nar y data from fi l e
f wr i t e Wr i te bi nary data to a fi l e
Formatted I/ O
f get l Return the next l i ne of a fi l e as a
stri ng wi thout l i ne termi nator(s)
f get s Return the next l i ne of a fi l e as a
stri ng wi th l i ne termi nator(s)
f pr i nt f Wr i te formatted data to fi l e
f s canf Read formatted data from fi l e
File Positioning
f eof Test for end-of-fi l e
f er r or Quer y MATLAB about errors i n
fi l e i nput or output
f r ewi nd Rewi nd an open fi l e
f s eek Set fi l e posi ti on i ndi cator
f t el l Get fi l e posi ti on i ndi cator
String Conversion
s pr i nt f Wr i te for matted data to a stri ng
s s canf Read stri ng under format
contr ol
Specialized File I/ O
dl mr ead Read an ASCI I del i mi ted fi l e
i nto a matr i x
dl mwr i t e Wr i te a matr i x to an ASCI I
del i mi ted fi l e
hdf HDF i nterface
i mf i nf o Return i nformati on about a
graphi cs fi l e
i mr ead Read i mage from graphi cs fi l e
i mwr i t e Wr i te an i mage to a gr aphi cs fi l e
t ex t r ead Read formatted data from text
fi l e
wk1r ead Read a Lotus123 WK1
spreadsheet fi l e i nto a matri x
wk1wr i t e Wr i te a matr i x to a Lotus123
WK1 spreadsheet fi l e
Bitwise Functions
bi t and Bi t-wi se AND
bi t cmp Compl ement bi ts
bi t or Bi t-wi se OR
bi t max Maxi mum fl oati ng-poi nt i nteger
bi t s et Set bi t
bi t s hi f t Bi t-wi se shi ft
bi t get Get bi t
bi t x or Bi t-wi se XOR
Structure Functions
deal Deal i nputs to outputs
f i el dnames Fi el d names of a structure
get f i el d Get fi el d of structure array
r mf i el d Remove structure fi el ds
s et f i el d Set fi el d of structure arr ay
s t r uct Create str ucture array
s t r uct 2cel l Structur e to cel l array
conversi on
Specialized File I/ O (Continued)
A M A TLA B Q uick Reference
A-16
Object Functions
Usi ng the object functi ons you can create objects,
detect objects of a gi ven cl ass, and return the cl ass
of an object.
Cell Array Functions
Cel l arrays are arrays compri sed of cel l s, whi ch can
hol d any MATLAB data type such as text, numeri c
arrays, or other cel l arrays. Unl i ke structures, you
access these cel l s by number. Use the cel l array
functi ons to create and operate on these arrays.
Multidimensional Array Functions
These functi ons provi de a mechani sm for worki ng
wi th arrays of di mensi on greater than 2.
Plotting and Data Visualization
Thi s extensi ve set of functi ons gi ves you the abi l i ty
to create basi c graphs such as bar, pi e, pol ar, and
three-di mensi onal pl ots, and advanced graphs
such as surface, mesh, contour, and vol ume
vi sual i zati on pl ots. I n addi ti on, you can use these
functi ons to control l i ghti ng, col or, vi ew, and many
other fi ne mani pul ati ons.
Object Functions
cl as s Create object or return cl ass of
object
i s a Detect an object of a gi ven cl ass
Cell Array Functions
cel l Create cel l array
cel l f un Appl y a functi on to each el ement
i n a cel l array
cel l s t r Create cel l array of str i ngs from
character ar ray
cel l 2s t r uct Cel l array to structure array
conversi on
cel l di s p Di spl ay cel l arr ay contents
cel l pl ot Graphi cal l y di spl ay the
structure of cel l arrays
num2cel l Convert a numeri c array i nto a
cel l ar r ay
Multidimensional Array Functions
cat Concatenate arr ays
f l i pdi m Fl i p ar ray al ong a speci fi ed
di mensi on
i nd2s ub Subscr i pts from l i near i ndex
i per mut e I nverse per mute the di mensi ons
of a mul ti di mensi onal array
ndgr i d Generate arrays for
mul ti di mensi onal functi ons and
i nterpol ati on
ndi ms Number of arr ay di mensi ons
per mut e Rearrange the di mensi ons of a
mul ti di mensi onal array
r es hape Reshape ar ray
s hi f t di m Shi ft di mensi ons
s queez e Remove si ngl eton di mensi ons
s ub2i nd Si ngl e i ndex from subscr i pts
Basic Plots and Graphs
bar Verti cal bar chart
bar h Hori zontal bar chart
hi s t Pl ot hi stograms
hol d Hol d curr ent graph
l ogl og Pl ot usi ng l og-l og scal es
pi e Pi e pl ot
pl ot Pl ot vector s or matri ces.
pol ar Pol ar coordi nate pl ot
s emi l ogx Semi -l og scal e pl ot
Plotting and D ata Visualization
A-17
s emi l ogy Semi -l og scal e pl ot
s ubpl ot Create axes i n ti l ed posi ti ons
Three-Dimensional Plotting
bar 3 Verti cal 3-D bar chart
bar 3h Hori zontal 3-D bar chart
comet 3 Thr ee-di mensi onal comet pl ot
cy l i nde r Generate cyl i nder
f i l l 3 Draw fi l l ed 3-D pol ygons i n
3-space
pl ot 3 Pl ot l i nes and poi nts i n 3-D
space
qui v er 3 Thr ee-di mensi onal qui ver (or
vel oci ty) pl ot
s l i ce Vol umetri c sl i ce pl ot
s pher e Generate sphere
s t em3 Pl ot di screte surface data
wat er f al l Waterfal l pl ot
Plot Annotation and Grids
cl abel Add contour l abel s to a contour
pl ot
dat et i c k Date for matted ti ck l abel s
gr i d Gri d l i nes for 2-D and 3-D pl ots
gt ex t Pl ace text on a 2-D graph usi ng
a mouse
l egend Graph l egend for l i nes and
patches
pl ot edi t Start pl ot edi t mode to edi t and
annotate pl ots
pl ot y y Pl ot gr aphs wi th Y ti ck l abel s on
the l eft and ri ght
t i t l e Ti tl es for 2-D and 3-D pl ots
x l abel X-axi s l abel s for 2-D and 3-D
pl ots
Basic Plots and Graphs (Continued)
y l abel Y-axi s l abel s for 2-D and 3-D
pl ots
z l abel Z-axi s l abel s for 3-D pl ots
Surface, Mesh, and Contour Plots
cont our Contour (l evel curves) pl ot
cont our c Contour computati on
cont our f Fi l l ed contour pl ot
hi dden Mesh hi dden l i ne removal mode
mes hc Combi nati on mesh/contour pl ot
mes h 3-D mesh wi th r efer ence pl ane
peaks A sampl e functi on of two
vari abl es
s ur f 3-D shaded sur face graph
s ur f ace Create surface l ow-l evel objects
s ur f c Combi nati on surf/contourpl ot
s ur f l 3-D shaded sur face wi th l i ghti ng
t r i mes h Tri angul ar mesh pl ot
t r i s ur f Tri angul ar surface pl ot
Volume Visualization
conepl ot Pl ot vel oci ty vectors as cones i n
3-D vector fi el d
cont our s l i ce Draw contours i n vol ume sl i ce
pl ane
i s ocaps Compute i sosurface end-cap
geometry
i s onor mal s Compute nor mal s of i sosur face
ver ti ces
i s os ur f ace Extr act i sosurface data from
vol ume data
r educepat ch Reduce the number of patch
faces
r educev ol ume Reduce number of el ements i n
vol ume data set
s hr i nkf aces Reduce the si ze of patch faces
s moot h3 Smooth 3-D data
Plot Annotation and Grids (Continued)
A M A TLA B Q uick Reference
A-18
s t r eam2 Compute 2-D stream l i ne data
s t r eam3 Compute 3-D stream l i ne data
s t r eaml i ne Draw stream l i nes from 2- or
3-D vector data
s ur f 2pat ch Convert sur face data to patch
data
s ubv ol ume Extract subset of vol ume data
set
Domain Generation
gr i ddat a Data gri ddi ng and sur face
fi tti ng
mes hgr i d Generati on of X and Y ar rays for
3-D pl ots
Specialized Plotting
ar ea Area pl ot
box Axi s box for 2-D and 3-D pl ots
comet Comet pl ot
compas s Compass pl ot
conv hul l Convex hul l
del aunay Del aunay tr i angul ati on
ds ear ch Search Del aunay tri angul ati on
for nearest poi nt
er r or bar Pl ot graph wi th er ror bars
ez cont our Easy to use contour pl otter
ez cont our f Easy to use fi l l ed contour pl otter
ez mes h Easy to use 3-D mesh pl otter
ez mes hc Easy to use combi nati on mesh/
contour pl otter
ez pl ot Easy to use functi on pl otter
ez pl ot 3 Easy to use 3-D parametri c
curve pl otter
ez pol ar Easy to use pol ar coordi nate
pl otter
ez s ur f Easy to use 3-D col ored surface
pl otter
Volume Visualization (Continued)
ez s ur f c Easy to use combi nati on sur face/
contour pl otter
f eat her Feather pl ot
f i l l Draw fi l l ed 2-D pol ygons
f pl ot Pl ot a functi on
i npol y gon True for poi nts i nsi de a
pol ygonal regi on
par et o Pareto char
pcol or Pseudocol or (checker boar d) pl ot
pi e3 Three-di mensi onal pi e pl ot
pl ot mat r i x Scatter pl ot matri x
pol y ar e a Area of pol ygon
qui v er Qui ver (or vel oci ty) pl ot
r i bbon Ri bbon pl ot
r os e Pl ot rose or angl e hi stogr am
s cat t er Scatter pl ot
s cat t er 3 Three-di mensi onal scatter pl ot
s t ai r s Stai r step graph
s t em Pl ot di screte sequence data
t s ear ch Search for encl osi ng Del aunay
tri angl e
v or onoi Voronoi di agram
View Control
camdol l y Move camera posi ti on and tar get
caml ookat Vi ew speci fi c objects
camor bi t Orbi t about camera target
campan Rotate camer a target about
camera posi ti on
campos Set or get camer a posi ti on
campr oj Set or get projecti on type
camr ol l Rotate camer a about vi ewi ng
axi s
camt ar get Set or get camer a target
camup Set or get camer a up-vector
camv a Set or get camer a vi ew angl e
Specialized Plotting (Continued)
Plotting and D ata Visualization
A-19
camz oom Zoom camera i n or out
das pect Set or get data aspect rati o
pbas pec t Set or get pl ot box aspect rati o
v i ew Thr ee-di mensi onal graph
vi ewpoi nt speci fi cati on.
v i ewmt x Generate vi ew transformati on
matri ces
x l i m Set or get the current x-axi s
l i mi ts
y l i m Set or get the current y-axi s
l i mi ts
z l i m Set or get the current z-axi s
l i mi ts
Lighting
caml i ght Create or posi ti on a l i ght
l i ght angl e Spheri cal posi ti on of a l i ght
l i ght i ng Li ghti ng mode
mat er i al Materi al refl ectance mode
Color Operations
br i ght e n Bri ghten or darken col or map
cax i s Pseudocol or axi s scal i ng
col or bar Di spl ay col or bar (col or scal e)
col or de f Set up col or defaul ts
col or map Set the col or l ook-up tabl e
gr ay mon Graphi cs fi gure defaul ts set for
grayscal e moni tor
hs v 2r gb Hue-saturati on-val ue to
red-green-bl ue conversi on
r gb2hs v RGB to HSV conversi on
r gbpl ot Pl ot col or map
s hadi ng Col or shadi ng mode
s pi nmap Spi n the col ormap
View Control (Continued)
s ur f nor m Three-di mensi onal surface
normal s
whi t ebg Change axes background col or
for pl ots
Colormaps
aut umn Shades of red and yel l ow col or
map
bone Gray-scal e wi th a ti nge of bl ue
col or map
cont r as t Gray col or map to enhance
i mage contrast
cool Shades of cyan and magenta
col or map
copper Li near copper-tone col or map
f l ag Al ternati ng red, whi te, bl ue, and
bl ack col or map
gr ay Li near gray-scal e col or map
hot Bl ack-red-yel l ow-whi te col or
map
hs v Hue-saturati on-val ue (HSV)
col or map
j et Vari ant of HSV
l i nes Li ne col or col ormap
pr i s m Col or map of pri sm col ors
s pr i ng Shades of magenta and yel l ow
col or map
s ummer Shades of green and yel l ow
col ormap
wi nt er Shades of bl ue and gr een col or
map
Printing
or i ent Hardcopy paper or i entati on
pr i nt Pri nt graph or save graph to fi l e
pr i nt opt Confi gure l ocal pri nter defaul ts
s av eas Save fi gure to graphi c fi l e
Color Operations (Continued)
A M A TLA B Q uick Reference
A-20
Graphical User Interface Creation
The graphi cal user i nterface functi ons l et you bui l d
your own i nterfaces for your appl i cati ons.
Handle Graphics, General
copy obj Make a copy of a graphi cs object
and i ts chi l dren
f i ndobj Fi nd objects wi th speci fi ed
property val ues
gcbo Return object whose cal l back i s
currentl y executi ng
gco Return handl e of cur rent object
get Get object pr operti es
i s handl e True for graphi cs objects
r ot at e Rotate objects about speci fi ed
ori gi n and di r ecti on
s et Set object pr oper ti es
Handle Graphics, Object Creation
ax es Create axes object
f i gur e Create fi gure (graph) wi ndows
i mage Create i mage (2-D matri x)
l i ght Create l i ght object (i l l umi nates
Patch and Surface)
l i ne Create l i ne object (3-D pol yl i nes)
pat ch Create patch object (pol ygons)
r ect angl e Create rectangl e object (2-D
rectangl e)
s ur f ace Create surface (quadr i l ateral s)
t ex t Create text object (char acter
stri ngs)
ui cont e x t menu Create context menu (pop-up
associ ated wi th object)
Handle Graphics, Figure Windows
capt ur e Screen capture of the current
fi gur e
cl c Cl ear fi gure wi ndow
cl f Cl ear fi gure
cl os e Cl ose speci fi ed wi ndow
gcf Get cur rent fi gure handl e
newpl ot Graphi cs M-fi l e preambl e for
Nex t Pl ot property
r ef r es h Refr esh fi gure
s av eas Save fi gure or model to desi red
output for mat
Handle Graphics, Axes
ax i s Pl ot axi s scal i ng and appear ance
cl a Cl ear axes
gca Get current axes handl e
Object Manipulation
r es et Reset axi s or fi gur e
r ot at e3d I nteracti vel y rotate the vi ew of a
3-D pl ot
s el ect mov er es i z e I nteracti vel y sel ect, move, or
resi ze objects
Interactive User Input
gi nput Graphi cal i nput from a mouse or
cursor
z oom Zoom i n and out on a 2-D pl ot
Region of Interest
dr agr ec t Drag XOR rectangl es wi th
mouse
dr awnow Compl ete any pendi ng dr awi ng
r bbox Rubber band box
Dialog Boxes
di al og Create a di al og box
er r or dl g Create err or di al og box
Handle Graphics, Figure Windows (Continued)
G raphical U ser Interface C reation
A-21
hel pdl g Di spl ay hel p di al og box
i nput dl g Create i nput di al og box
l i s t dl g Create l i st sel ecti on di al og box
ms gbox Create message di al og box
pagedl g Di spl ay page l ayout di al og box
pr i nt dl g Di spl ay pri nt di al og box
ques t dl g Create questi on di al og box
ui get f i l e Di spl ay di al og box to retri eve
name of fi l e for readi ng
ui put f i l e Di spl ay di al og box to retri eve
name of fi l e for wr i ti ng
ui s et col or I nteracti vel y set a Col or Spec
usi ng a di al og box
ui s et f ont I nteracti vel y set a font usi ng a
di al og box
war ndl g Create warni ng di al og box
User Interface Objects
menu Generate a menu of choi ces for
user i nput
ui cont e x t menu Create context menu
ui cont r ol Create user i nterface contr ol
ui menu Create user i nterface menu
Other Functions
dr agr ec t Drag rectangl es wi th mouse
gcbo Return handl e of object whose
cal l back i s executi ng
r bbox Create rubber band box for area
sel ecti on
s el ect mov er es i z e Sel ect, move, resi ze, or copy axes
and ui control gr aphi cs objects
t ex t wr ap Return wr apped str i ng matr i x
for gi ven ui contr ol
ui r es ume Used wi th ui wai t , contr ol s
program executi on
Dialog Boxes (Continued)
ui wai t Used wi th ui r es ume, control s
program executi on
wai t bar Di spl ay wai t bar
wai t f or but t onpr e s s Wai t for key/buttonpress over
fi gur e
Other Functions (Continued)
A M A TLA B Q uick Reference
A-22
B
Symbol i c Math Tool box
Qui ck Reference
Introduction . . . . . . . . . . . . . . . . . . . . B-2
Arithmetic Operations . . . . . . . . . . . . . . . B-3
Basic Operations . . . . . . . . . . . . . . . . . . B-3
Calculus . . . . . . . . . . . . . . . . . . . . . . B-3
Conversions . . . . . . . . . . . . . . . . . . . . B-3
Integral Transforms . . . . . . . . . . . . . . . . B-3
Linear Algebra . . . . . . . . . . . . . . . . . . . B-3
Pedagogical and Graphical Applications . . . . . . . B-4
Simplification . . . . . . . . . . . . . . . . . . . B-4
Solution of Equations . . . . . . . . . . . . . . . . B-4
VariablePrecision Arithmetic . . . . . . . . . . . . B-4
B Sym bolic M ath Toolbox Q uick Reference
B-2
Introduction
Thi s appendi x l i sts the Symbol i c Math Tool box functi ons that are avai l abl e i n
the Student Versi on of MATLAB & Si mul i nk. For compl ete i nformati on about
any of these functi ons, use the Hel p Desk and ei ther:
Sel ect the functi on from the Symbolic Math Toolbox Functions, or
Sel ect OnlineManuals and vi ew the Symbolic Math Toolbox Users Guide.
Note Al l of the functi ons l i sted i n Symbol i c Math Tool box Functi ons are
avai l abl e i n the Student Versi on of MATLAB & Si mul i nk except mapl e,
mapl ei ni t , mf un, mf unl i s t , and mhel p.
Sym bolic M ath Toolbox Q uick Reference
B-3
B
Arithmetic Operations
+ Addi ti on
- Subtracti on
* Mul ti pl i cati on
. * Array mul ti pl i cati on
/ Ri ght di vi si on
. / Array r i ght di vi si on
\ Left di vi si on
. \ Array l eft di vi si on
^ Matr i x or scal ar rai sed to a power
. ^ Array r ai sed to a power
Compl ex conjugate transpose
. Real tr anspose
Basic Operations
ccode C code repr esentati on of a
symbol i c expr essi on
conj Compl ex conjugate
f i nds y m Determi ne symbol i c var i abl es
f or t r an Fortr an representati on of a
symbol i c expr essi on
i mag I magi nary part of a compl ex
number
l at ex LaTeX representati on of a
symbol i c expr essi on
pr et t y Pretty pri nt a symbol i c expr essi on
r eal Real part of an i magi nary number
s y m Create symbol i c object
s y ms Shortcut for cr eati ng mul ti pl e
symbol i c objects
Calculus
di f f Di fferenti ate
i nt I ntegrate
j acobi an Jacobi an matri x
l i mi t Li mi t of an expressi on
s y ms um Summati on of seri es
t ay l or Tayl or seri es expansi on
Conversions
char Convert s y mobject to str i ng
doubl e Convert symbol i c matri x to doubl e
pol y 2s y m Functi on cal cul ator
s y m2pol y Symbol i c pol ynomi al to coeffi ci ent
vector
Integral Transforms
f our i er Fouri er tr ansform
i f our i e r I nverse Fouri er tr ansform
i l apl ac e I nverse Lapl ace tr ansform
i z t r ans I nverse z-transfor m
l apl ace Lapl ace tr ansform
z t r ans z-transfor m
Linear Algebra
col s pac e Basi s for col umn space
det Determi nant
di ag Create or extract di agonal s
ei g Ei genval ues and ei genvector s
ex pm Matr i x exponenti al
i nv Matr i x i nverse
j or dan Jordan canoni cal form
nul l Basi s for nul l space
pol y Char acteri sti c pol ynomi al
r ank Matr i x rank
r r ef Reduced row echel on form
s v d Si ngul ar val ue decomposi ti on
t r i l Lower tri angl e
t r i u Upper tri angl e
Calculus (Continued)
B Sym bolic M ath Toolbox Q uick Reference
B-4
Pedagogical and Graphical Applications
ez pl ot Easy-to-use functi on pl otter
f unt ool Functi on cal cul ator
r s ums Ri emann sums
Simplification
col l ect Col l ect common terms
ex pand Expand pol ynomi al s and
el ementary functi ons
f act or Factor
hor ner Nested pol ynomi al representati on
numden Numerator and denomi nator
s i mpl e Search for shortest form
s i mpl i f y Si mpl i fi cati on
s ubex pr Rewri te i n terms of
subexpressi ons
Solution of Equations
compos e Functi onal composi ti on
ds ol v e Sol uti on of di fferenti al equati ons
f i nv er s e Functi onal i nverse
s ol v e Sol uti on of al gebr ai c equati ons
Variable Precision Arithmetic
di gi t s Set vari abl e preci si on accuracy
v pa Vari abl e preci si on ari thmeti c
I-1
I ndex
Symbols
: oper ator 3-8
@s y m di rectory 6-15
\ 6-68, 6-69
A
abstract functi ons 6-10
accessi ng onl i ne i nformati on 3-37
Ai ry di ffer enti al equati on 6-96
Ai ry functi on 6-96
al gori thms
vectori zi ng 5-23
ani mati on 4-31
annotati ng pl ots 4-13
ans 3-5
Appl i cati on Pr ogram I nterface (API ) 1-12
array oper ators 3-23
arrays 3-19, 3-22
cel l 5-9
character 5-11
col umnwi se organi zati on 3-24
concatenati ng 3-17
cr eati ng i n M-fi l es 3-16
del eti ng rows and col umns 3-18
del eti ng rows or col umns 3-18
el ements 3-11
generati ng wi th functi ons and operators 3-15
l i sti ng contents 3-11
l oadi ng from exter nal data fi l es 3-16
mul ti di mensi onal 5-7
notati on for el ements 3-11
preal l ocati ng 5-23
structure 5-14
vari abl e names 3-11
aspect rati o of axes 4-11
axes 4-11
axi s
l abel s 4-12
ti tl es 4-12
ax i s 4-11
B
backsl ash operator 6-68
Bessel functi ons
di fferenti ati ng 6-17
i ntegrati ng 6-24
bes s el j 6-17
bes s el k 6-96
bi t map 4-20
bi twi se functi ons A-15
bl ockset 1-13
books
MATLAB-rel ated 1-9
branch cut 6-43
br e ak 5-5
C
cal cul us 6-166-43
c as e 5-4
cel l array functi ons A-16
cel l arrays 5-9
c har 5-13
char acter arrays 5-11
char acter str i ng functi ons A-14
char acteri sti c pol ynomi al 3-22, 6-72, 6-74
ci rcul ant matri x 6-10, 6-56
c l e ar 3-33
c l e ar 6-26
Index
I-2
cl eari ng vari abl es
i n the Mapl e workspace 6-27
i n the MATLAB workspace 6-27
col l e c t 6-45
col on operator 3-8
col ormap 4-17
col ors
l i nes for pl otti ng 4-4
col s pace 6-70
col umn space 6-70
command l i ne edi ti ng 3-31
commands
general pur pose A-3A-4
comp. s of t - s y s . mat l ab 1-9
compl ex numbers, pl otti ng 4-6
compl ex symbol i c var i abl es 6-9
concatenati ng
arrays 3-17
stri ngs 5-13
concatenati on 3-17
confi guri ng addi ti onal products
on Li nux 2-13
on PC 2-4
conj 6-9
constants
speci al 3-13
conti nui ng statements on mul ti pl e l i nes 3-31
converti ng symbol i c matri ces to numer i c for m
6-8
coordi nate system conversi on A-9
D
data anal ysi s and Fouri er tr ansfor m functi ons
A-10A-11
debuggi ng commands A-6
deci mal symbol i c expressi ons 6-8
defaul t symbol i c vari abl e 6-12
defi ni te i ntegrati on 6-23
del eti ng array el ements 3-18
demonstrati on programs 5-27
demos
MATLAB 1-6
demos 5-27
deter mi nant of matri x 3-20
di ag 3-6
di ar y 3-35
di f f 6-16
di fferenti ati on 6-166-19
di gi t s 6-8
di sconti nui ti es 6-42
doc 3-38
documentati on path
setti ng on Li nux 2-12
doubl e 6-64
ds ol v e 6-94
E
edi ti ng command l i nes 3-31
e i g 6-71
ei genval ue 3-21
ei genval ue trajector i es 6-816-88
ei genval ues 6-716-77, 6-82
computi ng 6-71
ei genvector 3-21, 6-72
el ementary math functi ons A-7A-8
el ementary matri ces and matri x mani pul ati on
A-6A-7
el ements of arrays 3-11
enteri ng matri ces 3-4
e ps 6-8
erase mode 4-31
e v al 5-22
Index
I-3
e x pand 6-46
expressi ons 3-11, 3-14
eval uati ng 5-22
external progr ams
runni ng fr om MATLAB 3-35
e z pl ot 6-30
F
f act or 6-47
f i gur e 4-9
fi gure wi ndows 4-9
wi th mul ti pl e pl ots 4-9
fi l e mani pul ati on 3-35
f i nd 3-27
fi ndi ng object handl es 4-28
f i nds y m6-13
f l i pl r 3-7
fl oati ng-poi nt ari thmeti c 6-61
fl oati ng-poi nt numbers 3-12
fl oati ng-poi nt symbol i c expressi ons 6-7
fl ow control 5-2
f or 5-4
format
of output di spl ay 3-29
f or mat 3-29, 6-61
f unc t i on 5-19
functi on functi ons 5-24
nonl i near numeri cal methods A-12
functi on M-fi l e 5-17, 5-19
functi on of two vari abl es 4-15
functi ons 3-12, 5-19
bi twi se A-15
bui l t-i n 3-13
cel l array A-16
character stri ng A-14
coor di nate system conversi on A-9
data anal ysi s and Fouri er transform
A-10A-11
el ementary math A-7A-8
el ementary matri ces and matri x mani pul ati on
A-6A-7
fi ndi ng speci fi c 1-7
functi on functi ons - nonl i near numeri cal
methods A-12
general purpose commands A-3A-4
graphi cal user i nter face control A-20A-21
l anguage constr ucts and debuggi ng A-5A-6
l ogi cal A-5
l ow-l evel fi l e I /O A-14A-15
matri x functi ons - numeri cal l i near al gebr a
A-9A-10
mul ti di mensi onal array A-16
object A-16
operators and speci al characters A-4A-5
pl otti ng and data vi sual i zati on A-16A-20
pol ynomi al and i nterpol ati on A-11
sound processi ng A-13
sparse matr i x A-12A-13
speci al i zed math A-8
speci al i zed matr i ces A-7
str ucture A-15
vari abl e number of ar guments 5-20
G
Gi vens transformati on 6-66, 6-75
gl obal vari abl es 5-20
gol den rati o 6-6
graphi cal user i nter face 4-30
graphi cal user i nter face control A-20A-21
graphi cs
2-D 4-2
annotati ons 4-13
Index
I-4
fi l es 4-21
handl e graphi cs 4-23
objects 4-23
pri nti ng 4-21
gri ds 4-12
H
Handl e Graphi cs 1-12, 4-23
fi ndi ng handl es 4-28
properti es 4-26
hel p
sources of 1-8
vi a MathWorks Knowl edge Base 1-10
vi a MathWorks Web si te 1-9
vi a newsgroup 1-9
he l p 3-39
Hel p Desk 3-37
accessi ng 1-8
accessi ng on Li nux 2-14
accessi ng on PC 2-5
hi erarchy of graphi cs objects 4-24
Hi l bert matri x 6-8, 6-67
hol d 4-7
hor ne r 6-46
I
I EEE fl oati ng-poi nt ari thmeti c 6-62
i f 5-2
i mages 4-19
i magi nary number 3-11
i nstal l ati on procedure
sel ecti ng pr oducts on Li nux 2-11
i nstal l ati on root di rectory
speci fyi ng 2-10
i nstal l i ng MATLAB
on Li nux 2-8
on PC 2-4
i nstal l i ng tool boxes
on Li nux 2-14
on PC 2-5
i nt 6-22
i ntegrati on 6-226-27
defi ni te 6-23
wi th real constants 6-24
J
j ac obi an 6-18
Jacobi an matri x 6-18
j or dan 6-77
Jordan canoni cal form 6-776-79
L
l anguage constructs A-5A-6
l earni ng MATLAB 1-6
l ege nd 4-3
l egend, addi ng to pl ot 4-3
l i brary
mathemati cal functi on 1-12
l i cense agreement 2-10
l i ghti ng 4-17
l i mi t 6-20
l i mi ts 6-206-21
two-si ded 6-20
undefi ned 6-21
l i mi ts, axes 4-11
l i ne conti nuati on 3-31
l i ne styl es of pl ots 4-4
l i near al gebra 6-666-88
Index
I-5
Li nux
setti ng onl i ne documentati on path 2-12
l oad 3-16
l oadi ng arrays 3-16
l ocal vari abl es 5-20
l ogi cal functi ons A-5
l ogi cal vectors 3-26
l ookf or 3-40
l ow-l evel fi l e I /O functi ons A-14A-15
M
machi ne epsi l on 6-8
Macl auri n seri es 6-28
magi c 3-9
magi c square 3-5
Mapl e 6-2
markers 4-5
MAT-fi l e 4-19
mathemati cal functi on l i brary 1-12
mathemati cal functi ons
l i sti ng advanced 3-13
l i sti ng el ementary 3-12
l i sti ng matri x 3-13
MathWorks Knowl edge Base 1-10
MathWorks Store
purchasi ng products fr om 1-9
MathWorks Web si te 1-9
MATLAB
Appl i cati on Pr ogram I nterface 1-12
books 1-9
cal l i ng C subrouti ne 2-3, 2-7
cal l i ng Fortran subr outi ne 2-3, 2-7
demos 1-6
Handl e Graphi cs 1-12
hi stor y 1-11
how to l ear n 1-6
i nstal l i ng on Li nux 2-8
i nstal l i ng on PC 2-4
l anguage 1-12
mathemati cal functi on l i br ary 1-12
overvi ew 1-11
pr oduct fami l y 1-14
recor di ng a sessi on 3-35
star ti ng 3-2
star ti ng on Li nux 2-13
supported Li nux compi l ers 2-8
supported PC compi l ers 2-3
worki ng envi ronment 1-12
workspace 3-33
mat l ab command
setti ng up symbol i c l i nk 2-12
mat l abdoc command
setti ng up symbol i c l i nk 2-12
matri ces 3-19
creati ng 3-15
enteri ng 3-4
matri x 3-3
anti di agonal 3-7
determi nant 3-20
mai n di agonal 3-6
si ngul ar 3-20
swappi ng col umns 3-9
symmetri c 3-19
transpose 3-5
matri x condi ti on number 6-69
matri x functi ons - numeri cal l i near al gebr a
A-9A-10
matri x mul ti pl i cati on 3-20
mesh pl ot 4-15
mex command
setti ng up symbol i c l i nk 2-12
MEX-fi l e 2-3, 2-7
supported PC compi l ers 2-3
Index
I-6
M-fi l e 1-11, 3-16, 5-17
creati ng 5-17, 6-15
for creati ng arrays 3-16
functi on 5-17, 5-19
scri pt 5-17
movi es 4-32
mul ti di mensi onal array functi ons A-16
mul ti di mensi onal arrays 5-7
mul ti pl e data sets, pl otti ng 4-3
mul ti pl e pl ots per fi gure 4-9
mul ti var i ate data, or gani zi ng 3-24
N
newsgroup 1-9
nul l 6-69
nul l space 6-69
numbers 3-11
fl oati ng-poi nt 3-12
numeri c symbol i c expressi ons 6-7
O
object functi ons A-16
object properti es 4-26
objects
fi ndi ng handl es 4-28
graphi cs 4-23
onl i ne documentati on 3-37
speci fyi ng vi ewi ng opti ons on Li nux 2-13
onl i ne hel p 3-37
pri nti ng 3-38
operator 3-12, A-4A-5
col on 3-8
output
control l i ng format 3-29
suppressi ng 3-30
overl ayi ng pl ots 4-7
P
patches
acqui r i ng 1-5
pat h 3-34
pl ot 4-2
Pl ot Edi tor 4-13
pl ots
annotati ons i n 4-13
pl otti ng
addi ng l egend 4-3
addi ng pl ots 4-7
annotati ng 4-13
basi c 4-2
compl ex data 4-6
compl ex numbers 4-6
contours 4-8
l i ne col ors 4-4
l i ne styl es 4-4
l i nes and mar kers 4-5
mesh and surface 4-15
mul ti pl e data sets 4-3
mul ti pl e pl ots 4-9
pl otti ng and data vi sual i zati on A-16A-20
pol y 6-72
pol ynomi al and i nterpol ati on functi ons A-11
PostScri pt 4-21
preal l ocati on 5-23
pr e t t y 6-28
pr i nt 4-21
pri nti ng
graphi cs 4-21
onl i ne reference pages 3-38
product
fami l y 1-14
Index
I-7
regi strati on 1-10
professi onal versi on
di fferences wi th Student Versi on 1-3
R
rati onal ari thmeti c 6-62
rati onal symbol i c expressi ons 6-7
Re adMe fi l e 2-5
r e al property 6-9
real symbol i c vari abl es 6-9, 6-26
Reference Gui de 3-37
reference i nformati on, obtai ni ng 1-7
regi steri ng your software 1-10
root di rectory
speci fyi ng 2-10
Rosser matri x 6-73, 6-74
runni ng exter nal programs 3-35
S
s av e 3-34
scal ar expansi on 3-25
sci enti fi c notati on 3-11
screens
i nstal l ati on data 2-12
root di rectory 2-10
scri pt M-fi l e 5-17
scri pts 5-17
sear ch path 3-34
sear chi ng onl i ne hel p 3-38, 3-40
sel ecti ng products to be i nstal l ed on Li nux 2-11
semi col on to suppress output 3-30
s i mpl e 6-49
si mpl i fi cati ons 6-446-52
s i mpl i f y 6-49
Si mul i nk 1-13
si mul taneous di fferenti al equati ons
sol vi ng 6-956-96
si mul taneous l i near equati ons
sol vi ng systems of 6-68, 6-92
si ngul ar matri x 3-20
si ngul ar val ue decomposi ti on 6-796-81
Sol uti on Search Engi ne 1-10
s ol v e 6-89
sol vi ng equati ons 6-896-96
al gebrai c 6-896-93
ordi nary di ffer enti al 6-936-96
sound processi ng functi ons A-13
sparse matri x functi ons A-12A-13
speci al character s A-4A-5
speci al constants 3-13
i nfi ni ty 3-13
not-a-number 3-13
speci al i zed graphs 4-8
speci al i zed math functi ons A-8
speci al i zed matr i ces A-7
speci fyi ng vi ewi ng opti ons on Li nux 2-13
spheri cal coordi nates 6-18
star ti ng MATLAB 3-2
s t ar t up. mfi l e 2-4, 2-12
statements
conti nui ng on mul ti pl e l i nes 3-31
executi ng 5-22
str i ngs
concatenati ng 5-13
str ucture functi ons A-15
str uctures 5-14
Student Versi on
di fferences wi th professi onal versi on 1-3
MATLAB di fferences 1-3
Si mul i nk di fferences 1-4
s ube x pr 6-53
subexpr essi ons 6-536-55
Index
I-8
s ubpl ot 4-9
s ubs 6-56
subscr i pti ng
wi th l ogi cal vectors 3-26
subscr i pts 3-7
substi tuti ons 6-536-60
s um3-5
summati on
symbol i c 6-27
support
sources of 1-8
suppressi ng output 3-30
surface pl ot 4-15
s v d 6-79
s wi t c h 5-4
s y m6-5, 6-6, 6-7, 6-8, 6-26
symbol i c expressi ons 6-89
creati ng 6-6
deci mal 6-8
fl oati ng-poi nt 6-7
numeri c 6-7
rati onal 6-7
symbol i c l i nks
setti ng up 2-12
symbol i c math functi ons
creati ng 6-14
Symbol i c Math Tool box
demo 6-5
l earni ng 1-7
Symbol i c Math Tool box functi ons
ari thmeti c operati ons B-3
basi c operati ons B-3
cal cul us B-3
conversi ons B-3
i ntegral transfor ms B-3
l i near al gebr a B-3
pedagogi cal and graphi cal appl i cati ons B-4
si mpl i fi cati on B-4
sol uti on of equati ons B-4
vari abl e pr eci si on ari thmeti c B-4
symbol i c matri x
computi ng ei genval ue of 6-74
converti ng to numeri c form 6-8
creati ng 6-10
di fferenti ati ng 6-17
symbol i c objects
about 6-5
symbol i c summati on 6-27
symbol i c vari abl es
compl ex 6-9
creati ng 6-6
defaul t 6-12
real 6-9, 6-26
symmetri c matr i x 3-19
s y ms 6-7
s y ms um6-27
system requi r ements
Li nux 2-7
PC 2-2
T
t ay l or 6-28
Tayl or seri es 6-28
techni cal suppor t 1-10
text 5-11
text edi tor
accessi ng 3-16
TI FF 4-22
ti tl e
fi gure 4-12
tool box 1-11
tool boxes
i nstal l i ng on Li nux 2-14
Index
I-9
i nstal l i ng on PC 2-5
t r ans pos e 3-5
troubl eshooti ng 1-8
U
unr e al pr operty 6-9
updates
acqui ri ng 1-5
user i nter face 4-30
bui l di ng 4-30
V
vari abl e-preci si on ari thmeti c 6-616-65
vari abl e-preci si on number s 6-63
vari abl es 3-11
gl obal 5-20
l ocal 5-20
vector 3-3
l ogi cal 3-26
preal l ocati ng 5-23
vectori zati on 5-23
vi si bi l i ty of axes 4-12
v pa 6-63
W
Web si te 1-9
whi l e 5-5
who 3-33
whos 3-33
wi ndows for pl otti ng 4-9
wi reframe 4-15
surface 4-15
worki ng envi ronment
MATLAB 1-12
workspace
cl eari ng 3-33
l i sti ng contents 3-33
l i sti ng storage i nformati on 3-33
savi ng 3-34
www. mat hwor ks . c om1-9
www. mat hwor ks . c om/books 1-9
www. mat hwor ks . c om/educ at i on 1-9
www. mat hwor ks . c om/s t or e 1-9, 2-5, 2-14
www. mat hwor ks . c om/s uppor t 1-8, 1-10
X
xor erase mode 4-31
Index
I-10