BoundsError trying to solve DDEProblem

function f(du,u,h,p,t)
k1,k2,tau=p
hist=h(p,t-tau)[0]
du[0]=u0
end
h(p,t)=ones(1)
tau=7
lags=[tau]
k1=0.5;k2=0.1
p=k1,k2,tau
span=(0.0,10.0)
u0=[1.0]
prob = DDEProblem(f,u0,h,span,p; constant_lags=lags)
sol=solve(prob)
plot(sol)
BoundsError: attempt to access 1-element Vector{Float64} at index [0]
Stacktrace:

I’m guessing this line is the problem since Julia is 1-based indexed.

2 Likes

then what should i do

Replace the zeros with ones…

2 Likes

You can change the indexing so that arrays start at zero, but this can cause problems as can be seen in a recent thread - far too much library code assumes that arrays start at index one.

The style with which you write code is not best practice. Because Julia has an ‘end’ keyword you don’t need to indent the code as with Python, nevertheless it is a good thing to do. It makes is easier to see how the code execution will flow.

using DifferentialEquations, Plots
function f(du, u, h, p, t)
:v: = u
k1, k2, τ = p
:hourglass: = h(p, t - τ,idxs=1)
du[1] = d​:v: = :v:(1 - :v: - k1 * :v: - k2)

end

u0 = 1.0
τ = 7
h(p, t,idxs=1) = 1.0
p = 0.7, 0.5, 7
tspan = (0.0, 10.0)
prob = DDEProblem(f, u0, h, p, canstant_lag = [τ])
as a message error
MethodError: no method matching promote_tspan(::Tuple{Float64, Float64, Int64})

Closest candidates are:
promote_tspan(::Any, !Matched::Any, !Matched::Any, !Matched::Any, !Matched::Any) at C:\Users\ACHRAF ELFADLI.julia\packages\DiffEqBase\foWeO\src\init.jl:3
promote_tspan(!Matched::Tuple{T, S}) where {T, S} at C:\Users\ACHRAF ELFADLI.julia\packages\SciMLBase\r3XzZ\src\problems\problem_utils.jl:7
promote_tspan(!Matched::Number) at C:\Users\ACHRAF ELFADLI.julia\packages\SciMLBase\r3XzZ\src\problems\problem_utils.jl:8

in eval at base\boot.jl:373

in top-level scope at untitled:16

in at SciMLBase\r3XzZ\src\problems\dde_problems.jl:240

in var"#DDEProblem#301" at SciMLBase\r3XzZ\src\problems\dde_problems.jl:240

in at SciMLBase\r3XzZ\src\problems\dde_problems.jl:243

in var"#DDEProblem#302" at SciMLBase\r3XzZ\src\problems\dde_problems.jl:243

in at SciMLBase\r3XzZ\src\problems\dde_problems.jl:222

in at SciMLBase\r3XzZ\src\problems\dde_problems.jl:222

in at SciMLBase\r3XzZ\src\problems\dde_problems.jl:222

it still don’t working

It says your tspan is wrong because you didn’t pass it to the problem constructor.

prob = DDEProblem(f, u0, h, tspan, p, canstant_lag = [τ])