# DifferentialEquations.jl Matrix ODE error

**URL:** <https://discourse.julialang.org/t/differentialequations-jl-matrix-ode-error/42074>\
**Category:** General Usage\
**Tags:** question\
**Created:** [June 25, 2020, 10:15pm UTC](https://discourse.julialang.org/t/differentialequations-jl-matrix-ode-error/42074 "2020-06-25T22:15:54Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![teamtelefunken](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/teamtelefunken/32/16231_2.png) [@teamtelefunken](https://discourse.julialang.org/u/teamtelefunken)\
**Post date:** [June 25, 2020, 10:15pm UTC](https://discourse.julialang.org/t/differentialequations-jl-matrix-ode-error/42074/1 "2020-06-25T22:15:54Z")

</div>

Hi. I’m trying to use DifferentialEquations.jl, but I’m running into an elementary problem of setting up the ODE (i’m probably incompetent, as i’m new to Julia). I have as my test function a simple system of ODEs. I’m running Julia 1.4.2 and DifferentialEquations v6.14.0. I get as an error MethodError: no method matching \*(::Adjoint{Float64,Array{Float64,2}}, ::DiffEqBase.NullParameters). Here is my MWE:

```julia
using DifferentialEquations
using LinearAlgebra

# Parameters
K1	= [1.0 0.0; 0.0 1.0]
H1	= reshape([.16 0.0 0.0 .25 .09 0.0 0.0 .04],(2,2,2)) # CUBE
rho1 = [1.0;0.5]

# ODE inputs
tspan = (0.0,10.0)
u0 = [1.0,0.0]

# Linear function of cube multiplication
cube_multiply_dd(cube::Array{Float64,3},u) = dropdims(mapslices(i-> u'*i*u, cube; dims=[1,2]);dims=(1,2)) 
# mapslices maps the quadratic function i-> u'*i*u to each slice of cube (sliced along 3rd dimension, thus each slice is a square matrix), then dropdims kills the singleton dimensions (1,2)

# ODE 
function ODE_beta2!(du,u,t)
	du = (rho1 - K1'*u - 1/2*cube_multiply_dd(H1,u));
end

cube_multiply_dd(H1,u0) # checking that cube_multiply works
ODE_beta2!(u0,u0,0) # checking that ODE_beta2! works
prob2 = ODEProblem(ODE_beta2!,u0,tspan)
sol2 = solve(prob2)

```

In the “system of equations” documentation of DifferentialEquations.jl, the function refers to each element of du (i.e. du[1] du[2] …) directly (see Lorenz example), whereas I just write abstractly du, so maybe i’m departing from recommended syntax but i can’t seem to find where it would say not to write it in my way.

---

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [June 25, 2020, 11:29pm UTC](https://discourse.julialang.org/t/differentialequations-jl-matrix-ode-error/42074/2 "2020-06-25T23:29:59Z")

</div>

Your function needs to have an input for parameters even if you’re not using them:

`ODE_beta2!(du,u,p,t)`

---

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [June 25, 2020, 11:32pm UTC](https://discourse.julialang.org/t/differentialequations-jl-matrix-ode-error/42074/3 "2020-06-25T23:32:38Z")

</div>

Also, if the function is in place, you’ll need do use broadcasted assignment like:

`du .= (rho1 - K1'*u - 1/2*cube_multiply_dd(H1,u))`

---

<div class="post-metadata">

**Author:** ![teamtelefunken](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/teamtelefunken/32/16231_2.png) [@teamtelefunken](https://discourse.julialang.org/u/teamtelefunken)\
**Post date:** [June 26, 2020, 2:07am UTC](https://discourse.julialang.org/t/differentialequations-jl-matrix-ode-error/42074/4 "2020-06-26T02:07:17Z")

</div>

Thanks so much – that solved it!

i had written it before not using an in-place function (and thus wouldn’t have needed the broadcasting) and with an included parameter p, but i must have messed up something else.

Two further questions: (1) does the parameter p have to be in a certain position in the definition of the ODE function? (2) is my understanding correct that an in-place function is most likely faster (but needs the broadcasting operation .=)?

---

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [June 26, 2020, 3:53pm UTC](https://discourse.julialang.org/t/differentialequations-jl-matrix-ode-error/42074/5 "2020-06-26T15:53:22Z")

</div>

> [@teamtelefunken](#):
>
> does the parameter p have to be in a certain position in the definition of the ODE function?

Yes, it needs to be in the second-to-last position.

> [@teamtelefunken](#):
>
> is my understanding correct that an in-place function is most likely faster (but needs the broadcasting operation .=)?

Mostly yeah. But things like StaticArrays are really fast out-of-place for small arrays.
