# Putting linear ODESystem in matrix-vector form

**URL:** <https://discourse.julialang.org/t/putting-linear-odesystem-in-matrix-vector-form/113333>\
**Category:** Modelling & Simulations\
**Tags:** modelingtoolkit, symbolics\
**Created:** [April 22, 2024, 12:49pm UTC](https://discourse.julialang.org/t/putting-linear-odesystem-in-matrix-vector-form/113333 "2024-04-22T12:49:37Z")\
**Posts on this page:** 7\
**Page:** 2

<div class="post-metadata">

**Author:** ![langestefan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/langestefan/32/207923_2.png) [@langestefan](https://discourse.julialang.org/u/langestefan)\
**Post date:** [April 23, 2024, 12:15pm UTC](https://discourse.julialang.org/t/putting-linear-odesystem-in-matrix-vector-form/113333/21 "2024-04-23T12:15:17Z")

</div>

Hi, sorry to bother you again. I have noticed that the matrices returned by `linearize_symbolic` don’t follow the order of the output variables specified as:

```julia
ss_matrices, ssys = ModelingToolkit.linearize_symbolic(model, [Ta.V.u, Φhp_c.I.u, Φcv_c.I.u], [Ce.v, Ci.v, Ch.v])

```

Will give the exact same `ss_matrices.A` as for example:

```julia
ss_matrices, ssys = ModelingToolkit.linearize_symbolic(model, [Ta.V.u, Φhp_c.I.u, Φcv_c.I.u], [Ci.v, Ch.v, Ce.v])

```

Where I changed the order of `[Ce.v, Ci.v, Ch.v]` to `[Ci.v, Ch.v, Ce.v]`.

Interestingly the matrix `ss_matrices.B` does follow the order of the input vector `[Ta.V.u, Φhp_c.I.u, Φcv_c.I.u]`.

Is there a way to figure out what order of variables it used? (by matching some parameters to the analytical result I know what order it used but that’s not super convenient for larger equations)

---

<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:** [April 23, 2024, 12:23pm UTC](https://discourse.julialang.org/t/putting-linear-odesystem-in-matrix-vector-form/113333/22 "2024-04-23T12:23:40Z")

</div>

It’s expected that the A matrix remains the same, what you expect will change is the C matrix that gives the output, recall

\begin{aligned} \dot x &= Ax + Bu\\ y &= Cx + Du \end{aligned}

where x is the state and y are the outputs.

I expect the two C matrices will differ by a permutation.

---

<div class="post-metadata">

**Author:** ![langestefan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/langestefan/32/207923_2.png) [@langestefan](https://discourse.julialang.org/u/langestefan)\
**Post date:** [April 23, 2024, 1:50pm UTC](https://discourse.julialang.org/t/putting-linear-odesystem-in-matrix-vector-form/113333/23 "2024-04-23T13:50:08Z")

</div>

That’s not what I would expect. If I change the order of [x\_1, x\_2, x\_3] to [x\_2, x\_1, x\_3] then A should have column 1 swapped for column 2.

I checked the matrix C and it was the identity matrix for both options (or any option x really).

---

<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:** [April 23, 2024, 1:54pm UTC](https://discourse.julialang.org/t/putting-linear-odesystem-in-matrix-vector-form/113333/24 "2024-04-23T13:54:32Z")

</div>

You are not specifying the state x when you call `linearize`, you are specifying the outputs y. The state x is picked by the MTK model compiler and its size is independent on the number of outputs you pick. Try picking zero outputs (`[]`) and you’ll still get the same A matrix, but a C matrix of size 0 \times 3

I just tried swapping the output order and I get this

```julia
julia> ss_matrices.C
3×3 Matrix{Num}:
 1 0 0
 0 1 0
 0 0 1

# swap order

julia> ss_matrices.C
3×3 Matrix{Num}:
 1 0 0
 0 0 1
 0 1 0
```

---

<div class="post-metadata">

**Author:** ![langestefan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/langestefan/32/207923_2.png) [@langestefan](https://discourse.julialang.org/u/langestefan)\
**Post date:** [April 23, 2024, 2:06pm UTC](https://discourse.julialang.org/t/putting-linear-odesystem-in-matrix-vector-form/113333/25 "2024-04-23T14:06:45Z")

</div>

Ah okay that makes sense, thanks. But then my question would be, is there a way to figure out what x the compiler picked?

---

<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:** [April 23, 2024, 2:09pm UTC](https://discourse.julialang.org/t/putting-linear-odesystem-in-matrix-vector-form/113333/26 "2024-04-23T14:09:52Z")

</div>

There is, that’s why `simplified_sys` is returned 🙂 :

```julia
julia> unknowns(ssys)
3-element Vector{SymbolicUtils.BasicSymbolic{Real}}:
 Ci₊v(t)
 Ce₊v(t)
 Ch₊v(t)

```

---

<div class="post-metadata">

**Author:** ![langestefan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/langestefan/32/207923_2.png) [@langestefan](https://discourse.julialang.org/u/langestefan)\
**Post date:** [April 23, 2024, 2:18pm UTC](https://discourse.julialang.org/t/putting-linear-odesystem-in-matrix-vector-form/113333/27 "2024-04-23T14:18:37Z")

</div>

Ah and ofcourse I realize now I could have also gotten that from C. Thanks again…

[Previous page](https://discourse.julialang.org/t/putting-linear-odesystem-in-matrix-vector-form/113333.md?page=1)
