Learn Python with Talk Python's 270 hours of courses

#423: Solving 10 different simulation problems with Python Transcript

Recorded on Friday, Jun 2, 2023.

00:00 Python is used for a wide variety of software projects.

00:03 One area that's really gained a lot of momentum is in the computational space, including data science.

00:08 On this episode, we welcome back Allen Downey to dive into a particular slice of this space, simulation problems in Python and physics and engineering in general.

00:17 This is "Talk Python to Me," episode 423, recorded June 6th, 2023.

00:24 (upbeat music)

00:27 Welcome to Talk Python To Me, a weekly podcast on Python.

00:40 This is your host, Michael Kennedy.

00:42 Follow me on Mastodon, where I'm @mkennedy, and follow the podcast using @talkpython, both on fosstodon.org.

00:49 Be careful with impersonating accounts on other instances.

00:52 There are many.

00:53 Keep up with the show and listen to over seven years of past episodes at talkpython.fm.

00:58 We've started streaming most of our episodes live on YouTube.

01:02 Subscribe to our YouTube channel over at talkpython.fm/youtube to get notified about upcoming shows and be part of that episode.

01:10 This episode of Talk Python to Me is brought to you by InfluxDB.

01:14 InfluxDB is the database purpose-built for handling time series data at a massive scale for real-time analytics.

01:21 Try them for free at talkpython.fm/influxdb.

01:25 And it's brought to you by the Pybites Developer Mindset Program.

01:28 Pybites' core mission is to help you break the vicious cycle of tutorial paralysis through developing real-world applications.

01:35 The Pybites Developer Mindset Program will help you build the confidence you need to become a highly effective developer.

01:41 Check it out at talkpython.fm/pdm.

01:43 Hey, Alan.

01:46 Welcome to Talk Python, I mean, welcome back.

01:48 Thank you.

01:49 It's a pleasure to be here.

01:50 Pleasure to have you back.

01:51 talking just a little bit before the show, realizing it's only been six years since you were on the podcast.

01:56 How about that?

01:57 Last time we talked about Python and engineering way back in 2017, which was a lot of fun and has some interesting lead-ins to some of the things that are actually created now.

02:08 You were hinting, "I'm working on this project "about simulating things with Python." Here we are talking about it.

02:13 - Yeah, I was just getting started then and the book has come out very recently.

02:18 So we've kind of bookended the project.

02:21 >> Absolutely. Well, it's quite a project and it's a very interesting one.

02:25 I think a lot of people out there, they hear maybe they should use Python instead of MATLAB or some other language for modeling their domain.

02:37 With your book that you put together, which is available for purchase or you can read it online for free, that works for you.

02:44 It has a bunch of different examples.

02:46 How many examples? Maybe 30 or so?

02:47 >> I think that's right.

02:49 There are examples in the chapter and then case studies that get into more detail.

02:53 Yeah, so there's a lot of different areas of specialization where you're bringing Python to answer problems.

02:59 And we're going to have a lot of fun talking about that.

03:02 But, you know, let's just do a quick catch up.

03:04 It's been six years since we last spoke.

03:06 What have you been up to?

03:07 Well, I've changed jobs a couple of times.

03:10 I was a professor at Olin College for 19 years.

03:14 I left at the end of last May.

03:17 And then I was at Driven Data for a little while, working on data competitions and machine learning.

03:24 And then recently I've joined Brilliant, where I'm doing curriculum design.

03:28 - That sounds very fun.

03:30 First of all, leaving college, you were at for 19 years.

03:33 That's a big deal.

03:34 - Yeah, I've been an academic pretty much all the way through.

03:38 I graduated from college, I went to grad school, I had my first job.

03:41 I was a faculty member at Colby College and then Wellesley College.

03:45 And then, as I said, I've been at Colby for 19 years.

03:49 So it's a real change of career.

03:50 How's it going?

03:52 It's good. I'm learning a ton.

03:55 You just, you know, whenever you start something new, you're always, you're on this, you know, the steep part of the learning curve, which is challenging, but it's also just exciting.

04:03 You're getting exposed to different things, learning a ton.

04:06 Yeah, absolutely.

04:07 And working at Brilliant now, you know, Brilliant's a good company.

04:12 There's, they've been past sponsor of the show, which is great.

04:15 And, you know, maybe just talk a bit about how you think about curriculum and design, since that's what you're focused on there. How do you think about putting together small class, not semester wide classes for people to learn? Yeah, that's a big part of it. It's a different format. It's smaller, as you said, it's interactive, in the sense that the learner is interacting with these new graphical elements and other ways of communicating ideas visually.

04:43 We use a lot of animation, and the text is deliberately very concise.

04:49 We're trying to show, not tell.

04:52 And that's a really different kind of curriculum design for me.

04:55 I'm used to, you know, I have a semester-long course.

04:57 I've got several hours a week.

04:59 It's very different.

05:01 And that's part of the challenge that was appealing to me, because I saw some of the courses and I was very impressed with the pedagogy and the opportunities that you have there with animation and sound and interactives.

05:14 It's a very different kind of teaching.

05:15 But it's not--

05:16 Yeah.

05:17 There's no-- I don't have any interaction with students.

05:20 So that's a challenge as well.

05:21 It's a big challenge.

05:22 I went from teaching in person to teaching online as well.

05:26 Not too brilliant.

05:27 But I think there's a couple of really big differences.

05:30 One is when you're either--

05:32 you have a student for a semester Monday, Wednesday, Friday at 11 to 12, or if you're doing a week-long training class in person, those groups are captive, right?

05:43 And so the constant need to keep their attention for every single minute is really different, right?

05:50 Because it's not impossible, but it's rare that students just get up and walk out because there's a five minute section they weren't engaged with, but they could easily leave a website.

05:59 - Yeah, it's particularly in a format like Brilliant, there's not a certificate at the end.

06:05 The motivation to keep going is you're having a good time and you're intrinsically interested in the topic.

06:12 So from my point of view as an instructor, the challenge there is to maintain that.

06:17 It has to be interesting, engaging all the time.

06:21 As you said, if you've got a five minute lull, you might lose your audience.

06:25 - Yeah, the other part that is a big difference is when you have those folks in class, even in a Zoom meeting to a degree, you get immediate feedback of how does an idea land?

06:36 If I present it this way, are they engaged?

06:39 or they clearly not getting it.

06:41 Whereas you put it out into the world at Brilliant or TalkByThon or wherever, and they've got to, you take a guess and you put it out there, right?

06:49 You don't get that feedback.

06:50 - Yeah, that's definitely challenging.

06:52 We do have some signals where we can see if a learner is getting stuck in a particular place or if we see a lot of drop off on a particular lesson, we can go in and try to figure out what's happening.

07:03 We get a little bit of explicit feedback.

07:05 Sometimes the learners will tell us what's wrong.

07:08 but most of the time, you're kind of, it's on you to make sure that that lesson works.

07:13 And your colleagues were constantly reviewing each other's lessons and looking for those places where we think we might lose learners.

07:20 - Excellent, well, I'm sure it's a lot of fun.

07:22 Some nice comments in the live chat out here, people appreciating that as well.

07:26 So one thing that you said you learned, which I haven't really learned before, is I don't even really know what it looks like, is Elm, tell us about Elm, what is this?

07:37 This is a programming language, as it says, it's primarily for web applications.

07:41 It is a functional programming language, and it's mostly a stateless computational model.

07:47 So it's very different from Python.

07:50 I've just been learning it.

07:51 This is what we use for a lot of the interactive elements.

07:54 And it was new to me.

07:56 I had honestly, I had not even heard of it.

07:58 But it's been great.

08:00 I just, you know, it's been a little while since I learned a new language.

