# How to get the state-space model of a dynamic system

State-space system representation lays the foundations for modern control theory. It solves many of the limitations of the classical control theory in which transfer functions were used to asses the behavior of a closed loop system.

A state-space model describes the behavior of a dynamic system as a set of first order ordinary differential equations (ODE). If a dynamic model is described by a higher order ODE, using state-space, the same model can be described as a set of coupled first order ODEs. The internal variables of the state-space model are called state variables and they fully describe the dynamic system and its response for certain inputs.

The numbers of state variables of the state-space model is equal with the highest order of the ODE describing the dynamic system. State variables can also be defined as the smallest set of independent variables that completely describe the system. The set of state variables is not unique and they may be defined in terms of physical variables which can be measured, or in terms of variables that can not be measured directly.

For a given state-space model, the number of state variables is equal with the number of initial conditions needed to completely solve the system model. The state variables are exactly those variables for which initial conditions are required. The number of state variables is equal with the order to ODE describing the system.

A state-space model is formed by:

• state equations
• output equations

The state equations has the general form:

$\dot{x} = \mathbf{A} \cdot x + \mathbf{B} \cdot u$

where:

$\begin{split} x &= \begin{Bmatrix} x_1\\ x_2\\ …\\ x_n \end{Bmatrix}_{n \times 1} \quad &\text{state vector} \\ \text{ } \\ u &= \begin{Bmatrix} u_1\\ u_2\\ …\\ u_m \end{Bmatrix}_{m \times 1} \quad &\text{input vector} \\ \text{ } \\ A &= \begin{bmatrix} a_{11} & a_{12} & … & a_{1n}\\ a_{21} & a_{22} & … & a_{2n}\\ … & … & … & …\\ a_{n1} & a_{n2} & … & a_{nn} \end{bmatrix}_{n \times n} \quad &\text{state matrix} \\ \text{ } \\ B &= \begin{bmatrix} b_{11} & b_{12} & … & b_{1m}\\ b_{21} & b_{22} & … & b_{2m}\\ … & … & … & …\\ b_{n1} & b_{n2} & … & b_{nm} \end{bmatrix}_{n \times m} \quad &\text{input matrix} \end{split}$

The output equations have the general form:

$y = \mathbf{C} \cdot x + \mathbf{D} \cdot u$

where:

$\begin{split} y &= \begin{Bmatrix} y_1\\ y_2\\ …\\ y_p \end{Bmatrix}_{p \times 1} \quad &\text{output vector} \\ \text{ } \\ C &= \begin{bmatrix} c_{11} & c_{12} & … & c_{1n}\\ c_{21} & c_{22} & … & c_{2n}\\ … & … & … & …\\ c_{p1} & c_{p2} & … & c_{pn} \end{bmatrix}_{p \times n} \quad &\text{output matrix} \\ \text{ } \\ D &= \begin{bmatrix} d_{11} & d_{12} & … & d_{1m}\\ d_{21} & d_{22} & … & d_{2m}\\ … & … & … & …\\ d_{p1} & d_{p2} & … & d_{pm} \end{bmatrix}_{p \times m} \quad &\text{direct transmission matrix} \end{split}$

By putting together the the state equation and the output equation, we get the general state-space form:

