The Language Of Technical Computing

Transcription

MATLAB The Language of Technical ComputingComputationVisualizationProgrammingUsing MATLABVersion 6

How to Contact The Newsgroupinfo@mathworks.comTechnical supportProduct enhancement suggestionsBug reportsDocumentation error reportsOrder status, license renewals, passcodesSales, pricing, and general information508-647-7000Phone508-647-7001FaxThe MathWorks, Inc.3 Apple Hill DriveNatick, MA athworks.comFor contact information about worldwide offices, see the MathWorks Web site.Using MATLAB COPYRIGHT 1984 - 2001 by The MathWorks, Inc.The software described in this document is furnished under a license agreement. The software may be usedor copied only under the terms of the license agreement. No part of this manual may be photocopied or reproduced in any form without prior written consent from The MathWorks, Inc.FEDERAL ACQUISITION: This provision applies to all acquisitions of the Program and Documentation byor for the federal government of the United States. By accepting delivery of the Program, the governmenthereby agrees that this software qualifies as "commercial" computer software within the meaning of FARPart 12.212, DFARS Part 227.7202-1, DFARS Part 227.7202-3, DFARS Part 252.227-7013, and DFARS Part252.227-7014. The terms and conditions of The MathWorks, Inc. Software License Agreement shall pertainto the government’s use and disclosure of the Program and Documentation, and shall supersede anyconflicting contractual terms or conditions. If this license fails to meet the government’s minimum needs oris inconsistent in any respect with federal procurement law, the government agrees to return the Programand Documentation, unused, to MathWorks.MATLAB, Simulink, Stateflow, Handle Graphics, and Real-Time Workshop are registered trademarks, andTarget Language Compiler is a trademark of The MathWorks, Inc.Other product or brand names are trademarks or registered trademarks of their respective holders.Printing History: December 1996June 1997January 1998January 1999November 2000June 2001First printingSecond printingThird printingFourth printingFifth printingOnline onlyFirst Printing for MATLAB 5Revised for MATLAB 5.1Revised for MATLAB 5.2Revised for MATLAB 5.3 (Release 11)Revised for MATLAB 6 (Release 12)Revised for MATLAB 6.1 (Release 12.1)

ContentsDevelopment EnvironmentStarting and Quitting MATLAB1Starting MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-3Starting MATLAB on Windows Platforms . . . . . . . . . . . . . . . . . 1-3Starting MATLAB on UNIX Platforms . . . . . . . . . . . . . . . . . . . 1-3Startup Directory for MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . 1-3Startup Options . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-5Reducing Startup Time with Toolbox Path Caching . . . . . . . . 1-10Quitting MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-15Running a Script When Quitting MATLAB . . . . . . . . . . . . . . . 1-15Using the Desktop2Desktop Tools . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-4Launch Pad . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-5Configuring the Desktop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7Opening and Closing Desktop Tools . . . . . . . . . . . . . . . . . . . . . . 2-7Resizing Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-9Moving Windows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-10Using Predefined Desktop Configurations . . . . . . . . . . . . . . . . 2-16Common Desktop Features . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-17Desktop Toolbar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-17Context Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-17i

Keyboard Shortcuts and Accelerators . . . . . . . . . . . . . . . . . . . .Selecting Multiple Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Using the Clipboard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Accessing The MathWorks on the Web . . . . . . . . . . . . . . . . . . .2-182-192-202-20Setting Preferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-21General Preferences for MATLAB . . . . . . . . . . . . . . . . . . . . . . 2-23Running MATLAB Functions3The Command Window . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-2Opening the Command Window . . . . . . . . . . . . . . . . . . . . . . . . . 3-2Running Functions and Entering Variables . . . . . . . . . . . . . . . . 3-2Controlling Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-4Running Programs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-11Keeping a Session Log . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-12Preferences for the Command Window . . . . . . . . . . . . . . . . . . . 3-13Command History . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Viewing Functions in the Command History Window . . . . . . .Running Functions from the Command History Window . . . .Copying Functions from the Command History Window . . . .3-173-173-183-19Getting Help4Types of Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-3Using the Help Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-4Changing the Size of the Help Browser . . . . . . . . . . . . . . . . . . . 4-5Using the Help Navigator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-7Using the Product Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-7iiContents

