Difference between revisions of "Physics 212, 2019: Lecture 7"
(Created page with "{{PHYS212-2019}} In this module, we further expand on our toolkit of computational methods and Python coding and learn how to solve dynamical (ordinary differential equations...") |
(→The simplest dynamical model: Malthusian growth) |
||
Line 8: | Line 8: | ||
;Analysis: How should we model this? The model we need is definitely dynamic. However, almost all other aspects of the model are for you to decide. I encourage you to consider different modeling assumptions. We can have discrete number of bacteria vs. continuous (explain what it means to have a continuous number here -- after all, the number of bacteria is natural). We can have discrete time (bacteria divide synchronously) or continuous time -- bacteria divide at different times. Which (if any) is a better choice and why? We can have a stochastic model (the number of dividing bacteria is random) or a deterministic model, where in a certain interval <math>dt</math> a certain fraction of bacteria divides that is proportional to the duration of the interval. In what follows, I choose to assume that each bacterium divides at the same rate. But they divide in different time, so that the time is continuous. We will assume that the number of bacteria is large, so that a large number of new bacteria gets created during any, even very short, period of time. Thus the total number of bacteria dividing per unit time is proportional to the current population, with a certain coefficient of proportionality, which we call the rate. | ;Analysis: How should we model this? The model we need is definitely dynamic. However, almost all other aspects of the model are for you to decide. I encourage you to consider different modeling assumptions. We can have discrete number of bacteria vs. continuous (explain what it means to have a continuous number here -- after all, the number of bacteria is natural). We can have discrete time (bacteria divide synchronously) or continuous time -- bacteria divide at different times. Which (if any) is a better choice and why? We can have a stochastic model (the number of dividing bacteria is random) or a deterministic model, where in a certain interval <math>dt</math> a certain fraction of bacteria divides that is proportional to the duration of the interval. In what follows, I choose to assume that each bacterium divides at the same rate. But they divide in different time, so that the time is continuous. We will assume that the number of bacteria is large, so that a large number of new bacteria gets created during any, even very short, period of time. Thus the total number of bacteria dividing per unit time is proportional to the current population, with a certain coefficient of proportionality, which we call the rate. | ||
;Model building: The following variables are needed to initialize the model: initial number of bacteria (constant), growth rate (constant), total duration of the experiment (constant), the number of bacteria -- a variable that will be updated with time. The considerations above suggest that we can write the model using a differential equation for the population size <math>dn/dt=rn</math>, with the initial condition <math>n(0)=n_0</math>. This assumes that the number of bacteria is always much larger than 1. This model is easy to solve analytically. We rewrite the equation as <math>dn=rn\, dt</math>, and integrate both sides. This gives <math>n(t)=n_0 e^{rt}</math>. However, our focus here is on solving similar equations numerically, and so we will implement numerical solution of the equation, and the analytical solution will be a useful check of accuracy of our numerical code. | ;Model building: The following variables are needed to initialize the model: initial number of bacteria (constant), growth rate (constant), total duration of the experiment (constant), the number of bacteria -- a variable that will be updated with time. The considerations above suggest that we can write the model using a differential equation for the population size <math>dn/dt=rn</math>, with the initial condition <math>n(0)=n_0</math>. This assumes that the number of bacteria is always much larger than 1. This model is easy to solve analytically. We rewrite the equation as <math>dn=rn\, dt</math>, and integrate both sides. This gives <math>n(t)=n_0 e^{rt}</math>. However, our focus here is on solving similar equations numerically, and so we will implement numerical solution of the equation, and the analytical solution will be a useful check of accuracy of our numerical code. | ||
− | ;Model implementation: We rewrite the differential equation as a finite difference equation <math>n(t+\Delta t)=n(t)+\Delta n(t)=n(t)+rn(t) | + | ;Model implementation: We need to solve the differential equation that we write down, but we do not know how to do this yet. |
+ | |||
+ | ==Euler method for solving differential equations== | ||
+ | The problem above is the first problem of a different kind: dynamic, continuous time model, where temporal evolution of variables is governed by ordinary differential equations (in this case, there's just one variable and one equation describing it). Intuitively, solving such models should be easy -- instead of a continuous change of the variable, we can increment the changing variable in steps corresponding to quantum units of time. To understand what the increment should be, one recalls that the derivative is the tangent to a function, and so one can approximate the change of the function <math>n(t)</math> over the time <math>\Delta t</math> by <math>\frac{dn}{dt}\Delta t</math>. In other words, we can rewrite the differential equation as a ''finite difference'' equation <math>n(t+\Delta t)=n(t)+\Delta n(t)=n(t)+rn(t)\Delta t</math>. One then can solve for <math>n</math> over the entire range of time that we are interested in by looping over a few simple commands: evaluating the derivative, calculating the function increment as the derivative times the time step, incrementing the function, and advancing the time. This is known as the Euler algorithm for solving differential equations. | ||
+ | |||
+ | This tells us that two more variables will be needed for implementation of the problem: <math>dn</math> -- a dependent variable that will store the change of the number of bacteria on a dish at a given time, and <math>dt</math>, which is a constant determining the step size for the solution of the differential equation. One can then The following script implements the solution: [[media:malthus.txt | Simple Malthusian growth]]. | ||
;Model verification: We can solve this whole problem analytically and compare to the output of the experiment. Or we can verify the code by comparing to special cases, such as growth rate or initial bacterial number being zeros (what are the solutions for both of these cases?). | ;Model verification: We can solve this whole problem analytically and compare to the output of the experiment. Or we can verify the code by comparing to special cases, such as growth rate or initial bacterial number being zeros (what are the solutions for both of these cases?). | ||
;Discussion: We have modeled the exponential bacterial growth, and the findings agree with the analytics. While it seems that there is not much to discuss here, one could still describe what will happen to the solution if any of our assumptions (large number of bacteria, deterministic growth, continuous time, etc.) are violated. Please think of answers to these questions. For example, how would this code change if we assumed the model to be a discrete time model? In fact, only the steps would probably get larger -- but the structure of the code would remain the same, with the same stepping through time intervals of <math>dt</math>. There are no, in fact, continuous models on digital computers! | ;Discussion: We have modeled the exponential bacterial growth, and the findings agree with the analytics. While it seems that there is not much to discuss here, one could still describe what will happen to the solution if any of our assumptions (large number of bacteria, deterministic growth, continuous time, etc.) are violated. Please think of answers to these questions. For example, how would this code change if we assumed the model to be a discrete time model? In fact, only the steps would probably get larger -- but the structure of the code would remain the same, with the same stepping through time intervals of <math>dt</math>. There are no, in fact, continuous models on digital computers! | ||
;Your work: explore how the solution depends on <math>dt</math>. Change the code to output the results only when <math>t</math> is an integer. | ;Your work: explore how the solution depends on <math>dt</math>. Change the code to output the results only when <math>t</math> is an integer. |
Revision as of 08:04, 6 February 2019
Back to the main Teaching page.
Back to Physics 212, 2019: Computational Modeling.
In this module, we further expand on our toolkit of computational methods and Python coding and learn how to solve dynamical (ordinary differential equations) models.
The simplest dynamical model: Malthusian growth
A few bacteria are placed in a Petri dish. With time, each bacterium grows with a certain rate and then divides into two daughter cells. How many bacteria are there in the dish a certain while later?
- Analysis
- How should we model this? The model we need is definitely dynamic. However, almost all other aspects of the model are for you to decide. I encourage you to consider different modeling assumptions. We can have discrete number of bacteria vs. continuous (explain what it means to have a continuous number here -- after all, the number of bacteria is natural). We can have discrete time (bacteria divide synchronously) or continuous time -- bacteria divide at different times. Which (if any) is a better choice and why? We can have a stochastic model (the number of dividing bacteria is random) or a deterministic model, where in a certain interval a certain fraction of bacteria divides that is proportional to the duration of the interval. In what follows, I choose to assume that each bacterium divides at the same rate. But they divide in different time, so that the time is continuous. We will assume that the number of bacteria is large, so that a large number of new bacteria gets created during any, even very short, period of time. Thus the total number of bacteria dividing per unit time is proportional to the current population, with a certain coefficient of proportionality, which we call the rate.
- Model building
- The following variables are needed to initialize the model: initial number of bacteria (constant), growth rate (constant), total duration of the experiment (constant), the number of bacteria -- a variable that will be updated with time. The considerations above suggest that we can write the model using a differential equation for the population size , with the initial condition . This assumes that the number of bacteria is always much larger than 1. This model is easy to solve analytically. We rewrite the equation as , and integrate both sides. This gives . However, our focus here is on solving similar equations numerically, and so we will implement numerical solution of the equation, and the analytical solution will be a useful check of accuracy of our numerical code.
- Model implementation
- We need to solve the differential equation that we write down, but we do not know how to do this yet.
Euler method for solving differential equations
The problem above is the first problem of a different kind: dynamic, continuous time model, where temporal evolution of variables is governed by ordinary differential equations (in this case, there's just one variable and one equation describing it). Intuitively, solving such models should be easy -- instead of a continuous change of the variable, we can increment the changing variable in steps corresponding to quantum units of time. To understand what the increment should be, one recalls that the derivative is the tangent to a function, and so one can approximate the change of the function over the time by . In other words, we can rewrite the differential equation as a finite difference equation . One then can solve for over the entire range of time that we are interested in by looping over a few simple commands: evaluating the derivative, calculating the function increment as the derivative times the time step, incrementing the function, and advancing the time. This is known as the Euler algorithm for solving differential equations.
This tells us that two more variables will be needed for implementation of the problem: -- a dependent variable that will store the change of the number of bacteria on a dish at a given time, and , which is a constant determining the step size for the solution of the differential equation. One can then The following script implements the solution: Simple Malthusian growth.
- Model verification
- We can solve this whole problem analytically and compare to the output of the experiment. Or we can verify the code by comparing to special cases, such as growth rate or initial bacterial number being zeros (what are the solutions for both of these cases?).
- Discussion
- We have modeled the exponential bacterial growth, and the findings agree with the analytics. While it seems that there is not much to discuss here, one could still describe what will happen to the solution if any of our assumptions (large number of bacteria, deterministic growth, continuous time, etc.) are violated. Please think of answers to these questions. For example, how would this code change if we assumed the model to be a discrete time model? In fact, only the steps would probably get larger -- but the structure of the code would remain the same, with the same stepping through time intervals of . There are no, in fact, continuous models on digital computers!
- Your work
- explore how the solution depends on . Change the code to output the results only when is an integer.