0% found this document useful (0 votes)
1K views296 pages

Matalb 2015a

matlab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views296 pages

Matalb 2015a

matlab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 296

Computa tion

Visua liza tion


Progra mming
Learning MATLAB
Version 6 (Release 12)
MATLAB

STUDEN T VERSION
How to Conta ct The Ma 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 ugges t @mat hwor ks . com Product enhancement suggesti ons
bugs @mat hwor ks . com Bug reports
doc@mat hwor ks . com Documentati on error reports
I SBN 0-9672195-3-1
Learning MATLAB
COPYRI GHT 1999 - 2001 by The MathWorks, I nc.
The software descri bed i n thi s document i s furni 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 agreement. No part of thi s manual may be photocopi ed or repro-
duced i n any form wi thout pri or wri tten consent from The MathWorks, I nc.
FEDERAL ACQUI SI TI ON: Thi s provi si on appl i es to al l acqui si ti ons of the Program and Documentati on by
or for the federal government of the Uni ted States. By accepti ng del i very of the Program, the government
hereby agrees that thi s software qual i fi es as "commerci al " computer software wi thi n the meani ng of FAR
Part 12.212, DFARS Part 227.7202-1, DFARS Part 227.7202-3, DFARS Part 252.227-7013, and DFARS Part
252.227-7014. The terms and condi ti ons of The MathWorks, I nc. Software Li cense Agreement shal l pertai n
to the governments use and di scl osure of the Program and Documentati on, and shal l supersede any
confl i cti ng contractual terms or condi ti ons. I f thi s l i cense fai l s to meet the governments mi ni mum needs or
i s i nconsi stent i n any respect wi th federal procurement l aw, the government agrees to return the Program
and Documentati on, unused, to MathWorks.
MATLAB, Si mul i nk, Statefl ow, Handl e Graphi cs, and Real -Ti me Workshop are regi stered trademarks, and
Target Language Compi l er i s a trademark of The MathWorks, I nc.
Other product or brand names are trademarks or regi stered trademarks of thei r respecti ve hol ders.
Pri nti ng Hi story: August 1999 Fi rst pri nti ng New manual
January 2001 Second pri nti ng Revi sed for MATLAB 6.0 (Rel ease 12)
iii
Contents
1
Introduction
About the Student 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
Obtaining Additional MathWorks Products . . . . . . . . . . . . . 1-5
Getting Started with MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . 1-6
Finding Reference Information . . . . . . . . . . . . . . . . . . . . . . . . 1-7
Troubleshooting and Other Resources . . . . . . . . . . . . . . . . . . 1-8
Documentati on Li brary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-8
MathWorks Web Si te . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-10
MathWorks Educati on Web Si te . . . . . . . . . . . . . . . . . . . . . . . 1-10
MATLAB Rel ated Books . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-10
MathWorks Store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-10
Usenet Newsgroup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-10
MathWorks Knowl edge Base . . . . . . . . . . . . . . . . . . . . . . . . . . 1-10
Techni cal Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-11
Product Regi strati on . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-11
About MATLAB and Simulink . . . . . . . . . . . . . . . . . . . . . . . . . 1-12
What I s MATLAB? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-12
What I s Si mul i nk? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-14
iv Contents
2
Installation
Installing on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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) . . . . . . . . . . . . . . . 2-6
Installing on 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-13
Accessi ng the Onl i ne Documentati on (Hel p) . . . . . . . . . . . . . . 2-14
3
Development Environment
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-2
Starting and Quitting MATLAB . . . . . . . . . . . . . . . . . . . . . . . . 3-3
Starti ng MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-3
Qui tti ng MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-3
MATLAB Desktop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-4
Desktop Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-6
Command Wi ndow . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-6
Launch Pad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-8
Hel p Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-8
Current Di rectory Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-11
Workspace Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-12
Edi tor/Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-14
Other Development Environment Features . . . . . . . . . . . . . 3-15
v
4
Getting Started
Matrices and Magic Squares . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2
Enteri ng Matri ces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-3
sum, transpose, and di ag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-4
Subscri pts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-6
The Col on Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-7
The magi c Functi on . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-8
Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-10
Vari abl es . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-10
Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-10
Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-11
Functi ons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-11
Exampl es of Expressi ons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-13
Working with Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-14
Generati ng Matri ces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-14
The l oad Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-15
M-Fi l es . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-15
Concatenati on . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-16
Del eti ng Rows and Col umns . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-16
More About Matrices and Arrays . . . . . . . . . . . . . . . . . . . . . . 4-18
Li near Al gebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-18
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-21
Mul ti vari ate Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-23
Scal ar Expansi on . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-24
Logi cal Subscri pti ng . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-25
The fi nd Functi on . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-26
Controlling Command Window Input and Output . . . . . . . 4-28
The format Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-28
Suppressi ng Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-30
Enteri ng Long Command Li nes . . . . . . . . . . . . . . . . . . . . . . . . 4-30
Command Li ne Edi ti ng . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-30
vi Contents
5
Graphics
Basic Plotting. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-2
Creati ng a Pl ot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-2
Mul ti pl e Data Sets i n One Graph . . . . . . . . . . . . . . . . . . . . . . . . 5-3
Speci fyi ng Li ne Styl es and Col ors . . . . . . . . . . . . . . . . . . . . . . . . 5-4
Pl otti ng Li nes and Markers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-5
I magi nary and Compl ex Data . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-6
Addi ng Pl ots to an Exi sti ng Graph . . . . . . . . . . . . . . . . . . . . . . . 5-7
Fi gure Wi ndows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-9
Mul ti pl e Pl ots i n One Fi gure . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-9
Control l i ng the Axes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-10
Axi s Label s and Ti tl es . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-12
Savi ng a Fi gure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-13
Editing Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-14
I nteracti ve Pl ot Edi ti ng . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-14
Usi ng Functi ons to Edi t Graphs . . . . . . . . . . . . . . . . . . . . . . . . 5-14
Usi ng Pl ot Edi ti ng Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-15
Usi ng the Property Edi tor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-16
Mesh and Surface Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-18
Vi sual i zi ng Functi ons of Two Vari abl es . . . . . . . . . . . . . . . . . . 5-18
Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-24
Printing Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-26
Handle Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-28
Graphi cs Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-28
Setti ng Object Properti es . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-31
Fi ndi ng the Handl es of Exi sti ng Objects . . . . . . . . . . . . . . . . . 5-33
Graphics User Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-35
Graphi cal User I nterface Desi gn Tool s . . . . . . . . . . . . . . . . . . . 5-35
Animations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-37
Erase Mode Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-37
Creati ng Movi es . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-38
vii
6
Programming with MATLAB
Flow Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2
i f . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2
swi tch and case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-4
for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-4
whi l e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-5
conti nue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-5
break . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-6
Other Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-7
Mul ti di mensi onal Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-7
Cel l Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-9
Characters and Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-11
Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-14
Scripts and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-17
Scri pts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-17
Functi ons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-18
Gl obal Vari abl es . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-20
Passi ng Stri ng Arguments to Functi ons . . . . . . . . . . . . . . . . . . 6-20
The eval Functi on . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-22
Vectori zati on . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-23
Preal l ocati on . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-23
Functi on Handl es . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-24
Functi on Functi ons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-24
Demonstration Programs Included with MATLAB . . . . . . 6-27
7
Symbolic Math Toolbox
Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-2
Getting Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-4
viii Contents
Getting Started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-5
Symbol i c Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-5
Creati ng Symbol i c Vari abl es and Expressi ons . . . . . . . . . . . . . . 7-6
Symbol i c and Numeri c Conversi ons . . . . . . . . . . . . . . . . . . . . . . 7-7
Creati ng Symbol i c Math Functi ons . . . . . . . . . . . . . . . . . . . . . 7-14
Calculus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-16
Di fferenti ati on . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-16
Li mi ts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-20
I ntegrati on . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-22
Symbol i c Summati on . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-27
Tayl or Seri es . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-28
Extended Cal cul us Exampl e . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-29
Simplifications and Substitutions . . . . . . . . . . . . . . . . . . . . . 7-43
Si mpl i fi cati ons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-43
Substi tuti ons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-51
Variable-Precision Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . 7-58
Overvi ew . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-58
Exampl e: Usi ng the Di fferent Ki nds of Ari thmeti c . . . . . . . . . 7-59
Another Exampl e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-61
Linear Algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-63
Basi c Al gebrai c Operati ons . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-63
Li near Al gebrai c Operati ons . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-64
Ei genval ues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-68
Jordan Canoni cal Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-74
Si ngul ar Val ue Decomposi ti on . . . . . . . . . . . . . . . . . . . . . . . . . 7-76
Ei genval ue Trajectori es . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-78
Solving Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-86
Sol vi ng Al gebrai c Equati ons . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-86
Several Al gebrai c Equati ons . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-87
Si ngl e Di fferenti al Equati on . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-90
Several Di fferenti al Equati ons . . . . . . . . . . . . . . . . . . . . . . . . . 7-92
ix
A
MATLAB Quick Reference
B
Symbolic Math Toolbox Quick Reference
x Contents
1
I ntroducti on
About the Student Version . . . . . . . . . . . . . 1-2
Obtaining Additional MathWorks Products . . . . . . 1-5
Getting Started with MATLAB . . . . . . . . . . . . 1-6
Finding Reference Information . . . . . . . . . . . 1-7
Troubleshooting and Other Resources . . . . . . . . 1-8
About MATLAB and Simulink . . . . . . . . . . . . 1-12
1 Introduction
1-2
About the Student Version
MATLAB

& Si mul i nk

are the premi er software packages for techni cal


computati on, data anal ysi s, and vi sual i zati on i n educati on and i ndustry. The
Student Versi on of MATLAB & Si mul i nk provi des al l of the features of
professi onal MATLAB, wi th no l i mi tati ons, and the ful l functi onal i ty of
professi onal Si mul i nk, wi th model si zes up to 300 bl ocks. The Student Versi on
gi ves you i mmedi ate access to the hi gh-performance numeri c computi ng power
you need.
MATLAB al l ows you to focus on your course work and appl i cati ons rather than
on programmi ng detai l s. I t enabl es you to sol ve many numeri cal probl ems i n a
fracti on of the ti me i t woul d take you to wri te a program i n a l ower l evel
l anguage. MATLAB hel ps you better understand and appl y concepts i n
appl i cati ons rangi ng from engi neeri ng and mathemati cs to chemi stry, bi ol ogy,
and economi cs.
Si mul i nk, i ncl uded wi th the Student Versi on, provi des a bl ock di agram tool for
model i ng and si mul ati ng dynami cal systems, i ncl udi ng si gnal processi ng,
control s, communi cati ons, and other compl ex systems.
The Symbol i c Math Tool box, al so i ncl uded wi th the Student Versi on, i s based
on the Mapl e

V symbol i c kernel and l ets you perform symbol i c computati ons


and vari abl e-preci si on ari thmeti c.
MATLAB products are used i n a broad range of i ndustri es, i ncl udi ng
automoti ve, aerospace, el ectroni cs, envi ronmental , tel ecommuni cati ons,
computer peri pheral s, fi nance, and medi cal . More than 400,000 techni cal
professi onal s at the worl ds most i nnovati ve technol ogy compani es,
government research l abs, fi nanci al i nsti tuti ons, and at more than 2,000
uni versi ti es rel y on MATLAB and Si mul i nk as the fundamental tool s for thei r
engi neeri ng and sci enti fi c work.
Student Use Policy
Thi s Student Li cense i s for use i n conjuncti on wi th courses offered at a
degree-granti ng i nsti tuti on. The MathWorks offers thi s l i cense as a speci al
servi ce to the student communi ty and asks your hel p i n seei ng that i ts terms
are not abused.
To use thi s Student Li cense, you must be a student usi ng the software i n
conjuncti on wi th courses offered at degree-granti ng i nsti tuti ons.
A bout the Student Version
1-3
You may not use thi s Student Li cense at a company or government l ab. Al so,
you may not use i t for research or for commerci al or i ndustri al purposes. I n
these cases, you can acqui re the appropri ate professi onal or academi c versi on
of the software by contacti ng The MathWorks.
Differences Between the Student Version and the
Professional Version
M ATLAB
Thi s versi on of MATLAB provi des ful l support for al l l anguage features as wel l
as graphi cs, external i nterface and Appl i cati on Program I nterface support, and
access to every other feature of the professi onal versi on of MATLAB.
Note MATLAB does not have a matri x si ze l i mi tati on i n thi s Student
Versi on.
MATLAB Differences. There are a few smal l di fferences between the Student
Versi on and the professi onal versi on of MATLAB:
The MATLAB prompt i n the Student Versi on i s
EDU>>
The wi ndow ti tl e bars i ncl ude the words
<Student Versi on>
Al l pri ntouts contai n the footer
St udent Ver s i on of MATLAB
Thi s footer i s not an opti on that can be turned off; i t wi l l al ways appear i n
your pri ntouts.
1 Introduction
1-4
Simulink
Thi s Student Versi on contai ns the compl ete Si mul i nk product, whi ch i s used
wi th MATLAB to model , si mul ate, and anal yze dynami cal systems.
Simulink Differences.
Model s are l i mi ted to 300 bl ocks.
The wi ndow ti tl e bars i ncl ude the words
<Student Versi on>
Al l pri ntouts contai n the footer
St udent Ver s i on of MATLAB
Thi s footer i s not an opti on that can be turned off; i t wi l l al ways appear i n
your pri ntouts.
Note UsingSimulink, whi ch i s accessi bl e from the Hel p browser, contai ns al l
of the Si mul i nk rel ated i nformati on i n the Learning Simulink book pl us
addi ti onal , advanced i nformati on.
Symbolic M a th Toolbox
The Symbol i c Math Tool box i ncl uded wi th thi s Student Versi on l ets you use an
i mportant subset of Mapl e. You can access al l of the functi ons i n the
professi onal versi on of the Symbol i c Math Tool box except mapl e, mapl ei ni t ,
mf un, mf unl i s t , and mhel p. For a compl ete l i st of al l the avai l abl e functi ons, see
Appendi x B, Symbol i c Math Tool box Qui ck Reference.
O btaining A dditional M athW orks Products
1-5
Obtaining Additional MathWorks Products
Many col l ege courses recommend MATLAB as thei r standard i nstructi onal
software. I n some cases, the courses may requi re parti cul ar tool boxes,
bl ocksets, or other products. Many of these products are avai l abl e for student
use. You may purchase and downl oad these addi ti onal products at speci al
student pri ces from the MathWorks Store at www. mat hwor ks . com/s t or e.
Al though many professi onal tool boxes are avai l abl e at student pri ces from the
MathWorks Store, not every one i s avai l abl e for student use. Some of the
tool boxes you can purchase i ncl ude:
Communi cati ons
Control System
Fuzzy Logi c
I mage Processi ng
Neural Network
Opti mi zati on
Si gnal Processi ng
Stati sti cs
Statefl ow