$\bbox[#FFFF9D]{\left\{\begin{matrix} \dot{x}_{n \times 1} = \mathbf{A}_{n \times n} \cdot x_{n \times 1} + \mathbf{B}_{n \times m} \cdot u_{m \times 1}\\ y_{p \times 1} = \mathbf{C}_{p \times n} \cdot x_{n \times 1} + \mathbf{D}_{p \times m} \cdot u_{m \times 1} \end{matrix}\right.}$

In order to be easier to remember the general form of the state-space model, we can write it without specifying the size of the vectors and matrices.

$\left\{\begin{matrix} \dot{x} = \mathbf{A} \cdot x + \mathbf{B} \cdot u\\ y = \mathbf{C} \cdot x + \mathbf{D} \cdot u \end{matrix}\right.$

Now let’s go through a couple of examples of dynamic systems.

Example 1. Determine the state-space model for a translational body mass with spring and damper, upon which an input force F acts.

Image: Translational mass with spring and damper

where:

m [kg] – mass
k [N/m] – spring constant (stiffness)
c [Ns/m] – damping coefficient
F [N] – external force acting on the body
x [m] – displacement of the body

The system is governed by the following ordinary differential equation.

$F – kx – c \frac{dx}{dt} – m \frac{d^2x}{dt^2} = 0$

To understand how the ODE was determined, read the article Mechanical systems modeling using Newton’s and D’Alembert equations.

The first step is to rearange the equation into a new format:

$m \ddot{x} + c \dot{x} + kx = F \tag{1}$

As you can see, the order of the differential equation is two, therefore we need to choose two state variables, x1 and x2, each equal to:

$\left\{\begin{matrix} x_1 = x \tag{2}\\ x_2 = \dot{x} \end{matrix}\right.$

Now, if we differentiate the state variables, we get:

$\left\{\begin{matrix} \dot{x_1} = \dot{x} \\ \dot{x_2} = \ddot{x} \end{matrix}\right.$

Taking into account the definition of the state variables, the derivatives can be written as:

$\left\{\begin{matrix} \dot{x_1} = x_2 \tag{3}\\ \dot{x_2} = \ddot{x} \end{matrix}\right.$

Using (1), (2) and (3), we can write our set of first order ordinary differential equations as:

$\left\{\begin{matrix} \dot{x_1} = x_2 \\ m\dot{x_2} + c x_2 + k x_1 = F \end{matrix}\right.$

Dividing the second equation with m and rearranging gives:

$\left\{\begin{matrix} \dot{x_1} &=& x_2 \\ \dot{x_2} &=& -\frac{c}{m} x_2 -\frac{k}{m} x_1 + \frac{F}{m} \end{matrix}\right.$

If we write the set of equations in matrix format, we’ll get the following:

$\begin{Bmatrix} \dot{x_1}\\ \dot{x_2} \end{Bmatrix}=\begin{bmatrix} 0 & 1\\ -\frac{k}{m} & -\frac{c}{m} \end{bmatrix} \cdot \begin{Bmatrix} x_1\\ x_2 \end{Bmatrix} + \begin{bmatrix} 0\\ \frac{1}{m} \end{bmatrix} \cdot \begin{Bmatrix} F \end{Bmatrix}$

Now we can easily identify the state matrix A and the input matrix B.

$A =\begin{bmatrix} 0 & 1\\ -\frac{k}{m} & -\frac{c}{m} \end{bmatrix}, \quad B =\begin{bmatrix} 0\\ \frac{1}{m} \end{bmatrix}$

To identify the output and direct transmission matrices, we need to decide which is the measurable output. Let’s consider that we want to measure the position of the body. For this we write the output equation as:

$y = x_1$

If we write the output equation in matrix format, we get:

$\begin{Bmatrix} y \end{Bmatrix}= \begin{bmatrix} 1 & 0 \end{bmatrix} \cdot \begin{Bmatrix} x_1\\ x_2 \end{Bmatrix} + \begin{bmatrix} 0 \end{bmatrix} \cdot \begin{Bmatrix} F \end{Bmatrix}$

Which gives the output matrix C and the direct transmission matrix D:

$C = \begin{bmatrix} 1 & 0 \end{bmatrix}, \quad D = \begin{bmatrix} 0 \end{bmatrix}$

In order to check that we have determined the state-space model parameters (matrices) corectly, we are going to use Xcos to simulate our dynamic system.

First step is to load the parameters in a Scilab script and run it.

m = 2; // [kg]
c = 1; // [Ns/m]
k = 2; // [N/m]
x0 = 0; // [m]
v0 = 0; // [m/s]

A = [0 1;-k/m -c/m];
B = [0;1/m];
C = [1 0];
D = 0;
X0 = [x0 v0];


As you may notice, we need to provide initial conditions for the state variables. These are x0 and v0.

In the Xcos block diagram we need to add a Continuous state-space system block (CLSS), which can be found in the Continuous time systems palette:

Image: State-space model Xcos block diagram – mechanical system

Since we have defined our state-space matrices in the workspace, we only need to add them as parameters in the Xcos state-space block:

Image: State-space model Xcos block parameters

The step input force F is set to give 1 N after 1 s of simulation.

After the simulation is run for 20 s and the Scope block is setup with the right signal limits, running the model gives the following graphical window:

Image: State-space model Xcos block diagram – mechanical system position response

We can easily verify the results of the simulation by comparing them against the result explained in the article Mechanical systems modeling using Newton’s and D’Alembert equations, in which we simulated the ordinary differential equation (ODE) of the system.

If we need to measure the speed, we have to modify the output equation accordingly:

$y = x_2$

This will result in the output matrix C being:

$C = \begin{bmatrix} 0 & 1 \end{bmatrix}, \quad D = \begin{bmatrix} 0 \end{bmatrix}$

Running the model with the updated C matrix gives the following graphical window:

Image: State-space model Xcos block diagram – mechanical system speed response

Example 2. Determine the state-space model for a simple RL circuit connected to an external voltage source.

The schematic of the circuit is depicted in the image below.

Image: Series RL circuit schematic

where:

E [V] – continuous voltage source
S – switch
R [Ω] – resistance
L [H] – inductance
u [V] – voltage drop across the circuit
uR [V] – voltage drop across the resistor
uL [V] – voltage drop across the inductor
i [A] – electrical current through the circuit

The differential equation governing the system is:

$E = R i(t) + L \frac{di(t)}{dt} \tag{4}$

In order to understand how the equation was obtained, read the article RL circuit – detailed mathematical analysis.

Because the differential equation has the order 1, we only need one state variable:

$x_1 = i \tag{5}$

Replacing (5) in (4), gives:

$E = R x_1 + L \dot{x}_1$

Dividing with L and rearranging, we get:

$\dot{x}_1 = – \frac{R}{L} x_1 + \frac{E}{L}$

In matrix format, the equation is written as:

$\begin{Bmatrix} \dot{x}_1 \end{Bmatrix}=\begin{bmatrix} – \frac{R}{L} \end{bmatrix} \cdot \begin{Bmatrix} x_1 \end{Bmatrix}+\begin{bmatrix} \frac{1}{L} \end{bmatrix} \cdot \begin{Bmatrix} E \end{Bmatrix}$

from which we can extract the state matrix A and input matrix B:

$A=\begin{bmatrix} – \frac{R}{L} \end{bmatrix}, \quad B = \begin{bmatrix} \frac{1}{L} \end{bmatrix}$

Our measurable variable is the electrical current i, therefore the output equation is:

$y = x_1$

If we write the output equation in matrix format, we get:

$\begin{Bmatrix} y \end{Bmatrix}= \begin{bmatrix} 1 \end{bmatrix} \cdot \begin{Bmatrix} x_1 \end{Bmatrix} + \begin{bmatrix} 0 \end{bmatrix} \cdot \begin{Bmatrix} E \end{Bmatrix}$

Which gives the output matrix C and the direct transmission matrix D:

$C = \begin{bmatrix} 1 \end{bmatrix}, \quad D = \begin{bmatrix} 0 \end{bmatrix}$

In order to check our state-space model parameters (matrices), we are going to use the same Xcos block diagram to simulate our dynamic system.

Since we had only one state variable, our state-space matrices have turned into scalars. In order to simulate our electrical system, as a state-space model, we only need to update the definition of the state-space matrices in the model we’ve used:

E = 12;
R = 0.3;
L = 0.04;

A = -R/L;
B = 1/L;
C = 1;
D = 0;
X0 = 0;

The step input voltage E is set to give 12 V after 0.1 s of simulation.

After running the simulation for 2 s, we get the following graphical window:

Image: State-space model Xcos block diagram – electrical system current response

We can easily verify the results of the simulation by comparing them against the result explained in the article RL circuit – detailed mathematical analysis, in which we simulated the ordinary differential equation (ODE) of the system.

#### Scilab implementation of state-space models (systems)

State-space models can be implemented and simulated in Scilab as well, using the predefined functions syslin() and csim(). With these function the user can define a linear systems in continious or discrete time domain and check the response for input functions.

The general syntax of the syslin() function is:

sys=syslin(dom,A,B,C [,D [,x0] ])

where:

dom – character string ('c', 'd'), or [], or a scalar, which specifies the time domain of the system; can have the following values: 'c' for a continuous time system, 'd' for a discrete time system, n for a sampled system with sampling period n (in seconds).
A, B, C, D – matrices of the state-space system representation (D optional, with default value 0)
x0 – vector representing the initial values of the state variables (if not provided the default value is 0)
sys – the name of the linear system defined as a tlist

For our mechanical system example, in order to define the state-space model, we need to enter the following Scilab instructions:

sysMech = syslin('c',A,B,C,D,X0');

Notice that the initial condition vector needs to be a column vector, that is why we use its transpose of X0.

Next, we need to define the time domain for which we are going to perform the simulation and also the type of input function for which we want to observe the response. For out example we are going to simulate 20 s for a unitary step input (the step value is assigned at t = 0 s).

t = 0:0.01:20;
[ys,xs]=csim("step",t,sysMech);

The response of the system is stored in the ys vector. To plot its variation in time, we’ll use the following Scilab instructions:

plot(t,ys), xgrid()
xlabel('t')
ylabel('y')

Running the instructions will produce the following graphical window:

Image: State-space model Xcos block diagram – mechanical system position response csim()

As you can see, we have the same response as for the Xcos block diagram model, the only difference being that the unitary step time is 0 s for the Scilab model and 0.1 s for the Xcos model.

For any questions, observations and queries regarding this article, use the comment form below.

Don’t forget to Like, Share and Subscribe!