# Add sum term to ODE in DifferentialEquations

**URL:** <https://discourse.julialang.org/t/add-sum-term-to-ode-in-differentialequations/49463>\
**Category:** General Usage\
**Tags:** question, diffeq\
**Created:** [November 2, 2020, 3:12pm UTC](https://discourse.julialang.org/t/add-sum-term-to-ode-in-differentialequations/49463 "2020-11-02T15:12:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [November 2, 2020, 3:12pm UTC](https://discourse.julialang.org/t/add-sum-term-to-ode-in-differentialequations/49463/1 "2020-11-02T15:12:20Z")

</div>

Hello,  
I wrote some ODEs in Julia with DifferentialEquations. Now I have to translate the following into Julia:  
 ![fig](https://global.discourse-cdn.com/julialang/original/3X/5/f/5facec2a057748dec4b9c536c5dbf540ece799e9.jpeg)  
The ODEs I have written are:

```julia
function ode!(du, u, p, t)
    μ, ϕ, ω, β, τ = p
    #= with:
      a = μ
      b = ϕ
      l,s = τ
      r = β
     du[1] = susceptible
     du[2] = infected
     du[3] = phages
    =#
    du[1] = (μ * u[1]) -(ϕ * u[1] * u[3]) - (ω * u[1])
    du[2] = (ϕ * u[1] * u[3]) - (ω * u[2]) # - SUM
    du[3] = - (ϕ * u[1] * u[3]) - (ω * u[3]) # + (β * SUM) 
end

```

But the problem is how to insert the SUM term. In a delayed ODE set I wrote:

```julia
function dynDelay!(du, u, h, p, t)
    μ, κ, ϕ, ω, β, ρ, τ = p
    #=
    du[1] = susceptible
    du[2] = infected
    du[3] = phages
    =#
    history = h(p, t - τ)
    delay_lysis = ϕ * history[1] * history[3] * exp(-1 * ω * τ)
    du[1] = (μ * u[1]) * (1 - (u[1]+u[2])/κ) - (ϕ * u[1] * u[3]) - (ω * u[1])
    du[2] = (ϕ * u[1] * u[3]) - delay_lysis - (ω * u[2])
    du[3] = (β * delay_lysis) - (ρ * ϕ * u[1] * u[3]) - (ω * u[3])
end

```

In this case, there is also an additional term (delay\_lysisi), which comes from history, to add to the ODEs. Would the sum be a similar thing?  
Thank you

---

<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:** [November 2, 2020, 4:05pm UTC](https://discourse.julialang.org/t/add-sum-term-to-ode-in-differentialequations/49463/2 "2020-11-02T16:05:34Z")

</div>

Yes, you’d just do `sum(...)`

---

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [November 8, 2020, 8:07pm UTC](https://discourse.julialang.org/t/add-sum-term-to-ode-in-differentialequations/49463/3 "2020-11-08T20:07:06Z")

</div>

I converted the formulae in:

```julia
function dynVirul!(du, u, p, t)
    μ, ϕ, ω, Ω, β, τ = p
    #=
    du[1] = susceptible
    du[2] = infected
    du[3] = phages
    τ = latency time, l
    μ = growth rate, a
    ϕ = adsorption rate, b
    ω = outflow (loss rate bacteria), p
    Ω = outflow (loss rate phage), s = p
    β = burst size, r
    =#
    sumTerm = sum(u[2] * (ϕ * u[1] * u[3] - ω * ϕ * u[1] * u[3]))
    du[1] = (μ * u[1]) - (ϕ * u[1] * u[3]) - (ω * u[1])
    du[2] = (ϕ * u[1] * u[3]) - (ω * u[2]) - sumTerm
    du[3] = (β * sumTerm) - (ϕ * u[1] * u[3]) - (Ω * u[3])
end

```

but when I run it I get an error:

```julia
mu = 0.03 # maximum growth rate susceptible strain
phi = 2e-8 # adsorption rate
omega = 0.05 # outflow
beta = 150 # burst size
tau = 26 # latency time
tmax = 4000.0 # time span 0-tmax
s0 = 1.3e5 # initial susceptible population
i0 = 0.0 # initial infected population
v0 = 5.3e3 # initial phage population
omega_bact = omega_phage = omega

tspan = (0.0, tmax)
u0 = [s0, i0, v0]
parms = [mu, phi, omega_bact, omega_phage, beta, tau]
prob = DDEProblem(dynVirul!, u0, tspan, parms)

ERROR: MethodError: objects of type Array{Float64,1} are not callable
Use square brackets [] for indexing an Array.
Stacktrace:
 [1] DDEProblem{false,tType,lType,lType2,isinplace,P,F,H,K} where K where H where F where P where isinplace where lType2 where lType where tType(::DDEFunction{false,typeof(dynVirul!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing}, ::Array{Float64,1}, ::Tuple{Float64,Float64}, ::Array{Float64,1}; order_discontinuity_t0::Int64, kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at /home/gigiux/.julia/packages/DiffEqBase/KnYSY/src/problems/dde_problems.jl:32
 [2] DDEProblem{false,tType,lType,lType2,isinplace,P,F,H,K} where K where H where F where P where isinplace where lType2 where lType where tType(::DDEFunction{false,typeof(dynVirul!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing}, ::Array{Float64,1}, ::Tuple{Float64,Float64}, ::Array{Float64,1}) at /home/gigiux/.julia/packages/DiffEqBase/KnYSY/src/problems/dde_problems.jl:32
 [3] DDEProblem(::DDEFunction{false,typeof(dynVirul!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing}, ::Array{Float64,1}, ::Vararg{Any,N} where N; kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at /home/gigiux/.julia/packages/DiffEqBase/KnYSY/src/problems/dde_problems.jl:44
 [4] DDEProblem(::DDEFunction{false,typeof(dynVirul!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing}, ::Array{Float64,1}, ::Vararg{Any,N} where N) at /home/gigiux/.julia/packages/DiffEqBase/KnYSY/src/problems/dde_problems.jl:44
 [5] DDEProblem(::Function, ::Array{Float64,1}, ::Vararg{Any,N} where N; kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at /home/gigiux/.julia/packages/DiffEqBase/KnYSY/src/problems/dde_problems.jl:41
 [6] DDEProblem(::Function, ::Array{Float64,1}, ::Vararg{Any,N} where N) at /home/gigiux/.julia/packages/DiffEqBase/KnYSY/src/problems/dde_problems.jl:41
 [7] top-level scope at none:0

```

What did I get wrong?  
Thanks

---

<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:** [November 8, 2020, 8:56pm UTC](https://discourse.julialang.org/t/add-sum-term-to-ode-in-differentialequations/49463/4 "2020-11-08T20:56:15Z")

</div>

That’s not the syntax for DDEProblem.

[https://diffeq.sciml.ai/stable/types/dde\_types/](https://diffeq.sciml.ai/stable/types/dde_types/)

See the tutorial: [Delay Differential Equations · DifferentialEquations.jl](https://diffeq.sciml.ai/stable/tutorials/dde_example/)

A DDEProblem requires a history function for the pre-initial condition in order to be well-defined.

---

<div class="post-metadata">

**Author:** ![Luigi\_Marongiu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/luigi_marongiu/32/7909_2.png) [@Luigi\_Marongiu](https://discourse.julialang.org/u/Luigi_Marongiu)\
**Post date:** [November 9, 2020, 9:13am UTC](https://discourse.julialang.org/t/add-sum-term-to-ode-in-differentialequations/49463/5 "2020-11-09T09:13:10Z")

</div>

Quite right! it worked with `ODEProblem`. Thank you