08:03 It's been about 23 years since I learned a new language.

08:07 And I've done functional programming, but I haven't used it, you know, really for applications.

08:13 So this has been, you know, challenging, but fun.

08:15 It looks a little bit like Lisp meets HTML.

08:19 That's not bad.

08:20 Yeah.

08:21 Okay.

08:22 Certainly the semantics is very Lisp-like.

08:23 Syntax can be, although not too many parentheses.

08:27 Not as many.

08:29 You get that blister on your pinky or whatever finger you use for the phrase.

08:33 - Maybe you're right, index figure.

08:34 - Right. - Or ring figure.

08:36 - Yeah, and then the syntax, it has a couple of nice features like pipes.

08:40 So, you know, taking a value and piping it through a sequence of functions.

08:44 And of course, you know, functional features like passing around functions as first class objects and partial function evaluation.

08:50 - Is this a front end language like a JavaScript or is it a back end language more like Python?

08:54 - Yeah, no front end, it's getting compiled into JavaScript.

08:57 But it is strongly typed, like extremely strongly typed.

09:00 like integer and float are incompatible types.

09:03 So--

09:04 - There we go.

09:05 - So very strongly typed, but that along with a lot of error checking gives you some more confidence that if you put this on a webpage and a large number of people interact with it, there are maybe fewer errors that can get past a compiler.

09:19 You know, a really dynamically typed language like Python, you're rolling the dice, you deploy that code.

09:26 You know, some of those lines of code have never run.

09:28 and the first time they do, you might be in trouble.

09:30 - The type hints are helping.

09:32 mypy is helping, but that's not a runtime thing.

09:34 Pydantic, for example, is a runtime version, that option that helps a lot.

09:39 Before we move off this front end stuff, then we'll get to the modeling.

09:42 Have you been tracking PyScript and the front end Python stuff?

09:46 Is that interesting to you?

09:46 - It definitely is, and especially from a teaching point of view, getting people into Python, one of the challenges from way back has always been, How do you get a new programmer to the point of executing their first line of code?

10:01 That is definitely not as big a barrier as it used to be.

10:05 It used to be, you had to install things and download things and all that.

10:09 But yeah, the lower that barrier, the better.

10:11 And yeah, I think PyScript is one of the things that can help to lower it.

10:14 - Yeah, I do too.

10:15 I'm very excited about PyScript.

10:17 It had been interesting in the realm of kind of different ways to execute notebook style code and so on when it was based on Pyodide.

10:25 And now that they're talking about moving to MicroPython, it starts to get into the realm of it.

10:31 It could download the same size as Vue or Angular almost.

10:35 And then it becomes, maybe you can make real apps with this thing.

10:38 So I'm looking forward to see where that goes.

10:39 - Oh, interesting.

10:40 I had not heard about that.

10:42 So that's exciting.

10:42 - Yeah, yeah.

10:43 One of the big things that they're trying out, I had a couple of the folks on from various projects four or five months ago, is they're trying to, instead of using full CPython as the foundation to use MicroPython, which means the download goes from like 10 megs to 100K.

10:59 - Yeah.

11:00 - And you're talking 100K downloads, you're like, you know, that's kind of like a big JavaScript and if it's over a CDN and it's cached, you know, maybe you could just make that part of your app in general, not as a specialized thing.

11:09 - Yeah, that's interesting, particularly because I think one of the other challenges for Python right now is it takes a while before you're doing really interesting stuff.

11:20 And JavaScript, for example, you can immediately start doing front end kinds of interactions and a beginner can start to write programs where it resembles the kind of apps that they're using.

11:34 And I think that's a pretty important hook.

11:36 For Python, that's a long path.

11:38 And so it sounds like this could shorten that path quite a bit.

11:41 - It sure could, that would be very interesting.

11:43 I think your insight there comes in as a bit of a teacher as well, right?

11:48 Like it's one thing as an engineer, Like I'm gonna build this thing, so I'm gonna choose this language for such and such.

11:53 But as a teacher, you realize like, if you don't have a win early enough in the learning cycle, then you might just bail.

12:00 And for a lot of people, a win is I have a UI.

12:02 I have a thing that looks like an app that I could see on my phone, or I could see in my web browser or something like that.

12:09 - Yeah, definitely.

12:10 Or a robot or some interaction with the real world.

12:13 But yeah, converting Fahrenheit to Celsius (laughs)

12:17 is not gonna do it.

12:18 - No, but Celsius to Fahrenheit, that one is the one.

12:20 That's the one, no.

12:21 - Please forget it.

12:22 - But speaking of converting numbers and so on.

12:26 This portion of Talk Python to Me is brought to you by Influx Data, the makers of InfluxDB.

12:34 InfluxDB is a database purpose-built for handling time series data at a massive scale for real-time analytics.

12:41 Developers can ingest, store, and analyze all types of time series data, metrics, events, and traces in a single platform.

12:49 So dear listener, let me ask you a question.

12:51 How would boundless cardinality and lightning fast SQL queries impact the way that you develop real-time applications?

12:57 InfluxDB processes large time series data sets and provides low latency SQL queries, making it the go-to choice for developers building real-time applications and seeking crucial insights.

13:09 For developer efficiency, InfluxDB helps you create IoT, analytics, and cloud applications using timestamped data rapidly and at scale.

13:18 It's designed to ingest billions of data points in real time with unlimited cardinality.

13:23 InfluxDB streamlines building once and deploying across various products and environments from the edge, on-premise, and to the cloud.

13:32 Try it for free at talkpython.fm/influxdb.

13:36 The link is in your podcast player show notes.

13:38 Thanks to InfluxData for supporting the show.

13:41 Let's talk about your latest book.

13:44 You have a lot of books over at Green Tea Press.

13:48 This is your home for where your books go, right?

13:51 - Yeah.

13:52 - As well as links to other ones you've written at places like O'Reilly.

13:54 - Right, yeah.

13:55 So everything starts here and there's a free version of all of the books and you can get to it from here.

14:01 You can see kind of the evolution of my process 'cause some of the earlier books, some of them look better than others.

14:07 Let's put it that way.

14:08 And then links there for the ones that have been published, There are hard copies available and electronic versions in convenient formats.

14:17 - I think we might have spoken about this six years ago, but it was six years ago and I'm not sure, and I imagine a lot of people don't remember it.

14:22 Did we talk about your textbook manifesto?

14:24 What is this?

14:25 - That was what really got me started.

14:27 And it came from my frustration with existing programming books, but also just textbooks in general.

14:35 The way they're used in a lot of classes, they are expensive, they are way too big.

14:40 you know, if there are 700 pages or 1000 pages, nobody's gonna read that in a semester.

14:47 And if you're assigning 50 pages a day, I think, you know, you're kidding yourself and the students are not getting much out of that.

14:55 So this is the idea that you can write a book that your students can read and understand and that is at the size where you can do something like a flipped classroom, where they are actually gonna do some reading before class, not 50 pages, but 10 maybe.

15:13 Come to class.

15:14 Now, as an instructor, I can help to figure out, what are the problem spots?

15:18 What should we cover in class?

15:19 What can we do to practice those ideas in class to take advantage of the time when we're actually in a room together?

15:26 So, read when you're on your own, come to class, let's do things that are more useful than me standing at the front of the room talking to you.

15:33 - Yeah, sure.

15:34 Okay, that makes a lot of sense.

15:36 So let's dive into your book, modeling and simulation in Python.

15:41 Now, as I'm going through this, I'm not sure what I would classify it as.

15:45 Is this a science book?

15:47 Is this an engineering book?

15:48 Is it a programming book?

15:49 It kind of feels like it's a little bit of all of those.

