How to generate reference trajectory for hidden states? I would like to implement control law on a model that has states that are estimated from measurements.
$$
u = -K\,x + G\,r
$$
However, I'm confused about how to generate reference signal $r$. Not to mention finding the matrix $G$. Which would be inverse of measurement matrix $C$, but I don't have all states on the output and $C$ is not square. 
I can easily generate trajectory for two of the states, however the two remaining states are only there to make the model behave more precisely and they are not easily understood because the model is result of model fit. I have done experiments with running a separate observer that observes the two generated reference states and estimates the other two. However this does not seem right. 
If the control law is used for control then how is the reference trajectory usually generated and mapped to the full state vector $x$ so that it can be used with the control law? I don't want all states to go to zero after all, I want them to track a trajectory defined by two out of four states in $x$. 
 A: The signal $r$ is a desired value for the output of your system $y$. A common reference might be a series of steps, for example the first $T_1$ time units it is desired that the output is zero, then the next $T_2$ time units it is desired that the output is some other constant, ect. However in general the reference signal can be any real signal in time. But when feedback is combined with feed-forward the reference is often chosen to be sufficiently smooth, such that some of its time derivatives are continuous and known. But in general the reference signal is free to be chosen by you, the operator.

Usually the matrix $G$ is defined such that in steady state, which is assumed to be stable ($A-B\,K$ is Hurwitz), the output of the state space model $y$ converges to $r$. Is also important to note that this does assume that $r$ is constant. This statement can be formulated in an equation as follows
$$
\begin{bmatrix}
\dot{x} \\ y
\end{bmatrix} = 
\begin{bmatrix}
A & B \\ C & D
\end{bmatrix}
\begin{bmatrix}
x_r \\ u_r
\end{bmatrix} = 
\begin{bmatrix}
0 \\ r
\end{bmatrix}
$$
where $x_r$ and $u_r$ are the state vector and input in steady state respectively, such the corresponding $y$ is equal to $r$. In order to solve for what the state vector $x_r$ and input $u_r$ should be in steady state, given as constant reference $r$, one can use
$$
\begin{bmatrix}
x_r \\ u_r
\end{bmatrix} = 
\begin{bmatrix}
A & B \\ C & D
\end{bmatrix}^{-1}
\begin{bmatrix}
0 \\ I
\end{bmatrix} r
$$
this only work if the matrix consisting of $A$, $B$, $C$ and $D$ is invertible.
In order to assure that the $u=u_r$ when $x=x_r$ the control law can be adjusted to
$$
u = -K\,(x - x_r) + u_r
$$
which after substitution of the solution for $x_r$ and $u_r$ gives
$$
\begin{align}
u&= -K\,x + 
\begin{bmatrix}
K & I
\end{bmatrix}
\begin{bmatrix}
x_r \\ u_r
\end{bmatrix} \\
&= -K\,x + 
\underbrace{
\begin{bmatrix}
K & I
\end{bmatrix}
\begin{bmatrix}
A & B \\ C & D
\end{bmatrix}^{-1}
\begin{bmatrix}
0 \\ I
\end{bmatrix}}_G r.
\end{align}
$$
But if your model is not exactly equal to that of the plant you are trying to control then you are very likely to have a steady state error. In such a case you could maybe use adaptive control or extend the state with an integral, such as LQI.
