SciML / DifferentialEquations.jl

Multi-language suite for high-performance solvers of differential equations and scientific machine learning (SciML) components. Ordinary differential equations (ODEs), stochastic differential equations (SDEs), delay differential equations (DDEs), differential-algebraic equations (DAEs), and more in Julia.
https://docs.sciml.ai/DiffEqDocs/stable/
Other
2.84k stars 225 forks source link

Solver fails with in-place definition of a first order ODE #466

Closed ThomasChaffey closed 5 years ago

ThomasChaffey commented 5 years ago

Solving a first order ODE (that is, scalar state variable u) with an in-place definition produces the error MethodError: no method matching similar(::Float64).

A small example is solving Example 1 from the Ordinary Differential Equations Tutorial with an in-place definition:

using DifferentialEquations
function f(du,u,p,t)  
    du = 1.01*u
end
u0=1/2
tspan = (0.0,1.0)
prob = ODEProblem(f,u0,tspan)
sol = solve(prob)

No error is produced when the ODE definition is replaced with f(u,p,t) = 1.01*u.

I am using Julia 1.1.1 on Mac OS X 10.14.3.

ChrisRackauckas commented 5 years ago
function f(du,u,p,t)  
    du = 1.01*u
end

That is not an in-place definition. There is no such thing as in-place mutation of a number since a number is a stack-allocated bitstype. Only something that is mutable, like an array, can be mutated in-place for this to make sense. So closing since there's nothing actionable here, but feel free to ask questions.

ChrisRackauckas commented 5 years ago

Maybe you'll find this tutorial helpful: http://juliadiffeq.org/DiffEqTutorials.jl/html/introduction/optimizing_diffeq_code.html

ThomasChaffey commented 5 years ago

Thanks. I read up on mutable/immutable objects in Julia and it makes sense that this isn't an in-place definition.