# How to choose reference signal in a state space model?

I am a bit confused about choosing reference signal in order to control state space models.

I have read(however without deep math explanation) that one has to scale his reference signal for to make system to be able to track input signal. Also there is a function which gives a scaling coefficient Nbar, so I have to multiply my reference signal by it:

s = size(A,1);
Z = [zeros([1,s]) 1];
N = inv([A,B;C,D])*Z';
Nx = N(1:s);
Nu = N(1+s);
Nbar=Nu + K*Nx;

In my particular case there is the model of a pendulum on a one dimensional cart with arbitrary chosen poles. My state variables are ${\vec{\textbf{x}}} = [x\ \dot{x}\ \theta\ \dot{\theta}]^{T}$.

Below is matlab code:

clear all;

M = 1;
m = 1;
l = 1.5;
g = 9.8;
I = m*l^2;
b = 0.05;

denom = M*(m*l^2) + I*(m + m);

a22 = -(b*I + b*m*l^2)/denom;
a23 = (g*(l^2)*(m^2))/denom;
a42 = -(b*l*m)/denom;
a43 = (g*(M+m)*l*m)/denom;

b21 = (I + m*l^2) / denom;
b41 = l*m / denom;

A = [0 1 0 0; 0 a22 a23 0; 0 0 0 1; 0 a42 a43 0];
B = [0; b21; 0; b41];
C = [1 0 0 0];
D = 0;

% Check for controllability
co = ctrb(A, B);
fprintf("%f\r\n", rank(co));

% POLES
P = [-1.5 -0.9 -2.5 -3.5];

% Placing poles
K = place(A, B, P);

% Reference signal rescaling
sys = ss(A, B, C, D);
N = rscale(sys, K);

And position plot:

Everything works like a charm. However I totally miss the intuition behind that.

Imagine standard PID control, in this case if I would like to control position I would set the error to ´current_position - desired_position´ and apply standard PID formula to that error. From the above it is intuitively clear why position is controlled and if I want to control, for example, velocity I will just set error to velocity divergence.

However in a state space I have some coefficient which arises after calculating dot product between my state and gain matrix. And for some reason after subtracting it from a scaled version of u I get controlled position. Why is a big secret for me. And how would one control any other state variable in this case, like velocity for example?

• Position is the most practical one, that's the reason. If you want to control speed or acceleration, you can do so. But first, please consider the physical scope of the system, and what you would like to accomplish. – Gürkan Çetin Oct 5 '17 at 19:21
• @GürkanÇetin I cleary understand that I can do so. The question is how. – Long Smith Oct 5 '17 at 19:30
• Ok I misunderstood maybe. Because the first question in the last paragraph asks which parameter to control, not how to do that. Did you try replacing x with x_dot? What was the result? If you specify a little bit more I'm sure the question and answers will be more useful for everyone. Otherwise I fear it may be classified as "too broad". – Gürkan Çetin Oct 5 '17 at 19:35
• @GürkanÇetin actually this question may sound dually. What I meant is that I cannot see any reason why exactly position is controlled. Imagine standard PID control, in this case if I would like to control position I would set the error to (current_position - desired_position) and apply standard PID formula to that error. From the above it is intuitively clear why position is controlled and if I want to control, for example, velocity I will just set error to velocity divergence. – Long Smith Oct 5 '17 at 19:57
• @GürkanÇetin However in a state space I have some coefficient which arises after calculating dot product between my state and gain matrix. And for some reason after subtracting it from a scaled version of u we get controlled position. I just miss intuition behind that. – Long Smith Oct 5 '17 at 20:00

So assuming that we are dealing with LTI systems of the form

\begin{align} \dot{x} &= A\,x + B\,u \\ y &= C\,x + D\,u \end{align} \tag{1}

with $x\in\mathbb{R}^{n_x}$, $u\in\mathbb{R}^{n_u}$, $y\in\mathbb{R}^{n_y}$ and which is stabilizable and detectable, however I will assume that the full state is known so no need for constructing an actual observer. The goal is to find a control law for $u$ such that as time goes to infinity the steady state of the output, $y_{ss}$, is equal to a known constant reference, denoted by $r$.

