# Julia DifferentialEquations: increase maxiter

**URL:** <https://discourse.julialang.org/t/julia-differentialequations-increase-maxiter/50318>\
**Category:** Numerics\
**Tags:** question, package, diffeq\
**Created:** [November 17, 2020, 6:38pm UTC](https://discourse.julialang.org/t/julia-differentialequations-increase-maxiter/50318 "2020-11-17T18:38:19Z")\
**Posts on this page:** 7\
**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 17, 2020, 6:38pm UTC](https://discourse.julialang.org/t/julia-differentialequations-increase-maxiter/50318/1 "2020-11-17T18:38:19Z")

</div>

Hello,  
I have set an ODEProblem object, but the procedure is stopped (although I still get t a solution) because a higher maxiters is needed.  
How can I check what is the default value and what is the syntax to increase the value?  
Thank you

---

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [November 17, 2020, 8:23pm UTC](https://discourse.julialang.org/t/julia-differentialequations-increase-maxiter/50318/2 "2020-11-17T20:23:54Z")

</div>

[https://diffeq.sciml.ai/stable/basics/common\_solver\_opts/#Miscellaneous](https://diffeq.sciml.ai/stable/basics/common_solver_opts/#Miscellaneous)

EDIT: In my modest experience, running into maxiters may be a sign that something is wrong with your model. If it is stopping at some discontinuity you may need to do something structural like using callbacks to address a change in the model behavior at that point.

---

<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 17, 2020, 8:49pm UTC](https://discourse.julialang.org/t/julia-differentialequations-increase-maxiter/50318/3 "2020-11-17T20:49:58Z")

</div>

> [@klaff](#):
>
> EDIT: In my modest experience, running into maxiters may be a sign that something is wrong with your model. If it is stopping at some discontinuity you may need to do something structural like using callbacks to address a change in the model behavior at that point.

Yes. Or you’re using a non-stiff ODE solver on a stiff problem. Usually if you hit maxiters you’re doing something wrong. Though there are cases where you just truly need more iterations.

---

<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 17, 2020, 9:44pm UTC](https://discourse.julialang.org/t/julia-differentialequations-increase-maxiter/50318/4 "2020-11-17T21:44:03Z")

</div>

That makes sense. I have the following:

```julia
function doubleInfect!(du, u, p, t)
    μ, ν, κ, φ, ω, β, ρ, τ, η = p
    #=
    du[1] = susceptible
    du[2] = infected
    du[3] = resistant
    du[4] = phages
    =#
    TOT = u[1]+u[2]+u[3]
    du[1] = (μ * u[1]) * (1 - TOT/κ) - (φ * u[1] * u[3]) - (ω * u[1])
    du[2] = (φ * u[1] * u[3]) - (η * u[2]) - (ω * u[2])
    du[3] = ((ν * u[4]) * (1 - TOT/κ)) - (ω * u[4])
    du[4] = (β * η * u[2]) - (ρ * φ * u[1] * u[3]) - (ω * u[3])
end
mu = 0.47 # maximum growth rate susceptible (B. longum)
nu = 0.72 # maximum growth rate resistant (E. coli)
kappa = 2.2*10^7 # maximum population density
phi = 10.0^-9 # adsorption rate
psi = 10.0^-9 # adsorption rate alternative
omega = 0.05 # outflow
eta = 1.0 # lyse rate
Eta = 1.0 # lyse rate alternative
beta = 50.0 # burst size
Beta = 50.0 # burst size alternative
rho = 0.6 # reinfection rate
tau = 3.62 # latency time
tmax = 4000.0 # time span 0-tmax
s0 = 50000.0 # initial susceptible population
i0 = 1.0e-9 # initial infected population
v0 = 80.0 # initial phage population
0 = 500.0 # initial resistant population
u0 = [s0, i0, r0, v0]
parms = [mu, nu, kappa, phi, omega, beta, rho, tau, eta]
tspan = (0.0, tmax)
prob = ODEProblem(doubleInfect!, u0, tspan, parms)
soln = solve(prob, AutoVern7(Rodas5()))

```

I changed the algorithm from Tsit5() to AutoVern7(Rodas5()) and AutoTsit5(Rosenbrock23()), but I always get:

```julia
julia> prob = ODEProblem(doubleInfect!, u0, tspan, parms)
ODEProblem with uType Array{Float64,1} and tType Float64. In-place: true
timespan: (0.0, 4000.0)
u0: [50000.0, 1.0e-9, 500.0, 80.0]

julia> soln = solve(prob, AutoVern7(Rodas5()))
┌ Warning: Interrupted. Larger maxiters is needed.
└ @ DiffEqBase ~/.julia/packages/DiffEqBase/3iigH/src/integrator_interface.jl:329
retcode: MaxIters
Interpolation: automatic order switching interpolation

```

What is the error here? How can I solve it? I get a solution, but I don’t like having errors dangling around…  
Thank you

---

<div class="post-metadata">

**Author:** ![klaff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klaff/32/7637_2.png) [@klaff](https://discourse.julialang.org/u/klaff)\
**Post date:** [November 17, 2020, 10:22pm UTC](https://discourse.julialang.org/t/julia-differentialequations-increase-maxiter/50318/5 "2020-11-17T22:22:24Z")

</div>

When I run it, it seems to blow up at 492.9 seconds (time in the simulation).

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/3/d3539b77817565a4b6b329553f9768b4682b4d70.png)

---

<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 18, 2020, 4:12am UTC](https://discourse.julialang.org/t/julia-differentialequations-increase-maxiter/50318/6 "2020-11-18T04:12:17Z")

</div>

Are you sure you wrote down the equations you wanted? Your model doesn’t make too much sense.

```julia
du[3] = ((ν * u[4]) * (1 - TOT/κ)) - (ω * u[4])

```

simplifies with your values to `du[3] = 0.68*u[4]`, so if the concentration of phages is always positive, then then the number of resistant diverges to infinity. As it diverges, `TOT` goes to infinity so `(1 - TOT/κ)` is going to go negative, so you’re going to then get very large negative terms. So your ODE won’t stay positive in `u[1]` (also the `φ * u[1] * u[3]` term). Once those two are negative, u[2] goes negative and…

I assume this isn’t what you wanted… but it’s what you told the computer to do. My guess is that one of those `u[4]` values is really `u[3]`, so your equation should’ve been:

```julia
du[3] = ((ν * u[4]) * (1 - TOT/κ)) - (ω * u[3])

```

---

<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 18, 2020, 7:57am UTC](https://discourse.julialang.org/t/julia-differentialequations-increase-maxiter/50318/7 "2020-11-18T07:57:54Z")

</div>

You are right, there we some problems with the indices. The model comes from the book Weitz’s Quantitative viral ecology, and the ODEs are:  
 ![1](https://global.discourse-cdn.com/julialang/original/3X/e/9/e9afb99a0d9ec45caf0158b1777a403f97bbb7a4.jpeg)  
which I converted in:

 ![2](https://global.discourse-cdn.com/julialang/original/3X/d/2/d2a2bc37e32b4613e10a84da674a7a0bb0faae18.jpeg)  
and then I added a bacterium that is not affected by phages:  
 ![3](https://global.discourse-cdn.com/julialang/original/3X/3/0/308d17a98d51dc847f126f9d8734e4cee92b42bf.jpeg)  
hence:

```julia
function doubleInfect!(du, u, p, t)
    μ, ν, κ, φ, ω, β, τ, η = p
    #=
    du[1] = susceptible
    du[2] = infected
    du[3] = resistant
    du[4] = phages
    =#
    TOT = u[1]+u[2]+u[4]
    du[1] = (μ * u[1]) * (1 - TOT/κ) - (φ * u[1] * u[3]) - (ω * u[1])
    du[2] = (φ * u[1] * u[3]) - (η * u[2]) - (ω * u[2])
    du[3] = (β * η * u[2]) - (φ * u[1] * u[3]) - (ω * u[3])
    du[4] = (ν * u[4]) * (1 - TOT/κ) - 0 - (ω * u[4])
    
end
# parameters
mu = 0.47 # maximum growth rate susceptible (B. longum)
nu = 0.72 # maximum growth rate resistant (E. coli)
kappa = 2.2*10^7 # maximum population density
phi = 10.0^-9 # adsorption rate
psi = 10.0^-9 # adsorption rate alternative
omega = 0.05 # outflow
eta = 1.0 # lyse rate
Eta = 1.0 # lyse rate alternative
beta = 50.0 # burst size
Beta = 50.0 # burst size alternative
rho = 0.6 # reinfection rate
tau = 3.62 # latency time
tmax = 4000.0 # time span 0-tmax
s0 = 50000.0 # initial susceptible population
i0 = 0.0 # initial infected population
v0 = 80.0 # initial phage population
r0 = 0.0 # initial resistant population
u0 = [s0, i0, r0, v0]
parms = [mu, nu, kappa, phi, omega, beta, tau, eta]
tspan = (0.0, tmax)
# delay infection
condition(u, t, integrator) = t==1000 # time of inoculum
affect!(integrator) = integrator.u[3] += 500 # amount of inoculum
cb = DiscreteCallback(condition,affect!)
# implement model
prob = ODEProblem(doubleInfect!, u0, tspan, parms)
soln = solve(prob, AutoVern7(Rodas5()), callback=cb, tstops=[1000])

```

This time:

```julia
julia> prob = ODEProblem(doubleInfect!, u0, tspan, parms)
ODEProblem with uType Array{Float64,1} and tType Float64. In-place: true
timespan: (0.0, 4000.0)
u0: [50000.0, 0.0, 0.0, 80.0]

julia> soln = solve(prob, AutoVern7(Rodas5()), callback=cb, tstops=[1000])
retcode: Success
Interpolation: automatic order switching interpolation
t: 105-element Array{Float64,1}:

```

 ![Scenario_2](https://global.discourse-cdn.com/julialang/original/3X/9/5/959e002572ecd52845bfb890c797335d4b5354f1.png)

So the problem was indeed in the equations. Thank you
