TExcelTAD
Home › Calculus in Depth › Predator-Prey Systems

Predator-Prey Systems

When two populations affect each other, one differential equation becomes a system of two. The Lotka–Volterra predator-prey model is the classic example: it has a conserved quantity, so its solutions cycle forever, and that property makes it a good test of numerical integrators and a good warning for anyone building an ecosystem into a game.

Background: Models for Population Growth for single species, and Numerical Solutions: Euler and RK4 for the solvers used here.

The Lotka–Volterra equations

Let x(t)x(t) be the prey (say rabbits) and y(t)y(t) the predators (foxes). Assume that without foxes rabbits grow exponentially, without rabbits foxes die out exponentially, and that encounters, proportional to x⋅yx \cdot y, hurt rabbits and help foxes:

dxdt=a⋅x−b⋅x⋅ydydt=−c⋅y+d⋅x⋅y(a,b,c,d>0)\begin{aligned} \frac{dx}{dt} &= a \cdot x - b \cdot x \cdot y \\ \frac{dy}{dt} &= -c \cdot y + d \cdot x \cdot y && (a, b, c, d > 0) \end{aligned}

The system is autonomous (tt does not appear on the right), so we can study it in the phase plane: each solution traces a curve (x(t),y(t))(x(t), y(t)), and dividing the equations gives the slope of that curve without tt:

dydx=y(−c+d⋅x)x(a−b⋅y)\frac{dy}{dx} = \frac{y(-c + d \cdot x)}{x(a - b \cdot y)}

Equilibria

Setting both right-hand sides to zero: x(a−by)=0x(a - by) = 0 and y(−c+dx)=0y(-c + dx) = 0. The solutions are the extinction point (0,0)(0, 0) and the coexistence point

(x∗,y∗)=(cd,ab)(x^*, y^*) = \left(\frac{c}{d}, \frac{a}{b}\right)

Linearising at (x∗,y∗)(x^*, y^*) gives the Jacobian [0−bc/dad/b0]\begin{bmatrix} 0 & -bc/d \\ ad/b & 0 \end{bmatrix}, whose eigenvalues are ±i⋅ac\pm i \cdot \sqrt{ac}: purely imaginary, so small deviations circle the equilibrium with period close to 2π/ac2\pi/\sqrt{ac}. Linearisation alone cannot decide whether a centre is really a centre (a tiny nonlinear effect could make it a slow spiral). The next theorem settles it.

A conserved quantity

Theorem. Along every solution with x,y>0x, y > 0, the function

V(x,y)=d⋅x−c⋅ln⁡x+b⋅y−a⋅ln⁡yV(x, y) = d \cdot x - c \cdot \ln x + b \cdot y - a \cdot \ln y

is constant. Consequently the orbits are closed curves and the populations are periodic.

Proof. By the chain rule,

dVdt=(d−cx)⋅x′+(b−ay)⋅y′=(d−cx)⋅x(a−by)+(b−ay)⋅y(dx−c)=(dx−c)(a−by)+(by−a)(dx−c)=0\begin{aligned} \frac{dV}{dt} &= \left(d - \frac{c}{x}\right) \cdot x' + \left(b - \frac{a}{y}\right) \cdot y' \\ &= \left(d - \frac{c}{x}\right) \cdot x(a - by) + \left(b - \frac{a}{y}\right) \cdot y(dx - c) \\ &= (dx - c)(a - by) + (by - a)(dx - c) = 0 \end{aligned}

VV is strictly convex with its minimum at (c/d,a/b)(c/d, a/b), so its level sets are closed loops around that point; a solution stays on one loop and, since it never stops (no other equilibria with x,y>0x, y > 0), it goes round and round. ∎

Corollary (averages). Over one period TT, the average prey is c/dc/d and the average predator is a/ba/b, the equilibrium values. Divide the first equation by xx: (ln⁡x)′=a−b⋅y(\ln x)' = a - b \cdot y. Integrate over a period; the left side gives ln⁡x(T)−ln⁡x(0)=0\ln x(T) - \ln x(0) = 0, so 1T∫0Ty dt=a/b\frac{1}{T}\int_0^T y\,dt = a/b. The prey average follows the same way.

python · try it yourself
import numpy as np
from scipy.integrate import solve_ivp

a, b, c, d = 1.0, 0.1, 1.5, 0.075            # equilibrium (20, 10)

def lv(t, s):
    x, y = s
    return [a * x - b * x * y, -c * y + d * x * y]

V = lambda x, y: d * x - c * np.log(x) + b * y - a * np.log(y)
up = lambda t, s: s[0] - c / d               # prey crosses 20 ...
up.direction = 1                             # ... going upward

