{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "E3GfWVDOYyUC"
   },
   "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",
    "\n",
    "Note that this document is primarily aimed at being consulted as a Jupyter notebook, the PDF rendering being not optimal."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "id": "ndP1q-j6YyUE"
   },
   "outputs": [],
   "source": [
    "#import important libraries\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "from ipywidgets import interact\n",
    "from scipy.integrate import odeint\n",
    "from IPython.display import set_matplotlib_formats\n",
    "from matplotlib.markers import MarkerStyle\n",
    "set_matplotlib_formats('png', 'pdf')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "FYm7Kq47YyUF"
   },
   "source": [
    "# Population dynamics model with a limit cycle"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the course, we studied predator-prey models with a stable spiral, which means that the two populations settle to coexist after the oscillatory transient decay.\n",
    "\t\n",
    "Here, we study the following model:\n",
    "\n",
    "\\begin{align}\n",
    "&\\frac{dN}{dt} = a\\,N(K-N)-c\\,P\\frac{N}{N+R} \\\\\n",
    "&\\frac{dP}{dt} = b\\,P \\left(s N - P\\right) \n",
    "\\end{align}\n",
    "\n",
    "where the predators $P$ might represent C. Elegans worms feeding on E. Coli bacteria $N$ (typically used as food in laboratory dishes). All parameters should be taken positive ($>{0}$).\n",
    "\t\n",
    "**1) Explain the different parameters in the model (a,b, c, R, K, s). What are their units?**  \n",
    "\n",
    "__Hint__: Make the connection with the logistic growth model."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**2) Calculate and plot the nullclines for the following values of the parameters:**\n",
    "\n",
    "**a = b = 0.01**\n",
    "\n",
    "**c = 1.**\n",
    "\n",
    "**K = 200**\n",
    "\n",
    "**R = 50**\n",
    "\n",
    "**s = 5**\n",
    "\n",
    "**Plot the fixed points and qualitatively describe their meanings in terms of populations of predators and preys**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**3) Plot the stability of the fixed point with both N,P $\\neq$ 0 in the R-K plane ( for K, R in [5, 500])**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**4) Fix K, and use the same values of the parameters for a,b,c and s as above. Choose 3 values of R for which you have respectively a stable f.p., a stable spiral and a limit cycle.**\n",
    "\n",
    "**Simulate the trajectories (of N and P) using a python solver (ex: odeint). Plot the trajectories in function of time and the phase portrait (P in function of N) (use subplots) for the three cases with different initial conditions.**\n",
    "\n",
    "**In the case of the limit cycle, what is the stability of the fixed point?**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [],
   "source": [
    "# paramters:\n",
    "a = 0.01\n",
    "b = 0.01\n",
    "c = 1\n",
    "K = 200\n",
    "s = 5\n",
    "tspan = np.linspace(0,100, 10000)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**5) Describe in words the behavior of the trajectories in terms of the number of predators and preys for each case.**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**6. (OPTIONAL): Check your answers using euler's method**\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Euler's method is the simplest way to solve a differential equation numerically. In order to approximate the solution of :\n",
    "\t\n",
    "$$\n",
    "\\dot{x} = F(x(t))  \\, ,  \\, x(t_0) = x_0\n",
    "$$\n",
    "\t\n",
    "We can write one step of the method as :\n",
    "\t\n",
    "$$\n",
    "x(t \\, + \\, dt) \\simeq x(t) \\, + \\, dt \\, F(x(t))\n",
    "$$\n",
    "\t\n",
    "for a specific timestep size dt."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**a) Implement your own Euler method using Python to solve numerically the following differential equation:**"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**b) Simulate the same trajectories than in 4) using the Euler's method.**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "colab": {
   "name": "Solution_6.ipynb",
   "provenance": []
  },
  "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 6: Population dynamics model with a limit cycle"
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
