Predator-Prey and Competition Models
Two interacting populations are the classic nonlinear system. Depending on the signs of the interaction terms they model predation (one species eats the other) or competition (both want the same food). The linearization tools of this chapter predict who survives.
The Lotka-Volterra predator-prey model
Prey (rabbits) grow on their own; predators (foxes) die without prey; encounters, proportional to , remove prey and feed predators:
Critical points: and . The Jacobian at the origin has eigenvalues and : a saddle. At the coexistence point it has eigenvalues : a linear center. Here the center survives, because the system has a conserved quantity
whose level curves are closed. Populations cycle forever, the predator peak lagging behind the prey peak.
import numpy as np
from scipy.integrate import solve_ivp
a, b, c, d = 1.0, 0.1, 1.5, 0.075
H = lambda x, y: d * x - c * np.log(x) + b * y - a * np.log(y)
sol = solve_ivp(lambda t, z: [a*z[0] - b*z[0]*z[1], -c*z[1] + d*z[0]*z[1]], (0, 30), [10.0, 5.0],
dense_output=True, rtol=1e-10, atol=1e-12)
t = np.linspace(0, 30, 3001)
x, y = sol.sol(t)
print("equilibrium:", (c / d, a / b))
print(f"prey range [{x.min():.2f}, {x.max():.2f}], predator range [{y.min():.2f}, {y.max():.2f}]")
print(f"H drift over 30 time units: {np.ptp(H(x, y)):.1e}")
print(f"first prey peak at t = {t[np.argmax(x[:1000])]:.2f}, first predator peak at t = {t[np.argmax(y[:1000])]:.2f}")equilibrium: (20.0, 10.0)
prey range [7.93, 40.60], predator range [3.09, 23.28]
H drift over 30 time units: 6.7e-10
first prey peak at t = 2.49, first predator peak at t = 3.39
The average populations over one cycle equal the equilibrium values, a surprising consequence of the model ("Volterra's principle"): fishing both species reduces the average prey less than you would think and the average predators more.
A more realistic variant
Lotka-Volterra cycles are structurally fragile: any small change to the model can turn the center into a spiral. Adding a carrying capacity for the prey,
makes the coexistence point a spiral sink: oscillations damp out and the populations settle.
import numpy as np
from scipy.integrate import solve_ivp
import sympy as sp
a, b, c, d, K = 1.0, 0.1, 1.5, 0.075, 100.0
X, Y = sp.symbols("x y")
F = sp.Matrix([a*X*(1 - X/K) - b*X*Y, -c*Y + d*X*Y])
eq = {X: c / d, Y: a / b * (1 - c / (d * K))}
print("coexistence point:", {k: round(float(v), 3) for k, v in eq.items()})
print("eigenvalues:", [complex(e) for e in F.jacobian([X, Y]).subs(eq).evalf().eigenvals()])
sol = solve_ivp(lambda t, z: [a*z[0]*(1 - z[0]/K) - b*z[0]*z[1], -c*z[1] + d*z[0]*z[1]], (0, 200), [10.0, 5.0], rtol=1e-9)
print("state at t = 200:", np.round(sol.y[:, -1], 3))coexistence point: {x: 20.0, y: 8.0}
eigenvalues: [(-0.10000000000000003-1.0908712114635715j), (-0.10000000000000003+1.0908712114635715j)]
state at t = 200: [20. 8.]
Competition
For two species competing for the same resource,
the outcome depends on whether competition between species is stronger than within each species. In the example of Critical Points and Linearization, the coexistence point was a saddle: one species always wins (competitive exclusion). If each species limits itself more than it limits the other, the coexistence point is a sink and both survive.
In software
- Population ecologists fit these models to data with
solve_ivpinside an optimizer (scipy.optimize.least_squares), the same workflow as the epidemic fitter at the end of this course. - Game designers use predator-prey dynamics to balance simulated ecosystems and in-game economies (resources versus consumers), and the cycles explain boom-and-bust behaviour players notice.
Exercises
Exercise 1: Equilibrium
Find the coexistence point of , .
Answer
.
Solution
with gives ; with gives .
Exercise 2: Cycle period near the center
What is the approximate period of small oscillations around the coexistence point in Exercise 1?
Answer
.
Solution
The eigenvalues are , so small cycles have angular frequency .
Exercise 3: Averages over a cycle (code)
For the lesson's parameters, check numerically that the average prey and predator populations over one cycle equal and .
Answer
The averages come out as 20 and 10 (to the accuracy of the integration).
Solution
Integrate exactly one cycle (detect the return to the starting prey value while prey is increasing) and average.
import numpy as np
from scipy.integrate import solve_ivp
a, b, c, d = 1.0, 0.1, 1.5, 0.075
f = lambda t, z: [a*z[0] - b*z[0]*z[1], -c*z[1] + d*z[0]*z[1]]
back = lambda t, z: z[0] - 10.0
back.direction = 1
s = solve_ivp(f, (0, 30), [10.0, 5.0], events=back, rtol=1e-11, atol=1e-12)
T = s.t_events[0][1] if s.t_events[0][0] < 1e-6 else s.t_events[0][0]
s2 = solve_ivp(f, (0, T), [10.0, 5.0], dense_output=True, rtol=1e-11, atol=1e-12)
t = np.linspace(0, T, 200001)
x, y = s2.sol(t)
print(round(T, 4), round(np.trapezoid(x, t) / T, 4), round(np.trapezoid(y, t) / T, 4))5.4797 20.0 10.0
Test yourself
In the Lotka-Volterra model, the coexistence equilibrium is…
- a sink
- a center surrounded by closed cycles
- a saddle
- a source
Adding a prey carrying capacity to Lotka-Volterra typically…
- makes the populations explode
- turns the cycles into damped oscillations toward equilibrium
- removes the equilibrium
- has no effect
In a predator-prey cycle, the predator peak…
- comes before the prey peak
- lags behind the prey peak
- coincides with it
- never happens