Normally when you use state feedback of the form $u = -K\,x$ such that $A-B\,K$ is Hurwitz, then the only thing that this will do is force the full state $x$ to zero and in turn the the output $y$ as well. So this would not solve the problem. In order to solve the problem stated above one also needs to find what the steady state of the full state, $x_{ss}$, and input, $u_{ss}$, would need to be such that $y_{ss} = r$. Since the model is detectable $\dot{x}$ should be zero when $y = r$. For now I will assume that $x_{ss}$ and $u_{ss}$ can be expressed as a linear combination of $y_{ss}$ and thus $r$, so $x_{ss} = M_x\,r$ and $u_{ss} = M_u\,r$. When we start to solve the problem you will see that such matrices can exists which solve the problem. Namely if we plug this into equation $(1)$ we get

\begin{align} 0_{n_x\times n_x} &= (A\,M_x + B\,M_u)\,r \\ r &= (C\,M_x + D\,M_u)\,r. \end{align} \tag{2}

But this should be solvable for every $r$. So factoring out $r$ then this can be rewritten as

$$\begin{bmatrix} A & B \\ C & D \end{bmatrix} \begin{bmatrix} M_x \\ M_u \end{bmatrix} = \begin{bmatrix} 0_{n_x\times n_x} \\ I_{n_y\times n_y} \end{bmatrix} \tag{3}$$

which can be solved with

$$\begin{bmatrix} M_x \\ M_u \end{bmatrix} = \begin{bmatrix} A & B \\ C & D \end{bmatrix}^{-1} \begin{bmatrix} 0_{n_x\times n_x} \\ I_{n_y\times n_y} \end{bmatrix}. \tag{4}$$

This has a solution if the matrix that needs to be inverted is full rank and if $n_y \leq n_u$ (when $n_y < n_u$ you could need to use the right inverse). If $n_y > n_u$ then it might still be solvable, but not for every $r$.

Assuming equation $(4)$ can be solved then by defining a new state variable $\hat{x} = x - x_{ss}$ and new control input $\hat{u} = u - u_{ss}$, then if a control law exist that brings $\hat{x}$ to zero, then $x$ will go to $x_{ss}$ as well by definition. The time derivative of $\hat{x}$ can be expressed as follows

$$\dot{\hat{x}} = \dot{x} - \dot{x}_{ss} = A\,x + B\,u - \underbrace{(A\,x_{ss} + B\,u_{ss})}_{0} = A\,(x - x_{ss}) + B\,(u - u_{ss}) = A\,\hat{x} + B\,\hat{u}$$

As stated before using the control law $\hat{u} = -K\,\hat{x}$ such that $A-B\,K$ is Hurwitz, should force $\hat{x}$ to zero. By combining the definitions for $\hat{x}$, $\hat{u}$ and the control law for $\hat{u}$ one can solve for $u$

$$u = -K\,x + K\,x_{ss} + u_{ss}. \tag{5}$$

Substituting in the expressions for $x_{ss}$ and $u_{ss}$ as a function of $r$ gives

$$u = -K\,x + \underbrace{(K\,M_x + M_u)}_N\,r. \tag{6}$$

Using equation $(4)$ then the gain multiplied by $r$ can also be expressed as follows

$$N = \begin{bmatrix} K & I_{n_u \times n_u} \end{bmatrix} \begin{bmatrix} A & B \\ C & D \end{bmatrix}^{-1} \begin{bmatrix} 0_{n_x\times n_x} \\ I_{n_y\times n_y} \end{bmatrix}. \tag{7}$$

So this gain $N$ will change by choosing a different $K$. However it can be noted that if you design a $K$ and corresponding $N$ based on a state space model whose dynamics not exactly matches the system you are using the control law on, due to some error during the identification of this system, then the steady state error might not go to zero as time goes to infinity. A solution for this might be to extend the state space by the integral of the error $y-r$, such as the LQI controller.