(A demo versi on of Statefl ow i s i ncl uded


wi th your Student Versi on.)
For an up-to-date l i st of whi ch tool boxes are avai l abl e, vi si t the MathWorks
Store.
Note The tool boxes that are avai l abl e for the Student Versi on of MATLAB &
Si mul i nk have the same functi onal i ty as the ful l , professi onal versi ons.
However, these student versi ons wi l l only work wi th the Student Versi on.
Li kewi se, the professi onal versi ons of the tool boxes wi l l not work wi th the
Student Versi on.
1 Introduction
1-6
Getting Started with MATLAB
What I Want What I Should Do
I need to i nstal l MATLAB. See Chapter 2, I nstal l ati on, i n thi s book.
I want to start MATLAB. (PC) Your MathWorks documentati on CD must be i n your
CD-ROM dri ve to start MATLAB. Doubl e-cl i ck the MATLAB
i con on your desktop.
(Linux) Enter the mat l ab command.
I m new to MATLAB and
want to l earn i t qui ckl y.
Start by readi ng Chapters 1 through 6 of Learning MATLAB.
The most i mportant thi ngs to l earn are how to enter matri ces,
how to use the : (col on) operator, and how to i nvoke functi ons.
You wi l l al so get a bri ef overvi ew of graphi cs and programmi ng
i n MATLAB. After you master the basi cs, you can access the rest
of the documentati on through the onl i ne hel p faci l i ty (Hel p).
I want to l ook at some
sampl es of what you can do
wi th MATLAB.
There are numerous demonstrati ons i ncl uded wi th MATLAB.
You can see the demos by sel ecti ng Demos from the Help menu.
(Li nux users type demo at the MATLAB prompt.) There are
demos i n mathemati cs, graphi cs, vi sual i zati on, and much more.
You al so wi l l nd a l arge sel ecti on of demos at
www. mat hwor ks . com/demos .
Finding Reference Inform ation
1-7
Finding Reference Information
What I Want What I Should Do
I want to know how to use a
speci c functi on.
Use the onl i ne hel p faci l i ty (Hel p). To access Hel p, use the
command hel pbr ows er or use the Help menu. The MATLAB
Functi on Reference i s al so avai l abl e from Hel p i n PDF format
(under Printable Documentation) i f you want to pri nt out any
of the functi on descri pti ons i n hi gh-qual i ty form. Note: Your
MathWorks documentati on CD must be i n your CD-ROM dri ve
to access Hel p.
I want to nd a functi on for
a speci c purpose but I dont
know i ts name.
There are several choi ces:
See MATLAB Qui ck Reference i n thi s book for a l i st of
MATLAB functi ons.
From Hel p, peruse the MATLAB functi ons by Category or
Al phabeti cal l y.
Use l ookf or (e.g., l ookf or i nv er s e) from the command l i ne.
Use I ndex or Search from Hel p.
I want to l earn about a
speci c topi c l i ke sparse
matri ces, ordi nary
di fferenti al equati ons, or cel l
arrays.
Use Hel p to l ocate the appropri ate secti ons i n Using MATLAB.
I want to know what
functi ons are avai l abl e i n a
general area.
Use Hel p to see the Functi on Reference by Category, or see
Appendi x A, MATLAB Qui ck Reference, i n thi s book for a l i st
of MATLAB functi ons. Hel p provi des access to the reference
pages for the hundreds of functi ons i ncl uded wi th MATLAB.
I want to l earn about the
Symbol i c Math Tool box.
See Chapter 7, Symbol i c Math Tool box, and Appendi x B,
Symbol i c Math Tool box Qui ck Reference, i n thi s book. For
compl ete descri pti ons of the Symbol i c Math Tool box functi ons,
use Hel p and sel ect Reference from Symbolic Math Toolbox.
1 Introduction
1-8
Troubleshooting and Other Resources
Documentation Library
Your Student Versi on of MATLAB & Si mul i nk contai ns much more
documentati on than the two pri nted books, Learning MATLAB and Learning
Simulink. On your CD i s a personal reference l i brary of every book and
reference page di stri buted by The MathWorks. Access thi s documentati on
l i brary from Hel p.
Note Even though you have the documentati on set for the MathWorks fami l y
of products, not every product i s avai l abl e for the Student Versi on of MATLAB
& Si mul i nk. For an up-to-date l i st of avai l abl e products, vi si t the MathWorks
Store. At the store you can al so purchase pri nted manual s for the MATLAB
fami l y of products.
Accessing the O nline Documenta tion
Access the onl i ne documentati on (Hel p) di rectl y from your product CD. (Li nux
users shoul d refer to Chapter 2, I nstal l ati on, for speci fi c i nformati on on
confi guri ng and accessi ng the onl i ne Hel p from the CD.)
1 Pl ace the CD i n your CD-ROM dri ve.
2 Sel ect Full Product Family Help from the Help menu.
Hel p appears i n a separate wi ndow.
What I Want What I Should Do
I have a MATLAB speci c
probl em I want hel p wi th.
Vi si t the Techni cal Support secti on
(www. mat hwor ks . com/s uppor t ) of the MathWorks Web si te and
search the Knowl edge Base of probl em sol uti ons.
I want to report a bug or
make a suggesti on.
Use Hel p or send e-mai l to bugs @mat hwor ks . com or
s ugges t @mat hwor ks . com.
Troubleshooting and O ther Resources
1-9
Note When you start MATLAB for the rst ti me, the Hel p Navi gator
di spl ays entri es for addi ti onal products. To l earn how to change the di spl ayed
product l i st, see the Product Fi l ter on page 3-10.
Tutorialsandreferencefor
MATLAB
Tutorialsandreferencefor
Simulink
Tutorialsandreferencefor
SymbolicMath Toolbox
Tutorialsandreferencefor
Stateflow
1 Introduction
1-10
MathWorks Web Site
Use your browser to vi si t the MathWorks Web si te, www. mat hwor ks . com. Youl l
fi nd l ots of i nformati on about MathWorks products and how they are used i n
educati on and i ndustry, product demos, and MATLAB based books. From the
Web si te you wi l l al so be abl e to access our techni cal support resources, vi ew a
l i brary of user and company suppl i ed M-fi l es, and get i nformati on about
products and upcomi ng events.
MathWorks Education Web Site
Thi s educati on-speci fi c Web si te, www. mat hwor ks . com/educat i on, contai ns
many resources for vari ous branches of engi neeri ng, mathemati cs, and sci ence.
Many of these i ncl ude teachi ng exampl es, books, and other rel ated products.
You wi l l al so fi nd a comprehensi ve l i st of l i nks to Web si tes where MATLAB i s
used for teachi ng and research at uni versi ti es.
MATLAB Related Books
Hundreds of MATLAB rel ated books are avai l abl e from many di fferent
publ i shers. An up-to-date l i st i s avai l abl e at www. mat hwor ks . com/s uppor t /
books .
MathWorks Store
The MathWorks Store (www. mat hwor ks . com/s t or e) gi ves you an easy way to
purchase add-on products and documentati on.
Usenet Newsgroup
I f you have access to Usenet newsgroups, you can joi n the acti ve communi ty of
parti ci pants i n the MATLAB speci fi c group, comp. s of t - s y s . mat l ab. Thi s
forum i s a gatheri ng of professi onal s and students who use MATLAB and have
questi ons or comments about i t and i ts associ ated products. Thi s i s a great
resource for posi ng questi ons and answeri ng those of others. MathWorks staff
al so parti ci pates acti vel y i n thi s newsgroup.
MathWorks Knowledge Base
You can access the MathWorks Knowl edge Base from the Support l i nk on our
Web si te. Our Techni cal Support group mai ntai ns thi s database of frequentl y
asked questi ons (FAQ). You can peruse the Knowl edge Base to qui ckl y l ocate
Troubleshooting and O ther Resources
1-11
rel evant data. You wi l l fi nd numerous exampl es on graphi cs, mathemati cs,
API , Si mul i nk, and others. You can answer many of your questi ons by spendi ng
a few mi nutes wi th thi s around-the-cl ock resource.
Technical Support
The MathWorks does not provi de tel ephone techni cal support to users of the
Student Versi on of MATLAB & Si mul i nk. There are numerous other vehi cl es
of techni cal support that you can use. The Addi ti onal Sources of I nformati on
secti on i n the CD hol der i denti fi es the ways to obtai n support.
Regi stered users of the Student Versi on of MATLAB & Si mul i nk can use our
el ectroni c techni cal support servi ces to answer product questi ons. Vi si t our
Techni cal Support Web si te at www. mat hwor ks . com/s uppor t .
After checki ng the avai l abl e MathWorks sources for hel p, i f you sti l l cannot
resol ve your probl em, you shoul d contact your i nstructor. Your i nstructor
shoul d be abl e to hel p you, but i f not, there i s tel ephone techni cal support for
regi stered i nstructors who have adopted the Student Versi on of MATLAB &
Si mul i nk i n thei r courses.
Product Registration
Vi si t the MathWorks Web si te (www. mat hwor ks . com/s t udent ) and regi ster
your Student Versi on.
1 Introduction
1-12
About MATLAB and Simulink
What Is MATLAB?
MATLAB i s a hi gh-performance l anguage for techni cal computi ng. I t
i ntegrates computati on, vi sual i zati on, and programmi ng i n an easy-to-use
envi ronment where probl ems and sol uti ons are expressed i n fami l i ar
mathemati cal notati on. Typi cal uses i ncl ude:
Math and computati on
Al gori thm devel opment
Model i ng, si mul ati on, and prototypi ng
Data anal ysi s, expl orati on, and vi sual i zati on
Sci enti fi c and engi neeri ng graphi cs
Appl i cati on devel opment, i ncl udi ng graphi cal user i nterface bui l di ng
MATLAB i s an i nteracti ve system whose basi c data el ement i s an array that
does not requi re di mensi oni ng. Thi s al l ows you to sol ve many techni cal
computi ng probl ems, especi al l y those wi th matri x and vector formul ati ons, i n
a fracti on of the ti me i t woul d take to wri te a program i n a scal ar noni nteracti ve
l anguage such as C or Fortran.
The name MATLAB stands for matrix laboratory. MATLAB was ori gi nal l y
wri tten to provi de easy access to matri x software devel oped by the LI NPACK
and EI SPACK projects. Today, MATLAB uses software devel oped by the
LAPACK and ARPACK projects, whi ch together represent the state-of-the-art
i n software for matri x computati on.
MATLAB has evol ved over a peri od of years wi th i nput from many users. I n
uni versi ty envi ronments, i t i s the standard i nstructi onal tool for i ntroductory
and advanced courses i n mathemati cs, engi neeri ng, and sci ence. I n i ndustry,
MATLAB i s the tool of choi ce for hi gh-producti vi ty research, devel opment, and
anal ysi s.
Toolbox es
MATLAB features a fami l y of appl i cati on-speci fi c sol uti ons cal l ed toolboxes.
Very i mportant to most users of MATLAB, tool boxes al l ow you to learn and
apply speci al i zed technol ogy. Tool boxes are comprehensi ve col l ecti ons of
MATLAB functi ons (M-fi l es) that extend the MATLAB envi ronment to sol ve
A bout M A TLA B and Sim ulink
1-13
parti cul ar cl asses of probl ems. Areas i n whi ch tool boxes are avai l abl e i ncl ude
si gnal processi ng, control systems, neural networks, fuzzy l ogi c, wavel ets,
si mul ati on, and many others.
The M ATLAB System
The MATLAB system consi sts of fi ve mai n parts:
Development Environment. Thi s i s the set of tool s and faci l i ti es that hel p you use
MATLAB functi ons and fi l es. Many of these tool s are graphi cal user i nterfaces.
I t i ncl udes the MATLAB desktop and Command Wi ndow, a command hi story,
and browsers for vi ewi ng hel p, the workspace, fi l es, and the search path.
The MATLAB Ma thema tica l Function Libra r y. Thi s i s a vast col l ecti on of computati onal
al gori thms rangi ng from el ementary functi ons l i ke sum, si ne, cosi ne, and
compl ex ari thmeti c, to more sophi sti cated functi ons l i ke matri x i nverse, matri x
ei genval ues, Bessel functi ons, and fast Fouri er transforms.
The MATLAB la ngua ge. Thi s i s a hi gh-l evel matri x/array l anguage wi th control
fl ow statements, functi ons, data structures, i nput/output, and object-ori ented
programmi ng features. I t al l ows both programmi ng i n the smal l to rapi dl y
create qui ck and di rty throw-away programs, and programmi ng i n the l arge
to create compl ete l arge and compl ex appl i cati on programs.
Ha ndle Gra phics

. Thi s i s the MATLAB graphi cs system. I t i ncl udes hi gh-l evel


commands for two-di mensi onal and three-di mensi onal data vi sual i zati on,
i mage processi ng, ani mati on, and presentati on graphi cs. I t al so i ncl udes
l ow-l evel commands that al l ow you to ful l y customi ze the appearance of
graphi cs as wel l as to bui l d compl ete graphi cal user i nterfaces on your
MATLAB appl i cati ons.
The MATLAB Applica tion Progra m Interfa ce (API). Thi s i s a l i brary that al l ows you to
wri te C and Fortran programs that i nteract wi th MATLAB. I t i ncl ude faci l i ti es
for cal l i ng routi nes from MATLAB (dynami c l i nki ng), cal l i ng MATLAB as a
computati onal engi ne, and for readi ng and wri ti ng MAT-fi l es.
1 Introduction
1-14
What Is Simulink?
Si mul i nk, a compani on program to MATLAB, i s an i nteracti ve system for
si mul ati ng nonl i near dynami c systems. I t i s a graphi cal mouse-dri ven program
that al l ows you to model a system by drawi ng a bl ock di agram on the screen
and mani pul ati ng i t dynami cal l y. I t can work wi th l i near, nonl i near,
conti nuous-ti me, di screte-ti me, mul ti rate, and hybri d systems.
Blocksetsare add-ons to Si mul i nk that provi de addi ti onal l i brari es of bl ocks for
speci al i zed appl i cati ons l i ke communi cati ons, si gnal processi ng, and power
systems.
Real-Time Workshop

i s a program that al l ows you to generate C code from


