\input{ccs_gyak_defs}

\title{Computer controlled systems}
\author{Lecture 7, March 31, 2017}
\date{version: {\today.} -- {\currenttime} [$\beta$ version]}

\newcounter{tmp}
\begin{document}
\selectlanguage{english}
\maketitle


% \section*{Exercises (without solutions)}
% \begin{enumerate}
% \end{enumerate}

\section*{Pole-placement controller}

% Cél: a rendszer pozitív valósrészű sajátértékeit a negatív tengelyre tolni negatív állapot-visszacsatolással.

\subsection*{Pole-placement controller based on Bass-Gura formula}
$$\TODOx{$K=(\underline{\alpha} -\underline{a})T_l^{-1}\mathcal{C}^{-1}$}$$
where $\underline{\alpha}$ is the expected (prescribed) characteristic polynomial of the closed-loop system,
% az elvárt karakterisztikus polinom együtthatóvektora,
$\underline{a}$ is the characteristic polynomial of the original (uncontrolled) system,
% az eredeti rendszer karakterisztikus polinomjának együtthatóvektora,
$\mathcal{C}$ is the controllability matrix,
finally $T_l$ is the following Toeplitz matrix:
\begin{eqnarray*}
T_l = \begin{pmatrix}
1 & a_1 & a_2 & \cdots & a_{n-1} \\
0 & 1 & a_1 & \cdots & a_{n-2} \\
0 & 0 & 1 & \cdots & a_{n-3} \\
\cdot & \cdot &\cdot & \cdots & \cdot
\end{pmatrix}
\end{eqnarray*}

\begin{cexamplew}*{}\label{}
Design a pole-placement controller for the following CT LTI SISO system:
$$
A=
\begin{pmatrix}
2 & -2 \\ 0 & 1
\end{pmatrix}
\quad
B=
\begin{pmatrix}
1 \\ 2
\end{pmatrix}
\quad
C=
\begin{pmatrix}
1 & 1
\end{pmatrix}
$$
\emph{Solution.}
\begin{eqnarray*}
a(s) = s^2-3s+2 \\
a_1 = -3 \\
a_2 = 2
\end{eqnarray*}
The prescribed characteristic polynomial ($\phi_c(s)$):
\begin{eqnarray*}
\alpha(s) = s^2+3s+2 \\
\alpha_1 = 3 \\
\alpha_2 = 2
\end{eqnarray*}
A Toeplitz matrix and the controllability matrix in this case are
\begin{equation*}
    \begin{aligned}
        &T_l = \begin{pmatrix}
        1 & a_1 \\
        0 & 1
        \end{pmatrix} =
        \begin{pmatrix}
        1 & -3 \\
        0 & 1
        \end{pmatrix}  \\
        &T_l^{-1} = \begin{pmatrix}
        1 & 3\\
        0 & 1
        \end{pmatrix}
    \end{aligned} ~~~~~~~~~
    \begin{aligned}
        &\mathcal{C} = \pmqty{1 & -2 \\ 2 & 2} \\
        &\mathcal{C}^{-1} = \frac16 \pmqty{2 & 2 \\ -2 & 1}
    \end{aligned}
\end{equation*}
Than the static state feedback will be the following:
\begin{eqnarray*}
K = \begin{pmatrix}
3 -(-3) & 2 - 2
\end{pmatrix} \begin{pmatrix}
1 & 3 \\
0 & 1
\end{pmatrix} \frac16 \pmqty{2 & 2 \\ -2 & 1} = \begin{pmatrix}
-4 & 5
\end{pmatrix}
\end{eqnarray*}
\end{cexamplew}

\subsection*{Ackermann formula}
% Az Ackermann képlet csak akkor használható, ha az előírt karakterisztikus polinom gyökei egyszeres multiplicitásúak.
$$\TODOx{$K=[0 \,0\, \cdots \,0 \,1]\mathcal{C}_n^{-1}\phi_c(A)$}$$
where $\phi_c(s)$ is the prescribed characteristic polynomial of the closed-loop (controlled) system.
In the previous example, it was denoted by $\alpha(s) = \phi_c(s)$.