Viewing the Contents Listing in the Help Browser . . . . . . . . . . 4-9Finding Documentation Using the Index . . . . . . . . . . . . . . . . . 4-11Searching Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-13Bookmarking Favorite Pages . . . . . . . . . . . . . . . . . . . . . . . . . . 4-15Viewing Documentation in the Display Pane . . . . . . . . . . .Browsing to Other Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Bookmarking Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Revisiting Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Printing Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Finding Terms in Displayed Pages . . . . . . . . . . . . . . . . . . . . . .Copying Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Evaluating a Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Viewing the Page Source (HTML) . . . . . . . . . . . . . . . . . . . . . . .Viewing Web Pages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . es for the Help Browser . . . . . . . . . . . . . . . . . . . . .Documentation Location – Specifying the help Directory . . . .Product Filter – Limiting the Product Documentation . . . . . .PDF Reader – Specifying Its Location . . . . . . . . . . . . . . . . . . .General – Synchronizing the Contents Panewith the Displayed Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Help Fonts Preferences – Specifying Font Name,Style, and Size . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4-214-224-234-234-234-24Printing Documentation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-26Printing a Page from the Help Browser . . . . . . . . . . . . . . . . . . 4-26Printing the PDF Version of Documentation . . . . . . . . . . . . . . 4-26Using Help Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-28Viewing Function Reference Pages – the doc Function . . . . . . 4-29Getting Help in the Command Window – the help Function . 4-29Other Methods for Getting Help . . . . . . . . . . . . . . . . . . . . . . .Product-Specific Help Features . . . . . . . . . . . . . . . . . . . . . . . . .Running Demos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Contacting Technical Support . . . . . . . . . . . . . . . . . . . . . . . . . .Providing Feedback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Getting Version and License Information . . . . . . . . . . . . . . . .4-314-314-314-324-324-32iii

Accessing Documentation for Other Products . . . . . . . . . . . . . 4-33Participating in the Newsgroup for MathWorks Products . . . 4-33Workspace, Search Path, and File Operations5MATLAB Workspace . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-3Workspace Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-3Viewing and Editing Workspace VariablesUsing the Array Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-10Search Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-14How the Search Path Works . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-14Viewing and Setting the Search Path . . . . . . . . . . . . . . . . . . . . 5-15File Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Current Directory Field . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Current Directory Browser . . . . . . . . . . . . . . . . . . . . . . . . . . . .Viewing and Making Changes to Directories . . . . . . . . . . . . . .Creating, Renaming, Copying, andRemoving Directories and Files . . . . . . . . . . . . . . . . . . . . . . . .Opening, Running, and Viewing the Content of Files . . . . . . .Finding and Replacing Content Within Files . . . . . . . . . . . . . .Preferences for the Current Directory Browser . . . . . . . . . . . .5-205-205-205-225-235-265-285-30Importing and Exporting Data6Importing Text Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-4Using the Import Wizard with Text Data . . . . . . . . . . . . . . . . . 6-4Using Import Functions with Text Data . . . . . . . . . . . . . . . . . . 6-9Importing Numeric Text Data . . . . . . . . . . . . . . . . . . . . . . . . . . 6-11Importing Delimited ASCII Data Files . . . . . . . . . . . . . . . . . . . 6-12Importing Numeric Data with Text Headers . . . . . . . . . . . . . . 6-13ivContents

Importing Mixed Alphabetic and Numeric Data . . . . . . . . . . . 6-14Exporting ASCII Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-16Exporting Delimited ASCII Data Files . . . . . . . . . . . . . . . . . . . 6-17Using the diary Command to Export Data . . . . . . . . . . . . . . . . 6-18Importing Binary Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-20Using the Import Wizard with Binary Data Files . . . . . . . . . . 6-20Using Import Functions with Binary Data . . . . . . . . . . . . . . . 6-22Exporting Binary Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-25Exporting MATLAB Graphs in AVI Format . . . . . . . . . . . . . . 6-27Working with HDF Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Overview of MATLAB HDF Support . . . . . . . . . . . . . . . . . . . .MATLAB HDF Function Calling Conventions . . . . . . . . . . . . .Importing HDF Data into the MATLAB Workspace . . . . . . . .Exporting MATLAB Data in an HDF File . . . . . . . . . . . . . . . .Including Metadata in an HDF File . . . . . . . . . . . . . . . . . . . . .Using the MATLAB HDF Utility API . . . . . . . . . . . . . . . . . . . .6-296-306-316-336-416-476-49Using Low-Level File I/O Functions . . . . . . . . . . . . . . . . . . . .Opening Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Reading Binary Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Writing Binary Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Controlling Position in a File . . . . . . . . . . . . . . . . . . . . . . . . . .Reading Strings Line-By-Line from Text Files . . . . . . . . . . . .Reading Formatted ASCII Data . . . . . . . . . . . . . . . . . . . . . . . .Writing Formatted Text Files . . . . . . . . . . . . . . . . . . . . . . . . . .Closing a File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .6-516-526-546-556-566-586-596-616-62Editing and Debugging M-Files7Starting the Editor/Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . 7-3Creating a New M-File in the Editor/Debugger . . . . . . . . . . . . . 7-4Opening Existing M-Files in the Editor/Debugger . . . . . . . . . . 7-5v