your bl ock di agrams and to run i t on a vari ety of real -ti me systems.
W ha t Is Sta teflow ?
Statefl ow i s an i nteracti ve desi gn tool for model i ng and si mul ati ng compl ex
reacti ve systems. Ti ghtl y i ntegrated wi th Si mul i nk and MATLAB, Statefl ow
provi des Si mul i nk users wi th an el egant sol uti on for desi gni ng embedded
systems by gi vi ng them an effi ci ent way to i ncorporate compl ex control and
supervi sory l ogi c wi thi n thei r Si mul i nk model s.
Wi th Statefl ow, you can qui ckl y devel op graphi cal model s of event-dri ven
systems usi ng fi ni te state machi ne theory, statechart formal i sms, and fl ow
di agram notati on. Together, Statefl ow and Si mul i nk serve as an executabl e
speci fi cati on and vi rtual prototype of your system desi gn.
Note Your Student Versi on of MATLAB & Si mul i nk i ncl udes a
comprehensi ve demo versi on of Stateow.
2
I nstal l ati on
Installing on Windows . . . . . . . . . . . . . . . 2-2
Installing on Linux . . . . . . . . . . . . . . . . . 2-7
2 Installation
2-2
Installing on Windows
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, Penti um I I , Penti um I I I , or AMD Athl on
personal computer
Mi crosoft Wi ndows 95, Wi ndows 98, Wi ndows 2000, Wi ndows Me, or
Wi ndows NT 4.0 (wi th Servi ce Pack 5 or 6a)
CD-ROM dri ve for i nstal l ati on, program executi on, and onl i ne
documentati on
Di sk space vari es dependi ng on si ze of parti ti on. The MathWorks I nstal l er
wi l l i nform you of the di sk space requi rement for your parti cul ar parti ti on.
64 MB RAM mi ni mum; 128 MB RAM strongl y recommended
8-bi t graphi cs adapter and di spl ay (for 256 si mul taneous col ors)
Netscape Navi gator 4.0 or hi gher or Mi crosoft I nternet Expl orer 4.0 or
hi gher i s requi red.
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
Mi crosoft Word 7.0 (Offi ce 95), or 8.0 (Offi ce 97), or Offi ce 2000 i s requi red to
run the MATLAB Notebook.
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.
Installing on W indow s
2-3
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 External I nterfaces/API provi des i nformati on on
how to wri te MEX-l es. External I nterfaces/API Reference descri bes the
col l ecti on of API functi ons. Both of these are avai l abl e from Hel p.
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:
Borl and C/C++ versi on 5.0 or 5.02
Borl and C++Bui l der versi on 3.0, 4.0, or 5.0
Compaq Vi sual Fortran versi on 6.1
DI GI TAL Vi sual Fortran versi on 5.0
Lcc C versi on 2.4 (i ncl uded wi th MATLAB)
Mi crosoft Vi sual C/C++ versi on 5.0 or 6.0
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:
ht t p: //www. mat hwor ks . com/s uppor t /t ech- not es /v 5/1600/1601. s ht ml
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 Turn off any vi rus protecti on software you have runni ng.
2 Exi t any exi sti ng copi es of MATLAB you have runni ng.
3 I nsert the MathWorks product 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 product CD.
4 I nstal l the Mi crosoft Java Vi rtual Machi ne (JVM), i f prompted. The
MathWorks I nstal l er requi res the Mi crosoft JVM.
Note: The Java i nstal l ati on requi res a system reboot.
5 Vi ew the Welcome screen and revi ew the Software Li cense Agreement.
6 Revi ew the Student Use Pol i cy.
7 Enter your name and school name.
8 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
Product List di al og box.
9 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.
10 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. To start MATLAB,
doubl e-cl i ck on the MATLAB i con that the i nstal l er creates on your desktop.
To run the demo programs, sel ect Demos from Help.
Note The MathWorks documentati on CD must be i n your CD-ROM dri ve to
start MATLAB.
Installing on W indow s
2-5
11 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.
12 Perform any addi ti onal 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.
For More Information The MATLAB I nstallation Guide for PC provi des
addi ti onal i nstal l ati on i nformati on. Thi s manual i s avai l abl e from Hel p.
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, 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 ReadMe l es associ ated wi th them. When you
downl oad the tool box, check to see i f there i s a ReadMe l e. These l es contai n
i mportant i nformati on about the tool box and possi bl y i nstal l ati on and
congurati on notes. To vi ew the ReadMe l e for a tool box, use the what s new
command.
2 Installation
2-6
Accessing the Online Documentation (Help)
Access the onl i ne documentati on (Hel p) di rectl y from your documentati on CD.
1 Pl ace the documentati on CD i n your CD-ROM dri ve.
2 Sel ect Full Product Family Help from the Help menu i n the MATLAB
Command Wi ndow. You can al so type hel pbr ows er at the MATLAB prompt.
The Hel p browser appears.
Installing on Linux
2-7
Installing on Linux
Note The Student Versi on of MATLAB & Si mul i nk for the Li nux pl atform i s
onl y avai l abl e i n the US and Canada.
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, Penti um I I , Penti um I I I , or AMD Athl on
personal computer
Li nux 2.2.x kernel
- gl i bc 2.1.x (2.1.2 or hi gher recommended)
- gcc 2.95.2 (gcc, g++, g77)
- xFree86 3.3.x (3.3.6 or hi gher recommended)
X Wi ndows (X11R6)
110 MB free di sk space for MATLAB, Si mul i nk, and Symbol i c Math Tool box
64 MB memory, addi ti onal memory strongl y recommended
64 MB swap space
CD-ROM dri ve for i nstal l ati on and onl i ne documentati on
8-bi t graphi cs adapter and di spl ay (for 256 si mul taneous col ors)
Netscape Navi gator 4.0 or hi gher i s requi red.
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.
2 Installation
2-8
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 External I nterfaces/API provi des i nformati on on
how to wri te MEX-l es. External I nterfaces/API Reference descri bes the
col l ecti on of API functi ons. Both of these are avai l abl e from Hel p.
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:
ht t p: //www. mat hwor ks . com/s uppor t /t ech- not es /v 5/1600/1601. s ht ml
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 /cdr om
2 Pl ace the MathWorks product 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 /cdr om /cdr om
shoul d work on most systems. I f your /et c/f s t ab fi l e has a l i ne si mi l ar to
/dev /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 l e /et c/f s t ab.
4 Move to the i nstal l ati on l ocati on usi ng the cd 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 ab6, use the
commands
cd /us r /l ocal
mkdi r mat l ab6
cd mat l ab6
Subsequent i nstructi ons i n thi s secti on refer to thi s di rectory as $MATLAB.
5 Run the CD i nstal l scri pt.
/cdr om/i ns t al l _gl 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
6 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.
7 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.
8 The system di spl ays your l i cense fi l e. Press OK.
Installing on Linux
2-11
9 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, and Symbol i c Math Tool box). Sel ect
OK.
2 Installation
2-12
10 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 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.
Sel ect OK to conti nue.
11 The Begin Product 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 Product 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.
12 You must edi t the docopt . m M-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). For
exampl e, i f /cdr om i s the path to your CD-ROM dri ve, then you woul d use
/cdr om/hel 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 . m fi l e to
i f i s uni x % UNI X
% doccmd = ' ' ;
% opt i ons = ' ' ;
docpat h = ' /cdr om/hel p' ;
The docopt . m fi 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.
Installing on Linux
2-13
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 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 Insta lla tion
I f MATLAB does not execute correctl y after i nstal l ati on:
1 Check the R12 Rel ease Notes for the l atest i nformati on concerni ng
i nstal l ati on. Thi s document i s accessi bl e from Hel p.
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 _gl 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 from Hel p.
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).
2 Installation
2-14
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 .
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 l es associ ated wi th them. When you
downl oad the tool box, check to see i f there i s a ReadMe l e. These l es contai n
i mportant i nformati on about the tool box and possi bl y i nstal l ati on and
congurati on notes. To vi ew the ReadMe l e for a tool box, use the what s new
command.
Accessing the Online Documentation (Help)
Access the onl i ne documentati on (Hel p) di rectl y from your documentati on CD.
1 Pl ace the documentati on CD i n your CD-ROM dri ve and mount i t.
2 Sel ect Full Product Family Help from the Help menu i n the MATLAB
Command Wi ndow. You can al so type hel pbr ows er at the MATLAB prompt.
The Hel p browser appears.
Installing on Linux
2-15
2 Installation
2-16
3
Devel opment
Envi ronment
Introduction . . . . . . . . . . . . . . . . . . . . 3-2
Starting and Quitting MATLAB . . . . . . . . . . . 3-3
MATLAB Desktop . . . . . . . . . . . . . . . . . 3-4
Desktop Tools . . . . . . . . . . . . . . . . . . . 3-6
Other Development Environment Features . . . . . . 3-15
3 D evelopm ent Environm ent
3-2
Introduction
Thi s chapter provi des a bri ef i ntroducti on to starti ng and qui tti ng MATLAB,
and the tool s and functi ons that hel p you to work wi th MATLAB vari abl es and
fi l es. For more i nformati on about the topi cs covered here, see the correspondi ng
topi cs under Devel opment Envi ronment i n the MATLAB documentati on,
whi ch i s avai l abl e onl i ne.
Starting and Q uitting M A TLA B
3-3
Starting and Quitting MATLAB
Starting MATLAB
On a Mi crosoft Wi ndows pl atform, to start MATLAB, doubl e-cl i ck the
MATLAB shortcut i con on your Wi ndows desktop.
On Li nux, to start MATLAB, type mat l ab at the operati ng system prompt.
Note On the Mi crosoft Wi ndows pl atform, the documentati on CD must be i n
your CD-ROM dri ve to start MATLAB. On both pl atforms, the documentati on
CD must be i n your CD-ROM dri ve to access the onl i ne documentati on.
After starti ng MATLAB, the MATLAB desktop opens see MATLAB
Desktop on page 3-4.
You can change the di rectory i n whi ch MATLAB starts, defi ne startup opti ons
i ncl udi ng runni ng a scri pt upon startup, and reduce startup ti me i n some
si tuati ons.
Quitting MATLAB
To end your MATLAB sessi on, sel ect Exit MATLAB from the Filemenu i n the
desktop, or type qui t i n the Command Wi ndow. To execute speci fi ed functi ons
each ti me MATLAB qui ts, such as savi ng the workspace, you can create and
run a f i ni s h. m scri pt.
3 D evelopm ent Environm ent
3-4
MATLAB Desktop
When you start MATLAB, the MATLAB desktop appears, contai ni ng tool s
(graphi cal user i nterfaces) for managi ng fi l es, vari abl es, and appl i cati ons
associ ated wi th MATLAB.
The fi rst ti me MATLAB starts, the desktop appears as shown i n the fol l owi ng
i l l ustrati on, al though your Launch Pad may contai n di fferent entri es.
Vieworchange
current
directory.
Viewor use previously run functions.
Enter
MATLAB
functions.
Close window.
Drag the separator bar to resize windows.
Click to move window
outside of desktop.
Get help. Expand to view
documentation, demos, and
tools for your products.
UsetabstogotoWorkspacebrowser
or Current Directory browser.
M A TLA B D esktop
3-5
You can change the way your desktop l ooks by openi ng, cl osi ng, movi ng, and
resi zi ng the tool s i n i t. You can al so move tool s outsi de of the desktop or return
them back i nsi de the desktop (docki ng). Al l the desktop tool s provi de common
features such as context menus and keyboard shortcuts.
You can speci fy certai n characteri sti cs for the desktop tool s by sel ecti ng
Preferences from the File menu. For exampl e, you can speci fy the font
characteri sti cs for Command Wi ndow text. For more i nformati on, cl i ck the
Help button i n the Preferences di al og box.
3 D evelopm ent Environm ent
3-6
Desktop Tools
Thi s secti on provi des an i ntroducti on to MATLABs desktop tool s. You can al so
use MATLAB functi ons to perform most of the features found i n the desktop
tool s. The tool s are:
Command Wi ndow on page 3-6
Command Hi story on page 3-7
Launch Pad on page 3-8
Hel p Browser on page 3-8
Current Di rectory Browser on page 3-11
Workspace Browser on page 3-12
Array Edi tor on page 3-13
Edi tor/Debugger on page 3-14
Command Window
Use the Command Windowto enter vari abl es and run functi ons and M-fi l es.
For more i nformati on on control l i ng i nput and output, see Control l i ng
Command Wi ndow I nput and Output on page 4-28.
Type functions and
variables at the
MATLAB prompt.
MATLAB displays the
results.
D esktop Tools
3-7
Comma nd Histor y
Li nes you enter i n the Command Wi ndow are l ogged i n the CommandHistory
wi ndow. I n the Command Hi story, you can vi ew previ ousl y used functi ons, and
copy and execute sel ected l i nes.
To save the i nput and output from a MATLAB sessi on to a fi l e, use the di ar y
functi on.
Note I f other users share the same machi ne wi th you, usi ng the same l og i n
i nformati on, then they wi l l have access to the functi ons you ran duri ng a
sessi on vi a the Command Hi story. I f you do not want other users to have
access to the Command Hi story from your sessi on, sel ect Clear Command
History from the Edit menu before you qui t MATLAB.
Running Ex ter na l Progra ms
You can run external programs from the MATLAB Command Wi ndow. 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 useful for i nvoki ng
Timestamp marks the
start of each session.
Select one or more lines
and right-click to copy,
evaluate, or create an
M-filefromtheselection.
3 D evelopm ent Environm ent
3-8
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
i nvokes an edi tor cal l ed emacs for a fi l e named magi k. m. When you qui t the
external program, the operati ng system returns control to MATLAB.
Launch Pad
MATLABs Launch Pad provi des easy access to tool s, demos, and
documentati on.
Help Browser
Use the Hel p browser to search and vi ew documentati on for al l MathWorks
products. The Hel p browser i s a Web browser i ntegrated i nto the MATLAB
desktop that di spl ays HTML documents.
Click+to showthe listing for a product.
Help- double-clicktogodirectlyto
documentation for the product.
Demos-double-clicktodisplaythedemo
launcher for the product.
Tools - double-click to open the tool.
Sampleof listingsinLaunchPad youll seelistings
for all products installed on your system.
D esktop Tools
3-9
To open the Hel p browser, cl i ck the hel p button i n the tool bar, or type
hel pbr ows er i n the Command Wi ndow.
The Hel p browser consi sts of two panes, the Hel p Navi gator, whi ch you use to
fi nd i nformati on, and the di spl ay pane, where you vi ew the i nformati on.
TabsintheHelpNavigatorpaneprovidedifferentwaystofinddocumentation.
Dragtheseparatorbartoadjustthewidthof thepanes.
Viewdocumentationinthedisplaypane.
Usethecloseboxtohidethepane.
3 D evelopm ent Environm ent
3-10
Help N a viga tor
Use to Hel p Navi gator to fi nd i nformati on. I t i ncl udes:
Product filter Set the fi l ter to show documentati on onl y for the products
you speci fy.
Note I n the Student Versi on of MATLAB & Si mul i nk, the product l ter i s
i ni ti al l y set to di spl ay a subset of the enti re documentati on set. You can add or
del ete whi ch product documentati on i s di spl ayed by usi ng the product l ter.
Contents tab Vi ew the ti tl es and tabl es of contents of documentati on for
your products.
Index tab Fi nd speci fi c i ndex entri es (sel ected keywords) i n the
MathWorks documentati on for your products.
Searchtab Look for a speci fi c phrase i n the documentati on. To get hel p for
a speci fi c functi on, set the Search type to Function Name.
Favorites tab Vi ew a l i st of documents you previ ousl y desi gnated as
favori tes.
Displa y Pa ne
After fi ndi ng documentati on usi ng the Hel p Navi gator, vi ew i t i n the di spl ay
pane. Whi l e vi ewi ng the documentati on, you can:
Browse to other pages Use the arrows at the tops and bottoms of the pages,
or use the back and forward buttons i n the tool bar.
Bookmark pages Cl i ck the Add to Favorites button i n the tool bar.
Pri nt pages Cl i ck the pri nt button i n the tool bar.
Fi nd a term i n the page Type a term i n the Findinpagefi el d i n the tool bar
and cl i ck Go.
Other features avai l abl e i n the di spl ay pane are: copyi ng i nformati on,
eval uati ng a sel ecti on, and vi ewi ng Web pages.
For M ore Help
I n addi ti on to the Hel p browser, you can use hel p functi ons. To get hel p for a
speci fi c functi on, use doc. For exampl e, doc f or mat di spl ays hel p for the
D esktop Tools
3-11
f or mat functi on i n the Hel p browser. Other means for getti ng hel p i ncl ude
contacti ng Techni cal Support (ht t p: //www. mat hwor ks . com/s uppor t ) and
parti ci pati ng i n the newsgroup for MATLAB users, comp. s of t - s y s . mat l ab.
Current Directory Browser
MATLAB fi l e operati ons use the current di rectory and the search path as
reference poi nts. Any fi l e you want to run must ei ther be i n the current
di rectory or on the search path.
A qui ck way to vi ew or change the current di rectory i s by usi ng the Current
Directory fi el d i n the desktop tool bar as shown bel ow.
To search for, vi ew, open, and make changes to MATLAB-rel ated di rectori es
and fi l es, use the MATLAB Current Di rectory browser. Al ternati vel y, you can
use the functi ons di r , cd, and del et e.
Use the pathname edit box to view
directories and their contents.
Click the find button to search for content within M-files.
Double-clickafile
to open it in an
appropriate tool.
Viewthe help
portion of the
selected M-file.
3 D evelopm ent Environm ent
3-12
Sea rch Pa th
To determi ne how to execute functi ons you cal l , MATLAB uses a search path to
fi nd M-fi l es and other MATLAB-rel ated fi l es, whi ch are organi zed i n
di rectori es on your fi l e system. Any fi l e you want to run i n MATLAB must
resi de i n the current di rectory or i n a di rectory that i s on the search path. By
defaul t, the fi l es suppl i ed wi th MATLAB and MathWorks tool boxes are
i ncl uded i n the search path.
To see whi ch di rectori es are on the search path or to change the search path,
sel ect Set Pathfrom the Filemenu i n the desktop, and use the Set Pathdi al og
box. Al ternati vel y, you can use the pat h functi on to vi ew the search path,
addpat h to add di rectori es to the path, and r mpat h to remove di rectori es from
the path.
Workspace Browser
The MATLAB workspace consi sts of the set of vari abl es (named arrays) bui l t
up duri ng a MATLAB sessi on and stored i n memory. You add vari abl es to the
workspace by usi ng functi ons, runni ng M-fi l es, and l oadi ng saved workspaces.
To vi ew the workspace and i nformati on about each vari abl e, use the
Workspace browser, or use the functi ons who and whos .
Double-click
a variable to
see and
change its
contents in
the Array
Editor.
D esktop Tools
3-13
To del ete vari abl es from the workspace, sel ect the vari abl e and sel ect Delete
from the Edit menu. Al ternati vel y, use the cl ear functi on.
The workspace i s not mai ntai ned after you end the MATLAB sessi on. To save
the workspace to a fi l e that can be read duri ng a l ater MATLAB sessi on, sel ect
SaveWorkspaceAs from the Filemenu, or use the s av e functi on. Thi s saves
the workspace to a bi nary fi l e cal l ed a MAT-fi l e, whi ch has a . mat extensi on.
There are opti ons for savi ng to di fferent formats. To read i n a MAT-fi l e, sel ect
Import Data from the File menu, or use the l oad functi on.
Arra y Editor
Doubl e-cl i ck on a vari abl e i n the Workspace browser to see i t i n the Array
Edi tor. Use the Array Edi tor to vi ew and edi t a vi sual representati on of one- or
two-di mensi onal numeri c arrays, stri ngs, and cel l arrays of stri ngs that are i n
the workspace.
Change values of array elements. Change the display format.
Use the tabs to viewthe variables you have open in the Array Editor.
3 D evelopm ent Environm ent
3-14
Editor/ Debugger
Use the Edi tor/Debugger to create and debug M-fi l es, whi ch are programs you
wri te to run MATLAB functi ons. The Edi tor/Debugger provi des a graphi cal
user i nterface for basi c text edi ti ng, as wel l as for M-fi l e debuggi ng.
You can use any text edi tor to create M-fi l es, such as Emacs, and can use
preferences (accessi bl e from the desktop File menu) to speci fy that edi tor as
the defaul t. I f you use another edi tor, you can sti l l use the MATLAB Edi tor/
Debugger for debuggi ng, or you can use debuggi ng functi ons, such as dbs t op,
whi ch sets a breakpoi nt.
I f you just need to vi ew the contents of an M-fi l e, you can di spl ay i t i n the
Command Wi ndow by usi ng the t y pe functi on.
Set breakpoints
where you want
execution to pause
soyoucanexamine
variables.
Find and replace strings. Comment selectedlinesandspecifyindentingstyleusingtheTextmenu.
Holdthecursorover
a variable and its
current value
appears (known as
a datatip).
O ther D evelopm ent Environm ent Features
3-15
Other Development Environment Features
Addi ti onal devel opment envi ronment features are:
I mporti ng and Exporti ng Data Techni ques for bri ngi ng data created by
other appl i cati ons i nto the MATLAB workspace, i ncl udi ng the I mport
Wi zard, and packagi ng MATLAB workspace vari abl es for use by other
appl i cati ons.
I mprovi ng M-Fi l e Performance The Profi l er i s a tool that measures where
an M-fi l e i s spendi ng i ts ti me. Use i t to hel p you make speed i mprovements.
I nterfaci ng wi th Source Control Systems Access your source control system
from wi thi n MATLAB, Si mul i nk, and Statefl ow.
Usi ng Notebook Access MATLABs numeri c computati on and vi sual i zati on
software from wi thi n a word processi ng envi ronment (Mi crosoft Word).
3 D evelopm ent Environm ent
3-16
4
Getti ng Started
Matrices and Magic Squares . . . . . . . . . . . . . 4-2
Expressions . . . . . . . . . . . . . . . . . . . . 4-10
Working with Matrices . . . . . . . . . . . . . . . 4-14
More About Matrices and Arrays . . . . . . . . . . . 4-18
Controlling Command Window Input and Output . . . 4-28
4 G etting Started
4-2
Matrices and Magic Squares
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. 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.
M atrices and M agic Squares
4-3
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 turn out to
have some fasci nati ng
characteri sti cs worth expl ori ng.
Entering Matrices
The best way for you to get started wi th MATLAB i s to l earn how to handl e
matri ces. Start MATLAB and fol l ow al ong wi th each exampl e.
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 i n the Command Wi ndow
A = [ 16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]
4 G etting Started
4-4
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'
produces
M atrices and M agic Squares
4-5
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
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.
4 G etting Started
4-6
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 stored 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 mens i ons .
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
M atrices and M agic Squares
4-7
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) )
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) )
4 G etting Started
4-8
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 the Symbol i c Math Tool box, you can di scover that the magi c sum for an
n-by-n magi c square i s .
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] )
n
3
n + ( ) 2
M atrices and M agic Squares
4-9
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 Usi ng MATLAB, whi ch i s accessi bl e from Hel p,
provi des comprehensi ve materi al on the devel opment envi ronment,
mathemati cs, programmi ng and data types, graphi cs, 3-D vi sual i zati on,
external i nterfaces/API , and creati ng graphi cal user i nterfaces i n MATLAB.
4 G etting Started
4-10
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
Expressions
4-11
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 range of 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 , ex 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 Using MATLAB)
^ Power
' Compl ex conjugate transpose
( ) Speci fy eval uati on order
4 G etting Started
4-12
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 Hel p to access compl ete
descri pti ons of al l the MATLAB functi ons by category or al phabeti cal l y.
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
pi 3.14159265
i I magi nary uni t,
j
Same as i
eps Fl oati ng-poi nt rel ati ve preci si on,
r eal mi n Smal l est fl oati ng-poi nt number,
r eal max
Largest fl oati ng-poi nt number,
I nf I nfi ni ty
NaN Not-a-number
1
2
52
2
1022
2 ( )2
1023
Expressions
4-13
eps = 1. e- 6
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
Examples of 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 = ex p( l og( r eal max ) )
huge =
1. 7977e+308
t oobi g = pi *huge
t oobi g =
I nf
4 G etting Started
4-14
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
W orking w ith M atrices
4-15
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.
An easy way to read data i nto MATLAB i n many text or bi nary formats i s to
use the I mport Wi zard.
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. Use the MATLAB Edi tor or another text edi tor to 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.
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 ] ;
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.
4 G etting Started
4-16
Concatenation
Concatenation i s the process 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.
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) = [ ]
W orking w ith M atrices
4-17
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
4 G etting Started
4-18
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
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.
M ore A bout M atrices and A rrays
4-19
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 cl 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.
4 G etting Started
4-20
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
M ore A bout M atrices and A rrays
4-21
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 approaches i nfi ni ty, al l the el ements i n the th power,
, approach .
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
i s
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!
For More Information Al l of the MATLAB math functi ons are descri bed i n
the MATLAB Functi on Reference, whi ch i s accessi bl e from Hel p.
Arrays
When they are taken away from the worl d of l i near al gebra, matri 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
k k
p
k
1 4
det A I ( )

4
34
3
64
2
2176 +
4 G etting Started
4-22
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.
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
Building Ta bles
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
M ore A bout M atrices and A rrays
4-23
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.
4 G etting Started
4-24
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
M ore A bout M atrices and A rrays
4-25
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 NaN i 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,
4 G etting Started
4-26
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 there i s one observati 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 - mean( 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
M ore A bout M atrices and A rrays
4-27
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
4 G etting Started
4-28
Controlling Command Window Input and Output
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 how to:
Control the appearance of the output val ues
Suppress output from MATLAB commands
Enter l ong commands at the command l i ne
Edi t the command l i ne
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.
Note To ensure proper spaci ng, use a xed-wi dth font, such as Fi xedsys or
Couri er.
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
C ontrolling C om m and W indow Input and O utput
4-29
f or mat l ong
1. 33333333333333 0. 00000123450000
f or mat l ong e
1. 333333333333333e+000 1. 234500000000000e- 006
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 or smal l er than ,
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 compact
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.
10
3
10
3
4 G etting Started
4-30
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) ;
Entering 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 unct 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. You can al so copy previ ousl y executed commands from the
Command Hi story. For more i nformati on, see Command Hi story on page 3-7.
C ontrolling C om m and W indow Input and O utput
4-31
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.)
Ta b Completion
MATLAB compl etes the name of a functi on, vari abl e, fi l ename, or handl e
graphi cs property i f you type the fi rst few l etters and then press the Tab key.
I f there i s a uni que name, the name i s automati cal l y compl eted. I f there i s more
than one name that starts wi th the l etters you typed, press the Tabkey agai n
to see a l i st of the possi bi l i ti es.
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
Ctrl+ Ctrl+l Move l eft one word
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
4 G etting Started
4-32
5
Graphi cs
Basic Plotting . . . . . . . . . . . . . . . . . . . 5-2
Editing Plots . . . . . . . . . . . . . . . . . . . 5-14
Mesh and Surface Plots . . . . . . . . . . . . . . . 5-18
Images . . . . . . . . . . . . . . . . . . . . . . 5-24
Printing Graphics . . . . . . . . . . . . . . . . . 5-26
Handle Graphics . . . . . . . . . . . . . . . . . . 5-28
Graphics User Interfaces . . . . . . . . . . . . . . 5-35
Animations . . . . . . . . . . . . . . . . . . . . 5-37
5 G raphics
5-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 Information Graphi cs and 3-D Vi sual i zati on provi de i n-depth
coverage of MATLAB graphi cs and vi sual i zati on tool s. Access these from Hel p.
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 , 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)
2