\begin{cexamplew}*{}\label{}
    Design a pole-placement controller for the following CT LTI SISO system:
    $$
    A=
    \begin{pmatrix}
    2 & -2 \\ 0 & 1
    \end{pmatrix}
    \quad
    B=
    \begin{pmatrix}
    1 \\ 2
    \end{pmatrix}
    \quad
    C=
    \begin{pmatrix}
    1 & 1
    \end{pmatrix}
    $$
    \emph{Solution.}
    $$
    \mathcal{C}_2=
    \begin{pmatrix}
    B & AB
    \end{pmatrix}
    =
    \begin{pmatrix}
    1 & -2 \\ 2 & 2
    \end{pmatrix}
    \rightarrow
    \mathcal{C}_2^{-1}=
    \begin{pmatrix}
    \frac{1}{3} & \frac{1}{3} \\ -\frac{1}{3} & \frac{1}{6}
    \end{pmatrix}
    $$
    Legyen $\lambda_1=-1$ és $\lambda_2=-2$.
    $$
    \phi_c=(s-\lambda_1)(s-\lambda_2)=s^2+3s+2
    $$
    $$
    \phi_c(A)=A^2+3A+2I=
    \begin{pmatrix}
    12 & -12 \\ 0 & 6
    \end{pmatrix}
    $$
    $$K=
    \begin{pmatrix}
    0 & 1
    \end{pmatrix}
    \begin{pmatrix}
    \frac{1}{3} & \frac{1}{3} \\ -\frac{1}{3} & \frac{1}{6}
    \end{pmatrix}
    \begin{pmatrix}
    12 & -12 \\ 0 & 6
    \end{pmatrix} =
    \begin{pmatrix}
    -4 & 5
    \end{pmatrix}$$
    Check
    $$
    A-BK =
    \begin{pmatrix}
    2 & -2 \\ 0 & 1
    \end{pmatrix}
    -
    \begin{pmatrix}
    1 \\ 2
    \end{pmatrix}
    \begin{pmatrix}
    -4 & 5
    \end{pmatrix}
    =
    \begin{pmatrix}
    6 & -7 \\ 8 & -9
    \end{pmatrix}
    $$
    $$\det(\lambda I - (A-BK))=\lambda^2+3\lambda+2$$
    Namely, the poles of the obtained closed-loop system are indeed the prescribed values.
\end{cexamplew}

\begin{cexamplew}*{}\label{}
    Design a pole-placement controller for the following CT LTI SISO system:
    $$
    A=
    \begin{pmatrix}
    2 & -1 \\ 3 & -2
    \end{pmatrix}
    \quad
    B=
    \begin{pmatrix}
    1 \\ 0
    \end{pmatrix}
    \quad
    C=
    \begin{pmatrix}
    1 & 1
    \end{pmatrix}
    $$
    \emph{Solution.}
    $$
    \mathcal{C}_2=
    \begin{pmatrix}
    B & AB
    \end{pmatrix}
    =
    \begin{pmatrix}
    1 & 2 \\ 0 & 3
    \end{pmatrix}
    \rightarrow
    \mathcal{C}_2^{-1}=
    \begin{pmatrix}
    1 & -\frac{2}{3} \\ 0 & \frac{1}{3}
    \end{pmatrix}
    $$
    Let $\lambda_1=-1$ and $\lambda_2=-2$.
    $$
    \phi_c=(s+\lambda_1)(s+\lambda_2)=s^2+3s+2
    $$
    $$
    \phi_c(A)=A^2+3A+2I=
    \begin{pmatrix}
    9 & -3 \\ 9 & -3
    \end{pmatrix}
    $$
    $$K=
    \begin{pmatrix}
    0 & 1
    \end{pmatrix}
    \begin{pmatrix}
    1 & -\frac{2}{3} \\ 0 & \frac{1}{3}
    \end{pmatrix}
    \begin{pmatrix}
    9 & -3 \\ 9 & -3
    \end{pmatrix}
    =
    \begin{pmatrix}
    3 & -1
    \end{pmatrix}$$
    Check:
    $$
    A-BK =
    \begin{pmatrix}
    2 & -1 \\ 3 & -2
    \end{pmatrix}
    -
    \begin{pmatrix}
    1 \\ 0
    \end{pmatrix}
    \begin{pmatrix}
    3 & -1
    \end{pmatrix}
    =
    \begin{pmatrix}
    -1 & 0 \\ 3 & -2
    \end{pmatrix}
    $$
    $$\det(\lambda I - (A-BK))=\lambda^2+3\lambda+2$$
    Indeed, the poles of the closed loop system are the prescribed values.
