Delay Differential Equations (DDEs)
6 min read
Welcome to the Delay Differential Equations section of the course! π
So far, youβve mastered Ordinary Differential Equations (ODEs) where the future depends only on the current state. But what if your system has a memory? What if the rate of change right now depends on what happened 5 seconds ago?
This happens everywhere:
- Biology: Cells don't divide instantly; there's a maturation phase. π¦
- Engineering: A thermostat takes time to sense temperature changes. π‘οΈ
- Epidemiology: A virus has an incubation period before a person becomes infectious. π·
These systems are modeled using Delay Differential Equations (DDEs). In DifferentialEquations.jl, DDEs are first-class citizens, handled with the same high-performance magic as ODEs.
1. Understanding Memory in Dynamical Systems: The History Function
In an ODE, to start the simulation at , you only need a single point: the initial condition .
In a DDE, to calculate the derivative at involving a delay of , the equation looks at . This means you need to know the state of the system before the simulation even starts!
This "pre-start" memory is defined by the History Function, usually denoted as .
The Concept
Think of the history function as the "backstory" of your system.
- ODE: Starts at page 1.
- DDE: Starts at page 1, but references a Prequel book. is that prequel.
Defining h
In Julia, h is a simple function. It takes parameters p and time t (and optionally idxs for specific variables) and returns the state at that time.
# A simple history function assuming the system was at 0 constant before t=0
function history(p, t)
return 1.0
endπ‘ Did you know?
Mathematically, the "state" of a DDE isn't a finite vector like in ODEs. The state is actually an infinite-dimensional function space because you need the entire curve from to predict the future. Don't worry,DifferentialEquations.jlhandles this infinity for you! π§
2. Defining a DDEProblem with Constant Delays
Now let's code the "Hello World" of DDEs: the Hutchinson-Wright Equation (also known as the Delayed Logistic Equation).
Here, the growth rate slows down based on the population size seconds ago.
The Syntax
The signature for a DDE function is slightly different from an ODE. It receives the history function h as an argument:
f(u, h, p, t)
Step-by-Step Implementation
1. Define the Equation:
Notice we use h(p, t - tau) to peek into the past.
using DifferentialEquations
# Parameters: tau (delay)
const tau = 1.0
function hutchinson_wright(u, h, p, t)
# The history function h is a callable!
# We call it with parameters p and the past time (t - tau)
past_u = h(p, t - tau)
# The equation: du/dt = u(t) * (1 - u(t-tau))
return u * (1.0 - past_u)
end2. Define the History and Initial Data:
We need to tell the solver what happened before . Let's say the population was stable at .
# History function: returns the state for t < t0
h(p, t) = 0.1
# Initial condition at exactly t0
u0 = 0.1
tspan = (0.0, 40.0)
p = nothing3. The constant_lags Argument:
This is critical for performance and accuracy. Discontinuities propagate in DDEs. If you jerk the steering wheel now, the car jerks now, and if there's a delay, the reaction jerks seconds later.
You must tell the solver about these lags so it can step carefully over them.
# Define the problem
# constant_lags is a list of all constant delays used in your equation
prob = DDEProblem(hutchinson_wright, u0, h, tspan, p; constant_lags=[tau])4. Solve it!
We use MethodOfSteps, which transforms the DDE into a sequence of ODEs.
# MethodOfSteps is the standard wrapper for DDE algorithms
alg = MethodOfSteps(Tsit5())
sol = solve(prob, alg)
using Plots
plot(sol, title="Hutchinson-Wright Delay Equation", label="Population")3. Accessing Past States: Working with the h Interpolator
In the example above, we used h(p, t - tau). Let's look "under the hood" of this object.
When you are inside your function f(u, h, p, t), the object h is not just the simple function you defined in step 2.
It is a powerful interpolator constructed by the solver.
- If you ask for
h(p, t)where , it calls your user-defined history function. - If you ask for
h(p, t)where (but in the past relative to current time), it interpolates the solution the solver has already calculated.
Performance Tip: idxs
If you have a system of equations (e.g., 3 variables) but only need the past value of the 2nd variable, don't compute the whole vector! Use the idxs keyword.
function complex_system(u, h, p, t)
# SLOW: Computes past values for all variables, allocates a vector
# past_state = h(p, t - 1.0)
# v_past = past_state[2]
# FAST: Only computes the 2nd variable, returns a scalar
v_past = h(p, t - 1.0; idxs=2)
du1 = ...
du2 = ...
du3 = ...
endβ οΈ Warning: Never use
u(t - tau)inside the function.uis the current state vector (just numbers). You must usehto access time.
4. Handling Dependent Delays and Lag Specifications
Sometimes, the delay isn't constant. It might depend on the state itself. This is called a State-Dependent Delay.
Scenario: A queue at a server. The time it takes to get a response (delay) depends on how many people are currently in the queue (state ).
This is mathematically much harder because the "jumps" (discontinuities) move around as the solution changes.
The dependent_lags Tuple
Instead of constant_lags, we pass a tuple of functions to dependent_lags. Each function calculates a lag (not the time , but the delay ).
Syntax: (u, p, t) -> lag
Example:
Let's solve a simple equation where the delay depends on : .
# The lag function: returns the magnitude of the delay
my_lag_func(u, p, t) = 0.2 * u[1] # delay grows as u grows
function state_dependent_model(du, u, h, p, t)
# Calculate current delay
tau = 0.2 * u[1]
# Access history using calculated delay
# Note: We use idxs=1 because u is a vector
past_u = h(p, t - tau; idxs=1)
du[1] = -past_u
end
u0 = [1.0]
h_dep(p, t) = [1.0] # History must match u0 shape (vector)
tspan = (0.0, 10.0)
# Pass the lag function tuple to dependent_lags
prob_dep = DDEProblem(state_dependent_model, u0, h_dep, tspan;
dependent_lags = ((u,p,t) -> 0.2*u[1],))
# We generally need a residual control integrator for these
# RK4 is a robust choice for state-dependent delays
alg_dep = MethodOfSteps(RK4())
sol_dep = solve(prob_dep, alg_dep)π Industry Insight: State-dependent delays are frequent in supply chain management. The "delay" for a product to arrive depends on the "congestion" (state) of the shipping network. Solving these correctly prevents the "Bullwhip Effect" in simulations!
Summary
- DDEs depend on past states.
- History Function
h(p, t)defines the past before . - Inside the function,
hacts as an interpolator. Useh(p, t - tau). - Constant Lags should be declared in
DDEProblemfor accuracy. - State-Dependent Delays require
dependent_lagsand careful solver selection.
π Further Reading
Challenge
You are tasked with modeling a biological population that exhibits logistic growth, but with a twist: the regulatory effect of the carrying capacity is delayed. Furthermore, this delay is not constant. As the population size increases, resource competition intensifies, leading to a longer maturation period for individuals. This means the time delay Ο is dependent on the current state u(t).
Your model must implement the following state-dependent Delay Differential Equation:
du/dt = r * u(t) * (1 - u(t - Ο(t)) / K)
Where:
u(t)is the population size at timet.ris the intrinsic growth rate.Kis the environmental carrying capacity.Ο(t)is the state-dependent delay, defined asΟ(t) = a * u(t), whereais a constant coefficient.
Your task is to write the complete Julia code using the DifferentialEquations.jl package to set up and solve this problem.
Requirements:
- Parameters: Use the following values:
r = 0.1K = 100.0a = 0.05
- History: For all times
t β€ 0, the population was constant at10.0. - Time Span: The simulation should run from
t = 0.0tot = 200.0. - Problem Definition:
- Define the DDE function
population_dde!. - Define the history function
history_func. - Define the state-dependent lag function
lag_func. - Construct the
DDEProblem, correctly specifying thedependent_lagsargument with a tuple containing your lag function.
- Define the DDE function
- Solver: Solve the constructed problem using the
MethodOfStepsalgorithm withRK4()as the underlying integrator.
Expected result
The output should be a single, complete block of Julia code. This code must successfully:
- Import the
DifferentialEquationslibrary. - Correctly define the parameters
pas a tuple or another suitable structure containingr,K, anda. - Implement the DDE function
population_dde!, which uses the history functionhto access the delayed stateu(t - Ο(t)). - Implement the history function
history_functo return[10.0]. - Implement the state-dependent lag function
lag_functo calculatea * u[1]. - Correctly define
u0andtspan. - Construct the
DDEProblemwith the DDE function, initial condition, history function, time span, parameters, and thedependent_lagskeyword argument assigned to a tuple containinglag_func. - Call the
solvefunction with theDDEProblemand the specified algorithm:MethodOfSteps(RK4()).
The code should be syntactically correct and follow the structure provided in the initial code block. The final line of code should be the solve call.