{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Course**: BIO-341 [_Dynamical systems in biology_](https://moodle.epfl.ch/course/info.php?id=14291)\n",
    "\n",
    "**Professor**: _Julian Shillcock_ & _Felix Naef_\n",
    "\n",
    "SSV, BA5, 2025\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from ipywidgets import interact"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Growth models in 1D"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Linear model for population growth"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Consider a population of $N$ birds with birth and death rates $n$ and $m$. Arrival of new individuals through migrations occurs at rate $a > 0$. This can be translated into the simple model:\n",
    "\n",
    "\\begin{equation}\n",
    "\\frac{dN}{dt} = F(N)=(n-m)N+a\n",
    "\\end{equation}\n",
    "\n",
    "**1) Write down what type of equation this is, e.g. first order, second order, linear, non-linear, etc.**\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**2) Solve this equation analytically using the Ansatz**: $N(t)=Ae^{\\lambda t} + B$. Express $A,B$ and $\\lambda$ in function of the rates and the population size $N_{0} =N(t=0)$; explicitly write the solution $N(t)$."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**3) Solve the equation using an alternative method, such as the separation of variables.**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**4) Qualitative analysis**\n",
    "\n",
    "1) Draw $F(N)$ in function of $N$ for the two cases (i) $n<m$ and (ii) $n>m$. *Note: you are also expected to draw and solve such simple problems by hand.*"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2) What is the main qualitative difference between the two cases and how does this affect the long time $t\\to\\infty$ behavior of the solution that you found above?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3) Here you can verify what you answered under 2) numerically. Generate some representative plots for $N(t)$ where you vary the parameters and initial conditions of the model."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**5) Discuss why this equation is good or bad at describing real populations.**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Integration by separation of variables"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**1) Solve the following differential equations to obtain $x(t)$ by the method of separation of variables. Use the initial condition $x(t)=x_{0}$ when $t=0$.**\n",
    "\n",
    "1. $\\frac{dx}{dt}= xe^{-2t}$\n",
    "\n",
    "2. $\\frac{dx}{dt}= 4x^{2}-1$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The non-autonomous Gompertz model for tumor growth"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A surprisingly accurate model for the growth of a tumor of volume $N$ is given by the following differential equation\n",
    "\\begin{equation}\n",
    "\\frac{dN}{dt}=r(t)N(t) \n",
    "\\end{equation}\n",
    "with $r(t)=r_{0}e^{-at}$ and initial size $N(0)=N_{0}$. In other words, the population grows with a time dependent rate $r(t)$, which decreases exponentially in time with a rate $a$.  \n",
    "\n",
    "**1) Give a plausible explanation for the proposed behavior of $r(t)$. Why should the growth rate decrease with time?**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**2) What is the meaning of $r_{0}$?**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**3) Show that the solution for $N(t)$ in function of the 3 parameters $N_{0}, r_{0}, a$  can be written as $N(t)=N_{0}e^{\\frac{r_{0}}{a}(1-e^{-at})}$.**  \n",
    "*Hint*: Use the method of separation of variables."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**4) Study the solution:**\n",
    "\n",
    "1. Show that for very short times the population grows linearly like $N(t)= N_0 (1 + r_0 t)$.\n",
    "2. Show that for very long times $N(t)\\cong N_{max}(1-\\frac{r_{0}}{a}e^{-at})$.\n",
    "\n",
    "\n",
    "*Hint*: Use the Taylor approximation $e^{x} \\approx 1 + x$ (valid for small x) for the inner or the outer exponential when appropriate."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**5) Sketch the solution. Indicate $N_{max}$. How does the $N$ approaches $N_{max}$?**"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  },
  "title": "Exercise 1: Growth models in 1D",
  "widgets": {
   "state": {
    "24aa0b8d62014553b6fdce05a067bb36": {
     "views": [
      {
       "cell_index": 18
      }
     ]
    }
   },
   "version": "1.2.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
