STUDENT VERSION
Learning
马
英
How to Contact The MathWorks:
www.mathworks.com Web
comp.soft-sys.matlab Newsgroup
suggest@mathworks.com Product enhancement suggestions
[email protected]m Bug reports
[email protected] Documentation error reports
ISBN 0-9755787-090000
Learning MATLAB
COPYRIGHT 1984 - 2004 by The MathWorks, Inc.
The software described in this document is furnished under a license agreement. The software may be used
or copied only under the terms of the license agreement. No part of this manual may be photocopied or repro-
duced in any form without prior written consent from The MathWorks, Inc.
FEDERAL ACQUISITION: This provision applies to all acquisitions of the Program and Documentation by,
for, or through the federal government of the United States. By accepting delivery of the Program or
Documentation, the government hereby agrees that this software or documentation qualifies as commercial
computer software or commercial computer software documentation as such terms are used or defined in
FAR 12.212, DFARS Part 227.72, and DFARS 252.227-7014. Accordingly, the terms and conditions of this
Agreement and only those rights specified in this Agreement, shall pertain to and govern the use,
modification, reproduction, release, performance, display, and disclosure of the Program and Documentation
by the federal government (or other entity acquiring for or through the federal government) and shall
supersede any conflicting contractual terms or conditions. If this License fails to meet the government's
needs or is inconsistent in any respect with federal procurement law, the government agrees to return the
Program and Documentation, unused, to The MathWorks, Inc.
MATLAB, Simulink, Stateflow, Handle Graphics, and Real-Time Workshop are registered
trademarks, and TargetBox is a trademark of The MathWorks, Inc.
Other product or brand names are trademarks or registered trademarks of their respective holders.
Printing History: August 1999 First printing New manual
January 2001 Second printing Revised for MATLAB 6.0 (Release 12)
November 2002 Third printing Revised for MATLAB 6.5 (Release 13)
July 2004 Fourth printing Revised for MATLAB 7.0 (Release 14)
i
Contents
1
Introducing the
MATLAB & Simulink Student Version
Quick Start . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2
About the Student Version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-3
Student Use Policy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-3
Obtaining Additional MathWorks Products . . . . . . . . . . . . . 1-5
Getting Started with MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . 1-6
Finding Reference Information . . . . . . . . . . . . . . . . . . . . . . . . 1-7
Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-8
Other Resources . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-9
Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-9
MathWorks Web Site . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-9
MathWorks Academia Web Site . . . . . . . . . . . . . . . . . . . . . . . . . 1-9
MATLAB and Simulink Based Books . . . . . . . . . . . . . . . . . . . . 1-9
MathWorks Store . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-9
MATLAB Central — File Exchange/Newsgroup Access . . . . . 1-10
Technical Support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-10
Product Registration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-10
Differences Between the Student and
Professional Versions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1-11
MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-11
Simulink . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-11
Symbolic Math Toolbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-12
ii Contents
2
Installing the
MATLAB & Simulink Student Version
Installing on Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2
System Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2
Installing MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3
Installing Additional Toolboxes . . . . . . . . . . . . . . . . . . . . . . . . . . 2-5
Accessing the Online Documentation (Help) . . . . . . . . . . . . . . . 2-6
Installing on Mac OS X . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7
System Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7
Installing MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-8
Installing Additional Toolboxes . . . . . . . . . . . . . . . . . . . . . . . . . 2-14
Accessing the Online Documentation (Help) . . . . . . . . . . . . . . 2-14
Mac OS X Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-14
Installing on Linux . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-15
System Requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-15
Installing MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-16
Installing Additional Toolboxes . . . . . . . . . . . . . . . . . . . . . . . . . 2-21
Accessing the Online Documentation (Help) . . . . . . . . . . . . . . 2-21
3
Introduction
About MATLAB and Simulink . . . . . . . . . . . . . . . . . . . . . . . . . . 3-2
What Is MATLAB? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-2
What Is Simulink? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-4
MATLAB Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-5
MATLAB Online Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-5
Starting and Quitting MATLAB . . . . . . . . . . . . . . . . . . . . . . . . 3-7
Starting MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-7
Quitting MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-7
MATLAB Desktop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-8
iii
4
Matrices and Arrays
Matrices and Magic Squares . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2
Entering Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-3
sum, transpose, and diag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-4
Subscripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-6
The Colon Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-7
The magic Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-8
Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-10
Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-10
Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-10
Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-11
Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-11
Examples of Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-13
Working with Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-14
Generating Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-14
The load Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-15
M-Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-15
Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-16
Deleting Rows and Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-17
More About Matrices and Arrays . . . . . . . . . . . . . . . . . . . . . . 4-18
Linear Algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-18
Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-21
Multivariate Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-24
Scalar Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-25
Logical Subscripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-26
The find Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-27
Controlling Command Window Input and Output . . . . . . . 4-28
The format Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-28
Suppressing Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-30
Entering Long Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-30
Command Line Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-30