Python implementation of Lotka–Volterra Equations.


# © 2019 TheFlyingKeyboard and released under MIT License # theflyingkeyboard.net from matplotlib import pyplot as plt from matplotlib.ticker import FuncFormatter from pylab import rcParams x = 10 y = 5 a = 0.9 b = 0.1 c = 0.1 d = 0.4 step = 0.00001 # the smaller the more precise plots are epochs = 100 xOverTime = [] yOverTime = [] for i in range(int(1 / step) * epochs): xOverTime.append(x) yOverTime.append(y) deltaX = a * x - b * x * y deltaY = c * x * y - d * y x += step * deltaX y += step * deltaY rcParams['figure.figsize'] = 16, 9 plt.gca().xaxis.grid(True) plt.gca().yaxis.grid(True) plt.gca().get_xaxis().set_major_formatter(FuncFormatter(lambda x, p: format(int(x * step), ','))) plt.xlabel("Epoch") plt.ylabel("Population") plt.plot(xOverTime, label="Prey") plt.plot(yOverTime, label="Predator") plt.legend(loc='upper right') plt.show() plt.gca().xaxis.grid(True) plt.gca().yaxis.grid(True) plt.plot(xOverTime, yOverTime) plt.xlabel("Prey") plt.ylabel("Predator") plt.show()
Python Lotka–Volterra Equations