Opening the Editor Without Starting MATLAB . . . . . . . . . . . . 7-6Closing the Editor/Debugger . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-6Creating and Editing M-Files with the Editor/Debugger . . 7-8Appearance of an M-File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-8Navigating in an M-File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-11Saving M-Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-15Running M-Files from the Editor/Debugger . . . . . . . . . . . . . . . 7-15Printing an M-File . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-16Closing M-Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-16Debugging M-Files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Types of Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Finding Errors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Debugging Example – The Collatz Problem . . . . . . . . . . . . . . .Using Debugging Features . . . . . . . . . . . . . . . . . . . . . . . . . . . .7-177-177-177-187-22Preferences for the Editor/Debugger . . . . . . . . . . . . . . . . . . .General Preferences for the Editor/Debugger . . . . . . . . . . . . .Font & Colors Preferences for the Editor/Debugger . . . . . . . .Display Preferences for the Editor/Debugger . . . . . . . . . . . . . .Keyboard and Indenting Preferences for the Editor/DebuggerPrinting Preferences for the Editor/Debugger . . . . . . . . . . . . .7-357-367-377-387-407-42Improving M-File Performance – the Profiler8What Is Profiling? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-3Using the Profiler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-4The profile Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-4An Example Using the Profiler . . . . . . . . . . . . . . . . . . . . . . . . . . 8-6Viewing Profiler Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-7Viewing Profile Reports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-7Profile Plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-12Saving Profile Reports . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8-13viContents

Interfacing with Source Control Systems9Process of Interfacing to an SCS . . . . . . . . . . . . . . . . . . . . . . . . 9-3Viewing or Selecting the Source Control System . . . . . . . . . 9-4Function Alternative for Viewing the SCS . . . . . . . . . . . . . . . . . 9-5Setting Up the Source Control System . . . . . . . . . . . . . . . . . . 9-6For SourceSafe Only – Mirroring MATLAB Hierarchy . . . . . . . 9-6For ClearCase on UNIX Only – Set a View andCheck Out a Directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-6Checking Files into the Source Control System . . . . . . . . . . 9-7Function Alternative for Checking In Files . . . . . . . . . . . . . . . . 9-8Checking Files Out of the SCS . . . . . . . . . . . . . . . . . . . . . . . . . . 9-9Function Alternative for Checking Out Files . . . . . . . . . . . . . . 9-10Undoing the Check-Out . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9-10Using Notebook10Notebook Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Creating an M-Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Entering MATLAB Commands in an M-Book . . . . . . . . . . . .Protecting the Integrity of Your Workspace . . . . . . . . . . . . . . .Ensuring Data Consistency . . . . . . . . . . . . . . . . . . . . . . . . . . . .10-310-310-610-610-7Defining MATLAB Commands as Input Cells . . . . . . . . . . . 10-8Defining Cell Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-8Defining Autoinit Input Cells . . . . . . . . . . . . . . . . . . . . . . . . . 10-10Defining Calc Zones . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-10Converting an Input Cell to Text . . . . . . . . . . . . . . . . . . . . . . 10-11Evaluating MATLAB Commands . . . . . . . . . . . . . . . . . . . . . 10-12Evaluating Cell Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-13vii