15:52 How would you, what's your elevator pitch here?

15:54 - Yeah, it's that.

15:55 It is the solution to a chicken and egg problem, which is that in an engineering curriculum, a science curriculum, and maybe even more broadly, I think for teaching physics and math to almost anyone, a computational approach is really appealing.

16:12 There are things that you can do with computation that are very difficult to do analytically.

16:18 You can do more interesting things faster.

16:21 So that's the appeal.

16:22 The hard part is most people are not coming into college with programming experience.

16:28 Some do, most don't.

16:30 So whatever your curriculum is, but let me focus on engineering because that's where I was.

16:34 You've got a cohort coming in who have programmed, a cohort who have never programmed, and then kind of maybe a middle, maybe they've done like HTML or something like that.

16:43 And now I want to teach them physical modeling, but I have to do kind of three things at the same time.

16:49 The modeling piece, the physical models, the domain knowledge, you know, if it's a scientific system or an engineering system, whatever it is, and programming.

16:59 And there's no good sequence.

17:02 Like, I can't send people out to take a computer science class, and then they come back and we do computational science.

17:10 It just, there are a number of reasons that that doesn't work.

17:13 And same thing, if they go, let's say they go take calculus or linear algebra, and then they come back, collecting that knowledge and then trying to synthesize it is really hard.

17:22 And it means it's a sequence of courses instead of starting in right from the beginning.

17:27 So those are some of the problems that we were working with.

17:30 And the best solution, at least we think, is put it all together.

17:35 We're going to teach people how to program using these physical models as examples.

17:40 And so they're getting some mathematics, not a ton, kind of a gentle introduction to differential equations, some physics, but it's a bit more broad because it's not just mechanics, and then some programming, but it's really a subset of programming that's focused on those domains.

18:00 So that was my best explanation.

18:02 >> Oh, it's good. It's good.

18:04 I can see how there's not a real great solution for that in a college curriculum.

18:08 You send them off to take a CS class and they might come back of having done a introduction to programming course but still not really be able to do too much, or an algorithms course and like, I can really tell you about a doubly linked list.

18:21 You're like, "All right, you're still not ready. I'm sorry." It's a challenge to bring these things together.

18:25 I think that's generally a challenge in education a lot, us to cross these things over and why a lot of people feel like certain things are that's useless. I'll never use that. That doesn't have any value to me because it's taught in such an abstract, isolated silo.

18:40 Yeah. And you never close the loop because we're making a promise. If, if, if we send a student to take a prerequisite class, the contract is you go do this thing. And then when you come back, we're going to make it pay off.

18:53 You're going to see the benefit and we never do.

18:58 It's tough, it's tough.

19:00 So like we said, there's about 30 different problems in here like population growth, how long does it take for your coffee to cool?

19:08 What's the optimal way to transfer it to work?

19:10 Glucose, disease modeling, as well as vaccines.

19:14 People can check out.

19:15 So I thought it might be fun for us to just pick out however many we get through.

19:20 Let's say we take a shot at 10, we'll see if we get there.

19:22 10 of these and just kind of talk through what is the problem?

19:25 What are some of the Python ideas at play here?

19:28 or maybe what are some of the math or science ideas as well?

19:31 So you wanna start with world population?

19:33 - Sure, yeah, this one was interesting for me.

19:36 It's something I didn't know very much about.

19:38 So I got to learn.

19:40 I think it's a sweet spot of, you don't need a lot of computational tools 'cause this one comes very early in the book.

19:47 But you can study a real system that's happening right now and get to understand it.

19:53 So it's a discrete system.

19:55 So we're just counting the number of people and modeling population growth.

19:59 And so the number of new people in any year is gonna be some function of the current population.

20:06 And so we can introduce a couple of different models, you know, starting very simple.

20:10 One of them says, what if we have the same number of births and deaths every year?

20:15 Well, okay, the population would grow linearly or maybe shrink linearly, but it would be linear growth, constant slope.

20:22 All right, what if it's proportional?

20:24 What if we have a birth rate that's, you know, proportional to the current population.

20:28 Some people might know you're gonna get exponential growth.

20:31 And I think if you ask people what's going on in the world right now, they probably believe that we are experiencing exponential growth.

20:40 - That was my thought when I read this.

20:41 I thought, oh yeah, okay, so we're probably talking about geometric growth and those types of things, yeah.

20:45 - Right, well, that's the model.

20:47 And we try to fit that model to the data and it doesn't.

20:50 And so that's where the first lesson comes from, which is you can do all the theory you want and eventually you will collide with data.

20:57 And your theory may or may not survive the impact.

21:00 And that's what happened here is, the population is currently not growing exponentially.

21:06 Going back to about 1960, it's pretty close to linear.

21:10 And that one's kind of weird because, okay, wait a minute, if it's linear, that means we have roughly the same number of births and deaths every year, but how?

21:20 How do you get 7 billion people to coordinate and make that happen?

21:24 So there are a couple of ways to think about what's happening.

21:27 One of them is the logistic growth model, which starts out exponential, and then as you approach a carrying capacity, you approach this limit, it approaches that limit asymptotically at the high end.

21:39 And in the middle, there's a piece that looks linear.

21:42 So you could look at that model and you could, okay, maybe that's what's going on, except it turns out that's not quite right either.

21:48 And that's where the domain knowledge comes in.

21:51 'Cause so far, it's just been programming and looking at data.

21:55 At some point, you have to talk to a demographer because they actually know what's going on.

21:59 And what's going on turns out to be very interesting.

22:02 It's the demographic transition.

22:04 And this is a social and economic phenomenon, which is as societies, countries become more economically developed and have more personal freedom, fertility rates tend to go down.

22:16 And they very often, it's often dramatic.

22:19 It would be a shift from lifetime fertility of five, six, seven, or eight children down to very close to two.

22:27 And 2.1 is the replacement level where population levels off.

22:30 A lot of developed countries right now are at or below replacement in terms of birth rate.

22:36 And so the populations are leveling off or starting to decline.

22:40 - Yeah, there's gonna be some knock-on economic effects.

22:43 Although I don't know that those were modeled in your book, but you know, like how does things like social security and support nets happen when the thing being supported is heavier or larger than the people doing the supporting, right?

22:56 - Right.

22:57 - Speaking of economies like Japan, for example, and so on.

22:59 - Yes, yeah, they've been below replacement level for quite a while.

23:02 So in some sense, they are the predictor of what a lot of other countries are looking like now.

23:07 And as you said, you know, vast economic consequences.

23:11 Now, that's where I stopped.

23:12 It's not an economics book.

23:14 (laughing)

23:14 - Yep.

23:15 - That's where it was, I think, fun for us to, you know, get to that point and say, look, okay, we have found some interesting things, and now, you know, go explore, 'cause there's a lot more, lot more to learn about this.

23:26 - Yeah.

23:27 I doubt it would surprise the listeners that the standard data science pieces are at play here.

23:33 Jupyter Notebooks, Pandas, Matplotlib, those kinds of things, right?

23:37 - Yeah, there it is.

23:39 From Pandas, read HTML.

23:40 Yes, whenever we can, we try to get real data.

23:43 One of the goals of this class is to connect students to the real world, and that's one of the places where data science is a real opportunity because the availability of data is great and the computational tools for working with data are very good.

23:58 Now, obviously in Python, Pandas is one of the popular libraries for data.

24:03 There are others, more generally, there's R, Matlab.

24:07 This is a real, I think, teaching opportunity.

24:09 - One of the things I thought was interesting from a lot of these was the acquisition of the starter data for a lot of these.

24:17 So for example, on this one, you have, you're getting the world population estimates from a table.

