MATLAB The Language of Technical Computing Computation Visualization Programming Getting Started with MATLAB The MathWorks Version 6
Computation Visualization Programming Getting Started with MATLAB Version 6 MATLABÆ The Language of Technical Computing
Contents Introduction What Is matlaB? The MATLAB System MATLAB Documentatio MATLAB Online Help Development Environment Starting and quitting MATLAB 22 Starting MATLAB Quitting MATLAB MATLAB Desktop 2 Desktop Tools Command window Start Button and launch pad 27 Help Browser nt Directory Brows 2-10 pace Browser Editor/Debugger 2-14 Profiler 2-15 Other Development Environment Features
i Contents 1 Introduction What Is MATLAB? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2 The MATLAB System . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-3 MATLAB Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-4 MATLAB Online Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-4 2 Development Environment Starting and Quitting MATLAB . . . . . . . . . . . . . . . . . . . . . . . . 2-2 Starting MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2 Quitting MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2 MATLAB Desktop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3 Desktop Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-5 Command Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-5 Start Button and Launch Pad . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7 Help Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7 Current Directory Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-10 Workspace Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-12 Editor/Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-14 Profiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-15 Other Development Environment Features . . . . . . . . . . . . 2-16
Manipulating Matrices Matrices and Magic Squares 32 Entering Matrices 33 sum, transpose and diag Subscripts 36 The Colon Operator 3.7 The magic Function 3-8 Expressions Variables 10 Numb 3-10 perators Functions 3-11 Examples of Expressions 3-13 Working with Matrices 3-14 Generating Matrices The load Function M-Files 3-15 Concatenation Deleting Rows and Columns 3-17 More About Matrices and Arrays 3-18 Linear algebra 18 Arrays Multivariate data Scalar Expansion 3-25 Logical Subscripting The find Function 3-27 Controlling Command window Input and Output The format function Suppressing Out 3-30 Entering Long Statements d Line editing
ii Contents 3 Manipulating Matrices Matrices and Magic Squares . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-2 Entering Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-3 sum, transpose, and diag . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-4 Subscripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-6 The Colon Operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-7 The magic Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-8 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-10 Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-10 Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-10 Operators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-11 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-11 Examples of Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-13 Working with Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-14 Generating Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-14 The load Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-15 M-Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-15 Concatenation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-16 Deleting Rows and Columns . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-17 More About Matrices and Arrays . . . . . . . . . . . . . . . . . . . . . . 3-18 Linear Algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-18 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-21 Multivariate Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-24 Scalar Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-25 Logical Subscripting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-26 The find Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-27 Controlling Command Window Input and Output . . . . . . . 3-28 The format Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-28 Suppressing Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-30 Entering Long Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-30 Command Line Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-30
raphics Basic Plotting 42 Creating a Plot. 42 Multiple Data Sets in One graph Specifying Line Styles and Colors Plotting Lines and Markers 45 Imaginary and Complex Data 46 Adding Plots to an Existing graph 47 Indows Multiple Plots in One Figure 49 4-10 Axis Labels and Titles 4-12 Editing Plots 4-14 Interactive Plot Editing 414 Using Functions to Edit Graphs 414 Using Plot Editing Mode 4-15 sing the Prop 416 Mesh and Surface plots 418 Visualizing Functions of Two variables 418 Imag 4-22 Printing Graphics 4-24 Handle graphics 4-26 Setting Object Properties Finding the Handles of Existing Objects Graphics User Interfaces Graphical User Interface Design Tools 4-33 Animations 4-34 Erase mode method 4-34 Creating Movies 4-35
iii 4 Graphics Basic Plotting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2 Creating a Plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2 Multiple Data Sets in One Graph . . . . . . . . . . . . . . . . . . . . . . . . 4-3 Specifying Line Styles and Colors . . . . . . . . . . . . . . . . . . . . . . . . 4-4 Plotting Lines and Markers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-5 Imaginary and Complex Data . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-6 Adding Plots to an Existing Graph . . . . . . . . . . . . . . . . . . . . . . . 4-7 Figure Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-8 Multiple Plots in One Figure . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-9 Controlling the Axes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-10 Axis Labels and Titles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-12 Saving a Figure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-13 Editing Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-14 Interactive Plot Editing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-14 Using Functions to Edit Graphs . . . . . . . . . . . . . . . . . . . . . . . . 4-14 Using Plot Editing Mode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-15 Using the Property Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-16 Mesh and Surface Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-18 Visualizing Functions of Two Variables . . . . . . . . . . . . . . . . . . 4-18 Images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-22 Printing Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-24 Handle Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-26 Graphics Objects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-26 Setting Object Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-28 Finding the Handles of Existing Objects . . . . . . . . . . . . . . . . . 4-31 Graphics User Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-33 Graphical User Interface Design Tools . . . . . . . . . . . . . . . . . . . 4-33 Animations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-34 Erase Mode Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-34 Creating Movies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-35
Programming with MATLAB 5 Flow Control 52 if 5-2 fo continue break 5-6 Other Data Structures 5·7 Multidimensional Arrays.... Cell Arrays 59 Characters and Text 5-11 Structures 514 Scripts and Functions 5-18 Functions 5-21 Passing String Arguments to Functions The eval function Vectorization Preallocation 5-24 Function handles 5-24 Function Functions Demonstration Programs Included with MATLAB 5-28 Matrix Demonstration Programs 529 Numeric Demonstration Programs 530 Graphics Demonstration Programs 531 Language Demonstration Programs Differential Equations Demonstration Programs 533 Automation Client Interface(COM) Gallery Demonstration Programs 5-34 Miscellaneous Demonstration Progran 536 Getting More Information
iv Contents 5 Programming with MATLAB Flow Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-2 if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-2 switch and case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-3 for . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-4 while . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-5 continue . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-5 break . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-6 Other Data Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-7 Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-7 Cell Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-9 Characters and Text . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-11 Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-14 Scripts and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-17 Scripts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-18 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-19 Global Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-21 Passing String Arguments to Functions . . . . . . . . . . . . . . . . . . 5-21 The eval Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-23 Vectorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-23 Preallocation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-24 Function Handles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-24 Function Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-25 Demonstration Programs Included with MATLAB . . . . . . 5-28 Matrix Demonstration Programs . . . . . . . . . . . . . . . . . . . . . . . 5-29 Numeric Demonstration Programs . . . . . . . . . . . . . . . . . . . . . . 5-30 Graphics Demonstration Programs . . . . . . . . . . . . . . . . . . . . . 5-31 Language Demonstration Programs . . . . . . . . . . . . . . . . . . . . . 5-32 Differential Equations Demonstration Programs . . . . . . . . . . 5-33 Automation Client Interface (COM) . . . . . . . . . . . . . . . . . . . . . 5-34 Gallery Demonstration Programs . . . . . . . . . . . . . . . . . . . . . . . 5-34 Miscellaneous Demonstration Programs . . . . . . . . . . . . . . . . . 5-36 Getting More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-36