|
发表于 2011-8-14 03:08:36
|
显示全部楼层
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
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
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
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
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
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
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
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 |
|