\end{cexamplew}


\begin{cexamplew}*{}[breakable=false]\label{}
    Given the following CT LTI SISO systems
    \begin{multicols}{2}
    \begin{enumerate}
        \item~~
        $
            \begin{dcases}
                ~ \dot x = \pmqty{2 & 0 \\ 9 & -3} x + \pmqty{0 \\ 3} u \\
                ~ y = \pmqty{1 & 1} x
            \end{dcases}
        $

        \item~~
        $
            \begin{dcases}
                ~ \dot x = \pmqty{2 & 0 \\ 9 & -3} x + \pmqty{\Hiba{2} \\ 3} u \\
                ~ y = \pmqty{1 & 1} x
            \end{dcases}
        $

    \end{enumerate}
    \end{multicols}
    Design a state feedback controller (if it is possible), that stabilizes the system!
\end{cexamplew}

\begin{cexamplew}*{}\label{}
    Given the following CT LTI SISO system
    $H(s) = \frac{2s - 4}{s^2 + s - 6}$.
    \begin{enumerate}
        \item Is the system asymptotically stable?
        \item If it is possible, design a controller, that shifts the system's poles to $p_1 = -3$ and $p_2 = -5$!
        Hint: controllability normal form.
    \end{enumerate}
\end{cexamplew}

\section*{Linear state observer design}
\textbf{Goal}: computation of the values of the non-measured state variables of the system using the observed output.

The dynamical system
$$\frac{\mathrm{d}\hat{x}}{\mathrm{d} t} = F\hat{x} + Ly + Hu$$
is called a full order state observer, if the error dynamics
$e =  x - \hat{x}$ tends to zero, i.e. $\lim \limits_{t \rightarrow \infty}e=0$
\\
In case of an LTI system:
$$\dot{x} = Ax + Bu$$
$$y = Cx$$

$$\dot{e} = \dot{x} - \dot{\hat{x}} = A x + B u - F \hat{x} - Ly - Hu + Fx - Fx = $$
$$ = Ax + Bu - F\hat{x} - LCx - Hu + Fx - Fx = $$
$$ =(A-LC-F)x + (B-H)u + F(x-\hat{x}) = (A-LC-F)x + (B-H)u + F(e)$$
\\
Let $F = A - LC$ and $H=B$
\\
Than $\dot{e}=Fe$
\\
We require that the system be asymptotically stable, namely the real part of the roots of the characteristic polynomial $\det(sI-(A-LC))$ be negative.
$$\det(sI-(A-LC))=\det(sI-(A^T -C^T L^T))$$
We can observe that the state observer design can be traced back to a pole placement problem of $(A',B')$, where $A' = A^T$, $B' = C^T$, and the result $(K)$ of the pole placement should be interpreted as $L=K^T$.

\pagebreak



