# State Dependent Mass Matrix

**URL:** <https://discourse.julialang.org/t/state-dependent-mass-matrix/40238>\
**Category:** General Usage\
**Tags:** diffeq\
**Created:** [May 27, 2020, 7:27am UTC](https://discourse.julialang.org/t/state-dependent-mass-matrix/40238 "2020-05-27T07:27:03Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![321bwa](https://avatars.discourse-cdn.com/v4/letter/3/e56c9b/32.png) [@321bwa](https://discourse.julialang.org/u/321bwa)\
**Post date:** [May 27, 2020, 7:27am UTC](https://discourse.julialang.org/t/state-dependent-mass-matrix/40238/1 "2020-05-27T07:27:03Z")

</div>

Hello,

I’ve looked at the documentation for Handling Mass Matrices for DifferentialEquations.jl. In the example was a singular matrix. I would like to know how to implement a state dependent mass matrix, where the matrix M in equation  
Mu’ =f(u,p,t)  
has the dependent variables in the matrix itself.

The image below is the equation I’m trying to replicate:

 ![Capture](https://global.discourse-cdn.com/julialang/original/3X/f/8/f80491fe87ec7271ba54bab4404ebfd7ae6ea438.png)

I’m trying to integrate from Matlab to Julia for this type of problem. I thought replacing the mass\_matrix option with a function would work but it produces the method error MethodError: no method matching issingular(::typeof(_ **function goes here** _))

An example or helpful tip would be appreciated. The link relating to mass matrices is [https://docs.sciml.ai/latest/tutorials/advanced\_ode\_example/#Handling-Mass-Matrices-1](https://docs.sciml.ai/latest/tutorials/advanced_ode_example/#Handling-Mass-Matrices-1) Thank you!

---

<div class="post-metadata">

**Author:** ![Julius\_Martensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/julius_martensen/32/17077_2.png) [@Julius\_Martensen](https://discourse.julialang.org/u/Julius_Martensen)\
**Post date:** [May 27, 2020, 7:32am UTC](https://discourse.julialang.org/t/state-dependent-mass-matrix/40238/2 "2020-05-27T07:32:04Z")

</div>

I think this is not implemented right now ( but will be, I am sure ).

I planned on doing this after a discussion with @ChrisRackauckas and @YingboMa on Slack, but never found the time.

If I encounter such problems right now, I tend to use something along the lines of

```julia
function dudt(u, p, t)
    m = M(u,p,t)
    f = F(u,p,t)
    inv(m)*f
end

```

or solve it manually, if possible.

---

<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:** [May 27, 2020, 7:39am UTC](https://discourse.julialang.org/t/state-dependent-mass-matrix/40238/3 "2020-05-27T07:39:09Z")

</div>

> [@Julius\_Martensen](#):
>
> I think this is not implemented right now ( but will be, I am sure ).

It’s implemented. Examples:

> <https://github.com/SciML/OrdinaryDiffEq.jl/blob/master/test/interface/mass_matrix_tests.jl>

We just need to clean up our documentation of DiffEqOperators to make it more clear how to define dependent DiffEqOperators, since that’s all it is (the mass matrix can be any DiffEqOperator)

---

<div class="post-metadata">

**Author:** ![Julius\_Martensen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/julius_martensen/32/17077_2.png) [@Julius\_Martensen](https://discourse.julialang.org/u/Julius_Martensen)\
**Post date:** [May 27, 2020, 7:42am UTC](https://discourse.julialang.org/t/state-dependent-mass-matrix/40238/4 "2020-05-27T07:42:37Z")

</div>

Ah, I did not know that 👍

---

<div class="post-metadata">

**Author:** ![321bwa](https://avatars.discourse-cdn.com/v4/letter/3/e56c9b/32.png) [@321bwa](https://discourse.julialang.org/u/321bwa)\
**Post date:** [May 27, 2020, 3:03pm UTC](https://discourse.julialang.org/t/state-dependent-mass-matrix/40238/5 "2020-05-27T15:03:58Z")

</div>

Thank you for that. I’m still new to Julia, could you link me to the equation your code snippet is solving to help visualize it for me? I can try to figure out the syntax from there.

---

<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:** [May 27, 2020, 3:07pm UTC](https://discourse.julialang.org/t/state-dependent-mass-matrix/40238/6 "2020-05-27T15:07:51Z")

</div>

I am really really slow with making matrices in LaTeX, so does this simplification help?

```julia
function update_func(A,u,p,t)
    A[1,1] = cos(t)
    A[2,1] = sin(t)*u[1]
    A[3,1] = t^2
    A[1,2] = cos(t)*sin(t)
    A[2,2] = cos(t)^2 + u[3]
    A[3,2] = sin(t)*u[2]
    A[1,3] = sin(t)
    A[2,3] = t^2
    A[3,3] = t*cos(t) + 1
end
M = DiffEqArrayOperator(ones(3,3),update_func=update_func)
u0 = ones(3)
tspan = (0.0, 1.0)
f(du,u,p,t) = (@. du = u + t; nothing)
prob = ODEProblem(ODEFunction(f, mass_matrix=M), u0, tspan)
sol = solve(prob,RadauIIA5())

```

The mass matrix is just the `A` defined in the update\_func. I’ll trade you for LaTeX and together we can get this in the docs 🙂

---

<div class="post-metadata">

**Author:** ![MantasJ](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mantasj/32/23529_2.png) [@MantasJ](https://discourse.julialang.org/u/MantasJ)\
**Post date:** [April 1, 2021, 2:01pm UTC](https://discourse.julialang.org/t/state-dependent-mass-matrix/40238/7 "2021-04-01T14:01:04Z")

</div>

Has the solution to this problem updated? DifferentialEquations.jl documentation says that state dependent mass matrices are “not directly supported” and there is no documentation in DiffEqOperators.jl either. Would the current implementation support Complex variables? @ChrisRackauckas @Julius_Martensen

---

<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:** [April 17, 2021, 10:50am UTC](https://discourse.julialang.org/t/state-dependent-mass-matrix/40238/8 "2021-04-17T10:50:46Z")

</div>

State-dependent mass matrices are inherently pretty unstable, so I would recommend a DAE formulation instead.

---

<div class="post-metadata">

**Author:** ![Maucejo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maucejo/32/39090_2.png) [@Maucejo](https://discourse.julialang.org/u/Maucejo)\
**Post date:** [November 26, 2024, 5:49pm UTC](https://discourse.julialang.org/t/state-dependent-mass-matrix/40238/9 "2024-11-26T17:49:44Z")

</div>

Sorry for making revive this thread.

I have a question of curiosity. What are the reasons that may prevent from adding a mass matrix as a parameter of a solver ? Is it for a technical reason ?

Currently, I include the mass matrix in the definition of the problem using `du = M\u`. I am happy with that, but I notice that Matlab allows for the definition of a mass matrix via `odeset` for a couple of its solvers. I guess that in this case some numerical strategies are implemented to solve the problem efficiently (but I may be wrong).

Thank you for helping me to fill my ingnorance on this topic.

---

<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:** [November 26, 2024, 5:52pm UTC](https://discourse.julialang.org/t/state-dependent-mass-matrix/40238/10 "2024-11-26T17:52:29Z")

</div>

> [@Maucejo](#):
>
> Currently, I include the mass matrix in the definition of the problem using `du = M\u`. I am happy with that, but I notice that Matlab allows for the definition of a mass matrix via `odeset` for a couple of its solvers. I guess that in this case some numerical strategies are implemented to solve the problem efficiently (but I may be wrong).

Your setup does not work when `M` is singular (i.e. a DAE).

> **[Differential Algebraic Equations · DifferentialEquations.jl](https://docs.sciml.ai/DiffEqDocs/stable/tutorials/dae_example/)**
>
> Documentation for DifferentialEquations.jl.

Also you can specialize the nonlinear solver so the `M` part is effectively free.
