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

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

``````@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 = c1x1 + c2x2 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?

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

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

``````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:

``````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
``````
2 Likes

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