24:22 And I think this table originally comes from Wikipedia, but you've saved it locally because you're smart.

24:28 And in case they change the format of that page, you don't have to rewrite your book.

24:32 But you know, it really highlights certain things that are just so easy if you know the way, but are not necessarily obvious.

24:39 So for example, here you have from pandas import HTML, you can just go read HTML and you give it a, either a URL or just a HTML file in this case.

24:50 And then you can say, well, we just want the second table that was in that HTML.

24:54 And it literally grabs out of Wikipedia, it grabs the table and turns it into a Pandas data frame, which is pretty remarkable, honestly.

25:02 - Yeah, well, I have mixed feelings about this 'cause it's great that we can get the data and it's easy with these tools.

25:10 It's a little bit sketchy in the sense that, as you said, we have to, What if Wikipedia changes the format?

25:17 This is not exactly data that anybody intended to export.

25:22 So I think I would prefer to get this data from the original source in a format that was really intended.

25:30 An HTML table is not exactly a data transport format.

25:34 - No, an Elm table.

25:35 No, just kidding.

25:36 I mean, it's fragile in a lot of ways.

25:38 Somebody could insert a column, delete a column, more likely than insert one, right?

25:42 To add, oh, we found a new data source.

25:43 we added it and all of a sudden all of your stuff breaks.

25:45 But it could even be as simple as we added more information to the Wikipedia page and now there's a table that precedes this table in HTML.

25:53 So when you say, get me the second table, you actually get some unrelated thing, right?

25:56 So obviously, I mean, it's effectively web scraping behind the scenes, which is always fraught with instability.

26:03 - But you're taking advantage of what Wikipedia did, which is to collate all of these sources.

26:09 People have gathered this and curated this collection of data.

26:13 So that's why I say mixed feelings, 'cause it is a great opportunity to take advantage of that, but web scraping might not be the best way to do it.

26:21 - Yeah, quick question from Marwan.

26:24 Does the book include attempts to visualize the simulations like modeling a bouncing ball becomes more rewarding if you can see it in action?

26:31 - Definitely, and that comes up in the third part of the book, which is about mechanical systems.

26:36 And those are the things where, yeah, we've got objects moving in space and that begs to be an animation.

26:42 There's not a ton of it, because it's a little...

26:45 The tools for animation in Jupyter Notebooks using Matplotlib are a little bit clumsy, so I didn't get deep into it.

26:54 But definitely the point where you get to see the simulation and you see the objects moving in space, you, I think, appreciate it, for one thing.

27:03 You also, I think, get to debug it, because your eye is really good at seeing physical errors.

27:10 If your physical model is doing something incorrect, it's gonna, you're gonna see it instantly.

27:15 It's gonna bother you.

27:16 - Yeah, that does look right.

27:18 And you know, it's probably not necessarily a problem with the animation, with the modeling, but even things like acceleration and deceleration.

27:25 If a thing just goes over and stops, you're like, oh, it didn't.

27:27 That's not right, it shouldn't do that.

27:28 It should slow down and come to a stop.

27:30 It didn't hit an imaginary wall.

27:32 This portion of Talk Python to Me is brought to you by the PyBite Developer Mindset Program.

27:40 It's run by my two friends and frequent guests, Bob Delderbos and Julian Sequeira.

27:45 And instead of me telling you about it, let's hear them describe their program.

27:49 Are you trying to code your way to a more flexible and impactful career, but aren't getting anywhere?

27:56 If so, the Pybites Developer Mindset Coaching Program, PDM, can help get you unblocked.

28:02 Imagine this, building and deploying your own applications, mastering Python, and getting personalized one-on-one coaching every step of the way. We know the struggle of tutorial paralysis all too well and that's why PDM focuses on real-world applications. Our mission is to help you build confidence as an effective developer. And remember, this isn't a boot camp. It's a highly personalized invite-only program. You won't be just another face in the crowd in some classroom, but a valued part of our inclusive community.

28:37 And are you ready to give back to the open source world?

28:40 With PDM, you'll gain the skills and the confidence to contribute meaningful code to the projects that matter to you.

28:49 And it's not just about the coding.

28:51 It's about joining a community of like-minded developers, fueling your intellectual curiosity and making a difference with your skills.

29:00 So, are you ready to take your Python skills to new heights?

29:04 We hope you are.

29:05 So please apply now for the PDM program.

29:08 With Pybites, you don't just learn to code, you code to learn, to grow, and to give back.

29:16 See you on the inside.

29:17 - Apply for the Python Developer Mindset today at talkpython.fm/pdm. It's quick and free to apply.

29:26 The link is in your podcast player show notes. Thanks to Pybites for sponsoring the show.

29:30 All right, next one to talk about is epidemiology. And in this one, we talk about the freshman plague. People maybe with regard to college have heard of the freshman 15, like a certain amount amount of weight a lot of people seem to put on from the stress of going to college.

29:49 But the freshman plague is more about coming together in a big group from all over the place more than maybe people are used to coming from their hometown.

29:58 Yeah.

29:59 Well, we started this example.

30:01 Well, let's see.

30:02 I think we were working on this in 2017, but we were using this example previously.

30:08 So before COVID is the point.

30:10 And I guess at the time it was sort of a fun example.

30:13 like, "Hey, everybody gets a cold "during the first few weeks of the semester, ha ha." And it's less funny now, but it's also definitely more relevant.

30:22 We're thinking about modeling an epidemic and modeling interventions, and in particular, understanding a concept like herd immunity.

30:32 So if you're talking about public health and what's our civic obligation, it's not just looking after yourself.

30:40 there is a public consequence to your individual decisions.

30:44 - Yeah, absolutely, there sure is.

30:47 And yeah, this used to be somewhat of an interesting example and then it became one of the biggest stories, maybe of our lifetimes, who knows.

30:58 Anyway, so this is the Kermack-McKendrick model.

31:01 And there's a couple of interesting things that I thought were interesting models here.

31:05 So in this model, you have the susceptible folks who are not yet infected, but potentially could be.

31:11 You have the recovered people, and you assume they have some kind of immunity, at least for their freshman year, I guess.

31:16 And then the people who are infected.

31:18 And you talk about this idea of having, what's it called, stock?

31:22 Yeah, stock and flow diagrams.

31:26 Those are pretty interesting, 'cause you think of the freshman class as kind of a fixed set, right?

31:34 - There are some different versions of the model.

31:36 And right, Kermack-McKendrick, nobody calls it that.

31:39 Everybody just calls it an SIR model, 'cause those are your three.

31:42 - SIR, okay. - And you can extend it.

31:43 There are additional groups.

31:46 And what you're modeling is the flow of, in this case, it's people, from one stock to another.

31:52 So someone who is susceptible, if they become infected, they move into the infectious stock, and when they recover, they move into the recovered.

32:02 So this is the simplest model, and usually it's a fixed population.

32:06 The reason that this is where we start is that this lends itself to a differential equation that we can do some work with.

32:15 We can't solve it analytically, but there are some useful analytic methods.

32:19 And we can also work with it computationally.

32:22 So this is one of several examples in the book where we do both.

32:25 And that gives us a chance to show the strengths and weaknesses of analysis and computation, and also to do some validation by comparing the results from one and the other.

32:37 So yeah, that's why this is where it is in the book.

32:40 It's kind of the transition from the discrete systems in part one to the first order continuous systems.

32:47 Yeah.

32:48 Okay.

32:49 One of the things that, I don't know, maybe I should have just known this or not, but was interesting to me was thinking about applying differential equations to a discrete set, right?

32:58 - I think of differential equations of being either applied to like maybe an interval, all of the real numbers or the complex numbers, or, but not to 90 things, you know?

