Order of parameters in a vector differential equation function

What is the correct format for a differential equation function?

My understanding is that when the function doesn’t take any parameters, it should be like the following:

function F!(t,dx,x)
   dx.=[x[1]+x[2];x[2]];
end

and if it takes a parameter, the time variable should go at the end, something like this:

function F!(dx,x,p,t)
   dx.=[x[1]+x[2];-p*x[2]];
end

Is this correct? I eventually plan to solve this system in the typical way as follows (for the non-parameterized function):

prob = ODEProblem(F!,[0,0],(0,5))
solve(prob)

I’ve never seen a version where no parameter is present in the function definition. Maybe there is one, but personally, i would just define

function F!(dx,x,p,t)
   dx.=[x[1]+x[2];x[2]];
end

And simply not use p.
That way if you ever need to use params which happens a lot, it’s straight forward to extend.
When solving this, you need to pass the solver the params. Here you can put in anything since the function doesn’t do anything with it but I would probably pass nothing.

Edit: note that If performance matters, you should not allocate a small array on the right hand side the way you do, but probably rather use a tuple or similar.

2 Likes

No, the time argument always comes last and the parameters always comes after the state variables x. This signature: F!(t,dx,x) does not exist, where have you seen that?

2 Likes

I was just trying it myself, but yes thanks for letting me know that function signature doesn’t exist. I still don’t know how to define a function without using a parameter though. F!(dx,x,t) doesn’t work for me, it gives an error, but I do use F!(dx,x,p,t) and then not use p in the function, but that seems like a workaround than a proper way.

Thanks for your reply! :smiley:

Thanks! I have been doing that since it’s the one which works for me but seems like a workaround. Is it not possible to define a differential equation function simply like F!(dx,x,t)?

Thanks for the performance suggestion! :slight_smile:

My understanding is that the parameter vector needs to be passed into the function regardless if it is actually used or not; I have never seen a working DifferentialEquations.jl (or anything SciML, for that matter) example without a parameter vector being passed in (similarly to @salmon). I believe that this is a design choice specifically by the SciML team to make their packages all operate in a similar way, with similar syntax. It’s not required to pass anything into the problem constructor for the parameter vector (i.e., to prob = ODEProblem(... as can be seen from this example.

TL;DR: I believe it’s a design choice that p is included, but it’s been made so that if it isn’t used, it has minimal impact on model definition.

4 Likes

Oh alright makes sense. Thanks a lot for your answer! :smiley:

1 Like