Skip to content

Instantly share code, notes, and snippets.

@wijiler
Last active March 12, 2026 19:17
Show Gist options
  • Select an option

  • Save wijiler/158fa6e37ce0727caad3fac4298d5fb4 to your computer and use it in GitHub Desktop.

Select an option

Save wijiler/158fa6e37ce0727caad3fac4298d5fb4 to your computer and use it in GitHub Desktop.

State Space Model

This is a mathematic representation of a dynamic system as a linear equation. Although there is no perfectly linear system in the real world, they work well enough as approximations and make calculations much, much easier

$$ \bar{x} = (\text{position, velocity, ...}) $$

$$ \bar{u} = (\text{motor voltage}) $$

$$ \dot{x} = A\bar{x} + B\bar{u} $$

$$ y = C\bar{x} + D\bar{u} $$

What does this mean?

State

Vector x represents all possible states, or internal factors that could affect the system, like its current velocity, mass, etc.
There's no 1 size fits all solution for state, it depends on the system, like for our Vex robot the only 3 factors that matter are, position, velocity, and heading.
This can be different per system, for example in a mass/damper system the mass, and damping force need to be taken into account.

Inputs

Vector u represents all inputs. These are all of the things you have control over, or more generally, all external forces acting on the system. Using the mass/damper system as an example if you have a force acting on the mass, F(t), that would be an input, because it is an external force acting on the system, affecting the system.

What are A,B,C, and D

These are matrices that relate the variable they're next to how it changes over time, the first equation is known as the state equation, and the second as the output. The output isn't really useful for much, and C is generally the Identity matrix, while D is a matrix or vector and is usually just zeroes. This is because you generally want to observe the full state, and D = 0 means that inputs won't just instantly affect outputs. A, and B are a bit more complex and deserve their own section.

A, and B

Lets say vector x has a length n, and vector u has a length m, matrix A (the state matrix) is of dimension n * n, while matrix B is of dimensions n * m.
So for this equation (the variables inside the vectors are arbitrary as this is just an example)

$$ \bar{x} = (p,v,h) $$

$$ \bar{u} = (vo) $$

$$ \text{to note these variables are irrelevant im using them as an example} $$ then $$ A = \begin{bmatrix} 0, 0,0 \\ 0,0,0 \\ 0,0,0 \end{bmatrix} B = \begin{bmatrix} 0\\ 0\\ 0 \end{bmatrix} $$

In the state matrix (A) each row and column corresponds to 1 input each. So 11 is how time affects p (or p/dt), but 12 is how v affects p in relation to time. (will be elaborated on later)
In the input matrix (B) each row and column corresponds to how the input affects the state in relation to time.

Filling A, and B

Now obviously A, and B can't just be 0's, we need to fill them up with actually useful information. Each entry to A shows how state contributes to the rate of change in the system, and in B entries show how inputs affect state. To fill a you solve differential equations in relation to time, I think the simplest way to understand this is to start with an arbitrary first order differential equation then work our way up.

$$ 3\frac{dx}{dt} + 12x = 6u $$

$$ \text{first we need to isolate }\frac{dx}{dt} $$

$$ 3\frac{dx}{dt} = -12x + 6u $$

$$ \dot{x} = -4x + 2u $$

$$ \text{solving the equation gives us the A, and B matrices in a first order equation} $$

$$ A = [-4] B = [2] $$

Now onto systems of differential equations

$$ 2\frac{dx_{1}}{dt} + 6x_{1} = 8u $$

$$ 3\frac{dx_{2}}{dt} + 6x_{1} + 9x_{2} = 0 $$

$$ \text{To get the first row of A, and B we need to solve for }x_{1}\text{ in the first equation} $$

$$ 2\frac{dx_{1}}{dt} = - 6x_{1} + 8u $$

$$ \dot{x_{1}} = -3x_{1} + 4u $$

$$ A = \begin{bmatrix} -3,0\\ 0,0 \end{bmatrix} B = \begin{bmatrix} 4\\ 0 \end{bmatrix} $$

$$ \text{now on both we have the first row, and just need to solve for}x_{1}\text{ and }x_{2}\text{ in the next equation} $$

$$ 3\frac{dx_{2}}{dt} = - 6x_{1} - 9x_{2} $$

