{ "cells": [ { "cell_type": "markdown", "metadata": { "cell_style": "center", "slideshow": { "slide_type": "slide" } }, "source": [ "\n", "\n", "# Class 7: System dynamics models: Growth and decay\n", "\n", "---\n", "\n", "\n", "\n", "This notebook is licensed under a [Creative Commons Attribution-ShareAlike 4.0 International License](http://creativecommons.org/licenses/by-sa/4.0/)." ] }, { "cell_type": "markdown", "metadata": { "cell_style": "center", "slideshow": { "slide_type": "slide" } }, "source": [ "## Load packages" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "cell_style": "center", "slideshow": { "cell_style": "split", "slide_type": "-" } }, "outputs": [], "source": [ "%matplotlib inline\n", "\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import pandas as pd" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Growth model for bacterial colony\n", "\n", "Iteratively solve the difference equation using numerical methods\n", "\n", "\\begin{equation}\n", "P(t)=P(t-\\Delta{}t)-rP(t-\\Delta{}t)\\Delta{}t\n", "\\end{equation}\n", "\n", "Assume the following constants:\n", "\n", "```python\n", "population = 100\n", "r = 0.1\n", "delta_t = 0.001\n", "time_of_simulation = 50.5\n", "```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Class-led solution\n", "\n", "How do I handle this in Python?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Define our constants:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "population = 100\n", "r = 0.1\n", "delta_t = 0.001\n", "time_of_simulation = 50.5" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Define a list to store our simulation history." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "trace = [[0, population]]" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Iteratively solve the difference equation using a `for` loop and store the `[time_step, population]` pair in `trace`:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "slideshow": { "slide_type": "fragment" } }, "outputs": [], "source": [ "for time_step in range(1, int(time_of_simulation / delta_t) + 1):\n", " population = population + r * population * delta_t\n", " trace.append([time_step, population])" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Convert our results into a DataFrame.\n", "\n", "**How can we extract out the first column and the second column of `trace`?**\n", "\n", "Remember, these are not `numpy` arrays!" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "We use list comprehensions, for example:\n", "\n", "```\n", "[time_step[0] for time_step in trace]\n", "```\n", "\n", "This is equivalent to the following:\n", "\n", "```python\n", "column_one = []\n", "for time_step in trace:\n", " column_one.append(time_step[0])\n", "```" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "With list comprehensions, we can feed each column into `pd.DataFrame` as follows:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "bacteria_df = pd.DataFrame({\n", " \"time_step\": [time_step[0] for time_step in trace],\n", " \"population\": [time_step[1] for time_step in trace]\n", "})" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "slideshow": { "slide_type": "slide" } }, "outputs": [ { "data": { "text/html": [ "
| \n", " | time_step | \n", "population | \n", "
|---|---|---|
| 0 | \n", "0 | \n", "100.000000 | \n", "
| 1 | \n", "1 | \n", "100.010000 | \n", "
| 2 | \n", "2 | \n", "100.020001 | \n", "
| 3 | \n", "3 | \n", "100.030003 | \n", "
| 4 | \n", "4 | \n", "100.040006 | \n", "