Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Contents

PREFACE ...................................................................................... xvii

Part 1 Essentials 1
CHAPTER 1 Introduction ........................................................... 3
1.1 Using MATLAB ................................................... 5
1.1.1 Arithmetic................................................. 5
1.1.2 Variables................................................... 7
1.1.3 Mathematical functions .............................. 8
1.1.4 Functions and commands ........................... 9
1.1.5 Vectors ..................................................... 9
1.1.6 Linear equations ........................................ 11
1.1.7 Demo........................................................ 12
1.1.8 Help ......................................................... 12
1.1.9 Additional features .................................... 13
1.2 The MATLAB Desktop .......................................... 15
1.3 Sample Program .................................................. 16
1.3.1 Cut and paste ............................................ 16
1.3.2 Saving a program: script files ...................... 18
1.3.3 A program in action.................................... 19
Summary ................................................................... 21
Chapter Exercises ....................................................... 21

CHAPTER 2 MATLAB Fundamentals .......................................... 23


2.1 Variables ............................................................ 23
2.1.1 Case sensitivity ......................................... 24
2.2 The Workspace .................................................... 24
2.2.1 Adding commonly used constants to the
workspace ................................................ 25
v
vi Contents

2.3 Arrays: Vectors and Matrices ................................ 26


2.3.1 Initializing vectors: Explicit lists .................. 26
2.3.2 Initializing vectors: The colon operator ......... 27
2.3.3 The linspace function............................... 28
2.3.4 Transposing vectors ................................... 28
2.3.5 Subscripts ................................................. 28
2.3.6 Matrices ................................................... 29
2.3.7 Capturing output ....................................... 30
2.4 Vertical Motion Under Gravity ............................... 30
2.5 Operators, Expressions, and Statements ................. 32
2.5.1 Numbers ................................................... 33
2.5.2 Data types................................................. 34
2.5.3 Arithmetic operators .................................. 34
2.5.4 Operator precedence .................................. 34
2.5.5 The colon operator ..................................... 35
2.5.6 The transpose operator............................... 36
2.5.7 Arithmetic operations on arrays................... 36
2.5.8 Expressions............................................... 37
2.5.9 Statements ................................................ 38
2.5.10 Statements, commands, and functions .......... 39
2.5.11 Formula vectorization ................................. 39
2.6 Output................................................................ 42
2.6.1 The disp statement ................................... 42
2.6.2 The format command ................................ 43
2.6.3 Scale factors .............................................. 45
2.7 Repeating with for.............................................. 45
2.7.1 Square roots with Newton’s method............. 46
2.7.2 Factorials! ................................................. 47
2.7.3 Limit of a sequence .................................... 47
2.7.4 The basic for construct.............................. 48
2.7.5 for in a single line ..................................... 50
2.7.6 More general for....................................... 50
2.7.7 Avoid for loops by vectorizing! ................... 50
2.8 Decisions ............................................................ 53
2.8.1 The one-line if statement .......................... 53
2.8.2 The if-else construct............................... 55
2.8.3 The one-line if-else statement ................. 56
2.8.4 elseif ..................................................... 56
2.8.5 Logical operators ....................................... 57
Contents vii

2.8.6 Multiple ifs versus elseif ........................ 58


2.8.7 Nested ifs................................................ 59
2.8.8 Vectorizing ifs? ........................................ 60
2.8.9 The switch statement ............................... 60
2.9 Complex Numbers ............................................... 61
2.10 More on Input and Output .................................... 63
2.10.1 fprintf ................................................... 63
2.10.2 Output to a disk file with fprintf ............... 64
2.10.3 General file I/O .......................................... 65
2.10.4 Saving and loading data.............................. 65
2.11 Odds and Ends .................................................... 65
2.11.1 Variables, functions, and scripts with the
same name................................................ 65
2.11.2 The input statement ................................. 66
2.11.3 Shelling out to the operating system ............ 67
2.11.4 More Help functions ................................... 67
2.12 Programming Style............................................... 67
Summary ................................................................... 68
Chapter Exercises ....................................................... 71

CHAPTER 3 Program Design and Algorithm Development .......... 77


3.1 The Program Design Process ................................. 78
3.1.1 The projectile problem ................................ 80
3.2 Structure Plan Examples ....................................... 85
3.2.1 Quadratic equation .................................... 86
3.3 Structured Programming with Functions................. 88
Summary ................................................................... 88
Chapter Exercises ....................................................... 88

CHAPTER 4 MATLAB Functions and Data Import–Export


