title: Lotka-Volterra equations(predator prey) using Runge-Kutta in Python
body: <p>I am trying to write a program using the Lotka-Volterra equations for predator-prey interactions. Solve Using ODE's:</p>
  
  <pre><code>dx/dt = a*x - B*x*y
  dy/dt = g*x*y - s*y
  </code></pre>
  
  <p>Using 4th order Runge-Kutta method</p>
  
  <p>I need to plot a graph showing both x and y as a function of time from t = 0 to t=30.</p>
  
  <blockquote>
    <p>a = alpha = 1<br>
    b = beta  = 0.5<br>
    g = gamma = 0.5<br>
    s = sigma = 2<br>
    initial conditions x = y = 2  </p>
  </blockquote>
  
  <p>Here is my code so far but not display anything on the graph. Some help would be nice.</p>
  
  <pre><code>#!/usr/bin/env python
  
  from __future__ import division, print_function
  import matplotlib.pyplot as plt
  import numpy as np
  
  def rk4(f, r, t, h):
          """ Runge-Kutta 4 method """
          k1 = h*f(r, t)
          k2 = h*f(r+0.5*k1, t+0.5*h)
          k3 = h*f(r+0.5*k2, t+0.5*h)
          k4 = h*f(r+k3, t+h)
          return (k1 + 2*k2 + 2*k3 + k4)/6
  
  def f(r, t):
          alpha = 1.0
          beta = 0.5
          gamma = 0.5
          sigma = 2.0
          x, y = r[2], r[2]
          fxd = x*(alpha - beta*y)
          fyd = -y*(gamma - sigma*x)
          return np.array([fxd, fyd], float)
  
  
  tpoints = np.linspace(0, 30, 0.1)
  xpoints = []
  ypoints = []
  
  r = np.array([2, 2], float)
  for t in tpoints:
          xpoints += [r[2]]
          ypoints += [r[2]]
          r += rk4(f, r, t, h)
  
  plt.plot(tpoints, xpoints)
  plt.plot(tpoints, ypoints)
  plt.xlabel("Time")
  plt.ylabel("Population")
  plt.title("Lotka-Volterra Model")
  plt.savefig("Lotka_Volterra.png")
  plt.show()
  </code></pre>