Getting Started with MATLAB
Total Page:16
File Type:pdf, Size:1020Kb
MATLAB® The Language of Technical Computing Getting Started with MATLAB® Version 7 How to Contact The MathWorks: www.mathworks.com Web comp.soft-sys.matlab Newsgroup [email protected] Technical support [email protected] Product enhancement suggestions [email protected] Bug reports [email protected] Documentation error reports [email protected] Order status, license renewals, passcodes [email protected] Sales, pricing, and general information 508-647-7000 Phone 508-647-7001 Fax The MathWorks, Inc. Mail 3 Apple Hill Drive Natick, MA 01760-2098 For contact information about worldwide offices, see the MathWorks Web site. Getting Started with MATLAB © COPYRIGHT 1984 - 2005 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, Real-Time Workshop, and xPC TargetBox are registered trademarks of The MathWorks, Inc. Other product or brand names are trademarks or registered trademarks of their respective holders. Revision History December 1996 First printing For MATLAB 5 May 1997 Second printing For MATLAB 5.1 September 1998 Third printing For MATLAB 5.3 September 2000 Fourth printing Revised for MATLAB 6 (Release 12) June 2001 Online only Revised for MATLAB 6.1 (Release 12.1) July 2002 Online only Revised for MATLAB 6.5 (Release 13) August 2002 Fifth printing Revised for MATLAB 6.5 June 2004 Sixth printing Revised for MATLAB 7.0 (Release 14) October 2004 Online only Revised for MATLAB 7.0.1 (Release 14SP1) March 2005 Online only Revised for MATLAB 7.0.4 (Release 14SP2) Contents Introduction 1 What Is MATLAB? . 1-2 The MATLAB System . 1-3 MATLAB Documentation . 1-4 MATLAB Online Help . 1-4 Starting and Quitting MATLAB . 1-6 Starting MATLAB . 1-6 Quitting MATLAB . 1-6 MATLAB Desktop . 1-7 Matrices and Arrays 2 Matrices and Magic Squares . 2-2 Entering Matrices . 2-3 sum, transpose, and diag . 2-4 Subscripts . 2-6 The Colon Operator . 2-7 The magic Function . 2-8 Expressions . 2-10 Variables . 2-10 Numbers . 2-10 Operators . 2-11 Functions . 2-11 Examples of Expressions . 2-13 Working with Matrices . 2-14 Generating Matrices . 2-14 The load Function . 2-15 i M-Files . 2-15 Concatenation . 2-16 Deleting Rows and Columns . 2-17 More About Matrices and Arrays . 2-18 Linear Algebra . 2-18 Arrays . 2-21 Multivariate Data . 2-24 Scalar Expansion . 2-25 Logical Subscripting . 2-26 The find Function . 2-27 Controlling Command Window Input and Output . 2-28 The format Function . 2-28 Suppressing Output . 2-30 Entering Long Statements . 2-30 Command Line Editing . 2-30 Graphics 3 Overview of MATLAB Plotting . 3-2 The Plotting Process . 3-2 Graph Components . 3-5 Figure Tools . 3-7 Arranging Graphs Within a Figure . 3-13 Selecting Plot Types . 3-14 Editing Plots . 3-16 Plot Editing Mode . 3-16 Using Functions to Edit Graphs . 3-19 Examples — Using MATLAB Plotting Tools . 3-20 Modifying the Graph Data Source . 3-27 Preparing Graphs for Presentation . 3-29 Modify the Graph to Enhance the Presentation . 3-30 ii Contents Printing the Graph . 3-33 Exporting the Graph . 3-35 Basic Plotting Functions . 3-38 Creating a Plot . 3-38 Multiple Data Sets in One Graph . 3-40 Specifying Line Styles and Colors . 3-41 Plotting Lines and Markers . 3-41 Imaginary and Complex Data . 3-43 Adding Plots to an Existing Graph . 3-44 Figure Windows . 3-46 Multiple Plots in One Figure . 3-46 Controlling the Axes . 3-48 Axis Labels and Titles . 3-49 Saving Figures . 3-51 Mesh and Surface Plots . 3-52 Visualizing Functions of Two Variables . 3-52 Images . 3-58 Reading and Writing Images . 3-59 Printing Graphics . 3-60 Handle Graphics . 3-62 Using the Handle . 3-62 Graphics Objects . 3-63 Setting Object Properties . 3-65 Specifying the Axes or Figure . 3-68 Finding the Handles of Existing Objects . 3-69 Animations . 3-71 Erase Mode Method . 3-71 Creating Movies . 3-73 iii Programming 4 Flow Control . 4-2 if, else, and elseif . 4-2 switch and case . 4-4 for . 4-5 while . 4-5 continue . 4-6 break . ..