Skip to content

Commit

Permalink
visualization
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexEMG committed Nov 7, 2021
1 parent 1c03007 commit 953718b
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 10 deletions.
7 changes: 7 additions & 0 deletions LotkaVolterraModel.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
"""
Module containing simulation code of the Lotka Volterra model.
Adapted from:
https://github.com/scipy/scipy-cookbook/blob/master/ipython/LotkaVolterraTutorial.ipynb
"""

import numpy as np


Expand Down
41 changes: 41 additions & 0 deletions Visualization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""
Module containing matlab code for visualization of the Lotka Volterra model.
Adapted from:
https://github.com/scipy/scipy-cookbook/blob/master/ipython/LotkaVolterraTutorial.ipynb
"""

import matplotlib.pyplot as plt
import os


def _createfolder(folder="graphs"):
""" Make directory named folder in current working directory,
if it doesn't exist. Returns name of folder.
"""
if not os.path.isdir(folder):
os.mkdir(folder)

return folder


def evolution(t, X, savefig=True, showfig=True):
""" Simple function to plot temporal evolution of X. """
rabbits, foxes = X.T

fig1 = plt.figure()
plt.plot(t, rabbits, "r-", label="Rabbits")
plt.plot(t, foxes, "b-", label="Foxes")

plt.grid()
plt.legend(loc="best")
plt.xlabel("time")
plt.ylabel("population")
plt.title("Evolution of fox and rabbit populations")

if savefig:
foldername = _createfolder()
fig1.savefig(os.path.join(foldername, "rabbits_and_foxes_1.png"))

if showfig:
fig1.show()
Binary file added graphs/rabbits_and_foxes_1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 13 additions & 10 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
# Code adapted from https://scipy-cookbook.readthedocs.io/items/LoktaVolterraTutorial.html
# Code adapted from https://github.com/scipy/scipy-cookbook/blob/master/ipython/LotkaVolterraTutorial.ipynb

import numpy as np
from scipy import integrate
from LotkaVolterraModel import dX_dt, check_equilibrium

from Visualization import evolution

# Definition of parameters
a = 1.0 # natural growth rate of rabbits (prey)
b = 0.1 # natural dying rate of rabbits
c = 1.5 # natural dying rate of foxes
a = 1.0 # natural growth rate of rabbits (prey)
b = 0.1 # natural dying rate of rabbits
c = 1.5 # natural dying rate of foxes
d = 0.75 # factor describing growth of foxes based on caught rabbits

"""
# Running dynamical system:
t = np.linspace(0, 15, 1000) # time
X0 = np.array([10, 5]) # initial conditions: 10 rabbits and 5 foxes
X, infodict = integrate.odeint(lambda x, _: dX_dt(x, a, b, c, d), X0, t, full_output=True)
"""
X, infodict = integrate.odeint(
lambda x, _: dX_dt(x, a, b, c, d), X0, t, full_output=True
)

evolution(t, X)


print("Checking fix points of ODE.")
check_equilibrium(a, b, c, d)
# print("Checking fix points of ODE.")
# check_equilibrium(a, b, c, d)

0 comments on commit 953718b

Please sign in to comment.