Python coding... Using Lotka Volterra Equations, simulate a prey-predator model
ID: 3583845 • Letter: P
Question
Python coding... Using Lotka Volterra Equations, simulate a prey-predator model using the info below and plot graphs.
2. The differential equations for this model can be given as, dt dy yy oxy dt where x(t): Predator population, y(t): Prey population, the constants: a: Prey increase rate (ifleft alone), B: Probability that the two species coming together. Y: Predator decrease rate (if left alone o: Rate of encounters of the two species (B -o generally) Take a 0.25, A3 3-o 0.01, y J -1, xo 80 and yo 30 and solve these equations using either the fourth order Runge-Kutta scheme or special commands defined by a Python module. Plot (t-x), (t-y), (x-y) graphsExplanation / Answer
Following code will perform the working for plotting the equation graph on the basis of runga kutta 4th order method
#!/usr/bin/env python
# importing the library for plotting the graph
import matplotlib.pyplot as plt
#import library to manage the multidimenissional array or matrics
import numpy as np
#function defintion for runga kutta 4th order equation solution
def rungakutta4(f, r, t, h):
""" calculate the k1, k2, k3,k4
k1 = h*finit(r, t)
k2 = h*finit(r+0.5*k1, t+0.5*h)
k3 = h*finit(r+0.5*k2, t+0.5*h)
k4 = h*finit(r+k3, t+h)
#calculate and reurn the value of runga kutta 4th order
return ((k1 + 2*k2 + 2*k3 + k4)/6)
"""
Definition of function to describe the intial values of alpha, beta, gama, sigma and intial x0, y0
"""
def finit(r, t):
alpha = 0.25
beta = -0.01
gamma = -1.0
sigma = -0.01
x, y = r[80], r[30]
#calculate the dt/dx
fxd = x*(alpha + beta*y)
#calculate the dt/dy
fyd = -y*(gamma + sigma*x)
#return the
return np.array([fxd, fyd], float)
"""
Let us assume to plot the points between 0 to 50
"""
tpoints = np.linspace(0, 50, 0.1)
xpoints = []
ypoints = []
r = np.array([80, 30], float)
for t in tpoints:
xpoints += [r[80]]
ypoints += [r[30]]
r += rungakutta4(f, r, t, h)
# drawing the point for x and y
plt.plot(tpoints, xpoints)
plt.plot(tpoints, ypoints)
plt.show()
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.