# Order of parameters in a vector differential equation function

**URL:** <https://discourse.julialang.org/t/order-of-parameters-in-a-vector-differential-equation-function/95114>\
**Category:** Numerics\
**Tags:** diffeq, differentialequation\
**Created:** [February 24, 2023, 5:32am UTC](https://discourse.julialang.org/t/order-of-parameters-in-a-vector-differential-equation-function/95114 "2023-02-24T05:32:09Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![shaurya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shaurya/32/30468_2.png) [@shaurya](https://discourse.julialang.org/u/shaurya)\
**Post date:** [February 24, 2023, 5:32am UTC](https://discourse.julialang.org/t/order-of-parameters-in-a-vector-differential-equation-function/95114/1 "2023-02-24T05:32:10Z")

</div>

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:

```julia
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:

```julia
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):

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

```

---

<div class="post-metadata">

**Author:** ![Salmon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/salmon/32/22968_2.png) [@Salmon](https://discourse.julialang.org/u/Salmon)\
**Post date:** [February 24, 2023, 8:36am UTC](https://discourse.julialang.org/t/order-of-parameters-in-a-vector-differential-equation-function/95114/2 "2023-02-24T08:36:10Z")

</div>

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

```julia
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.

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [February 24, 2023, 9:10am UTC](https://discourse.julialang.org/t/order-of-parameters-in-a-vector-differential-equation-function/95114/3 "2023-02-24T09:10:47Z")

</div>

> [@shaurya](#):
>
> Is this correct?

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?

---

<div class="post-metadata">

**Author:** ![shaurya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shaurya/32/30468_2.png) [@shaurya](https://discourse.julialang.org/u/shaurya)\
**Post date:** [February 24, 2023, 9:04pm UTC](https://discourse.julialang.org/t/order-of-parameters-in-a-vector-differential-equation-function/95114/4 "2023-02-24T21:04:40Z")

</div>

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! 😃

---

<div class="post-metadata">

**Author:** ![shaurya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shaurya/32/30468_2.png) [@shaurya](https://discourse.julialang.org/u/shaurya)\
**Post date:** [February 24, 2023, 9:06pm UTC](https://discourse.julialang.org/t/order-of-parameters-in-a-vector-differential-equation-function/95114/5 "2023-02-24T21:06:22Z")

</div>

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! 🙂

---

<div class="post-metadata">

**Author:** ![liamfdoherty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/liamfdoherty/32/24516_2.png) [@liamfdoherty](https://discourse.julialang.org/u/liamfdoherty)\
**Post date:** [February 25, 2023, 12:58am UTC](https://discourse.julialang.org/t/order-of-parameters-in-a-vector-differential-equation-function/95114/6 "2023-02-25T00:58:35Z")

</div>

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](https://docs.sciml.ai/DiffEqDocs/stable/examples/classical_physics/#First-order-linear-ODE).

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.

---

<div class="post-metadata">

**Author:** ![shaurya](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shaurya/32/30468_2.png) [@shaurya](https://discourse.julialang.org/u/shaurya)\
**Post date:** [February 25, 2023, 1:02am UTC](https://discourse.julialang.org/t/order-of-parameters-in-a-vector-differential-equation-function/95114/7 "2023-02-25T01:02:22Z")

</div>

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