The Mystery of Matlab's tf2ss

I love Matlab. Because it’s simple, with just the bare minimum of programming language acquisition, you can do big things easily.
And yet, it’s structured and systematic.
“You are beautiful”

Hello, I’m Hiroki🐶, a data scientist.

The Mystery of the State Space Representation tf2ss from Transfer Functions

as introduced in the previous blog, the handy function “tf2ss” that calculates the coefficients (A,B,C,D) of the state space representation from a transfer function is very convenient.

For example, let’s calculate the coefficients of the state space representation from the transfer function of the model above.

$$\begin{align*} G=\frac{x}{y}=\frac{cs+k}{ms^2+cs+k} \
\end{align*}$$

It’s simple to use.
For example, the Matlab code would look like this:

% Definition of parameters
m = 2; % Mass [kg]
k = 1; % Spring constant [N/m]
c = 3; % Damping coefficient [N.s/m]

num=[c k];
den=[m c k];
[A,B,C,D]=tf2ss(num,den);

% Creation of the state space model
sys = ss(A, B, C, D);


% Plotting the results
figure;
bode(sys)
grid on;

Looking into the content of A,B,C,D outputted by tf2ss,

$$ A \begin{align*} &= \end{align*} \begin{pmatrix} -1.5 & -0.5 \\
1 & 0 \\
\end{pmatrix}$$

$$ B \begin{align*} &= \end{align*} \begin{pmatrix} 1 \\
0 \\
\end{pmatrix}$$

$$ C \begin{align*} &= \end{align*} \begin{pmatrix} 1.5 & 0.5 \\
\end{pmatrix}$$
$$ D=0 $$

it differs from the A,B,C,D discussed in the previous blog.

$$ A \begin{align*} &= \end{align*} \begin{pmatrix} 0 & 1 \\
-k/m & -c/m \\
\end{pmatrix}$$

$$ B \begin{align*} &= \end{align*} \begin{pmatrix} 0 \\
1/m \\
\end{pmatrix}$$

$$ C \begin{align*} &= \end{align*} \begin{pmatrix} k & c \\
\end{pmatrix}$$
$$ D=0 $$

There’s a difference.
In this article,

  • Verifying the state space parameters A,B,C,D derived from the transfer function G using tf2ss

will be introduced.
Let’s first verify with the following code.

  % Definition of Parameters
  m = 2; % Mass [kg] ※Please set the appropriate value
  k = 1; % Spring constant [N/m] ※Please set the appropriate value
  c = 3; % Damping coefficient [N.s/m] ※Please set the appropriate value

num=[c k];
den=[m c k];
[A,B,C,D]=tf2ss(num,den)

% Creation of the state space model
sys = ss(A, B, C, D);

A1 = [0 1; -k/m -c/m];
B1 = [0; 1/m];
C1 = [k c];
D1 = 0;

sys1=ss(A1,B1,C1,D1)


% Plotting the results
figure;
bode(sys)
hold on
bode(sys1)
grid on;
legend("sys","sys1")

The results match exactly as shown below.

Next, let’s verify by hand calculation. The ABCD determined by Matlab’s “tf2ss” looks like the following form:

$$ A \begin{align*} &= \end{align*} \begin{pmatrix} a1 & a2 \\
1 & 0 \\
\end{pmatrix}$$

$$ B \begin{align*} &= \end{align*} \begin{pmatrix} 1 \\
0 \\
\end{pmatrix}$$

$$ C \begin{align*} &= \end{align*} \begin{pmatrix} c1 & c2 \\
\end{pmatrix}$$
$$ D=0 $$

Then, substituting the above ABCD into the following transfer function G

$$\begin{align*} G=\mathbf{C} (s\mathbf{I} -A)^{-1} B +D \
\end{align*}$$

and comparing it with the transfer function directly derived from the model
$$\begin{align*} G=\frac{x}{y}=\frac{cs+k}{ms^2+cs+k} \
\end{align*}$$

to calculate a1, a2, c1, c2, indeed, it matches the results of tf2ss. Please try the hand calculation yourself. ♡

Lastly, why does the ABCD obtained by tf2ss take such a form?

The reason is that generally, when converting from a transfer function to a state space representation, there isn’t just one way to express it, but rather, there are various ways to do so.

That’s all for today.

Thank you for reading until the end.
Love&Respect♡
Hiroki🐶