sol = solve_ivp(lv, (0, 40), [10.0, 5.0], rtol=1e-10, atol=1e-10,
                dense_output=True, events=up)
for t in (0, 2, 4, 6):
    x, y = sol.sol(t)
    print(f"t = {t}: prey {x:6.2f}, predators {y:6.2f}, V = {V(x, y):.8f}")

T0, T1 = sol.t_events[0][:2]
ts = np.linspace(T0, T1, 20001)
xs, ys = sol.sol(ts)
print(f"period ≈ {T1 - T0:.4f}  (small-oscillation estimate {2*np.pi/np.sqrt(a*c):.4f})")
print(f"averages over one period: prey {np.trapezoid(xs, ts)/(T1-T0):.4f}, "
      f"predators {np.trapezoid(ys, ts)/(T1-T0):.4f}")
t = 0: prey  10.00, predators   5.00, V = -3.81331555
t = 2: prey  35.31, predators   5.06, V = -3.81331555
t = 4: prey  10.08, predators  17.70, V = -3.81331555
t = 6: prey  13.51, predators   3.60, V = -3.81331555
period ≈ 5.4797  (small-oscillation estimate 5.1302)
averages over one period: prey 20.0000, predators 10.0000

The period is longer than the linear estimate because this orbit is far from the equilibrium; the averages match (c/d,a/b)=(20,10)(c/d, a/b) = (20, 10) as the corollary promises.

Integrators and ecosystem simulations

A game or simulation usually advances an ecosystem one frame at a time. The obvious update, explicit Euler, adds a little "energy" on every step: VV creeps up, the cycles grow, and eventually a population crashes to almost zero and the species goes extinct in your game for purely numerical reasons. A one-line change, using the new prey value when updating the predators (semi-implicit or symplectic Euler, the same trick used for game physics, see Integrators), keeps the orbit bounded.

python · try it yourself
import math
import matplotlib.pyplot as plt

a, b, c, d = 1.0, 0.1, 1.5, 0.075
V = lambda x, y: d * x - c * math.log(x) + b * y - a * math.log(y)
dt, steps = 0.05, 1200                       # 60 time units

def run(semi):
    x, y, path = 10.0, 5.0, []
    for _ in range(steps):
        xn = x + dt * (a * x - b * x * y)
        y = y + dt * (-c * y + d * (xn if semi else x) * y)
        x = xn
        path.append((x, y))
    return path

plt.figure(figsize=(6, 4))
for semi, name in ((False, "explicit Euler"), (True, "semi-implicit Euler")):
    p = run(semi)
    plt.plot(*zip(*p), lw=0.8, label=name)
    print(f"{name:20s}: V start {V(10, 5):.4f}, V end {V(*p[-1]):.4f}, "
          f"min prey {min(q[0] for q in p):.3f}")
plt.xlabel("prey"); plt.ylabel("predators"); plt.legend(); plt.show()
explicit Euler      : V start -3.8133, V end 11.2334, min prey 0.000
semi-implicit Euler : V start -3.8133, V end -3.8165, min prey 8.056

With the same step size, explicit Euler drives VV from −3.81 to about 11.2 and the prey down to (numerically) zero, while the semi-implicit run keeps VV within 0.004 of its true value.

Tip: For ecosystem mechanics, also clamp populations at a minimum or use integer agents with a seeded RNG. Real game ecosystems are usually agent-based (each rabbit is an entity), and the ODE describes their average behaviour, which is a useful sanity check for tuning.

Beyond the basic model

Model Change Effect
Logistic prey a⋅x(1−x/K)a \cdot x(1 - x/K) instead of a⋅xa \cdot x cycles spiral into a stable equilibrium
Holling type II predation b⋅x⋅y/(1+h⋅x)b \cdot x \cdot y/(1 + h \cdot x) (predators get full) can produce a stable limit cycle
Competition both terms negative: x′=x(a−bx−ey)x' = x(a - bx - ey) one species wins or both coexist
Three species food chains chaos becomes possible

In software

  • Game ecosystems and economies: wildlife simulations, resource-and-consumer loops in strategy games, even player-versus-gold-farmer balance follow Lotka–Volterra-like feedback. Use the averages theorem to predict long-run levels from parameters.
  • Testing ODE solvers: VV is an exact invariant, so its drift measures integration error, like energy drift in physics engines.
  • Networks and security: models of worms versus patching, or predators (attackers) and prey (vulnerable hosts), have the same structure. SciPy's ODE tools solve all of them the same way.

