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;
du = pp*u

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;
  return pp*u

(the function doesn’t make sense of course because pp isn’t always defined). This is explained in the tutorial: https://diffeq.sciml.ai/latest/tutorials/ode_example/

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

1 Like