SpaceX’s self landing rocket. Photo by yahoo.com

Using an MPC to control a system: Discretizing the state-space equation system

Tiago Miguel
8 min readNov 7, 2020

--

Last time we discussed what controllers are and some examples where they are used. We also defined a system to control and deduced two important equations that allow us to model it:

In this post we are proceeding with some more manipulations and then we are going to discretize the model since computers don’t work in a continuous fashion. Finally we deduce the algebraic form for the predictions within a certain horizon of samples.

But why more manipulations? Currently the equations still don’t fit into the canonical form of a State-Space Equation System (SSES). This system is going to be quickly explained at the end of its deduction. Let’s see how we can shape them to the correct form.

State-Space Equation System

What we’ll do first is the following change:

This is a simple change that gives the first equation the shape it needs. Note that both temperatures can be separated into two different terms in the equation, which makes the transformation into algebraic form more complicated and can be avoided by aggregating both temperatures.

But what’s the implication? Whilst the reference for T_o is 0 °C, the reference for T is T_i. So if T_i is 10 °C, meaning the solution is entering in the vessel at that temperature, and we want it to heat up to 80 °C, we set our reference point to 70 because it is a difference of 70 °C from 10 to 80 °C. That’s it!

For the second equation we face the same problem. We need to have our control input be multiplied by a constant. If we unfold the pressures we get one term too many.

A new variable θ will be our control input and will cover the pressure ranges that p can take by creating a linear relationship between the lowest and greatest value. We set p_o to be 1 atm and p_i to be the greatest pressure value, 1.2 atm for instance. We can see that p will be 0.8 atm when θ is -1 and 1.2 atm when θ is 1.

The system equations are now in the desired form. We have two states: V dot and T and a control input θ. These are the variables of our system. It is time to transform the equations into algebraic form.

For simplicity’s sake, let’s turn the parameters into the letters a, b and c. Can you unfold the algebraic system of equations to match the two equations we had before?

You may see it more clearly now that the purpose of these equations is to understand how the system changes in time based on the current states by calculating their variation w.r.t. time.

Also, it sould be mentioned that this system of equations does not need to take this form. What I mean is that both equations may be more complex and contain all states and control input in them like the following examples:

Consider these systems. K, J and R are some random states and ξ a random control input. You can see that all states have their dotted counterparts and that the matrices of parameters may be more or less sparse. Instead of only one control input, there might be various other variables that help control the system.

For instance, in the system we are working with, we may increase the steam’s flowrate but we could also decrease the solution’s fowrate. This would allow the same mass of solution to recieve energy from the steam for a longer time and heat up more.

Now we need only add one more equation. A very simple one that will isolate T since it is the variable that we are controlling for and we’ll need it to calculate the error later on.

Let’s join both systems together and make some changes for the sake of ease of visualisation.

where:

We have finally derived our State-Space Equation System (SSES), in which the first equation describes how our system behaves and the second isolates the controlled state. The transformation of θ into a vector containing its value is just to represent the SSES in a more general way to be applicable to systems with two or more control inputs.

Discretizing the SSES and the Forward Euler method

The SSES as we have it now is function of a continuous time variable. However, machines work in a discrete fashion and instead of dealing with time, we have to deal with samples that are a discrete snapshot in time of the value of the states. So, instead of calculating how the state changes w.r.t. time, we are calculating its value one sample of time later.

To calculate the states one sampleof time later, we are using the Forward Euler method. This method says that in an infinitesimally small variation, the system variation can be linearly approximated.

If we make our time samples (t_s) small enough they could fit the method well. The subscript K stands for the sample number. Let’s substitute it in our system equation.

I is the identity matrix of the same shape of vector X_K. By changing the notation we can make this expression closer to what we had before. We can also represent the second equation of our SSES in a discrete form even though nothing changed.

where:

Great! We have discretized our system. We can now calculate the value of the states a time sample later. How can we extend it to make an horizon of time samples greater than the first?

Horizon of predictions

Before we go into the calculation of the horizon of predictions let’s get the difference between iterations and predictions clear. First, in each iteration there is an actual variation in our system. Each prediction is the how the model predicts the system to change with the following iterations that haven’t happened yet.

Let’s discuss this further with an example of a rocket doing a vertical landing.

Height predictions of a self landing rocket during three iterations. Figure by author.

At every iteration (three are shown with different colours and represented by the first subscript), our controller calculates an horizon of seven predictions (represented by the second subscript), using seven control inputs and the states at the instant zero (where the rocket is).

We take the first control input (θ_1,0) to produce the predicted change for the next iteration and discard the remaining predictions.

Because models aren’t perfect, if we actually had a real world system, we would see a difference between the measured height by a sensor attached to the rocket and what the model had predicted. So X_1,1 isn’t really equal to X_2,0.

However, since we are not making real world measurements, we assume we have a good enough model so that X_1,1 is equal to X_2,0. Well, at least for now.

So, at the new iteration, after we used θ_1,0 to calculate X_2,0, we repeat the process. We calculate a new set of control inputs that the model believes to be the new best and use the first value to calculate the states for the next iteration.

Now that we understand how the variables are intertwined, let’s see it in an algebraic form.

To extend the horizon of predictions we need only take the prediction for the sample number K+1 and use it to calculate sample K+2. Same for the sample K+3 and so on. If we have an horizon of three samples, we’ll have the following:

We want to calculate the predictions based only on the current iteration (K). This is important because the way the MPC works is that for every iteration, a new set of predictions is calculated like it was discussed with the rocket example.

Can you see a pattern? The generalization for the (K+N)th time sample would be:

And the generalization of all N time samples of the horizon is:

These are the calculations for the predictions, not the change in iteration. Now we need to pick θ_0 from this iteration and apply it to our real world system to create the changes of the states that our model tells us would be best in order to decrease the error. After these changes are applied, we enter a new iteration, calculate a new vector of control inputs and, along with the new states, the predictions. Then we take our new θ_0, apply it to our real world system and make changes to our states. And now it’s rinse and repeat.

Wrapping up

And there you have it. We went from giving the system equations the proper form, creating the SSES, discretizing it and to being able to predict values of the states for a certain horizon of time samples.

Next time we are going to make slight changes to the control input so that it makes more sense in the perspective of the real world and we are going to close the loop by solving an objective funtion that deals with the error to calculate the vector of control inputs. Finally we’ll see how we apply the control input to integrate our OLS and change states.

I hope the explanations are clear enough. I know it is alot of theory to take in. Read as many times as you want and don’t be affraid to ask questions if you need to!

Thanks for reading and stay tuned!

--

--

Tiago Miguel

Chemical Engineer enthusiast in learning programming with python, data science and machine/deep learning skills