Basic Plotting
5-3
Multiple Data Sets in One Graph
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 egend 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
5 G raphics
5-4
For More Information See Deni ng the Col or of Li nes for Pl otti ng i n
Axes Properti es i n Hel p.
Specifying Line Styles and 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) when you pl ot your data usi ng the pl ot command.
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:
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.
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
5-5
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.
You can al so edi t col or, l i ne styl e, and markers i nteracti vel y. See Edi ti ng
Pl ots on page 5-14 for more i nformati on.
Plotting Lines and Markers
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 marker 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 +' )
5 G raphics
5-6
For More Information See Basi c Pl otti ng i n Hel p for more exampl es of
pl otti ng opti ons.
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
5-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
5 G raphics
5-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 Creati ng Speci al i zed Pl ots i n Hel p for
i nformati on on a vari ety of graph types.
Basic Plotting
5-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
Clea ring the Figure for a N ew Plot
When a fi gure al ready exi sts, most pl otti ng commands cl ear the axes and use
thi s fi gure to create the new pl ot. However, these commands do not reset fi gure
properti es, such as the background col or or the col ormap. I f you have set any
fi gure properti es i n the previ ous pl ot, you may want to use the cl f command
wi th the r es et opti on,
cl f r es et
before creati ng your new pl ot to set the fi gures properti es to thei r defaul ts.
For More Information See Fi gure Properti es and the reference page for
the f i gur e command i n Hel p. See Control l i ng Graphi cs Output for
i nformati on on how to control property resetti ng i n your graphi cs programs.
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)
5 G raphics
5-10
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
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)
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. You can al so set these opti ons
i nteracti vel y. See Edi ti ng Pl ots on page 5-14 for more i nformati on.
Basic Plotting
5-11
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.
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
5 G raphics
5-12
makes the axi s i nvi si bl e.
Setting Grid 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 Axes
Properti es i n Hel p.
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. You can al so set these opti ons i nteracti vel y. See Edi ti ng Pl ots on
page 5-14 for more i nformati on.
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
5-13
For More Information See Formatti ng Graphs i n Hel p for addi ti onal
i nformati on on addi ng l abel s and annotati ons to your graphs.
Saving a Figure
To save a fi gure, sel ect Save from the File menu. The fi gure i s saved as a
FI G-fi l e, whi ch you can l oad usi ng the open or hgl oad commands.
For ma ts for Impor ting into O ther Applica tions
You can export the fi gure as a standard graphi cs format, such as TI FF, for use
wi th other appl i cati ons. To do thi s, sel ect Export from the Filemenu. You can
al so export fi gures from the command l i ne usi ng the s av eas and pr i nt
commands.
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.
5 G raphics
5-14
Editing Plots
MATLAB formats a graph to provi de readabi l i ty, setti ng the scal e of axes,
i ncl udi ng ti ck marks on the axes, and usi ng col or and l i ne styl e to di sti ngui sh
the pl ots i n the graph. However, i f you are creati ng presentati on graphi cs, you
may want to change thi s defaul t formatti ng or add descri pti ve l abel s, ti tl es,
l egends and other annotati ons to hel p expl ai n your data.
MATLAB supports two ways to edi t the pl ots you create:
Usi ng the mouse to sel ect and edi t objects i nteracti vel y
Usi ng MATLAB functi ons at the command-l i ne or i n an M-fi l e
Interactive Plot Editing
I f you enabl e pl ot edi ti ng mode i n the MATLAB fi gure wi ndow, you can perform
poi nt-and-cl i ck edi ti ng of the objects i n your graph. I n thi s mode, you sel ect the
object or objects you want to edi t by doubl e-cl i cki ng on i t. Thi s starts the
Property Edi tor, whi ch provi des access to properti es of the object that control
i ts appearance and behavi or.
For more i nformati on about i nteracti ve edi ti ng, see Usi ng Pl ot Edi ti ng Mode
on page 5-15. For i nformati on about edi ti ng object properti es i n pl ot edi ti ng
mode, see Usi ng the Property Edi tor on page 5-16.
Note Pl ot edi ti ng mode provi des an al ternati ve way to access the properti es
of MATLAB graphi c objects. However, you can onl y access a subset of object
properti es through thi s mechani sm. You may need to use a combi nati on of
i nteracti ve edi ti ng and command l i ne edi ti ng to achi eve the effect you desi re.
Using Functions to Edit Graphs
I f you prefer to work from the MATLAB command l i ne or i f you are creati ng an
M-fi l e, you can use MATLAB commands to edi t the graphs you create. Taki ng
advantage of MATLABs Handl e Graphi cs system, you can use the s et and get
commands to change the properti es of the objects i n a graph. For more
i nformati on about usi ng command l i ne, see Handl e Graphi cs on page 5-28.
Editing Plots
5-15
Using Plot Editing Mode
The MATLAB fi gure wi ndow supports a poi nt-and-cl i ck styl e edi ti ng mode that
you can use to customi ze the appearance of your graph. The fol l owi ng
i l l ustrati on shows a fi gure wi ndow wi th pl ot edi ti ng mode enabl ed and l abel s
the mai n pl ot edi ti ng mode features.
Clickthisbuttontostartplot
edit mode.
UsetheEdit, Insert, andTools
menus to add objects or edit
existingobjectsinthegraph.
Double-clickonanobject to
select it.
Positionlabels, legends, and
otherobjectsbyclickingand
dragging them.
Access object-specificplot
edit functions through
context-sensitive pop-up
menus.
Use these toolbar buttons to add text, arrows, and lines to a graph.
5 G raphics
5-16
Using the Property Editor
I n pl ot edi ti ng mode, you can use a graphi cal user i nterface, cal l ed the Property
Edi tor, to edi t the properti es of objects i n the graph. The Property Edi tor
provi des access to many properti es of the root, fi gure, axes, l i ne, l i ght, patch,
i mage, surfaces rectangl e, and text objects. For exampl e, usi ng the Property
Edi tor, you can change the thi ckness of a l i ne, add ti tl es and axes l abel s, add
l i ghts, and perform many other pl ot edi ti ng tasks.
Thi s fi gure shows the components of the Property Edi tor i nterface.
Use these buttons to move back and forth among the graphics objects you have edited.
Click Help to get information about
particular properties.
Use the navigation bar to select
the object you want to edit.
Clickonatabtoviewagroup
of properties.
Click here to viewa list of
values for this field.
Checkthischeckboxtoseethe
effect of your changes as you
make them.
Click OK to apply your changes
and dismiss the Property Editor.
Click Cancel to dismiss the Property Editor
without applying your changes.
Click Apply to apply your changes
without dismissing the Property Editor.
Editing Plots
5-17
Sta r ting the Proper ty Editor
You start the Property Edi tor by doubl e-cl i cki ng on an object i n a graph, such
as a l i ne, or by ri ght-cl i cki ng on an object and sel ecti ng the Properties opti on
from the objects context menu.
You can al so start the Property Edi tor by sel ecti ng ei ther the Figure
Properties, Axes Properties, or Current Object Properties from the fi gure
wi ndow Edit menu. These opti ons automati cal l y enabl e pl ot edi ti ng mode, i f i t
i s not al ready enabl ed.
Once you start the Property Edi tor, keep i t open throughout an edi ti ng sessi on.
I t provi des access to al l the objects i n the graph. I f you cl i ck on another object
i n the graph, the Property Edi tor di spl ays the set of panel s associ ated wi th that
object type. You can al so use the Property Edi tors navi gati on bar to sel ect an
object i n the graph to edi t.
To save a fi gure, sel ect Save from the File menu. 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
File menu. 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.
5 G raphics
5-18
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 mes hgr i d functi on transforms 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 Gra 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 x and y di 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) + eps ;
Z = s i n( R) . /R;
mes h( X, Y, Z, ' EdgeCol or ' , ' bl ack' )
M esh and Surface Plots
5-19
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 col or map i s an ordered l i st of col ors). These statements graph
the sinc 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.4
0.2
0
0.2
0.4
0.6
0.8
1
5 G raphics
5-20
See the col or map reference page for i nformati on on col ormaps.
For More Information See Creati ng 3-D Graphs i n Hel p for more
i nformati on on surface pl ots.
Tra nspa rent Sur fa ces
You can make the faces of a surface transparent to a varyi ng degree.
Transparency (referred to as the al pha val ue) can be speci fi ed for the whol e
object or can be based on an al phamap, whi ch behaves i n a way anal ogous to
col ormaps. For exampl e,
s ur f ( X, Y, Z)
col or map hs v
al pha( . 4)
0.2
0
0.2
0.4
0.6
0.8
10
5
0
5
10
10
5
0
5
10
0.4
0.2
0
0.2
0.4
0.6
0.8
1
M esh and Surface Plots
5-21
produces a surface wi th a face al pha val ue of 0.4. Al pha val ues range from 0
(compl etel y transparent) to 1 (not transparent).
For More Information See Transparency i n Hel p for more i nformati on on
usi ng thi s feature.
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).
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
5 G raphics
5-22
M a nipula ting the Sur fa ce
The Camera Tool bar provi des a way to i nteracti vel y expl ore 3-D graphi cs.
Di spl ay the tool bar by sel ecti ng Camera Toolbar from the fi gure wi ndow's
View menu. Here i s the tool bar wi th the orbi t camera tool sel ected:
The Camera Tool bar enabl es you to move the camera around the surface object,
zoom, add a l i ght, and perform other vi ewi ng operati ons wi thout i ssui ng
commands. The fol l owi ng pi cture shows the surface vi ewed by orbi ti ng the
camera toward the bottom. A scene l i ght has been added to i l l umi nate the
undersi de of the surface, whi ch i s not l i t by the l i ght added i n the previ ous
secti on.
M esh and Surface Plots
5-23
For More Information See the Li ghti ng as a Vi sual i zati on Tool and Vi ew
Control wi th the Camera Tool bar i n Hel p for i nformati on on these
techni ques.
5 G raphics
5-24
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
capt i on 2x 28 112 char 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.
Note MAT-l es, such as dur er . mat , are bi nary 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 unusual col ori ng to the si xteenth century etchi ng. The functi on hot
generates a col ormap contai ni ng shades of reds, oranges, and yel l ows.
Im ages
5-25
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 Di spl ayi ng Bi t-Mapped I mages i n Hel p for
i nformati on on the i mage processi ng capabi l i ti es of MATLAB.
5 G raphics
5-26
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 File menu
Usi ng the pr i nt command
Printing from the M enu
There are four menu opti ons under the File menu that pertai n to pri nti ng:
The Page Setup 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.
General 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.
Sel ect the PageSetupdi al og box Helpbutton to di spl ay i nformati on on how to
set up the page.
Ex por ting Figure to Gra phics Files
The Export opti on under the File menu 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
Printing G raphics
5-27
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 Basi c
Pri nti ng and Exporti ng i n Hel p for more i nformati on on pri nti ng.
5 G raphics
5-28
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 Graphi cs Objects on
page 5-28 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 query the
val ue of each property 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.
Graphics Objects
Graphi cs objects are the basi c el ements used to di spl ay graphi cs and user
i nterface el ements. Thi s tabl e l i sts the graphi cs 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
Axes Axes for di spl ayi ng graphs i n a gure
Ui control User i nterface control that executes a functi on i n
response to user i nteracti on
Ui menu User-dened gure wi ndow menu
Ui contextmenu Pop-up menu i nvoked by ri ght cl i cki ng on a graphi cs
object
I mage Two-di mensi onal pi xel -based pi cture
H andle G raphics
5-29
O bject Hiera rchy
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.
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
Object Description
Uimenu
Line
Axes Uicontrol
Image
Figure
Uicontextmenu
Light Surface Patch Text
Root
Rectangle
5 G raphics
5-30
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
i nformati on about an object and a descri pti on of i ts properti es, see the
reference page for the objects creati on functi on. Object creati on functi ons have
the same name as the object. For exampl e, the object creati on functi on for axes
objects i s cal l ed ax 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 the MATLAB Functi on Reference i n Hel p for a
descri pti on of each of these functi ons.
Function Purpose
copy 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 ed property val ues
gca Return the handl e of the current axes
gcf Return the handl e of the current 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
H andle G raphics
5-31
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.
For More Information See Handl e Graphi cs Objects i n Hel p for
i nformati on on graphi cs objects.
Setting Proper ties from Plotting Comma nds
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 .
For exampl e, 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. 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 Proper ties of Ex isting O bjects
To modi fy the property val ues of exi sti ng objects, you can use the s et command
or, i f pl ot edi ti ng mode i s enabl ed, the Property Edi tor. The Property Edi tor
provi des a graphi cal user i nterface to many object properti es. Thi s secti on
descri bes how to use the set command. See Usi ng the Property Edi tor on
page 5-16 for more i nformati on.
Many pl otti ng commands can 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' )
5 G raphics
5-32
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.
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 FaceCol 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 get command).
H andle G raphics
5-33
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 )
For More Information See Structures and Cel l Arrays i n Hel p for
i nformati on on cel l arrays.
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.
1 1.5 2 2.5 3 3.5 4 4.5 5
0
5
10
15
20
25
5 G raphics
5-34
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 .
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 rch
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 ( gca, ' Ty pe' , ' t ex t ' , ' St r i ng' , ' \pi /2' ) ;
fi nds the stri ng 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 ed' ) , ' 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 Accessi ng Object Handl es i n Hel p for more
i nformati on.
2
G raphics U ser Interfaces
5-35
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 ck 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 i ncl udes a set of l ayout tool s that si mpl i fy the process of creati ng
graphi cal user i nterfaces (GUI s). These tool s i ncl ude:
Layout Edi tor add and arrange objects i n the fi gure wi ndow.
Al i gnment Tool al i gn objects wi th respect to each other.
Property I nspector i nspect and set property val ues.
Object Browser observe a hi erarchi cal l i st of the Handl e Graphi cs objects
i n the current MATLAB sessi on.
Menu Edi tor create wi ndow menus and context menus.
Access these tool s from the Layout Edi tor. To start the Layout Edi tor, use the
gui de command. For exampl e,
gui de
5 G raphics
5-36
di spl ays an empty l ayout.
To l oad an exi sti ng GUI for edi ti ng, type (the . f i g i s not requi red)
gui de mygui . f i g
or use Open... from the File menu on the Layout Edi tor.
For More Information See Creati ng Graphi cal User I nterfaces for more
i nformati on.
A nim ations
5-37
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 eMode 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
and .
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.
1 2 +1 2
5 G raphics
5-38
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
5-39
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)
5 G raphics
5-40
6
Programmi ng wi th
MATLAB
Flow Control . . . . . . . . . . . . . . . . . . . . 6-2
Other Data Structures . . . . . . . . . . . . . . . 6-7
Scripts and Functions . . . . . . . . . . . . . . . 6-17
Demonstration Programs Included with MATLAB . . . 6-27
6 Program m ing w ith M A TLA B
6-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
cont i nue statements
br eak statements
For More Information See Programmi ng and Data Types i n Hel p for a
compl ete di scussi on about programmi ng i n MATLAB.
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 true condi ti on woul d be executed.
Flow C ontrol
6-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
where they 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 are 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 equal
i s empt y
al l
any
6 Program m ing w ith M A TLA B
6-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 her wi s e
er r or ( ' Thi s i s i mpos s i bl e' )
end
Note 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 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
6-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 , 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.
continue
The cont i nue statement passes control to the next i terati on of the f or or whi l e
l oop i n whi ch i t appears, ski ppi ng any remai ni ng statements i n the body of the
x
3
2x 5
6 Program m ing w ith M A TLA B
6-6
l oop. I n nested l oops, cont i nue passes control to the next i terati on of the f or
or whi l e l oop encl osi ng i t.
The exampl e bel ow shows a cont i nue l oop that counts the l i nes of code i n the
fi l e, magi c. m, ski ppi ng al l bl ank l i nes and comments. A cont i nue statement i s
used to advance to the next l i ne i n magi c. m wi thout i ncrementi ng the count
whenever a bl ank l i ne or comment l i ne i s encountered.
f i d = f open( ' magi c. m' , ' r ' ) ;
count = 0;
whi l e ~f eof ( f i d)
l i ne = f get l ( f i d) ;
i f i s empt y ( l i ne) | s t r ncmp( l i ne, ' %' , 1)
cont i nue
end
count = count + 1;
end
di s p( s pr i nt f ( ' %d l i nes ' , count ) ) ;
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 eak exi ts from the i nnermost l oop onl y.
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
6-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 Programmi ng and Data Types i n Hel p.
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, , or sampl es of a ti me-dependent matri x,
. I n these l atter cases, the (i, j)th el ement of the kth matri x, or the 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) ;
A
k ( )
A t ( ) t
k
6 Program m ing w ith M A TLA B
6-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 thi rd matri x i n the sequence i s Drers.
M( : , : , 3)
ans =
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
16 3 2 13
8 11 10 8
12 7 6 12
1 14 15 1
16 2 13 3
10 8 11 10
6 12 7 6
15 1 14 15
13 16 2 3
8 5 11 10
12 9 7 6
1 4 14 15
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
.
.
.
O ther D ata Structures
6-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) ) }
6 Program m ing w ith M A TLA B
6-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
same si 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
6-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
6 Program m ing w ith M A TLA B
6-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. Sel ect
Preferences from the File menu. Be sure to try the Symbol and 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.
O ther D ata Structures
6-13
! "#$%&' ( ) *+, - . /
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
same l ength, and forms a character array wi th each l i ne i n a separate row. For
exampl e,
6 Program m ing w ith M A TLA B
6-14
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 ' ; ' moment 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+'
creates a scal ar structure wi th three fi el ds.
O ther D ata Structures
6-15
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 el ds :
name
s cor e
gr ade
There are several ways to reassembl e the vari ous fi el ds i nto other MATLAB
arrays. They are 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]
i t i s the same as
[ S( 1) . s cor e, S( 2) . s cor e, S( 3) . s cor e]
6 Program m ing w ith M A TLA B
6-16
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
6-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 .
For exampl e, create a fi l e cal l ed magi cr ank. m that 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
6 Program m ing w ith M A TLA B
6-18
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.
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. m i s avai l abl e i n the
di rectory
t ool box /mat l ab/mat f un
0 5 10 15 20 25 30 35
0
5
10
15
20
25
30
35
Scripts and Functions
6-19
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 es t i mat e of t he number of l i near l y
% i ndependent r ows or col 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.
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)
6 Program m ing w ith M A TLA B
6-20
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.
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' )
Scripts and Functions
6-21
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 es 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 egh, obj h] = l egend( ' appl es ' , ' or anges ' ) ;
I n addi ti on, you cannot use the unquoted form i f any of the arguments are not
stri ngs.
Constr ucting String Arguments in Code
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
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
6 Program m ing w ith M A TLA B
6-22
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 stri ng s .
Scripts and Functions
6-23
The exampl e of the previ 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 DO l 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 = . 01;
f or k = 1: 1001
y ( k) = l og10( x ) ;
x = x + . 01;
end
A vectori zed versi on of the same code i s
x = . 01: . 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 er 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
6 Program m ing w ith M A TLA B
6-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 Handles
You can create a handl e to any MATLAB functi on and then use that handl e as
a means of referenci ng the functi on. A functi on handl e i s typi cal l y passed i n an
argument l i st to other functi ons, whi ch can then execute, or evaluate, the
functi on usi ng the handl e.
Construct a functi on handl e i n MATLAB usi ng the at si gn, @, before the
functi on name. The fol l owi ng exampl e creates a functi on handl e for the s i n
functi on and assi gns i t to the vari abl e f handl e.
f handl e = @s i n;
Eval uate a functi on handl e usi ng the MATLAB f ev al functi on. The functi on
pl ot _f handl e, shown bel ow, recei ves a functi on handl e and data, and then
performs an eval uati on of the functi on handl e on that data usi ng f ev al .
f unct i on x = pl ot _f handl e( f handl e, dat a)
pl ot ( dat a, f ev al ( f handl e, dat a) )
When you cal l pl ot _f handl e wi th a handl e to the s i n functi on and the
argument shown bel ow, the resul ti ng eval uati on produces a si ne wave pl ot.
pl ot _f handl e( @s i n, - pi : 0. 01: pi )
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
Scripts and Functions
6-25
MATLAB represents 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 wi th
x = 0: . 002: 1;
y = humps ( x ) ;
Then pl ot the functi on wi th
pl ot ( x , y )
The graph shows that the functi on has a l ocal mi ni mum near x = 0.6. The
functi on f mi ns ear ch fi nds the minimizer, the val ue of x where the functi on
takes on thi s mi ni mum. The fi rst argument to f mi ns ear ch i s a functi on handl e
to 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.
0 x 1
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
6 Program m ing w ith M A TLA B
6-26
p = f mi ns ear ch( @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 quadrature and integration to di sti ngui sh
between numeri cal approxi mati on of defi ni te i ntegral s and numeri cal
i ntegrati on of ordi nary di fferenti al equati ons. MATLABs quadrature routi nes
are quad and quadl . The statement
Q = quadl ( @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
6-27
Demonstration Programs Included with MATLAB
MATLAB i ncl udes many demonstrati on programs 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.
The fol l owi ng tabl es l i st some of the current demonstrati on programs that are
avai l abl e, organi zed i nto these categori es:
MATLAB Matri x Demonstrati on Programs
MATLAB Numeri c Demonstrati on Programs
MATLAB Vi sual i zati on Demonstrati on Programs
MATLAB Language Demonstrati on Programs
MATLAB Di fferenti al Equati on Programs
MATLAB Gal l ery Demonstrati on Programs
MATLAB Game Demonstrati on Programs
MATLAB Mi scel l aneous Demonstrati on Programs
MATLAB Hel per Functi ons Demonstrati on Programs
6 Program m ing w ith M A TLA B
6-28
.
MATLAB Matrix Demonstration Programs
ai r f oi l Graphi cal demonstrati on of sparse matri x from NASA
ai rfoi l .
bucky dem Connecti vi ty graph of the Buckmi nster Ful l er geodesi c
dome.
del s qdemo 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 er 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 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.
MATLAB Numeric Demonstration Programs
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, or ?
f f t demo Use of the FFT functi on for spectral anal ysi s.
f i t demo Nonl i near curve t wi th si mpl ex al gori thm.
f pl ot demo Demonstrati on of pl otti ng a functi on.
e

e
D em onstration Program s Included w ith M A TLA B
6-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.
qhul l demo Tessel l ati on and i nterpol ati on of scattered data.
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 ndi ng wi th f z er o.
MATLAB Visualization Demonstration Programs
col or menu Demonstrati on of addi ng a col ormap to the current
gure.
cpl x demo Maps of functi ons of a compl ex vari abl e.
ear 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.
MATLAB Numeric Demonstration Programs (Continued)
6 Program m ing w ith M A TLA B
6-30
l or enz Graphi cal demonstrati on of the orbi 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.
x f our i er 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.
MATLAB Language Demonstration Programs
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.
MATLAB Differential Equation Programs
amp1dae Sti ff DAE from an el ectri cal ci rcui t.
bal l ode Equati ons of moti on for a bounci ng bal l used by
BALLDEMO.
br us s ode Sti ff probl em, model l i ng a chemi cal reacti on
(Brussel ator).
bur ger s ode Burger's equati on sol ved usi ng a movi ng mesh
techni que.
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.
MATLAB Visualization Demonstration Programs (Continued)
D em onstration Program s Included w ith M A TLA B
6-31
hb1dae Sti ff DAE from a conservati on l aw.
hb1ode Sti ff probl em 1 of Hi ndmarsh and Byrne.
hb3ode Sti ff probl em 3 of Hi ndmarsh and Byrne.
mat 4bv p Fi nd the fourth ei genval ue of the Mathi eus equati on.
odedemo Demonstrati on of the ODE sui te i ntegrators.
odeex ampl es Browse the MATLAB ODE/DAE/BVP/PDE exampl es.
or bi t ode Restri cted 3 body probl em used by ORBI TDEMO.
pdex 1 Exampl e 1 for PDEPE.
pdex 2 Exampl e 2 for PDEPE.
pdex 3 Exampl e 3 for PDEPE.
pdex 4 Exampl e 4 for PDEPE.
r i gi dode Eul er equati ons of a ri gi d body wi thout external forces.
s hockbv p The sol uti on has a shock l ayer near x = 0.
t wobv p BVP that has exactl y two sol uti ons.
v dpode Parameteri zabl e van der Pol equati on (sti ff for l arge ).
MATLAB Gallery Demonstration Programs
cr 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.
MATLAB Differential Equation Programs (Continued)
6 Program m ing w ith M A TLA B
6-32
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 .
MATLAB Game Demonstration Programs
f i f t een Sl i di ng puzzl e.
l i f e Conways Game of Li fe.
s oma Soma cube.
x pbombs Mi nesweeper game.
MATLAB Miscellaneous Demonstration Programs
chai ngui Matri x chai n mul ti pl i cati on opti mi zati on.
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.
MATLAB Gallery Demonstration Programs (Continued)
D em onstration Program s Included w ith M A TLA B
6-33
Getting More Information
The MathWorks Web si te (www. mat hwor ks . com) 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 Downloads. Al so, Technical 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.
wr l dt r v Great ci rcl e 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.
MATLAB Helper Functions Demonstration Programs
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.
f i nddemo Command that 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 gure wi ndow.
MATLAB Miscellaneous Demonstration Programs (Continued)
6 Program m ing w ith M A TLA B
6-34
7
Symbol i c Math Tool box
Introduction . . . . . . . . . . . . . . . . . . . . 7-2
Getting Help . . . . . . . . . . . . . . . . . . . . 7-4
Getting Started . . . . . . . . . . . . . . . . . . 7-5
Calculus . . . . . . . . . . . . . . . . . . . . . . 7-16
Simplifications and Substitutions . . . . . . . . . . 7-43
Variable-Precision Arithmetic . . . . . . . . . . . . 7-58
Linear Algebra . . . . . . . . . . . . . . . . . . . 7-63
Solving Equations . . . . . . . . . . . . . . . . . 7-86
7 Sym bolic M ath Toolbox
7-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 6
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 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 fferenti 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 ed accuracy
Introduction
7-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 Information You can access compl ete reference i nformati on for
the Symbol i c Math Tool box functi ons from Hel p. Al so, you can pri nt the PDF
versi on of the compl ete Symbolic Math Toolbox Users Guide (tutori al and
reference i nformati on) from the Symbol i c Math Tool box roadmap i n Hel p.
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 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
7 Sym bolic M ath Toolbox
7-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 onl i ne Hel p,
whi ch contai ns tutori al s and 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
Overl oadi ng Operators and Functi ons i n UsingMATLAB, whi ch i s accessi bl e
from Hel p.
G etting Started
7-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(for more i nformati on on data types, the MATLAB topi c
Programmi ng and Data Types i n Using MATLAB). 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.
7 Sym bolic M ath Toolbox
7-6
Creating Symbolic Variables and Expressions
The s y m command 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 . The
statement
f = s y m( ' a*x ^2 + b*x + c' )
assi gns the symbol i c expressi on 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, , , , and . To perform symbol i c math

1 5 +
2
----------------- =
f ax
2
bx c + + =
ax
2
bx c + +
a b c x
G etting Started
7-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 m or 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 m functi 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 m wi thout a second
argument i s the same as usi ng s y m wi th the ' r ' opti on.
s y m( t )
ans =
1/10
7 Sym bolic M ath Toolbox
7-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 m i 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 m to A
A = s y m( A)
G etting Started
7-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 m command 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 ' )
7 Sym bolic M ath Toolbox
7-10
The command
cl ear x
does not make x a nonreal vari abl e.
Crea ting Abstra ct Functions
I f you want to create an abstract (i .e., i ndetermi nant) functi on , type
f = s y m( ' f ( x ) ' )
Then f acts l i ke 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 m i s useful when computi ng Fouri er, Lapl ace, and
z-transforms.
Ex a mple: Crea ting a Symbolic M a trix
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 ]
f x ( )
f x ( )
G etting Started
7-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.
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.
7 Sym bolic M ath Toolbox
7-12
I f we ask for the deri vati ves of these expressi ons, wi thout speci fyi ng the
i ndependent vari abl e, then by mathemati cal conventi on we obtai n ,
, and . Lets assume that the
i ndependent vari abl es i n these three expressi ons are , , and , respecti vel y.
The other symbol s, , , , and , 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 , for exampl e, we coul d wri te
to get .
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 )
returns
ans =
x ^n*n/x
Mathematical Function MATLAB Command
f = x ^n
g = s i n( a*t + b)
h = bes s el j ( nu, z )
f x
n
=
g at b + ( ) si n =
h J
v
z ( ) =
f nx
n
=
g a at b + ( ) cos = h J
v
z ( ) v z ( ) J
v 1 +
z ( ) =
x t z
n a b v
n
or
d
dn
------- f x ( )
d
dn
------- x
n
x
n
x l n
G etting Started
7-13
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 m command
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 m rul e (see bel ow). The
absence of a second argument i n f i nds y m resul ts i n a l i st of al l symbol i c
vari abl es i n a gi ven symbol i c expressi on. We see thi s demonstrated bel ow. The
command
f i nds y m( g)
returns the resul t
ans =
a, b, t
ndsym 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.
7 Sym bolic M ath Toolbox
7-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 ndsym
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
7-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 unct 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 vari abl es. See the
MATLAB topi c Programmi ng and Data Types i n UsingMATLAB for a more
detai l ed di scussi on on object-ori ented programmi ng.
7 Sym bolic M ath Toolbox
7-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
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
df da
C alculus
7-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 =
- bes 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) cos ( a*t +b) *a
ex p( i *t het a) i *ex p( i *t het a)
7 Sym bolic M ath Toolbox
7-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
coordi nates as gi ven by , , and
. 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 ] )
return the Jacobi an
r , , ( ) x r cos cos = y r si n cos =
z r si n =
z
y
x
( x , y , z )

