'Getting wrong output for Lotka Volterra Model in Python (Jupyter) using RK4 to solve , HOw much precision can affect?
I am new to coding. i don't know what is wrong with this code. I expect to get periodic solution for both x and y variable i.e. prey and predator with respect ot time but i am getting oscillations where amplitude of both goes on increasing with time. where is the problem.
import matplotlib.pyplot as plt
def x_dot(x,y):
return a*x-b*x*y
def y_dot(x,y):
return -c*y+d*x*y
h=0.01
a=1.1
b=0.1
c=1.4
d=0.1
def f(x,y):
t=0
x_values=[x]
y_values=[y]
t_values=[t]
while(t<500):
# RK4 Method to solve differential equations
k1=x_dot(x,y)
l1=y_dot(x,y)
k2=x_dot(x+(h*k1)/2,y+(k1*h)/2)
l2=y_dot(x+(h*l1)/2,y+(l1*h)/2)
k3=x_dot(x+(h*k2)/2,y+(k2*h)/2)
l3=y_dot(x+(h*l2)/2,y+(l2*h)/2)
k4=x_dot(x+h*k3,y+h*k3)
l4=y_dot(x+h*l3,y+h*l3)
t=(t+h)
# Appending calculated values in the respective list
t_values.append(t)
x=x+h*(k1/6+k2/3+k3/3+k4/6)
x_values.append(x)
y=y+h*(l1/6+l2/3+l3/3+l4/6)
y_values.append(y)
return x_values,y_values,t_values
x_values,y_values,t_values= f(1,1.3) #calling the function with intial values
plt.plot(t_values,x_values)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
