# MTK and Jacobian

**URL:** <https://discourse.julialang.org/t/mtk-and-jacobian/96075>\
**Category:** Modelling & Simulations\
**Created:** [March 14, 2023, 6:20pm UTC](https://discourse.julialang.org/t/mtk-and-jacobian/96075 "2023-03-14T18:20:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [March 14, 2023, 6:20pm UTC](https://discourse.julialang.org/t/mtk-and-jacobian/96075/1 "2023-03-14T18:20:50Z")

</div>

I have an unbalanced/partial model consisting of 2 differential equations, 11 algebraic equations, and 17 unknowns/states. I want to compute the Jacobian of the model.

I have 3 questions regarding the `calculate_jacobian` method, see at the bottom.

Here is the model (just to show the structure), named `mod_p`:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/6/a6484dd602db55695256ceb022159b71b58f718b.png)

…and here are the 17 unknowns:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/a/8/a82d284e20035907a15e7eb08afcdb60d936fad0.png)

I then calculate the Jacobian of the partial model:

```julia
J = calculate_jacobian(mod_p)

```

**Questions** :

1. When computing J, are the equations preserved in exactly the order shown above (listing of equations for `mod_p`)?
2. The algebraic equations: I assume all terms are moved to one side of the equation before taking the Jacobian, i.e., that one has algebraic equations 0=g(x)?
3. When computing J, are the unknowns preserved in exactly the order given by `states(mod_p)`?
4. If the answer to any of questions 1 & 3 is _no_, is there a way to find the orders used by `calculate_jacobian`?

---

<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:** [March 14, 2023, 8:09pm UTC](https://discourse.julialang.org/t/mtk-and-jacobian/96075/2 "2023-03-14T20:09:16Z")

</div>

> [@BLI](#):
>
> When computing J, are the equations preserved in exactly the order shown above (listing of equations for `mod_p`)?

They are computed in the order of `states(sys)`. This ordering can change due to simplifications.

> [@BLI](#):
>
> The algebraic equations: I assume all terms are moved to one side of the equation before taking the Jacobian, i.e., that one has algebraic equations 0=g(x)0=g(x)0=g(x)?

yes

> [@BLI](#):
>
> - When computing J, are the unknowns preserved in exactly the order given by `states(mod_p)`?

yes

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [March 14, 2023, 8:22pm UTC](https://discourse.julialang.org/t/mtk-and-jacobian/96075/3 "2023-03-14T20:22:24Z")

</div>

> [@ChrisRackauckas](#):
>
> They are computed in the order of `states(sys)`.

Hm. I need to keep track of the order of the equations.

If I have two expressions (terms?) and an equation `expr1 ~ expr2`, I can extract the Left Hand Side and the Right Hand Side by

```julia
@variables expr1 expr2
(exrp1 ~ expr2).lhs
(expr1 ~ expr2).rhs

```

Is there a _function_ to do the same, or should I use comprehension if I want to do this for a vector of equations?

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [March 14, 2023, 8:40pm UTC](https://discourse.julialang.org/t/mtk-and-jacobian/96075/4 "2023-03-14T20:40:09Z")

</div>

OK. I extracted the RHS of equations using field`.rhs` etc., and created a vector of equations and a vector of variables (states).

Then I used `Symbolics.jacobian(rhs,states(sys_p))`. The advantage of this is that both `rhs` and `states(sys_p)` are vectors, so I know precisely which order the content of each vector has, and therefore I can check each element of the Jacobian.

The result _seems_ to be the same as when using `ModelingToolkit.calculate_jacobian(sys_p)`. I don’t know if this will _always_ be the case, though.

Final **question** :

- If I build a model `sys = ODESystem(eqs)` where `eqs` is a vector of equations, is there a way to _extract_ the equations `eqs` from `sys` and get them in exactly the same order as in `eqs`?
- Will function `equations` do this, or is there a chance that this function will change the order or change the terms in the equations?

---

<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:** [March 18, 2023, 3:57am UTC](https://discourse.julialang.org/t/mtk-and-jacobian/96075/5 "2023-03-18T03:57:38Z")

</div>

> [@BLI](#):
>
> - If I build a model `sys = ODESystem(eqs)` where `eqs` is a vector of equations, is there a way to _extract_ the equations `eqs` from `sys` and get them in exactly the same order as in `eqs`?

Not necessarily. There won’t even necessarily be the same number of equations, or the same equations, so it’s not a well-defined question.

> [@BLI](#):
>
> - Will function `equations` do this, or is there a chance that this function will change the order or change the terms in the equations?

It will give you the current equations within the system, which could be simplified in many ways.

---

<div class="post-metadata">

**Author:** ![BLI](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bli/32/37206_2.png) [@BLI](https://discourse.julialang.org/u/BLI)\
**Post date:** [March 23, 2023, 11:22am UTC](https://discourse.julialang.org/t/mtk-and-jacobian/96075/6 "2023-03-23T11:22:14Z")

</div>

> [@ChrisRackauckas](#):
>
> Not necessarily. There won’t even necessarily be the same number of equations, or the same equations, so it’s not a well-defined question.

In one case, we put the differential equations at the bottom of `eqs_p`. Then `eqs_p |> ODESystem |> equations` had them reordered with the differential equations on top. Also, the order of variables found from `get_variables(eqs_p)` hand been altered in `eqs_p |> ODESystem |> states` with the differential variables on top.

Anyways, I can confirm that converting `eqs_p` into an `ODESystem` may change order of equations and variables. And as you (CR) say: it can even eliminate variables/equations.
