List of Figures1.1 MATLAB desktop at start of tutorial.. Being interpreted, MATLAB inevitably su ers some loss of e-ciency compared with compiled languages, but this can be mitigated by
Trang 1MATLAB Guide1
Desmond J Higham and Nicholas J Higham
Version of June 27, 2000
To be published by SIAM in 2000.
1c D J Higham and N J Higham, 2000
Trang 42.1 Interaction and Script Files 21
2.2 More Fundamentals 22
3 Distinctive Features of MATLAB 31 3.1 Automatic Storage Allocation 31
3.2 Functions with Variable Arguments Lists 31
3.3 Complex Arrays and Arithmetic 32
4 Arithmetic 35 4.1 IEEE Arithmetic 35
4.2 Precedence 37
4.3 Mathematical Functions 38
5 Matrices 39 5.1 Matrix Generation 39
5.2 Subscripting and the Colon Notation 45
5.3 Matrix and Array Operations 47
5.4 Matrix Manipulation 50
5.5 Data Analysis 52
6 Operators and Flow Control 57 6.1 Relational and Logical Operators 57
6.2 Flow Control 62
7 M-Files 67 7.1 Scripts and Functions 67
7.2 Editing M-Files 73
7.3 Working with M-Files and the MATLAB Path 73
7.4 Command/Function Duality 75
vii
Trang 5viii Contents
8.1 Two-Dimensional Graphics 77
8.1.1 Basic Plots 77
8.1.2 Axes and Annotation 80
8.1.3 Multiple Plots in a Figure 86
8.2 Three-Dimensional Graphics 88
8.3 Specialized Graphs for Displaying Data 99
8.4 Saving and Printing Figures 102
8.5 On Things Not Treated 104
9 Linear Algebra 107 9.1 Norms and Condition Numbers 107
9.2 Linear Equations 109
9.2.1 Square System 109
9.2.2 Overdetermined System 109
9.2.3 Underdetermined System 110
9.3 Inverse, Pseudo-Inverse and Determinant 111
9.4 LU and Cholesky Factorizations 112
9.5 QR Factorization 113
9.6 Singular Value Decomposition 115
9.7 Eigenvalue Problems 116
9.7.1 Eigenvalues 116
9.7.2 More about Eigenvalue Computations 118
9.7.3 Generalized Eigenvalues 118
9.8 Iterative Linear Equation and Eigenproblem Solvers 120
9.9 Functions of a Matrix 123
10 More on Functions 125 10.1 Passing a Function as an Argument 125
10.2 Subfunctions 127
10.3 Variable Numbers of Arguments 128
10.4 Global Variables 129
10.5 Recursive Functions 131
10.6 Exemplary Functions in MATLAB 132
11 Numerical Methods: Part I 135 11.1 Polynomials and Data Fitting 135
11.2 Nonlinear Equations and Optimization 139
11.3 The Fast Fourier Transform 143
12 Numerical Methods: Part II 145 12.1 Quadrature 145
12.2 Ordinary Dierential Equations 148
12.2.1 Examples withode45 149
12.2.2 Case Study: Pursuit Problem with Event Location 154
12.2.3 Sti Problems and the Choice of Solver 157
12.3 Boundary Value Problems withbvp4c 163
12.4 Partial Dierential Equations withpdepe 170
Trang 6Contents ix
13.1 User Input 177
13.2 Output to the Screen 178
13.3 File Input and Output 180
14 Troubleshooting 183 14.1 Errors and Warnings 183
14.2 Debugging 185
14.3 Pitfalls 186
15 Sparse Matrices 189 15.1 Sparse Matrix Generation 189
15.2 Linear Algebra 191
16 Further M-Files 195 16.1 Elements of M-File Style 195
16.2 Pro ling 196
17 Handle Graphics 201 17.1 Objects and Properties 201
17.2 Animation 207
17.3 Examples 209
18 Other Data Types and Multidimensional Arrays 217 18.1 Strings 217
18.2 Multidimensional Arrays 219
18.3 Structures and Cell Arrays 221
19 The Symbolic Math Toolbox 227 19.1 Equation Solving 227
19.2 Calculus 230
19.3 Linear Algebra 234
19.4 Variable Precision Arithmetic 237
19.5 Other Features 238
20 Optimizing M-Files 241 20.1 Vectorization 241
20.2 Preallocating Arrays 243
20.3 Miscellaneous Optimizations 244
20.4 Case Study: Bifurcation Diagram 244
21 Tricks and Tips 249 21.1 Empty Arrays 249
21.2 Exploiting In nities 250
21.3 Permutations 250
21.4 Rank 1 Matrices 252
21.5 Set Operations 253
21.6 Subscripting Matrices as Vectors 253
21.7 Triangular and Symmetric Matrices 254
Trang 7x Contents
A.1 MATLAB 5.0 257A.2 MATLAB 5.3 257A.3 MATLAB 6 257
Trang 8List of Figures
1.1 MATLAB desktop at start of tutorial 2
1.2 Basic 2D picture produced byplot 8
1.3 Histogram produced byhist 8
1.4 Growth of a random Fibonacci sequence 9
1.5 Plot produced bycollatz.m 11
1.6 Plot produced bycollbar.m 13
1.7 Mandelbrot set approximation produced bymandel.m 14
1.8 Phase plane plot fromode45 15
1.9 Removal process for the Sierpinski gasket 17
1.10 Level 5 Sierpinski gasket approximation fromgasket.m 17
1.11 Sierpinski gasket approximation frombarnsley.m 18
1.12 3D picture produced bysweep.m 19
2.1 Help Browser 26
2.2 Workspace Browser 28
2.3 Array Editor 28
7.1 Histogram produced byrouldist 69
7.2 MATLAB Editor/Debugger 74
8.1 Simplex-y plots Left: default Right: nondefault 78
8.2 Two nondefaultx-y plots 79
8.3 loglogexample 80
8.4 Usingaxis off 81
8.5 Use ofylim(right) to change automatic (left)y-axis limits 82
8.6 Epicycloid example 83
8.7 Legendre polynomial example, usinglegend 84
8.8 Bezier curve and control polygon 86
8.9 Example withsubplotandfplot 87
8.10 First 5 (upper) and 35 (lower) Chebyshev polynomials, plotted using fplotandchebyin Listing 7.4 88
8.11 Irregular grid of plots produced withsubplot 89
8.12 3D plot created withplot3 90
8.13 Contour plots withezcontour(upper) and contour(lower) 91
8.14 Contour plot labelled usingclabel 92
8.15 Surface plots withmeshandmeshc 93
8.16 Surface plots withsurf,surfcand waterfall 94
8.17 3D view of a 2D plot 95
8.18 Fractal landscape views 97
8.19 surfcplot of matrix containing NaNs 98
8.20 Riemann surface forz1 = 3 98
xi
Trang 9xii List of Figures
8.21 2D bar plots 100
8.22 3D bar plots 101
8.23 Histograms produced withhist 102
8.24 Pie charts 103
8.25 Area graphs 103
8.26 From the 1964 Gatlinburg Conference on Numerical Algebra 105
10.1 Koch curves created with functionkoch 133
koch 133
11.1 Left: least squares polynomial t of degree 3 Right: cubic spline Data is from 1=(x+ (1;x)2) 137
11.2 Interpolating a sine curve usinginterp1 139
11.3 Interpolation withgriddata 140
11.4 Plot produced byezplot('x-tan(x)',[-pi,pi]), grid 141
12.1 Integration of humpsfunction byquad 147
12.2 Fresnel spiral 147
12.3 Scalar ODE example 150
12.4 Pendulum phase plane solutions 152
12.5 Rossler system phase space solutions 153
12.6 Pursuit example 155
12.7 Pursuit example, with capture 156
12.8 Chemical reaction solutions Left: ode45 Right: ode15s 158
12.9 Zoom of chemical reaction solution fromode45 158
12.10 Sti ODE example, with Jacobian information supplied 161
12.11 Water droplet BVP solved bybvp4c 165
12.12 Liquid crystal BVP solved bybvp4c 168
12.13 Skipping rope eigenvalue BVP solved bybvp4c 170
12.14 Black{Scholes solution withpdepe 174
12.15 Reaction-diusion system solution withpdepe 176
15.1 Wathen matrix (left) and its Cholesky factor (right) 193
15.2 Wathen matrix (left) and its Cholesky factor (right) with symmetric reverse Cuthill{McKee ordering 193
15.3 Wathen matrix (left) and its Cholesky factor (right) with symmetric minimum degree ordering 193
16.1 profile plotformembraneexample 198
17.1 Hierarchical structure of Handle Graphics objects 202
17.2 Left: original Right: modi ed bysetcommands 203
17.3 Straightforward use of subplot 205
17.4 Modi ed version of Figure 17.3 postprocessed using Handle Graphics 205 17.5 One frame from a movie 208
17.6 Animated gure upon completion 209
17.7 Default (upper) and modi ed (lower) settings 210
17.8 Word frequency bar chart created bywfreq 212
17.9 Example with superimposed Axes created by scriptgarden 214
17.10 Diagram created bysqrt ex 214
Trang 10List of Figures xiii
18.1 cellplot(testmat) 225
19.1 taylortoolwindow 234
20.1 Approximate Brownian path 243
20.2 Numerical bifurcation diagram 246
Trang 12List of Tables
1 Versions of MATLAB xx
2.1 10*exp(1)displayed in several output formats 24
2.2 MATLAB directory structure (under Windows) 25
2.3 Command line editing keypresses 27
2.4 Information and demonstrations 29
4.1 Arithmetic operator precedence 37
4.2 Elementary and special mathematical functions 38
5.1 Elementary matrices 40
5.2 Special matrices 42
5.3 Matrices available throughgallery 43
5.4 Matrices classi ed by property 44
5.5 Elementary matrix and array operations 47
5.6 Matrix manipulation functions 51
5.7 Basic data analysis functions 55
6.1 Selected logicalis*functions 58
6.2 Operator precedence 59
8.1 Options for theplotcommand 78
8.2 Some commands for controlling the axes 81
8.3 Some of the TEX commands supported in text strings 85
8.4 2D plotting functions 89
8.5 3D plotting functions 97
9.1 Iterative linear equation solvers 122
12.1 MATLAB's ODE solvers 160
18.1 Multidimensional array functions 221
19.1 Calculus functions 234
19.2 Linear algebra functions 237
B.1 Application toolboxes marketed by The MathWorks 259
xv
Trang 14List of M-Files
1.1 Script M- lerfib.m 10
1.2 Script M-