Skip to content

Instantly share code, notes, and snippets.

@LinusLach
Created January 28, 2023 13:32
Show Gist options
  • Save LinusLach/5fb89ebc2854655349e6798603875b45 to your computer and use it in GitHub Desktop.
Save LinusLach/5fb89ebc2854655349e6798603875b45 to your computer and use it in GitHub Desktop.
Predator_Prey_Control_Ex.ipynb
channels:
- defaults
dependencies:
- ipython
- numpy
- scipy
- matplotlib
- notebook
- console_shortcut
- ipywidgets
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "01d7bb8c",
"metadata": {
"pycharm": {
"is_executing": true
}
},
"outputs": [],
"source": [
"import numpy as np\n",
"from scipy.integrate import solve_ivp\n",
"import matplotlib.pyplot as plt\n",
"from ipywidgets import interact, widgets, Layout\n",
"%matplotlib inline"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "7c4824de",
"metadata": {},
"outputs": [],
"source": [
"def u(t):\n",
" if 0<= t <= 2:\n",
" return 0\n",
" elif 2< t <= 4.5:\n",
" return 1\n",
" elif 4.5< t <=7.5:\n",
" return 0.5\n",
" else:\n",
" return 0\n",
"vu = np.vectorize(u,otypes=[np.float64])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "9fe85e13",
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "7390ef4846d542458bfacfaad4225c01",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(FloatSlider(value=0.5, description='Initial Prey Population $\\\\quad$', layout=Layout(wid…"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"def model_control_ex(t,x):\n",
" return np.array([x[0]-x[0]*x[1]-0.4*x[0]*u(t), -x[1]+x[0]*x[1]-0.2*x[1]*u(t)])\n",
"def plot_pp(a,b):\n",
" t0 = 0\n",
" tmax = 12\n",
" sol = solve_ivp(lambda t, x:model_control_ex(t,x), [t0, tmax], np.array([a,b]), t_eval=np.linspace(t0, tmax, 100))\n",
" plt.subplot(1,2,1)\n",
" plt.plot(sol.t,sol.y[0], label = 'Prey')\n",
" plt.plot(sol.t,sol.y[1], label = 'Predator')\n",
" plt.xlabel(\"time\")\n",
" plt.ylabel(\"population\")\n",
" plt.title(\"Solution of the ODE system\")\n",
" plt.legend()\n",
" plt.subplot(1,2,2)\n",
" plt.plot(sol.y[1],sol.y[0], label = 'Prey', color = \"r\")\n",
" plt.rcParams[\"figure.figsize\"]=10,5\n",
" plt.title(\"Phase portrait of the solution\")\n",
" plt.xlabel(\"predator population\")\n",
" plt.ylabel(\"prey population\")\n",
"\n",
"style = {'description_width': 'initial'}\n",
"layout = Layout(width = \"400px\")\n",
"slid1 = widgets.FloatSlider(description = 'Initial Prey Population $\\quad$',min=0, max=1, step=0.1, value=0.5,style = style, layout = layout)\n",
"slid2 = widgets.FloatSlider(description = 'Initial Predator Population',min=0, max=1, step=0.1, value=0.7, style = style, layout = layout)\n",
"interact(plot_pp, a= slid1 ,b=slid2);"
]
}
],
"metadata": {
"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.8.16"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment