Physics and Functional Programming

Physics and Functional Programming

Physics and Functional Programming Scott N. Walck Version 0.1, August 17, 2018 ii Contents Preface xiii I Language 1 1 Introduction to Haskell 5 1.1 Using GHCi as a calculator . .5 1.2 Numeric functions . .6 1.3 Operators . .8 1.4 Numbers in Haskell . 11 1.4.1 Negative numbers in Haskell . 11 1.4.2 Decimal numbers in Haskell . 12 1.4.3 Exponential notation . 12 1.5 Functions with two arguments . 12 1.6 Approximate calculation . 13 1.7 Errors . 15 1.7.1 Variable not in scope . 15 1.7.2 No instance for Show . 15 1.8 Getting help and quitting . 16 1.9 More information . 16 1.10 Exercises . 16 2 Functions 19 2.1 Constants, functions, and types . 19 2.2 How we talk about functions . 22 2.3 Anonymous functions . 24 2.4 Exercises . 26 iii iv CONTENTS 3 Types 29 3.1 Basic types . 29 3.1.1 The Boolean type . 30 3.1.2 The character type . 32 3.1.3 The string type . 33 3.2 Function types . 33 3.3 Exercises . 35 4 Lists 39 4.1 List basics . 39 4.2 Infinite lists . 43 4.3 List constructors and pattern matching . 44 4.4 Exercises . 46 5 Higher-order Functions 49 5.1 Functions with parameters . 49 5.2 Numerical integration . 53 5.3 Anonymous higher-order functions . 55 5.4 Mapping over lists . 56 5.5 Operators as higher-order functions . 57 5.6 Predicate-based higher-order functions . 58 5.7 Exercises . 59 6 Quick Plotting 61 6.1 Exercises . 63 7 Type Classes 65 7.1 Introduction . 65 7.2 Type classes from the Prelude . 66 7.2.1 Type class Eq ....................... 67 7.2.2 Type class Ord ...................... 68 7.2.3 Type class Show ...................... 68 7.2.4 Type class Num ...................... 69 7.2.5 Type class Integral ................... 69 7.2.6 Type class Fractional .................. 69 7.2.7 Type class Floating ................... 70 7.3 Prelude functions with type class constraints . 71 7.4 Sections . 71 CONTENTS v 7.5 Higher-order functions as operators . 72 7.6 Example of type classes and plotting . 73 7.7 Exercises . 76 8 Tuples 79 8.1 Pairs . 79 8.2 Functions of two variables . 80 8.3 Triples . 82 8.4 Comparison of lists and tuples . 83 8.5 Maybe types............................ 83 8.6 Lists of pairs . 85 8.7 Exercises . 87 9 List Comprehensions 91 9.1 Mapping . 91 9.2 Filtering . 92 9.3 Exercises . 94 10 Presentation Plotting 95 10.1 Title and axis labels . 95 10.2 Other labels . 97 10.3 Plotting data . 98 10.4 Multiple curves on one set of axes . 98 10.5 Controlling the plot ranges . 100 10.6 Exercises . 101 11 Animation 103 11.1 2D Animation . 103 11.1.1 Displaying a picture . 103 11.1.2 Making an animation . 104 11.1.3 Making a simulation . 105 11.2 3D Animation . 105 II Newtonian Mechanics 107 12 Newton's Second Law 109 12.1 Newton's First Law . 109 12.2 Newton's Second Law . 110 vi CONTENTS 12.3 State update . 116 13 Mechanics in One Dimension 123 13.1 State update . 123 13.2 Units . 131 13.3 Air resistance . 132 13.3.1 Introduction . 132 13.3.2 Collision model . 132 13.4 Euler-Cromer method . 133 13.5 Exercises . 134 14 Mechanics in Three Dimensions 137 14.1 Vectors in Haskell . 137 14.2 Euler method with vectors . 139 14.3 Animation . 143 15 Multiple Objects in Three Dimensions 149 15.1 The State of a Physical System . 149 15.2 Multiple Objects in Three Dimensions . 155 15.3 Waves on a Flexible String . 156 III Electromagnetic Theory 159 16 Coordinate Systems 163 16.1 Polar coordinates . 163 16.2 Cylindrical coordinates . 165 16.3 Spherical coordinates . 166 16.4 A type for position . 168 16.5 Displacement . 173 16.6 Scalar and vector fields . 174 17 Curves, Surfaces, and Volumes 177 17.1 Curves . 178 17.2 Surfaces . 180 17.3 Volumes . 183 17.3.1 A unit ball . 184 CONTENTS vii 18 Electric Charge 187 18.1 Charge Distributions . 187 18.2 A type for charge distribution . 190 18.3 Total charge . 191 19 Electric Field 193 19.1 Coulomb's law . 193 19.2 Electric Field . 194 19.3 Electric Field produced by a Line Segment of Charge . 201 20 Electric Current 203 20.1 Current Distributions . 203 20.2 A type for current distribution . 205 20.3 Total current . 206 21 Magnetic Field 207 21.1 Magnetic Field of a Circular Current Loop . 208 22 Motion of a Charged Particle 209 A Color Summary 213 B A Type for Vectors 215 C Vector Integrals 221 C.1 A table of vector integrals . 221 C.2 Applications of the integrals . 221 C.2.1 Scalar line integral . 221 C.2.2 Vector line integral . 222 C.2.3 Dotted line integral . 222 C.2.4 Scalar surface integral . 223 C.2.5 Vector surface integral . 223 C.2.6 Flux integral . 223 C.2.7 Scalar volume integral . 224 C.2.8 Vector volume integral . 224 C.3 Code for integrals . 225 C.3.1 Scalar line integral . 226 C.3.2 Vector line integral . 233 C.3.3 Dotted line integral . 233 viii CONTENTS C.3.4 Scalar surface integral . 234 C.3.5 Vector surface integral . 235 C.3.6 Flux integral . 236 C.3.7 Scalar volume integral . 236 C.3.8 Vector volume integral . 237 C.4 Fundamental theorems of calculus . 238 C.4.1 Gradient theorem . 238 C.4.2 Stokes' theorem . 238 C.4.3 Divergence theorem . 238 C.5 Calculation . ..

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    275 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us