33:10 Which that was surprising, I thought.

33:12 And then your ability to model that in Python was pretty cool.

33:16 - Yeah, so I'm glad you mentioned that because it's a really useful modeling method and it's a good example of the kind of modeling decisions that you're making all the time.

33:26 And it can go in both directions.

33:27 You can take something that you know is actually discrete, like people, and treat it as if it were continuous.

33:34 And that works pretty well if you have a large population.

33:36 Obviously, if you get down to very small numbers of people, it starts to break down.

33:40 And that's where we can have a conversation about modeling and say, look, this is an approximation, right?

33:46 This isn't how the world really is, but I can pretend I can run the model and those results can be useful even if the model isn't perfectly realistic.

33:56 'Cause that's how models work.

33:58 None of them are perfectly realistic.

34:01 So that's, I think one of the goals of the book is to engage the learner in those kinds of discussions about modeling decisions.

34:10 'Cause I think that's missing in a lot of science classes.

34:15 If you think about the traditional freshman physics class, you are told what the model is.

34:20 So we're gonna do a block on a frictionless plane.

34:23 We're gonna do a projectile moving in a vacuum.

34:26 Well, the frictionless, that's a modeling decision.

34:29 It says we're going to temporarily ignore friction in order to solve this problem.

34:33 But you as the learner didn't get to make that decision.

34:36 You're not required to figure out the consequences of it or think about when it doesn't apply.

34:43 And that's what we really wanted to get at here.

34:44 - Yeah, that's a really interesting point.

34:47 And that's why a lot of people think math is just a bunch of formulas or science, hard sciences are often just a bunch of formulas.

34:55 then you just got to remember to use the formula because it just goes from this sterilized version, final version, without understanding, going from observation to making your way through it till you end up with something like this, right?

35:10 - Yeah, yeah.

35:12 Now there's a scary version of this, which is that it's not objective.

35:16 'Cause if there's one answer and your job is to figure out what's the right formula, apply the formula, get the answer at the back of the book, That's nice and objective.

35:26 And, you know, we want our science to be objective, but regardless of what we want, it isn't.

35:32 'Cause it's based on modeling decisions.

35:34 All science is based on modeling decisions.

35:38 And those are subjective decisions.

35:41 It's deciding whether a particular model is suitable for purpose.

35:46 Depends on what you're trying to do.

35:47 If you want to make a general, qualitative description of a system, maybe you can use a simple model.

35:53 maybe you can leave friction out of it.

35:55 If you want to land an orbiter on Mars, or a rover on Mars, you might need a more detailed model.

36:03 - Yeah, if you're off by 1% of one degree, it's gonna matter by the time you get there.

36:10 - Yep.

36:11 - Where's that plane in again?

36:12 Yeah, so in this one, you talk through using this, a couple of types and ideas in here, with states and systems, and it sounds like in the book, it looks like in the book, you provided some foundational building blocks for modeling the mod-sim module, right?

36:29 So in addition to just having the books, you've got a bunch of Jupyter notebooks that people can check out, and as well as some code to kind of help this modeling here.

36:38 You want to talk a bit about that?

36:39 - Sure.

36:40 So this was my attempt to use some programming ideas to communicate modeling ideas by creating these objects.

36:48 So one of them is a state object, and it represents the state of a system.

36:53 So if you think about an object moving through space, it has position and velocity.

36:58 And at any point in time, if I write down the position and velocity, that's the state of that object.

37:04 So in object-oriented programming, that begs to be an object.

37:08 It is a state object that contains position and velocity.

37:12 Now, under the hood, it is a panda's series that is mapping from names to values.

37:20 Similarly with a system object, that's actually a Python namespace.

37:23 So there are a couple of different things, but this one's a namespace.

37:26 And it represents everything that you need to know about a system in order to model it.

37:32 So it'll be the parameters of the system.

37:34 If it's, let's take that projectile moving in space again, it might be mass, cross-sectional area, coefficient of drag, density of air, temperature of air, all the things to model.

37:45 One of the examples is like a baseball in flight.

37:48 - Excellent.

37:49 you go through, you model, you run the simulation over time and eventually end up with a graph that shows, you know, a little over half of the students got lucky and didn't get the plague, but about half of them did at some point throughout the year or the semester, whatever it was.

38:03 - So that's, you can do a single run in order to simulate a hypothetical course of the disease.

38:10 And now you can start to vary the parameters and say, you know, what if the disease is more infectious?

38:16 What if the recovery time is longer or shorter?

38:18 What if people start washing their hands?

38:21 And what if we start vaccinating them?

38:23 And so by varying the parameters now, you can see how the output of the model depends on the system parameters.

38:31 And that's where another one of these objects comes in.

38:33 We have a thing that's called a sweep series.

38:36 It's a mapping from a hypothetical parameter to the output of the model using that parameter.

38:43 - Yeah, when you hear people talking about COVID, I remember them talking about the infection rate, Is it bigger than one?

38:49 Is it less than one?

38:51 All of those things were very relevant here.

38:54 >> Right. Yes. The R0 and R, the contact rate.

38:59 That term comes straight out of this model.

39:03 That is the ratio of the two parameters here.

39:06 Beta is the rate of infection, depending on how many people are susceptible and how many are infected.

39:13 Gamma is the rate of recovery, which is the fraction of infected people who recover in a unit of time.

39:20 And the ratio of those two things is the contact rate, the R0.

39:25 Yep. All right. Well, way more relevant these days than it was a few years ago.

39:28 Oh, yeah. We all know a lot more about epidemiology than we used to.

39:33 We sure do. My daughter was just enjoying going back and watching some of her favorite YouTubers videos from February and March of 2020, just laughing.

39:44 They were saying, "Oh, it looks like we're gonna have to stay home for a week." And she's just like, "They have no idea.

39:49 They have no idea." - Yeah. - Okay.

39:50 All right, let's go on to something more fun.

39:52 One of my favorite things, coffee.

39:54 - Come on.

39:56 - Yeah, awesome.

39:57 And so tell us about the coffee cooling problem that you solve here.

40:00 - This one's a classic.

40:02 The source that I'm aware of is a Martin Gardner column in Scientific American.

40:07 And it posed this question, which is if I get my coffee and I also have some milk and I'm planning to add the milk to the coffee, should I add it now and so that they cool down together or should I drive to my destination?

40:21 Let's suppose you're commuting.

40:22 Should I get to my destination and then mix them with the goal of getting the coffee down to a pleasantly drinkable temperature as quickly as possible?

40:31 - Okay, yeah, you pick it up, maybe hit one of those drive-thru coffee shack things that's just stands on its own.

40:38 You know, it's always too hot here.

40:39 And like, how should I do this so that I can basically have a perfect coffee when I arrive where I'm going, right?

40:45 Okay, excellent.

40:46 So this one involves Newton's law of cooling, right?

40:51 Did I get that right?

40:52 - You got it.

40:53 - And the heat transfer, there we go.

40:54 Newton's law of cooling.

40:55 Another differential equation here, as they want to be in simulation.

41:00 - Yes, right.

41:01 So this is another one, which is a first order differential equation.

41:04 You can do it two ways, which is you can break it up into discrete steps in time.

41:09 And so your differential equation becomes a difference equation.

41:13 And that's a natural thing for a programming language because now it's a loop.

41:18 And during each time step, you figure out the difference between the coffee and the environment.

41:23 That tells you the rate of change.

41:25 And then that lets you make a prediction for the next time step.

41:29 And so if you repeat that process, that is Euler's method for solving differential equations.

41:35 So there's kind of a natural transition there.

41:38 And then I think for this problem, we don't yet use the ODE solver, but then later in the book we do.