\begin{cexamplew}*{}\label{}
Design a state observer for the following CT LTI SISO system
$$
A=\begin{pmatrix}
-3 & 1 \\
2 & -1
\end{pmatrix}
\quad
B=\begin{pmatrix}
1 \\
-1
\end{pmatrix}
\quad
C=\begin{pmatrix}
0 & 1
\end{pmatrix}$$
\emph{Solution.} \\
Let the characteristic polynomial of the closed-loop system: $\phi_o(s) = (s + 3)(s + 3)$\\
In order to use the Ackermann, formula we should substitute $A' = A^T$ into $\phi_o(s)$:
$$\phi_o(A')=\begin{pmatrix}
2 & 4 \\
2 & 6
\end{pmatrix}$$
If $B'=C^T$, the obtained controllability matrix for $(A',B')$ (which is actually the transpose of the observability matrix of $(A,C)$) is:
$$\mathcal{C}_2' =
\begin{pmatrix}
0 & 2 \\
1 & -1
\end{pmatrix}$$
Its inverse will be:
$$\qty(\mathcal{C}'_2)^{-1} =
\begin{pmatrix}
1/2 & 1 \\
1/2 & 0
\end{pmatrix}$$
Finally, we compute the feedback gain $K$:
$$K=
\begin{pmatrix}
0 & 1
\end{pmatrix}
\begin{pmatrix}
1/2 & 1 \\
1/2 & 0
\end{pmatrix}
\begin{pmatrix}
2 & 4 \\
2 & 6
\end{pmatrix}
=
\begin{pmatrix}
1 & 2
\end{pmatrix}$$
From this:
$$L=K^T=\begin{pmatrix}
1 \\ 2
\end{pmatrix}
\quad
F=A-LC=
\begin{pmatrix}
-3 & 0 \\
2 & -3
\end{pmatrix}
\quad
H=
\begin{pmatrix}
1 \\
-1
\end{pmatrix}
$$
\end{cexamplew}

\begin{cexamplew}*{}\label{}
Design a state observer for the following CT LTI SISO system
$$
A=\begin{pmatrix}
2 & 1 \\
1 & -2
\end{pmatrix}
\quad
B=\begin{pmatrix}
1 \\
1
\end{pmatrix}
\quad
C=\begin{pmatrix}
1 & 0
\end{pmatrix}$$
\end{cexamplew}


\begin{cexamplew}*{}\label{}
Design a state observer AND a stabilizer state feedback controller for the following CT LTI SISO system.
$$
A=\begin{pmatrix}
2 & -1 \\
3 & -2
\end{pmatrix}
\quad
B=\begin{pmatrix}
1 \\
0
\end{pmatrix}
\quad
C=\begin{pmatrix}
1 & 0
\end{pmatrix}$$
\textbf{Separation principle}: the observer gain $L$ and the feedback gain $K$ can be designed separately.
\end{cexamplew}

\pagebreak
\section*{Optimal state feedback controller - LQR controller design}

% \subsection*{Basics of variational calculus}

% Application: Search for extremal values in vector spaces.

% $x(t) \rightarrow x(\alpha,t)=x(t)+\alpha\eta(t)$, where $\alpha \in \mathbb{R}$

% $u(t) \rightarrow u(\beta,t)=x(t)+\beta\gamma(t)$  where $\beta \in \mathbb{R}$

% $$\frac{\partial x(\alpha, t)}{\partial \alpha} = \eta(t) $$
% $$\frac{\partial u(\beta, t)}{\partial \beta} = \gamma(t) $$
% $$\eta(t_0) = \eta(t_1) = \gamma(t_0) = \gamma(t_1)=0$$

% \subsection*{LQR szabályozó tervezése}

We want to minimize the following functional:
$$J(x,u)=\frac{1}{2} \int_0^T x^T Q x + u^T R u \ dt$$
where $Q$ and $R$ are positive definite symmetric matrices.
In case of LTI systems this problem can be traced back to a CARE (continuous-time algebraic Riccati equation):
$$\TODOx{$KA + A^T K - KBR^{-1}B^T K + Q = 0$}$$
The system can be stabilized with the $u=-Gx$ state feedback, where
$$\TODOx{$G=R^{-1}B^T K$}$$

\begin{cexamplew}*{}\label{}
    Design an optimal LQR controller for the following system:
    $\dot{x}=2x+u$, i.e $A = 2, B = 1$. \\
    \emph{Solution.}
    We minimize the following functional:
    $$J=\frac{1}{2} \int 5x^2+u^2 dt$$
    meaning that in our case $Q=5$ and $R=1$.
    In this case (first order system -- only one single state variable) the CARE will have the following form:
    $$-K^2+4K+5=0$$
    The solutions for $K$ are $5$ and $-1$. By definition, we should choose the positive one, otherwise, we obtain a positive feedback.
    $$G=1 \cdot 1 \cdot 5=5$$
    Finally, the computed state feedback:  $u=-5x$.
\end{cexamplew}

\end{document}
