# Using transfer function model to generate state space model

I am trying to generate the state space model for the following system:

However, the following code does not generate the state space model I expect:

tf = TransferFunctionModel[{{1/s}, {1/(s + 2)}}, s]
ssm = StateSpaceModel[tf]


What I'm expecting is

$A = \begin{bmatrix} -2 & 0 \\ 1 & 0 \end{bmatrix}$ $B = \begin{bmatrix} 0 \\ 1 \end{bmatrix}$ $C = \begin{bmatrix} 0 & 1 \\ 1 & 0 \end{bmatrix}$ $D = \begin{bmatrix} 0 \\ 1 \end{bmatrix}$

but what I'm getting is

$A = \begin{bmatrix} 0 & 1 \\ 0 & -2 \end{bmatrix}$ $B = \begin{bmatrix} 0 \\ 1 \end{bmatrix}$ $C = \begin{bmatrix} 2 & 1 \\ 0 & 1 \end{bmatrix}$ $D = \begin{bmatrix} 0 \\ 0 \end{bmatrix}$

The state equations are:

$$\dot{x_1}(t) = -2 x_1 (t) + w_1 (t)$$ $$\dot{x_2}(t) = x_1(t) + u(t)$$ $$y_1(t) = x_2(t)$$ $$y_2(t) = \dot{x}_2(t)$$

where $w_1(t)$ denotes the white noise added to the system, and $x_2(t)$ denotes the system error, $e$.

How should I enter my transfer function model to generate such a state space model?

-
I think you have a typo, because the matrices you expect do not give the transfer function you started with. In any case, look at the option StateSpaceRealization and see if that will solve your problem. – Suba Thomas Apr 5 '13 at 16:29
It looks like you are entering it in the form TransferFunctionModel[{num,den},s]. I don't see the relation between this num, den and your picture. What variable is the input and what variable is the output of your transfer function? – bill s Apr 5 '13 at 16:29
No, it's not {num,den}, but a matrix, as it's a MIMO system with one input and two outputs. The input to the system is the white noise, and two outputs are $e$ and $\dot{e}$. I'll add the state equations to hopefully make more sense. – Gerrit Apr 8 '13 at 8:20
The state is {x1, x2}. So your C matrix says that $y_2(t)=x_2(t)$, not that $y_2(t)=\dot{x}_2(t)$ as in your state equation. – bill s Apr 8 '13 at 9:01
But more fundamentally, how is u ("control input") calculated from $e$ and $\dot{e}$? If you don't specify this, then you have a 2-input ($u$ and white noise) and 2-output ($e$ and $\dot{e}$) system. So you have neither the state equations nor the state space equations correct. – bill s Apr 8 '13 at 9:12

System sys1 has inputs $u(t)$ and the white noise.

sys1 = StateSpaceModel@TransferFunctionModel[{{1, 1/((s + 2))}}, s];


We are only interested in the input $u(t)$ and so we extract the subsystem corresponding to that.

sys2 = SystemsModelExtract[sys1, 1];


The desired output of the overall system is 1) $e(t)$ which is the integral of the output of sys1 and 2) $\dot{e}(t)$ which can just be taken as the output of sys1. So I am going to construct a system that I plan to connect in series with sys2.

sys3 = TransferFunctionModel[{{1/s}, {1}}, s];


Now the series connection will give the desired result.

SystemsModelSeriesConnect[sys2, sys3]


StateSpaceModel[{{{-2, 0}, {1, 0}}, {{0}, {1}}, {{0, 1}, {1, 0}}, {{0}, {1}}}, SamplingPeriod -> None, SystemsModelLabels -> None]

-
In sys1 if you remove StateSpaceModel and then wrap the final result in StateSpaceModel, you will get yet another i-o equivalent state-space represenation. The interal dynamics of these are different and they are sometimes very very important. – Suba Thomas Apr 12 '13 at 0:01

An answer based on the discussion above. For the system with inputs comprising of the deterministic feedback input $u(t)$ and the white noise input $w(t)$, and outputs $e(t)$ and $\dot{e}(t)$, a state-space representation would be

StateSpaceModel@TransferFunctionModel[{{1/s, 1/(s (s + 2))}, {1, 1/(s + 2)}}, s]


which gives StateSpaceModel[{{{0, 0, 0}, {0, 0, 1}, {0, 0, -2}}, {{1, 0}, {0, 0}, {0, 1}}, {{1, 1, 0}, {0, 0, 1}}, {{0, 0}, {1, 0}}}, SamplingPeriod ->None, SystemsModelLabels -> None]

You could then use this to design a feedback controller.

-
While I believe that you're right, can you please explain how you got to that conclusion? For reference, I'm working from pg. 177 of this report (on my dropbox). The original link is down for maintenance. – Gerrit Apr 11 '13 at 16:34
Thanks for the reference. That helps. State-space representations are not unique, so we could go through two different routes and end up with two different but input-output equivalent state-space representations. I will give detailed explanations in my next answer. I am leaving this answer as is, because it is also correct. – Suba Thomas Apr 11 '13 at 23:50
I expected there would be equivalent state-space representations, but wanted to know how I can use Mathematica to produce the same matrices as in the reference. Your other answer does this beautifully, thank you. – Gerrit Apr 12 '13 at 10:24