I am struggling with something as basic as definition of a function for ODEProblem using DifferentialEquations package:
In Example 1 in the documentation for the package, the ODE is defined by a function on the right hand side of the ODE d/dt u(t) = f(u,p,t)
, that is
f(u,p,t) = 1.01*u
Then in the following Example 2 that deals with systems of ODEs, an in-place function is defined, namely
function lorenz(du,u,p,t)
du[1] = 10.0*(u[2]-u[1])
du[2] = u[1]*(28.0-u[3]) - u[2]
du[3] = u[1]*u[2] - (8/3)*u[3]
end
In both cases, the ODEProblem
is defined by including the name of the corresponding function among the input arguments to the ODEProblem
constructor, that is, f
and lorenz
, respectively.
In the former (scalar) case, the function is a standard (output assigning) function while in the latter (multidimensional) case it is an in-place function, isn’t it? (By the way, shouldn’t then the latter function be named lorenz!
and not just lorenz
?)
Now, what if I define the problem from Example 1 using an in-place function also in the scalar case:
using DifferentialEquations
using Plots
function f!(du,u,p,t)
du = 10.01*u
end
u0=1/2
tspan = (0.0,1.0)
prob = ODEProblem(f!,u0,tspan)
sol = solve(prob,Tsit5(),reltol=1e-8,abstol=1e-8)
plot(sol)
The above code returns an error ERROR: MethodError: no method matching similar(::Float64)
after calling solve
.
Thanks for any shared insight.