"Bounds Error" when trying to solve an ODE system

I’m trying to solve an ODE system in Julia for the first time and I’ve encountered an error that I don’t understand. I’m just trying to solve a simple SEIR model with some arbitrary parameter values and initial conditions. Here’s my code:

using DifferentialEquations

function SEIR(t,y,pars)
    β, σ, M = pars
    S, E, I, R = y

    dS = -β*S*I
    dE = β*S*I - σ*E
    dI = σ*E - M*I
    dR = M*I
    return dS, dE, dI, dR
end

#Initial Conditions: S(0) = 0.950, E(0) = 0.0125, I(0) = 0.0125, R(0) = 0.0250
#tspan = [0,5.0]
#Parameters: β = 0.20, σ = 0.25, M = 0.23  
prob = ODEProblem(g = SEIR, u0 = (0.950, 0.0125, 0.0125, 0.0250), tspan = (0.0,5.0), parameters = (0.2, 0.25, 0.23))

sol = solve(prob, Tsit5(),reltol = 1e-8, abstol = 1e-8)

This is giving the following error:

ERROR: LoadError: MethodError: no method matching ODEProblem(; sys=SEIR, u0map=(0.95, 0.0125, 0.0125, 0.025), tspan=(0.0, 5.0), parammap=(0.2, 0.25, 0.23))
Closest candidates are:
  ODEProblem(::DiffEqBase.AbstractODEFunction, ::Any, ::Any, ::Any...; kwargs...) at C:\Users\Michael\.julia\packages\DiffEqBase\elwdl\src\problems\ode_problems.jl:71
  ODEProblem(::ModelingToolkit.AbstractODESystem, ::Any...; kwargs...) at C:\Users\Michael\.julia\packages\ModelingToolkit\v15TO\src\systems\diffeqs\abstractodesystem.jl:238        
  ODEProblem(::ReactionSystem, ::Union{Number, AbstractArray}, ::Any) at C:\Users\Michael\.julia\packages\ModelingToolkit\v15TO\src\systems\reaction\reactionsystem.jl:489 got unsupported keyword arguments "sys", "u0map", "tspan", "parammap"

It looks like it doesn’t recognize ODEProblem, but I’m just going by this tutorial: Ordinary Differential Equations · DifferentialEquations.jl. Is my syntax wrong? Or is there some other package I need to install?

That is not the documented syntax from the tutorial.

function SEIR(dy,y,pars,t)
    β, σ, M = pars
    S, E, I, R = y

    du[1] = -β*S*I
    du[2] = β*S*I - σ*E
    du[3] = σ*E - M*I
    du[4] = M*I
    return nothing
end

ODEProblem(SEIR, [0.950, 0.0125, 0.0125, 0.0250],(0.0,5.0),(0.2, 0.25, 0.23))

You might want to read the tutorial again.

2 Likes

Ah ok…I skimmed the tutorial too quickly…Thank you for the help! By the way, I watched a YouTube video you made on machine learning and modeling Covid-19!! It was super cool!

1 Like

No worries. Hopefully that puts you in the right path. Sometimes people just need a bump in the right direction.

Hi Chris, just quick follow-up question: When I change the function body to

    dS = -β*S*I
    dE = β*S*I - σ*E
    dI = σ*E - M*I
    dR = M*I
    du = dS, dE, dI, dR
    return nothing

it doesn’t work. (The solution when plotted was clearly wrong.) I also tried it with du = Array{Float64,1}([dS, dE, dI, dR]) and du = (dS, dE, dI, dR), but both were wrong. Why doesn’t this work? Isn’t du just an Array{Float64,1}?

There is some confusion above with both dy and du, make sure that you are using the name that appears in the argument list.

This syntax

dy = dS, dE, dI, dR

creates a new Tuple and assigns it the name dy, replacing the old definition.

You need to write new values to the existing dy, one way is to write to each index as in the solution above, another would be to use broadcast assignment to write new values in to the provided dy:

dy .= [dS, dE, dI, dR]
1 Like

Thanks! The broadcasting approach is exactly what I was looking for.

And yes, Chris made a little typo in his post, using both y’s and u’s…