# "Bounds Error" when trying to solve an ODE system

**URL:** <https://discourse.julialang.org/t/bounds-error-when-trying-to-solve-an-ode-system/52537>\
**Category:** General Usage\
**Tags:** diffeq, ode\
**Created:** [December 28, 2020, 8:44pm UTC](https://discourse.julialang.org/t/bounds-error-when-trying-to-solve-an-ode-system/52537 "2020-12-28T20:44:49Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Barmann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/michael_barmann/32/20389_2.png) [@Michael\_Barmann](https://discourse.julialang.org/u/Michael_Barmann)\
**Post date:** [December 28, 2020, 8:44pm UTC](https://discourse.julialang.org/t/bounds-error-when-trying-to-solve-an-ode-system/52537/1 "2020-12-28T20:44:49Z")

</div>

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:

```julia
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:

```julia
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](https://diffeq.sciml.ai/v2.0/tutorials/ode_example.html). Is my syntax wrong? Or is there some other package I need to install?

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [December 28, 2020, 8:57pm UTC](https://discourse.julialang.org/t/bounds-error-when-trying-to-solve-an-ode-system/52537/2 "2020-12-28T20:57:40Z")

</div>

> [@Michael\_Barmann](#):
>
> ```julia
> 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))
> 
> ```

That is not the documented syntax from the tutorial.

[https://diffeq.sciml.ai/stable/tutorials/ode\_example/](https://diffeq.sciml.ai/stable/tutorials/ode_example/)

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

---

<div class="post-metadata">

**Author:** ![Michael\_Barmann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/michael_barmann/32/20389_2.png) [@Michael\_Barmann](https://discourse.julialang.org/u/Michael_Barmann)\
**Post date:** [December 28, 2020, 9:08pm UTC](https://discourse.julialang.org/t/bounds-error-when-trying-to-solve-an-ode-system/52537/3 "2020-12-28T21:08:50Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [December 28, 2020, 9:28pm UTC](https://discourse.julialang.org/t/bounds-error-when-trying-to-solve-an-ode-system/52537/4 "2020-12-28T21:28:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Michael\_Barmann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/michael_barmann/32/20389_2.png) [@Michael\_Barmann](https://discourse.julialang.org/u/Michael_Barmann)\
**Post date:** [January 6, 2021, 11:26pm UTC](https://discourse.julialang.org/t/bounds-error-when-trying-to-solve-an-ode-system/52537/5 "2021-01-06T23:26:38Z")

</div>

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

```julia
    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}`?

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [January 6, 2021, 11:44pm UTC](https://discourse.julialang.org/t/bounds-error-when-trying-to-solve-an-ode-system/52537/6 "2021-01-06T23:44:58Z")

</div>

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

```julia
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](https://docs.julialang.org/en/v1/manual/arrays/#Broadcasting) assignment to write new values in to the provided `dy`:

```julia
dy .= [dS, dE, dI, dR]

```

---

<div class="post-metadata">

**Author:** ![Michael\_Barmann](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/michael_barmann/32/20389_2.png) [@Michael\_Barmann](https://discourse.julialang.org/u/Michael_Barmann)\
**Post date:** [January 6, 2021, 11:48pm UTC](https://discourse.julialang.org/t/bounds-error-when-trying-to-solve-an-ode-system/52537/7 "2021-01-06T23:48:04Z")

</div>

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…
