No method matching getindex() using ODEProblem

Hey there,

I am very new to Julia and I am working on an ODE Problem. Basically this is my set-up:

etaspan = (0.0, 1.0)
F0 = [1, -1e+10, q_, 0] 

function eventfcn(vcondition, value, eta, integrator)
    out[1] = (qmax-F(3))   
    out[2] = F(2)
    out[3] = F(4)

function affect!(integrator, idx)
    if (idx == 1 | idx == 2 | idx == 3)
      affect!(integrator) = terminate!(integrator)

cb = VectorContinuousCallback(eventfcn,affect!,3)    

# odefun(eta,f) will be a vector containing 4 partial derivatives (ODEs), that I
# calculate before with the function fnct
odefun(eta, f) = fnct(eta, f, r, rho, a, a_, delta, delta_, sigma)[1]

# dyn will be a vector containing all dynamic parameters. I also get them through function fnct
dyn(eta, f) = fnct(eta, f, r, rho, a, a_, delta, delta_, sigma)[2]

QL = 0
QR = 1e+15

for iter = 1:50
    F0[4] = (QL+QR)/2
    prob = ODEProblem(odefun, F0, etaspan, dyn)
    sol = solve(prob, Tsit5(), callback = cb, RelTol = 1e-08, abstol = 1e-10)

However, when trying to run all of this, I get the following error message:

MethodError: no method matching getindex(::typeof(dyn), ::Int64)

As far as I understood, the ODEProblem will at one point call function getindex, but I don’t know why it doesn’t work. Any help would be highly appreciated! <3

That’s not a vector, it’s a function.

Hey Chris. It’s actually a vector. The function “fnct” gives me 2 vectors as an output. dyn will be the second of the 2. I just didn’t want to include the “fnct” function cause it would be too long and it’s not necessary for the purpose here…

No, dyn is function that takes two arguments eta and f. This is how you define it.

Are you trying to have time-dependent parameters? If so, you should shift the time dependence into odefun

No, it’s not a vector. If you wanted a vector you would write:

dyn = fnct(eta, f, r, rho, a, a_, delta, delta_, sigma)[2]

and that’s exactly your error.

Hey @ChrisRackauckas and @skleinbo. Thank you very much for your answers.

Indeed, the getindex() error message disappeared once I defined dyn as a vector. But from that I got some other problems and they related to what you mentioned @skleinbo. Function fnct(eta, f, …) does 2 things. It calculates fp, the vector of derivatives and it will update the parameters at every iteration, which is what you @skleinbo were referring to (just that in my model, everything evolves with eta rather than with time).

What I can’t figure out is how to integrate these dynamic parameters in my model. This is what happened when I defined dyn as a global inside the fnct function (instead of returning it):

MethodError: no method matching +(::Float64, ::Vector{Float64}) 
For element-wise addition, use broadcasting with dot syntax: scalar .+ array

Thanks again !

a .+ b when it’s between a Float64 and a Vector{Float64} to do it element-wise.