# ModelingToolkit.jl state-space representations -- what is the recommendation?

**URL:** https://discourse.julialang.org/t/modelingtoolkit-jl-state-space-representations-what-is-the-recommendation/102212
**Category:** New to Julia
**Tags:** modelingtoolkit
**Created:** [July 28, 2023, 7:54pm UTC](https://discourse.julialang.org/t/modelingtoolkit-jl-state-space-representations-what-is-the-recommendation/102212 "2023-07-28T19:54:22Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Andrew\_Liu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrew_liu/32/14282_2.png) [@Andrew\_Liu](https://discourse.julialang.org/u/Andrew_Liu)
#### Post date: [July 28, 2023, 7:54pm UTC](https://discourse.julialang.org/t/modelingtoolkit-jl-state-space-representations-what-is-the-recommendation/102212/1 "2023-07-28T19:54:23Z")

</div>

What is the best way to represent a state space system in ModelingToolkit? For example

```julia
@variables t x1(t) x2(t) u(t) y(t)
@constants a11 = -1 a12 = 0.1a21 = 0 a22 = -1 b1 = 1 b2 = 0 c1 = 1 c2 = 1
D = Differential(t)
eqs = [D(x1) ~ a11*x1 + a12*x2 + b1*u,
		D(x2) ~ a21*x1 + a22*x2 + b2*u]

```

1. When I try to add the y = c1_x1 + c2_x2 equation, I run into an error (The LHS cannot contain nondifferentiated variables. Please run `structural_simplify`) and following the suggestion to use structural\_simplify gives me an ExtraVariablesSystemException. Is this not intended to be supported; therefore, without any output feedback, I would compute y separately?
2. I’ve seen elsewhere that the equations can be written as a matrix differential equation, but I get an error about unknown axes for x.

Are there any simple examples of doing this?

---

<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: [July 29, 2023, 1:10am UTC](https://discourse.julialang.org/t/modelingtoolkit-jl-state-space-representations-what-is-the-recommendation/102212/2 "2023-07-29T01:10:43Z")

</div>

It looks like you’re talking about linear statespace systems? If that’s the case, check out [GitHub - JuliaControl/ControlSystemsMTK.jl: Interface between ControlSystems and ModelingToolkit](https://github.com/JuliaControl/ControlSystemsMTK.jl)

---

<div class="post-metadata">

### Author: ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)
#### Post date: [July 29, 2023, 2:16pm UTC](https://discourse.julialang.org/t/modelingtoolkit-jl-state-space-representations-what-is-the-recommendation/102212/3 "2023-07-29T14:16:09Z")

</div>

The answer from @baggepinnen above is the correct “I want to solve a problem answer”. Here is the “I want to build this myself” answer.

The reason you get an error is that `u(t)` is unspecified. When you apply `structural_simplify`, all the pieces of the system must be in place.

The way to debug this is to run

```julia
partial_sys = structural_simplify(sys; check_consistency=false)

```

You can then compare `states(partial_sys)` to `equations(partial_sys)`, and immediately notice that there is no expression for `u`.

Here is one possible approach:

```julia
function build_system()
    sts = @variables t x(t)[1:2] u(t) y(t)
    ps = @parameters a[1:2,1:2]=[-1 0.1; 0 -1] b[1:2]=[1 0] c[1:2]=[1 1]
    D = Differential(t)

    eqs = [D.(x) .~ a * x + b .* u
           y ~ c' * x
           u ~ sin(t)
          ]

    sys = ODESystem(eqs, t; name=:test)

    sys = structural_simplify(sys)
end

```

---

<div class="post-metadata">

### Author: ![Andrew\_Liu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrew_liu/32/14282_2.png) [@Andrew\_Liu](https://discourse.julialang.org/u/Andrew_Liu)
#### Post date: [July 29, 2023, 2:43pm UTC](https://discourse.julialang.org/t/modelingtoolkit-jl-state-space-representations-what-is-the-recommendation/102212/4 "2023-07-29T14:43:52Z")

</div>

Thank you guys so much! This is exactly what I needed.