$$ \dot{x_{2}} = -3x_{2} -2x_{1} $$

$$ \text{This finally gives us the completed input and state matrices} $$

$$ A = \begin{bmatrix} -3,0\\ -3,-2 \end{bmatrix} B = \begin{bmatrix} 4\\ 0 \end{bmatrix} $$

$$ \text{The 2nd row of B is left 0 because }\dot{x_{2}}\text{ does not contain any variance from inputs} $$

Although these aren't very useful, so let's imagine a simple physical scenario of a mass damper. This mass damper has some force acting on it F(t) driving it forward. It also has a resistive damping force from the ground f. Before we get further I'll be using newtonian notation for this next part, if you do not know what that is it's pretty simple

$$ x = \text{position} $$

$$ \dot{x} = \text{velocity} $$

$$ \ddot{x} = \text{acceleration} $$

So we know from Newton's 2nd law that F = ma, we already will have the mass, which to keep it general we'll call m for now. We also know the 2 forces acting on the mass, which is the resistive force, and F(t). We want the damping constant, which we'll call b to scale up with velocity, because this is how real friction works, so the greater your velocity increases the greater the resistive force is.

From that we can derive this equation

$$ F(t) - b\dot{x} = m\ddot{x} $$

We can see the damping force subtracts from our forward force, accurately describing the forces. We also have to keep in mind the right hand side of the equation, which is mass * acceleration limiting this equation within physical bounds.

However this is a 2nd order differential equation, which is not only hard to solve, but not dealt with by State-Space, so how do we put this into state space? Well we have to separate out the states.

We need our variables to be actual measurable quantities, not derivatives, so we pick our 2 highest ones and try to step down, so velocity becomes position, and acceleration becomes velocity

$$ \bar{x} = \begin{pmatrix} x_{1}\\ x_{2} \end{pmatrix}= \begin{pmatrix} x\\ \dot{x} \end{pmatrix} $$

$$ \text{by definition }\dot{x}_{1} = x_{2} \text{ since differentiating } x_{1}=x \text{ gives } \dot{x}_{1} = \dot{x} = x_{2} $$

This makes things really easy as we already have one of our equations, now we need to derive the other one.

Remember our equation from before? we need to rearrange that to get acceleration

$$ \text{First I swap the lhs and rhs for cleanliness} $$

$$ m\ddot{x} = F(t) - b\dot{x} $$

$$ \text{Then to isolate }\ddot{x}\text{ we can divide by m} $$

$$ \ddot{x} = \frac{F(t)}{m} - \frac{b\dot{x}}{m} $$

$$ \text {Then we can do some stylistic changes just to make everything nice and tidy in standard form} $$

$$ \ddot{x} = -\frac{b}{m}\dot{x} + \frac{1}{m}F(t) $$

If you've done polynomials before it's the same rules for standard form, and then we just keep inputs and state more distinct here. That's besides the point though, we've now finally arrived at the final equation to find acceleration so now we have our 2 equations (the 2nd one has some of the states subbed in but is equivalent to above)

$$ \dot{x_{1}} = x_{2} $$

$$ \dot{x_{2}} = -\frac{b}{m}x_{2} + \frac{1}{m}F(t) $$

$$ \text{which we can now solve the first one for our first row of A which is} $$

$$ (0,1) $$

$$ \text{since this has no input term the first row of B is 0 } $$

$$ \text{and now our second one is pretty much already set up for us with the 2nd row of A being} $$

$$ (0,-\frac{b}{m}) $$

$$ \text{and the 2nd row of b being} $$

$$ (\frac{1}{m}) \text{Now all together!} $$

$$ A = \begin{bmatrix} 0,1\\ 0,-\frac{b}{m} \end{bmatrix} B=\begin{bmatrix} 0\\ \frac{1}{m} \end{bmatrix} $$

Tying it together: How is this useful in dynamic systems?

Differential equations are a great way of representing dynamic systems, but are computationally hard, this gives us a computationally easy way to represent them linearly with matrices, a very easy (but tedious) and optimized problem in computer science.
We can use these states combined with laws of physics to actually create our modeling differential equations. This matrix form allows for much easier observation, stability analysis, and many control algorithms, like LQR, Kalman filters, and MPC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment