UrbanPro
true

Learn Python Training from the Best Tutors

  • Affordable fees
  • 1-1 or Group class
  • Flexible Timings
  • Verified Tutors

Search in

Python - Solving Ordinary Differential Equations - Predator Prey Model aka Lotka Volterra Equations

S
Sri Vallabha Deevi
30/03/2017 0 1

This is an assignment in Python, I contributed to a numerical Python MOOC from George Washington University. The link to this assignment on github is here.

The Lotka–Volterra equations, also known as the predator–prey equations, are a pair of first-order, non-linear, differential equations. They are frequently used to describe the dynamics of biological systems in which two species interact, one as a predator and the other as prey. You can read more about this from Wikipedia http://en.wikipedia.org/wiki/Lotka-Volterra_equation.

Equations describing the system

Their populations change with time according to the following pair of equations:

$ dx/dt = x ( \alpha - \beta y ) $

$ dy/dt = -y( \gamma - \delta x ) $

Where, $x$ is the number of prey (say rabbits), $y$ is the number of predators (say foxes). $dx/dt, dy/dt$ gives the rate of change of their respective populations over time $t$. $\alpha, \beta, \gamma, \delta $ are the parameters describing the interaction between the two species. This models makes some assumptions about the predator and prey. You can read about them from the Wikipedia page mentioned above.

 

The above equations can be written in a slightly different form to interpret the physical meaning of the four parameters used.

1.Equation for prey

$dx/dt = \alpha x - \beta x y $

The prey are supposed to have unlimited supply of food and $\alpha x$ represents the rate of population growth of prey. Rate of decrease of population of prey is assumed to be proportional to the rate at which predator and prey meet and is given by $ \beta y x$

2.Equation for predator

$ dy/dt = \delta x y -\gamma y $

For the predators, $\delta x y $ gives the rate of growth of predator population. Note that this is similar to the rate of decrease of population of prey. The second term $\gamma y $ gives the rate of population decrease for predators due to natural death or emigration.

Numerical solution using Python

A simple python code for solving these equations is shown below.

 

# importrequired librariesimport numpyimport matplotlib.pyplot as plt%matplotlib inline

# set the initial parametersalpha = 1.beta = 1.2gamma = 4.delta = 1.
 
#define the time stepping scheme - euler forward, as used in earlier lessonsdef euler_step(u, f, dt):    """Returns the solution at the next time-step using Euler's method.        Parameters    ----------    u : array of float        solution at the previous time-step.    f : function        function to compute the right hand-side of the system of equation.    dt : float        time-increment.        Returns    -------    u_n_plus_1 : array of float        approximate solution at the next time step.    """        return u + dt * f(u)

# define the function that represents the Lotka-Volterra equationsdef f(u):    """Returns the rate of change of species numbers.        Parameters    ----------    u : array of float        array containing the solution at time n.            Returns    -------    dudt : array of float        array containing the RHS given u.    """    x = u[0]    y = u[1]    return numpy.array([x*(alpha - beta*y), -y*(gamma - delta*x)])

# set time-increment and discretize the timeT  = 15.0                           # final timedt = 0.01                           # set time-incrementN  = int(T/dt) + 1                  # number of time-stepsx0 = 10.y0 = 2.t0 = 0.# set initial conditionsu_euler = numpy.empty((N, 2))# initialize the array containing the solution for each time-stepu_euler[0] = numpy.array([x0, y0])# use a for loop to call the function rk2_step()for n in range(N-1):        u_euler[n+1] = euler_step(u_euler[n], f, dt)

time = numpy.linspace(0.0, T,N)x_euler = u_euler[:,0]y_euler = u_euler[:,1]

plt.plot(time, x_euler, label = 'prey ')plt.plot(time, y_euler, label = 'predator')plt.legend(loc='upper right')#labelsplt.xlabel("time")plt.ylabel("number of each species")#titleplt.title("predator prey model")
0 Dislike
Follow 2

Please Enter a comment

Submit

D

Doug Varney | 13/07/2020

excellent

2 0

Other Lessons for You

Write a python program to display dictonary details from list of dictonary
Print the Following: Print all dictonary in a seperate line. Name of all the products. Total quantity of all the products. Details of any product name when provided. Convert all the product name to upper case and print.
A

Ankit P.

0 0
0

Oscillator
An oscillator is a circuit that creates a waveform output from a direct current input. The two main types of oscillator are harmonic and relaxation. The harmonic oscillators have smooth curved waveforms,...

How to Become a Self Made Digital Marketing Expert?
Applying Digital Marketing on a Small Scale Choose a topic you are passionate about and start a blog about it. Cost: Approx $50 per year for domain and hosting. Learn a little about SEO and write SEO...

How to start writing a program using a Language like C?
Usually the starters get confused of certain things like which proagramming tool they would use and why. Like where should one use "for" loop and where to use "while". However, the most common problem...

what are the assumptions of torsion equation?
Following are the assumptions made for the derivation of torsion equation: The material is homogeneous (elastic property throughout) The material should follow Hook’s law The material should...
X

Looking for Python Training Classes?

The best tutors for Python Training Classes are on UrbanPro

  • Select the best Tutor
  • Book & Attend a Free Demo
  • Pay and start Learning

Learn Python Training with the Best Tutors

The best Tutors for Python Training Classes are on UrbanPro

This website uses cookies

We use cookies to improve user experience. Choose what cookies you allow us to use. You can read more about our Cookie Policy in our Privacy Policy

Accept All
Decline All

UrbanPro.com is India's largest network of most trusted tutors and institutes. Over 55 lakh students rely on UrbanPro.com, to fulfill their learning requirements across 1,000+ categories. Using UrbanPro.com, parents, and students can compare multiple Tutors and Institutes and choose the one that best suits their requirements. More than 7.5 lakh verified Tutors and Institutes are helping millions of students every day and growing their tutoring business on UrbanPro.com. Whether you are looking for a tutor to learn mathematics, a German language trainer to brush up your German language skills or an institute to upgrade your IT skills, we have got the best selection of Tutors and Training Institutes for you. Read more