Utilities .................................................................... 91
4.1 Common Functions .............................................. 91
4.2 Importing and Exporting Data ............................... 96
4.2.1 The load and save commands .................... 96
4.2.2 Exporting text (ASCII) data ......................... 97
4.2.3 Importing text (ASCII) data ......................... 97
4.2.4 Exporting and importing binary data ............ 97
4.2.5 The Import Wizard ..................................... 98
4.2.6 *Low-level file I/O functions ........................ 98
4.2.7 *Other import/export functions .................... 103
viii Contents

Summary ................................................................... 103


Chapter Exercises ....................................................... 104

CHAPTER 5 Logical Vectors ........................................................ 107


5.1 Examples ............................................................ 108
5.1.1 Discontinuous graphs ................................. 108
5.1.2 Avoiding division by zero ............................ 109
5.1.3 Avoiding infinity ........................................ 110
5.1.4 Counting random numbers.......................... 111
5.1.5 Rolling dice ............................................... 112
5.2 Logical Operators ................................................ 113
5.2.1 Operator precedence .................................. 114
5.2.2 Incorrect conversion ................................... 115
5.2.3 Logical operators and vectors ...................... 115
5.3 Subscripting with Logical Vectors .......................... 116
5.4 Logical Functions................................................. 117
5.4.1 Using any and all ..................................... 118
5.5 Logical Vectors Instead of elseif Ladders ............. 119
Summary ................................................................... 122
Chapter Exercises ....................................................... 122

CHAPTER 6 Matrices of Numbers and Arrays of Strings ............. 125


6.1 Matrices ............................................................. 126
6.1.1 A concrete example.................................... 126
6.1.2 Creating matrices ...................................... 127
6.1.3 Subscripts ................................................. 127
6.1.4 The transpose operator............................... 128
6.1.5 The colon operator ..................................... 128
6.1.6 Duplicating rows and columns: Tiling .......... 132
6.1.7 Deleting rows and columns ......................... 132
6.1.8 Elementary matrices .................................. 133
6.1.9 Specialized matrices ................................... 134
6.1.10 Using MATLAB functions with matrices ....... 135
6.1.11 Manipulating matrices................................ 136
6.1.12 Array (element-by-element) operations
on matrices ............................................... 136
6.1.13 Matrices and for ....................................... 137
6.1.14 Visualization of matrices ............................. 137
Contents ix

6.1.15 Vectorizing nested fors: Loan repayment


tables ....................................................... 137
6.1.16 Multidimensional arrays ............................. 140
6.2 Matrix Operations ................................................ 140
6.2.1 Multiplication ............................................ 140
6.2.2 Exponentiation .......................................... 142
6.3 Other Matrix Functions......................................... 143
6.4 *Strings .............................................................. 143
6.4.1 Input ........................................................ 143
6.4.2 Strings as arrays ........................................ 144
6.4.3 String concatenation .................................. 144
6.4.4 ASCII codes: double and char.................... 144
6.4.5 String display with fprintf ....................... 146
6.4.6 Comparing strings ..................................... 146
6.4.7 Other string functions ................................ 146
6.5 *Two-Dimensional Strings..................................... 147
6.6 *eval and Text Macros......................................... 148
6.6.1 Error trapping with eval and lasterr......... 148
6.6.2 eval with try...catch............................. 149
Summary ................................................................... 150
Chapter Exercises ....................................................... 150

CHAPTER 7 Introduction to Graphics .......................................... 153


7.1 Basic Two-Dimensional Graphs ............................. 153
7.1.1 Labels ...................................................... 155
7.1.2 Multiple plots on the same axes................... 155
7.1.3 Line styles, markers, and color..................... 156
7.1.4 Axis limits................................................. 156
7.1.5 Multiple plots in a figure: subplot .............. 157
7.1.6 figure, clf, and cla................................. 159
7.1.7 Graphical input.......................................... 159
7.1.8 Logarithmic plots ....................................... 159
7.1.9 Polar plots ................................................. 160
7.1.10 Plotting rapidly changing mathematical
functions: fplot........................................ 161
7.1.11 The Property Editor .................................... 162
7.2 Three-Dimensional Plots ....................................... 162
7.2.1 The plot3 function .................................... 162
7.2.2 Animated 3D plots with the
comet3 function ........................................ 163
x Contents

7.2.3 Mesh surfaces ........................................... 163


7.2.4 Contour plots............................................. 165
7.2.5 Cropping a surface with NaNs ...................... 167
7.2.6 Visualizing vector fields .............................. 167
7.2.7 Matrix visualization.................................... 168
7.2.8 3D graph rotation ....................................... 169
7.2.9 Other graphics functions............................. 170
Summary ................................................................... 178
Chapter Exercises ....................................................... 179

CHAPTER 8 Loops ....................................................................... 185


