iv CONTENTS 3.6 Sequencing,branching and looping 26 3.7 A mini-summary before the examples...···....···,.. 。。 27 3.8 Some examples.................·....·.·:.·.··· 27 3.8.1 The Breakfast algorithm,or,Bielajew's Sunday Morning Internationally- famous pancakes......... 28 3.8.2 Solve for x:Ax2+Bx+C=0 where A.B.C are arbitrary constants 29 3.8.3 Iteration:A summing loop.. 33 3.8.4 Iteration:A product loop 36 3.9 An aside on computer architecture 40 3.9.1 What does S=S+1 mean?. 40 3.10 Problems..... 3.11 Projects..·. 47 4 Getting started in C++ 49 4.1 Simple input/output (I/O):A first program in C++.... 49 4.2 Compiling,linking,loading and running 52 4.3 Declaring and initializing variables 55 4.4 Integer math in C++.·..··. 57 4.5 Floating point math in C++... 59 4.6 The if/else if/else construct... 60 4.7 Logical expressions.···· 63 4.7.1 Logical expressions with AND or OR 66 4.7.2 Mixed arithmetic and logical expressions 67 4.8 Problems.... 69 4.9 Projects·· 73 5 Loops 83 5.1 The while loop . 83 5.2 The do/while loop 86 5.3 The for loop 89
iv CONTENTS 3.6 Sequencing, branching and looping . . . . . . . . . . . . . . . . . . . . . . . 26 3.7 A mini-summary before the examples . . . . . . . . . . . . . . . . . . . . . . 27 3.8 Some examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 3.8.1 The Breakfast algorithm, or, Bielajew’s Sunday Morning Internationallyfamous pancakes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 3.8.2 Solve for x: Ax2 + Bx + C = 0 where A, B, C are arbitrary constants 29 3.8.3 Iteration: A summing loop . . . . . . . . . . . . . . . . . . . . . . . . 33 3.8.4 Iteration: A prod uct loop . . . . . . . . . . . . . . . . . . . . . . . . 36 3.9 An asid e on computer architecture . . . . . . . . . . . . . . . . . . . . . . . 40 3.9.1 What does S = S + 1 mean? . . . . . . . . . . . . . . . . . . . . . . . 40 3.10 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 3.11 Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 4 Getting started in C++ 49 4.1 Simple input/output (I/O): A first program in C++ . . . . . . . . . . . . . . 49 4.2 Compiling, linking, loading and running . . . . . . . . . . . . . . . . . . . . 52 4.3 Declaring and initializing variables . . . . . . . . . . . . . . . . . . . . . . . 55 4.4 Integer math in C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 4.5 Floating point math in C++ . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 4.6 The if/else if/else construct . . . . . . . . . . . . . . . . . . . . . . . . . 60 4.7 Logical expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 4.7.1 Logical expressions with AND or OR . . . . . . . . . . . . . . . . . . 66 4.7.2 Mixed arithmetic and logical expressions . . . . . . . . . . . . . . . . 67 4.8 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 4.9 Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 5 L oops 83 5.1 The while loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 5.2 The do/while loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 5.3 The for loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
CONTENTS 5.4 Problems....... 94 5.5 Projects······· 111 6 Early Abstraction-Functions 123 6.1 Motivation for functions.... 123 6.2 User-defined functions........... 129 6.3 Example:A problem tackled with teamwork.... 134 6.4 Call by value,call by reference,reference parameters 138 6.4.1 The address of a variable..... 138 6.4.2 Call-by-value us.call-by-reference 140 6.5 The rules of scope..... 145 6.6 Problems.····. 153 6.7 Projects······ 160 7 More Variable Types,Data Abstraction 167 7.1 Representation of floating-point numbers ... 167 7.1.1 When is one not one?Floating point anomalies. 174 7.2char:the character variable...:·.·· 175 7.2.1 Character strings;the string class 177 7.3 The vector class..·...·..· 179 7.3.1 Introduction to vectors... 179 7.3.2 Declaring and using vectors 180 7.3.3 Vector syntax and rules:... 182 7.3.4 Vectors and functions.. 187 7.4 Problems........·. 189 8 More Data Abstraction,Arrays,Structures 199 8.1 Arrays·············· 199 8.1.1 Declaring a 2-dimensional array 200 8.1.2 Initializing a 2-dimensional array.... 201
CONTENTS v 5.4 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 5.5 Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 6 Early Abstraction—Functions 123 6.1 Motivation for functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 6.2 User-d efined functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 6.3 Example: A problem tackled with teamwork . . . . . . . . . . . . . . . . . . 134 6.4 Call by value, call by reference, reference parameters . . . . . . . . . . . . . 138 6.4.1 The ad d ress of a variable . . . . . . . . . . . . . . . . . . . . . . . . . 138 6.4.2 Call-by-value vs. call-by-reference . . . . . . . . . . . . . . . . . . . . 140 6.5 The rules of scope . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145 6.6 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153 6.7 Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160 7 More Variable Types, Data Abstraction 167 7.1 Representation of floating-point numbers . . . . . . . . . . . . . . . . . . . . 167 7.1.1 When is one not one? Floating point anomalies. . . . . . . . . . . . . 174 7.2 char: the character variable . . . . . . . . . . . . . . . . . . . . . . . . . . . 175 7.2.1 Character strings; the string class . . . . . . . . . . . . . . . . . . . . 177 7.3 The vector class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 179 7.3.1 Introd uction to vectors . . . . . . . . . . . . . . . . . . . . . . . . . . 179 7.3.2 Declaring and using vectors . . . . . . . . . . . . . . . . . . . . . . . 180 7.3.3 Vector syntax and rules: . . . . . . . . . . . . . . . . . . . . . . . . . 182 7.3.4 Vectors and functions . . . . . . . . . . . . . . . . . . . . . . . . . . . 187 7.4 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189 8 More Data Abstraction, Arrays, Structures 199 8.1 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199 8.1.1 Declaring a 2-d imensional array . . . . . . . . . . . . . . . . . . . . . 200 8.1.2 Initializing a 2-d imensional array . . . . . . . . . . . . . . . . . . . . 201
vi CONTENTS 8.1.3 Passing a two-dimensional array to a function .... 204 8.2 Character arrays··················· 209 8.2.1 One dimensional character arrays 209 8.2.2 Two dimensional character arrays . 211 8.3 Structures·················· 211 8.3.1 The structure definition..... 212 8.3.2 Where can structures be defined? 213 8.3.3 The members of a structure··.·· 213 8.3.4 Declaring structure variables..... 213 8.3.5 Assigning values to structure members 213 8.3.6 Re-assigning values of structure members... 214 8.3.7 Function call-by-value of a structure 214 8.3.8 Function call-by-reference to a structure... 215 8.3.9 Structure arrays...·················· 215 8.3.10 Example using structures and arrays:Ion transport.......... 218 8.4 Problems........······ 222 8.5 Projects..··. 228 9 Miscellaneous Topics 239 g.1 Generating random numbers.·.·····.···· 239 9.1.1 Example:Integrating functions by random sampling 246 9.2 Simple Sorting:The bubble or sinking sort....... 248 9.3 Recursion-A function calling itself ........ 249 9.4 Input and Output using files...··. 252 9.4.1 File and stream handling in C++..... 252 9.4.2 Creating sequential access files and writing to them.·,..·.··· 255 9.4.3 Reading from sequential access files 258 9.5 Command line arguments 261 9.6 Problems..·. 264
vi CONTENTS 8.1.3 Passing a two-d imensional array to a function . . . . . . . . . . . . . 204 8.2 Character arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209 8.2.1 One d imensional character arrays . . . . . . . . . . . . . . . . . . . . 209 8.2.2 Two d imensional character arrays . . . . . . . . . . . . . . . . . . . . 211 8.3 Structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 8.3.1 The structure d efinition . . . . . . . . . . . . . . . . . . . . . . . . . 212 8.3.2 Where can structures be d efined ? . . . . . . . . . . . . . . . . . . . . 213 8.3.3 The members of a structure . . . . . . . . . . . . . . . . . . . . . . . 213 8.3.4 Declaring structure variables . . . . . . . . . . . . . . . . . . . . . . . 213 8.3.5 Assigning values to structure members . . . . . . . . . . . . . . . . . 213 8.3.6 Re-assigning values of structure members . . . . . . . . . . . . . . . . 214 8.3.7 Function call-by-value of a structure . . . . . . . . . . . . . . . . . . 214 8.3.8 Function call-by-reference to a structure . . . . . . . . . . . . . . . . 215 8.3.9 Structure arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 8.3.10 Example using structures and arrays: Ion transport . . . . . . . . . . 218 8.4 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 222 8.5 Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 228 9 Miscellaneous Topics 239 9.1 Generating rand om numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . 239 9.1.1 Example: Integrating functions by rand om sampling . . . . . . . . . 246 9.2 Simple Sorting: The bubble or sinking sort . . . . . . . . . . . . . . . . . . . 248 9.3 Recursion—A function calling itself . . . . . . . . . . . . . . . . . . . . . . . 249 9.4 Input andOutput using files . . . . . . . . . . . . . . . . . . . . . . . . . . . 252 9.4.1 File and stream hand ling in C++ . . . . . . . . . . . . . . . . . . . . 252 9.4.2 Creating sequential access files andwriting to them . . . . . . . . . . 255 9.4.3 Reading from sequential access files . . . . . . . . . . . . . . . . . . . 258 9.5 Command line arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . 261 9.6 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 264
CONTENTS vii 10 A Potpourri of Applications 267 l0.1 Finding a zero using the“binary chop” 267 11 Programming in MATLAB 271 11.1 A taste of MATLAB 271 11.2 Arrays in Matlab.. 273 11.3L0ops..·· 279 11.3.1 The for loop.·· 279 11.3.2 The while loop 282 1l.4 The if/else construct·· 282 11.5 Some Matlab terminology 283 11.6 Operators in MATLAB..... 284 11.6.1 Math operators in Matlab 284 11.7 Pointwise operators in MATLAB 285 11.7.1 Logical Operators in Matlab 286 11.8M-fles.········· 288 11.8.1 Script M-files 288 11.8.2 Function M-files ... 292 11.9 Problems........ 295 11.10Projects 327 11.10.1Supplementary material:Trajectories without air resistance 346 11.10.2Supplementary material:Trajectories with air resistance ...... 346 11.10.3Supplementary material:Stepping algorithms ............ 347 11.10.4Supplementary material:Trajectories of objects in the universe... 354 11.10.5Supplementary material:Stepping algorithms...... 355 12 Graphics 367 12.1 Two Dimensional Plots.. 367 12.1.1 A basic plot...... 367 12.1.2 Using different line colors 367
CONTENTS vii 10 A Potpourri of Applications 267 10.1 Find ing a zero using the “binary chop” . . . . . . . . . . . . . . . . . . . . . 267 11 Programming in MATLAB 271 11.1 A taste of MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 271 11.2 Arrays in Matlab . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273 11.3 Loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 11.3.1 The for loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279 11.3.2 The while loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282 11.4 The if/else construct . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 282 11.5 Some Matlab terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . 283 11.6 Operators in MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284 11.6.1 Math operators in Matlab . . . . . . . . . . . . . . . . . . . . . . . . 284 11.7 Pointwise operators in MATLAB . . . . . . . . . . . . . . . . . . . . . . . . 285 11.7.1 Logical Operators in Matlab . . . . . . . . . . . . . . . . . . . . . . . 286 11.8 M-files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288 11.8.1 Script M-files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 288 11.8.2 Function M-files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 292 11.9 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 295 11.10Projects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327 11.10.1 Supplementary material: Trajectories without air resistance . . . . . 346 11.10.2 Supplementary material: Trajectories with air resistance . . . . . . . 346 11.10.3 Supplementary material: Stepping algorithms . . . . . . . . . . . . . 347 11.10.4 Supplementary material: Trajectories of objects in the universe . . . 354 11.10.5 Supplementary material: Stepping algorithms . . . . . . . . . . . . . 355 12 Graphics 367 12.1 Two Dimensional Plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367 12.1.1 A basic plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367 12.1.2 Using d ifferent line colors . . . . . . . . . . . . . . . . . . . . . . . . 367
viii CONTENTS 12.1.3 Making titles 370 l2.1.4 Axis labels.·····.·· 370 12.1.5 Different line styles ....... 370 12.1.6 Point plots,plotting with point symbols 372 12.1.7 Plotting more than one thing at once.. 377 12.1.8 Subplots..·.. 378 12.2 Three dimensional graphics 379 12.2.1Plot3 plots.····· 379 12.2.2 Mesh plots... 379 12.2.3 Axis labelling···· 381 12.2.4 meshc and meshz plots 382 12.2.5 Surface plots using surf. 382 12.2.6 Surface plots using surfc 383 12.2.7 Surface plots using surfl 383 12.2.8 Contour plots using contour 384 12.2.9 Contour plots using contour3 384 12.2.10 Contour plots using pcolor.. 384 12.2.11 Contour plots using contourf.. 385 12.2.12 Contour plots with labels.. 385 13 Miscellaneous topics 387 13.1 Pitfall review 387 13.1.1 Review of for loops.... 387 13.1.2 Review of functions... 390 13.2 The MATLAB way...... 395 l3.3 Selected Applications..·.···· 395 13.3.1 Free fall with air resistance... 395 13.3.2 Diffusion of charged ions in an electric field......... 400 13.3.3 Calculation of Electric fields.... 402
viii CONTENTS 12.1.3 Making titles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370 12.1.4 Axis labels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370 12.1.5 Different line styles . . . . . . . . . . . . . . . . . . . . . . . . . . . . 370 12.1.6 Point plots, plotting with point symbols . . . . . . . . . . . . . . . . 372 12.1.7 Plotting more than one thing at once . . . . . . . . . . . . . . . . . . 377 12.1.8 Subplots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378 12.2 Three d imensional graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . 379 12.2.1 Plot3 plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379 12.2.2 Mesh plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379 12.2.3 Axis labelling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 381 12.2.4 meshc and meshz plots . . . . . . . . . . . . . . . . . . . . . . . . . . 382 12.2.5 Surface plots using surf . . . . . . . . . . . . . . . . . . . . . . . . . . 382 12.2.6 Surface plots using surfc . . . . . . . . . . . . . . . . . . . . . . . . . 383 12.2.7 Surface plots using surfl . . . . . . . . . . . . . . . . . . . . . . . . . 383 12.2.8 Contour plots using contour . . . . . . . . . . . . . . . . . . . . . . . 384 12.2.9 Contour plots using contour3 . . . . . . . . . . . . . . . . . . . . . . 384 12.2.10 Contour plots using pcolor . . . . . . . . . . . . . . . . . . . . . . . . 384 12.2.11 Contour plots using contourf . . . . . . . . . . . . . . . . . . . . . . . 385 12.2.12 Contour plots with labels . . . . . . . . . . . . . . . . . . . . . . . . . 385 13 Miscellaneous topics 387 13.1 Pitfall review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387 13.1.1 Review of for loops . . . . . . . . . . . . . . . . . . . . . . . . . . . . 387 13.1.2 Review of functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 390 13.2 The MATLAB way . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395 13.3 Selected Applications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395 13.3.1 Free fall with air resistance . . . . . . . . . . . . . . . . . . . . . . . . 395 13.3.2 Diffusion of charged ions in an electric field . . . . . . . . . . . . . . . 400 13.3.3 Calculation of Electric field s . . . . . . . . . . . . . . . . . . . . . . . 402