99dB
June 4, 2020, 8:55pm
1
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.
99dB
June 4, 2020, 9:14pm
3
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!
Was your initial condition a scalar? If it’s a scalar, why are you using the in-place form?
99dB
June 4, 2020, 9:42pm
5
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
99dB
June 4, 2020, 9:57pm
7
Many thanks @ChrisRackauckas ! It is working super well now. I understand the in-place now.
1 Like