Introduction to Computers and Programming using C++and MATLAB Alex F.Bielajew The University of Michigan Department of Nuclear Engineering and Radiological Sciences 2927 Cooley Building (North Campus) 2355 Bonisteel Boulevard Ann Arbor,Michigan 48109-2104 U.S.A. Tel:7347646364 Fax:7347634540 email:bielajew@umich.edu 2000-2003 Alex F Bielajew Revision:October 9.2005
Introduction to Computers and Programming using C++ and MATLAB Alex F. Bielajew The University of Michigan Department of Nuclear Engineering andRadiological Sciences 2927 Cooley Building (North Campus) 2355 Bonisteel Boulevard Ann Arbor, Michigan 48109-2104 U. S. A. Tel: 734 764 6364 Fax: 734 763 4540 email: bielajew@umich.edu c 2000—2003 Alex F Bielajew Revision: October 9, 2005
2
2
Preface This "book"arises out of a course I teach,a four-credit (52 hour)freshman-level course Introduction to Computers and Programming being taught in the College of Engineering at the University of Michigan.The book is in reasonably rough shape at this stage.It was assembled from my lecture notes two years ago and is under constant revision.I may never finish it!A wise person once said,"Old age happens when you dwell more on the past than on the future."By this definition,I have found eternal youth,insofar as this book is concerned. My educational objectives are quite simple.This is not really a course in computing.It is a course in thinking,technical thinking,logical thinking,about formulating a problem,a mathematical problem,a physics problem,a game,and then executing a solution and making it "work".I consider the computer and the ability to program it as a kind of laboratory-a laboratory to investigate practically,the theories and ideas of other technical courses.It is possible,for example,to teach a lot of Calculus to students without ever mentioning the word,and there are several examples throughout this book. This course is not about syntax.Hence,the book introduces the minimum amount syntax to get through a problem.Indeed,I even keep some syntax hidden,to encourage students to discover their own algorithms.So,if you are thinking of using this book as a technical reference in C++or Matlab,I anticipate that you will be disappointed. The greatest value in this book,if there is any to be found,is in the exercises,problems and projects at the back of almost every chapter.The ideal way to learn a computer language is to learn a little syntax and then try it out on a computer.The ideal way to think is not to read about it,but to actually do it!The book reflects this.The material in the chapters,separated from the exercises,is worse than useless,for reading the material and not doing the problems is just a waste of time.Do the problems!Moreover,don't ask me for the solutions!There is much more pedagogical value in a well-posed question than a well-articulated answer. OK,I'll step off my soap box now! Several professors and many students have contributed to the ideas put into this book. Professor James Holloway and I have had endless discussion on the general problem of teaching algorithmic thinking to freshman.We still have not come to any conclusions except i
Preface This “book” arises out of a course I teach, a four-credit (52 hour) freshman-level course Introduction to Computers and Programming being taught in the College of Engineering at the University of Michigan. The book is in reasonably rough shape at this stage. It was assembledfrom my lecture notes two years ago andis under constant revision. I may never finish it! A wise person once said, “Old age happens when you dwell more on the past than on the future.” By this definition, I have found eternal youth, insofar as this book is concerned. My educational objectives are quite simple. This is not really a course in computing. It is a course in thinking, technical thinking, logical thinking, about formulating a problem, a mathematical problem, a physics problem, a game, andthen executing a solution andmaking it “work”. I consider the computer and the ability to program it as a kind of laboratory—a laboratory to investigate practically, the theories andideas of other technical courses. It is possible, for example, to teach a lot of Calculus to students without ever mentioning the word, and there are several examples throughout this book. This course is not about syntax. Hence, the book introduces the minimum amount syntax to get through a problem. Indeed, I even keep some syntax hidden, to encourage students to discover their own algorithms. So, if you are thinking of using this book as a technical reference in C++ or Matlab, I anticipate that you will be disappointed. The greatest value in this book, if there is any to be found, is in the exercises, problems and projects at the back of almost every chapter. The ideal way to learn a computer language is to learn a little syntax andthen try it out on a computer. The ideal way to think is not to readabout it, but to actually do it! The book reflects this. The material in the chapters, separatedfrom the exercises, is worse than useless, for reading the material and not doing the problems is just a waste of time. Do the problems! Moreover, don’t ask me for the solutions! There is much more pedagogical value in a well-posed question than a well-articulatedanswer. OK, I’ll step off my soap box now! Several professors and many students have contributed to the ideas put into this book. Professor James Holloway and I have had endless discussion on the general problem of teaching algorithmic thinking to freshman. We still have not come to any conclusions except i
iⅱ that it is highly challenging and rewarding.As for how it actually gets done,well,to be sure it is a works-in-progress.I spent a very enjoyable term co-teaching this course (the first time I taught it)with Professor Ken Powell.Thanks,Ken,for nursing me through that first year! Both James's and Ken's diverse computational backgrounds are reflected to some degree in this book. I owe a debt of gratitude to the dozens of Graduate Student Instructors who have taught with me on this course.One of them,Dan Osborne,deserves special recognition.He is one of the most gifted and committed teachers I have ever encountered.Dan and I spent hour after hour discussing the challenges of teaching computing and thinking skills to undergraduates. To the 2500 or so undergraduates,mostly freshmen,who have taken my course:Every time I teach this course I learn something new-about computing,about teaching,about the joy of making an early "deflection"in a student's career. The support of the Dean for Engineering Undergraduate Education,Professor Gary Herrin, I acknowledged warmly.Gary removes many of the roadblocks that could otherwise make large-scale teaching odious.It is remarkable to have on one's side an administrator who is more likely to say "Yes!"rather than "No!"to my numerous outrageous requests. Finally,none of this would be possible without the support of my wife,Karen Vineberg.Your strong support during the insanely busy term teaching this course is deeply appreciated.Yes Karen,you are right,I do love teaching this course despite the long hours,the insomnia,the lost weekends and lost vacations To all of them I give my thanks and to Karen,my love.Your efforts will go to help future students. AFB,October 9,2005
ii that it is highly challenging and rewarding. As for how it actually gets done, well, to be sure it is a works-in-progress. I spent a very enjoyable term co-teaching this course (the first time I taught it) with Professor Ken Powell. Thanks, Ken, for nursing me through that first year! Both James’s and Ken’s diverse computational backgrounds are reflected to some degree in this book. I owe a debt of gratitude to the dozens of Graduate Student Instructors who have taught with me on this course. One of them, Dan Osborne, deserves special recognition. He is one of the most giftedandcommittedteachers I have ever encountered. Dan andI spent hour after hour discussing the challenges of teaching computing and thinking skills to undergraduates. To the 2500 or so undergraduates, mostly freshmen, who have taken my course: Every time I teach this course I learn something new—about computing, about teaching, about the joy of making an early “deflection” in a student’s career. The support of the Dean for Engineering Undergraduate Education, Professor Gary Herrin, I acknowledged warmly. Gary removes many of the roadblocks that could otherwise make large-scale teaching odious. It is remarkable to have on one’s side an administrator who is more likely to say “Yes!” rather than “No!” to my numerous outrageous requests. Finally, none of this wouldbe possible without the support of my wife, Karen Vineberg. Your strong support during the insanely busy term teaching this course is deeply appreciated. Yes Karen, you are right, I do love teaching this course despite the long hours, the insomnia, the lost weekends and lost vacations. To all of them I give my thanks andto Karen, my love. Your efforts will go to help future students. AFB, October 9, 2005
Contents 1 Introduction to the course 1.1 What this course is about 1 1.2 Problems....·..·· 6 2 Data representations 7 2.1 Bits,.nibbles,bytes and words·.··.· 7 2.2 4-bit Binary,Hexadecimal and Decimal Digits........ 9 2.3 Binary arithmetic........... 9 2.4 Converting from binary to decimal 10 2.4.1 Whole numbers....... 10 2.4.2 Real numbers.......:·· 11 2.5 Binary integer arithmetic on computers... 11 2.5.1 Two's-complement integer arithmetic ... 12 2.6 32-bit Binary,Hexadecimal,Unsigned and Signed Integers 13 2.7 Problems...·..······· 15 3 Algorithms and Pseudocodes 23 3.1 What is an algorithm?.... 23 3.2 Flowchart representation..... 24 3.3 Pseudocode representation···· 24 3.4 Decisions,conditionals,branch instructions 25 3.5 Looping or jump instructions······· 25
Contents 1 Introduction to the course 1 1.1 What this course is about . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 2 Data representations 7 2.1 Bits, nibbles, bytes and word s . . . . . . . . . . . . . . . . . . . . . . . . . . 7 2.2 4-bit Binary, Hexad ecimal and Decimal Digits . . . . . . . . . . . . . . . . . 9 2.3 Binary arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2.4 Converting from binary to d ecimal . . . . . . . . . . . . . . . . . . . . . . . 10 2.4.1 Whole numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 2.4.2 Real numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 2.5 Binary integer arithmetic on computers . . . . . . . . . . . . . . . . . . . . . 11 2.5.1 Two’s-complement integer arithmetic . . . . . . . . . . . . . . . . . . 12 2.6 32-bit Binary, Hexad ecimal, Unsigned and Signed Integers . . . . . . . . . . 13 2.7 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 3 Algorithms and Pseudocodes 23 3.1 What is an algorithm? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 3.2 Flowchart representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 3.3 Pseud ocod e representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 3.4 Decisions, cond itionals, branch instructions . . . . . . . . . . . . . . . . . . 25 3.5 Looping or jump instructions . . . . . . . . . . . . . . . . . . . . . . . . . . 25 iii
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