Evaluating a Range of Input Cells . . . . . . . . . . . . . . . . . . . . .Evaluating a Calc Zone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Evaluating an Entire M-Book . . . . . . . . . . . . . . . . . . . . . . . . .Using a Loop to Evaluate Input Cells Repeatedly . . . . . . . . .Converting Output Cells to Text . . . . . . . . . . . . . . . . . . . . . . .Deleting Output Cells . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .10-1410-1410-1510-1510-1610-17Printing and Formatting an M-Book . . . . . . . . . . . . . . . . . .Printing an M-Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Modifying Styles in the M-Book Template . . . . . . . . . . . . . . .Choosing Loose or Compact Format . . . . . . . . . . . . . . . . . . . .Controlling Numeric Output Format . . . . . . . . . . . . . . . . . . .Controlling Graphic Output . . . . . . . . . . . . . . . . . . . . . . . . . .10-1810-1810-1810-1910-2010-20Configuring Notebook. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10-24Notebook Command Reference . . . . . . . . . . . . . . . . . . . . . . .Bring MATLAB to Front Command . . . . . . . . . . . . . . . . . . . .Define Autoinit Cell Command . . . . . . . . . . . . . . . . . . . . . . . .Define Calc Zone Command . . . . . . . . . . . . . . . . . . . . . . . . . .Define Input Cell Command . . . . . . . . . . . . . . . . . . . . . . . . . .Evaluate Calc Zone Command . . . . . . . . . . . . . . . . . . . . . . . .Evaluate Cell Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Evaluate Loop Command . . . . . . . . . . . . . . . . . . . . . . . . . . . .Evaluate M-Book Command . . . . . . . . . . . . . . . . . . . . . . . . . .Group Cells Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Hide Cell Markers Command . . . . . . . . . . . . . . . . . . . . . . . . .Notebook Options Command . . . . . . . . . . . . . . . . . . . . . . . . . .Purge Output Cells Command . . . . . . . . . . . . . . . . . . . . . . . .Toggle Graph Output for Cell Command . . . . . . . . . . . . . . . .Undefine Cells Command . . . . . . . . . . . . . . . . . . . . . . . . . . . .Ungroup Cells Command . . . . . . . . . . . . . . . . . . . . . . . . . . . . .viii -3010-3010-3110-3110-3110-3110-3210-32

MathematicsMatrices and Linear Algebra11Function Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-3Matrices in MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-5Creation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-5Addition and Subtraction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-7Vector Productsand Transpose . . . . . . . . . . . . . . . . . . . . . . . . . 11-7Matrix Multiplication . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-9The Identity Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-11The Kronecker Tensor Product . . . . . . . . . . . . . . . . . . . . . . . . 11-11Vector and Matrix Norms . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-12Solving Linear Systems of Equations . . . . . . . . . . . . . . . . .Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Square Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Overdetermined Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Underdetermined Systems . . . . . . . . . . . . . . . . . . . . . . . . . . .11-1311-1311-1511-1511-18Inverses and Determinants . . . . . . . . . . . . . . . . . . . . . . . . . . 11-21Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-21Pseudoinverses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-22Cholesky, LU, and QR Factorizations . . . . . . . . . . . . . . . . .Cholesky Factorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .LU Factorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .QR Factorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .11-2511-2511-2611-28Matrix Powers and Exponentials . . . . . . . . . . . . . . . . . . . . . 11-32Eigenvalues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11-35Singular Value Decomposition . . . . . . . . . . . . . . . . . . . . . . . 11-39ix

Polynomials and Interpolation12Polynomials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Polynomial Function Summary . . . . . . . . . . . . . . . . . . . . . . . . .Representing Polynomials . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Polynomial Roots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Characteristic Polynomials . . . . . . . . . . . . . . . . . . . . . . . . . . . .Polynomial Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Convolution and Deconvolution . . . . . . . . . . . . . . . . . . . . . . . .Polynomial Derivatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Polynomial Curve Fitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Partial Fraction Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . tion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Interpolation Function Summary . . . . . . . . . . . . . . . . . . . . . .One-Dimensional Interpolation . . . . . . . . . . . . . . . . . . . . . . . .Two-Dimensional Interpolation . . . . . . . . . . . . . . . . . . . . . . .Comparing Interpolation Methods . . . . . . . . . . . . . . . . . . . . .Interpolation and Multidimensional Arrays . . . . . . . . . . . . .Triangulation and Interpolation of Scattered Data . . . . . . . .Tessellation and Interpolation of Scattered Data inHigher Dimensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .12-1012-1012-1112-1312-1412-1612-1912-27Selected Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12-38Data Analysis and Statistics13Column-Oriented Data Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-4Basic Data Analysis Functions . . . . . . . . . . . . . . . . . . . . . . . . 13-8Function Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-8Covariance and Correlation Coefficients . . . . . . . . . . . . . . . . 13-11Finite Differences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-12xContents

