# Solving a system of matrix differential equations using DifferentialEquations.jl

**URL:** <https://discourse.julialang.org/t/solving-a-system-of-matrix-differential-equations-using-differentialequations-jl/107663>\
**Category:** General Usage\
**Tags:** question, package\
**Created:** [December 15, 2023, 1:07pm UTC](https://discourse.julialang.org/t/solving-a-system-of-matrix-differential-equations-using-differentialequations-jl/107663 "2023-12-15T13:07:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aleksandr\_Mikheev](https://avatars.discourse-cdn.com/v4/letter/a/b4bc9f/32.png) [@Aleksandr\_Mikheev](https://discourse.julialang.org/u/Aleksandr_Mikheev)\
**Post date:** [December 15, 2023, 1:07pm UTC](https://discourse.julialang.org/t/solving-a-system-of-matrix-differential-equations-using-differentialequations-jl/107663/1 "2023-12-15T13:07:25Z")

</div>

How shall I initialize my initial condition for the system of matrix differential equations using DifferentialEquations.jl? As a simple example, suppose I want to solve the following trivial system of equations:

```julia
function g!(du,u,p,t)
   du[1] .= u[1] + ones(3,3)
   du[2] .= ones(3,3)
end

```

If I then define my problem as

```julia
u0 = [zeros(3,3),zeros(3,3)]
prob = ODEProblem(g!,u0,(0,1.0))

```

and try to solve it `solve(prob,Tsit5())`, I get the error:

> ERROR: MethodError: no method matching zero(::Type{Matrix{Float64}})  
> Closest candidates are:  
> zero(::Union{Type{P}, P}) where P\<:Dates.Period at C:\ …\Julia-1.8.5\share\julia\stdlib\v1.8\Dates\src\periods.jl:53  
> zero(::AbstractIrrational) at irrationals.jl:150  
> zero(::FillArrays.AbstractOnes{T, N}) where {T, N} at C:\ ….julia\packages\FillArrays\sIOcj\src\FillArrays.jl:605  
> …

I also tried to pass my initial condition as a tuple of matrices as well as as a static array with an out-of-place definition of the problem, but nothing worked. Are systems of matrix equations supported by the package, and if so, how do I correctly define the problem?

---

<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:** [December 15, 2023, 1:33pm UTC](https://discourse.julialang.org/t/solving-a-system-of-matrix-differential-equations-using-differentialequations-jl/107663/2 "2023-12-15T13:33:48Z")

</div>

Your state array has to be an abstractArray containing numbers, yours is an array of arrays.

You can either include your two matrices into a higherdimensional array `u0 = zeros(2,3,3)` , or you use more specialized types such as from RecursiveArrayTools.jl

---

<div class="post-metadata">

**Author:** ![Aleksandr\_Mikheev](https://avatars.discourse-cdn.com/v4/letter/a/b4bc9f/32.png) [@Aleksandr\_Mikheev](https://discourse.julialang.org/u/Aleksandr_Mikheev)\
**Post date:** [December 15, 2023, 1:48pm UTC](https://discourse.julialang.org/t/solving-a-system-of-matrix-differential-equations-using-differentialequations-jl/107663/3 "2023-12-15T13:48:35Z")

</div>

How could I not think of that… So this is what I ended up with

```julia
function g!(du,u,p,t)
   du[1,:,:] = u[1,:,:] + ones(3,3)
   du[2,:,:] = ones(3,3)
end

```

which seems to do the trick.

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [December 16, 2023, 3:34am UTC](https://discourse.julialang.org/t/solving-a-system-of-matrix-differential-equations-using-differentialequations-jl/107663/4 "2023-12-16T03:34:17Z")

</div>

See the new error message:

ERROR: Non-Number element type inside of an `Array` detected.  
Arrays with non-number element types, such as  
`Array{Array{Float64}}`, are not supported by the  
solvers.

If you are trying to use an array of arrays structure,  
look at the tools in RecursiveArrayTools.jl. For example:

If this was a mistake, promote the element types to be  
all the same. If this was intentional, for example,  
using Unitful.jl with different unit values, then use  
an array type which has fast broadcast support for  
heterogeneous values such as the ArrayPartition  
from RecursiveArrayTools.jl. For example:

```julia
using RecursiveArrayTools
u0 = ArrayPartition([1.0,2.0],[3.0,4.0])
u0 = VectorOfArray([1.0,2.0],[3.0,4.0])

```

are both initial conditions which would be compatible with  
the solvers.

Element type:  
Matrix{Float64}

---

<div class="post-metadata">

**Author:** ![Aleksandr\_Mikheev](https://avatars.discourse-cdn.com/v4/letter/a/b4bc9f/32.png) [@Aleksandr\_Mikheev](https://discourse.julialang.org/u/Aleksandr_Mikheev)\
**Post date:** [December 16, 2023, 7:32pm UTC](https://discourse.julialang.org/t/solving-a-system-of-matrix-differential-equations-using-differentialequations-jl/107663/5 "2023-12-16T19:32:41Z")

</div>

Thanks! Is using RecusiveArrayTools.jl preferable compared to using higher-dimensional arrays?

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 16, 2023, 7:56pm UTC](https://discourse.julialang.org/t/solving-a-system-of-matrix-differential-equations-using-differentialequations-jl/107663/6 "2023-12-16T19:56:11Z")

</div>

no. using a higher dimensions array is preferred for fixed sizes.
