Physics 212, 2017: Lecture 7
Back to the main Teaching page.
Back to Physics 212, 2017: Computational Modeling.
Here we start a new module, where we will explore how to model and solve dynamical systems in more detail. In this lecture we discussed a few important concepts in programming, some specific to Python, and some not.
- We discussed the importance of encapsulating the code that you use repeatedly into either a script or a function.
- We discussed the difference between scripts (no inputs and no outputs) and functions (have inputs and outputs).
- We did the book section 5.1 -- Writing your own functions, and we wrote our first functions -- various growth rates for bacterial growth and solvers of the corresponding differential equations.
- We started talking about variable types, and importance of type checking, and how Python figures out which type is a variable that has been passed to a function; we will return to this later.
- We wrote our own loadable modules, and discussed the important of using the reload function.
- Finally, we discussed some common coding mistakes that we all do when working with python, and specifically:
- Appending list or arrays instead of pre-allocating arrays -- appending arrays causes creation of new arrays, and then copying numbers, which takes time.
- Doing loops instead of vectorizing your math and operating on arrays -- doing loops causes Python to do type checking of variables at every step of the way, which is costly.
- Finally, we talked about notation, and we saw, by writing the scripts that I have uploaded below that the simple Euler method of solving differential equations has an error of
- Scripts for this Module:
- GrowthFunctions -- module defining different growth functions.
- Integrators -- module defining different integrators (Euler and Runge-Kutta second order).
- Module 2 -- catch-all scripts I used during the class.
I also asked you to show that the numerical error is dependent largely (but not fully) on the solving method, not the equation being solved. For this you needed to define a different growth function, solvee the growth equation, and submit your work.