ebook img

MATLAB Guide PDF

486 Pages·32.049 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview MATLAB Guide

OT150_Higham-Higham_FM.indd 1 11/1/2016 10:43:36 AM T E hird diTion d J. h Esmond igham University of Strathclyde Glasgow, Scotland n J. h icholas igham University of Manchester Manchester, England Society for Industrial and Applied Mathematics Philadelphia OT150_Higham-Higham_FM.indd 3 11/1/2016 10:43:37 AM Copyright © 2017 by the Society for Industrial and Applied Mathematics 10 9 8 7 6 5 4 3 2 1 All rights reserved. Printed in the United States of America. No part of this book may be reproduced, stored, or transmitted in any manner without the written permission of the publisher. For information, write to the Society for Industrial and Applied Mathematics, 3600 Market Street, 6th Floor, Philadelphia, PA 19104-2688 USA. MATLAB is a registered trademark of The MathWorks, Inc. For MATLAB product information, please contact The MathWorks, Inc., 3 Apple Hill Drive, Natick, MA 01760-2098 USA, 508-647-7000, Fax: 508-647-7001, [email protected], www.mathworks.com. No warranties, express or implied, are made by the publisher, authors, and their employers that the programs contained in this volume are free of error. They should not be relied on as the sole basis to solve a problem whose incorrect solution could result in injury to person or property. If the programs are employed in such a manner, it is at the user’s own risk and the publisher, authors, and their employers disclaim all liability for such misuse. Publisher David Marshall Acquisitions Editor Elizabeth Greenspan Developmental Editor Gina Rinelli Harris Managing Editor Kelly Thomas Production Editor David Riegelhaupt Copy Editor Sam Clark, T&T Productions Ltd, London Production Manager Donna Witzleben Production Coordinator Cally Shrader Graphic Designer Lois Sellers Library of Congress Cataloging-in-Publication Data Names: Higham, Desmond J., 1964- | Higham, Nicholas J., 1961- Title: MATLAB guide / Desmond J. Higham, University of Strathclyde, Glasgow, Scotland, United Kingdom, Nicholas J. Higham, University of Manchester, Manchester, United Kingdom. Description: Third edition. | Philadelphia : Society for Industrial and Applied Mathematics, [2016] | Series: Other titles in applied mathematics ; 150 | Includes bibliographical references and index. Identifiers: LCCN 2016039121 (print) | LCCN 2016039323 (ebook) | ISBN 9781611974652 (print) | ISBN 9781611974669 (e-book) Subjects: LCSH: MATLAB. | Numerical analysis--Data processing. Classification: LCC QA297 .H5217 2016 (print) | LCC QA297 (ebook) | DDC 518.0285/53--dc23 LC record available at https://lccn.loc.gov/2016039121 is a registered trademark. OT150_Higham-Higham_FM.indd 4 11/1/2016 10:43:37 AM In memory of our mother and father, Doris and Ken. Contents List of Figures xiii List of Tables xvii List of Program Files xix Preface xxi 1 A Brief Tutorial 1 2 Basics 23 2.1 MATLAB Desktop . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 2.2 Interaction and Script Files. . . . . . . . . . . . . . . . . . . . . . . . 23 2.3 More Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 2.4 Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 2.5 Variables and the Workspace . . . . . . . . . . . . . . . . . . . . . . . 30 3 Distinctive Features of MATLAB 35 3.1 Automatic Storage Allocation . . . . . . . . . . . . . . . . . . . . . . 35 3.2 Variable Arguments Lists . . . . . . . . . . . . . . . . . . . . . . . . . 35 3.3 Complex Arrays and Arithmetic . . . . . . . . . . . . . . . . . . . . . 37 4 Arithmetic 39 4.1 IEEE Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 4.2 Precedence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 4.3 Mathematical Functions . . . . . . . . . . . . . . . . . . . . . . . . . 42 4.4 Other Data Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 5 Matrices 47 5.1 Matrix Generation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 5.2 Subscripting and the Colon Notation . . . . . . . . . . . . . . . . . . 54 5.3 Matrix and Array Operations . . . . . . . . . . . . . . . . . . . . . . 57 5.3.1 Implicit Expansion . . . . . . . . . . . . . . . . . . . . . . . . 61 5.4 Empty Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 5.5 Matrix Manipulation . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 5.6 Data Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 6 Operators and Flow Control 71 6.1 Relational and Logical Operators . . . . . . . . . . . . . . . . . . . . 71 6.2 Flow Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 vii viii Contents 7 Program Files 83 7.1 Scripts and Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 7.2 Naming and Editing Program Files . . . . . . . . . . . . . . . . . . . 90 7.3 Working with Program Files and the MATLAB Path . . . . . . . . . 91 7.4 Startup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 7.5 Command/Function Duality . . . . . . . . . . . . . . . . . . . . . . . 93 8 Graphics 97 8.1 Two-Dimensional Graphics . . . . . . . . . . . . . . . . . . . . . . . . 97 8.1.1 Basic Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 8.1.2 Axes and Annotation . . . . . . . . . . . . . . . . . . . . . . . 102 8.1.3 Multiple Plots in a Figure . . . . . . . . . . . . . . . . . . . . 109 8.2 Three-Dimensional Graphics . . . . . . . . . . . . . . . . . . . . . . . 113 8.3 Specialized Graphs for Displaying Data . . . . . . . . . . . . . . . . . 125 8.4 Saving and Printing Figures . . . . . . . . . . . . . . . . . . . . . . . 129 8.5 On Things Not Treated . . . . . . . . . . . . . . . . . . . . . . . . . . 131 9 Linear Algebra 135 9.1 Matrix Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135 9.2 Norms and Condition Numbers . . . . . . . . . . . . . . . . . . . . . 136 9.3 Linear Equations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 9.3.1 Square System . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 9.3.2 Overdetermined System . . . . . . . . . . . . . . . . . . . . . 140 9.3.3 Underdetermined System . . . . . . . . . . . . . . . . . . . . . 141 9.4 Inverse, Pseudoinverse, and Determinant . . . . . . . . . . . . . . . . 142 9.5 LU, LDL∗, and Cholesky Factorizations . . . . . . . . . . . . . . . . . 143 9.6 QR Factorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 9.7 Singular Value Decomposition . . . . . . . . . . . . . . . . . . . . . . 146 9.8 Eigenvalue Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 9.8.1 Eigenvalues . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148 9.8.2 More about Eigenvalue Computations. . . . . . . . . . . . . . 150 9.8.3 Generalized Eigenvalues . . . . . . . . . . . . . . . . . . . . . 151 9.9 Iterative Linear Equation and Eigenproblem Solvers . . . . . . . . . . 153 9.10 Functions of a Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . 156 10 More on Functions 159 10.1 Function Handles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159 10.2 Anonymous Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . 160 10.3 Local Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 10.4 Default Input Arguments . . . . . . . . . . . . . . . . . . . . . . . . . 163 10.5 Variable Numbers of Arguments . . . . . . . . . . . . . . . . . . . . . 165 10.6 Argument Checking and Parsing . . . . . . . . . . . . . . . . . . . . . 167 10.7 Nested Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 168 10.8 Private Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 169 10.9 Recursive Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170 10.10 Global and Persistent Variables . . . . . . . . . . . . . . . . . . . . . 173 10.11 Exemplary Functions in MATLAB . . . . . . . . . . . . . . . . . . . 174 Contents ix 11 Numerical Methods: Part I 175 11.1 Polynomials and Data Fitting . . . . . . . . . . . . . . . . . . . . . . 175 11.2 Nonlinear Equations. . . . . . . . . . . . . . . . . . . . . . . . . . . . 180 11.3 Optimization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184 11.4 The Fast Fourier Transform . . . . . . . . . . . . . . . . . . . . . . . 185 12 Numerical Methods: Part II 189 12.1 Numerical Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 12.2 Ordinary Differential Equations . . . . . . . . . . . . . . . . . . . . . 193 12.2.1 Examples with Ode45 . . . . . . . . . . . . . . . . . . . . . . . 193 12.2.2 Case Study: Pursuit Problem with Event Location . . . . . . 201 12.2.3 StiffProblems,Differential-AlgebraicEquations,andtheChoice of Solver . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205 12.3 Boundary-Value Problems . . . . . . . . . . . . . . . . . . . . . . . . 213 12.4 Delay-Differential Equations . . . . . . . . . . . . . . . . . . . . . . . 221 12.5 Partial Differential Equations . . . . . . . . . . . . . . . . . . . . . . 225 13 Input and Output 233 13.1 User Input . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233 13.2 Output to the Screen . . . . . . . . . . . . . . . . . . . . . . . . . . . 234 13.3 File Input and Output . . . . . . . . . . . . . . . . . . . . . . . . . . 236 13.4 Fine Tuning the Display of Arrays . . . . . . . . . . . . . . . . . . . . 238 14 Troubleshooting 241 14.1 Errors and Assertions . . . . . . . . . . . . . . . . . . . . . . . . . . . 241 14.2 Warnings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 243 14.3 Debugging . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 245 14.4 Pitfalls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 246 15 Sparse Matrices 249 15.1 Sparse Matrix Generation . . . . . . . . . . . . . . . . . . . . . . . . 249 15.2 Linear Algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 16 More on Coding 257 16.1 Elements of Coding Style . . . . . . . . . . . . . . . . . . . . . . . . . 257 16.2 Cleaning Up . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258 16.3 Checking and Comparing Code Files . . . . . . . . . . . . . . . . . . 259 16.4 Profiling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 260 16.5 P-Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 16.6 Source Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 16.7 Live Editor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264 16.8 Creating a Toolbox . . . . . . . . . . . . . . . . . . . . . . . . . . . . 265 16.9 Distributing Code Files . . . . . . . . . . . . . . . . . . . . . . . . . . 268 16.10 Unit Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269 17 Advanced Graphics 273 17.1 Objects, Handles, and Properties . . . . . . . . . . . . . . . . . . . . 273 17.2 Root and Default Properties . . . . . . . . . . . . . . . . . . . . . . . 278 17.3 Animation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 17.4 Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281 x Contents 18 Other Data Types and Multidimensional Arrays 291 18.1 Character Vectors and Arrays . . . . . . . . . . . . . . . . . . . . . . 292 18.2 String Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 18.3 Multidimensional Arrays . . . . . . . . . . . . . . . . . . . . . . . . . 297 18.4 Categorical Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . 299 18.5 Datetime and Duration Arrays . . . . . . . . . . . . . . . . . . . . . . 300 18.6 Tables and Timetables . . . . . . . . . . . . . . . . . . . . . . . . . . 304 18.7 Structures and Cell Arrays . . . . . . . . . . . . . . . . . . . . . . . . 308 19 Object-Oriented Programming 315 19.1 Max-Plus Algebra Class . . . . . . . . . . . . . . . . . . . . . . . . . 315 19.2 Circulant Matrix Class . . . . . . . . . . . . . . . . . . . . . . . . . . 321 19.3 On Things Not Treated . . . . . . . . . . . . . . . . . . . . . . . . . . 324 20 The Symbolic Math Toolbox 325 20.1 Creating Symbolic Variables and Expressions. . . . . . . . . . . . . . 325 20.2 Equation Solving . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327 20.3 Calculus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330 20.3.1 Integration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330 20.3.2 Differentiation . . . . . . . . . . . . . . . . . . . . . . . . . . . 332 20.3.3 Solving Differentiation Equations . . . . . . . . . . . . . . . . 335 20.3.4 Taylor Expansions. . . . . . . . . . . . . . . . . . . . . . . . . 336 20.4 Linear Algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337 20.5 Polynomials and Rationals . . . . . . . . . . . . . . . . . . . . . . . . 339 20.6 Variable-Precision Arithmetic . . . . . . . . . . . . . . . . . . . . . . 343 20.7 Other Features . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 347 21 Graphs 349 21.1 Undirected Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349 21.2 Directed Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 351 22 Large Data Sets 361 22.1 Datastores . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 361 22.2 MapReduce . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364 22.3 Tall Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364 23 Optimizing Codes 369 23.1 Timing Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 369 23.2 Vectorization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370 23.3 Accessing Matrices by Column . . . . . . . . . . . . . . . . . . . . . . 372 23.4 Preallocating Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . 374 23.5 Miscellaneous Optimizations . . . . . . . . . . . . . . . . . . . . . . . 374 23.6 Illustration: Bifurcation Diagram . . . . . . . . . . . . . . . . . . . . 375 23.7 External Codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375 24 Tricks and Tips 379 24.1 Empty Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379 24.2 Exploiting Infinities . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380 24.3 Permutations. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380 24.4 Rank-1 Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 382 Contents xi 24.5 Set Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383 24.6 Subscripting Matrices as Vectors . . . . . . . . . . . . . . . . . . . . . 384 24.7 Avoiding If Statements . . . . . . . . . . . . . . . . . . . . . . . . . . 385 25 The Parallel Computing Toolbox 387 25.1 The Parfor Loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388 25.2 Asynchronous Computing with Parfeval . . . . . . . . . . . . . . . . . 392 25.3 Batch Computations . . . . . . . . . . . . . . . . . . . . . . . . . . . 393 25.4 Single Program, Multiple Data . . . . . . . . . . . . . . . . . . . . . . 395 25.5 Distributed and Codistributed Arrays . . . . . . . . . . . . . . . . . . 397 25.6 GPU Computing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 398 25.7 On Things Not Treated . . . . . . . . . . . . . . . . . . . . . . . . . . 401 26 Case Studies 403 26.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403 26.2 Brachistochrone . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 403 26.3 Small-World Networks . . . . . . . . . . . . . . . . . . . . . . . . . . 404 26.4 Performance Profiles . . . . . . . . . . . . . . . . . . . . . . . . . . . 409 26.5 Multidimensional Calculus . . . . . . . . . . . . . . . . . . . . . . . . 416 26.6 L-Systems and Turtle Graphics . . . . . . . . . . . . . . . . . . . . . 420 26.7 Black–Scholes Delta Surface . . . . . . . . . . . . . . . . . . . . . . . 422 26.8 Chutes and Ladders . . . . . . . . . . . . . . . . . . . . . . . . . . . . 425 26.9 Pythagorean Sum . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430 26.10 Fisher’s Equation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 432 A The Top 111 MATLAB Functions 439 Glossary 445 Bibliography 447 Index 459

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.