Exercises

Exercise 1: Equilibrium

Find the coexistence equilibrium of x′=0.6x−0.02xyx' = 0.6x - 0.02xy, y′=−0.9y+0.03xyy' = -0.9y + 0.03xy.

Answer

(x∗,y∗)=(30,30)(x^*, y^*) = (30, 30)

Solution
0.6−0.02y=0⇒y=30−0.9+0.03x=0⇒x=30\begin{aligned} 0.6 - 0.02y &= 0 \quad \Rightarrow \quad y = 30 \\ -0.9 + 0.03x &= 0 \quad \Rightarrow \quad x = 30 \end{aligned}

Exercise 2: Small-oscillation period

For the system in Exercise 1, estimate the period of small oscillations around the equilibrium.

Answer

2π/0.6⋅0.9≈8.552\pi/\sqrt{0.6 \cdot 0.9} \approx 8.55

Solution

The Jacobian at (c/d,a/b)(c/d, a/b) is [0−bc/dad/b0]\begin{bmatrix} 0 & -bc/d \\ ad/b & 0 \end{bmatrix}, with eigenvalues ±iac\pm i\sqrt{ac}, so the period is 2π/ac=2π/0.54≈8.5502\pi/\sqrt{ac} = 2\pi/\sqrt{0.54} \approx 8.550.

Exercise 3: Volterra's principle

Fishing removes a fraction ff of both populations per unit time: aa becomes a−fa - f and cc becomes c+fc + f. What happens to the average prey and predator levels? Check with a,b,c,d=1.0,0.1,1.5,0.075a, b, c, d = 1.0, 0.1, 1.5, 0.075 and f=0.3f = 0.3.

Answer

Average prey rises to (c+f)/d=24(c + f)/d = 24, average predators fall to (a−f)/b=7(a - f)/b = 7.

Solution

By the averages corollary the long-run means are the new equilibrium values. Moderate fishing helps the prey: this was Volterra's explanation of why the share of predatory fish in the Adriatic rose when fishing stopped during World War I.

python · try it yourself
a, b, c, d, f = 1.0, 0.1, 1.5, 0.075, 0.3
print("before:", (c / d, a / b))
print("after: ", (round((c + f) / d, 6), round((a - f) / b, 6)))
before: (20.0, 10.0)
after:  (24.0, 7.0)

Exercise 4: Competition equilibria

For the competing-species system x′=x(2−x−y)x' = x(2 - x - y), y′=y(3−x−2y)y' = y(3 - x - 2y), find all equilibria in the closed first quadrant.

Answer

(0,0)(0, 0), (2,0)(2, 0), (0,1.5)(0, 1.5) and (1,1)(1, 1).

Solution
x=0 or x+y=2;y=0 or x+2y=3x=0, y=0→(0,0)x=0, x+2y=3→(0,3/2)x+y=2, y=0→(2,0)x+y=2, x+2y=3→y=1, x=1\begin{aligned} &x = 0 \text{ or } x + y = 2; \qquad y = 0 \text{ or } x + 2y = 3 \\ &x = 0,\ y = 0 && \to (0, 0) \\ &x = 0,\ x + 2y = 3 && \to (0, 3/2) \\ &x + y = 2,\ y = 0 && \to (2, 0) \\ &x + y = 2,\ x + 2y = 3 && \to y = 1,\ x = 1 \end{aligned}
python · try it yourself
import sympy as sp

x, y = sp.symbols("x y")
print(sp.solve([x * (2 - x - y), y * (3 - x - 2 * y)], [x, y]))
[(0, 0), (0, 3/2), (1, 1), (2, 0)]

Test yourself

In x′=ax−bxyx' = ax - bxy, y′=−cy+dxyy' = -cy + dxy, where is the coexistence equilibrium?

  1. (a/b,c/d)(a/b, c/d)
  2. (c/d,a/b)(c/d, a/b)
  3. (b/a,d/c)(b/a, d/c)
  4. (0,0)(0, 0)

Why are Lotka–Volterra orbits closed curves rather than spirals?

  1. The Jacobian has real eigenvalues
  2. The function V=dx−cln⁡x+by−aln⁡yV = dx - c \ln x + by - a \ln y is constant along solutions
  3. Because the equations are linear
  4. Because solve_ivp uses small steps

A game updates rabbits and foxes with explicit Euler and the cycles keep growing until a species dies out. What is the cheapest fix?

  1. Use a larger time step
  2. Update one population using the other's new value (semi-implicit Euler) or use RK4
  3. Add random noise
  4. Round populations to integers