[Tutor] Lotka-Volterra Model Simulation Questions

Jim Apto jimmyapt0 at gmail.com
Fri Sep 28 22:32:23 CEST 2012

Hello folks,

I'm relatively new to python, and was asked to program a lotka-volterra
model (predator and prey relation) simulator.  The program basically will
basically have a menu that takes user input, collect data, and then create
a graph.  Currently i've been working on the simulator section; I can't
seem to get the lists right.  I've assigned the following variables and
parameters to the model for the program:

x represents prey population
y represents predator population
dy/dt and dx/dt represents growth rate of the two populations over time
t represents time

a is the growth rate of prey
b is the rate at which predators kill prey
g is the death rate of predators
d is the rate at which the predators population increases by consuming prey

The equation:
dx/dt = x(a-by)
dy/dt = -y(g-dx)

The code I have for this section is:
def deltaX(a,b,x,y):
    dx = x*(a-b*y)

def deltaY(g,d,x,y):
    dy = -y*(g-d*x)

The simulation function is where I am having trouble.

For the simulation function, I need to ask the user for the number of runs
and then save it in a variable, create a list for prey and predator.  For
each run, i need to calculate the increment of change in prey and predator
populations by calling the deltaX and deltaY functions, then save these in
a variable, and then update the population information.  The newly
calculated populations then need to be added to the existing lists.  After
this is completed, a function for the graph is called.

The following is my current simulation function:

def simulation():
    a=eval(input("Growth rate of prey:"))
    b=eval(input("Rate at which predators eat prey:"))
    g=eval(input("Death rate of predators:"))
    d=eval(input("Rate at which predators increase by consuming prey:"))
    x=eval(input("Current prey population:"))
    y=eval(input("Current predator population:"))


n=eval(input("Number of runs:")
    r = 0
    yList = [0]
    while r <= n:
        r = r + 1
        count = count + 1
        yList.append(dx + dx)

    zList= [0]
       while r <= n:
       r = r + 1
       count = count +1
       zList.append(dy + dy)

It seems terribly wrong.  The following is my graph function:

def drawCurve(yList,zList,n):
    x = pylab.arange(n)
    pylab.title("Foxes and Rabbits")
    pylab.ylabel("Number of predator (Foxes)")
    pylab.xlabel("\nNumber of prey  (Rabbits)")
    pylab.plot(x, yList, 'b')
    pylab.plot(x, zList, 'r')
    pylab.legend(('Rabbits','Foxes'),loc='upper left')

The issue i'm having is the logic in the lists.  How can I create the
simulation function using lists and make it perform the expected task of
creating a graph?  I can't seem to get the logic right.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120928/7b72c30c/attachment-0001.html>

More information about the Tutor mailing list