Matlab - Primer.pdf

(283 KB) Pobierz
498097825 UNPDF
MATLAB Primer
Third Edition
Kermit Sigmon
Department of Mathematics
University of Florida
Department of Mathematics University of Florida Gainesville, FL 32611
sigmon@math.ufl.edu
Copyright c
1989, 1992, 1993 by Kermit Sigmon
On the Third Edition
The Third Edition of the MATLAB Primer is based on version 4.0/4.1 of MATLAB.
While this edition reflects an extensive general revision of the Second Edition, most sig-
nicant is the new information to help one begin to use the major new features of version
4.0/4.1, the sparse matrix and enhanced graphics capabilities.
The plain T E X source and corresponding PostScript le of the latest printing of the
MATLAB Primer are always available via anonymous ftp from:
Address: math.ufl.edu Directory: pub/matlab Files: primer.tex, primer.ps
You are advised to download anew each term the latest printing of the Primer since minor
improvements and corrections may have been made in the interim. If ftp is unavailable
to you, the Primer can be obtained via listserv by sending an email message to list-
serv@math.ufl.edu containing the single line send matlab/primer.tex.
Also available at this ftp site are both English (primer35.tex, primer35.ps)and
Spanish (primer35sp.tex, primer35sp.ps) versions of the Second Edition of the Primer,
which was based on version 3.5 of MATLAB. The Spanish translation is by Celestino
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 bound copy
with a cover, copy center style.
(12-93)
1989, 1992, 1993 by Kermit Sigmon
The MATLAB Primer may be distributed as desired subject to the following con-
ditions:
1. It may not be altered in any way, except possibly adding an addendum giving
information about the local computer installation or MATLAB toolboxes.
2. It, or any part thereof, may not be used as part of a document distributed for
a commercial purpose.
In particular, it may be distributed via a local copy center or bookstore.
Department of Mathematics
University of Florida
Gainesville, FL 32611
sigmon@math.ufl.edu
i
Copyright c
Introduction
MATLAB is an interactive, matrix-based system for scientic and engineering numeric
computation and visualization. You can solve complex numerical problems in a fraction of
the time required with a programming language such as Fortran or C. The name MATLAB
is derived from MATrix LABoratory.
The purpose of this Primer is to help you begin to use MATLAB. It is not intended
to be a substitute for the User's Guide and Reference Guide for MATLAB. The Primer
can best be used hands-on. You are encouraged to work at the computer as you read the
Primer and freely experiment with examples. This Primer, along with the on-line help
facility, usually suce for students in a class requiring use of MATLAB.
You should liberally use the on-line help facility for more detailed information. When
using MATLAB, the command help functionname will give information about a specic
function. For example, the command help eig will give information about the eigenvalue
function eig. By itself, the command help will display a list of topics for which on-line
help is available; then help topic will list those specic functions under this topic for which
help is available. The list of functions in the last section of this Primer also gives most of
this information. You can preview some of the features of MATLAB by rst entering the
command demo and then selecting from the options oered.
The scope and power of MATLAB go far beyond these notes. Eventually you will
want to consult the MATLAB User's Guide and Reference Guide. Copies of the complete
documentation are often available for review at locations such as consulting desks, terminal
rooms, computing labs, and the reserve desk of the library. Consult your instructor or your
local computing center to learn where this documentation is located at your institution.
MATLAB is available for a number of environments: Sun/Apollo/VAXstation/HP
workstations, VAX, MicroVAX, Gould, PC and AT compatibles, 80386 and 80486 com-
puters, Apple Macintosh, and several parallel machines. There is a relatively inexpensive
Student Edition available from Prentice Hall publishers. The information in these notes
applies generally to all of these environments.
MATLAB is licensed by The MathWorks, Inc., 24 Prime Park Way, Natick, MA 01760,
(508)653-1415, Fax: (508)653-2997, Email: info@mathworks.com.
Copyright c
1989, 1992, 1993 by Kermit Sigmon
ii
Contents
Page
1. Accessing MATLAB ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1
2. Entering matrices :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 1
3. Matrix operations, array operations :::::::::::::::::::::::::::::::::::::::::::::: 2
4. Statements, expressions, variables; saving a session ::::::::::::::::::::::::::::::: 3
5. Matrix building functions :::::::::::::::::::::::::::::::::::::::::::::::::::::::: 4
6. For, while, if | and relations :::::::::::::::::::::::::::::::::::::::::::::::::::: 4
7. Scalar functions ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 7
8. Vector functions ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 7
9. Matrix functions ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 7
10. Command line editing and recall ::::::::::::::::::::::::::::::::::::::::::::::::: 8
11. Submatrices and colon notation :::::::::::::::::::::::::::::::::::::::::::::::::: 8
12. M-les: script les, function les ::::::::::::::::::::::::::::::::::::::::::::::::: 9
13. Text strings, error messages, input :::::::::::::::::::::::::::::::::::::::::::::: 12
14. Managing M-les ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 13
15. Comparing eciency of algorithms: flops, tic, toc ::::::::::::::::::::::::::::::: 14
16. Output format ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 14
17. Hard copy :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 15
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
iii
1. Accessing MATLAB.
On most systems, after logging in one can enter MATLAB with the system command
matlab and exit MATLAB with the MATLAB command quit or exit. However, your
local installation may permit MATLAB to be accessed from a menu or by clicking an icon.
On systems permitting multiple processes, such as a Unix system or MS Windows,
you will nd it convenient, for reasons discussed in section 14, to keep both MATLAB
and your local editor active. If you are working on a platform which runs processes in
multiple windows, you will want to keep MATLAB active in one window and your local
editor active in another.
You should consult your instructor or your local computer center for details of the local
installation.
2. Entering matrices.
MATLAB works with essentially only one kind of object|a rectangular numerical
matrix with possibly complex entries; all variables represent matrices. In some situations,
1-by-1 matrices are interpreted as scalars and matrices with only one row or one column
are interpreted as vectors.
Matrices can be introduced into MATLAB in several dierent ways:
Entered by an explicit list of elements,
Generated by built-in statements and functions,
Created in a diskle with your local editor,
Loaded from external data les or applications (see the User's Guide).
For example, either of the statements
A=[123;456;789]
and
A=[
123
456
789]
creates the obvious 3-by-3 matrix and assigns it to a variable A . Try it. The elements
within a row of a matrix may be separated by commas as well as a blank. When listing a
number in exponential form (e.g. 2.34e-9), blank spaces must be avoided.
MATLAB allows complex numbers in all its operations and functions. Two convenient
ways to enter complex matrices are:
A = [1 2;3 4] + i*[5 6;7 8]
A = [1+5i 2+6i;3+7i 4+8i]
When listing complex numbers (e.g. 2+6i) in a matrix, blank spaces must be avoided.
Either i or j may be used as the imaginary unit. If, however, you use i and j as vari-
ables and overwrite their values, you may generate a new imaginary unit with, say,
ii = sqrt(-1).
1
Zgłoś jeśli naruszono regulamin