41:44 And that's trying to motivate, why are we using this library?

41:48 Well, there are limits to Euler's method.

41:51 At some point, it's not very accurate.

41:53 - Yeah, it might not be accurate enough for sure.

41:55 And ODE, ordinary differential equations for people out there listening.

41:59 - You got it right, thank you.

42:01 - Yeah, it's a very differential equation.

42:03 And so, you know, Python has, you know, SciPy, SciPy has several functions that will solve differential equations for you.

42:10 And so we're kind of, we're sneaking up on that.

42:12 - Yeah, okay.

42:13 It is interesting that you can say, yeah, sure, it's a differential equation and those might sound scary and hard to solve and all those kinds of things, but you can subtract two numbers.

42:23 You can do a simple thing and looping is good.

42:25 And so if you just make your loop have small enough increments, you're kind of doing the same thing that, you know, taking the limit to get to calculus does anyway, right?

42:33 - Yeah, we're kind of, we're short circuiting what I think is a really unnecessary loop, which is there's a lot of physical modeling where you take a system, you write a differential equation, but then you can't actually do anything with that differential equation.

42:49 You can't solve it.

42:50 And so you're going to have to use numerical methods anyway.

42:53 And if you look at what the numerical method is doing, it's basically Euler's method.

42:58 It's, you know, they're more complicated versions, but it's the same idea.

43:03 We're gonna take time, we're gonna break it up into discrete steps, and we're basically simulating the system.

43:09 So what we do in the book is cut out the middle, and go straight to simulation.

43:14 There's almost no reason to write a differential equation if you can't solve it.

43:19 You might as well just start with a simulation.

43:21 - Yeah.

43:22 So what's the story with the coffee?

43:23 How should I approach this?

43:25 - I'm not gonna give it away.

43:26 (laughing)

43:27 People got to earn it.

43:28 They got to come figure it out, huh?

43:30 You do have at the end.

43:31 So one of the big challenges here is not like, how soon in my trip should I get the coffee?

43:36 How long should I let it sit or any, those kinds of things.

43:39 It's one of the important parts is you have milk or cream you want to put into the coffee.

43:44 You should decide if you put it into the hot coffee, the cold cream right away, that starts it somewhere.

43:49 Other than if you maybe wait till you get to the office and then mix the cream.

43:53 So people can come back and solve that about the milk.

43:57 Here's a question.

43:58 This came up, one of the people I chat with on Twitter posed this question, it's a variation of this.

44:04 He said he was disagreeing with his wife about using an electric kettle.

44:08 The question is, let's say that you heat up a kettle full of water, you use half of it, and now the other half is there.

44:17 You have a stay warm feature where you can set the kettle so that it keeps the water at let's say tea making temperature for you, or you can shut it off, let it cool down, and then later on when you want to make tea, heat it up again.

44:31 Now, let's leave out of it for now that the time that it would take to heat up again.

44:36 Let's say you don't care about that.

44:37 You just want to know about energy consumption.

44:39 Is it better to keep the water hot or let it cool down and then you have to use all that energy to heat it up again?

44:46 Do you want to get in on this?

44:48 - Yeah, yeah, my guess is it's better to let it cool down because the wider temperature gradient you keep between the ambient air and the water, the higher rate of loss of temperature, which you actually had a really interesting piece, tidbit of information here.

45:05 You said, okay, if the coffee cools from, you know, cooking temperature down to drinking temperature, that's a ton of energy.

45:11 It says to give you a sense, if you're able to harness all of that heat to do work, which you can't because of inefficiencies of engines, But if somehow you had a magic levitating engine that converted a heat energy to potential energy, you could use this lost energy to lift the cup of coffee from sea level to 8,500 meters just under the height of Mount Everest.

45:33 That's an insane amount of energy when you think about it.

45:36 - That is, and honestly, I did this calculation 10 times and I'm still not sure.

45:41 - It's gotta be wrong.

45:42 No, it's awesome, I love it.

45:45 - I believe that this is true, but if someone contradicts me, I would be grateful because I just wanna know.

45:51 But at least according to the calculation I did, the difference between mechanical energy and heat energy is just, it defies your intuition.

46:00 And it's really, it's thermodynamics that is the difference because to take something from sea level up to the top of Mount Everest is organized energy and heat is disorganized energy.

46:13 And what that means is you can have quite a lot of heat and not be able to do much work with it because it's disorganized.

46:20 - Yeah, okay, excellent.

46:22 Before we move off this one, I remember you were, I can't remember who I saw, I think it might've also been Scientific American, but there was a debate about coffee of whether blowing on it, if it was the cool air hitting the water, that would cool it, or if it was just the evaporation.

46:36 And someone did an experiment where they put a fan on it with cool background air, and they put a hairdryer with hot air blowing on it, And it's still, it's the evaporation.

46:47 It's like these state changes and this energy of this heat loss.

46:50 It's pretty powerful.

46:51 - Yeah, the latent heat of vaporization is a huge number, which is good 'cause that's what keeps us cool.

46:57 That's why sweat works.

46:58 - That's right, that's right, it's good.

46:59 We're coming up on summer, so we're gonna appreciate that, I'm sure.

47:02 All right, on to the next one.

47:04 Limits to growth.

47:06 Tell me about this one.

47:06 - Yeah, so this is related to the population growth that we were talking about earlier.

47:12 and this is developing the logistic growth model, but then trying to figure out, is that really what's going on?

47:19 Is human population leveling off because we are hitting resource limits?

47:25 Like, are we running out of food or other resources?

47:29 The answer for now is no.

47:30 I mean, we can certainly look at places in the world where resources are scarce, but that's not what's limiting growth.

47:37 Interestingly, it's almost the opposite.

47:39 It is the places that are most economically developed where resources are most available, where fertility rates are-- - That's a good point, yeah.

47:47 - The fastest. - Okay.

47:48 - And it's not being driven by death rate.

47:50 That's the other thing.

47:51 I think the other reason that we know that what we're seeing is not a resource limit because that would drive up the death rate.

47:59 And that's not the case.

48:00 The death rate is dropping at all ages in all places.

48:04 Well, with a few exceptions.

48:06 - Yeah, yeah, of course.

48:07 So this one's kind of about finding that equilibrium.

48:09 Yeah, this is taking the model to its limit and saying, okay, if we use the logistic model, even though we think physically, this isn't exactly what's happening, can we make a prediction about where we think world population will level off and possibly even start to fall?

48:27 And one thing that surprised me is that even though this model is very simple and not realistic, The predictions that come out of it are not wildly different from what we're getting from real demographers.

48:40 So the UN and the US Census both have projections for world population out to about 2100.

48:48 And they predict world population will probably level off around 2100 at somewhere between nine and 11 billion people.

48:57 And this very simple model is not too far off from that.

49:01 That's a pretty wide range.

49:02 (laughing)

49:04 You know, a lot of things can happen.

49:05 - Yeah, sure.

49:05 A lot of assumptions and current equilibrium might change.

49:10 Right?

49:10 Like, for example, climate change.

49:12 Who knows what that does?

49:12 - Yep.

49:13 That's big, but right now, the biggest drivers are the indices of world development.

49:19 If you look at things like health and especially child mortality.

49:23 When child mortality is low, fertility goes down.

49:27 That's counterintuitive, but to some extent, people are having, let's say, enough children.

49:34 If your children have a very high chance of survival, enough is a smaller number.

49:40 It's a funny way of looking at it, but that's, so health, economic opportunity, human rights is one of the other big driving forces.

49:48 - Right, autonomy.

49:49 - Yeah, freedom to make reproductive decisions and education, that's the other big one.

49:54 - Yep, yep, okay, another one from the health area, glucose and insulin.

