(This page has no text content)
LEARN PHYSICS WITH FUNCTIONAL PROGRAMMING A Hands-on Guide to Exploring Physics with Haskell by Scott N. Walck San Francisco
LEARN PHYSICS WITH FUNCTIONAL PROGRAMMING. Copyright © 2023 by Scott N. Walck. All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. First printing 26 25 24 23 22 1 2 3 4 5 ISBN-13: 978-1-7185-0166-9 (print) ISBN-13: 978-1-7185-0167-6 (ebook) Publisher: William Pollock Managing Editor: Jill Franklin Production Manager: Sabrina Plomitallo-González Production Editor: Miles Bond Developmental Editor: Alex Freed Cover Illustrator: Gina Redman Interior Design: Octopod Studios Technical Reviewer: Gregory Wright Copyeditor: George Hale Proofreader: Bart Reed Indexer: Sanjiv Kumar Sinha For information on distribution, bulk sales, corporate sales, or translations, please contact No Starch Press, Inc. directly at info@nostarch.com or: No Starch Press, Inc. 245 8th Street, San Francisco, CA 94103 phone: 1.415.863.9900 www.nostarch.com Library of Congress Cataloging-in-Publication Data Names: Walck, Scott N., author. Title: Learn physics with functional programming : a hands-on guide to exploring physics with Haskell / by Scott N. Walck.
Description: San Francisco : No Starch Press, [2023] — Includes bibliographical references and index. Identifiers: LCCN 2022018706 (print) — LCCN 2022018707 (ebook) — ISBN 9781718501669 (print) — ISBN 9781718501676 (ebook) Subjects: LCSH: Physics–Data processing. — Functional programming (Computer science) Classification: LCC QC52 .W34 2023 (print) — LCC QC52 (ebook) — DDC 530.0285/5133–dc23/eng20220722 LC record available at https://lccn.loc.gov/2022018706 LC ebook record available at https://lccn.loc.gov/2022018707 No Starch Press and the NoStarch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in it.
To Peggy, Carl, Dan, and Jodi
About the Author Scott N. Walck has a PhD in physics from Lehigh University. He has taught physics, including computational physics, to undergraduates (physics majors and non-majors) for 20 years at Lebanon Valley College, where he has been recognized with a Distinguished Teaching Award. Walck is a three-time NSF grant recipient for research in quantum information and is the author of more than 30 peer-reviewed research articles in physics.
About the Technical Reviewer Gregory Wright received his PhD in physics from Princeton University, where he built receivers for studying cosmic microwave background radiation. He did postdoctoral studies at Bell Laboratories and was later hired as a Member of Technical Staff there. He was introduced to Haskell when he was a Visiting Industrial Fellow at the University of California’s Berkeley Wireless Research Center, where he used it to model wireless communication systems. For many years he maintained MacPort’s version of the Glasgow Haskell Compiler (GHC) and contributed to porting GHC to the AMD64 architecture on both macOS and FreeBSD. He was (probably) the first person to build GHC from source on the continent of Antarctica and encourages others to build their tools from source.
BRIEF CONTENTS Acknowledgments Introduction PART I: A HASKELL PRIMER FOR PHYSICISTS Chapter 1: Calculating with Haskell Chapter 2: Writing Basic Functions Chapter 3: Types and Entities Chapter 4: Describing Motion Chapter 5: Working with Lists Chapter 6: Higher-Order Functions Chapter 7: Graphing Functions Chapter 8: Type Classes Chapter 9: Tuples and Type Constructors Chapter 10: Describing Motion in Three Dimensions Chapter 11: Creating Graphs Chapter 12: Creating Stand-Alone Programs Chapter 13: Creating 2D and 3D Animations PART II: EXPRESSING NEWTONIAN MECHANICS AND SOLVING PROBLEMS Chapter 14: Newton’s Second Law and Differential Equations Chapter 15: Mechanics in One Dimension Chapter 16: Mechanics in Three Dimensions Chapter 17: Satellite, Projectile, and Proton Motion Chapter 18: A Very Short Primer on Relativity Chapter 19: Interacting Particles Chapter 20: Springs, Billiard Balls, and a Guitar String
PART III: EXPRESSING ELECTROMAGNETIC THEORY AND SOLVING PROBLEMS Chapter 21: Electricity Chapter 22: Coordinate Systems and Fields Chapter 23: Curves, Surfaces, and Volumes Chapter 24: Electric Charge Chapter 25: Electric Field Chapter 26: Electric Current Chapter 27: Magnetic Field Chapter 28: The Lorentz Force Law Chapter 29: The Maxwell Equations Appendix: Installing Haskell Bibliography Index
CONTENTS IN DETAIL ACKNOWLEDGMENTS INTRODUCTION Who This Book Is For Why Functional Programming, and Why Haskell? About This Book PART I A HASKELL PRIMER FOR PHYSICISTS 1 CALCULATING WITH HASKELL A Kinematics Problem The Interactive Compiler Numeric Functions Operators Precedence and Associativity The Application Operator Functions with Two Arguments Numbers in Haskell Negative Numbers in Haskell Decimal Numbers in Haskell Exponential Notation Approximate Calculation Errors Getting Help and Quitting More Information Summary Exercises
2 WRITING BASIC FUNCTIONS Constants, Functions, and Types How We Talk About Functions Anonymous Functions Composing Functions Variable Not in Scope Error Summary Exercises 3 TYPES AND ENTITIES Basic Types The Boolean Type The Character Type The String Type Numeric Types Function Types Summary Exercises 4 DESCRIBING MOTION Position and Velocity on an Air Track Types for Physical Quantities Introducing Derivatives Derivatives in Haskell Modeling the Car’s Position and Velocity Modeling Acceleration Approximate Algorithms and Finite Precision Summary Exercises
5 WORKING WITH LISTS List Basics Selecting an Element from a List Concatenating Lists Arithmetic Sequences List Types Functions for Lists of Numbers When Not to Use a List Type Variables Type Conversion The Length of Lists A String Is a List of Characters List Comprehensions Infinite Lists List Constructors and Pattern Matching Summary Exercises 6 HIGHER-ORDER FUNCTIONS How to Think About Functions with Parameters Mapping a Function Over a List Iteration and Recursion Anonymous Higher-Order Functions Operators as Higher-Order Functions Combinators Predicate-Based Higher-Order Functions Numerical Integration Introducing Integrators Digital Integration Implementing Antiderivatives
Summary Exercises 7 GRAPHING FUNCTIONS Using Library Modules Standard Library Modules Other Library Modules Plotting Function Only Function and Module Function, Module, and Plot Definition Summary Exercises 8 TYPE CLASSES Type Classes and Numbers Type Classes from the Prelude The Eq Type Class The Show Type Class The Num Type Class The Integral Type Class The Ord Type Class The Fractional Type Class The Floating Type Class Exponentiation and Type Classes Sections Example of Type Classes and Plotting Summary Exercises
9 TUPLES AND TYPE CONSTRUCTORS Pairs Currying a Function of Two Variables Triples Comparing Lists and Tuples Maybe Types Lists of Pairs Tuples and List Comprehensions Type Constructors and Kinds Numerical Integration Redux Summary Exercises 10 DESCRIBING MOTION IN THREE DIMENSIONS Three-Dimensional Vectors Coordinate-Free Vectors Geometric Definition of Vector Addition Geometric Definition of Scaling a Vector Geometric Definition of Vector Subtraction Geometric Definition of Dot Product Geometric Definition of Cross Product Derivative of a Vector-Valued Function Coordinate Systems Vector Addition with Coordinate Components Vector Scaling with Coordinate Components Vector Subtraction with Coordinate Components Dot Product with Coordinate Components Cross Product with Coordinate Components Derivative with Coordinate Components Kinematics in 3D
Defining Position, Velocity, and Acceleration Two Components of Acceleration Projectile Motion Making Your Own Data Type Single Data Constructor Multiple Data Constructors Defining a New Data Type for 3D Vectors Possible Implementations Data Type Definition for Vec Vec Functions Summary Exercises 11 CREATING GRAPHS Title and Axis Labels Other Labels Plotting Data Multiple Curves on One Set of Axes Controlling the Plot Ranges Making a Key Summary Exercises 12 CREATING STAND-ALONE PROGRAMS Using GHC to Make a Stand-Alone Program Hello, World! A Program That Imports Modules Using Cabal to Make a Stand-Alone Program Using Stack to Make a Stand-Alone Program Summary Exercises
13 CREATING 2D AND 3D ANIMATIONS 2D Animation Displaying a 2D Picture Making a 2D Animation Making a 2D Simulation 3D Animation Displaying a 3D Picture Making a 3D Animation Making a 3D Simulation Summary Exercises PART II EXPRESSING NEWTONIAN MECHANICS AND SOLVING PROBLEMS 14 NEWTON’S SECOND LAW AND DIFFERENTIAL EQUATIONS Newton’s First Law Newton’s Second Law in One Dimension Second Law with Constant Forces Second Law with Forces That Depend Only on Time Air Resistance Second Law with Forces That Depend Only on Velocity Euler Method by Hand Euler Method in Haskell The State of a Physical System Second Law with Forces That Depend on Time and Velocity Method 1: Produce a List of States Method 2: Produce a Velocity Function Example: Pedaling and Coasting with Air Resistance
Euler Method by Hand Method 1: Produce a List of States Method 2: Produce a Velocity Function Summary Exercises 15 MECHANICS IN ONE DIMENSION Introductory Code Forces That Depend on Time, Position, and Velocity A General Strategy for Solving Mechanics Problems Solving with Euler’s Method Producing a List of States Position and Velocity Functions A Damped Harmonic Oscillator Euler Method by Hand Method 1: Producing a List of States Method 2: Producing Position and Velocity Functions Euler-Cromer Method Solving Differential Equations Generalizing the State Space Type Classes for State Spaces One More Numerical Method Comparison of Numerical Methods Summary Exercises 16 MECHANICS IN THREE DIMENSIONS Introductory Code Newton’s Second Law in Three Dimensions The State of One Particle Solving Newton’s Second Law
One-Body Forces Earth Surface Gravity Gravity Produced by the Sun Air Resistance Wind Force Force from Uniform Electric and Magnetic Fields State Update for One Particle Preparing for Animation Two Helpful Animation Functions How the Functions Work Summary Exercises 17 SATELLITE, PROJECTILE, AND PROTON MOTION Satellite Motion State-Update Function Initial State Time-Scale Factor Animation Rate Display Function Projectile Motion with Air Resistance Calculating a Trajectory Finding the Angle for Maximum Range 2D Animation 3D Animation Proton in a Magnetic Field Summary Exercises 18 A VERY SHORT PRIMER ON RELATIVITY A Little Theory
A Replacement for Newton’s Second Law Response to a Constant Force Proton in a Magnetic Field Summary Exercises 19 INTERACTING PARTICLES Newton’s Third Law Two-Body Forces Universal Gravity Constant Repulsive Force Linear Spring Central Force Elastic Billiard Interaction Internal and External Forces The State of a Multi-Particle System State Update for Multiple Particles Implementing Newton’s Second Law Numerical Methods for Multiple Particles Composite Functions Summary Exercises 20 SPRINGS, BILLIARD BALLS, AND A GUITAR STRING Introductory Code Two Masses and Two Springs Forces Animation Functions Stand-Alone Animation Program Using Mechanical Energy as a Guide to Numerical Accuracy A Collision
Data Representations Spring Constant and Time Step Momentum and Energy Conservation Numerical Issues Animated Results Wave on a Guitar String Forces State-Update Function Initial State Stand-Alone Program Asynchronous Animation Summary Exercises PART III EXPRESSING ELECTROMAGNETIC THEORY AND SOLVING PROBLEMS 21 ELECTRICITY Electric Charge Coulomb’s Law Two Charges Interacting Looking at Extremes Modeling the Situation in Haskell Summary Exercises 22 COORDINATE SYSTEMS AND FIELDS Polar Coordinates Cylindrical Coordinates Spherical Coordinates
Comments 0
Loading comments...
Reply to Comment
Edit Comment