Statistics
384
Views
79
Downloads
0
Donations
Support
Share
Uploader

高宏飞

Shared on 2025-08-30

AuthorAllen B. Downey

Modeling and Simulation in Python teaches readers how to analyze real-world scenarios using the Python programming language, requiring no more than a background in high school math. Modeling and Simulation in Python is a thorough but easy-to-follow introduction to physical modeling—that is, the art of describing and simulating real-world systems. Readers are guided through modeling things like world population growth, infectious disease, bungee jumping, baseball flight trajectories, celestial mechanics, and more while simultaneously developing a strong understanding of fundamental programming concepts like loops, vectors, and functions. Clear and concise, with a focus on learning by doing, the author spares the reader abstract, theoretical complexities and gets right to hands-on examples that show how to produce useful models and simulations.

Tags
ai agent
ISBN: 1718502168
Publisher: No Starch Press
Publish Year: 2023
Language: 英文
Pages: 310
File Format: PDF
File Size: 6.9 MB
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

(This page has no text content)
PRAISE FOR MODELING AND SIMULATION IN PYTHON “Downey uses a combination of Python, calculus, bespoke helper functions, and easily accessible online materials to model a diverse and interesting set of simulation projects. In the process, he presents a practical and reusable framework for modeling dynamical systems with Python.” — LEE VAUGHAN, AUTHOR OF PYTHON TOOLS FOR SCIENTISTS, REAL-WORLD PYTHON, AND IMPRACTICAL PYTHON PROJECTS AND FORMER SENIOR PRINCIPAL SCIENTIST AT EXXONMOBIL “Modeling and Simulation in Python is an introduction to physical modeling using a computational approach [which] makes it possible to work with more realistic models than what you typically see in a first-year physics class.” — PYTHON KITCHEN “An impressive introduction to physical modeling and Python programming, featuring clear, concise explanations and examples . . . perfect for readers of any level.” — CHRISTIAN MAYER, AUTHOR OF PYTHON ONE- LINERS AND FOUNDER OF FINXTER.COM “Modeling and Simulation in Python provides a wealth of instructive examples of all kinds of modeling. . . . This book can be valuable as a textbook for classes on scientific computation or as a guide to exploration for interested amateurs.” — BRADFORD TUCKFIELD, AUTHOR OF DIVE INTO ALGORITHMS AND DIVE INTO DATA SCIENCE “Downey’s book fills a significant gap in the market. For those unwilling to commit to the prolonged dullness of a bottom-up approach to programming, Downey’s top-down, context-rich, and motivating approach dramatically lowers
the barrier to gaining literacy in programming and explicitly and insightfully teaches modeling.” — PHAT VU, DIRECTOR OF THE SCIENCE AND MATHEMATICS PROGRAM AT SOKA UNIVERSITY OF AMERICA
MODELING AND SIMULATION IN PYTHON An Introduction for Scientists and Engineers by Allen B. Downey San Francisco
MODELING AND SIMULATION IN PYTHON. Copyright © 2023 by Allen B. Downey. 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. 27 26 25 24 23 1 2 3 4 5 ISBN-13: 978-1-7185-0216-1 (print) ISBN-13: 978-1-7185-0217-8 (ebook) Publisher: William Pollock Managing Editor: Jill Franklin Production Manager: Sabrina Plomitallo-González Production Editor: Jennifer Kepler Developmental Editor: Alex Freed Cover Illustrator: Gina Redman Interior Design: Octopod Studios Technical Reviewer: Valerie Barr Copyeditor: Gary Smith Proofreader: Lisa Devoto Farrell 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 Control Number: 2022049830 Figure 19-2 has been been reproduced under a CC-BY 4.0 license, http://creativecommons.org/licenses/by/4.0. No Starch Press and the No Starch 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.
About the Author Allen Downey is a staff scientist at DrivenData and professor emeritus at Olin College, where he taught Modeling and Simulation and other classes related to software and data science. He is the author of several textbooks, including Think Python, Think Bayes, and Elements of Data Science. Previously, he taught at Wellesley College and Colby College. He received his PhD in computer science from the University of California, Berkeley, in 1997. His undergraduate and master’s degrees are from the civil engineering department at MIT. He is the author of Probably Overthinking It, a blog about data science and Bayesian statistics.
About the Technical Reviewer Valerie Barr has spent more than a decade focusing on interdisciplinary applications and curricular strategies to expose students from all fields to computing. This has included developing and offering courses in modeling and simulation, data visualization, and other areas that now also cross into data science. She has a PhD in computer science from Rutgers University, held the Jean Sammet Chair at Mount Holyoke College, and now holds the Margaret Hamilton Chair at Bard College, where she is launching the Bard Network Computing Initiative.
BRIEF CONTENTS Acknowledgments Introduction PART I: DISCRETE SYSTEMS Chapter 1: Introduction to Modeling Chapter 2: Modeling a Bike Share System Chapter 3: Iterative Modeling Chapter 4: Parameters and Metrics Chapter 5: Building a Population Model Chapter 6: Iterating the Population Model Chapter 7: Limits to Growth Chapter 8: Projecting into the Future Chapter 9: Analysis and Symbolic Computation Chapter 10: Case Studies Part I PART II: FIRST-ORDER SYSTEMS Chapter 11: Epidemiology and SIR Models Chapter 12: Quantifying Interventions Chapter 13: Sweeping Parameters Chapter 14: Nondimensionalization Chapter 15: Thermal Systems Chapter 16: Solving the Coffee Problem Chapter 17: Modeling Blood Sugar Chapter 18: Implementing the Minimal Model Chapter 19: Case Studies Part II
PART III: SECOND-ORDER SYSTEMS Chapter 20: The Falling Penny Revisited Chapter 21: Drag Chapter 22: Two-Dimensional Motion Chapter 23: Optimization Chapter 24: Rotation Chapter 25: Torque Chapter 26: Case Studies Part III Appendix: Under the Hood Index
CONTENTS IN DETAIL ACKNOWLEDGMENTS INTRODUCTION Who Is This Book For? How Much Math and Science Do I Need? How Much Programming Do I Need? Book Overview Teaching Modeling Getting Started Installing Python Running Jupyter Suggestions and Corrections PART I DISCRETE SYSTEMS 1 INTRODUCTION TO MODELING The Modeling Framework Testing the Falling Penny Myth Computation in Python False Precision Computation with Units Summary Exercises 2 MODELING A BIKE SHARE SYSTEM Our Bike Share Model Defining Functions Print Statements if Statements
Parameters for Loops TimeSeries Plotting Summary Exercises Under the Hood 3 ITERATIVE MODELING Iterating on Our Bike Share Model Using More Than One State Object Documentation Dealing with Negative Bikes Comparison Operators Introducing Metrics Summary Exercises 4 PARAMETERS AND METRICS Functions That Return Values Loops and Arrays Sweeping Parameters Incremental Development Summary Exercises Challenge Exercises Under the Hood 5 BUILDING A POPULATION MODEL Exploring the Data Absolute and Relative Errors Modeling Population Growth Simulating Population Growth Summary
Exercise 6 ITERATING THE POPULATION MODEL System Objects A Proportional Growth Model Factoring Out the Update Function Combining Birth and Death Summary Exercise Under the Hood 7 LIMITS TO GROWTH Quadratic Growth Net Growth Finding Equilibrium Dysfunctions Summary Exercises 8 PROJECTING INTO THE FUTURE Generating Projections Comparing Projections Summary Exercise 9 ANALYSIS AND SYMBOLIC COMPUTATION Difference Equations Differential Equations Analysis and Simulation Analysis with WolframAlpha Analysis with SymPy Differential Equations in SymPy Solving the Quadratic Growth Model
Summary Exercises 10 CASE STUDIES PART I Historical World Population One Queue or Two? Predicting Salmon Populations Tree Growth PART II FIRST-ORDER SYSTEMS 11 EPIDEMIOLOGY AND SIR MODELS The Freshman Plague The Kermack-McKendrick Model The KM Equations Implementing the KM Model The Update Function Running the Simulation Collecting the Results Now with a TimeFrame Summary Exercise 12 QUANTIFYING INTERVENTIONS The Effects of Immunization Choosing Metrics Sweeping Immunization Summary Exercise 13 SWEEPING PARAMETERS Sweeping Beta
Sweeping Gamma Using a SweepFrame Summary Exercise 14 NONDIMENSIONALIZATION Beta and Gamma Exploring the Results Contact Number Comparing Analysis and Simulation Estimating the Contact Number Summary Exercises Under the Hood 15 THERMAL SYSTEMS The Coffee Cooling Problem Temperature and Heat Heat Transfer Newton’s Law of Cooling Implementing Newtonian Cooling Finding Roots Estimating r Summary Exercises 16 SOLVING THE COFFEE PROBLEM Mixing Liquids Mix First or Last? Optimal Timing The Analytic Solution Summary Exercises
17 MODELING BLOOD SUGAR The Minimal Model The Glucose Minimal Model Getting the Data Interpolation Summary Exercises 18 IMPLEMENTING THE MINIMAL MODEL Implementing the Model The Update Function Running the Simulation Solving Differential Equations Summary Exercise 19 CASE STUDIES PART II Revisiting the Minimal Model The Insulin Minimal Model Low-Pass Filter Thermal Behavior of a Wall HIV PART III SECOND-ORDER SYSTEMS 20 THE FALLING PENNY REVISITED Newton’s Second Law of Motion Dropping Pennies Event Functions Summary Exercise
21 DRAG Calculating Drag Force The Params Object Simulating the Penny Drop Summary Exercises 22 TWO-DIMENSIONAL MOTION Assumptions and Decisions Vectors Simulating Baseball Flight Drag Force Adding an Event Function Visualizing Trajectories Animating the Baseball Summary Exercises 23 OPTIMIZATION The Manny Ramirez Problem Finding the Range Summary Exercise Under the Hood 24 ROTATION The Physics of Toilet Paper Setting Parameters Simulating the System Plotting the Results The Analytic Solution Summary
Exercise 25 TORQUE Angular Acceleration Moment of Inertia Teapots and Turntables Two-Phase Simulation Phase 1 Phase 2 Combining the Results Estimating Friction Animating the Turntable Summary Exercise 26 CASE STUDIES PART III Bungee Jumping Bungee Dunk Revisited Orbiting the Sun Spider-Man Kittens Simulating a Yo-Yo Congratulations APPENDIX: UNDER THE HOOD How run_solve_ivp Works How root_scalar Works How maximize_scalar Works INDEX
ACKNOWLEDGMENTS My early work on this book benefited from conversations with my colleagues at Olin College, including John Geddes, Mark Somerville, Alison Wood, Chris Lee, and Jason Woodard. I am grateful to Lisa Downey and Jason Woodard for their thoughtful and careful copyediting, and to Eoghan Downey and Jason Moore for their technical review. Thanks to Alessandra Ferzoco, Erhardt Graeff, Emily Tow, Kelsey Houston- Edwards, Linda Vanasupa, Matt Neal, Joanne Pratt, and Steve Matsumoto for their helpful suggestions.
INTRODUCTION This book is about dynamical systems, that is, things that change over time. The first example we’ll look at is a penny falling from the Empire State Building, where the thing that’s changing is the position of the penny in space. Other examples include a cup of coffee, where temperature changes over time, and glucose in the human bloodstream, where concentration changes over time. We will define models, which are simplifications intended to include the most important elements of the real world and leave out the least important, and we will write Python programs that simulate these models. We will use models and simulations to do three kinds of work: predicting how a system will behave, explaining why it behaves as it does, and designing systems to behave the way we want. If you have taken an introductory physics class, you have seen models of dynamical systems. For example, you might have modeled a block on a plane, a projectile, or a planet in orbit. If you took a good class, you were aware of the decisions those models were based on. Most likely the model of the block did not include friction, the model of the projectile did not include air resistance, and the model of the planet was a “point mass.” And if you took a very good class, you were also aware of the limitations of these models. For example, in many real-world systems, friction and air resistance are among the most important elements; if you leave them out of the model, your predictions will not be accurate and your designs will not work. On the other hand, a model that treats a planet as a point mass is good enough to compute orbits with high accuracy.
But most physics classes are based on mathematical analysis, which makes it hard to work with elements like friction and air resistance. That’s why the focus of this book is computational simulation, which makes it possible to work with more realistic models and to try out different models. With a computational approach, we can also take on a wide variety of systems. Examples in this book include a bike share system, world population growth, and queueing systems; epidemics, electronic circuits, and external walls; baseballs and bungee jumpers; and turntables and yo-yos. I hope you will find this approach interesting, empowering, and at least a little bit fun. Who Is This Book For? If you are studying or working in the natural or social sciences, this book will help you think about models and the work we can do with them. I hope there’s at least one model in this book that is related to your field, but even if not, the lessons you learn about modeling apply to almost every field. How Much Math and Science Do I Need? I’ll assume that you know what derivatives and integrals are, but that’s about all. In particular, you don’t need to know (or remember) much about finding derivatives or integrals of functions analytically. If you know the derivative of x2 and you can integrate 2x dx, that will do it. More importantly, you should understand what those concepts mean; but if you don’t, this book might help you figure it out. You don’t have to know anything about differential equations. As for science, we will cover topics from a variety of fields, including demography, epidemiology, medicine, thermodynamics, and mechanics. For the most part, I don’t assume you know anything about these topics. But one of the skills you will develop is the ability to learn enough about new fields to develop models and simulations. When we get to mechanics, I’ll assume you understand the relationship between position, velocity, and acceleration, and that you are familiar with Newton’s laws of motion, especially the second law, which is often expressed as F = ma (force equals mass times acceleration). How Much Programming Do I Need? If you have never programmed before, you should be able to read this book, understand it, and do the exercises. I will do my best to explain everything you