r
J
x y x , , ( )
r , , ( )
----------------------- =

C alculus
7-19
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 acobi 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 acobi an( [ r : t ] , [ u, v ] )
x d
df
a d
df
b
2
2
d
d f
J
r t , ( )
u v , ( )
----------------- =
7 Sym bolic M ath Toolbox
7-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) - cos ( 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 are not. Hence, the three
l i mi ts
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
f x ( )
, , and
1
x
---
x 0
l i m
1
x
---
x 0-
l i m
1
x
---
x 0+
l i m
+
C alculus
7-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
7 Sym bolic M ath Toolbox
7-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 ( bes s el j ( 1, z ) ) or
i nt ( bes 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
7-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 expressi on for
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) - 1/a*cos ( a*t het a+b)
ex p( - x 1^2) 1/2*pi ^( 1/2) *er f ( x 1)
1/( 1+u^2) at an( u)
and f x ( )
a
b

dx f v ( ) v d
a
b

7 Sym bolic M ath Toolbox


7-24
Here are some addi ti onal exampl es.
For the Bessel functi on (bes s el j ) exampl e, i t i s possi bl e to compute a
numeri cal approxi mati on to the val ue of the i ntegral , usi ng the doubl e
functi on. The command
a = i nt ( bes s el j ( 1, z ) , 0, 1)
returns
a =
1/4*hy per geom( [ 1] , [ 2, 2] , - 1/4)
and the command
a = doubl e( a)
returns
a =
0. 2348
Integra tion w ith Rea l Consta nts
One of the subtl eti es i nvol ved i n symbol i c i ntegrati on i s the val ue of vari ous
parameters. For exampl e, the expressi on
i s the posi ti ve, bel l shaped curve that tends to 0 as x tends to for any real
number k. An exampl e of thi s curve i s depi cted bel ow wi th
f a, b int(f,a,b)
x ^7 0, 1 1/8
1/x 1, 2 l og( 2)
l og( x ) *s qr t ( x ) 0, 1 - 4/9
ex p( - x ^2) 0, i nf 1/2*pi ^( 1/2)
bes s el j ( 1, z ) 0, 1 1/4*hy per geom( [ 1] , [ 2, 2] , - 1/4)
e
kx ( )
2

C alculus
7-25
and generated, usi ng these commands.
s y ms x
k = s y m( 1/s qr t ( 2) ) ;
f = ex p( - ( k*x ) ^2) ;
ez pl ot ( f )
The Mapl e kernel , however, does not, a priori, treat the expressi ons or
as posi ti ve numbers. To the contrary, Mapl e assumes that the symbol i c
vari abl es and as a priori i ndetermi nate. That i s, they are purel y formal
vari abl es wi th no mathemati cal properti es. Consequentl y, the i ni ti al attempt
to compute the i ntegral
k
1
2
------- =
3 2 1 0 1 2 3
0
0.2
0.4
0.6
0.8
1
x
exp(1/2 x
2
)
k
2
x
2
x k
7 Sym bolic M ath Toolbox
7-26
i n the Symbol i c Math Tool box, usi ng the commands
s y ms x k;
f = ex p( - ( k*x ) ^2) ;
i nt ( f , x , - i nf , i nf )
resul ts i n the output
Def i ni t e i nt egr at i on: Can' t det er mi ne i f t he i nt egr al i s
conv er gent .
Need t o know t he s i gn of - - > k^2
Wi l l now t r y i ndef i ni t e i nt egr at i on and t hen t ake l i mi t s .
War ni ng: Ex pl i ci t i nt egr al coul d not be f ound.
ans =
i nt ( ex p( - k^2*x ^2) , x = - i nf . . i nf )
I n the next secti on, you wi l l see how to make a real vari abl e and therefore
posi ti ve.
Rea l Va ria bles via sym
Noti ce that Mapl e i s not abl e to determi ne the si gn of the expressi on k^2. How
does one surmount thi s obstacl e? The answer i s to make k a real vari abl e, usi ng
the s y m command. One parti cul arl y useful feature of s y m, namel y the r eal
opti on, al l ows you to decl are k to be a real vari abl e. Consequentl y, the i ntegral
above i s computed, i n the tool box, usi ng the sequence
s y ms k r eal
i nt ( f , x , - i nf , i nf )
whi ch returns
ans =
s i gnum( k) /k*pi ^( 1/2)
Noti ce that k i s now a symbol i c object i n the MATLAB workspace and a real
vari abl e i n the Mapl e kernel workspace. By typi ng
cl ear k
e
kx ( )
2

dx
k
k
2
C alculus
7-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 , whi l e the geometri c seri es adds to ,
provi ded . 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
------ + + +

2
6 1 x x
2
+ + + 1 1 x ( )
x 1 <
7 Sym bolic M ath Toolbox
7-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 i n the
Tayl or seri es for .
Techni cal l y, T i 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 T i 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.
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.
O x
8
( ) ( )
f x ( )
x a ( )
n
n 0 =

f
n ( )
a ( )
n!
------------------
C alculus
7-29
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.
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
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
f x ( )
1
5 4 x ( ) cos +
------------------------------ =
7 Sym bolic M ath Toolbox
7-30
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 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
domai n for a functi on di spl ayed by ez pl ot i s . To produce a graph
of for , type
ez pl ot ( f , [ a b] )
f x ( )
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))
2 x 2
f x ( ) a x b
C alculus
7-31
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] )
From the graph, i t appears that the val ues of 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).
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)
f x ( )
7 Sym bolic M ath Toolbox
7-32
The actual maxi ma and mi ni ma of occur at the zeros of . The
statements
f 3 = di f f ( f 2) ;
pr et t y ( f 3)
compute 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 .
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 ]
each of whose entri es i s a zero of . The commands
f or mat ; % Def aul t f or mat of 5 di gi t s
z r = doubl e( z )
f x ( ) f x ( )
f x ( )
f x ( )
f x ( )
C alculus
7-33
convert 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 4 2 0 2 4 6
3
2
1
0
1
2
3
x
Zeros of f3
7 Sym bolic M ath Toolbox
7-34
Thi s occurs because contai ns a factor of , 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 zero 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
z r = [ z r - 2*pi z r z r +2*pi ] ;
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' )
f x ( ) x ( ) si n

2
6 4 2 0 2 4 6
3
2
1
0
1
2
3
x
Zeros of f3
C alculus
7-35
The fi rst zero of 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 20 =
0. 0494
A l ook at the graph of 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
f x ( )
f 0 ( )
f x ( )
7 Sym bolic M ath Toolbox
7-36
i ndi cates that the gl obal mi ni ma occur near and . We can
demonstrate that they occur exactl y at , usi ng the fol l owi ng sequence of
commands. Fi rst we try substi tuti ng and i nto .
s i mpl e( [ s ubs ( f 3, x , - s y m( pi ) ) , s ubs ( f 3, x , s y m( pi ) ) ] )
The resul t
ans =
[ 0, 0]
shows that and happen to be cri ti cal poi nts of . 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' )
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
x = x =
x =
f x ( )
f x ( )

C alculus
7-37
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
i s [ - 4, 1] . We can confi rm the other part by exami ni ng the fourth zero
of 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
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
f x ( )
f x ( )
7 Sym bolic M ath Toolbox
7-38
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' )
vi sual l y confi rms that s i s a maxi mum.
The maxi mum i s M1 = 1. 0051.
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
C alculus
7-39
Therefore, our guess that the maxi mum of 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 twi ce wi th respect to x recovers our ori gi nal
functi on . The command
g = i nt ( i nt ( f 2) )
returns
g =
- 8/( t an( 1/2*x ) ^2+9)
Thi s i s certai nl y not the ori gi nal expressi on for . Lets l ook at the di fference
.
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 twi ce, then i ntegrati ng the
resul t twi ce, produces a functi on that may di ffer from by a l i near functi on
of .
Fi nal l y, i ntegrate 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.
f x ( )
f x ( )
f x ( ) 1 5 4 x cos + ( ) =
f x ( )
f x ( ) g x ( )
f x ( )
f x ( )
x
f x ( )
7 Sym bolic M ath Toolbox
7-40
Though 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 has jumps at . Thi s occurs because i s si ngul ar at
.
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 .
F 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))
F x ( ) x = x tan
x =
2
C alculus
7-41
To obtai n a representati on of that does not have jumps at these poi nts, we
must i ntroduce a second functi on, , that compensates for the
di sconti nui ti es. Then we add the appropri ate mul ti pl e of to
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 )
and pl ot the resul t.
ez pl ot ( F1, [ - 6. 28, 6. 28] )
Thi s representati on does have a conti nuous graph.
6 4 2 0 2 4 6
1.5
1
0.5
0
0.5
1
1.5
x
atan(tan(x))
F x ( )
J x ( )
J x ( ) F x ( )
7 Sym bolic M ath Toolbox
7-42
Noti ce that we use the domai n [-6.28, 6.28] i n ez pl ot rather than the defaul t
domai n . The reason for thi s i s to prevent an eval uati on of
at the si ngul ar poi nts and 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 round(1/2 x/)
2 2 [ , ]
F 1 2 3 1 3 1 2 x tan ( ) atan = x = x =
Sim plifications and Substitutions
7-43
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 numeri cal eval uati on near the roots. 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?
7 Sym bolic M ath Toolbox
7-44
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.
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,
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
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
Sim plifications and Substitutions
7-45
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,
fa ctor
I f f i s a pol ynomi al wi th rati onal coeffi ci ents, the statement
f act 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,
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 ) ) 4*x ^3- 3*x
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
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)
f expand(f)
7 Sym bolic M ath Toolbox
7-46
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) ]
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' ) ]
Sim plifications and Substitutions
7-47
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) ]
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.
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( x y ) l og( x ) +l og( y )
ex 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
cos ( x ) ^2 + s i n( x ) ^2 1
7 Sym bolic M ath Toolbox
7-48
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 ect , 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 )
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( cos ( 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
Sim plifications and Substitutions
7-49
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
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)
7 Sym bolic M ath Toolbox
7-50
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
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
2+1/a
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
2cos ( x ) ^2- s i n( x ) ^2 3cos ( x ) ^2- 1
cos ( x ) ^2- s i n( x ) ^2 cos ( 2x )
Sim plifications and Substitutions
7-51
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) ) ]
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
f simple(f)
7 Sym bolic M ath Toolbox
7-52
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 ubex pr ( s )
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) ) ]
Sim plifications and Substitutions
7-53
[ - 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 ubex 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)
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)
7 Sym bolic M ath Toolbox
7-54
throughout v and E. We fi rst use s ubex pr
v = s ubex 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]
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
Sim plifications and Substitutions
7-55
a = 10; b = 2; c = 10;
s ubs ( S)
ans =
8
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 m objects.
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.
One approach i s to type
ez pl ot ( M) ; hol d on; ez pl ot ( P)
7 Sym bolic M ath Toolbox
7-56
but thi s pl ot
does not readi l y hel p us i denti fy the curves.
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.
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)
Sim plifications and Substitutions
7-57
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
7 Sym bolic M ath Toolbox
7-58
Variable-Precision Arithmetic
Overview
There are three di fferent ki nds of ari thmeti c operati ons i n thi s tool box.
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.
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
Variable-Precision A rithm etic
7-59
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
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 m functi 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]
7 Sym bolic M ath Toolbox
7-60
[ 21/10, 11/5, 23/10]
[ 31/10, 16/5, 33/10]
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)
Variable-Precision A rithm etic
7-61
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
7 Sym bolic M ath Toolbox
7-62
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.
Linear A lgebra
7-63
Linear Algebra
Basic Algebraic Operations
Basi c al gebrai c operati ons on symbol i c objects are the same as operati 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 G to 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)
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.
7 Sym bolic M ath Toolbox
7-64
A =
[ cos ( 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
The computed el ements of H are fl oati ng-poi nt numbers that are the rati os of
smal l i ntegers. I ndeed, H i s a MATLAB array of cl ass doubl e. Converti ng H to
a symbol i c matri x
H = s y m( H)
Linear A lgebra
7-65
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 H to 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 operator 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]
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) )
7 Sym bolic M ath Toolbox
7-66
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 H i s nonsi ngul ar, the nul l space of H
nul l ( H)
and the col umn space of H
col s pace( H)
Linear A lgebra
7-67
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 H si 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 H to 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
and
i nv ( H)
7 Sym bolic M ath Toolbox
7-68
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)
The vari abl e-preci si on counterparts are
E = ei g( v pa( A) )
[ V, E] = ei g( v pa( A) )
Linear A lgebra
7-69
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 H from the l ast secti on provi 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 T and E. The col umns of T are 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 T and E to deci mal notati on. We proceed as
fol l ows. The commands
Td = doubl e( T)
Ed = doubl e( E)
return
Td =
1. 0000 1. 6497 - 1. 2837
- 4. 0000 1. 0000 1. 0000
3. 3333 0. 7051 1. 5851
7 Sym bolic M ath Toolbox
7-70
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 act or ( pol y ( H) ) /x ) ;
s ol v e( g)
Cl osed form symbol i c expressi ons for the ei genval ues are 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]
The commands
p = pol y ( R) ;
pr et t y ( f act or ( p) )
Linear A lgebra
7-71
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
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.
7 Sym bolic M ath Toolbox
7-72
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 ) ]
Next, the command
g = ei g( G)
produces
A
0 1
1 0
=
Linear A lgebra
7-73
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 ) ]
how =
combi ne
7 Sym bolic M ath Toolbox
7-74
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.
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]
Linear A lgebra
7-75
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
A
2
I ( )v
4
v
3
=
A
1
I ( )v
2
v
1
=
7 Sym bolic M ath Toolbox
7-76
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
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) ) ;

