3
$\begingroup$

I'm trying to plot Lotka-Volterra Equations using Python. I am a real beginner when it comes to Python. I have these two equations: $$\frac{dR}{dt}=\alpha R-\gamma RF$$ and $$\frac{dF}{dt}=-\beta F+\delta RF$$ I have also rewritten these equations into an iterative formula: $$R(t+\Delta t)=\alpha R(t)\Delta t - \gamma R(t)F(t)\Delta t + R(t)$$ and $$F(t+\Delta t)=-\beta F(t)\Delta t + \delta R(t)F(t)\Delta t + F(t)$$ Am I right to assume that I can use this approach to solve this problem? How do I implement this in python and graph it using matplotlib?

I would like to use Euler method but I am struggling to code the iterative equations into Python. Thank you in advance and please try to explain any Python stuff on a rookie level.

$\endgroup$
4
  • 2
    $\begingroup$ Welcome to Computational Science SE. As the question stands, there is little more than give you some code with explanations (here is mine), but that’s unlikely to help you because we don’t know where we need to go into deep detail in our explanations (and doing this everywhere would be too much). Please edit your question to tell us what you have tried and where you are stuck. Also, do you insist on implementing the Euler method yourself or are you open to using a better, pre-implemented method? $\endgroup$
    – Wrzlprmft
    Aug 3, 2020 at 13:45
  • $\begingroup$ @Wrzlprmft I would like to use Euler method as I have looked into this before. How can I input the iterative formulas into Python? $\endgroup$
    – Tom Murphy
    Aug 3, 2020 at 16:24
  • $\begingroup$ Your question is a question for a TA. Fortunately for you, I am grad student, I haven't slept and I am bored. The easiest way to implement this in python is to use a for loop and four arrays $R_{new}, F_{new}, R_{old}, F_{old}$. Each iteration, you compute $R_{new} = \alpha R_{old}\Delta t - \gamma R_{old}F_{old}\Delta t + R_{old}$ and similarly $F_{new}$. Here $R_{old}F_{old}$ is element-wise multiplication of the entries of the arrays $R_{old}$ and $F_{old}$. Then you update $R_{old} = R_{new}$, $F_{old}=F_{new}$ and repeat. The question is closed, so this is all I can do. Hope it helps. $\endgroup$ Aug 5, 2020 at 11:44
  • 1
    $\begingroup$ @AbdullahAliSivas feel free to add it as an answer :) $\endgroup$
    – Anton Menshov
    Aug 5, 2020 at 14:20

1 Answer 1

2
$\begingroup$

There are two main questions you are asking:

  1. Can I solve Lotka-Volterra problem using explicit Euler time stepping method? Answer: Probably, but you will need to take very small time steps. It is non-linear, it sometimes has chaotic behaviour depending on the parameters. So the choice of $\Delta t$ will be important. I would probably use other time steppers, especially implicit and adaptive ones, but I guess this is either an assignment or you are trying to teach something to yourself. So I will entertain your next question.

  2. How do I implement this in python and plot it? Answer:

I suggest you to use something like numpy to make implementation easier. Here is a python code

import numpy as np
import matplotlib.pyplot as plt

def LotkaVolterra_EEuler(R0, F0, alpha, beta, gamma, delta, t):
# Solves Lotka-Volterra equations for one prey and one predator species using
# explicit Euler method.
#
#  R0 and F0 are inputs and are the initial populations of each species
#  alpha, beta, gamma, delta are inputs and problem parameters
#  t is an input and 1D NumPy array of t values where we approximate y values. 
#    Time step at each iteration is given by t[n+1] - t[n].

 R = np.zeros(len(t)) # Pre-allocate the memory for R
 F = np.zeros(len(t)) # Pre-allocate the memory for F

 R[0] = R0
 F[0] = F0

 for n in range(0,len(t)-1):
  dt = t[n+1] - t[n]
  R[n+1] = R[n]*(1 + alpha*dt - gamma*dt*F[n])
  F[n+1] = F[n]*(1 - beta*dt + delta*dt*R[n])
 return R,F
 
def main():
 # Main driver to organize the code better
 t = np.linspace(0,40,3201) # interval [0,40] with 3201 equispaced points
                            # as you increase the number of points the
                            # solution becomes more similar to the 
                            # reference solution on wikipedia

 # You should set the parameters below as in your problem
 # I am using the Baboon-Cheetah example from wikipedia
 alpha, beta, gamma, delta = 1.1,0.4,0.4,0.1
 R0, F0 = 10, 10

 # Actually solve the problem
 R, F = LotkaVolterra_EEuler(R0, F0, alpha, beta, gamma, delta, t)

 # Plot the solution
 plt.plot(t,R,'b.-',t,F,'r-')
 plt.legend(['Baboon','Cheetah'])
 plt.grid(True)
 plt.title("Solution of Lotka-Volterra system using explicit Euler") 
 plt.show()

main() # Call the driver to get the results

This code can be improved a lot. For example, as is, it only solves Lotka-Volterra but explicit Euler solver can be generalized to solve other problems. It is assumed that there will be one pair of predator and prey, but it does not have to be. I will leave the rest to you. You can ask further questions and I will try to help but I think this should be a good start.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.