On the Third Edition
The Third Edition of the MATLAB Primer is based on version 4.0/4.1 of MATLAB.
While this edition re ects an extensive general revision of the Second Edition, most sig-
ni cant is the new information to help one b egin to use the ma jor new features of version
4.0/4.1, the sparse matrix and enhanced graphics capabilities.
The plain T X source and corresp onding PostScript le of the latest printing of the
E
MATLAB Primer are always available via anonymous ftp from:
Address: math.ufl.edu Directory: pub/matlab Files: primer.tex, primer.ps
MATLAB Primer
You are advised to download anew each term the latest printing of the Primer since minor
Third Edition
improvements and corrections mayhave b een made in the interim. If ftp is unavailable
to you, the Primer can b e obtained via listserv by sending an email message to list-
[email protected] containing the single line send matlab/primer.tex.
Also available at this ftp site are b oth English primer35.tex, primer35.ps and
Kermit Sigmon
Spanish primer35sp.tex, primer35sp.psversions of the Second Edition of the Primer,
Department of Mathematics
whichwas based on version 3.5 of MATLAB. The Spanish translation is by Celestino
University of Florida
Montes, University of Seville, Spain. A Spanish translation of the Third Edition is under
development.
Users of the Primer usually appreciate the convenience and durability of a b ound copy
with a cover, copy center style.
12-93
c
Copyright 1989, 1992, 1993 by Kermit Sigmon
The MATLAB Primer may b e distributed as desired sub ject to the following con-
ditions:
1. It may not b e altered in anyway, except p ossibly adding an addendum giving
information ab out the lo cal computer installation or MATLAB to olb oxes.
2. It, or any part thereof, may not b e used as part of a do cument distributed for
a commercial purp ose.
In particular, it may b e distributed via a lo cal copy center or b o okstore.
Department of Mathematics University of Florida Gainesville, FL 32611
Department of Mathematics University of Florida Gainesville, FL 32611
[email protected] l.edu
[email protected] l.edu
c
Copyright 1989, 1992, 1993 by Kermit Sigmon
i
Introduction Contents
MATLAB is an interactive, matrix-based system for scienti c and engineering numeric
Page
computation and visualization. You can solve complex numerical problems in a fraction of
1. Accessing MATLAB :: ::::::: :::::::: ::::::: ::::::: ::::::: ::::::: :::::::: ::::::: : 1
the time required with a programming language suchasFortran or C. The name MATLAB
is derived from MATrix LABoratory.
2. Entering matrices :: ::::::: ::::::: ::::::: ::::::: :::::::: ::::::: ::::::: ::::::: ::::: 1
The purp ose of this Primer is to help you b egin to use MATLAB. It is not intended
3. Matrix op erations, array op erations : :::::::: ::::::: ::::::: ::::::: ::::::: :::::::: : 2
to b e a substitute for the User's Guide and Reference Guide for MATLAB. The Primer
can b est b e used hands-on. You are encouraged to work at the computer as you read the
4. Statements, expressions, variables; saving a session ::: ::::::: ::::::: :::::::: :::::: 3
Primer and freely exp eriment with examples. This Primer, along with the on-line help
facility, usually suce for students in a class requiring use of MATLAB.
5. Matrix building functions :: ::::::: :::::::: ::::::: ::::::: ::::::: ::::::: :::::::: ::: 4
6. For, while, if | and relations : ::::::: ::::::: ::::::: :::::::: ::::::: ::::::: ::::::: : 4
You should lib erally use the on-line help facility for more detailed information. When
using MATLAB, the command help functionname will give information ab out a sp eci c
7. Scalar functions : ::::::: ::::::: ::::::: ::::::: :::::::: ::::::: ::::::: ::::::: ::::::: 7
function. For example, the command help eig will give information ab out the eigenvalue
function eig. By itself, the command help will display a list of topics for which on-line
8. Vector functions ::::::: ::::::: ::::::: ::::::: :::::::: ::::::: ::::::: ::::::: ::::::: : 7
help is available; then help topic will list those sp eci c functions under this topic for which
help is available. The list of functions in the last section of this Primer also gives most of
9. Matrix functions :::::: ::::::: ::::::: ::::::: ::::::: :::::::: ::::::: ::::::: ::::::: :: 7
this information. You can preview some of the features of MATLAB by rst entering the
command demo and then selecting from the options o ered.
10. Command line editing and recall ::: :::::::: ::::::: ::::::: ::::::: ::::::: :::::::: :: 8
The scop e and p ower of MATLAB go far b eyond these notes. Eventually you will
11. Submatrices and colon notation ::: :::::::: ::::::: ::::::: ::::::: ::::::: :::::::: ::: 8
want to consult the MATLAB User's Guide and Reference Guide. Copies of the complete
do cumentation are often available for review at lo cations such as consulting desks, terminal
12. M- les: script les, function les :: ::::::: :::::::: ::::::: ::::::: ::::::: ::::::: :::: 9
ro oms, computing labs, and the reserve desk of the library. Consult your instructor or your
lo cal computing center to learn where this do cumentation is lo cated at your institution.
13. Text strings, error messages, input ::: ::::::: :::::::: ::::::: ::::::: ::::::: ::::::: 12
14. Managing M- les ::::: ::::::: ::::::: ::::::: ::::::: :::::::: ::::::: ::::::: ::::::: : 13
MATLAB is available for a number of environments: Sun/Ap ollo/VAXstation/HP
workstations, VAX, MicroVAX, Gould, PC and AT compatibles, 80386 and 80486 com-
15. Comparing eciency of algorithms: ops, tic, to c ::: ::::::: ::::::: ::::::: ::::::: 14
puters, Apple Macintosh, and several parallel machines. There is a relatively inexp ensive
Student Edition available from Prentice Hall publishers. The information in these notes
16. Output format :::::: ::::::: ::::::: :::::::: ::::::: ::::::: ::::::: ::::::: :::::::: : 14
applies generally to all of these environments.
17. Hard copy :::: :::::::: ::::::: ::::::: ::::::: ::::::: :::::::: ::::::: ::::::: ::::::: : 15
MATLAB is licensed by The MathWorks, Inc., 24 Prime Park Way, Natick, MA 01760,
508653-1415, Fax: 508653-2997, Email: [email protected].
18. Graphics :: ::::::: ::::::: :::::::: ::::::: ::::::: ::::::: ::::::: :::::::: ::::::: :::: 15
planar plots 15, hardcopy 17, 3-D line plots 18
mesh and surface plots 18, Handle Graphics 20
19. Sparse matrix computations :: ::::::: ::::::: :::::::: ::::::: ::::::: ::::::: ::::::: 20
20. Reference :::: ::::::: ::::::: :::::::: ::::::: ::::::: ::::::: ::::::: :::::::: ::::::: : 22
c
Copyright 1989, 1992, 1993 by Kermit Sigmon
iii ii
1. Accessing MATLAB. Listing entries of a large matrix is b est done in an ASCI I le with your lo cal editor,
where errors can b e easily corrected see sections 12 and 14. The le should consist of a
On most systems, after logging in one can enter MATLAB with the system command
rectangular array of just the numeric matrix entries. If this le is named, say, data.ext
matlab and exit MATLAB with the MATLAB command quit or exit. However, your
where .ext is any extension, the MATLAB command load data.ext will read this le
lo cal installation may p ermit MATLAB to b e accessed from a menuorby clicking an icon.
to the variable data in your MATLAB workspace. This may also b e done with a script le
On systems p ermitting multiple pro cesses, such as a Unix system or MS Windows,
see section 12.
you will nd it convenient, for reasons discussed in section 14, to keep b oth MATLAB
The built-in functions rand, magic, and hilb, for example, provide an easy wayto
and your lo cal editor active. If you are working on a platform which runs pro cesses in
create matrices with which to exp eriment. The command randn will create an n n
multiple windows, you will wanttokeep MATLAB active in one window and your lo cal
matrix with randomly generated entries distributed uniformly b etween 0 and 1, while
editor active in another.
randm,n will create an m n one. magicn will create an integral n n matrix which
You should consult your instructor or your lo cal computer center for details of the lo cal
is a magic square rows, columns, and diagonals have common sum; hilbn will create
installation.
the n n Hilb ert matrix, the king of ill-conditioned matrices m and n denote, of course,
p ositiveintegers. Matrices can also b e generated with a for-lo op see section 6 b elow.
2. Entering matrices.
Individual matrix and vector entries can b e referenced with indices inside parentheses
MATLAB works with essentially only one kind of ob ject|a rectangular numerical
in the usual manner. For example, A2; 3 denotes the entry in the second row, third
matrix with p ossibly complex entries; all variables represent matrices. In some situations,
column of matrix A and x3 denotes the third co ordinate of vector x.Try it. A matrix
1-by-1 matrices are interpreted as scalars and matrices with only one row or one column
or a vector will only accept positive integers as indices.
are interpreted as vectors.
Matrices can b e intro duced into MATLAB in several di erentways:
3. Matrix op erations, array op erations.
The following matrix op erations are available in MATLAB:
Entered by an explicit list of elements,
Generated by built-in statements and functions,
+ addition
Created in a disk le with your lo cal editor,