Difference between revisions of "Physics 212, 2019: Lecture 10"

From Ilya Nemenman: Theoretical Biophysics @ Emory
Jump to: navigation, search
(Created page with "{{PHYS212-2019}} In this lecture, we will introduce models that involved coupled, nonlinear ordinary differential equations, as well as methods for solving them. The first m...")
 
(Your work)
 
(7 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
{{PHYS212-2019}}
 
{{PHYS212-2019}}
  
In this lecture, we will introduce models that involved coupled, nonlinear ordinary differential equations, as well as methods for solving them.
+
To date, we only considered simple dynamics, which has a single dynamical variable, whose first temporal derivative is determined by the variable itself. In this lecture, we will introduce models that involved coupled, nonlinear ordinary differential equations, as well as methods for solving them.
  
 +
==Growth with carrying capacity==
 
The first model we will consider is the Malthusian growth with carrying capacity -- where a population grows nearly exponentially fast, before it reaches the maximum population size that can be supported by the environment, <math>dn/dt = r n(n_0-n)</math>, where <math>n_0</math> is known as the carrying capacity. During the previous lecture and the lab, you were supposed to solve the corresponding ODE numerically -- and you will have noticed how the growth starts quickly, and then saturates.  
 
The first model we will consider is the Malthusian growth with carrying capacity -- where a population grows nearly exponentially fast, before it reaches the maximum population size that can be supported by the environment, <math>dn/dt = r n(n_0-n)</math>, where <math>n_0</math> is known as the carrying capacity. During the previous lecture and the lab, you were supposed to solve the corresponding ODE numerically -- and you will have noticed how the growth starts quickly, and then saturates.  
  
Our second model that we will consider is the predator-prey model of population growth. You can read a lot about such generic systems, including their history, [https://en.wikipedia.org/wiki/Lotka–Volterra_equations here]. Briefly, we consider a prey animal species <math>x</math>, which grows exponentially, but at the same time it's eaten by the predator species, denoted by <math>y</math>. The dynamics of the prey is <math>\frac{dx}{dt}=rx - \alpha xy</math>, where the second term denotes the capture of the prey. Indeed, the more prey animals there are, the easier it is to capture them, and the more predators there are, the more prey they will capture, resulting in the bilinear form <math>\alpha x y</math>.  At the same time, when the predators eat, they have enough food to procreate -- but usually killing one prey only produces fewer than one predators. Further, predators also die a natural death. As a result, <math>\frac{dy}{dt}= \beta xy - d y</math>, where <math>\beta<\alpha</math>. This is an interesting dynamical system that exhibits oscillations -- number of preys goes up, they become abundant, this leads to the growth of the number of predators. Predators start eating prey, and collapse their number; then there's an insufficient number of prey to support the predator population, and the latter collapses as well. The whole cycle restarts with the renewed growth of prey at that point.
+
==Predator-prey Lotka-Volterra model==
+
Our second model that we will consider is the predator-prey model of population growth. You can read a lot about such generic systems, including their history, [https://en.wikipedia.org/wiki/Lotka–Volterra_equations here]. Briefly, we consider a prey animal species <math>x</math>, which grows exponentially, but at the same time it's eaten by the predator species, denoted by <math>y</math>. The dynamics of the prey is <math>\frac{dx}{dt}=rx - \alpha xy</math>, where the second term denotes the capture of the prey. Indeed, the more prey animals there are, the easier it is to capture them, and the more predators there are, the more prey they will capture, resulting in the bilinear form <math>\alpha x y</math>.  At the same time, when the predators eat, they have enough food to procreate -- but usually killing one prey only produces fewer than one predators. Further, predators also die a natural death. As a result, <math>\frac{dy}{dt}= \beta xy - d y</math>, where <math>\beta<\alpha</math>. This is an interesting dynamical system that exhibits oscillations -- number of preys goes up, they become abundant, this leads to the growth of the number of predators. Predators start eating prey, and collapse their number; then there's an insufficient number of prey to support the predator population, and the latter collapses as well. The whole cycle restarts with the renewed growth of prey at that point. One can think of more complicated similar systems of equations, but we will leave this to the homework project.
==Runge-Kutta 2 integration==
 
  
 +
Note that oscillations are always around the same point, and we can calculate it by setting the value of derivative to zero in the dynamical equations, which gives <math>x=d/\beta,\,y=r/\alpha</math>. One can thus verify our code by first setting the initial conditions exactly at this ''fixed point'', and then varying conditions and seeing that the fluctuations are always around the fixed point.
  
===Your work===
+
==Nonlinear pendulum==
;Problem 1: Use a different differential equation from those provided to show that Runge-Kutta 2 has error <math>O(dt^2)</math>, and the scaling of the error is not a scaling due to the equation being solved, but largely due to the methods used to solve it. Do this in a Jupyter notebook.
+
Consider a non-idealized pendulum, which undergoes oscillations with a non-infinitesimal amplitude. Then the restoring torque that it experiences is <math>M=mgl \sim \theta</math>, where <math>m</math> is the mass, <math>l</math> is the pendulum length, and <math>\theta</math> is the angle its axis forms with the vertical. Newton's equations of motion give for this system <math>\frac{d^2\theta}{dt^2}=gl\sin\theta</math>. This seems like a single equation, but it is second order in the temporal derivative. It is then equivalent to two coupled first order differential equations <math>\omega =\frac{d\theta}{dt}</math> and <math>\frac{d\omega}{dt^2}=gl\sin\theta</math>, where <math>\omega</math> is the angular velocity
  
===Scripts for this Lecture===
+
How would one verify the solution, once you write one? For small oscillations, <math>\sin \theta \approx \theta</math>, and the pendulum becomes harmonic. For it, the period is known -- and one can check if the value of the period is recovered your code.
::[[media:growthfunctions2019.txt | GrowthFunctions]] -- module defining different growth functions.
+
 
::[[media:Integrators2019.txt | Integrators]] -- module defining different integrators (Euler and Runge-Kutta, which we will use during the next lecture).
+
==Solving systems of ODEs==
::[[media:moduleTwo2019.txt | Module 2]] -- catch-all scripts I used during the class.
+
How do we solve such coupled, nonlinear dynamical systems numerically? In fact, an almost trivial extension of either the Euler or the RK2 method will work for this goal. In the attached Jupyter notebook, we solve the Lotka-Volterra predator-prey system by the extension of the Euler method to two variables, and we compare the solution to that obtained by the built-in ODE solver.
::[[media:IntegrateODE.txt | Module 2 as a notebook]] -- integration as a notebook.
+
 
 +
We will observe the oscillations, and how they can have different amplitudes depending on the initial condition, but always are around the same point.
 +
 
 +
==Additional Python concepts==
 +
We imported the module ''time'' and used it to time the execution of different versions of integrators.
 +
 
 +
==Your work==
 +
;Problem 1: Write an extension of the RK2 algorithm to a system of coupled ODEs of an arbitrary size and use it to solve the Lotka-Volterra model. Compare the results to the built-in ODE solver. Start with the multidimensional version of the Euler algorithm Jupyter notebook.  
 +
;Problem 2: Use the same extension of the RK2 algorithm to solve the nonlinear pendulum problem. Compare the results to the built-in ODE solver.
 +
 
 +
==Scripts for this Lecture==
 +
::[[media:SystemsOfODEs.txt|Solving Systems of ODEs]] Jupyter notebook

Latest revision as of 14:28, 17 February 2019

Emory Logo

Back to the main Teaching page.

Back to Physics 212, 2019: Computational Modeling.

To date, we only considered simple dynamics, which has a single dynamical variable, whose first temporal derivative is determined by the variable itself. In this lecture, we will introduce models that involved coupled, nonlinear ordinary differential equations, as well as methods for solving them.

Growth with carrying capacity

The first model we will consider is the Malthusian growth with carrying capacity -- where a population grows nearly exponentially fast, before it reaches the maximum population size that can be supported by the environment, , where is known as the carrying capacity. During the previous lecture and the lab, you were supposed to solve the corresponding ODE numerically -- and you will have noticed how the growth starts quickly, and then saturates.

Predator-prey Lotka-Volterra model

Our second model that we will consider is the predator-prey model of population growth. You can read a lot about such generic systems, including their history, here. Briefly, we consider a prey animal species , which grows exponentially, but at the same time it's eaten by the predator species, denoted by . The dynamics of the prey is , where the second term denotes the capture of the prey. Indeed, the more prey animals there are, the easier it is to capture them, and the more predators there are, the more prey they will capture, resulting in the bilinear form . At the same time, when the predators eat, they have enough food to procreate -- but usually killing one prey only produces fewer than one predators. Further, predators also die a natural death. As a result, , where . This is an interesting dynamical system that exhibits oscillations -- number of preys goes up, they become abundant, this leads to the growth of the number of predators. Predators start eating prey, and collapse their number; then there's an insufficient number of prey to support the predator population, and the latter collapses as well. The whole cycle restarts with the renewed growth of prey at that point. One can think of more complicated similar systems of equations, but we will leave this to the homework project.

Note that oscillations are always around the same point, and we can calculate it by setting the value of derivative to zero in the dynamical equations, which gives . One can thus verify our code by first setting the initial conditions exactly at this fixed point, and then varying conditions and seeing that the fluctuations are always around the fixed point.

Nonlinear pendulum

Consider a non-idealized pendulum, which undergoes oscillations with a non-infinitesimal amplitude. Then the restoring torque that it experiences is , where is the mass, is the pendulum length, and is the angle its axis forms with the vertical. Newton's equations of motion give for this system . This seems like a single equation, but it is second order in the temporal derivative. It is then equivalent to two coupled first order differential equations and , where is the angular velocity

How would one verify the solution, once you write one? For small oscillations, , and the pendulum becomes harmonic. For it, the period is known -- and one can check if the value of the period is recovered your code.

Solving systems of ODEs

How do we solve such coupled, nonlinear dynamical systems numerically? In fact, an almost trivial extension of either the Euler or the RK2 method will work for this goal. In the attached Jupyter notebook, we solve the Lotka-Volterra predator-prey system by the extension of the Euler method to two variables, and we compare the solution to that obtained by the built-in ODE solver.

We will observe the oscillations, and how they can have different amplitudes depending on the initial condition, but always are around the same point.

Additional Python concepts

We imported the module time and used it to time the execution of different versions of integrators.

Your work

Problem 1
Write an extension of the RK2 algorithm to a system of coupled ODEs of an arbitrary size and use it to solve the Lotka-Volterra model. Compare the results to the built-in ODE solver. Start with the multidimensional version of the Euler algorithm Jupyter notebook.
Problem 2
Use the same extension of the RK2 algorithm to solve the nonlinear pendulum problem. Compare the results to the built-in ODE solver.

Scripts for this Lecture

Solving Systems of ODEs Jupyter notebook