A Guide to MATLAB. for Beginners and Experienced Users Brian.R.Hunt Ronald L.Lipsman Jonathan M.Rosenberg With Kevin R.Coombes,John E.Osborn,Garrett J.Stuck CAMBRIDGE more information -www.cambridge.org/9780521803809
A Guide to MATLAB This book is a short,focused introduction to MATLAB,a comprehen- sive software system for mathematics and technical computing.It will be useful to both beginning and experienced users.It contains concise explanations of essential MATLAB commands,as well as easily under- stood instructions for using MATLAB's programming features,graphi- cal capabilities,and desktop interface.It also includes an introduction to SIMULINK,a companion to MATLAB for system simulation. Written for MATLAB 6,this book can also be used with earlier(and later)versions of MATLAB.This book contains worked-out examples of applications of MATLAB to interesting problems in mathematics, engineering,economics,and physics.In addition,it contains explicit instructions for using MATLAB's Microsoft Word interface to produce polished,integrated,interactive documents for reports,presentations, or online publishing. This book explains everything you need to know to begin using MATLAB to do all these things and more.Intermediate and advanced users will find useful information here,especially if they are making the switch to MATLAB 6 from an earlier version. Brian R.Hunt is an Associate Professor of Mathematics at the Univer- sity of Maryland.Professor Hunt has coauthored four books on math- ematical software and more than 30 journal articles.He is currently involved in research on dynamical systems and fractal geometry. Ronald L.Lipsman is a Professor of Mathematics and Associate Dean of the College of Computer,Mathematical,and Physical Sciences at the University of Maryland.Professor Lipsman has coauthored five books on mathematical software and more than 70 research articles.Professor Lipsman was the recipient of both the NATO and Fulbright Fellowships. Jonathan M.Rosenberg is a Professor of Mathematics at the Univer- sity of Maryland.Professor Rosenberg is the author of two books on mathematics (one of them coauthored by R.Lipsman and K.Coombes) and the coeditor of Novikou Conjectures,Index Theorems,and Rigidity, a two-volume set from the London Mathematical Society Lecture Note Series(Cambridge University Press,1995)
A Guide to MATLAB This book is a short, focused introduction to MATLAB, a comprehensive software system for mathematics and technical computing. It will be useful to bothbeginning and experienced users. It contains concise explanations of essential MATLAB commands, as well as easily understood instructions for using MATLAB’s programming features, graphical capabilities, and desktop interface. It also includes an introduction to SIMULINK, a companion to MATLAB for system simulation. Written for MATLAB 6, this book can also be used with earlier (and later) versions of MATLAB. This book contains worked-out examples of applications of MATLAB to interesting problems in mathematics, engineering, economics, and physics. In addition, it contains explicit instructions for using MATLAB’s Microsoft Word interface to produce polished, integrated, interactive documents for reports, presentations, or online publishing. This book explains everything you need to know to begin using MATLAB to do all these things and more. Intermediate and advanced users will find useful information here, especially if they are making the switch to MATLAB 6 from an earlier version. Brian R. Hunt is an Associate Professor of Mathematics at the University of Maryland. Professor Hunt has coauthored four books on mathematical software and more than 30 journal articles. He is currently involved in researchon dynamical systems and fractal geometry. Ronald L. Lipsman is a Professor of Mathematics and Associate Dean of the College of Computer, Mathematical, and Physical Sciences at the University of Maryland. Professor Lipsman has coauthored five books on mathematical software and more than 70 research articles. Professor Lipsman was the recipient of both the NATO and Fulbright Fellowships. Jonathan M. Rosenberg is a Professor of Mathematics at the University of Maryland. Professor Rosenberg is the author of two books on mathematics (one of them coauthored by R. Lipsman and K. Coombes) and the coeditor of Novikov Conjectures, Index Theorems, and Rigidity, a two-volume set from the London Mathematical Society Lecture Note Series (Cambridge University Press, 1995)
A Guide to matlaB for Beginners and Experienced Users Brian R.Hunt Ronald L.Lipsman Jonathan M.Rosenberg with Kevin R.Coombes,John E.Osborn,and Garrett J.Stuck CAMBRIDGE UNIVERSITY PRESS
A Guide to MATLAB for Beginners and Experienced Users Brian R. Hunt Ronald L. Lipsman Jonathan M. Rosenberg with Kevin R. Coombes, John E. Osborn, and Garrett J. Stuck
CAMBRIDGE UNIVERSITY PRESS Cambridge,New York,Melbourne,Madrid,Cape Town,Singapore,Sao Paulo Cambridge University Press The Edinburgh Building,Cambridge CB2 2RU,United Kingdom Published in the United States of America by Cambridge University Press,New York www.cambridge.org Information on this title:www.cambridge.org/9780521803809 B.Hunt,R.Lipsman,J.Rosenberg,K.Coombes,J.Osborn,G.Stuck 2001 This book is in copyright.Subject to statutory exception and to the provision of relevant collective licensing agreements,no reproduction of any part may take place without the written permission of Cambridge University Press. First published in print format 2001 ISBN-13 978-0-5I1-07792-o eBook (NetLibrary) ISBN-IO o-SI1-07792-o eBook (NetLibrary) ISBN-13 978-0-521-80380-9 hardback ISBN-I0 o-521-80380-2 hardback ISBN-13 978-0-521-00859-4 paperback ISBN-I0 o-521-00859-x paperback Cambridge University Press has no responsibility for the persistence or accuracy of URLs for external or third-party internet websites referred to in this book,and does not guarantee that any content on such websites is,or will remain,accurate or appropriate. MATLABO®,Simulink®,and Handle Graphics(®are registered trademarks of The Math Works,Inc.Microsoft®,MS-DOS®,and Windows(®are registered trademarks of Microsoft Corporation.Many other proprietary names used in this book are registered trademarks. Portions of this book were adapted from "Differential Equations with MATLAB"by Kevin R.Coombes,Brian R.Hunt,Ronald L.Lipsman,John E.Osborn,and Garrett J. Stuck,copyright 2000,John Wiley Sons,Inc.Adapted by permission of John Wiley Sons,Inc
Cambridge, New York, Melbourne, Madrid, Cape Town, Singapore, São Paulo Cambridge University Press The Edinburgh Building, Cambridge , United Kingdom First published in print format - ---- - ---- - ---- © B. Hunt, R. Lipsman, J. Rosenberg, K. Coombes, J. Osborn, G. Stuck 2001 MATLAB®, Simulink®, and Handle Graphics® are registered trademarks of The MathWorks, Inc. Microsoft®, MS-DOS®, and Windows® are registered trademarks of Microsoft Corporation. Many other proprietary names used in this book are registered trademarks. Portions of this book were adapted from “Differential Equations with MATLAB” by Kevin R. Coombes, Brian R. Hunt, Ronald L. Lipsman, John E. Osborn, and Garrett J. Stuck, copyright © 2000, John Wiley & Sons, Inc. Adapted by permission of John Wiley & Sons, Inc. 2001 Information on this title: www.cambridge.org/9780521803809 This book is in copyright. Subject to statutory exception and to the provision of relevant collective licensing agreements, no reproduction of any part may take place without the written permission of Cambridge University Press. - --- - --- - --- Cambridge University Press has no responsibility for the persistence or accuracy of s for external or third-party internet websites referred to in this book, and does not guarantee that any content on such websites is, or will remain, accurate or appropriate. Published in the United States of America by Cambridge University Press, New York www.cambridge.org hardback paperback paperback eBook (NetLibrary) eBook (NetLibrary) hardback
Contents at a Glance Preface page xiii 1 Getting Started 1 2 MATLAB Basics 8 3 Interacting with MATLAB 31 Practice Set A:Algebra and Arithmetic 48 4 Beyond the Basics 50 5 MATLAB Graphics 67 Practice Set B:Calculus,Graphics,and Linear Algebra 86 6 M-Books 91 7 MATLAB Programming 101 8 SIMULINK and GUls 121 9 Applications 136 Practice Set C:Developing Your MATLAB Skills 204 10 MATLAB and the Internet 214 11 Troubleshooting 218 Solutions to the Practice Sets 235 Glossary 299 Index 317
Contents at a Glance Preface page xiii 1 Getting Started 1 2 MATLAB Basics 8 3 Interacting with MATLAB 31 Practice Set A: Algebra and Arithmetic 48 4 Beyond the Basics 50 5 MATLAB Graphics 67 Practice Set B: Calculus, Graphics, and Linear Algebra 86 6 M-Books 91 7 MATLAB Programming 101 8 SIMULINK and GUIs 121 9 Applications 136 Practice Set C: Developing Your MATLAB Skills 204 10 MATLAB and the Internet 214 11 Troubleshooting 218 Solutions to the Practice Sets 235 Glossary 299 Index 317 v
Contents Preface page xiii 1 Getting Started 1 Platforms and Versions 1 Installation and Location 2 Starting MATLAB 2 Typing in the Command Window 3 Online Help 4 Interrupting Calculations 5 MATLAB Windows 6 Ending a Session 7 2 MATLAB Basics 8 Input and Output 8 Arithmetic 8 Algebra 10 Symbolic Expressions,Variable Precision,and Exact Arithmetic 11 Managing Variables 13 Errors in Input 14 Online Help 15 Variables and Assignments 16 Solving Equations 17 Vectors and Matrices 20 Vectors 21 Matrices 23 Suppressing Output 24 Functions 24 vii
Contents Preface page xiii 1 Getting Started 1 Platforms and Versions 1 Installation and Location 2 Starting MATLAB 2 Typing in the Command Window 3 Online Help 4 Interrupting Calculations 5 MATLAB Windows 6 Ending a Session 7 2 MATLAB Basics 8 Input and Output 8 Arithmetic 8 Algebra 10 Symbolic Expressions, Variable Precision, and Exact Arithmetic 11 Managing Variables 13 Errors in Input 14 Online Help 15 Variables and Assignments 16 Solving Equations 17 Vectors and Matrices 20 Vectors 21 Matrices 23 Suppressing Output 24 Functions 24 vii
vili Contents Built-in Functions 24 User-Defined Functions 25 Graphics 26 Graphing with ezplot 26 Modifying Graphs 27 Graphing with plot 28 Plotting Multiple Curves 30 3 Interacting with MATLAB 31 The MATLAB Interface 31 The Desktop 31 Menu and Tool Bars 33 The Workspace 33 The Working Directory 34 Using the Command Window 35 M-Files 36 Script M-Files 37 Function M-Files 39 Loops 41 Presenting Your Results 41 Diary Files 42 Presenting Graphics 43 Pretty Printing 45 A General Procedure 45 Fine-Tuning Your M-Files 46 Practice Set A:Algebra and Arithmetic 48 4 Beyond the Basics 50 Suppressing Output 50 Data Classes 51 String Manipulation 53 Symbolic and Floating Point Numbers 53 Functions and Expressions 54 Substitution 56 More about M-Files 56 Variables in Script M-Files 56 Variables in Function M-Files 57 Structure of Function M-Files 57
viii Contents Built-in Functions 24 User-Defined Functions 25 Graphics 26 Graphing with ezplot 26 Modifying Graphs 27 Graphing with plot 28 Plotting Multiple Curves 30 3 Interacting with MATLAB 31 The MATLAB Interface 31 The Desktop 31 Menu and Tool Bars 33 The Workspace 33 The Working Directory 34 Using the Command Window 35 M-Files 36 Script M-Files 37 Function M-Files 39 Loops 41 Presenting Your Results 41 Diary Files 42 Presenting Graphics 43 Pretty Printing 45 A General Procedure 45 Fine-Tuning Your M-Files 46 Practice Set A: Algebra and Arithmetic 48 4 Beyond the Basics 50 Suppressing Output 50 Data Classes 51 String Manipulation 53 Symbolic and Floating Point Numbers 53 Functions and Expressions 54 Substitution 56 More about M-Files 56 Variables in Script M-Files 56 Variables in Function M-Files 57 Structure of Function M-Files 57
Contents ix Complex Arithmetic 58 More on Matrices 59 Solving Linear Systems 60 Calculating Eigenvalues and Eigenvectors 60 Doing Calculus with MATLAB 61 Differentiation 61 Integration 62 Limits 63 Sums and Products 64 Taylor Series 65 Default Variables 65 5 MATLAB Graphics 67 Two-Dimensional Plots 67 Parametric Plots 67 Contour Plots and Implicit Plots 69 Field Plots 71 Three-Dimensional Plots 72 Curves in Three-Dimensional Space 72 Surfaces in Three-Dimensional Space 73 Special Effects 75 Combining Figures in One Window 76 Animations 77 Customizing and Manipulating Graphics 78 Change of Viewpoint 80 Change of Plot Style 80 Full-Fledged Customization 82 Quick Plot Editing in the Figure Window 84 Sound 85 Practice Set B:Calculus,Graphics,and Linear Algebra 86 M-Books 91 Enabling M-Books 92 Starting M-Books 93 Working with M-Books 95 Editing Input 95 The Notebook Menu 96
Contents ix Complex Arithmetic 58 More on Matrices 59 Solving Linear Systems 60 Calculating Eigenvalues and Eigenvectors 60 Doing Calculus withMATLAB 61 Differentiation 61 Integration 62 Limits 63 Sums and Products 64 Taylor Series 65 Default Variables 65 5 MATLAB Graphics 67 Two-Dimensional Plots 67 Parametric Plots 67 Contour Plots and Implicit Plots 69 Field Plots 71 Three-Dimensional Plots 72 Curves in Three-Dimensional Space 72 Surfaces in Three-Dimensional Space 73 Special Effects 75 Combining Figures in One Window 76 Animations 77 Customizing and Manipulating Graphics 78 Change of Viewpoint 80 Change of Plot Style 80 Full-Fledged Customization 82 Quick Plot Editing in the Figure Window 84 Sound 85 Practice Set B: Calculus, Graphics, and Linear Algebra 86 6 M-Books 91 Enabling M-Books 92 Starting M-Books 93 Working withM-Books 95 Editing Input 95 The Notebook Menu 96
X Contents M-Book Graphics 97 More Hints for Effective Use of M-Books 98 A Warning 99 7 MATLAB Programming 101 Branching 101 Branching with if 102 Logical Expressions 104 Branching with switch 108 More about Loops 109 Open-Ended Loops 110 Breaking from a Loop 111 Other Programming Commands 112 Subfunctions 112 Commands for Parsing Input and Output 112 User Input and Screen Output 114 Evaluation 116 Debugging 117 Interacting with the Operating System 118 Calling External Programs 118 File Input and Output 119 8 *SIMULINK and GUls 121 SIMULINK 121 Graphical User Interfaces(GUIs) 127 GUI Layout and GUIDE 127 Saving and Running a GUI 130 GUI Callback Functions 132 9 Applications 136 Illuminating a Room 137 One 300-Watt Bulb 137 Two 150-Watt Bulbs 138 Three 100-Watt Bulbs 143 Mortgage Payments 145 Monte Carlo Simulation 149 Population Dynamics 156 Exponential Growth and Decay 157
x Contents M-Book Graphics 97 More Hints for Effective Use of M-Books 98 A Warning 99 7 MATLAB Programming 101 Branching 101 Branching with if 102 Logical Expressions 104 Branching with switch 108 More about Loops 109 Open-Ended Loops 110 Breaking from a Loop 111 Other Programming Commands 112 Subfunctions 112 Commands for Parsing Input and Output 112 User Input and Screen Output 114 Evaluation 116 Debugging 117 Interacting withthe Operating System 118 Calling External Programs 118 File Input and Output 119 8 SIMULINK and GUIs 121 SIMULINK 121 Graphical User Interfaces (GUIs) 127 GUI Layout and GUIDE 127 Saving and Running a GUI 130 GUI Callback Functions 132 9 Applications 136 Illuminating a Room 137 One 300-Watt Bulb 137 Two 150-Watt Bulbs 138 Three 100-Watt Bulbs 143 Mortgage Payments 145 Monte Carlo Simulation 149 Population Dynamics 156 Exponential Growthand Decay 157
Contents xi Logistic Growth 159 Rerunning the Model with SIMULINK 166 Linear Economic Models 168 Linear Programming 173 The360°Pendulum 180 Numerical Solution of the Heat Equation 184 A Finite Difference Solution 185 The Case of Variable Conductivity 189 A SIMULINK Solution 191 Solution with pdepe 194 ★A Model of Traffic Flow 196 Practice Set C:Developing Your MATLAB Skills 204 10 MATLAB and the Internet 214 MATLAB Help on the Internet 214 Posting MATLAB Programs and Output 215 M-Files,M-Books,Reports,and HTML Files 215 Configuring Your Web Browser 216 Microsoft Internet Explorer 216 Netscape Navigator 216 11 Troubleshooting 218 Common Problems 218 Wrong or Unexpected Output 218 Syntax Error 220 Spelling Error 223 Error Messages When Plotting 223 A Previously Saved M-File Evaluates Differently 224 Computer Won't Respond 226 The Most Common Mistakes 226 Debugging Techniques 227 Solutions to the Practice Sets 235 Practice Set A 235 Practice Set B 246 Practice Set C 266
Contents xi Logistic Growth159 Rerunning the Model with SIMULINK 166 Linear Economic Models 168 Linear Programming 173 The 360◦ Pendulum 180 Numerical Solution of the Heat Equation 184 A Finite Difference Solution 185 The Case of Variable Conductivity 189 A SIMULINK Solution 191 Solution with pdepe 194 A Model of Traffic Flow 196 Practice Set C: Developing Your MATLAB Skills 204 10 MATLAB and the Internet 214 MATLAB Help on the Internet 214 Posting MATLAB Programs and Output 215 M-Files, M-Books, Reports, and HTML Files 215 Configuring Your Web Browser 216 Microsoft Internet Explorer 216 Netscape Navigator 216 11 Troubleshooting 218 Common Problems 218 Wrong or Unexpected Output 218 Syntax Error 220 Spelling Error 223 Error Messages When Plotting 223 A Previously Saved M-File Evaluates Differently 224 Computer Won’t Respond 226 The Most Common Mistakes 226 Debugging Techniques 227 Solutions to the Practice Sets 235 Practice Set A 235 Practice Set B 246 Practice Set C 266