MATLAB® The Language of Technical Computing Mathematics 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. MATLAB Mathematics © 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: June 2004 First printing New for MATLAB 7.0 (Release 14) Formerly part of Using MATLAB October 2004 Online only Revised for Version 7.0.1 (Release 14SP1) March 2005 Online only Revised for Version 7.0.4 (Release 14SP2) Contents Matrices and Linear Algebra 1 Function Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-2 Matrices in MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-4 Creating Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-4 Adding and Subtracting Matrices . . . . . . . . . . . . . . . . . . . . . . . 1-6 Vector Products and Transpose . . . . . . . . . . . . . . . . . . . . . . . . . 1-7 Multiplying Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-8 The Identity Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-10 The Kronecker Tensor Product . . . . . . . . . . . . . . . . . . . . . . . . . 1-11 Vector and Matrix Norms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-12 Solving Linear Systems of Equations . . . . . . . . . . . . . . . . . . 1-13 Computational Considerations . . . . . . . . . . . . . . . . . . . . . . . . . 1-13 General Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-15 Square Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-15 Overdetermined Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-18 Underdetermined Systems . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-20 Inverses and Determinants . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-23 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-23 Pseudoinverses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-24 Cholesky, LU, and QR Factorizations . . . . . . . . . . . . . . . . . . 1-28 Cholesky Factorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-28 LU Factorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-30 QR Factorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-31 Matrix Powers and Exponentials . . . . . . . . . . . . . . . . . . . . . . 1-35 Eigenvalues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1-39 Singular Value Decomposition . . . . . . . . . . . . . . . . . . . . . . . . 1-43 i Polynomials and Interpolation 2 Polynomials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2 Polynomial Function Summary . . . . . . . . . . . . . . . . . . . . . . . . . . 2-2 Representing Polynomials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3 Polynomial Roots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-3 Characteristic Polynomials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-4 Polynomial Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-4 Convolution and Deconvolution . . . . . . . . . . . . . . . . . . . . . . . . . 2-5 Polynomial Derivatives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-5 Polynomial Curve Fitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-6 Partial Fraction Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-7 Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-9 Interpolation Function Summary . . . . . . . . . . . . . . . . . . . . . . . . 2-9 One-Dimensional Interpolation . . . . . . . . . . . . . . . . . . . . . . . . . 2-10 Two-Dimensional Interpolation . . . . . . . . . . . . . . . . . . . . . . . . 2-12 Comparing Interpolation Methods . . . . . . . . . . . . . . . . . . . . . . 2-13 Interpolation and Multidimensional Arrays . . . . . . . . . . . . . . 2-15 Triangulation and Interpolation of Scattered Data . . . . . . . . . 2-18 Tessellation and Interpolation of Scattered Data in Higher Dimensions 2-26 Selected Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2-37 Data Analysis and Statistics 3 Column-Oriented Data Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-3 Basic Data Analysis Functions . . . . . . . . . . . . . . . . . . . . . . . . . 3-7 Function Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-7 Covariance and Correlation Coefficients . . . . . . . . . . . . . . . . . 3-10 Finite Differences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-11 Data Preprocessing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-13 Missing Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-13 ii Contents Removing Outliers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-15 Regression and Curve Fitting . . . . . . . . . . . . . . . . . . . . . . . . . 3-16 Polynomial Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-17 Linear-in-the-Parameters Regression . . . . . . . . . . . . . . . . . . . . 3-18 Multiple Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-20 Case Study: Curve Fitting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-21 Polynomial Fit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-21 Analyzing Residuals . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-23 Exponential Fit . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-25 Error Bounds . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-27 The Basic Fitting Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-28 Difference Equations and Filtering . . . . . . . . . . . . . . . . . . . . 3-39 Fourier Analysis and the Fast Fourier Transform (FFT) . 3-42 Function Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-42 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-43 Magnitude and Phase of Transformed Data . . . . . . . . . . . . . . 3-47 FFT Length Versus Speed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3-49 Function Functions 4 Function Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-2 Representing Functions in MATLAB . . . . . . . . . . . . . . . . . . . . 4-3 Plotting Mathematical Functions . . . . . . . . . . . . . . . . . . . . . . . 4-5 Minimizing Functions and Finding Zeros . . . . . . . . . . . . . . . 4-8 Minimizing Functions of One Variable . . . . . . . . . . . . . . . . . . . . 4-8 Minimizing Functions of Several Variables . . . . . . . . . . . . . . . . 4-9 Fitting a Curve to Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-10 Setting Minimization Options . . . . . . . . . . . . . . . . . . . . . . . . . . 4-13 Output Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-14 iii Finding Zeros of Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-21 Tips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-25 Troubleshooting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4-25 Numerical Integration (Quadrature) . . . . . . . . . . . . . . . . . . . 4-27 Example: Computing the Length of a Curve . . . . . . . . . . . . . . 4-27 Example: Double Integration . . . . . . . . . . . . . . . . . . . . . . . . . . 4-28 Parameterizing Functions Called by Function Functions 4-30 Providing Parameter Values Using Nested Functions . . . . . . 4-30 Providing Parameter Values to Anonymous Functions . . . . . . 4-31 Differential Equations 5 Initial Value Problems for ODEs and DAEs . . . . . . . . . . . . . . 5-2 ODE Function Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-2 Introduction to Initial Value ODE Problems . . . . . . . . . . . . . . . 5-4 Solvers for Explicit and Linearly Implicit ODEs . . . . . . . . . . . . 5-5 Examples: Solving Explicit ODE Problems . . . . . . . . . . . . . . . . 5-9 Solver for Fully Implicit ODEs . . . . . . . . . . . . . . . . . . . . . . . . . 5-15 Example: Solving a Fully Implicit ODE Problem . . . . . . . . . . 5-16 Changing ODE Integration Properties . . . . . . . . . . . . . . . . . . . 5-17 Examples: Applying the ODE Initial Value Problem Solvers . 5-18 Questions and Answers, and Troubleshooting . . . . . . . . . . . . . 5-39 Initial Value Problems for DDEs . . . . . . . . . . . . . . . . . . . . . . . 5-45 DDE Function Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-45 Introduction to Initial Value DDE Problems . . . . . . . . . . . . . . 5-46 DDE Solver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-47 Solving DDE Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-49 Discontinuities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-53 Changing DDE Integration Properties . . . . . . . . . . . . . . . . . . . 5-56 Boundary Value Problems for ODEs . . . . . . . . . . . . . . . . . . . 5-57 BVP Function Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-58 Introduction to Boundary Value ODE Problems . . . . . . . . . . . 5-59 iv Contents Boundary Value Problem Solver . . . . . . . . . . . . . . . . . . . . . . . . 5-60 Solving BVP Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-63 Using Continuation to Make a Good Initial Guess . . . . . . . . . 5-68 Solving Singular BVPs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-75 Solving Multi-Point BVPs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-79 Changing BVP Integration Properties . . . . . . . . . . . . . . . . . . . 5-79 Partial Differential Equations . . . . . . . . . . . . . . . . . . . . . . . . . 5-81 PDE Function Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-81 Introduction to PDE Problems . . . . . . . . . . . . . . . . . . . . . . . . . 5-82 MATLAB Partial Differential Equation Solver . . . . . . . . . . . . 5-83 Solving PDE Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-86 Evaluating the Solution at Specific Points . . . . . . . . . . . . . . . . 5-91 Changing PDE Integration Properties . . . . . . . . . . . . . . . . . . . 5-92 Example: Electrodynamics Problem . . . . . . . . . . . . . . . . . . . . . 5-92 Selected Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5-98 Sparse Matrices 6 Function Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-5 Sparse Matrix Storage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-5 General Storage Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-6 Creating Sparse Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-7 Importing Sparse Matrices from Outside MATLAB . . . . . . . . 6-11 Viewing Sparse Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-12 Information About Nonzero Elements . . . . . . . . . . . . . . . . . . . 6-12 Viewing Sparse Matrices Graphically . . . . . . . . . . . . . . . . . . . 6-14 The find Function and Sparse Matrices . . . . . . . . . . . . . . . . . . 6-15 Adjacency Matrices and Graphs . . . . . . . . . . . . . . . . . . . . . . . 6-16 Introduction to Adjacency Matrices . . . . . . . . . . . . . . . . . . . . . 6-16 Graphing Using Adjacency Matrices . . . . . . . . . . . . . . . . . . . . 6-17 v The Bucky Ball . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-17 An Airflow Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-22 Sparse Matrix Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-24 Computational Considerations . . . . . . . . . . . . . . . . . . . . . . . . . 6-24 Standard Mathematical Operations . . . . . . . . . . . . . . . . . . . . . 6-24 Permutation and Reordering . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-25 Factorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-29 Simultaneous Linear Equations . . . . . . . . . . . . . . . . . . . . . . . . 6-35 Eigenvalues and Singular Values . . . . . . . . . . . . . . . . . . . . . . . 6-38 Selected Bibliography . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6-40 Nondouble Data Types 7 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-2 Integer Mathematics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-4 Integer Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-4 Largest and Smallest Values for Integer Data Types . . . . . . . . 7-5 Integer Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-6 Example — Digitized Signals . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-8 Warnings for Integer Data Types . . . . . . . . . . . . . . . . . . . . . . . 7-15 Single-Precision Mathematics . . . . . . . . . . . . . . . . . . . . . . . . . 7-17 Data Type single . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-17 Single-Precision Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-18 The Function eps . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-19 Example — Writing M-Files for Different Data Types . . . . . . 7-21 Largest and Smallest Numbers of Type double and single . . . 7-23 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7-25 vi Contents Index vii viii Contents