Data Preprocessing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-14Missing Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-14Removing Outliers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13-15Regression and Curve Fitting . . . . . . . . . . . . . . . . . . . . . . . .Polynomial Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Linear-in-the-Parameters Regression . . . . . . . . . . . . . . . . . . .Multiple Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13-1713-1813-1913-21Case Study: Curve Fitting . . . . . . . . . . . . . . . . . . . . . . . . . . . .Polynomial Fit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Analyzing Residuals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Exponential Fit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Error Bounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .The Basic Fitting Interface . . . . . . . . . . . . . . . . . . . . . . . . . . .13-2213-2213-2413-2713-3013-31Difference Equations and Filtering . . . . . . . . . . . . . . . . . . . 13-40Fourier Analysis and the Fast Fourier Transform (FFT)Function Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Magnitude and Phase of Transformed Data . . . . . . . . . . . . .FFT Length Versus Speed . . . . . . . . . . . . . . . . . . . . . . . . . . . .13-4313-4313-4413-4913-50Function Functions14Function Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14-3Representing Functions in MATLAB . . . . . . . . . . . . . . . . . . . 14-4Plotting Mathematical Functions . . . . . . . . . . . . . . . . . . . . . . 14-6Minimizing Functions and Finding Zeros . . . . . . . . . . . . . . 14-9Minimizing Functions of One Variable . . . . . . . . . . . . . . . . . . . 14-9Minimizing Functions of Several Variables . . . . . . . . . . . . . . 14-10Setting Minimization Options . . . . . . . . . . . . . . . . . . . . . . . . . 14-11xi

Finding Zeros of Functions . . . . . . . . . . . . . . . . . . . . . . . . . . .Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Converting Your Optimization Code toMATLAB Version 5 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . .14-1214-1514-1514-16Numerical Integration (Quadrature) . . . . . . . . . . . . . . . . . . 14-19Example: Computing the Length of a Curve . . . . . . . . . . . . . 14-19Example: Double Integration . . . . . . . . . . . . . . . . . . . . . . . . . 14-20Differential Equations15Initial Value Problems for ODEs and DAEs . . . . . . . . . . . . . 15-3ODE Function Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-3Introduction to Initial Value ODE Problems . . . . . . . . . . . . . . 15-6Initial Value Problem Solvers . . . . . . . . . . . . . . . . . . . . . . . . . . 15-6Representing ODE Problems . . . . . . . . . . . . . . . . . . . . . . . . . . 15-10Improving ODE Solver Performance . . . . . . . . . . . . . . . . . . . 15-16Examples: Applying the ODE Initial Value Problem Solvers 15-31Questions and Answers, and Troubleshooting . . . . . . . . . . . . 15-50xiiContentsBoundary Value Problems for ODEs . . . . . . . . . . . . . . . . . .BVP Function Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Introduction to Boundary Value ODE Problems . . . . . . . . . .Boundary Value Problem Solver . . . . . . . . . . . . . . . . . . . . . . .Representing BVP Problems . . . . . . . . . . . . . . . . . . . . . . . . . .Using Continuation to Make a Good Initial Guess . . . . . . . .Improving BVP Solver Performance . . . . . . . . . . . . . . . . . . . .15-5715-5715-5915-6015-6315-6815-75Partial Differential Equations . . . . . . . . . . . . . . . . . . . . . . . .PDE Function Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . .Introduction to PDE Problems . . . . . . . . . . . . . . . . . . . . . . . .MATLAB Partial Differential Equation Solver . . . . . . . . . . .Representing PDE Problems . . . . . . . . . . . . . . . . . . . . . . . . . .Improving PDE Solver Performance . . . . . . . . . . . . . . . . . . . .Example: Electrodynamics Problem . . . . . . . . . . . . . . . . . . . .15-8215-8215-8315-8415-8815-9315-94

Selected Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15-99Sparse Matrices16Function Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-3Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-6Sparse Matrix Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-6General Storage Information . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-7Creating Sparse Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16-7Importing Sparse Matrices from Outside MATLAB . . . . . . . 16-12Viewing Sparse Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . .Information About Nonzero Elements . . . . . . . . . . . . . . . . . .Viewing Sparse Matrices Graphically . . . . . . . . . . . . . . . . . .The find Function and Sparse Matrices . . . . . . . . . . . . . . . . .16-1316-1316-1516-16Example: Adjacency Matrices and Graphs . . . . . . . . . . . . .Introduction to Adjacency Matrices . . . . . . . . . . . . . . . . . . . .Graphing Using Adjacency Matrices . . . . . . . . . . . . . . . . . . .The Bucky Ball . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .An Airflow Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16-1716-1716-1816-1816-23Sparse Matrix Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . .Computational Considerations . . . . . . . . . . . . . . . . . . . . . . . .Standard Mathematical Operations . . . . . . . . . . . . . . . . . . . .Permuta

Computation Visualization Programming Using MATLAB Version 6