49:59 - Yeah, what's the problem we're solving here?

50:01 - So this is a model of the system in our bodies, and actually I think all mammals, to regulate blood sugar.

50:09 And it's the interaction of glucose or blood sugar and the hormones that cause blood sugar to go up and down, the primary one being insulin.

50:17 And there's a model that's called the minimal model.

50:20 And it was proposed by researchers who were trying to describe, you know, what's the minimum set of interactions that we need in order to have a model that fits the data pretty well.

50:32 This is an example that I liked partly because the discussion of modeling was so explicit that there are some quotes from these papers where they are more open about their thinking than usual.

50:46 It seems like people kind of hide the models.

50:49 And these guys were really thinking about what do we need to include and what can we leave out And how do we evaluate whether the model is good enough?

50:58 And the way they did good enough is they actually had data from studies of blood sugar and diabetes where people have a challenge test.

51:09 They are actually injected with glucose solution, and then they monitor their insulin levels and blood sugar levels over three hours with measurements every two minutes.

51:22 And so you can see blood sugar goes up and then insulin goes up, and then glucose comes down, and then insulin comes down, and then you eventually get back, if everything works, to your steady state base levels where you started.

51:36 - I see, so if you don't have diabetes, your body should react that way, but maybe if you do, it can't produce the insulin to counterbalance that or something like this, right?

51:46 So I think I've heard of that as a test for seeing if you have, either have diabetes or maybe are pre-diabetic, right?

51:53 - Yep, exactly.

51:54 And there are two things.

51:55 You might not be producing enough insulin or your cells might be insensitive to it.

52:01 So there are kind of two ways that that can go wrong.

52:03 - Okay, so I think, I don't remember which one that you did this on here.

52:08 There's all, all these come with great plots over time and you're running the simulation and seeing how it evolves over time and those kinds of things.

52:16 But at least one of these you used SymPy, right?

52:21 - Yes.

52:21 - SymPy is pretty interesting.

52:23 - Yeah, so this is symbolic computation, and it allows you to solve systems of equations, nonlinear equations, differential equations, at least for the ones that have an analytic solution, SymPy will often find it for you.

52:38 And when I say find a solution, it's not a numerical approximation, it's a symbolic representation of the function.

52:45 - Right, that's why I thought it was so interesting as a thing, 'cause so everything we've been talking about so far are like these Euler type solutions, you're like, okay, well, if we just approximate, if the gap is small enough, it's like calculus, but it's a loop, you know?

52:59 Whereas this says, actually the integral of this differential equation is this other formula, right?

53:06 Like you would learn in calculus or something.

53:09 - Yeah, and so what you get is this object that represents the function, and you can render it as LaTeX, so you can kind of see it, you know, with the nice integral symbols and infinity symbols and all that.

53:21 And you can also from that generate Python code.

53:24 So you could have, you know, solve your differential equation and then evaluate the result as a numerical computation.

53:32 - Right, the answer instead of being 3.14, whatever is just the letter pi.

53:37 - Right.

53:38 - Right, it's like, you gave me a formula, here's the actual symbolic solution to it as a number, right?

53:43 - Yep.

53:44 - Yeah, pretty cool.

53:45 So you did, I believe you used that in some of your simulations or some of your models, right?

53:49 - There were a couple of examples where we did both.

53:51 we would do the numerical solver and then the symbolic computation and then compare them.

53:58 And then a couple of places where there's an analysis that you can do, like the SIR model, where you could do the analysis by hand, and certainly in lots of science and math classes, that's what you would do.

54:10 But I really wanted to show people how to do this symbolically, because I think it separates two parts of the activity.

54:18 there's the low level activity of actually, you know, writing all the symbols on paper and being very careful not to leave off the minus signs and the two, factor two and all that. And then there's the high level, keeping track of what am I doing? What's my goal? What are the steps I'm following to get there? And I think symbolic computation has a lot of potential to separate those two, where I think people could come away with a much better understanding of that high level stuff if they are freed from so much of their attention being on the low level stuff.

54:50 Yeah. Yeah. Just the grind of factoring polynomials or taking the derivative or whatever, right?

54:57 Yep. Yep. You see, you know, mechanical systems, if you're working with physical vectors, you just, you have a page that's just full of sines and cosines. And it's, in some sense, what that is a failure of notation, because that page full of sines and cosines is really telling you that you have not represented the computation in an effective way. That the sines and cosines are a consequence of not using vector arithmetic.

55:25 Right, which you do make a big use of with pandas and things like that, right?

55:30 Right. We get to it, and this is, I think, where I started to reach the limits of the current tools, because ideally, if you have vector quantities, like position and velocity and acceleration, you should represent them as vectors.

55:46 And so when you add two vectors, you're not converting them to their coordinates, adding the coordinates, and then putting the result back into a vector.

55:55 You just think of vector addition as a fundamental operation.

56:00 But in the same way, we would like to be able to do differentiation and numerical solution to these methods, also using vectors as primitive objects.

56:12 And we're not quite there.

56:13 The SciPy libraries don't really do that.

56:16 So I end up doing a lot of packing and unpacking where I start with vectors, but then to do the computation, I have to break them down into coordinates.

56:25 And then when I get the result, I have to stuff it back into vectors.

56:29 I think the next generation of scientific computing, we need vectors as basic types, and we need computation with units.

56:37 Yeah, right, which is something that's also often missing.

56:41 To my knowledge, this does not exist in Python or any other programming language that's not, at least it is mainstream.

56:47 There's probably some engineering ones, but we have things like Pint, right?

56:50 Yeah.

56:51 Which is a pretty neat library.

56:52 It's very neat, and it's very close to what I want.

56:56 Astropy is the other library I'm aware of that does a really good job with units.

57:02 And I've played around with each of them enough to be excited for the potential, but also to have hit a wall of, "I still can't quite do what I want." Ideally, it should be end-to-end. I should be able to start with the parameters of my system and the initial state described using vectors with units, and then run the whole computation so that at the end, my result is in the form of vectors with units.

57:29 - Yeah.

57:30 (laughs)

57:31 - And that's still, that's not easy yet.

57:32 - Yeah, not yet.

57:33 Okay.

57:34 You know, the SymPy stuff is a little, a little like that in the sense that the thing you get back is like kind of a function in the mathematical sense, like f of x equals, you know, e to the x.

57:45 And you can compose them and pass them around, right?

57:48 And sort of use them a little bit more than just, well, I got an answer out of this and then I feed the answer there, right?

57:54 Numerically.

57:54 - Yes.

57:55 So, SymPy is end to end in this way, that you start with symbols and you end with symbols.

58:01 There's no like in between where you have to break it down into numerical methods and then build it up again.

58:07 So yeah, I think SymPy has achieved this goal and we're still, I think, working on vectors and units and scientific computation.

58:16 - Yeah, absolutely.

58:17 All right, well, we're pretty close to out of time, but I think maybe let's just touch on one more kind of fun.

58:23 And that is the, people can go and play with this.

58:27 That's the bungee dunk.

58:29 Do you want to tell people what kind of insanity a bungee dunk is?

58:32 And then you can just talk about this modeling problem as a way to say some human being put their body in jeopardy to test this theory, right?

58:41 Tell us about this.

58:41 - Yes, okay.

58:42 So this was partly prompted by this video, which shows a person doing a bungee jump.

58:48 So they're jumping off a crane.

58:49 They have a bungee cord attached to their ankles.

58:52 And when the cord is stretched out and fully extended, They are about four feet from the ground and they can reach out and the person jumping has a cookie or a biscuit because they're in the UK.

59:06 And they dunk the cookie into a cup of tea that is on a dock at the bottom of this.

