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 t=0t=0, you only need a single point: the initial condition u0u_0.

In a DDE, to calculate the derivative at t=0t=0 involving a delay of Ο„\tau, the equation looks at u(0βˆ’Ο„)u(0 - \tau). 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 h(p,t)h(p, t).

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. h(p,t)h(p, t) 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.

julia
# 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 u(t)u(t) like in ODEs. The state is actually an infinite-dimensional function space because you need the entire curve from [tβˆ’Ο„,t][t-\tau, t] to predict the future. Don't worry, DifferentialEquations.jl handles 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).

dudt=u(t)(1βˆ’u(tβˆ’Ο„))\frac{du}{dt} = u(t) (1 - u(t - \tau))

Here, the growth rate slows down based on the population size Ο„\tau 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.

julia
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)
end

2. Define the History and Initial Data:
We need to tell the solver what happened before t=0t=0. Let's say the population was stable at u=0.1u=0.1.

julia
# 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 = nothing

3. 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 Ο„\tau seconds later.

You must tell the solver about these lags so it can step carefully over them.

julia
# 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.

julia
# 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.

  1. If you ask for h(p, t) where t<t0t < t_0, it calls your user-defined history function.
  2. If you ask for h(p, t) where t>t0t > t_0 (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.

julia
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. u is the current state vector (just numbers). You must use h to 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 uu).

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 tt, but the delay Ο„\tau).

Syntax: (u, p, t) -> lag

Example:
Let's solve a simple equation where the delay depends on uu: Ο„=0.2u(t)\tau = 0.2 u(t).

julia
# 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

  1. DDEs depend on past states.
  2. History Function h(p, t) defines the past before t=0t=0.
  3. Inside the function, h acts as an interpolator. Use h(p, t - tau).
  4. Constant Lags should be declared in DDEProblem for accuracy.
  5. State-Dependent Delays require dependent_lags and 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 time t.
  • r is the intrinsic growth rate.
  • K is the environmental carrying capacity.
  • Ο„(t) is the state-dependent delay, defined as Ο„(t) = a * u(t), where a is 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:

  1. Parameters: Use the following values:
    • r = 0.1
    • K = 100.0
    • a = 0.05
  2. History: For all times t ≀ 0, the population was constant at 10.0.
  3. Time Span: The simulation should run from t = 0.0 to t = 200.0.
  4. 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 the dependent_lags argument with a tuple containing your lag function.
  5. Solver: Solve the constructed problem using the MethodOfSteps algorithm with RK4() as the underlying integrator.

Expected result

The output should be a single, complete block of Julia code. This code must successfully:

  1. Import the DifferentialEquations library.
  2. Correctly define the parameters p as a tuple or another suitable structure containing r, K, and a.
  3. Implement the DDE function population_dde!, which uses the history function h to access the delayed state u(t - Ο„(t)).
  4. Implement the history function history_func to return [10.0].
  5. Implement the state-dependent lag function lag_func to calculate a * u[1].
  6. Correctly define u0 and tspan.
  7. Construct the DDEProblem with the DDE function, initial condition, history function, time span, parameters, and the dependent_lags keyword argument assigned to a tuple containing lag_func.
  8. Call the solve function with the DDEProblem and 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.

julia

Solve the challenge, then check it.

Ctrl+Enter

Course complete!

You've passed every challenge in this course.