State Space Modeling is also a kind of way to convert a differential equation into a set of matrix equation. But the way we did in previous section (Differential Equation meeting Matrix) was mainly for mathematical manipulation. It was not easy to extract any practical meaning out of the matrix. However, State Space Modeling is a method to convert a/a set of differential equation(s) into a form of matrix equation from which we can extract physical/practical meaning of a system.
- General Derivation of State Space Equation
- State Space Model Example : Simple Spring-Mass
- State Space Model Example : RLC Circuit
- What can you do with a state space model once you have it?
General Derivation of State Space Equation
Before any matrix appears, let's state the question this section answers. A differential equation describes how a system changes, but it does not say which quantities you control and which ones you measure. The state space model makes that split explicit. It separates the input, the internal state and the output, and it writes every equation as first order.
The logic behind the State Space Modeling is as follows.
For most of differential equations (especially those equations for engineering system), there would be terms that can be interpreted as an input to a system and terms that can be interpreted as output of the system.
For example, if you have a system with an input function labeled as u(t) and output function labeled as y(t) as shown below.

You may be able to mode this system in a differential equation as shown below.
Giving you more practical examples, the very common spring system and spring-damper systems can also be described as single input and single output system and can be described in a form of differential equation shown above. (Actually you can interprete all the examples in Modeling Building Block section into an input/output relations)


You can also think of a system with multiple inputs and multiple outputs as shown below.

Regardless of how many inputs and outputs you have, there is a certain form of differential equation (linear differential equation) that can be converted into a set of Matrix equation as shown below. The special matrix form as shown below is called 'State Space Model'.
The state space mode for a single input and single output can be modeled as shown below.

The state space mode for multiple inputs and multiple outputs can be modeled as shown below. Can you notice what is the difference between this equation and previous equation ? In single input/single output equation, you would notice that y(t) is a single function whereas in multiple input/multiple output equation y(t) is a vector (a set of functions). Similarly you would notice that u(t) is a single functionin single input/single output equation whereas u(t) is a vector (a set of functions) in mutiple input/multiple output equation.

One thing you have to be very careful about the state space modeling is the dimension of A, B, C, D matrix/vector which is described as shown below.

Let's write the dimensions out for the general case, because the diagram above covers only one input and one output. Take n state variables, m inputs and p outputs. Then A is n x n, B is n x m, C is p x n and D is p x m. With one input and one output, B is an n x 1 column, C is a 1 x n row and D is a single number. The diagram above draws D as an n x 1 vector, and that label is a mistake. In the single input and single output case, y(t) and u(t) are both scalars, so D must be 1 x 1.
A holds the dynamics : It is always square, n x n, and it alone decides whether the free response grows or decays.B and C connect the state to the outside : B says how the inputs push the states, and C says which combination of the states you measure.D is the direct path : It is non-zero only when an input reaches an output without passing through any state. In most mechanical and circuit examples D = 0.
How to figure out A,B,C,D matrix/vector out of a differential equation or a set of differential equations ? I will put down several samples from the simplest ones to complicated ones later when I have time. For now, just refer to <Example : 2nd Order Non-Homogeneous Lineare Differential Equation - System with Single External Input > as an example. You can easily get A, B from the process explained in Converting High Order Differential Equation into First Order Simultaneous Differential Equation, but you may put some thought and effort to figure out C and D (especially C).
Let's answer the red question for the third order equation shown earlier in this section, y''' + a2y'' + a1y' + a0y = u. The usual choice of states is x1 = y, x2 = y' and x3 = y''. The first two state equations are only definitions, x1' = x2 and x2' = x3. The third comes from the equation itself, x3' = -a0x1 - a1x2 - a2x3 + u. So A = [0 1 0; 0 0 1; -a0 -a1 -a2] and B = [0; 0; 1]. The output is y = x1, so C = [1 0 0] and D = 0. C is simply the row that picks the state you call the output.
Why we want to convert a or a set of differential equations into this form (State Space Model) ?
It is because
- There are many analysis methods developed for this form
- Many of the computer software requires this form as the description of the system (implying that this form is very useful to solve a differential equations with computer in numerical method).
State Space Model Example : Simple Spring-Mass
Let's look into a simple example as shown below. It is a spring-mass system with friction. You can think of the friction as a damping.

Now you can derive the two first order differential equations as shown below. (I will not show you each and every steps of how to draw these equations. I just assume that you became familiar enough to draw differetial equations for such a simple system like this).

If we determine the input, state variables and output as shown below, (these are the ones YOU determine. Depending on how you determine these variables, the state space matrix varies. When you first read text about state space modeling, you may ask "Why they picked as the state variables ?", "Why they picked this as the input and that as the output ?". Basically it is totally upto the textbook writer. Theoretically you can determine those variables in any way you like as long as you can define proper differential equations using those variables).

The diagram above labels x and v as control variables. The usual name for them is state variables. In control engineering the control variable is the input, which is the force F in this example.
The first matrix equation with A, B matrix is just direct conversion of the set of differential equations into matrix form, so there should be no ambiquity about it. But the second matrix equation with C, D matrix may not look straightforward. Actually the second matrix with C, D matrix become a little different depending on which variable you want to get as the output of the system. If you want to take v as the output, it become as follows.

If you want to take x as the output, it become as follows.