Linear A lgebra
7-77
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 subtracti 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) ]
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

7 Sym bolic M ath Toolbox


7-78
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 are 1, 2, 3.
Fi rst, we perturb A by another matri x E and parameter . As t
i ncreases from 0 to 10
-6
, the ei genval ues , , change to
, , .

t: A A tE +

1
1 =
2
2 =
3
3 =

1
1.5596 0.2726i +
2
1.5596 0.2726i
3
2.8808
Linear A lgebra
7-79
Thi s, i n turn, means that for some val ue of , the perturbed
matri x has a doubl e ei genval ue .
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 er 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
ei genval ues may vary from one machi ne to another, but on a typi cal
workstati on, the statements
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)
t 0 10
6
< < , =
A t ( ) A tE + =
1

2
=

7 Sym bolic M ath Toolbox


7-80
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
veri 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
coal esce i nto an actual doubl e root that i s extremel y sensi ti ve to roundoff and
other errors.
Linear A lgebra
7-81
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 ( col 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
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
7 Sym bolic M ath Toolbox
7-82
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 = 5e007
1 1.5 2 2.5 3
0.5
0
0.5
t = 1e006
Linear A lgebra
7-83
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
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
7 Sym bolic M ath Toolbox
7-84
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 .
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
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

Linear A lgebra
7-85
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
. To achi eve thi s, substi tute for t i n the perturbed matri x
and fi nd the ei genval ues of . That i s,
e = ei g( doubl e( s ubs ( A, t , t au) ) )
e =
1. 5476
1. 5476
2. 9047
confi rms that i s a doubl e ei genval ue of for t = 7.8379e-07.

1.5476 =
A t ( ) A tE + = A t ( )
1.5476 = A t ( )
7 Sym bolic M ath Toolbox
7-86
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 zero. 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 . I f you need
to sol ve equati ons of the form , 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' )
returns a vector wi th four sol uti ons.
f x ( ) 0 =
f x ( ) q x ( ) =
Solving Equations
7-87
s =
[ 0]
[ pi ]
[ 1/6*pi ]
[ 5/6*pi ]
Several Algebraic Equations
Now l ets l ook at systems of equati ons. Suppose we have the system
and we want to sol ve for x and 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
--- =
7 Sym bolic M ath Toolbox
7-88
appears to have redundant components. Thi s i s due to the fi rst equati on
, whi ch has two sol uti ons i n x and y: , . 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 m to
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]
x
2
y
2
0 = x 0 = y 0 =
Solving Equations
7-89
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 ]
and
7 Sym bolic M ath Toolbox
7-90
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 D to 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 . 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.
Ex a mple 1
The fol l owi ng cal l to ds ol v e
ds ol v e( ' Dy =1+y ^2' )
d
2
y dt
2