8.1 Determinate Repetition with for ........................... 185
8.1.1 Binomial coefficient .................................... 185
8.1.2 Update processes....................................... 186
8.1.3 Nested fors .............................................. 188
8.2 Indeterminate Repetition with while ..................... 188
8.2.1 A guessing game ....................................... 188
8.2.2 The while statement ................................. 189
8.2.3 Doubling time of an investment ................... 190
8.2.4 Prime numbers .......................................... 191
8.2.5 Projectile trajectory .................................... 192
8.2.6 break ....................................................... 194
8.2.7 Menus ...................................................... 195
Summary ................................................................... 196
Chapter Exercises ....................................................... 197

CHAPTER 9 Errors and Pitfalls .................................................... 201


9.1 Syntax Errors ...................................................... 201
9.1.1 Incompatible vector sizes ............................ 202
9.1.2 Name hiding ............................................. 202
9.2 Logic Errors ........................................................ 202
9.3 Rounding Error .................................................... 203
Summary ................................................................... 204
Chapter Exercises ....................................................... 204

CHAPTER 10 Function M-files ....................................................... 207


10.1 Inline Objects: Harmonic Oscillators ...................... 207
10.2 Function M-files: Newton’s Method Revisited ......... 209
10.3 Basic Rules ......................................................... 210
10.3.1 Subfunctions ............................................. 215
Contents xi

10.3.2 Private functions ........................................ 215


10.3.3 P-code files................................................ 215
10.3.4 Improving M-file performance with the
profiler...................................................... 215
10.4 Function Handles................................................. 216
10.5 Command/Function Duality................................... 217
10.6 Function Name Resolution .................................... 218
10.7 Debugging M-files ............................................... 219
10.7.1 Debugging a script..................................... 219
10.7.2 Debugging a function ................................. 221
10.8 Recursion............................................................ 221
Summary ................................................................... 222
Chapter Exercises ....................................................... 224

CHAPTER 11 Vectors as Arrays and *Advanced Data


Structures ................................................................ 227
11.1 Update Processes................................................. 227
11.1.1 Unit time steps .......................................... 228
11.1.2 Non–unit time steps ................................... 230
11.1.3 Using a function......................................... 231
11.1.4 Exact solution............................................ 233
11.2 Frequencies, Bar Charts, and Histograms ............... 233
11.2.1 A random walk .......................................... 233
11.2.2 Histograms ............................................... 235
11.3 *Sorting .............................................................. 235
11.3.1 Bubble sort ............................................... 236
11.3.2 MATLAB’s sort ........................................ 237
11.4 *Structures.......................................................... 238
11.5 *Cell Arrays ........................................................ 240
11.5.1 Assigning data to cell arrays ....................... 240
11.5.2 Accessing data in cell arrays ....................... 242
11.5.3 Using cell arrays ........................................ 242
11.5.4 Displaying and visualizing cell arrays ........... 243
11.6 *Classes and Objects............................................ 244
Summary ................................................................... 244

CHAPTER 12 *More Graphics ........................................................ 245


12.1 Handle Graphics .................................................. 245
12.1.1 Getting handles ......................................... 246
12.1.2 Changing graphics object properties ............ 247
xii Contents

12.1.3 A vector of handles .................................... 248


12.1.4 Graphics object creation functions ............... 249
12.1.5 Parenting .................................................. 249
12.1.6 Positioning figures ..................................... 250
12.2 Editing Plots ....................................................... 251
12.2.1 Plot edit mode ........................................... 251
12.2.2 Property Editor .......................................... 252
12.3 Animation ........................................................... 253
12.3.1 Animation with Handle Graphics ................. 254
12.4 Colormaps .......................................................... 256
12.4.1 Surface plot color ....................................... 258
12.4.2 Truecolor .................................................. 259
12.5 Lighting and Camera............................................ 259
12.6 Saving, Printing, and Exporting Graphs .................. 260
12.6.1 Saving and opening figure files .................... 260
12.6.2 Printing a graph......................................... 260
12.6.3 Exporting a graph ...................................... 261
Summary ................................................................... 261
Chapter Exercises ....................................................... 262

CHAPTER 13 *Graphical User Interfaces (GUIs) ............................ 263


13.1 Basic Structure of a GUI ........................................ 263
13.2 A First Example: Getting the Time......................... 264
13.3 Newton’s Method Yet Again.................................. 268
13.4 Axes on a GUI ..................................................... 271
13.5 Adding Color to a Button ...................................... 272
Summary ................................................................... 273

Part 2 Applications 275


CHAPTER 14 Dynamical Systems .................................................. 277
14.1 Cantilever Beam .................................................. 278
14.2 Electric Current ................................................... 279
14.3 Free Fall ............................................................. 281
14.4 Projectile with Friction ......................................... 291
Summary ................................................................... 294
Chapter Exercises ....................................................... 295