A third choice of output shows when D is not zero. Take the acceleration a = dv/dt as the output. The second state equation already gives it: a = -(k/m)x - (c/m)v + (1/m)F. So C = [-k/m -c/m] and D = 1/m. The force changes the acceleration immediately, without passing through a state, and D is the term that carries this direct path.
Notice also what stays the same across the three outputs. A and B do not change, because they describe the mass, the spring and the friction. Only C and D change, because they describe what you measure. The eigenvalues of A are the roots of mλ2 + cλ + k = 0, which is the characteristic equation of mx'' + cx' + kx = F. So every output of this system decays or oscillates at the same rates.
A and B belong to the system : They come from the physics, here m, k and c, and they do not depend on the output you pick.C and D belong to the measurement : Output v gives C = [0 1], output x gives C = [1 0], and output a gives C = [-k/m -c/m] with D = 1/m.The state vector x, v stores the energy : The spring energy depends on x and the kinetic energy depends on v, so these two numbers are enough to predict the future motion.
Once you build up this kind of state space model, you can get the solution of these system with various software package. (See Damped Spring-mass example in Matlab/Octave differential equation page)
State Space Model Example : RLC Circuit
The spring-mass model has a mechanical input and mechanical states. Let's check that the same method works for an electrical system. This example is a series RLC circuit. The input is the source voltage v1, and the output is the capacitor voltage v2.
Let's take a RLC circuit as another example as shown below.

If we determine the input, state variables and output as shown below, (these are the ones YOU determine. Depending on how you determine these variables, the state space matrix varies. When you first read text about state space modeling, you may ask "Why they picked as the state variables ?", "Why they picked this as the input and that as the output ?". Basically it is totally upto the textbook writer. Theoretically you can determine those variables in any way you like as long as you can define proper differential equations using those variables).
these

The diagram above uses the same label, control variables, for x1 and x2. They are the state variables of the circuit, and the input is v1. The label x2 = dv/dt means the derivative of the output voltage, dv2/dt.
The state space model become as shown below. (I will leave it to you to derive this equation or just use this equation -:)

Let's derive the model rather than leave it as an exercise. The same current i flows through R, L and C, and the capacitor gives i = C dv2/dt. Kirchhoff's voltage law around the loop gives v1 = Ri + L di/dt + v2. Substitute i, and you get LCv2'' + RCv2' + v2 = v1. Divide by LC, and the second row of A, [-1/LC -R/L], and the entry 1/LC of B appear directly.
Now compare it with the spring-mass example. The two models have the same shape. L plays the role of m, R plays the role of c, and 1/C plays the role of k. The eigenvalues of A are the roots of λ2 + (R/L)λ + 1/(LC) = 0. The output rings, which means it oscillates while it decays, when R < 2√(L/C). For a larger R it settles without oscillation.
Another state choice is also common for this circuit. Take the capacitor voltage v2 and the inductor current i as the states. Then A = [0 1/C; -1/L -R/L] and B = [0; 1/L]. The matrices look different, but the relation between v1 and v2 is exactly the same. This is the freedom the page describes above: you choose the state variables, and each valid choice gives a different A, B and C for the same system.
The circuit and the spring-mass system share one model : L, R and 1/C take the places of m, c and k.Physical states are a good default : The capacitor voltage and the inductor current store the energy of the circuit, just as x and v do for the spring.The model is not unique : Different state choices give different matrices, but the same input-output behavior.
If you are interested in the solution of this equation, see State Space Model - RLC in Matlab/Octave Differential Equation page.
What can you do with a state space model once you have it?
The two examples end with a matrix model. Let's see what that model gives you, because this is the reason for building it. The page lists two reasons above: many analysis methods use this form, and most software expects it. Both become concrete once you look at three standard operations.
The first operation is the transfer function. Take the Laplace transform of both equations with zero initial state. The result is Y(s) = G(s)U(s), with G(s) = C(sI - A)-1B + D. For the spring-mass system with output x, this gives G(s) = 1/(ms2 + cs + k). For the RLC circuit it gives G(s) = 1/(LCs2 + RCs + 1). The denominator of G(s) is the characteristic polynomial of A, so the poles of the transfer function are the eigenvalues of A.
The second operation is the stability check. The free response x' = Ax is a sum of terms eλt, one for each eigenvalue λ of A. So the system is stable when every eigenvalue has a negative real part. You do not need to solve the equation to know this. You only need the eigenvalues of A, and software computes them for any size of A.
The third operation is simulation. A numerical solver needs x' as a function of x and u, and the first state equation is exactly that. This is why Matlab and Octave functions such as ss() and lsim() take A, B, C and D as their input. The Matlab/Octave page linked in the two examples uses them.
One more point matters when you compare your model with a textbook. A change of state variables x = T z, with an invertible matrix T, gives new matrices T-1AT, T-1B and CT. The eigenvalues and G(s) do not change. So two correct models of the same system can look very different, and both are right.
Transfer function : G(s) = C(sI - A)-1B + D, and its poles are the eigenvalues of A.Stability : The system is stable when every eigenvalue of A has a negative real part.Simulation : Solvers such as lsim() integrate x' = Ax + Bu step by step and read the output through C and D.Many models, one system : A change of state variables changes A, B and C, but not the eigenvalues or G(s).