411 REFERENCE · Wikibooks
Scientific Calculations with Julia/Predator-Prey Model
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Scientific Calculations with Julia Predator-Prey Model SIR Model Exercises in Julia
The predator–prey model, also known as the Lotka–Volterra equations, describes the interaction between two species:
a prey (e.g., rabbits)
a predator (e.g., foxes)
The ordinary differential equations are:
Where:
= prey population
= predator population
= prey growth rate in the absence of predators
= predation rate
= predator mortality rate in the absence of predators of prey
= predator growth rate due to predation
Implementation in Julia
Considering a system in which the initial conditions at time zero are 40 prey and 9 predators, we want to plot the trajectory graph or phase diagram in Julia, placing the prey population x on the horizontal axis and the predator population y on the vertical axis over 50 days. Julia solves the system of differential equations and plots the graph. The curve is closed and forms a cycle (orbit) that repeats indefinitely in the basic model. Each point on the curve represents a state of the system (number of prey, number of predators) at a certain time. Moving along the curve clockwise or counterclockwise (depending on the parameters), we pass through four typical phases:
Abundant prey, few predators → predators have abundant food → they begin to grow.
Prey declines, predators increase → predation reduces prey → predators continue to thrive for a while.
Prey scarce, predators abundant → predator population collapses due to lack of food.
Prey recovers, predators few → prey multiply → cycle begins again.
The closed cycle indicates that in the basic model there is no stable equilibrium: populations always fluctuate.
The center of the cycle corresponds to the theoretical equilibrium of the system (constant populations), but the model without external factors does not converge there.
In nature, factors such as disease, migration, or resource scarcity can dampen or amplify oscillations.
using DifferentialEquations
using Plots
# Model Parameters
α = 1.1 # natural prey growth
β = 0.4 # predation rate
γ = 0.4 # predator mortality
δ = 0.1 # predation efficiency
# System Function
function lotka_volterra!(du, u, p, t)
x, y = u
du[1] = α*x - β*x*y # dx/dt
du[2] = δ*x*y - γ*y # dy/dt
end
# Initial Conditions
u0 = [40.0, 9.0] # 40 prey, 9 predators
tspan = (0.0, 50.0)
# Problem Definition and Solution
prob = ODEProblem(lotka_volterra!, u0, tspan)
sol = solve(prob)
# Population over time plot
plot(sol, xlabel="Time", ylabel="Population",
label=["Prey" "Predators"], lw=2)
# Phase plot (Prey vs. Predators)
plot(sol[1,:], sol[2,:], xlabel="Prey", ylabel="Predators",
label="Trajectory", lw=2)
thumb|left|600px