Using MATLAB Version 5 How to Contact the Mathworks
Total Page:16
File Type:pdf, Size:1020Kb
MATLAB® The Language of Technical Computing Computation Visualization Programming Using MATLAB Version 5 How to Contact The MathWorks: 508-647-7000 Phone 508-647-7001 Fax The MathWorks, Inc. Mail 24 Prime Park Way Natick, MA 01760-1500 http://www.mathworks.com Web ftp.mathworks.com Anonymous FTP server 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] Subscribing user registration [email protected] Order status, license renewals, passcodes [email protected] Sales, pricing, and general information Using MATLAB COPYRIGHT 1984 - 1999 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. U.S. GOVERNMENT: If Licensee is acquiring the Programs on behalf of any unit or agency of the U.S. Government, the following shall apply: (a) For units of the Department of Defense: the Government shall have only the rights specified in the license under which the commercial computer software or commercial software documentation was obtained, as set forth in subparagraph (a) of the Rights in Commercial Computer Software or Commercial Software Documentation Clause at DFARS 227.7202-3, therefore the rights set forth herein shall apply; and (b) For any other unit or agency: NOTICE: Notwithstanding any other lease or license agreement that may pertain to, or accompany the delivery of, the computer software and accompanying documentation, the rights of the Government regarding its use, reproduction, and disclo- sure are as set forth in Clause 52.227-19 (c)(2) of the FAR. MATLAB, Simulink, Stateflow, Handle Graphics, and Real-Time Workshop are registered trademarks, and Target Language Compiler is a trademark of The MathWorks, Inc. COPYRIGHT 1995 Bristol Technology, Inc. All rights reserved. COPYRIGHT 1995 Microsoft Corporation. All rights reserved. Other product or brand names are trademarks or registered trademarks of their respective holders. Printing History: December 1996 First printing for MATLAB 5.0 June 1997 Revised for MATLAB 5.1 January 1998 Revised for MATLAB 5.2 January 1999 Revised for MATLAB 5.3 (Release 11) Contents Introduction 1 MATLAB Working Environment 2 Using the Environment . 2-2 The Command Window . 2-5 The Figure Window . 2-18 Help and Online Documentation . 2-20 Disk File Manipulation and Shell Escape . 2-25 Data Import/Export . 2-26 Memory Utilization . 2-33 Microsoft Windows Handbook . 2-35 UNIX Handbook . 2-54 Debugger and Profiler 3 MATLAB Debugger . 3-2 M-File Profiler . 3-17 i Matrices and Linear Algebra 4 Matrices and Linear Algebra . 4-2 Matrices in MATLAB . 4-4 Solving Linear Equations . 4-13 Inverses and Determinants . 4-20 LU, QR, and Cholesky Factorizations . 4-24 Matrix Powers and Exponentials . 4-31 Eigenvalues . 4-34 Singular Value Decomposition . 4-38 Polynomials and Interpolation 5 Polynomials . 5-2 Interpolation . 5-9 Data Analysis and Statistics 6 Column-Oriented Data Sets . 6-3 Basic Data Analysis Functions . 6-7 Data Pre-Processing . 6-12 ii Contents Regression and Curve Fitting . 6-15 Case Study: Curve Fitting . 6-20 Difference Equations and Filtering . 6-29 Fourier Analysis and the Fast Fourier Transform (FFT) . 6-31 Function Functions 7 Representing Functions in MATLAB . 7-3 Plotting Mathematical Functions . 7-4 Minimizing Functions and Finding Zeros . 7-7 Numerical Integration (Quadrature) . 7-14 Ordinary Differential Equations 8 Quick Start . 8-3 Representing Problems . 8-5 ODE Solvers . 8-10 Creating ODE Files . 8-14 Improving Solver Performance . 8-17 Examples: Applying the ODE Solvers . 8-34 iii Questions and Answers . 8-50 Sparse Matrices 9 Introduction . 9-5 Viewing Sparse Matrices . 9-11 Example: Adjacency Matrices and Graphs . 9-15 Sparse Matrix Operations . 9-23 M-File Programming 10 MATLAB Programming: A Quick Start . 10-2 Scripts . 10-5 Functions . 10-6 Local and Global Variables . 10-16 Data Types . 10-19 Operators . 10-21 Flow Control . 10-30 Subfunctions . 10-38 Indexing and Subscripting . 10-40 iv Contents String Evaluation . 10-46 Command/Function Duality . 10-48 Empty Matrices . 10-49 Errors and Warnings . 10-51 Times and Dates . 10-54 Obtaining User Input . 10-61 Shell Escape Functions . 10-62 Optimizing the Performance of MATLAB Code . 10-63 Character Arrays (Strings) 11 Character Arrays . 11-4 Cell Arrays of Strings . 11-7 String Comparisons . 11-9 Searching and Replacing . 11-12 String/Numeric Conversion . 11-13 Multidimensional Arrays 12 Multidimensional Arrays . 12-3 v Computation with Multidimensional Arrays . 12-15 Organizing Data in Multidimensional Arrays . 12-17 Multidimensional Cell Arrays . 12-19 Multidimensional Structure Arrays . 12-20 Structures and Cell Arrays 13 Structures . 13-3 Cell Arrays . 13-19 MATLAB Classes and Objects 14 Classes and Objects: An Overview . 14-2 Designing User Classes in MATLAB . 14-9 Overloading Operators and Functions . 14-20 Example: A Polynomial Class . 14-23 Building on Other Classes . 14-34 Example: Assets and Asset Subclasses . 14-37 Example: The Portfolio Container . 14-54 Saving and Loading Objects . 14-61 vi Contents Object Precedence . 14-66 How MATLAB Determines Which Method to Call . 14-68 File I/O 15 Opening and Closing Files . 15-3 Temporary Files and Directories . 15-6 Binary Files . 15-7 Controlling Position in a File . 15-10 Formatted Files 15-13 vii viii Contents 1 Introduction What Is MATLAB? . 1-3 The MATLAB System . 1-4 How to Use the Documentation Set . 1-6 About Simulink . 1-8 About Toolboxes . 1-8 1 Introduction About the Cover The cover of this guide depicts a solution to a problem that has played a small, but interesting role in the history of numerical methods during the last 30 years. The problem involves finding the modes of vibration of a membrane supported by an L-shaped domain consisting of three unit squares. The nonconvex corner in the domain generates singularities in the solutions, thereby providing challenges for both the underlying mathematical theory and the computational algorithms. There are important applications, including wave guides, structures, and semiconductors. Two of the founders of modern numerical analysis, George Forsythe and J.H. Wilkinson, worked on the problem in the 1950s. (See G.E. Forsythe and W.R. Wasow, Finite-Difference Methods for Partial Differential Equations, Wiley, 1960.) One of the authors of this guide (Moler) used finite differences by combinations of distinguished fundamental solutions to the underlying differential equation formed from Bessel and trigonometric functions. The idea is a generalization of the fact that the real and imaginary parts of complex analytic functions are solutions to Laplace’s equation. In the early 1970s, new matrix algorithms, particularly Gene Golub’s orthogonalization techinques for least squares problems, provided further algorithmic improvements. Today, MATLAB allows us to express the entire algorithm in a few dozen lines, to compute the solution with great accuracy in a few minutes on a computer at home, and to readily manipulate color three-dimensional displays of the results. We have included our MATLAB program, membrane.m, with the M-files supplied along with MATLAB. 1-2 What Is MATLAB? MATLAB® is a high-performance language for technical computing. It integrates computation, visualization, and programming in an.