59:11 And I forget the number, was it 30 meters?

59:15 But it was a world record.

59:15 - Yeah, something like that, yeah.

59:17 It was way too far for a person to do that with their body.

59:20 - Now, and part of this is that the object--

59:21 - No, the video shows them, literally they come to a complete stop because the way the springs work and they are right at where they literally do dunk this biscuit into the tee and then shoot back up for the back and forth oscillation by Hooke's law of the bungee, right?

59:37 - You got it, right.

59:39 Now the margin of error here is low.

59:42 Most people want to do a bungee jump to not do this because if it just turns out that he had extended about two meters more than he expected to, he would have at least hit the plank And he was over a body of water, so maybe it's not a catastrophe, but it was a risky thing to do.

01:00:00 - Yeah, at a minimum, it's gonna hurt.

01:00:02 - Yes.

01:00:03 (laughing)

01:00:03 - Yeah, so you basically, you go through the idea of like, well, if you wanted to try, the record was 70 meters.

01:00:11 - Oh, okay.

01:00:11 - So that is way up there.

01:00:13 So, (laughing)

01:00:15 that's way up there.

01:00:16 So if you wanted to say, you know what, guy with a cool beard that currently holds the Guinness Book of World Records, we have pandas, we have notebooks, we're going to beat this, right?

01:00:26 You could go through and model all the details out.

01:00:29 And you talked through some of the assumptions, like the bungee will not apply a slowing force to you until it fully extends the slack length of the bungee.

01:00:38 And then it is a perfect spring and on and on, right?

01:00:41 - Right.

01:00:42 But here's where there is some interesting physics.

01:00:44 So here was the other motivation for this problem.

01:00:47 Remember, thinking about modeling decisions, what can you leave out and what do you have to include in order to get a precise answer.

01:00:55 So in this scenario, air resistance is definitely a factor.

01:00:59 And the stretch of the spring and its restoring force is definitely a factor.

01:01:04 But here's the interesting part.

01:01:06 During the first phase of the jump, while the bungee cord is not yet stretching, but it is uncoiling, because it starts out in a great big U shape, it's dangling down, and it has to unravel or unwrap itself as you are falling.

01:01:22 The first model that you probably think of is that it's just free fall, that you are being pulled by the force of gravity and the bungee cord is being pulled by the force of gravity.

01:01:33 And so there's no net interaction between the two.

01:01:36 And so you would think it would be just, your acceleration would be equal to G, 9.8 meters per second squared.

01:01:42 And that's what turns out not to be true.

01:01:44 And it's not at all obvious, but there is an interaction with the cord.

01:01:47 It is transferring momentum from the cord to you.

01:01:50 It's an alias to a whip action.

01:01:53 And the net effect is that you accelerate faster than gravity because the cord is actually pulling you down.

01:01:59 It turns out that the difference you would be off by if you ignore this effect, you would be off by about two meters, which is.

01:02:07 - And that's gonna be a problem.

01:02:09 - That was your margin of error.

01:02:12 Right.

01:02:13 So there's a nice physics paper that explains this.

01:02:15 And there's a really nice Veritasium video that explains the phenomenon and does a really compelling demonstration that shows that it's true.

01:02:25 Veritasium is fantastic for this kind of stuff.

01:02:28 It's a YouTube channel that does these kinds of experiments.

01:02:31 A little bit like the Discovery Show, but way better.

01:02:34 They're really good.

01:02:35 Did you see the downwind sort of sailboat, but a sail car?

01:02:39 No, I don't.

01:02:40 Oh, maybe I did actually see that.

01:02:42 Yeah, I think I did.

01:02:43 That one's very good.

01:02:44 It's been a while.

01:02:45 Yeah.

01:02:46 I'll see if I can find the channel and put it up in the show notes for people.

01:02:49 All right, I think with that, we probably should call it for time, but very interesting.

01:02:54 People don't actually recommend you go try to break the bungee dunk record.

01:02:59 Yeah, no.

01:03:00 Do something else.

01:03:00 But if you do it...

01:03:02 It is fun to watch.

01:03:03 Yeah, make a good model.

01:03:04 Exactly. Make a good model and let us know. We'll share it, but we don't recommend it.

01:03:10 All right, final two questions before we get out of here, Alan.

01:03:14 You're going to write some Python code. What editor do you use?

01:03:16 I'm using mostly Jupyter notebooks, but also a little bit of VS Code.

01:03:22 Okay. For notebooks, is it straight Jupyter? Is it JupyterLab?

01:03:25 Well, I just had JupyterLab 4.0 come out, which is a big update.

01:03:28 I know. And every time JupyterLab gets better, I think, now's the time for me to make the switch. But I have not.

01:03:34 And partly it's familiarity, and partly as a teaching environment, I prefer a simple environment with the minimum distraction.

01:03:41 Sure. That's certainly valid.

01:03:43 All right, and notable PyPI package, conda package if you prefer.

01:03:48 Right. Well, I have to say the SciPy, NumPy, Pandas, those were the primary things I was using for this book.

01:03:57 And then maybe a shout out to Pynt, which as I said, I think it's not quite doing everything I want, but I think it's a really great tool.

01:04:04 Excellent. Yeah, I agree.

01:04:06 All right. People are interested.

01:04:07 They want to check out your book.

01:04:08 I'll put the link in the show notes.

01:04:11 There's Jupyter Notebooks up on Google Colab.

01:04:14 If you're feeling brave, you can create your own virtual environment, install them locally and run them there as well.

01:04:19 That works too.

01:04:20 Anything else you want to say?

01:04:21 How people get started with this book?

01:04:22 - I, you know, the idea is to just get you into it.

01:04:25 There are some case studies and a lot of chances to take whatever system you're interested in and apply these tools.

01:04:32 So I hope people enjoy it.

01:04:34 And if you do anything interesting with it, let me know.

01:04:37 - Sounds good.

01:04:37 Well, thank you for being here and thanks to everyone for listening.

01:04:40 Thank you.

01:04:41 This has been another episode of Talk Python to Me.

01:04:45 Thank you to our sponsors.

01:04:47 Be sure to check out what they're offering.

01:04:48 It really helps support the show.

01:04:50 Influx Data encourages you to try InfluxDB.

01:04:54 InfluxDB is a database purpose-built for handling time series data at a massive scale for real-time analytics.

01:05:01 Try it for free at talkpython.fm/influxdb.

01:05:05 Are you ready to level up your Python career?

01:05:08 And could you use a little bit of personal and individualized guidance to do so, check out the PyBytes Python Developer Mindset Program at talkpython.fm/pdm.

01:05:20 Want to level up your Python?

01:05:22 We have one of the largest catalogs of Python video courses over at Talk Python.

01:05:26 Our content ranges from true beginners to deeply advanced topics like memory and async.

01:05:31 And best of all, there's not a subscription in sight.

01:05:34 Check it out for yourself at training.talkpython.fm.

01:05:37 Be sure to subscribe to the show, open your favorite podcast app, and search for Python.

01:05:42 We should be right at the top.

01:05:43 You can also find the iTunes feed at /itunes, the Google Play feed at /play, and the Direct RSS feed at /rss on talkpython.fm.

01:05:52 We're live streaming most of our recordings these days.

01:05:56 If you want to be part of the show and have your comments featured on the air, be sure to subscribe to our YouTube channel at talkpython.fm/youtube.

01:06:04 This is your host, Michael Kennedy.

01:06:05 Thanks so much for listening.

01:06:07 I really appreciate it.

01:06:08 Now get out there and write some Python code.

01:06:10 [Music]

01:06:28 [BLANK_AUDIO]

Back to show page
Talk Python's Mastodon Michael Kennedy's Mastodon