Solving Equations
7-91
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 )
produce
y =
- 2/3*cos ( x ) ^2+1/3+4/3*cos ( x )
7 Sym bolic M ath Toolbox
7-92
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
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 and D2u( 0) for .
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 =
ex p( 3*t ) *( cos ( 4*t ) *C1+s i n( 4*t ) *C2)
g = S. g
g =
- ex p( 3*t ) *( s i n( 4*t ) *C1- cos ( 4*t ) *C2)
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 )
x
3
3
d
d u
u =
u 0 ( ) 1 u 0 ( ) , 1, u 0 ( ) = = =
d
3
u dx
3
u 0 ( )
Solving Equations
7-93
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
4 y t ( ) + e
t
=
y 0 ( ) 1 =
x
2
2
d
d y
4 y 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 ( ) = , =
7 Sym bolic M ath Toolbox
7-94
A
MATLAB Qui ck Reference
Introduction . . . . . . . . . . . . . . . . . . . . A-2
General Purpose Commands . . . . . . . . . . . . A-3
Operators and Special Characters . . . . . . . . . . A-5
Logical Functions . . . . . . . . . . . . . . . . . A-5
Language Constructs and Debugging . . . . . . . . A-5
Elementary Matrices and Matrix Manipulation . . . . A-7
Specialized Matrices . . . . . . . . . . . . . . . . A-8
Elementary Math Functions . . . . . . . . . . . . . A-8
Specialized Math Functions . . . . . . . . . . . . . A-9
Coordinate System Conversion . . . . . . . . . . . A-10
Matrix Functions - Numerical Linear Algebra . . . . . A-10
Data Analysis and Fourier Transform Functions . . . A-11
Polynomial and Interpolation Functions . . . . . . . A-12
Function Functions - Nonlinear Numerical Methods . . A-13
Sparse Matrix Functions . . . . . . . . . . . . . . A-14
Sound Processing Functions . . . . . . . . . . . . A-15
Character String Functions . . . . . . . . . . . . . A-16
File I/O Functions . . . . . . . . . . . . . . . . . A-17
Bitwise Functions . . . . . . . . . . . . . . . . . A-17
Structure Functions . . . . . . . . . . . . . . . . A-18
MATLAB Object Functions . . . . . . . . . . . . . A-18
MATLAB Interface to J ava Functions . . . . . . . . A-18
Cell Array Functions . . . . . . . . . . . . . . . . A-19
Multidimensional Array Functions . . . . . . . . . . A-19
Data Visualization . . . . . . . . . . . . . . . . . A-19
Graphical User Interfaces . . . . . . . . . . . . . . A-24
Serial Port I/O . . . . . . . . . . . . . . . . . . . A-25
A
A-2
Introduction
Thi s appendi x l i sts the MATLAB functi ons as they are grouped i n Hel p 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 Hel p and ei ther:
Sel ect the functi on from the MATLAB Functi on Reference (Functi ons by
Category or Al phabeti cal Li st of Functi ons), or
From the Search tab i n the Hel p Navi gator, sel ect Function Name as
Search type, type the functi on name i n the Search for fi el d, and cl i ck Go.
Note I f you are vi ewi ng thi s book from Hel p, 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 Hel p browser
docopt Di spl ay l ocati on of hel p l e
di rectory for UNI X pl atforms
genpat h Generate a path stri ng
hel p Display M-file help for
MATLAB functions in the
Command Window
hel pbr ows er Di spl ay Hel p browser for access
to al l MathWorks onl i ne hel p
hel pdes k Di spl ay Hel p browser
hel pwi n Display M-file help and provide
access to M-file help for all
functions
l as t er r Last error message
l as t war n Last warni ng message
l i cens e Show MATLAB l i cense number
l ookf or Search for specified keyword in
M-file help entries
par t i al pat h Parti al pathname
pat h Control MATLABs di rectory
search path
pat ht ool Open the GUI for viewing and
modifying MATLABs path
pr of i l e Start the M-l e prol er, a uti l i ty
for debuggi ng and opti mi zi ng
code
pr of r epor t Generate a prol e report
r ehas h Refresh functi on and l e system
caches
r mpat h Remove di rectori es from
MATLABs search path
s uppor t Open MathWorks Techni cal
Support Web page
t y pe Li st 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 Get MATLAB versi on number
web Poi nt Hel p browser or Web
browser at l e or Web si te
what Li st MATLAB-speci c l es i n
current di rectory
what s new Di spl ay README l es for
MATLAB and tool boxes
whi ch Locate functi ons and l es
Managing Variables and the Workspace
cl ear Remove i tems from the
workspace
di s p Di spl ay text or array
l engt h Length of vector
l oad Retri eve vari abl es from di sk
memor y Hel p for memory l i mi tati ons
ml ock Prevent M-l e cl eari ng
munl ock Al l ow M-l e cl eari ng
openv ar Open workspace vari abl e i n
Array Edi tor for graphi cal
edi ti ng
pack Consol i date workspace memory
s av e Save workspace vari abl es on
di sk
s av eas Save gure or model usi ng
speci ed format
Managing Commands and Functions (Continued)
A M A TLA B Q uick Reference
A-4
s i z e Array di mensi ons
who, whos Li st the vari abl es i n the
workspace
wor ks pace Di spl ay the Workspace browser,
a GUI for managi ng the
workspace
Controlling the Command Window
cl c Cl ear Command Wi ndow
echo Echo M-l es duri ng executi on
f or mat Control the di spl ay format for
output
home Move cursor to upper l eft corner
of Command Wi ndow
mor e Control paged output for the
command wi ndow
Working with Files and the Operating
Environment
beep Produce a beep sound
cd Change worki ng di rectory
checki n Check l e i nto source control
system
checkout Check l e out of source control
system
cmopt s Get name of source control
system, and PVCS project
l ename
copy f i l e Copy l e
cus t omv er ct r l Al l ow custom source control
system
del et e Del ete l es and graphi cs objects
di ar y Save sessi on to a di sk l e
di r Di spl ay a di rectory l i sti ng
edi t Edi t an M-l e
f i l epar t s Get l ename parts
Managing Variables and the Workspace
(Continued)
f i l ebr ows er Di spl ay Current Di rectory
browser, for vi ewi ng fi l es
f ul l f i l e Bui l d ful l l ename from parts
i nf o Di spl ay contact i nformati on or
tool box Readme l es
i nmem Functi ons i n memory
l s Li st di rectory on UNI X
mat l abr oot Get root di rectory of MATLAB
i nstal l ati on
mkdi r Make new di rectory
open Open l es based on extensi on
pwd Di spl ay current di rectory
t empdi r Return the name of the systems
temporary di rectory
t empname Uni que name for temporary l e
undocheckout Undo previ ous checkout from
source control system
uni x Execute a UNI X command and
return the resul t
! Execute operati ng system
command
Starting and Quitting MATLAB
f i ni s h MATLAB termi nati on M-l e
ex i t Termi nate MATLAB
mat l ab Start MATLAB (UNI X systems
onl y)
mat l abr c MATLAB startup M-l e
qui t Termi nate MATLAB
s t ar t up MATLAB startup M-l e
Working with Files and the Operating
Environment (Continued)
O perators and Special C haracters
A-5
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.
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.
Operators and Special Characters
+ Pl us
- Mi nus
* Matri x mul ti pl i cati on
. * Array mul ti pl i cati on
^ Matri 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
== Equal i ty
< > Rel ati onal operators
& Logi cal AND
| Logi cal OR
~ Logi cal NOT
x or Logi cal EXCLUSI VE OR
Logical Functions
al l Test to determi ne i f al l el ements
are nonzero
any Test for any nonzeros
ex i s t Check i f a vari abl e or 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
i s key wor d Test i f stri ng i s a MATLAB
keyword
i s v ar name Test i f stri ng i s a val i d vari abl e
name
l ogi cal Convert numeri c val ues to
l ogi cal
mi s l ocked True i f M-l e cannot be cl eared
MATLAB as a Programming Language
bui l t i n Execute bui l ti n functi on from
overl oaded method
ev al I nterpret stri ngs contai ni ng
MATLAB expressi ons
ev al c Eval uate MATLAB expressi on
wi th capture
A M A TLA B Q uick Reference
A-6
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-l es
gl obal Dene gl obal vari abl es
nar gchk Check number of i nput
arguments
per s i s t ent Dene persi stent vari abl e
s cr i pt Scri pt M-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
cont i nue Pass control to the next i terati on
of f or or whi l e l oop
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 i ndex
er r or Di spl ay error messages
f or Repeat statements a speci 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 Return to the i nvoki ng functi on
s wi t ch Swi tch among several cases
based on expressi on
t r y Begi n t r y bl ock
MATLAB as a Programming Language
(Continued)
war ni ng Di spl ay warni ng message
whi l e Repeat statements an i ndeni te
number of ti mes
Interactive Input
i nput Request user i nput
key boar d I nvoke the keyboard i n an M-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 l ter for objects
s i ngl e Convert to si ngl e preci 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 breakpoi nts
dbcont Resume executi on
dbdown Change l ocal workspace context
dbmex Enabl e MEX-l e debuggi ng
dbqui t Qui t debug mode
dbs t ack Di spl ay functi on cal l stack
dbs t at us Li st al l breakpoi nts
Control Flow (Continued)
Elem entary M atrices and M atrix M anipulation
A-7
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.
dbs t ep Execute one or more l i nes from a
breakpoi nt
dbs t op Set breakpoi nts i n an M-l e
functi on
dbt y pe Li st M-l e wi th l i ne numbers
dbup Change l ocal workspace context
Function Handles
f unct i on_handl e MATLAB data type that is a
handle to a function
f unct i ons Return information about a
function handle
f unc2s t r Constructs a function name string
from a function handle
s t r 2f unc Constructs a function handle from
a function name string
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 pace Generate l i nearl y spaced vectors
l ogs pace Generate l ogari thmi cal l y spaced
vectors
numel Number of el ements i n a matri x
or cel l array
ones Create an array of al l ones
r and Uni forml y di stri buted random
numbers and arrays
r andn Normal l y di stri buted random
numbers and arrays
Debugging (Continued)
z er os Create an array of al l zeros
: ( col on) Regul arl y spaced vector
Special Variables and Constants
ans The most recent answer
comput er I denti fy the computer on whi ch
MATLAB i s runni ng
eps Fl oati ng-poi nt rel ati ve accuracy
i I magi nary uni t
I nf I nni ty
i nput name I nput argument name
j I magi nary uni t
NaN Not-a-Number
nar gi n, nar gout Number of functi on arguments
nar gout chk Val i date number of output
arguments
pi Rati o of a ci rcl es ci rcumference
to i ts di ameter
r eal max Largest posi ti ve oati ng-poi nt
number
r eal mi n Smal l est posi ti ve oati ng-poi nt
number
v ar ar gi n,
v ar ar gout
Pass or return vari 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
Elementary Matrices and Arrays (Continued)
A M A TLA B Q uick Reference
A-8
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.
t i c, t oc Stopwatch ti mer
weekday Day of the week
Matrix Manipulation
cat Concatenate arrays
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 array
r es hape Reshape array
r ot 90 Rotate matri x 90 degrees
t r i l Lower tri angul ar part of a
matri x
t r i u Upper tri angul ar part of a
matri x
: ( col on) I ndex i nto array, rearrange
array
Vector Functions
cr os s Vector cross product
dot Vector dot product
i nt er s ect Set intersection of two vectors
i s member Detect members of a set
s et di f f Return the set difference of two
vectors
s et x or Set exclusive or of two vectors
uni on Set union of two vectors
uni que Unique elements of a vector
Time and Dates (Continued)
Specialized Matrices
compan Compani on matri x
gal l er y Test matri ces
hadamar d Hadamard matri x
hankel Hankel matri x
hi l b Hi l bert 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
hyperbol i c cosi ne
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 nverse
hyperbol i c si ne
Specialized M ath Functions
A-9
Specialized Math Functions
Thi s set of functi ons i ncl udes Bessel , el l i pti c,
gamma, factori al , and others.
at an, at anh I nverse tangent and i nverse
hyperbol i c tangent
at an2 Four-quadrant i nverse tangent
cei l Round toward i nni ty
compl ex Construct compl ex data from
real and i magi nary components
conj Compl ex conjugate
cos , cos h Cosi ne and hyperbol 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 nni 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 Natural l ogari thm
l og2 Base 2 l ogari thm and di ssect
oati ng-poi nt numbers i nto
exponent and manti ssa
l og10 Common (base 10) l ogari thm
mod Modul us (si gned remai nder
after di vi si on)
nchoos ek Bi nomi al coefci ent or al l
combi nati ons
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 ech Secant and hyperbol i c secant
s i gn Si gnum functi on
s i n, s i nh Si ne and hyperbol i c si ne
s qr t Square root
t an, t anh Tangent and hyperbol i c tangent
Elementary Math Functions (Continued)
Specialized Math Functions
ai r y Ai ry 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 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
rst 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 ntegral
f act or i al Factori al functi on
gamma, gammai nc,
gammal n
Gamma functi ons
l egendr e Associ ated Legendre functi ons
pow2 Base 2 power and scal e
oati ng-poi nt numbers
r at , r at s Rati onal fracti on approxi mati on
A M A TLA B Q uick Reference
A-10
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 Transform Cartesi an
coordi nates to pol ar or
cyl i ndri cal
car t 2s ph Transform Cartesi an
coordi nates to spheri cal
pol 2car t Transform pol ar or cyl i ndri cal
coordi nates to Cartesi an
s ph2car t Transform spheri cal coordi nates
to Cartesi an
Matrix Analysis
cond Condi ti on number wi th respect
to i nversi on
condei g Condi ti on number wi th respect
to ei genval ues
det Matri x determi nant
nor m Vector and matri x norms
nul l Nul l space of a matri x
or t h Range space of a matri x
r ank Rank of a matri x
r cond Matri 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 pace Angl e between two subspaces
t r ace Sum of di agonal el ements
Linear Equations
chol Chol esky factori zati on
i nv Matri x i nverse
l s cov Least squares sol uti on i n the
presence of known covari ance
l u LU matri x factori zati on
l s qnonneg Nonnegati ve l east squares
mi nr es Mi ni mum Resi dual Method
pi nv Moore-Penrose pseudoi nverse of
a matri x
qr Orthogonal -tri angul ar
decomposi ti on
s y mml q Symmetri c LQ method
Eigenvalues and Singular Values
bal ance I mprove accuracy of computed
ei genval ues
cdf 2r df Convert compl ex di agonal form
to real bl ock di agonal form
ei g Ei genval ues and ei genvectors
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 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 Matri x exponenti al
f unm Eval uate general matri x
functi on
l ogm Matri x l ogari thm
s qr t m Matri x square root
D ata A nalysis and Fourier Transform Functions
A-11
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
cumpr od Cumul ati ve product
cums um Cumul ati ve sum
cumt r apz Cumul ati ve trapezoi dal
numeri cal i ntegrati on
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 arrays
mi n Mi ni mum el ements of an array
per ms Al l possi bl e permutati ons
pol y ar ea Area of pol ygon
pr i mes Generate l i st of pri me numbers
pr od Product of array el ements
r ect i nt Rectangl e i ntersecti on area
s or t Sort el ements i n ascendi ng
order
s or t r ows Sort rows i n ascendi ng order
s t d Standard devi ati on
s um Sum of array el ements
t r apz Trapezoi dal numeri cal
i ntegrati on
v ar Vari ance
Finite Differences
del 2 Di screte Lapl aci an
di f f Di fferences and approxi mate
deri vati ves
gr adi ent Numeri cal gradi ent
Correlation
cor r coef Correl ati on coefci ents
cov Covari ance matri 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 nni te
i mpul se response (I I R) or ni te
i mpul se response (FI R) l ter
f i l t er 2 Two-di mensi onal di gi tal
l teri ng
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 Fouri er
transform
Basic Operations (Continued)
A M A TLA B Q uick Reference
A-12
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
perform i nterpol ati on i n one, two, three, and
hi gher di mensi ons.
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 transform to center of
spectrum
i f f t I nverse one-di mensi onal fast
Fouri er transform
i f f t 2 I nverse two-di mensi onal fast
Fouri er transform
i f f t n I nverse mul ti di mensi onal fast
Fouri er transform
i f f t s hi f t I nverse FFT shi ft
nex t pow2 Next power of two
unwr ap Correct phase angl es
Vector Functions
cr os s Vector cross product
i nt er s ect Set i ntersecti on of two vectors
i s member 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
Fourier Transforms (Continued)
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 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 tti ng
pol y i nt Anal yti c pol ynomi al i ntegrati on
pol y v al Pol ynomi al eval uati on
pol y v al m Matri x pol ynomi al eval uati on
r es i due Convert between parti al fracti on
expansi on and pol ynomi al
coefci ents
r oot s Pol ynomi al roots
Data Interpolation
conv hul l Convex hul l
conv hul l n Mul ti di mensi onal convex hul l
del aunay Del aunay tri angul ati on
del aunay 3 3-D Del aunay tessel l ati on
del aunay n Mul ti di mensi onal Del aunay
tessel l ati on
ds ear ch Search for nearest poi nt
ds ear chn Mul ti di mensi onal cl osest poi nt
search
gr i ddat a Data gri ddi ng
gr i ddat a3 Data gri ddi ng and hypersurface
tti ng for three-di mensi onal
data
gr i ddat an Data gri ddi ng and hypersurface
tti ng (di mensi on >= 2)
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)
Function Functions - N onlinear N um erical M ethods
A-13
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.
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
pchi p Pi ecewi se Cubi c Hermi te
I nterpol ati ng Pol ynomi al
(PCHI P)
ppv al Pi ecewi se pol ynomi al eval uati on
s pl i ne Cubi c spl i ne i nterpol ati on
t s ear ch Search for encl osi ng Del aunay
tri angl e
t s ear chn Mul ti di mensi onal cl osest
si mpl ex search
v or onoi Voronoi di agram
v or onoi n Mul ti di mensi onal Voronoi
di agrams
Function Functions - Nonlinear Numerical
Methods
bv p4c Sol ve two-poi nt boundary val ue
probl ems (BVPs) for ordi nary
di fferenti al equati ons (ODEs)
bv pget Extract parameters from BVP
opti ons structure
bv pi ni t Form the i ni ti al guess for bv p4c
Data Interpolation (Continued)
bv ps et Create/al ter BVP opti ons
structure
bv pv al Eval uate the sol uti on computed
by bv p4c
dbl quad Numeri cal eval uati on of doubl e
i ntegral s
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 Fi nd zero of a functi on of one
vari abl e
ode45, ode23,
ode113, ode15s ,
ode23s , ode23t ,
ode23t b
Solve initial value problems for
ODEs
odeget Extract parameters from ODE
opti ons structure
odes et Create/al ter ODE opti ons
structure
opt i mget Get opti mi zati on opti ons
structure parameter val ues
opt i ms et Create or edi t opti mi zati on
opti ons parameter structure
pdepe Sol ve i ni ti al -boundary val ue
probl ems
pdev al Eval uate the sol uti on computed
by pdepe
quad Numerical evaluation of integrals,
adaptive Simpson quadrature
quadl Numeri cal eval uati on of
i ntegral s, adapti ve Lobatto
quadrature
v ect or i z e Vectori ze expressi on
Function Functions - Nonlinear Numerical
Methods (Continued)
A M A TLA B Q uick Reference
A-14
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.
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 forml 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 random
matri x
Full to Sparse Conversion
f i nd Fi nd i ndi ces and val ues of
nonzero el ements
f ul l Convert sparse matri x to ful l
matri x
s par s e Create sparse matri x
s pconv er t I mport matri x from sparse
matri x external format
Working with Nonzero Entries of Sparse
Matrices
nnz Number of nonzero matri x
el ements
nonz er os Nonzero matri x el ements
nz max Amount of storage al l ocated for
nonzero matri x el ements
s pal l oc Al l ocate space for sparse matri x
s pf un Appl y functi on to nonzero sparse
matri x el ements
s pones Repl ace nonzero sparse matri x
el ements wi th ones
Visualizing Sparse Matrices
s py Vi sual i ze sparsi ty pattern
Reordering Algorithms
col amd Col umn approxi mate mi ni mum
degree permutati on
col mmd Sparse col umn mi ni mum degree
permutati on
col per m Sparse col umn permutati on
based on nonzero count
dmper m Dul mage-Mendel sohn
decomposi ti on
r andper m Random permutati on
s y mamd Symmetri c approxi mate
mi ni mum degree permutati on
s y mmmd Sparse symmetri c mi ni mum
degree orderi ng
s y mr cm Sparse reverse Cuthi l l -McKee
orderi ng
Norm, Condition Number, and Rank
condes t 1-norm matri 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 nni 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 restarts)
Sound Processing Functions
A-15
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.
l s qr LSQR i mpl ementati on of
Conjugate Gradi ents on the
normal equati ons
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
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
Sparse Systems of Linear Equations (Continued)
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-Specic Sound Functions
aur ead Read NeXT/SUN (. au) sound l e
auwr i t e Wri te NeXT/SUN (. au) sound
l e
.WAV Sound Functions
wav pl ay Pl ay recorded sound on a
PC-based audi o output devi ce
wav r ead Read Mi crosoft WAVE (. wav )
sound l e
wav r ecor d Record sound usi ng a PC-based
audi o i nput devi ce
wav wr i t e Wri te Mi crosoft WAVE (. wav )
sound l e
A M A TLA B Q uick Reference
A-16
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.
General
abs Absol ute val ue and compl ex
magni tude
ev al I nterpret stri 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 to Function Handle Conversion
f unc2s t r Constructs a functi on name
stri ng from a functi on handl e
s t r 2f unc Constructs a functi on handl e
from a functi on name stri ng
String Manipulation
debl ank Stri p trai l i ng bl anks from the
end of a stri ng
f i nds t r Fi nd one stri 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 character array
s t r mat ch Fi nd possi bl e matches for a
stri ng
s t r ncmp Compare the rst n characters
of two stri ngs
s t r ncmpi Compare the rst n characters
of stri ngs, i gnori ng case
s t r r ep Stri ng search and repl ace
s t r t ok Fi rst token i n stri 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 abel Produce the TeX format from a
character stri ng
upper Convert stri ng to upper case
String to Number Conversion
char Create character array (stri ng)
i nt 2s t r I nteger to stri ng conversi on
mat 2s t r Convert a matri x i nto a stri ng
num2s t r Number to stri ng conversi on
s pr i nt f Wri te formatted data to a stri ng
s s canf Read stri ng under format
control
s t r 2doubl e Convert stri ng to
doubl e-preci si on val ue
s t r 2mat Stri ng to matri x conversi on
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 Hexadeci mal to deci mal number
conversi on
hex 2num Hexadeci mal to doubl e number
conversi on
String Manipulation (Continued)
File I/ O Functions
A-17
File I/ O Functions
The 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.
Bitwise Functions
These functi ons l et you operate at the bi t l evel
such as shi fti ng and compl ementi ng.
File Opening and Closing
f cl os e Cl ose one or more open l es
f open Open a l e or obtai n i nformati on
about open l es
Unformatted I/ O
f r ead Read bi nary data from l e
f wr i t e Wri te bi nary data to a l e
Formatted I/ O
f get l Return the next l i ne of a 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 l e as a
stri ng wi th l i ne termi nator(s)
f pr i nt f Wri te formatted data to l e
f s canf Read formatted data from l e
File Positioning
f eof Test for end-of-l e
f er r or Query MATLAB about errors i n
l e i nput or output
f r ewi nd Rewi nd an open l e
f s eek Set l e posi ti on i ndi cator
f t el l Get l e posi ti on i ndi cator
String Conversion
s pr i nt f Wri te formatted data to a stri ng
s s canf Read stri ng under format
control
Specialized File I/ O
dl mr ead Read an ASCI I del i mi ted l e
i nto a matri x
dl mwr i t e Wri te a matri x to an ASCI I
del i mi ted l e
hdf HDF i nterface
i mf i nf o Return i nformati on about a
graphi cs l e
i mr ead Read i mage from graphi cs l e
i mwr i t e Wri te an i mage to a graphi cs l e
s t r r ead Read formatted data from a
stri ng
t ex t r ead Read formatted data from text
l e
wk1r ead Read a Lotus123 WK1
spreadsheet l e i nto a matri x
wk1wr i t e Wri te a matri x to a Lotus123
WK1 spreadsheet 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 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
A M A TLA B Q uick Reference
A-18
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.
MATLAB 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.
MATLAB Interface to J ava Functions
These functi ons al l ow you to bri ng Java cl asses
i nto MATLAB, construct objects, and cal l and save
methods.
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 el d of structure array
r mf i el d Remove structure el ds
s et f i el d Set el d of structure array
s t r uct Create structure array
s t r uct 2cel l Structure to cel l array
conversi on
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
met hods Di spl ay method names
met hods v i ew Di spl ays i nformati on on al l
methods i mpl emented by a cl ass
s ubs as gn Overl oaded method for A(I )=B,
A{I }=B, and A.el d=B
s ubs i ndex Overl oaded method for X(A)
s ubs r ef Overl oaded method for A(I ), A{I }
and A.el d
Interface to J ava Functions
cl as s Create object or return cl ass of
object
i mpor t
Add a package or class to the
current Java import list
i s a Detect an object of a gi ven cl ass
i s j av a Test whether an object i s a Java
object
j av aAr r ay Constructs a Java array
j av aMet hod I nvokes a Java method
j av aObj ect Constructs a Java object
met hods Di spl ay method names
met hods v i ew Di spl ay i nformati on on al l
methods i mpl e.mented by a
cl ass
C ell A rray Functions
A-19
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.
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.
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 stri ngs from
character array
cel l 2s t r uct Cel l array to structure array
conversi on
cel l di s p Di spl ay cel l array 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 array
Multidimensional Array Functions
cat Concatenate arrays
f l i pdi m Fl i p array al ong a speci ed
di mensi on
i nd2s ub Subscri pts from l i near i ndex
i per mut e I nverse permute 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 array di mensi ons
per mut e Rearrange the di mensi ons of a
mul ti di mensi onal array
r es hape Reshape array
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 subscri pts
Basic Plots and Graphs
bar Verti cal bar chart
bar h Hori zontal bar chart
hi s t Pl ot hi stograms
hi s t c Hi stogram count
hol d Hol d current graph
l ogl og Pl ot usi ng l og-l og scal es
pi e Pi e pl ot
pl ot Pl ot vectors or matri ces.
pol ar Pol ar coordi nate pl ot
s emi l ogx Semi -l og scal e pl ot
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 3-D comet pl ot
cy l i nder Generate cyl i nder
Multidimensional Array Functions (Continued)
A M A TLA B Q uick Reference
A-20
f i l l 3 Draw 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 Three-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 ck Date formatted 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 graphs 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
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/contourpl ot
Three-Dimensional Plotting (Continued)
mes h 3-D mesh wi th reference pl ane
peaks A sampl e functi on of two
vari abl es
s ur f 3-D shaded surface 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 surface 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 el d
cont our s l i ce Draw contours i n vol ume sl i ce
pl ane
cur l Compute the curl and angul ar
vel oci ty of a vector el d
di v er gence Compute the di vergence of a
vector el d
f l ow Generate scal ar vol ume data
i nt er ps t r eams peed I nterpol ate streaml i ne verti ces
from vector-el d magni tudes
i s ocaps Compute i sosurface end-cap
geometry
i s ocol or s Compute the col ors of i sosurface
verti ces
i s onor mal s Compute normal s of i sosurface
verti ces
i s os ur f ace Extract 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 l i ce Draw sl i ce pl anes i n vol ume
s moot h3 Smooth 3-D data
Surface, Mesh, and Contour Plots (Continued)
D ata Visualization
A-21
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 t r eampar t i cl es Draw stream parti cl es from
vector vol ume data
s t r eamr i bbon Draw stream ri bbons from
vector vol ume data
s t r eams l i ce Draw wel l -spaced stream l i nes
from vector vol ume data
s t r eamt ube Draw stream tubes from vector
vol ume data
s ur f 2pat ch Convert surface 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 surface
tti ng
mes hgr i d Generati on of X and Y arrays 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 tri 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 error bars
ez cont our Easy to use contour pl otter
ez cont our f Easy to use l l ed contour pl otter
ez mes h Easy to use 3-D mesh pl otter
Volume Visualization (Continued)
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
ez s ur f c Easy to use combi nati on surface/
contour pl otter
f eat her Feather pl ot
f i l l Draw 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 (checkerboard) pl ot
pi e3 3-D pi e pl ot
pl ot mat r i x Scatter pl ot matri x
pol y ar ea 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 stogram
s cat t er Scatter pl ot
s cat t er 3 3-D scatter pl ot
s t ai r s Stai rstep 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
Specialized Plotting (Continued)
A M A TLA B Q uick Reference
A-22
View Control
camdol l y Move camera posi ti on and target
caml ookat Vi ew speci c objects
camor bi t Orbi t about camera target
campan Rotate camera target about
camera posi ti on
campos Set or get camera posi ti on
campr oj Set or get projecti on type
camr ol l Rotate camera about vi ewi ng
axi s
camt ar get Set or get camera target
camup Set or get camera up-vector
camv a Set or get camera vi ew angl e
camz oom Zoom camera i n or out
das pect Set or get data aspect rati o
pbas pect Set or get pl ot box aspect rati o
v i ew 3-D graph vi ewpoi nt
speci 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 Li ght object creati on functi on
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 reectance mode
Transparency
al pha Set or query transparency
properti es for objects i n current
axes
al phamap Speci fy the gure al phamap
al i m Set or query the axes al pha
l i mi ts
Color Operations
br i ght en 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 def Set up col or defaul ts
col or map Set the col or l ook-up tabl e (l i st of
col ormaps)
gr ay mon Graphi cs 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
s ur f nor m 3-D 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
D ata Visualization
A-23
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 ormap 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 green col or
map
Printing
or i ent Hardcopy paper ori entati on
pages et updl g Page posi ti on di al og box
pr i nt Pri nt graph or save graph to l e
pr i nt dl g Pri nt di al og box
pr i nt opt Congure l ocal pri nter defaul ts
s av eas Save gure to graphi c l e
Handle Graphics, General
al l chi l d Fi nd al l chi l dren of speci ed
objects
copy obj Make a copy of a graphi cs object
and i ts chi l dren
f i ndal l Fi nd al l graphi cs objects
(i ncl udi ng hi dden handl es)
f i ndobj Fi nd objects wi th speci ed
property val ues
gcbo Return object whose cal l back i s
currentl y executi ng
gco Return handl e of current object
Colormaps (Continued)
get Get object properti es
i s handl e True for graphi cs objects
r ot at e Rotate objects about speci ed
ori gi n and di recti on
s et Set object properti es
Working with Application Data
get appdat a Get val ue of appl i cati on data
i s appdat a True i f appl i cati on data exi sts
r mappdat a Remove appl i cati on data
s et appdat a Speci fy appl i cati on data
Handle Graphics, Object Creation
ax es Create axes object
f i gur e Create 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 (quadri l ateral s)
t ex t Create text object (character
stri ngs)
ui cont ex t menu Create context menu (pop-up
associ ated wi th object)
Handle Graphics, Figure Windows
capt ur e Screen capture of the current
gure
cl c Cl ear gure wi ndow
cl f Cl ear gure
cl os e Cl ose speci ed wi ndow
cl os er eq Defaul t cl ose request functi on
gcf Get current gure handl e
Handle Graphics, General (Continued)
A M A TLA B Q uick Reference
A-24
Graphical User Interfaces
The graphi cal user i nterface functi ons l et you bui l d
your own i nterfaces for your appl i cati ons.
newpl ot Graphi cs M-l e preambl e for
Nex t Pl ot property
r ef r es h Refresh gure
s av eas Save gure or model to desi red
output format
Handle Graphics, Axes
ax i s Pl ot axi s scal i ng and appearance
cl a Cl ear axes
gca Get current axes handl e
Object Manipulation
r es et Reset axi s or gure
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 ect Drag XOR rectangl es wi th
mouse
dr awnow Compl ete any pendi ng drawi ng
r bbox Rubberband box
Handle Graphics, Figure Windows (Continued)
Dialog Boxes
di al og Create a di al og box
er r or dl g Create error di al og box
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 l e for readi ng
ui put f i l e Di spl ay di al og box to retri eve
name of l e for wri 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 Deployment
gui dat a Store or retri eve appl i cati on
data
gui handl es Create a structure of handl es
mov egui Move GUI gure onscreen
openf i g Open or rai se GUI gure
User Interface Development
gui de Open the GUI Layout Edi tor
i ns pect Di spl ay Property I nspector
Serial Port I/ O
A-25
Serial Port I/ O
These functi ons provi des di rect access to
peri pheral devi ces that you connect to your
computers seri al port.
User Interface Objects
menu Generate a menu of choi ces for
user i nput
ui cont ex t menu Create context menu
ui cont r ol Create user i nterface control
ui menu Create user i nterface menu
Other Functions
dr agr ect Drag rectangl es wi th mouse
f i ndf i gs Di spl ay off-screen vi si bl e gure
wi ndows
gcbf Return handl e of gure
contai ni ng cal l back object
gcbo Return handl e of object whose
cal l back i s executi ng
r bbox Create rubberband 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 graphi cs objects
t ex t wr ap Return wrapped stri ng matri x
for gi ven ui control
ui r es ume Used wi th ui wai t , control s
program executi on
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 es s Wai t for key/buttonpress over
gure
Creating a Serial Port Object
s er i al Create a seri al port object
Writing and Reading Data
f get l Read one l i ne of text from the
devi ce and di scard the
termi nator
f get s Read one l i ne of text from the
devi ce and i ncl ude the
termi nator
f pr i nt f Wri te text to the devi ce
f r ead Read bi nary data from the
devi ce
f s canf Read data from the devi ce, and
format as text
f wr i t e Wri te bi nary data to the devi ce
r eadas y nc Read data asynchronousl y from
the devi ce
s t opas y nc Stop asynchronous and wri te
operati ons
Conguring and Returning Properties
get Return seri al port object
properti es
s et Congure or di spl ay seri al port
object properti es
State Change
f cl os e Di sconnect a seri al port object
from the devi ce
f open Connect a seri al port object to
the devi ce
r ecor d Record data and event
i nformati on to a l e
A M A TLA B Q uick Reference
A-26
General Purpose
cl ear Remove a seri al port object from
the MATLAB workspace
del et e Remove a seri al port object from
memory
di s p Di spl ay seri al port object
summary i nformati on
i ns t r act i on Di spl ay event i nformati on when
an event occurs
i ns t r f i nd Return seri al port objects from
memory to the MATLAB
workspace
i s v al i d Determi ne i f seri al port objects
are val i d
l engt h Length of seri al port object array
l oad Load seri al port objects and
vari abl es i nto the MATLAB
workspace
s av e Save seri al port objects and
vari abl es to a MAT-l e
s er i al br eak Send a break to the devi ce
connected to the seri al port
s i z e Si ze of seri al port object array
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
Special Functions . . . . . . . . . . . . . . . . . B-4
Variable Precision 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 Hel p and sel ect Reference from the Symbolic Math
Toolbox.
Note Al l of the functi ons l i sted i n Symbol i c Math Tool box Reference 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 ri ght di vi si on
\ Left di vi si on
. \ Array l eft di vi si on
^ Matri x or scal ar rai sed to a power
. ^ Array rai sed to a power
Compl ex conjugate transpose
. Real transpose
Basic Operations
ccode C code representati on of a
symbol i c expressi on
conj Compl ex conjugate
f i nds y m Determi ne symbol i c vari abl es
f or t r an Fortran representati on of a
symbol i c expressi on
i mag I magi nary part of a compl ex
number
l at ex LaTeX representati on of a
symbol i c expressi on
pr et t y Pretty pri nt a symbol i c expressi on
r eal Real part of an i magi nary number
s y m Create symbol i c object
s y ms Shortcut for creati 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 m object to stri 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 coefci ent
vector
Integral Transforms
f our i er Fouri er transform
i f our i er I nverse Fouri er transform
i l apl ace I nverse Lapl ace transform
i z t r ans I nverse z-transform
l apl ace Lapl ace transform
z t r ans z-transform
Linear Algebra
col s pace Basi s for col umn space
det Determi nant
di ag Create or extract di agonal s
ei g Ei genval ues and ei genvectors
ex pm Matri x exponenti al
i nv Matri x i nverse
j or dan Jordan canoni cal form
nul l Basi s for nul l space
pol y Characteri sti c pol ynomi al
r ank Matri 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 cont our Contour pl otter
ez cont our f Fi l l ed contour pl otter
ez mes h Mesh pl otter
ez mes hc Combi ned mesh and contour
pl otter
ez pl ot Functi on pl otter
ez pl ot Easy-to-use functi on pl otter
ez pl ot 3 3-D curve pl otter
ez pol ar Pol ar coordi nate pl otter
ez s ur f Surface pl otter
ez s ur f c Combi ned surface and contour
pl otter
f unt ool Functi on cal cul ator
r s ums Ri emann sums
t ay l or t ool Tayl or seri es cal cul ator
Simplication
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 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 gebrai c equati ons
Special Functions
cos i nt Cosi ne i ntegral , Ci(x)
l amber t w Sol uti on of
s i ni nt Si ne i ntegral , Si(x)
z et a Ri emann z et a functi on
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
x ( )e
x ( )
x =
I-1
Index
Symbols
: operator 4-7
@s y m di rectory 7-15
\ 7-65, 7-66
A
abstract functi ons 7-10
Ai ry di fferenti al equati on 7-93
Ai ry functi on 7-93
al gori thms
vectori zi ng 6-23
ani mati on 5-37
annotati ng pl ots 5-14
ans 4-4
Appl i cati on Program I nterface (API ) 1-13
Array Edi tor 3-13
array operators 4-22
arrays 4-18, 4-21
cel l 6-9
character 6-11
col umnwi se organi zati on 4-23
concatenati ng 4-16
creati ng i n M-fi l es 4-15
del eti ng rows and col umns 4-16
del eti ng rows or col umns 4-16
el ements 4-10
generati ng wi th functi ons and operators 4-14
l i sti ng contents 4-10
l oadi ng from external data fi l es 4-15
mul ti di mensi onal 6-7
notati on for el ements 4-10
preal l ocati ng 6-23
structure 6-14
vari abl e names 4-10
aspect rati o of axes 5-11
axes 5-10
axi s
l abel s 5-12
ti tl es 5-12
ax i s 5-10
B
backsl ash operator 7-65
Bessel functi ons
di fferenti ati ng 7-17
i ntegrati ng 7-24
bes s el j 7-17
bes s el k 7-93
bi t map 5-25
bi twi se functi ons A-17
bl ockset 1-14
bookmarki ng documentati on 3-10
books
MATLAB-rel ated 1-10
branch cut 7-42
br eak 6-6
C
cal cul us 7-167-42
cas e 6-4
cel l array functi ons A-19
cel l arrays 6-9
char 6-13
character arrays 6-11
character stri ng functi ons A-16
characteri sti c pol ynomi al 4-21, 7-69, 7-71
ci rcul ant matri x 7-10, 7-53
cl ear 7-26
cl eari ng vari abl es
i n the Mapl e workspace 7-27
Index
I-2
i n the MATLAB workspace 7-27
col l ect 7-44
col on operator 4-7
col ormap 5-20
col ors
l i nes for pl otti ng 5-4
col s pace 7-66
col umn space 7-66
Command Hi story 3-7
command l i ne edi ti ng 4-30
Command Wi ndow 3-6
commands
general purpose A-3A-4
comp. s of t - s y s . mat l ab 1-10
compl ex numbers, pl otti ng 5-6
compl ex symbol i c vari abl es 7-9
concatenati ng
arrays 4-16
stri ngs 6-13
concatenati on 4-16
confi guri ng addi ti onal products
on Li nux 2-13
on PC 2-5
confi guri ng the desktop 3-5
conj 7-9
constants
speci al 4-12
contents i n Hel p browser 3-10
cont i nue 6-5
conti nui ng statements on mul ti pl e l i nes 4-30
converti ng symbol i c matri ces to numeri c form
7-8
coordi nate system conversi on A-10
current di rectory 3-11
Current Di rectory browser 3-11
D
data anal ysi s and Fouri er transform functi ons
A-11A-12
data vi sual i zati on A-19A-24
debuggi ng commands A-6A-7
debuggi ng M-fi l es 3-14
deci mal symbol i c expressi ons 7-8
defaul t symbol i c vari abl e 7-11
defi ni te i ntegrati on 7-23
del eti ng array el ements 4-16
demonstrati on programs 6-27
demos
MATLAB 1-6
demos 6-27
demos, runni ng from the Launch Pad 3-8
desktop for MATLAB 3-4
desktop tool s 3-6
determi nant of matri x 4-19
devel opment envi ronment 3-2
di ag 4-5
di f f 7-16
di fferenti ati on 7-167-19
di gi t s 7-8
di sconti nui ti es 7-41
di spl ay pane i n Hel p browser 3-10
documentati on 3-8
documentati on path
setti ng on Li nux 2-12
doubl e 7-61
ds ol v e 7-90
E
edi ti ng command l i nes 4-30
edi ti ng pl ots
i nteracti vel y 5-15
Edi tor/Debugger 3-14
Index
I-3
ei g 7-68
ei genval ue 4-20
ei genval ue trajectori es 7-787-85
ei genval ues 7-687-74, 7-79
computi ng 7-68
ei genvector 4-20, 7-69
el ementary math functi ons A-8A-9
el ementary matri ces and matri x mani pul ati on
A-7A-8
el ements of arrays 4-10
enteri ng matri ces 4-3
envi ronment 3-2
eps 7-8
erase mode 5-37
ev al 6-22
executi ng MATLAB 3-3
exi ti ng MATLAB 3-3
ex pand 7-44
exporti ng data 3-15
expressi ons 4-10, 4-13
eval uati ng 6-22
external programs, runni ng from MATLAB 3-7
ez pl ot 7-30
F
f act or 7-45
favori tes i n Hel p browser 3-10
f i gur e 5-9
fi gure wi ndows 5-9
wi th mul ti pl e pl ots 5-9
fi l e I /O functi ons A-17
f i nd 4-26
fi ndi ng i n a page 3-10
fi ndi ng object handl es 5-33
f i nds y m 7-13
f l i pl r 4-5
fl oati ng-poi nt ari thmeti c 7-58
fl oati ng-poi nt numbers 4-11
fl oati ng-poi nt symbol i c expressi ons 7-7
fl ow control 6-2
f or 6-4
format
of output di spl ay 4-28
f or mat 4-28, 7-58
f unct i on 6-19
functi on functi ons 6-24
nonl i near numeri cal methods A-13
functi on handl es
defi ned 6-24
usi ng 6-25
functi on M-fi l e 6-17, 6-18
functi on of two vari abl es 5-18
functi ons 4-11, 6-18
bi twi se A-17
bui l t-i n 4-12
cel l array A-19
character stri ng A-16
coordi nate system conversi on A-10
data anal ysi s and Fouri er transform A-11
A-12
data vi sual i zati on A-19A-24
el ementary math A-8A-9
el ementary matri ces and matri x mani pul ati on
A-7A-8
fi l e I /O A-17
fi ndi ng speci fi c 1-7
functi on functi ons - nonl i near numeri cal
methods A-13
general purpose commands A-3A-4
graphi cal user i nterface control A-24A-25
Java i nterface A-18
l anguage constructs and debuggi ng A-5A-7
l ogi cal A-5
Index
I-4
matri x functi ons - numeri cal l i near al gebra
A-10A-11
mul ti di mensi onal array A-19
object A-18
operators and speci al characters A-5
pol ynomi al and i nterpol ati on A-12A-13
seri al port I /O A-25A-26
sound processi ng A-15
sparse matri x A-14A-15
speci al i zed math A-9
speci al i zed matri ces A-8
structure A-18
vari abl e number of arguments 6-19
G
Gi vens transformati on 7-63, 7-72
gl obal vari abl es 6-20
gol den rati o 7-6
graphi cal user i nterface 5-35
graphi cal user i nterface control A-24A-25
graphi cs
2-D 5-2
fi l es 5-26
handl e graphi cs 5-28
objects 5-28
pri nti ng 5-26
gri ds 5-12
gui de 5-35
H
Handl e Graphi cs 1-13, 5-28
fi ndi ng handl es 5-33
properti es 5-31
Hel p
accessi ng 1-8
accessi ng on Li nux 2-14
accessi ng on PC 2-6
hel p
sources of 1-8
vi a MathWorks Knowl edge Base 1-10
vi a MathWorks Web si te 1-10
vi a newsgroup 1-10
Hel p browser 3-8
hel p functi ons 3-10
Hel p Navi gator 3-10
hi erarchy of graphi cs objects 5-29
Hi l bert matri x 7-8, 7-64
hol d 5-7
hor ner 7-45
I
I EEE fl oati ng-poi nt ari thmeti c 7-59
i f 6-2
i mages 5-24
i magi nary number 4-10
I mport Wi zard 3-15
i mporti ng data 3-15
i ndex i n Hel p browser 3-10
i nstal l ati on procedure
sel ecti ng products 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-13
on PC 2-5
i nt 7-22
i ntegrati on 7-227-27
defi ni te 7-23
Index
I-5
wi th real constants 7-24
J
j acobi an 7-18
Jacobi an matri x 7-18
Java i nterface functi ons A-18
j or dan 7-74
Jordan canoni cal form 7-747-75
L
l anguage constructs A-5A-6
Launch Pad 3-8
l earni ng MATLAB 1-6
l egend 5-3
l egend, addi ng to pl ot 5-3
l i brary
mathemati cal functi on 1-13
l i cense agreement 2-10
l i ghti ng 5-21
l i mi t 7-20
l i mi ts 7-207-21
two-si ded 7-20
undefi ned 7-21
l i mi ts, axes 5-11
l i ne conti nuati on 4-30
l i ne styl es of pl ots 5-4
l i near al gebra 7-637-85
Li nux
setti ng onl i ne documentati on path 2-12
l oad 4-15
l oadi ng arrays 4-15
l ocal vari abl es 6-19
l og of functi ons used 3-7
l ogi cal functi ons A-5
l ogi cal vectors 4-25
M
machi ne epsi l on 7-8
Macl auri n seri es 7-28
magi c 4-8
magi c square 4-4
Mapl e 7-2
markers 5-5
MAT-fi l e 5-24
mathemati cal functi on l i brary 1-13
mathemati cal functi ons
l i sti ng advanced 4-12
l i sti ng el ementary 4-11
l i sti ng matri x 4-12
MathWorks Knowl edge Base 1-10
MathWorks Store
purchasi ng products from 1-10
MathWorks Web si te 1-10
MATLAB
Appl i cati on Program I nterface 1-13
books 1-10
cal l i ng C subrouti ne 2-3, 2-8
cal l i ng Fortran subrouti ne 2-3, 2-8
demos 1-6
hi story 1-12
how to l earn 1-6
i nstal l i ng on Li nux 2-8
i nstal l i ng on PC 2-4
l anguage 1-13
mathemati cal functi on l i brary 1-13
overvi ew 1-12
starti ng on Li nux 2-13
supported Li nux compi l ers 2-8
supported PC compi l ers 2-3
mat l ab command
setti ng up symbol i c l i nk 2-12
matri ces 4-18
creati ng 4-14
Index
I-6
enteri ng 4-3
matri x 4-2
anti di agonal 4-5
determi nant 4-19
mai n di agonal 4-5
si ngul ar 4-19
swappi ng col umns 4-8
symmetri c 4-18
transpose 4-4
matri x condi ti on number 7-66
matri x functi ons - numeri cal l i near al gebra A-10
A-11
matri x mul ti pl i cati on 4-18
mesh pl ot 5-18
mex command
setti ng up symbol i c l i nk 2-12
MEX-fi l e 2-3, 2-8
supported PC compi l ers 2-3
M-fi l e 1-12, 4-15, 6-17
creati ng 6-17, 7-15
for creati ng arrays 4-15
functi on 6-17, 6-18
scri pt 6-17
M-fi l e performance 3-15
M-fi l es 3-14
Mi crosoft Word and access to MATLAB 3-15
movi es 5-38
mul ti di mensi onal array functi ons A-19
mul ti di mensi onal arrays 6-7
mul ti pl e data sets, pl otti ng 5-3
mul ti pl e pl ots per fi gure 5-9
mul ti vari ate data, organi zi ng 4-23
N
newsgroup 1-10
newsgroup for MATLAB users 3-11
Notebook 3-15
nul l 7-66
nul l space 7-66
numbers 4-10
fl oati ng-poi nt 4-11
numeri c symbol i c expressi ons 7-7
O
object functi ons A-18
object properti es 5-31
objects
fi ndi ng handl es 5-33
graphi cs 5-28
onl i ne documentati on
speci fyi ng vi ewi ng opti ons on Li nux 2-12
onl i ne hel p, vi ewi ng 3-8
operator 4-11, A-5
col on 4-7
output
control l i ng format 4-28
suppressi ng 4-30
overl ayi ng pl ots 5-7
P
path 3-12
performance of M-fi l es 3-15
pl ot 5-2
pl ot edi ti ng mode
overvi ew 5-15
pl ots
edi ti ng 5-14
pl otti ng
addi ng l egend 5-3
addi ng pl ots 5-7
annotati ng 5-14
Index
I-7
basi c 5-2
compl ex data 5-6
compl ex numbers 5-6
contours 5-8
l i ne col ors 5-4
l i ne styl es 5-4
l i nes and markers 5-5
mesh and surface 5-18
mul ti pl e data sets 5-3
mul ti pl e pl ots 5-9
pol y 7-69
pol ynomi al and i nterpol ati on functi ons A-12
A-13
PostScri pt 5-26
preal l ocati on 6-23
preferences 3-5
pr et t y 7-28
pr i nt 5-26
pri nti ng
graphi cs 5-26
product
regi strati on 1-11
professi onal versi on
di fferences wi th Student Versi on 1-3
profi l er 3-15
Property Edi tor
i nterface 5-16
Q
qui tti ng MATLAB 3-3
R
rati onal ari thmeti c 7-59
rati onal symbol i c expressi ons 7-7
ReadMe fi l e 2-5
r eal property 7-9
real symbol i c vari abl es 7-9, 7-26
reference i nformati on, obtai ni ng 1-7
regi steri ng your software 1-11
revi si on control systems, i nterfaci ng to MATLAB
3-15
root di rectory
speci fyi ng 2-10
Rosser matri x 7-70, 7-71
runni ng functi ons 3-6
runni ng MATLAB 3-3
S
scal ar expansi on 4-24
sci enti fi c notati on 4-10
screens
i nstal l ati on data 2-12
root di rectory 2-10
scri pt M-fi l e 6-17
scri pts 6-17
search path 3-12
searchi ng documentati on 3-10
sel ecti ng products to be i nstal l ed on Li nux 2-11
semi col on to suppress output 4-30
seri al port I /O functi ons A-25A-26
shutti ng down MATLAB 3-3
s i mpl e 7-48
si mpl i fi cati ons 7-437-50
s i mpl i f y 7-47
Si mul i nk 1-14
si mul taneous di fferenti al equati ons
sol vi ng 7-92
si mul taneous l i near equati ons
sol vi ng systems of 7-65, 7-89
si ngul ar matri x 4-19
si ngul ar val ue decomposi ti on 7-767-78
Index
I-8
s ol v e 7-86
sol vi ng equati ons 7-867-93
al gebrai c 7-867-90
ordi nary di fferenti al 7-907-93
sound processi ng functi ons A-15
source control systems, i nterfaci ng to MATLAB
3-15
sparse matri x functi ons A-14A-15
speci al characters A-5
speci al constants 4-12
i nfi ni ty 4-12
not-a-number 4-12
speci al i zed graphs 5-8
speci al i zed math functi ons A-9
speci al i zed matri ces A-8
speci fyi ng vi ewi ng opti ons on Li nux 2-12
spheri cal coordi nates 7-18
starti ng MATLAB 3-3
s t ar t up. m fi l e 2-5, 2-13
Statefl ow 1-14
statements
conti nui ng on mul ti pl e l i nes 4-30
executi ng 6-22
stri ngs
concatenati ng 6-13
structure functi ons A-18
structures 6-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
Symbol i c Math Tool box di fferences 1-4
s ubex pr 7-51
subexpressi ons 7-517-53
s ubpl ot 5-9
s ubs 7-53
subscri pti ng
wi th l ogi cal vectors 4-25
subscri pts 4-6
substi tuti ons 7-517-57
s um 4-4
summati on
symbol i c 7-27
support
sources of 1-8
suppressi ng output 4-30
surface pl ot 5-18
s v d 7-76
s wi t ch 6-4
s y m 7-5, 7-6, 7-7, 7-8, 7-26
symbol i c expressi ons 7-86
creati ng 7-6
deci mal 7-8
fl oati ng-poi nt 7-7
numeri c 7-7
rati onal 7-7
symbol i c l i nks
setti ng up 2-12
symbol i c math functi ons
creati ng 7-14
Symbol i c Math Tool box
demo 7-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 transforms B-3
l i near al gebra 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
speci al functi ons B-4
Index
I-9
vari abl e preci si on ari thmeti c B-4
symbol i c matri x
computi ng ei genval ue of 7-71
converti ng to numeri c form 7-8
creati ng 7-10
di fferenti ati ng 7-17
symbol i c objects
about 7-5
symbol i c summati on 7-27
symbol i c vari abl es
compl ex 7-9
creati ng 7-6
defaul t 7-11
real 7-9, 7-26
symmetri c matri x 4-18
s y ms 7-7
s y ms um 7-27
system requi rements
Li nux 2-7
PC 2-2
T
t ay l or 7-28
Tayl or seri es 7-28
techni cal support 1-11
text 6-11
TI FF 5-27
ti tl e
fi gure 5-12
tool box 1-12
tool boxes
i nstal l i ng on Li nux 2-13
i nstal l i ng on PC 2-5
tool s i n the desktop 3-6
t r ans pos e 4-4
troubl eshooti ng 1-8
U
unr eal property 7-9
user i nterface 5-35
bui l di ng 5-35
V
vari abl e-preci si on ari thmeti c 7-587-62
vari abl e-preci si on numbers 7-60
vari abl es 4-10
gl obal 6-20
l ocal 6-19
vector 4-2
l ogi cal 4-25
preal l ocati ng 6-23
vectori zati on 6-23
versi on control systems, i nterfaci ng to MATLAB
3-15
vi ewi ng documentati on 3-10
vi si bi l i ty of axes 5-11
v pa 7-60
W
Web si te 1-10
whi l e 6-5
wi ndows for pl otti ng 5-9
wi ndows i n MATLAB 3-4
wi reframe 5-18
surface 5-18
Word and access to MATLAB 3-15
word processi ng access to MATLAB 3-15
workspace 3-12
Workspace browser 3-12
www. mat hwor ks . com 1-10
www. mat hwor ks . com/educat i on 1-10
www. mat hwor ks . com/s t or e 1-10, 2-5, 2-13
Index
I-10
www. mat hwor ks . com/s uppor t 1-8, 1-11
www. mat hwor ks . com/s uppor t /books 1-10
X
xor erase mode 5-37

You might also like