ODE: no method matching similar

Hi All,

I try to compile an ODE model of 1 variable u but I need an if statement based on t.

like something:

function dudt!(du,u,p,t)
if rem(t,10) < 5
pp = p[1]*t;
end
du = pp*u
end

But I get the following message:

MethodError: no method matching similar(::Float64)

Do you have any ideas?
Many thanks!

Not sure if this will fix it because I don’t have the whole picture, but change du = pp*u to du .= pp*u for updating du in-place.

Thanks for the insight. That doesn’t work.
I solved the problem by adding a second variable that does not evolve during the time … but that is creepy:

du[1] = pp*u[1]
du[2] = 0 

if you have better ideas! :slight_smile:

Was your initial condition a scalar? If it’s a scalar, why are you using the in-place form?

Yes it is a scalar. I am new to Julia so I don’t really understand what is the in-place form?

(du,u,p,t) is in-place, modifying du. If you want to use scalars, use out-of-place:

function dudt(u,p,t)
  if rem(t,10) < 5
    pp = p[1]*t;
  end
  return pp*u
end

(the function doesn’t make sense of course because pp isn’t always defined). This is explained in the tutorial: Ordinary Differential Equations · DifferentialEquations.jl

1 Like

Many thanks @ChrisRackauckas! It is working super well now. I understand the in-place now.

1 Like