CHAPTER 15 Simulation ................................................................ 297


15.1 Random Number Generation ................................. 297
15.1.1 Seeding rand ............................................ 298
Contents xiii

15.2 Flipping Coins ..................................................... 298


15.3 Rolling Dice......................................................... 299
15.4 Bacterium Division ............................................... 300
15.5 A Random Walk ................................................... 300
15.6 Traffic Flow......................................................... 302
15.7 Normal (Gaussian) Random Numbers ..................... 305
Summary ................................................................... 305
Chapter Exercises ....................................................... 306

CHAPTER 16 *More Matrices ........................................................ 309


16.1 Leslie Matrices: Population Growth ....................... 309
16.2 Markov Processes ................................................ 313
16.2.1 A random walk .......................................... 313
16.3 Linear Equations ................................................. 315
16.3.1 MATLAB’s solution .................................... 316
16.3.2 The residual .............................................. 317
16.3.3 Overdetermined systems ............................ 317
16.3.4 Underdetermined systems .......................... 318
16.3.5 Ill-conditioned systems ............................... 318
16.3.6 Matrix division .......................................... 319
16.4 Sparse Matrices ................................................... 320
Summary ................................................................... 323
Chapter Exercises ....................................................... 323

CHAPTER 17 *Introduction to Numerical Methods ........................ 325


17.1 Equations ........................................................... 325
17.1.1 Newton’s method....................................... 325
17.1.2 The Bisection method ................................. 328
17.1.3 The fzero and roots functions .................. 329
17.2 Integration .......................................................... 330
17.2.1 The Trapezoidal rule .................................. 330
17.2.2 Simpson’s rule ........................................... 331
17.2.3 The quad function...................................... 332
17.3 Numerical Differentiation...................................... 332
17.3.1 The diff function...................................... 333
17.4 First-Order Differential Equations .......................... 334
17.4.1 Euler’s method .......................................... 334
17.4.2 Example: Bacteria colony growth................. 335
17.4.3 Alternative subscript notation ..................... 336
17.4.4 A predictor-corrector method....................... 338
xiv Contents

17.5 Linear Ordinary Differential Equations ................... 339


17.6 Runge-Kutta Methods .......................................... 339
17.6.1 A single differential equation ...................... 339
17.6.2 Systems of differential equations: Chaos ...... 340
17.6.3 Passing additional parameters to an ODE
solver ....................................................... 343
17.7 A Partial Differential Equation ............................... 344
17.7.1 Heat conduction ........................................ 344
17.8 Other Numerical Methods ..................................... 348
Summary ................................................................... 349
Chapter Exercises ....................................................... 349

CHAPTER 18 Toolboxes That Come with MATLAB


(online chapter: www.elsevierdirect.com/companions/978012374883-6)

APPENDIX A Syntax: Quick Reference ......................................... 353


A.1 Expressions ........................................................ 353
A.2 Function M-files................................................... 353
A.3 Graphics ............................................................. 353
A.4 if and switch .................................................... 354
A.5 for and while .................................................... 355
A.6 Input/output........................................................ 356
A.7 load/save .......................................................... 356
A.8 Vectors and Matrices............................................ 357

APPENDIX B Operators ................................................................ 359

APPENDIX C Command and Function: Quick Reference .............. 361


C.1 General-Purpose Commands ................................. 361
C.1.1 Managing variables and the workspace ........ 361
C.1.2 Files and the operating system .................... 361
C.1.3 Controlling the Command Window .............. 362
C.1.4 Starting and quitting MATLAB .................... 362
C.2 Logical Functions................................................. 362
C.3 MATLAB Programming Tools ................................ 362
C.3.1 Interactive input ........................................ 363
C.4 Matrices ............................................................. 363
C.4.1 Special variables and constants ................... 363
C.4.2 Time and date ........................................... 363
C.4.3 Matrix manipulation ................................... 363
C.4.4 Specialized matrices ................................... 364
Contents xv

C.5 Mathematical Functions ....................................... 364


C.6 Matrix Functions ................................................. 365
C.7 Data Analysis ...................................................... 365
C.8 Polynomial Functions ........................................... 365
C.9 Function Functions .............................................. 366
C.10 Sparse Matrix Functions ....................................... 366
C.11 Character String Functions.................................... 366
C.12 File I/O Functions ................................................ 366
C.13 2D Graphics ........................................................ 366
C.14 3D Graphics ........................................................ 367
C.15 General .............................................................. 367

APPENDIX D ASCII Character Codes ............................................ 369

APPENDIX E Solutions to Selected Exercises ............................... 371

INDEX .......................................................................................... 383

You might also like