The previous chapter introduced the robot localization problem, but assumed that the map $\m$ was \textit{given}. However, in many real-world robotics applications a map might not be known ahead of time, and therefore it wold need to be built on-the-fly. This problem, which involves using information about measurements $\z$ and controls $\bu$ to simultaneously localize the robot in the world and build a map, is known as \textit{simultaneous localization and mapping} (SLAM)\cite{ThrunBurgardEtAl2005}.

\notessection{Simultaneous Localization and Mapping (SLAM)}
Many real-world settings are challenging for robotic autonomy because both the map and the relative pose of the robot are unknown. For example, such a situation would occur in autonomous search-and-rescue operations where a robot needs to explore an unknown environment. The SLAM problem addresses this challenge by estimating the robot pose and constructing a map of the environment at the same time, based only on measurement $\z_{1:t}$ and control $\bu_{1:t}$ data.

Generally speaking there are two types of SLAM problems that can be considered. The \textit{online} SLAM problem aims to estimate the posterior $p(\x_t, \m\mid \z_{1:t}, \bu_{1:t})$ over the robot's current pose $\x_t$ and the map $\m$. Alternatively, the \textit{full} SLAM problem estimates the entire path of the robot instead of just the current position, namely $p(\x_{1:t}, \m\mid \z_{1:t}, \bu_{1:t})$. The difference between these two SLAM problems is demonstrated graphically in Figure \ref{fig:online_full_slam}. Both SLAM problems experience the same challenge: error in the pose causes error in map estimation and error in map estimation causes error in the pose estimate. In this chapter, algorithms for both the online and full SLAM problems are studied.
\begin{figure*}[ht]
    \centering
    \includegraphics[width=.75\textwidth]{tex/figs/ch17_figs/online_full_slam.png}
    \caption{Difference between online and full SLAM, where online SLAM only estimates the current robot pose while full SLAM also estimates the robot's history.}
    \label{fig:online_full_slam}
\end{figure*}

\subsection{EKF SLAM Algorithm}
One of the earliest approaches to the online SLAM problem leverages the extended Kalman filter, and is essentially an extension of the EKF localization algorithm discussed in the previous chapter. Again, the key aspects to the approach are the exploitation of Gaussian distributions to model the robot's belief distribution $bel(\x_t)$, and state transition and measurement models.
It will also be assumed that the map is feature-based:
\begin{equation*}
\m = \{m_1,m_2,\dots ,m_N\},
\end{equation*}
where $m_i$ is the $i$-th landmark with coordinates $(m_{i,x}, m_{i,y})$. As in the EKF localization problem, the measurement correspondences can either be assumed to be known or unknown (more common in practice).

The main idea behind EKF SLAM is that the coordinates $(m_{i,x}, m_{i,y})$ of each landmark $m_i$ are added, along with the robot pose $\x_t$, to an augmented state vector:
\begin{equation} \label{eq:augmentedstate}
\y_t = \begin{bmatrix}
\x_t \\ \m_1 \\ \vdots \\ \m_N
\end{bmatrix},
\end{equation}
where $\m_i = [m_{i,x}, m_{i,y}]^\top $. With the new state vector $\y$ the online SLAM problem is to compute the posterior:
\begin{equation*}
bel(\y_t) = p(\y_t \mid \z_{1:t}, \bu_{1:t}).
\end{equation*}

EKF SLAM approaches have the advantage of being computationally efficient such that they can be run online, and are also well understood from a theoretical perspective. They can also provide good performance when the uncertainty is low. However, their main disadvantages are that they are restricted by the Gaussian assumption to unimodal estimates, and that performance can degrade in settings with high uncertainty or when the states are not well approximated by normal distributions.

\subsubsection{State Transition and Measurement Models}
Assuming that the landmarks $m_i \in \m$ are static, the state transition model for the augmented state vector $\y$ is assumed to be given by:
\begin{equation*}
    \y_t = g(\bu_t, \y_{t-1}) + \bm{\epsilon}_t, \quad \bm{\epsilon}_t \sim \mathcal{N}(\bm{0},\bm{R}_t),
\end{equation*}
where the nonlinear vector function $g$ is defined by:
\begin{equation*}
g(\bu_t, \y_{t-1}) = \begin{bmatrix}
\tilde{g}(\bu_t, \x_{t-1}) \\ \m_{1,t-1} \\ \vdots \\ \m_{N,t-1}
\end{bmatrix},
\end{equation*}
and $\tilde{g}$ is the original robot motion model (e.g. differential drive robot model). The noise covariance is also defined as:
\begin{equation*}
\bm{R}_t = \begin{bmatrix}
\tilde{\bm{R}}_t & 0 \\
0 & 0
\end{bmatrix},
\end{equation*}
where $\tilde{\bm{R}}_t$ is the noise covariance associated with the original robot motion model and the rest of the matrix are zeros.
The Jacobian of the augmented motion model is defined as $G_t \coloneqq \nabla_{\y} g(\bu_t, \bmu_{t-1})$ where $\bmu_{t-1}$ is the expected value of the belief distribution $bel(\y_{t-1})$ at the previous time.

The measurement model is defined in the same way as the previous chapter:
\begin{equation*}
\z^i_t = h(\y_t, j) + \bm{\delta}_t,
\end{equation*}
where $\bm{\delta}_t \sim \mathcal{N}(\bm{0}, \bm{Q}_t)$ is Gaussian zero-mean noise and $j$ is the index of the map feature $m_j \in \m$ that measurement $i$ is associated with. The Jacobian is also defined in the same way with $H^{j}_t = \nabla_{\y}h(\bar{\bmu}_t,j)$, where $\bar{\bmu}_t$ is the predicted mean (that results from the EKF prediction step) of the distribution $\overline{bel}(\y_t)$.

\subsubsection{EKF SLAM with Known Correspondences}
As was the case in EKF localization, it is important to specify whether the the correspondences $c_t^i$ between the $i$-th measurement $\z_t^i$ and the associated landmark in the map is known. In this section an EKF SLAM algorithm will be developed which assumes the correspondences $\bc_t = [c_t^1, \dots ]^\top $ are \textit{known}.

\begin{algorithm}[ht]
 \KwData{$\bmu_{t-1}, \bSigma_{t-1}, \bu_{t}, \z_{t}, \bc_t$}
 \KwResult{$\bmu_t, \bSigma_t$}
 $\bar{\bmu}_t = g(\bu_t,\bmu_{t-1})$\\
 $\bar{\bSigma}_t = G_t\bSigma_{t-1} G_t^{T} + \bm{R}_t$\\
 \ForEach{$\z_t^i$}{
  $j = c_t^i$\\
  \If{landmark $j$ never seen before}{
    Initialize $\begin{bmatrix}\bar{\mu}_{j,x} \\ \bar{\mu}_{j,y} \end{bmatrix}$ as expected position based on $\z_t^i$
  }
  $S_t^i = H_t^j\bar{\bSigma}_{t}[H^j_t]^{T}+\bm{Q}_t$\\
  $K^i_t = \bar{\bSigma}_{t}[H^j_t]^{T}[S_t^i]^{-1}$\\
  $\bar{\bmu}_t = \bar{\bmu}_t + K^i_t(\z^i_t - h(\bar{\bmu}_{t}, j))$\\
  $\bar{\bSigma}_t = (I - K^i_t H^j_t)\bar{\bSigma}_t$\\
 }
 $\bmu_t = \bar{\bmu}_t$\\
 $\bSigma_t = \bar{\bSigma}_t$\\
 \Return $\bmu_t, \bSigma_t$
 \caption{Extended Kalman Filter Online SLAM Algorithm}
 \label{alg:ekfslam}
\end{algorithm}
Algorithm \ref{alg:ekfslam} presents the EKF SLAM algorithm with known correspondences. It is almost identical to the EKF localization algorithm from last chapter, except that the state vector is augmented with the landmark positions and the positions of these landmarks are initialized when they are first seen. For this algorithm a general initialization of the belief distribution $bel(\y_0)$ is with:
\begin{equation*}
\bmu_0 = \begin{bmatrix}
\x_0 \\ 0 \\ \vdots \\ 0
\end{bmatrix}, \quad \bSigma_0 = \begin{bmatrix}
\tilde{\bSigma}_0 & 0 & \cdots & 0 \\
    0 & \infty & \cdots & 0 \\
     \vdots & \vdots & \ddots & \vdots \\
    0 & 0 & \cdots & \infty \\
\end{bmatrix},
\end{equation*}
where:
\begin{equation*}
\x_0 = \begin{bmatrix}
0 \\ \vdots \\ 0
\end{bmatrix}, \quad \tilde{\bSigma}_0 = \begin{bmatrix}
0 & \cdots & 0 \\ \vdots & \ddots & \vdots \\ 0 & \cdots & 0
\end{bmatrix},
\end{equation*}
and $\x_0$ and $\tilde{\bSigma}$ are the initial robot state and associated covariance (which is set to zero).
Since the reference frame for the map can be defined arbitrarily, this initialization is used to say that the initial robot pose is known to be at the origin with certainty (and the map is built with respect to that origin). The covariance of the map positions is set to infinity to reflect that there is initially no knowledge of their position.

\subsection{EKF SLAM with Unknown Correspondences}
Performing EKF SLAM when the correspondences between measurements and landmarks are \textit{unknown} poses a more challenging problem. In the EKF localization case (when the map was known), a maximum likelihood method was used to determine correspondence. A similar approach is taken for EKF SLAM, which uses a maximum likelihood approach based on the \textit{estimated} landmark positions. The main difference is that now a mechanism for hypothesizing that a new landmark has been found is also required. The EKF SLAM with unknown correspondences algorithm is given in Algorithm \ref{alg:ekfslamunknowncorr}.
\begin{algorithm}[ht]
 \KwData{$\bmu_{t-1}, \bSigma_{t-1}, \bu_{t},\z_{t}, N_{t-1}$}
 \KwResult{$\bmu_t, \bSigma_t$}
 $N_t = N_{t-1}$\\
 $\bar{\bmu}_t = g(\bu_t,\bmu_{t-1})$\\
 $\bar{\bSigma}_t = G_t\bSigma_{t-1} G_t^{T} + \bm{R}_t$\\
 \ForEach{$\z_t^i$}{
  Hypothesize position $\begin{bmatrix}\bar{\mu}_{N_t + 1,x} \\ \bar{\mu}_{N_t + 1,y} \end{bmatrix}$ from $\z_t^i$ \\
    \ForEach{$k=1$ \KwTo $N_t+1$}{
      $\hat{\z}_t^k = h(\bar{\bmu}_{t}, k)$\\
      $S_t^k = H_t^k\bar{\bSigma}_{t}[H^k_t]^{T}+\bm{Q}_t$\\
      $d_t^{ik} = (\z_t^i-\hat{\z}^{k}_t)^\top  [S_t^{k}]^{-1} (\z_t^i-\hat{\z}^{k}_t)$\\
     }
  $d_t^{i(N_t+1)} = \alpha$\\
  $j = \arg\min_k \:\: d_t^{ik}$\\
  $N_t = \max \{N_t, j\}$\\
  $K^i_t = \bar{\bSigma}_{t}[H^j_t]^{T}[S_t^j]^{-1}$\\
  $\bar{\bmu}_t = \bar{\bmu}_t + K^i_t(\z^i_t - \hat{\z}_t^j)$\\
  $\bar{\bSigma}_t = (I - K^i_t H^j_t)\bar{\bSigma}_t$\\
 }
 $\bmu_t = \bar{\bmu}_t$\\
 $\bSigma_t = \bar{\bSigma}_t$\\
 \Return $\bmu_t, \bSigma_t$
 \caption{EKF Online SLAM Algorithm, Unknown Correspondences}
 \label{alg:ekfslamunknowncorr}
\end{algorithm}

As can be seen there are a couple differences between Algorithm \ref{alg:ekfslam} and Algorithm \ref{alg:ekfslamunknowncorr}. First, the measurements $\z_k^i$ are used to \textit{hypothesize} the position of a new landmark. The Mahalanobis distance $d_t^{ik}$ is then computed for all currently tracked landmarks, and the hypothesized landmark is added if the distance exceeds a threshold $\alpha$ (i.e. $d_t^{ik} > \alpha$ for all $k = 1,\dots,N_t$).

While this EKF-based algorithm can be used to solve the online SLAM problem without correspondences, it is not necessarily the most robust approach. In particular, extraneous measurements can result in the creation of fake landmarks, which will then propagate forward to future steps and cannot be corrected!
There are several techniques to mitigate these issues, such as using outlier rejection schemes or strategies to enhance the distinctiveness of landmarks (which may require prior knowledge or assumptions).
Another important disadvantage of EKF SLAM is that its computational complexity is quadratic with the number of landmarks $N$, but generally a large number of landmarks is required for good localization accuracy!

\begin{example}[Differential Drive Robot with Range and Bearing Measurements] \label{ex:rangeandbearingEKFSLAM}
\theoremstyle{definition}
Consider a differential drive robot with state $\x = [x, y, \theta]^\top $, and suppose a sensor is available on the robot which measures the range $r$ and bearing $\phi$ of landmarks $m_j \in \m$ relative to the robot’s local coordinate frame. Additionally, multiple measurements corresponding to different features can be collected at each time step:
\begin{equation*}
\z_t = \{[r_t^1,\phi_t^1]^\top , [r_t^2,\phi_t^2]^\top , \dots\},
\end{equation*}
where each measurement $\z_t^i$ contains the range $r_t^i$ and bearing $\phi_t^i$.

For the SLAM problem, the augmented state $\y_t$ is defined as:
\begin{equation*}
\y_t =
    \begin{bmatrix}
    \x_t \\
    \m_1 \\ \vdots \\ \m_N
    \end{bmatrix}
    = \begin{bmatrix}
    x & y & \theta & m_{1,x} &m_{1,y} & \dots & m_{N,x} &m_{N,y}
    \end{bmatrix}^\top .
\end{equation*}

Assuming the correspondences are known, the measurement model for the range and bearing is:
\begin{equation}
h(\y_t, j)  = \begin{bmatrix}
\sqrt{(m_{j,x} - x)^{2} + (m_{j,y}- y)^{2}} \\
\text{atan2}(m_{j,y}- y, m_{j,x} - x) - \theta
\end{bmatrix}.
\end{equation}
The measurement Jacobian $H^j_t$ corresponding to a measurement from landmark $j$ is then given by:
\begin{equation}
H^j_t = \begin{bmatrix}
-\frac{\bar{\mu}_{j,x} - \bar{\mu}_{t,x}}{\sqrt{q_{t,j}}} & -\frac{\bar{\mu}_{j,y} - \bar{\mu}_{t,y}}{\sqrt{q_{t,j}}} & 0 & 0 & \dots & 0 & \frac{\bar{\mu}_{j,x} - \bar{\mu}_{t,x}}{\sqrt{q_{t,j}}} & \frac{\bar{\mu}_{j,y} - \bar{\mu}_{t,y}}{\sqrt{q_{t,j}}} & 0 & \dots \\
\frac{\bar{\mu}_{j,y} - \bar{\mu}_{t,y}}{q_{t,j}} & -\frac{\bar{\mu}_{j,x} - \bar{\mu}_{t,x}}{q_{t,j}} & -1 & 0 & \dots & 0 & -\frac{\bar{\mu}_{j,y} - \bar{\mu}_{t,y}}{q_{t,j}} & \frac{\bar{\mu}_{j,x} - \bar{\mu}_{t,x}}{q_{t,j}} & 0 & \dots
\end{bmatrix},
\end{equation}
where:
\begin{equation*}
    q_{t,j} := (\bar{\mu}_{j,x} - \bar{\mu}_{t,x})^{2} + (\bar{\mu}_{j,y} - \bar{\mu}_{t,y})^{2},
\end{equation*}
and $\bar{\mu}_{j,x}$ and $\bar{\mu}_{j,y}$ are the estimate of the $x$ and $y$ coordinates of landmark $m_j$ from $\bar{\bmu}_t$.

With both a range and bearing measurement, the \textit{expected} position of landmark $m_j$ is given by:
\begin{equation*}
    \begin{bmatrix}
    \bar{\mu}_{j,x} \\
    \bar{\mu}_{j,y}
    \end{bmatrix}
    = \begin{bmatrix}
    \bar{\mu}_{t,x}\\
    \bar{\mu}_{j,y}
    \end{bmatrix}
    +
    \begin{bmatrix}
    r^{i}_{t}\text{cos}(\phi^{i}_{t} + \bar{\mu}_{t,\theta}) \\
    r^{i}_{t}\text{sin}(\phi^{i}_{t} + \bar{\mu}_{t,\theta})
    \end{bmatrix}.
\end{equation*}
This can be used in the known-correspondence EKF SLAM algorithm (Algorithm \ref{alg:ekfslam}) to initialize the landmark position and can be used in the unknown-correspondence case (Algorithm \ref{alg:ekfslamunknowncorr}) to hypothesize the position of new landmarks.
\end{example}

\subsection{Particle SLAM Algorithm}
Another approach to the robot SLAM problem is to leverage the non-parametric particle filter. In fact, particle SLAM can be used to solve the \textit{full} SLAM problem, unlike EKF SLAM which only solves the online SLAM problem. Specifically, the full SLAM problem is to estimate the posterior distribution $p(\x_{1:t}, \m \mid \z_{1:t}, \bu_{1:t})$, which includes the full robot path $\x_{1:t}$ up to time $t$ and the map $\m$. Similar to the EKF SLAM case, the robot state $\x_{1:t}$ and map feature positions $\m$ are combined into an augmented state vector $\y_{1:t}$ as in \eqref{eq:augmentedstate}.

A na\"ive implementation of the particle filter in the context of full SLAM would be computationally intractable, since the number of particles required to belief distribution would be extremely large. However, the key insight that makes this approach tractable is that the posterior over the map elements is \textit{conditionally independent} given the true path of the robot. Therefore the mapping component to the problem can be split up into separate problems, corresponding to each feature in the map! Splitting the problem in this way makes the overall problem much easier to solve.

Overall, particle filter SLAM approaches can be used with \textit{any} noise distribution and can express multimodal beliefs since they are non-parametric. Additionally, in practice they can be relatively easy to implement and can also be more robust to data association errors. Their main disadvantages are that they typically do not scale well to large scale problems (too many particles are needed), and that without enough particles convergence may not occur.

\subsubsection{Factoring the Posterior}
The key insight of particle SLAM that makes it a computationally tractable algorithm is that the posterior $p(\y_{1:t} \mid \z_{1:t}, \bu_{1:t}, c_{1:t})$ can be factored as:
\begin{equation} \label{eq:factored}
p(\y_{1:t} \mid \z_{1:t}, \bu_{1:t}, c_{1:t}) = p(\x_{1:t} \mid \z_{1:t}, \bu_{1:t}, c_{1:t}) \prod_{n=1}^N p(\m_n \mid \x_{1:t}, \z_{1:t},  c_{1:t}),
\end{equation}
where $\m_n$ is the $n$-th feature in the map $\m$, the term $p(\x_{1:t} \mid \z_{1:t}, \bu_{1:t}, c_{1:t})$ is referred to as the \textit{path posterior}, and the terms $p(\m_n \mid \x_{1:t}, \z_{1:t},  c_{1:t})$ are referred to as the \textit{feature posteriors}.

This factorization can be derived by first using Bayes' rule
\begin{equation*}
p(\y_{1:t} \mid \z_{1:t}, \bu_{1:t}, c_{1:t}) = p(\x_{1:t} \mid \z_{1:t}, \bu_{1:t}, c_{1:t}) p(\m \mid \x_{1:t}, \z_{1:t}, \bu_{1:t},  c_{1:t}),
\end{equation*}
and then noting that since the feature posterior is conditioned on $\x_{1:t}$, the dependence on $\bu_{1:t}$ is redundant:\begin{equation*}
p(\y_{1:t} \mid \z_{1:t}, \bu_{1:t}, c_{1:t}) = p(\x_{1:t} \mid \z_{1:t}, \bu_{1:t}, c_{1:t}) p(\m \mid \x_{1:t}, \z_{1:t},  c_{1:t}).
\end{equation*}

Now the feature posterior $p(\m \mid \x_{1:t}, \z_{1:t},  c_{1:t})$ can be explored in more detail. In particular two cases can be considered for each landmark $\m_n$: the case when the measurement at time $t$ is not associated with $n$ and the case when it is:
\begin{equation*}
p(\m_n \mid \x_{1:t}, \z_{1:t}, c_{1:t}) = \begin{cases}
p(\m_n \mid \x_{1:t-1}, \z_{1:t-1},  c_{1:t-1}), \quad &c_t \neq n, \\
\frac{p(\z_t \mid \m_n, \x_{t}, c_{t})p(\m_n \mid \x_{1:t-1}, \z_{1:t-1}, c_{1:t-1})}{p(\z_t \mid \x_{1:t}, \z_{1:t-1}, c_{1:t})}, \quad &c_t = n,
\end{cases}
\end{equation*}
where in the second case Bayes' rule was applied. It is now possible to show the result \eqref{eq:factored} by induction. First, suppose that:
\begin{equation*}
p(\m \mid \x_{1:t-1}, \z_{1:t-1},  c_{1:t-1}) = \prod_{n=1}^N p(\m_n \mid \x_{1:t-1}, \z_{1:t-1},  c_{1:t-1}).
\end{equation*}
Then, using Bayes' rule at time $t$:
\begin{equation*}
\begin{split}
p(\m \mid \x_{1:t}, \z_{1:t}, c_{1:t}) &= \frac{p(\z_t \mid \m, \x_{t}, c_{t})p(\m \mid \x_{1:t-1}, \z_{1:t-1}, c_{1:t-1})}{p(\z_t \mid \x_{1:t}, \z_{1:t-1}, c_{1:t})}, \\
&= \frac{p(\z_t \mid \m, \x_{t}, c_{t})}{p(\z_t \mid \x_{1:t}, \z_{1:t-1}, c_{1:t})} \prod_{n=1}^N p(\m_n \mid \x_{1:t-1}, \z_{1:t-1},  c_{1:t-1}). \\
\end{split}
\end{equation*}
Next, applying the analysis above for the cases where $c_t \neq n$ and $c_t = n$:
\begin{equation*}
\begin{split}
p(\m \mid \x_{1:t}, \z_{1:t}, c_{1:t}) &= p(\m_{c_t} \mid \x_{1:t}, \z_{1:t},  c_{1:t}) \prod_{n\neq c_t} p(\m_n \mid \x_{1:t}, \z_{1:t},  c_{1:t}), \\
&= \prod_{n=1}^N p(\m_n \mid \x_{1:t}, \z_{1:t},  c_{1:t}). \\
\end{split}
\end{equation*}

\subsubsection{Fast SLAM with Known Correspondences}
The particle SLAM algorithm referred to as Fast SLAM uses the factorization of the posterior $p(\y_{1:t} \mid \z_{1:t}, \bu_{1:t}, c_{1:t})$ in \eqref{eq:factored} to decompose the full SLAM problem into more manageable sub-problems. Specifically, the path posterior $p(\x_{1:t} \mid \z_{1:t}, \bu_{1:t}, c_{1:t})$ is estimated using a particle filter and the feature posteriors $p(\m_n \mid \x_{1:t}, \z_{1:t},  c_{1:t})$ are estimated by EKFs conditioned on the robot path $\x_{1:t}$ (i.e. there is a separate EKF for each feature $\m_n$).

Accordingly, the set of particles is given as:
\begin{equation*}
\mathcal{Y}_t \coloneqq \{Y_t^{[1]}, Y_t^{[2]},..., Y_t^{[M]}\},
\end{equation*}
where the $k$-th particle is defined by:
\begin{equation*}
Y_t^{[k]} = \{\x_t^{[k]}, \bmu_{1,t}^{[k]}, \bSigma_{1,t}^{[k]}, \dots, \bmu_{N,t}^{[k]}, \bSigma_{N,t}^{[k]} \},
\end{equation*}
where $\x_t^{[k]}$ is a hypothesis of the robot state at time $t$, $(\bmu_{n,t}^{[k]}, \bSigma_{n,t}^{[k]})$ are the mean and covariance of the EKF associated with landmark $\m_n$, and where it is assumed that there are $N$ total landmarks in the map $\m$. As can be seen, with a total of $M$ particles there are a total of $NM$ EKFs!
To summarize, the Fast SLAM algorithm is a particle based algorithm where each particle keeps track of a hypothesis of the robot state as well as the location (and uncertainty) of each landmark in the map! The algorithm is defined in Algorithm \ref{alg:fastslam}.
\begin{algorithm}[ht]
 \KwData{$\mathcal{Y}_{t-1}, \bu_{t}, \z_{t}, c_t$}
 \KwResult{$\mathcal{Y}_{t}$}
 \For{$k=1$ \KwTo $M$}{
  Sample $\x_{t}^{[k]} \sim p(\x_t\mid \bu_t, \x_{t-1}^{[k]})$\\
  $j = c_t$\\
  \eIf{landmark $j$ never seen before}{
    Initialize feature: $(\bmu^{[k]}_{j,t-1}, \bSigma^{[k]}_{j,t-1})$
  }
  {
  $\hat{\z}^{[k]} = h(\bmu^{[k]}_{j,t-1}, \x_{t}^{[k]})$\\
  $S = H^j\bSigma^{[k]}_{j,t-1}[H^j]^{T}+\bm{Q}_t$\\
  $K = \bSigma^{[k]}_{j,t-1}[H^j]^{T}[S]^{-1}$\\
  $\bmu^{[k]}_{j,t} = \bmu^{[k]}_{j,t-1} + K(\z_t - \hat{\z}^{[k]})$\\
  $\bSigma^{[k]}_{j,t} = (I - K H^j)\bSigma^{[k]}_{j,t-1}$\\
  $w^{[k]} = \text{det}(2\pi S)^{-1/2}\text{exp}\big(-\frac{1}{2}(\z_t - \hat{\z}^{[k]})\bm{Q}^{-1}(\z_t - \hat{\z}^{[k]})\big)$\\
  }
  \For{$n \in \{1,\dots,N\}$, $n \neq c_t$}{
  $\bmu^{[k]}_{n,t} = \bmu^{[k]}_{n,t-1}$\\
  $\bSigma^{[k]}_{n,t} = \bSigma^{[k]}_{n,t-1}$\\
 }
 }
 $\mathcal{Y}_t = \emptyset$\\
 \For{$m=1$ \KwTo $M$}{
  Draw $k$ with probability $\propto w_t^{[k]}$\\
  $\mathcal{Y}_t = \mathcal{Y}_t \cup (\bar{\x}_{t}^{[k]}, \bmu^{[k]}_{1,t}, \bSigma^{[k]}_{1,t}, \dots, \bmu^{[k]}_{N,t}, \bSigma^{[k]}_{N,t})$\\
 }
 \Return $\mathcal{Y}_t$
 \caption{Fast SLAM Algorithm}
 \label{alg:fastslam}
\end{algorithm}

Note the blending of the classical particle filter algorithm with the EKF localization algorithm. In particular, the particle filter steps can be seen with the sampling of the new pose $\x_t$ from the state transition model and the use of the weights $w$ for resampling a new set of particles (i.e. the measurement correction step). The EKF portions of the algorithm correspond to how the features are tracked, and in particular how the mean and covariance of the Gaussian corresponding to each landmark are updated based on new measurements.

\subsection{Exercises}
\subsubsection{EKF SLAM}
Complete \textit{Problem 2: EKF SLAM} located in the online repository:

\vspace{\baselineskip}

\url{https://github.com/PrinciplesofRobotAutonomy/AA274A_HW4},

\vspace{\baselineskip}

where you will implement an EKF SLAM algorithm. Note that the EKF localization exercise from the chapter on parametric filters should be completed first.


%&LaTeX

% Sections of this are duplicated from the DSP First labs 1a and
% 1b. Those sections MUST be rewritten before this can be released
% externally.

\section{Complex Exponentials in J-DSP}

% TODO: redo this beginning section to introduce the idea of a time
% series in JDSP relate the rotating phasors addition to moving back
% and forth along the real axis.  Scrap the MATLAB introductions in
% favor of introducing how to look at phasors in JDSP intuitively and
% that the complex plane is just a succinct way of looking at sine
% waves.

In this lab you will be investigating how complex exponentials relate
to sine and cosine waves. Once completed you should have a feel for
how the complex plane can be used to represent \textit{real,
  oscillating functions} (like sines and cosines). Moreover, you
should have an appreciation for why it is easier to mathematically
manipulate and analyze oscillating functions using complex
arithmetic. We will be using J-DSP exclusively to illustrate key
concepts of how complex numbers can represent frequency.

\subsection{Complex Vectors and Exponentials in J-DSP}
\label{sc:jdsp-sines}
In this section you will use J-DSP to generate vectors and phasors in the complex plane. Specifically, you will relate changes in the complex plane to changes in the time domain.

% Complex Vector in J-DSP
\begin{figure}[t]
  \begin{center}
    \includegraphics[height=3in]{lab2/PoleZeroAsComplexVector}
  \end{center}
\caption{J-DSP setup for simulating a two vectors that add to be a real output, represented as sample 2 in the plot.\label{fg:PoleZeroAsComplexVec}}
\end{figure}

% idea of the vectors in the complex plane
\paragraph{Step 1.1}
WARNING: The next step will overwrite any work not saved in J-DSP. Use the given script to generate the diagram in Figure \ref{fg:PoleZeroAsComplexVec}. Download: \url{http://faculty.washington.edu/stiber/pubs/Signal-Computing/jdsp-scripts/polezero_as_vector.txt}.

Use the \menu{File} $\rightarrow$\menu{Import from Script...} command in the J-DSP window and copy the text  over. You should see a block diagram similar to that in Figure \ref{fg:PoleZeroAsComplexVec}. If you do not, there was a failure loading the script, exit out of J-DSP, and try again.
This script provides an interactive way to \textit{view vectors} in the complex plane and what they look like in the time domain. We are not yet ready to talk about every block in the J-DSP diagram. However, two blocks are of interest to us now. We will interact with \block{PZ-Placement} (block a) and \block{Plot} (block b). Open the dialog for the \block{Plot} block in the diagram if it is not already active. You should see three discrete points in the output plot (Figure \ref{fg:PoleZeroAsComplexVec}).

Now open the dialog for \block{PZ-Placement} if it is not already active. You should see a plot of the complex plane with two $\circ$ marks on it and a large black circle that represents the unit circle. This \block{PZ-Placement} block is normally used for filter design, but we will be using it to represent a vector in the complex plane, $Re^{j\phi}$. If you drag the $\circ$ marks in \block{PZ-Placement}, you can change the amplitude and phase of the vector and view the ``real part'' of the output in the time domain plot. When you do this, you will also see the three points in the output plot change in magnitude. We will be interested in what happens to the middle value in the plot (marked ``real part'' in Figure \ref{fg:PoleZeroAsComplexVec}). You can (1) click and drag or (2) you can enter the real and complex values manually using the \button{Graphically} and \button{Manually} selections from the pop-up menu.
Note that the \block{PZ-placement} block is doing more than just helping to plot the ``real part'' of the complex vector. For now, just know that the second point plotted in the \block{Plot} dialog corresponds to the ``real part''  of the complex vector. It is directly affected by the magnitude and phase of the vector. For the following questions, recall that a phasor is just a spinning vector in the complex plane.

\begin{enumerate}
\item If you repeatedly drag the $\circ$ marks in a circular motion along the black circle in \block{PZ-placement}, what happens to the ``real part'' in the plot dialog? If you were to keep the vector magnitude constant and graph the ``real part'' of the plot over time as it moves around the complex plane, would you expect to see a sinusoidal wave? How would the amplitude and phase of the sinusoidal wave be affected by the complex vector?

\end{enumerate}

% Complex Phasor in J-DSP
\begin{figure}[t]
  \begin{center}
    \includegraphics[height=3in]{lab2/PoleZeroAsComplexExp}
  \end{center}
\caption{J-DSP setup for simulating two phasors that add to be a real sinusoid.\label{fg:PoleZeroAsComplexExp}}
\end{figure}

% introduce the pole zero placement as means of visualizing the phasor (only a weak analogy, it would be great to have a built-in J-DSP phasor animation function)
\paragraph{Step 1.2}
WARNING: The next step will overwrite any work not saved in J-DSP. Use the given script to generate the diagram in Figure \ref{fg:PoleZeroAsComplexExp}. Download: \url{http://faculty.washington.edu/stiber/pubs/Signal-Computing/jdsp-scripts/polezero_as_phasor.txt}.

Use the \menu{File} $\rightarrow$\menu{Import from Script...} command in the J-DSP window and copy the text  over. You should see a block diagram similar to that in Figure \ref{fg:PoleZeroAsComplexExp}. If you do not, there was a failure loading the script, exit out of J-DSP, and try again.

This script provides an interactive way to manipulate the \textit{frequency} of phasors in the complex plane and view the sinusoidal output. We are not yet ready to talk about every block in the J-DSP diagram. Like before, two blocks are of interest to us now. We will interact with \block{PZ-Placement} (block a) and \block{Plot} (block b). Open the dialog for the \block{Plot} block in the diagram if it is not already active. You should see a cosine wave. (Note that, in the plot in the figure, the limits of the $X$-axis have been changed manually to have a lower limit of 65, rather than zero. Unfortunately, the axis limits don't get restored when a script is imported, so you will see the auto axis limits, rather than the ones set as in the figure. You can set the limits in the plot to match the figure yourself.)

Now open the dialog for \block{PZ-Placement} if it is not already active. This block is normally used for filter design, but we will be using it to simulate a phasor in the complex plane. Recall that a phasor can be represented in polar form as $Re^{j\omega t}$ where $\omega $ represents the frequency at which the phasor rotates and $R$ represents the magnitude. Dragging the \texttt{x} marks in \block{PZ-Placement} lets you change the $\omega $ of the resulting phasor and view the sinusoid in real time. Note that you can also change the magnitude of the complex phasors in the dialog, but this is not the same as changing the amplitude of a complex phasor. There are other things going on in this block that we are not ready to talk about - we are actually treating something called a ``pole'' as a rotating phasor, which is not quite right. For now, try to keep the magnitude of the phasor as close to ``one'' as possible (i.e., try to keep the \texttt{x} on the black line representing the unit circle).

\begin{enumerate}
\item Drag the phasors along the unit circle towards the (1,0) point (where the unit circle meets the real axis in the right half plane). What happens? What happens when you move away from the the (1,0) point along the unit circle? Include a screenshot of the \block{Plot} and \block{PZ-placement} dialogs in your lab report.

\item Notice that you cannot move only one of the phasors. You must move both at the same time. Does it make sense that you must move both symmetrically? Why or why not? \textit{Hint:} think about the output as being the rotating phasors $e^{j\omega_1 t}+e^{j\omega_2 t}$, where $\omega_1$ and $\omega_2 $ are proportional to the angles of the ``poles.''

\end{enumerate}

\subsection{Complex Exponentials}

\paragraph{Step 2.1} Compute the following complex arithmetic by hand:
\begin{enumerate}\renewcommand{\theenumi}{\alph{enumi}}
\item What is the complex conjugate of $z_1 = -1 + j 0.3$?

\item Let $z_2 = 1 + j 0.6$ What is $z_1 + z_2$?

\item What is $z_1 - z_2$?

\item What is $z_1 z_2$?

\item What is $z_2/z_1$?

\end{enumerate}

\paragraph{Step 2.2} Compute or prove the following using complex arithmetic and Euler's Formula by hand. Recall that Euler's Formula is given by: $e^{\pm j\omega t}=\cos(\omega t) \pm j \sin(\omega t)$.

\begin{enumerate}
\item A complex number can be written in rectangular coordinates as $z
  = x + j y$. Write the relations to calculate the polar form, $z=(r,
  \theta)$ or $z = r e^{j\theta}$.

\item Using Euler's formula, express $\cos x$ and $\sin x$ as a
  combination of complex exponentials.

\item Convert $\cos(\omega t+\phi)$ into the sum of complex exponentials.

\item Find expressions for 1, $j$, $1 + j$, $(1 + j\sqrt{3})/2$ as
  complex exponentials.

\item Compute $[(1+j\sqrt{3})/2]^2$ and $(1+j)^4$ directly (using the
  rectangular representations).

\item Compute $[(1+j\sqrt{3})/2]^2$ and $(1+j)^4$ using complex
  exponentials.

\end{enumerate}

\paragraph{Step 2.3} Prove the following regarding complex conjugates:

\begin{enumerate}
\item Show that $(z_xz_y)^* = z_x^* z_y^*$.

\item Express $|z|^2$ as a function of $z$ and $z^*$.

\end{enumerate}

\subsection{Complex Exponentials in J-DSP}

\paragraph{Step 3.1} In this step, you are asked to use JDSP
to create a block diagram capable of synthesize a waveform of the form:
$\cos(2\pi (200) t+\phi_1) + \cos(2\pi (200) t +\phi_1) +\cos(2\pi (200) t+\phi_1) +\cos(2\pi (200) t+\phi_1) $
And plot the output of each cosine wave and their sum. Use the \block{Cont. Signal} and \block{Analog Adder} blocks in the ``Analog Blocks'' function set. Include a screenshot of the block diagram in your writeup. Use this diagram to complete Step 3.2.

\paragraph{Step 3.2} Generate four sinusoids with the following
amplitudes and phases (remember to convert from rad/s to Hz and from radians to degrees):
\begin{align}
x_1(t) &= 5 \cos(2\pi(200)t +0.5\pi) \\
x_2(t) &= 5 \cos(2\pi(200)t - 0.25\pi) \\
x_3(t) &= 5 \cos(2\pi(200)t +0.4\pi) \\
x_4(t) &= 5 \cos(2\pi(200)t - 0.9\pi)
\end{align}

\begin{enumerate}\renewcommand{\theenumi}{\alph{enumi}}
\item Make a plots of all four signals in J-DSP.

\item Verify that the phase of all four signals is correct at $t = 0$,
  and also verify that each one has the correct maximum amplitude. Use
  the \block{Analog Plot} blocks.

\item Create the sum sinusoid,
  $x_5(t)=x_1(t)+x_2(t)+x_3(t)+x_4(t)$. Make a plot of $x_5(t)$ over
  the same range of time as used in the last plot. Include your plot in your writeup

\item Using pencil and paper: Express   $x_1(t)$ through $x_4(t)$ as complex exponentials.

\item Using pencil and paper: Express   $x_5(t)$ as a sum of complex exponentials.

\item Using complex exponentials, express the amplitude and phase of $x_5(t)$ (use pencil and paper with the aide of a graphing calculator, spreadsheet, or MATLAB).

\end{enumerate}

% LocalWords:  MATLAB DSP


    \filetitle{regress}{Centred population regression for selected model variables}{model/regress}

	\paragraph{Syntax}\label{syntax}

\begin{verbatim}
[B,CovRes,R2] = regress(M,Lhs,Rhs,...)
\end{verbatim}

\paragraph{Input arguments}\label{input-arguments}

\begin{itemize}
\item
  \texttt{M} {[} model {]} - Model on whose covariance matrices the
  popolation regression will be based.
\item
  \texttt{Lhs} {[} char \textbar{} cellstr {]} - Lhs variables in the
  regression; each of the variables must be part of the state-space
  vector.
\item
  \texttt{Rhs} {[} char \textbar{} cellstr {]} - Rhs variables in the
  regression; each of the variables must be part of the state-space
  vector, or must refer to a larger lag of a transition variable present
  in the state-space vector.
\end{itemize}

\paragraph{Output arguments}\label{output-arguments}

\begin{itemize}
\item
  \texttt{B} {[} namedmat \textbar{} numeric {]} - Population regression
  coefficients.
\item
  \texttt{CovRes} {[} namedmat \textbar{} numeric {]} - Covariance
  matrix of residuals from the population regression.
\item
  \texttt{R2} {[} numeric {]} - Coefficient of determination
  (R-squared).
\end{itemize}

\paragraph{Options}\label{options}

\begin{itemize}
\itemsep1pt\parskip0pt\parsep0pt
\item
  \texttt{'matrixFmt='} {[} \emph{\texttt{'namedmat'}} \textbar{}
  \texttt{'plain'} {]} - Return matrices \texttt{B} and \texttt{CovRes}
  as either \href{namedmat/Contents}{\texttt{namedmat}} object
  (i.e.~matrices with named rows and columns) or plain numeric arrays.
\end{itemize}

\paragraph{Description}\label{description}

Population regressions calculated by this function are always centred.
This means the regressions are always calculated as if estimated on
observations with their uncondional means (the steady-state levels)
removed from them.

The Lhs and Rhs variables that are log-variables must include
\texttt{log( )} explicitly in their names. For instance, if \texttt{X}
is declared to be a log variable, then you must refer to \texttt{log(X)}
or \texttt{log(X\{-1\})}.

\paragraph{Example}\label{example}

\begin{verbatim}
[B,C] = regress('log(R)',{'log(R{-1})','log(dP)'});
\end{verbatim}


%
% now OS is already added in;
% will add the MD algorithm as well as DRK too in the future
% fm will be done in the near future
%
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\chapter{Algorithms for Integral Evaluation Over Gaussian Functions}
%
%
%
%
%

In this chapter, we will focus on the electron integrals; which generally has the form as:
\begin{equation}
 V_{ij} = \int \chi_{i}(r)\chi_{j}(r)f(r,r^{'})
\chi_{k}(r^{'})\chi_{l}(r^{'}) dr dr^{'}
\end{equation}
It is already shown in the shell pair data discussion\ref{shell_pair_eq:1}. Here $\chi$ is
the Gaussian primitive function with arbitrary angular momentum, and $f(r,r^{'})$ is the
electron operator for one or two electrons. In this chapter, we will discuss the integrals
and its derivatives.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{History for Algorithms of Integral Evaluation}
%
%
%
%
According to the general review given by Peter Gill\cite{gill1994molecular}, the history for
the development of integral algorithm could be divided into three period of times. The first
one is its infant period, at this time only simple SCF calculation could be performed. The
first algorithm for Gaussian functions was prompted by Boys\cite{SFBoys1950},
then his methodology was subsequently developed by
Shavitt\cite{1963_int_algorithm}, Clementi etc.
\cite{clementi1969study,clementi1972computation}.

In the second generation, the SCF algorithm was mean to be more general. The ``axis-switch''
method of Pople and Hehre(PH)\cite{PH} revolutionized the algorithms of integral in a sense
that it enables the SCF calculations to be a standard tool for the chemists. However, this method
is constructed primarily for the S and P type of Gaussian functions. Such deficiency motivated
the development of Dupuis-Rys-King(DRK)\cite{DRK1976JCP, DRK1976JCOMP, DRK1983JCOMP} method
and McMurchie-Davidson(MD)\cite{MD} method to give more general derivation for the integrals.

The third generation of integral algorithm started from the Obara-Saika method(OS)\cite{OS1986,
OS1988} method, it is derived from the idea to solve the derivatives of integral\cite{HB1982,HB1989}.
Encouraged by its success, a number of new algorithms came out\cite{HGP,gill1989efficient,
gill1990efficient,PRISM,coldprism} which constitutes the fundamental
layer for modern quantum chemistry packages.

In this chapter, we will mainly focus on the second and third generation of integral algorithms.
Namely the MD and DRK methods, OS, HGP and PRISM methods.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% this is about the mathematically formulas for integral
\input{./algorithm/technic/integral/direct}
\input{./algorithm/technic/integral/os}
\input{./algorithm/technic/integral/hgp}
\input{./algorithm/technic/integral/fm}
\input{./algorithm/technic/integral/erfc}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "../../note"
%%% End:


\subsection{part a}
In condition without controller from architector $C(s) = 1$ and $F(s) = 0$.
\begin{itemize}
	\item all figures from siso toolbox
	\begin{figure}[H]
		\caption{All figures from siso toolbox}
		\centering
		\includegraphics[width=16cm]{../Figure/Q1/Q1_a/siso_all.png}
	\end{figure}
	\newpage
	\item root locus
	\begin{figure}[H]
		\caption{root locus}
		\centering
		\includegraphics[width=12cm]{../Figure/Q1/Q1_a/rlocus.png}
	\end{figure}
	\item step response for closeloop system
	\begin{figure}[H]
		\caption{step response for closeloop system}
		\centering
		\includegraphics[width=12cm]{../Figure/Q1/Q1_a/feedback_step.png}
	\end{figure}
	\item closeloop bode (magnitude)
	\begin{figure}[H]
		\caption{closeloop bode (magnitude)}
		\centering
		\includegraphics[width=12cm]{../Figure/Q1/Q1_a/feedback_bode.png}
	\end{figure}
	\item openloop bode (magnitude)
	\begin{figure}[H]
		\caption{openloop bode (magnitude)}
		\centering
		\includegraphics[width=12cm]{../Figure/Q1/Q1_a/openloop_bode.png}
	\end{figure}
	\item sensitivity function
	\begin{figure}[H]
		\caption{sensitivity function}
		\centering
		\includegraphics[width=12cm]{../Figure/Q1/Q1_a/s_bode.png}
	\end{figure}
	\item complementary sensitivity function
	\begin{figure}[H]
		\caption{complementary sensitivity function}
		\centering
		\includegraphics[width=12cm]{../Figure/Q1/Q1_a/t_bode.png}
	\end{figure}
\end{itemize}


% !TEX root = ../main.tex

\chapter{ Conclusions}
\thispagestyle{myheadings}

This paper examines the predictability of Realized Volatility with different models. Raw transaction data is derived from Bitfinex and Bitstamp, which are major cryptocurrency exchanges. With regression, it is clearly presented that Realized Volatility of Bitcoin has a strong one-day lag autoregressive process, and also heterogeneous one-week autoregressive process in Heterogeneous Autoregressive (HAR) model. One step forward, this paper constructs Realized Quarticity as a consistent estimation of Intergraded Quarticity \cite{barndorffnielsen_econometric_2002} for HARQ model. Realised Quarticity plays an important role in filtering Brownian Motions out of daily Realized Volatility, with the outcome seconded the motion's negative effect. Also, the predictability of model is improved (in terms of mean-square error) by the inclusion of Realized Quarticity.\\

Furthermore, to extend interpretability, Metrics of Blockchain are selected for regression with Realized Volatility. AdjustedTxVolume(USD), txCount, marketcap(USD) averageDifficulty, medianTxValue(USD) gives significant and partially overlapping impact over bitcoin Volatility stand alone. Therefore, metrics contributing to significant result are filtered to participate in conjunction with Heterogeneous Model and Realized Quarticity. This mixed model (HARQ-X model) exerts forecast with better accuracy than HAR model of Bitcoin intraday volatility.\\

To affirm the conclusion achieved, intervals other than 10 min are also tested, such as 30 min, 60 min and 120 min. Reducing time interval helps in generating a more precise estimation of Intergraded Volatility and Intergraded Quarticity. All different intervals give incremental forecast result in the predictability of HAR, HARQ and HARQ-X model, and the effect of better asymptote forms reduction in prediction loss in larger-scale comparing to model progression.\\


\documentclass[10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{cite}
\usepackage[]{amsthm} %lets us use \begin{proof}
\usepackage[]{amssymb} %gives us the character \varnothing
\usepackage[ruled,vlined]{algorithm2e}
\usepackage{listings}
\usepackage[utf8]{inputenc}
\usepackage{hyperref}
\usepackage{amsmath}
\usepackage{csvsimple}
\usepackage{graphicx}
% One inch margins
\PassOptionsToPackage{margin=0.25in}{geometry}
\title{Modern Optimization Final Exam}
\author{Guanhua Huang}
\date\today

\begin{document}
\maketitle %This command prints the title based on information entered above
All the source code is stored at \url{https://github.com/victorhuangkk/york_optimization_final}
There are five problems in total in this final exam. Please find them in the following.

\begin{section}{Problem 9}
This is the problem \\
\includegraphics[width=5cm]{img/problem9.png}
\subsection{Part a}

Let's analyze the two parts separately, which are $a(T, T)$ and $l(T)$. Those two terms are generated by the variational method.

\[\frac{1}{2}a(T, T) = \frac{1}{2} \int_{0}^{1}T^\prime(x)T^{\prime}(x) dx\]
\[l(T) = \int_{0}^{1} f(x)T(x)dx\]

Since we assume the second order differentiable, we can rewrite all the terms into a series of integral summations. For simplicity reasons, I would omit $\frac{1}{2}$ here, But in the quadratic optimization part, we can add it back.
\[\frac{1}{2}a(T, T) = \frac{1}{2} \int_{0}^{1}T^\prime(x)T^{\prime}(x) dx\]
\[= \int_{0}^{x_1}(T^{\prime}(x))^2dx + \int_{x_1}^{x_2}(T^{\prime}(x))^2dx + ... \int_{x_{n-1}}^{x_n}(T^{\prime}(x))^2dx\]
\[\approxeq \sum_{i=1}^{N-1}h (T^{\prime}_{i+\frac{1}{2}})^2\]
\[\approxeq \sum_{i=1}^{N-1}h \frac{1}{h}(T_{i+1} - T_{i})\frac{1}{h}(T_{i+1} - T_{i})\]
\[= \sum_{i=1}^{N-1}\frac{1}{h}(T_{i+1} - T_{i})^2\]

And the approximation of $l(T)$ is done as below

\[\int_{0}^{1}f(x)T(x)dx = \int_{0}^{x_1}f(x)T(x)dx + \int_{x_1}^{x_2}f(x)T(x)dx + ... \int_{x_{n-1}}^{x_n}f(x)T(x)dx\]
\[= \sum_{i=1}^{N-1} \int_{x_i}^{x_{i+1}}f(x)T(x)dx\]
\[\approxeq \sum_{i=1}^{N-1} hf(x_{i+\frac{1}{2}})T(x_{i+\frac{1}{2}}), \;\;\; (\int_{x_i}^{x_{i+1}}g(x)dx \approxeq hg_{i+\frac{1}{2}} )\]

\[\approxeq h\sum_{i=1}^{N-1}\frac{1}{2} (f(x_i) + f(x_{i+1})) \frac{1}{2} (T(x_i) + T(x_{i+1}))\]

After combining these two terms together, we got the following approximation to the original objective function,
\[min(\frac{1}{h} \sum_{i=1}^{N-1}(T(x_{i+1}) - T(x_i))^2 - \frac{h}{4}\sum_{i=1}^{N-1} (f(x_i) + f(x_{i+1})) (T(x_i) + T(x_{i+1})))\]
\[s.t. \;\;\;\;\ T(0) = T(1) = 0\]

Then, in order to change the constraint optimization problem to an unconstrained version, Lagrange Multiple is used.

\[min(\frac{1}{h} \sum_{i=1}^{N-1}(T(x_{i+1}) - T(x_i))^2 - \frac{h}{4}\sum_{i=1}^{N-1} (f(x_i) + f(x_{i+1})) (T(x_i) + T(x_{i+1})) + \lambda_0T(0) + \lambda_1T(1))\]

Because of the definition, $T(0) = T(x_1) = 0, T(1) = T(x_N) = 0$

To convert the polynomial to matrix format, let's rewrite the objective function by parts.

\[\frac{1}{h} \sum_{i=1}^{N-1}(T(x_{i+1}) - T(x_i))^2 = \frac{1}{h} X^T A X\]
\[\frac{1}{h} (T_1^2 + 2T_2^2 + 2T_3^2 +... + 2(T_{N-1})^2 + (T_{N})^2 - 2T_1T_2 - 2T_2T_3 - .... - 2T_{N-1}T_{N})\]

One route to write the problem as an unconstrained problem is to do the following. Just to clarify here. $X = (T_1, T_2, T_3, ... T_{N-1}, T_N, \lambda_1, \lambda_N)^T$. This is the unknown vector we are trying to solve. However, $f(x_i)$ is a given value if $x_i$ is given. Everything looks fine up until now. The problem comes up when I tried to decompose matrix A. It is not positive definite anymore.

\includegraphics[width=13cm]{img/problem9_proof.png}

For that reason, I decided to use penalty terms, fix $\lambda_1$ and $\lambda_N$ to solve this problem. For illustration purposes, I fix $N = 6$, $h = 0.2$, $\lambda_1 = 1 = \lambda_N$ from now on and would write out all parameters explicitly.

\[A = 2 \times 5 \times \begin{pmatrix}
2 & -1 & 0 & 0 & 0 &0\\
-1 & 2 & -1 & 0 & 0 &0\\
0 & -1 & 2 & -1 & 0 &0\\
0 & 0 & -1 & 2 & -1 &0\\
0 & 0 & 0 & -1 & 2 &-1\\
0 & 0 & 0 & 0 & -1 &2 \end{pmatrix}\]

\[B = \begin{pmatrix}
1 & 1 & 0 & 0 & 0 &0\\
1 & 1 & 1 & 0 & 0 &0\\
0 & 1 & 1 & 1 & 0 &0\\
0 & 0 & 1 & 1 & 1 &0\\
0 & 0 & 0 & 1 & 1 &1\\
0 & 0 & 0 & 0 & 1 &1 \end{pmatrix}, \;\;\; f(x_i) = (1, 1, 1, 1, 1, 1)\]

\[b = f(x_i)B = 0.2/4 \times (2, 3, 3, 3, 3, 2) \]

So, in the end, the problem is formulated as the following:
\[l(T) = \frac{1}{2} X^T A X + b^T X \to min\]

As observed from the approximation, the quadratic terms omitted a few things. In the very end, we can see what's the results by these approximations.
\subsection{Part b}
After transforming the original problem into a standard quadratic optimization problem, the uniqueness of solution can be proven by the positive definiteness of matrix A. To prove the positive definiteness, I would try to prove all the pivots of A (which is a symmetric square metrics) are positive. Then, the matrix is positive definite.
\begin{proof}
	From linear algebra, we know that, in this scenario, A's quadratic summation can be written as
	\[x^T A x = \sum_{i=1}^{N-1}(x_{i+1} - x_{i})^2, \;\;\; \forall x_i \in \mathbf{x}\]
	if $\mathbf{x} \neq \mathbf{0}$, then $ \forall x, x^T A x >0$. For that reason, $A$ is positive definite. In quadratic programming, if $A$ is positive definite, there is a one and only one solution. Uniqueness is proven to be true.
\end{proof}

\subsection{Part c}
As hinted by the instructor, $f(x) = c = 1$ to simplify the visualization. Coordinate descent and gradient descent are used to optimize the objective function respectively. For simplicity reason, I designate $N = 6$ such that $h = 0.2$. And there are six points at $x_1 = 0, x_2 = 0.2, x_3 = 0.4, x_4 = 0.6, x_5 = 0.8, x_6 = 1.0$.

This is the exact temperature distribution over the bar, with the simple function $T(x) = \frac{1}{2}x(x-1)$\\
\includegraphics[width=10cm]{img/problem9_plt1.png}

The gradient method with Armijo step size rule's visualization is shown here
\includegraphics[width=10cm]{img/problem9_plt2.png}

The coordinate gradient descent code is shown here.
\lstinputlisting[language=python, basicstyle=\tiny]{src/coordinate_descent.py}
\subsection{Part d}
The Armijo size rule has been replaced by the predefined step size and the results are summarized below. In terms of gradient method, $\lambda = 0.2$ overflow on my laptop. So, I showed the $1/k$'s results below. \\
\includegraphics[width=10cm]{img/problem9_plt3.png}

As we see, they converge in a very similar manner. Coordinatewise descent is not able to converge in this case.
\end{section}

\begin{section}{Problem 12}
\includegraphics[width=10cm]{img/problem12.png}

\subsection{Part a}
The level plot of the function is shown below. The left hand side is the original function and the right hand side is the quadratic appropriation at $x_0 = (0, -1)^T$. \\

\includegraphics[width=12cm]{img/problem12_plt1.png}

\subsection{Part b}
The trust region method is implemented to solve this sub-problem
\lstinputlisting[language=python, basicstyle=\tiny]{src/trust_region.py}

Computational Results

\begin{tabular}{lll}
	\hline
	$\Delta_0$ & $d_0$ & Steps Until Converge \\
	\hline\hline
	0.25 & (0.02, 0.249) & 2 \\
	0.75 & (0.048, 1.0)  & 1 \\
	1.25 & (0.048, 1.0)  & 1
\end{tabular}

\subsection{Part c}
The original function's level graph is the same but the quadratic approximation is different. \\
\includegraphics[width=12cm]{img/problem12_plt2.png}

The algorithm failed in this case since the hessian matrix is not positive definite such that Cholesky cannot be performed.
\end{section}

\begin{section}{Problem 13}

   \includegraphics[width=10cm]{img/problem13.png}

    First, let's calculate A and b from the given formula:
    \[f(x) = \frac{1}{2}(x_1, x_2, x_3) \begin{pmatrix}
    a_1 & a_2 & a_3\\
    a_2 & a_4 & a_5 \\
    a_3 & a_5 & a_6 \end{pmatrix}
    \begin{pmatrix}
    x_1\\
    x_2 \\ x_3
    \end{pmatrix} + (b_1, b_2, b_3) \begin{pmatrix}
    x_1\\ x_2\\x_3  \end{pmatrix}\]

    Since there is no first order term in the final section, so, $b = (0, 0, 0)^T$
    \[\frac{1}{2}(a_1x_1+a_2x_2+a_3x_3, a_2x_1+a_4x_2+a_5x_3, a_3x_1+a_5x_2+a_6x_3)  \begin{pmatrix}
    x_1\\
    x_2 \\ x_3
    \end{pmatrix}\]

    \[=\frac{1}{2}x_1(a_1x_1+a_2x_2+a_3x_3) +
    \frac{1}{2}x_2(a_2x_1+a_4x_2+a_5x_3) +
    \frac{1}{2}x_3(a_3x_1+a_5x_2+a_6x_3)\]

    \[=\frac{1}{2}a_1x_1^2 + \frac{1}{2}a_4x_2^2 + \frac{1}{2}a_6x_3^2 + a_2x_1x_2 + a_3x_1x_3 + a_5x_2x_3\]
    \[=x_1^2 -x_1x_2 + x_2^2 -x_2x_3 + x_3^2\]

    For this reason, we know
    $$A = \begin{pmatrix}
    	2 & -1 & 0\\
    	-1 & 2 & -1 \\
    	 0  & -1 & 2 \end{pmatrix}, \;\;\; b = \begin{pmatrix}
    	  0\\
    	  0 \\
    	  0 \end{pmatrix}$$

    The implementation is done in python and I pasted the code here for reference purposes.
    \lstinputlisting[language=python, basicstyle=\small]{src/cg_algo.py}

    And the execution results is

   	\begin{tabular}{llll}
   		\hline
   		Iteration & $x_0$ & $x_1$& $x_2$ \\
   		\hline\hline
   		0 & 0     & 1     & 2     \\
   		1 & 0.5   & 1     & 0.5   \\
   		2 & 0.556 & 0.444 & 0.333 \\
   		3 & 0     & 0     & 0
   	\end{tabular}

\end{section}

\begin{section}{Problem 15}
	\includegraphics[width=12cm]{img/problem15.png}

	\subsection{Part 1}
	First, let's write the matrix format of the function,

	$$\frac{1}{2} (x_1, x_2) \begin{pmatrix}
	a_1 & a_2\\
	a_2 & a_3 \end{pmatrix}
	\begin{pmatrix}
	x_1\\
	x_2
	\end{pmatrix} + (b_1, b_2) \begin{pmatrix}
	x_1\\
	x_2
	\end{pmatrix}$$
	\[= \frac{1}{2}(a_1x_1 + a_2x_2, a_2x_1+a_3x_2) \begin{pmatrix}
	x_1\\
	x_2
	\end{pmatrix} + x_1b_1 + x_2b_2\]
	\[= \frac{1}{2}(a_1x_1 + a_2x_2)x_1 + \frac{1}{2}(a_2x_1 + a_3x_2)x_2 + x_1b_1 + x_2b_2\]
	\[= \frac{1}{2}a_1x_1^2 + a_2x_1x_2 + \frac{1}{2}a_3x_2^2 + b_1x_1 + b_2x_2\]
	\[=-12x_2 + 4x_1^2 + 4x_2^2-4x_1x_2\]

	By comparing parameters, we got the following,
	\[b_2 = -12, b_1 = 0, a_2 = -4, a_1 =a_3 = 8\]
	\[A = \begin{pmatrix}
	8 & -4\\
	-4 & 8
	\end{pmatrix}, b = \begin{pmatrix}
	0\\
	-12
	\end{pmatrix}\]

	Based on A-conjugate definition (on page 120), $\langle d_1, Ad_2 \rangle = 0, \;\; d_1 = (1,0)^T$, then, let's write out the problem as the following:

	$$(1,0) \begin{pmatrix}
	8 & -4\\
    -4 & 8 \end{pmatrix} \begin{pmatrix}
	\delta_1\\
     \delta_2 \end{pmatrix} = (8, -4) \begin{pmatrix}
     \delta_1\\
     \delta_2 \end{pmatrix} = 8\delta_1 - 4\delta_2 = 0$$
    So, as long as $2\delta_1 = \delta_2$, the condition $\langle d_1, Ad_2 \rangle = 0$ is satisfied
    $$d_2 = \begin{pmatrix}
    \gamma\\
    2\gamma \end{pmatrix}, \;\; \forall \gamma \in \mathbb{R}$$
	\subsection{Part 2}

	Let's calculate the gradient of the function,
	\[f(x) = -12x_2 + 4x_1^2 + 4x_2^2-4x_1x_2, \;\; \nabla f(x) = \begin{pmatrix}
	8x_1 - 4x_2\\
	-12 + 8x_2 - 4x_1 \end{pmatrix} = g(x)\]

	Then, we can evaluate $g_0 = \begin{pmatrix}
	-\frac{1}{2} \times 8 - 4 \times 1\\
	-12 + 8 + 2
	\end{pmatrix} = \begin{pmatrix}
	-8\\
	-2
	\end{pmatrix}$ by given $x_0 = \begin{pmatrix}
	-\frac{1}{2}\\
	1
	\end{pmatrix}$

	In this problem, $d_1$ is given so we don't have to calculate it by $g_0$. So, the next stuff we need is the $\lambda_0 = -\frac{ \langle g_0, d_0 \rangle}{\langle Ad_0, d_0\rangle}$. I would calculate the numerator and enumerator separately.

	\[-\langle g_0, d_0 \rangle = -(-8, -2) \begin{pmatrix}
	1\\
	0
	\end{pmatrix} = 8\]
	\[\langle Ad_0, d_0 \rangle  = (1,0) \begin{pmatrix}
	8 & -4\\
	-4 & 8
	\end{pmatrix} \begin{pmatrix}
	1\\
	0
	\end{pmatrix} = (8, -4) \begin{pmatrix}
	1\\
	0
	\end{pmatrix} = 8\]

	For this reason, $\lambda_0 = \frac{8}{8}=1$, and $x^{(1)}$ is,
	\[x^{(1)} = \begin{pmatrix}
	-1/2\\
	1
	\end{pmatrix} + 1 \begin{pmatrix}
	1\\
	0
	\end{pmatrix} = \begin{pmatrix}
	\frac{1}{2}\\
	1
	\end{pmatrix} \]

	In the next iteration, we calculate $x^{(2)}$. To accomplish that, we calculate $g_1 = \begin{pmatrix}
	\frac{1}{2} \times 8 - 4 \times 1\\
	-12 + 8 - 4 \times \frac{1}{2}
	\end{pmatrix} = \begin{pmatrix}
	0\\
	-6
	\end{pmatrix}$

	and by given in the problem, we know $d_1 = \begin{pmatrix}
	1\\
	2
	\end{pmatrix}$, I follow the same routine to calculate the $\lambda_1$
	\[-\langle g_1, d_1 \rangle = -(0, -6) \begin{pmatrix}
	1\\
	2
	\end{pmatrix} = 12\]

	\[\langle Ad_0, d_0 \rangle  = (1,2) \begin{pmatrix}
	8 & -4\\
	-4 & 8
	\end{pmatrix} \begin{pmatrix}
	1\\
	2
	\end{pmatrix} = (0, 12) \begin{pmatrix}
	1\\
	2
	\end{pmatrix} = 24\]

	For this reason, $\lambda_1 = \frac{1}{2}$ and we can evaluate $x^{(2)}$ as
	\[x^{(2)} = \begin{pmatrix}
	1/2\\
	1
	\end{pmatrix} + \frac{1}{2} \begin{pmatrix}
	1\\
	2
	\end{pmatrix} = \begin{pmatrix}
	1\\
	2
	\end{pmatrix} \]

	\subsection{Part 3}
	The visualization has been done in python via matplotlib. Please refer to the source code to see details. Here is the plot.

	\includegraphics[width=6cm]{img/problem15_plt1.png}

\end{section}

\begin{section}{Problem 18}
	\includegraphics[width=8cm]{img/problem18.png}
	\subsection{Part a}
	First, followed by the hints posted on the website, I tested the four methods, which are ,Newton-cg, trust-ncg, trust-krylov, and trust-exact in scipy with tolerance $10^{-6}$. All results are summarized below. To make sure all methods works fine, the original function, Jacobian and Hessian are all provided to the program. In terms of details, please refer to the source code.

    	\begin{tabular}{lllll}
    		\hline
    		Method &Iterations & $x_1$ & $x_2$& Function Value \\
    		\hline\hline
			newton-cg  & 14  & 2.541  & 0.260& 2.247    \\
			trust-ncg  & 15   & 2.541  & 0.260 & 2.247    \\
			trust-krylov & 21   & 2.541  & 0.260 & 2.247 \\
			trust-exact  & 14  & 2.541  & 0.260 & 2.247
    	 \end{tabular}

    So, all the methods generated the same results.
	\subsection{Part b}
	The least square result is summarized here, three methods were used as requested by the instructor. The objective function is different from part a, but just the residual of the least square function. After fitting, here are the results.

	\begin{tabular}{lllll}
		\hline
		Method &Iterations & $x_1$ & $x_2$& Function Value \\
		\hline\hline
		trf  & 8  & 2.541  &  0.260 & $5.159\times 10^{-5}$  \\
		dogbox & 8   & 2.541  &   0.260 & $5.167\times 10^{-5}$  \\
		lm    & 22   & 2.541  &  0.260 & $5.179\times 10^{-5}$
	\end{tabular}

	\subsection{Part c}
	The ultimate goal is to estimate parameters and fit the five data points. The graph below is the visualization in part a and b. Since all the parameters are the same, I used only one plot here for simplification.

	\includegraphics[width=10cm]{img/problem18_plt1.png}

\end{section}

\end{document}


    \filetitle{ews}{Exponential smoothing}{tseries/expsmooth}

	\paragraph{Syntax}\label{syntax}

\begin{verbatim}
X = expsmooth(X,Beta,...)
\end{verbatim}

\paragraph{Input arguments}\label{input-arguments}

\begin{itemize}
\item
  \texttt{X} {[} tseries {]} - Input time series.
\item
  \texttt{Beta} {[} numeric {]} - Exponential factor.
\end{itemize}

\paragraph{Output arguments}\label{output-arguments}

\begin{itemize}
\itemsep1pt\parskip0pt\parsep0pt
\item
  \texttt{X} {[} tseries {]} - Exponentially smoothed series.
\end{itemize}

\paragraph{Options}\label{options}

\begin{itemize}
\item
  \texttt{'init='} {[} numeric \textbar{} \emph{\texttt{NaN}} {]} - Add
  this value before the first observation to initialise the smoothing.
\item
  \texttt{'log='} {[} \texttt{true} \textbar{} \emph{\texttt{false}} {]}
  - Logarithmise the data before filtering, de-logarithmise afterwards.
\end{itemize}

\paragraph{Description}\label{description}

\paragraph{Examples}\label{examples}


% Created 2021-10-05 Tue 12:40
% Intended LaTeX compiler: pdflatex
\documentclass[presentation,aspectratio=169, usenames, dvipsnames]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{grffile}
\usepackage{longtable}
\usepackage{wrapfig}
\usepackage{rotating}
\usepackage[normalem]{ulem}
\usepackage{amsmath}
\usepackage{textcomp}
\usepackage{amssymb}
\usepackage{capt-of}
\usepackage{hyperref}
\usepackage{khpreamble}
\usepackage{amssymb}
\usepgfplotslibrary{groupplots}
\newcommand*{\shift}{\operatorname{q}}
\definecolor{ppc}{rgb}{0.1,0.1,0.6}
\definecolor{iic}{rgb}{0.6,0.1,0.1}
\definecolor{ddc}{rgb}{0.1,0.6,0.1}
\usetheme{default}
\author{Kjartan Halvorsen}
\date{\today}
\title{PID control}
\hypersetup{
 pdfauthor={Kjartan Halvorsen},
 pdftitle={PID control},
 pdfkeywords={},
 pdfsubject={},
 pdfcreator={Emacs 26.3 (Org mode 9.4.6)},
 pdflang={English}}
\begin{document}

\maketitle

\section{Integral control example}
\label{sec:org03412a9}

\begin{frame}[label={sec:org11a0c6e}]{Lead-lag compensator for position control of the DC motor}
\begin{center}
 \includegraphics[width=.6\linewidth]{../../figures/block-DC-leadlag-compensator-numerical}
\end{center}

\begin{columns}
\begin{column}{0.5\columnwidth}
\begin{center}
  \includegraphics[width=.8\linewidth]{../../figures/bode-loop-gain-leadlag-normalized-DC-crop}
\end{center}
\end{column}

\begin{column}{0.5\columnwidth}
\begin{center}
  \includegraphics[width=.8\linewidth]{../../figures/response-leadlag-normalized-DC-crop}
\end{center}
\end{column}
\end{columns}
\end{frame}

\section{The need for integral control}
\label{sec:org5b6b53d}

\begin{frame}[label={sec:orgd93ed95}]{Feedback control}
   \begin{center}
   \begin{tikzpicture}[node distance=22mm, block/.style={rectangle, draw, minimum width=15mm}, sumnode/.style={circle, draw, inner sep=2pt}]
  {
  \node[coordinate] (input) {};
  \node[sumnode, right of=input] (sum) {\tiny $\sum$};
  \node[block, right of=sum, node distance=2.6cm] (reg) {$F(s)$};
  \node[sumnode, right of=reg] (sumd) {\tiny $\sum$};
  \node[block, right of=sumd, node distance=2.2cm] (plant) {$G(s)$};
  \node[coordinate, right of=plant, node distance=2cm] (output) {};
  \node[coordinate, below of=plant, node distance=12mm] (feedback) {};
  \node[coordinate, above of=sumd, node distance=12mm] (dist) {};

  \draw[->] (plant) -- node[coordinate, inner sep=0pt] (meas) {} node[near end, above] {$y(t)$} (output);
  \draw[->] (meas) |- (feedback) -| node[very near end, left] {$-$} (sum);
  \draw[->] (input) -- node[very near start, above] {$r(t)$} (sum);
  \draw[->] (sum) -- node[above] {$e(t)$} (reg);
  \draw[->] (reg) -- node[above] {$u(t)$}(sumd);
  \draw[->] (dist) -- node[right, very near start] {$v(t)$}(sumd);
  \draw[->] (sumd) -- node[above] {} (plant);
}
\end{tikzpicture}
\end{center}

\pause

\alert{Activity} What is the transfer function from the load disturbance \(v(t)\) to the control error \(e(t)\)?
\end{frame}

\begin{frame}[label={sec:org61318c1}]{Feedback control - eliminating a constant disturbance}
\small

\[\frac{E(s)}{V(s)} = \frac{-G(s)}{1 + G(s)F(s)} \]

\begin{block}{The final value theorem}
If steady-state exists
\[\lim_{t\to\infty} e(t) = \lim_{s\to 0} sE(s)\]

\pause
\end{block}

\begin{block}{Applied to a constant (step input) disturbance}
\(V(s) = \frac{1}{s}\)
\begin{align*}
\lim_{t\to\infty} e(t) &= \lim_{s\to 0} sE(s) = \lim_{s\to 0} s \frac{-G(s)}{1 + G(s)F(s)} \frac{1}{s}\\
&= \lim_{s\to 0} \frac{-G(s)}{1 + G(s)F(s)} % = \frac{G(0)}{1 + G(0)F(0)}
\end{align*}

\pause
\alert{Activity} Assume \(F(s) = \frac{\bar{F}(s)}{s}\) and \(G(0) = b < \infty\). Determine \(\lim_{t\to\infty} e(t)\).
\end{block}
\end{frame}

\begin{frame}[label={sec:org0b55a8a}]{The PID controller}
\begin{center}
  \begin{tikzpicture}[node distance=22mm, block/.style={rectangle, draw, minimum width=15mm}, sumnode/.style={circle, draw, inner sep=2pt},scale=0.8, every node/.style={scale=0.8}]

    \node[coordinate] (input) {};
    \node[sumnode, right of=input, node distance=16mm] (sum) {\tiny $\Sigma$};
    \node[block, right of=sum, node distance=20mm] (pid)  {$F(s)$};
    \node[coordinate, below of=sum, node distance=12mm] (feedback) {};
    \node[coordinate, right of=pid, node distance=20mm] (output) {};

    \draw[->] (input) -- node[above, pos=0.3] {$r(t)$} (sum);
    \draw[->] (sum) -- node[above] {$e(t)$} (pid);
    \draw[->] (pid) -- node[above, near end] {$u(t)$} (output);
    \draw[->] (feedback) -- node[left, near start] {$y(t)$} node[right, pos=0.95] {-} (sum);
  \end{tikzpicture}
\end{center}

\alert{Parallel form (ISA)}
\[   F(s) &= k_c\left( 1 + \frac{1}{\tau_i s} + \tau_d s\right) \]

\alert{Series form}
\[F(s) = K_c \left( \frac{ \tau_I s + 1}{\tau_I s} \right) (\tau_D s + 1)
= \underbrace{\frac{K_c(\tau_I + \tau_D)}{\tau_I}}_{k_c} \left(1 + \frac{1}{\underbrace{(\tau_I + \tau_D)}_{\tau_i} s} + \underbrace{\frac{\tau_I\tau_D}{\tau_I + \tau_D}}_{\tau_d}s \right) \]
\end{frame}

\begin{frame}[label={sec:org288acd6}]{The PID - Parallel form}
\definecolor{ppc}{rgb}{0.1,0.1,0.6}
\definecolor{iic}{rgb}{0.6,0.1,0.1}
\definecolor{ddc}{rgb}{0.1,0.6,0.1}

\begin{center}
  \begin{tikzpicture}[node distance=22mm, block/.style={rectangle, draw, minimum width=15mm}, sumnode/.style={circle, draw, inner sep=2pt}]

    \node[coordinate] (input) {};
    \node[sumnode, right of=input, node distance=16mm] (sum) {\tiny $\Sigma$};
    \node[color=iic,block, right of=sum, node distance=28mm] (ii)  {$\frac{1}{\tau_is}$};
    \node[color=ppc, coordinate, above of=ii, node distance=10mm] (pp)  {};
    \node[color=ddc,block, below of=ii, node distance=10mm] (dd)  {$\tau_ds$};
    \node[sumnode, right of=ii, node distance=20mm] (sum2) {\tiny $\Sigma$};
    \node[block, right of=sum2, node distance=20mm] (gain)  {$k_c$};
    \node[coordinate, below of=sum, node distance=12mm] (feedback) {};
    \node[coordinate, right of=gain, node distance=20mm] (output) {};

    \draw[->] (input) -- node[above, pos=0.3] {$r(t)$} (sum);
    \draw[->] (sum) -- node[above, pos=0.2] {$e(t)$} node[coordinate] (mm) {}  (ii);
    \draw[->] (gain) -- node[above, near end] {$u(t)$} (output);
    \draw[->] (feedback) -- node[left, near start] {$y(t)$} node[right, pos=0.95] {-} (sum);
    \draw[->, color=ppc] (mm) |- (pp) -| node[right,] {$u_P(t)$} (sum2);
    \draw[->, color=ddc] (mm) |- (dd) -| node[right,] {$u_D(t)$} (sum2);
    \draw[->, color=iic] (ii)  -- node[above,] {$u_I(t)$} (sum2);
    \draw[->] (sum2) -- node[above, near end] {} (gain);

  \end{tikzpicture}
\end{center}

\begin{align*}
u(t) &= k_c\Big( \textcolor{ppc}{e(t)} + \textcolor{iic}{\frac{1}{\tau_i} \int_0^{t} e(\xi) d\xi} + \textcolor{ddc}{\tau_d \frac{d}{dt} e(t)} \Big)
\end{align*}
\end{frame}

\begin{frame}[label={sec:org9114acc}]{The PID - Parallel form, modified D-part}
\definecolor{ppc}{rgb}{0.1,0.1,0.6}
\definecolor{iic}{rgb}{0.6,0.1,0.1}
\definecolor{ddc}{rgb}{0.1,0.6,0.1}

\begin{center}
  \begin{tikzpicture}[node distance=22mm, block/.style={rectangle, draw, minimum width=15mm}, sumnode/.style={circle, draw, inner sep=2pt}]

    \node[coordinate] (input) {};
    \node[sumnode, right of=input, node distance=16mm] (sum) {\tiny $\Sigma$};
    \node[color=iic,block, right of=sum, node distance=28mm] (ii)  {$\frac{1}{\tau_is}$};
    \node[color=ppc, coordinate, above of=ii, node distance=10mm] (pp)  {};
    \node[color=ddc,block, below of=ii, node distance=10mm] (dd)  {$\tau_ds$};
    \node[sumnode, right of=ii, node distance=20mm] (sum2) {\tiny $\Sigma$};
    \node[block, right of=sum2, node distance=20mm] (gain)  {$k_c$};
    \node[coordinate, below of=sum, node distance=12mm] (feedback) {};
    \node[coordinate, right of=gain, node distance=20mm] (output) {};

    \draw[->] (input) -- node[above, pos=0.3] {$r(t)$} (sum);
    \draw[->] (sum) -- node[above, pos=0.2] {$e(t)$} node[coordinate] (mm) {}  (ii);
    \draw[->] (gain) -- node[above, near end] {$u(t)$} (output);
    \draw[->] (feedback) -- node[left, near start] {$y(t)$} node[right, pos=0.95] {-} (sum);
    \draw[->, color=ppc] (mm) |- (pp) -| node[right,] {$u_P(t)$} (sum2);
    \draw[->, color=ddc] (feedback |- dd) -- node[above, pos=0.95] {-} (dd);
    \draw[->, color=ddc] (dd) -| node[right,] {$u_D(t)$} (sum2)  ;
    \draw[->, color=iic] (ii)  -- node[above,] {$u_I(t)$} (sum2);
    \draw[->] (sum2) -- node[above, near end] {} (gain);

  \end{tikzpicture}
\end{center}

\[    u(t) = k_c\Big( \textcolor{ppc}{e(t)} + \textcolor{iic}{\overbrace{\frac{1}{\tau_i} \int_0^{t} e(\xi) d\xi}^{u_I(t)}} + \textcolor{ddc}{ \underbrace{\tau_d \frac{d}{dt} \big(-y(t)\big)}_{u_D(t)}} \Big) \]
\end{frame}

\begin{frame}[label={sec:orga8d20ff}]{The PID - Parallel form}
\begin{center}
  \begin{tikzpicture}[node distance=22mm, block/.style={rectangle, draw, minimum width=15mm}, sumnode/.style={circle, draw, inner sep=2pt}, scale=0.6, every node/.style={scale=0.6}]

    \node[coordinate] (input) {};
    \node[sumnode, right of=input, node distance=16mm] (sum) {\tiny $\Sigma$};
    \node[color=iic,block, right of=sum, node distance=28mm] (ii)  {$\frac{1}{\tau_is}$};
    \node[color=ppc, coordinate, above of=ii, node distance=10mm] (pp)  {};
    \node[color=ddc,block, below of=ii, node distance=10mm] (dd)  {$\tau_ds$};
    \node[sumnode, right of=ii, node distance=20mm] (sum2) {\tiny $\Sigma$};
    \node[block, right of=sum2, node distance=20mm] (gain)  {$k_c$};
    \node[coordinate, below of=sum, node distance=12mm] (feedback) {};
    \node[coordinate, right of=gain, node distance=20mm] (output) {};

    \draw[->] (input) -- node[above, pos=0.3] {$r(t)$} (sum);
    \draw[->] (sum) -- node[above, pos=0.2] {$e(t)$} node[coordinate] (mm) {}  (ii);
    \draw[->] (gain) -- node[above, near end] {$u(t)$} (output);
    \draw[->] (feedback) -- node[left, near start] {$y(t)$} node[right, pos=0.95] {-} (sum);
    \draw[->, color=ppc] (mm) |- (pp) -| node[right,] {$u_P(t)$} (sum2);
    \draw[->, color=ddc] (feedback |- dd) -- node[above, pos=0.95] {-} (dd) -| node[right,] {$u_D(t)$}   (sum2);
    \draw[->, color=iic] (ii)  -- node[above,] {$u_I(t)$} (sum2);
    \draw[->] (sum2) -- node[above, near end] {} (gain);

  \end{tikzpicture}
  \small
  \(  u(t) = k_c\Big( \textcolor{ppc}{e(t)} + \textcolor{iic}{\overbrace{\frac{1}{\tau_i} \int_0^{t} e(\xi) d\xi}^{u_I(t)}} + \textcolor{ddc}{ \underbrace{\tau_d \frac{d}{dt} \big(-y(t)\big)}_{u_D(t)}} \Big)\)
\end{center}

   \begin{center}
   \def\TT{1}
   \begin{tikzpicture}
   \begin{axis}[
    clip=false,
    width=14cm,
    height=4.2cm,
    ylabel={},
    xlabel={$t$},
    ymax = 2,
    ymin = -0.5,
    ]
      \addplot[black, no marks, domain=-0.1:8, samples=200] {(x>0)*(1 - (1+x/\TT)*exp(-x/\TT)} node[coordinate, pin=-20:{$y(t)$}, pos=0.4] {};
      \addplot[magenta!70!black, no marks, domain=-0.1:8, samples=200] coordinates {(-0.1, 0) (0,0) (0,1) (8,1)} node[coordinate, pin=90:{$r(t)$}, pos=0.4] {};
    \end{axis}

 \end{tikzpicture}
\end{center}
\alert{Activity} Sketch the error signal \(e(t)\), the derivative signal \(u_D(t)\) and the integral signal \(u_I(t)\) (use \(\tau_i=\tau_d=1\))
\end{frame}

\begin{frame}[label={sec:orgaea28f1}]{The PID - Parallel form, solution}
\end{frame}
\begin{frame}[label={sec:org192fa56}]{The PID - Parallel form, solution}
\(u(t) = k_c\Big( \textcolor{ppc}{e(t)} + \textcolor{iic}{\overbrace{\frac{1}{\tau_i} \int_0^{t} e(\xi) d\xi}^{u_I(t)}} + \textcolor{ddc}{ \underbrace{\tau_d \frac{d}{dt} \big(-y(t)\big)}_{u_D(t)}} \Big)\)
   \begin{center}
   \def\TT{1}
   \begin{tikzpicture}
   \begin{axis}[
    clip=false,
    width=14cm,
    height=5cm,
    ylabel={},
    xlabel={$t$},
    ymax = 2,
    ]
      \addplot[black, no marks, domain=-0.1:8, samples=200] {(x>0)*(1 - (1+x/\TT)*exp(-x/\TT)} node[coordinate, pin=-20:{$y(t)$}, pos=0.4] {};
      \addplot[magenta!70!black, no marks, domain=-0.1:8, samples=200] coordinates {(-0.1, 0) (0,0) (0,1) (8,1)} node[coordinate, pin=90:{$r(t)$}, pos=0.21] {};
      \addplot[color=ppc, no marks, domain=0:8, samples=200] {(x>=0)*( (1+x/\TT)*exp(-x/\TT)} node[coordinate, pin=20:{$e(t)$}, pos=0.7] {};
      \addplot[color=iic, no marks, domain=-0.1:8, samples=200] {(x>0)*(2*(1-exp(-x/\TT)) - \x/\TT*exp(-x/\TT))} node[coordinate, pin=-20:{$u_I(t)$}, pos=0.6] {};
      \addplot[color=ddc, no marks, domain=-0.1:8, samples=200] {(x>0)*(-\x/\TT*exp(-x/\TT))} node[coordinate, pin=-20:{$u_D(t)$}, pos=0.4] {};
    \end{axis}

 \end{tikzpicture}
\end{center}
\end{frame}

\begin{frame}[label={sec:org4816b36}]{The PID - practical form}
\definecolor{ppc}{rgb}{0.1,0.1,0.6}
\definecolor{iic}{rgb}{0.6,0.1,0.1}
\definecolor{ddc}{rgb}{0.1,0.5,0.1}

\begin{center}
  \begin{tikzpicture}[node distance=22mm, block/.style={rectangle, draw, minimum width=15mm}, sumnode/.style={circle, draw, inner sep=2pt}]

    \node[coordinate] (input) {};
    \node[sumnode, right of=input, node distance=16mm] (sum) {\tiny $\Sigma$};
    \node[color=iic,block, right of=sum, node distance=28mm] (ii)  {$\frac{1}{\tau_is}$};
    \node[color=ppc, coordinate, above of=ii, node distance=10mm] (pp)  {};
    \node[color=ddc,block, below of=ii, node distance=13mm] (dd)  {$\frac{\tau_ds}{\frac{\tau_d}{N}s + 1}$};
    \node[sumnode, right of=ii, node distance=20mm] (sum2) {\tiny $\Sigma$};
    \node[block, right of=sum2, node distance=20mm] (gain)  {$k_c$};
    \node[coordinate, below of=sum, node distance=12mm] (feedback) {};
    \node[coordinate, right of=gain, node distance=20mm] (output) {};

    \draw[->] (input) -- node[above, pos=0.3] {$r(t)$} (sum);
    \draw[->] (sum) -- node[above, pos=0.2] {$e(t)$} node[coordinate] (mm) {}  (ii);
    \draw[->] (gain) -- node[above, near end] {$u(t)$} (output);
    \draw[->] (feedback) -- node[left, near start] {$y(t)$} node[right, pos=0.95] {-} (sum);
    \draw[->, color=ppc] (mm) |- (pp) -| node[right,] {$u_P(t)$} (sum2);
    \draw[->, color=ddc] (feedback |- dd) -- node[above, pos=0.95] {-} (dd);
    \draw[->, color=ddc] (dd) -| node[right,] {$u_D(t)$} (sum2)  ;
    \draw[->, color=iic] (ii)  -- node[above,] {$u_I(t)$} (sum2);
    \draw[->] (sum2) -- node[above, near end] {} (gain);

  \end{tikzpicture}
\end{center}

The parameter \(N\) is chosen to limit the influence of noisy measurements. Typically,
\[  3 < N < 20 \]
\end{frame}

\section{PID tuning - (Smith and Corripio) Ziegler Nichols}
\label{sec:orgef9bf0d}
\begin{frame}[label={sec:orgc65ab9f}]{PID tuning}
\end{frame}
\begin{frame}[label={sec:org6cab308}]{Method by Smith \& Corripio using table by Ziegler-Nichols}
\small

Given process model (fitted to response of the system) \[ G(s) = K \frac{\mathrm{e}^{-s\theta}}{\tau s + 1} \] and PID controller
   \[ F(s) = k_c\left( 1 + \frac{1}{\tau_i s} + \tau_d s\right) \]
   Choose the PID parameters according to the following table (Ziegler-Nichols, 1943)
   \begin{center}
   \setlength{\tabcolsep}{20pt}
   \renewcommand{\arraystretch}{1.5}
   \begin{tabular}{llll}
   Controller & \(k_c\) & \(\tau_i\) & \(\tau_d\)\\
  \hline\hline
  P & \(\frac{\tau}{\theta K}\) &  & \\
  PI & \(\frac{0.9\tau}{\theta K}\) & \(\frac{\theta}{0.3}\) & \\
  PID & \(\frac{1.2\tau}{\theta K}\) & \(2\theta\) & \(\frac{\theta}{2}\)\\
  \hline
\end{tabular}
\end{center}

Gives good control for \[0.1 < \frac{\theta}{\tau} < 0.6.\]
\end{frame}

\section{Fitting first-order model with delay}
\label{sec:org5c3dc80}

\begin{frame}[label={sec:orgadb0320}]{Fitting first-order model with delay}
Assuming a plant model of first-order with time constant \(\tau\) and delay \(\theta\)
\[  \quad \textcolor{green!50!black}{Y(s)} = \frac{K\mathrm{e}^{-s\theta}}{s\tau + 1}\textcolor{blue!80!black}{U(s)} \quad \overset{U(s) = \frac{u_f}{s}}{\Longrightarrow} \quad \textcolor{green!50!black}{y(t)} = u_f K\big( 1 - \mathrm{e}^{-\frac{t-\theta}{\tau}}\big)u_H(t-\theta)\]
\def\Tcnst{3}
\def\tdelay{0.6}
\def\ggain{2}
\def\uampl{0.8}
\pgfmathsetmacro{\yfinal}{\uampl*\ggain}
\pgfmathsetmacro{\yone}{0.283*\yfinal}
\pgfmathsetmacro{\ytwo}{0.632*\yfinal}
\pgfmathsetmacro{\tone}{\tdelay + \Tcnst/3}
\pgfmathsetmacro{\two}{\tdelay + \Tcnst}

\begin{center}
  \begin{tikzpicture}
    \begin{axis}[
    width=14cm,
    height=4.5cm,
    grid = both,
    xtick = {0, \tdelay, \tone, \two},
    xticklabels = {0, $\theta$, $\theta+\frac{\tau}{3}$, $\theta + \tau$},
    ytick = {0, \yone, \ytwo, \uampl, \yfinal},
    yticklabels = {0, $ $, $ $, $u_f$, $y_f$},
    xmin = -0.2,
    %minor y tick num=9,
    %minor x tick num=9,
    %every major grid/.style={red, opacity=0.5},
    xlabel = {$t$},
    ]
      \addplot [thick, green!50!black, no marks, domain=0:10, samples=100] {\uampl*\ggain*(x>\tdelay)*(1 - exp(-(x-\tdelay)/\Tcnst)} node [coordinate, pos=0.9, pin=-90:{$y(t)$}] {};
      \addplot [const plot, thick, blue!80!black, no marks, domain=-1:10, samples=100] coordinates {(-1,0) (0,0) (0,\uampl) (10,\uampl)} node [coordinate, pos=0.9, pin=-90:{$u(t)$}] {};
    \end{axis}
  \end{tikzpicture}
\end{center}

\alert{Individual activity} Evaluate the response \(y(t)\) at the two time instants \(t=\theta + \frac{\tau}{3}\) and \(t=\theta + \tau\)!
\end{frame}

\begin{frame}[label={sec:org0049621}]{Fitting first-order model with delay}
Assuming a plant model of first-order with time constant \(\tau\) and delay \(\theta\)
\[  \quad \textcolor{green!50!black}{Y(s)} = \frac{K\mathrm{e}^{-s\theta}}{s\tau + 1}\textcolor{blue!80!black}{U(s)} \quad \overset{U(s) = \frac{u_f}{s}}{\Longrightarrow} \quad \textcolor{green!50!black}{y(t)} = u_f K\big( 1 - \mathrm{e}^{-\frac{t-\theta}{\tau}}\big)u_H(t-\theta)\]
\def\Tcnst{3}
\def\tdelay{0.6}
\def\ggain{2}
\def\uampl{0.8}
\pgfmathsetmacro{\yfinal}{\uampl*\ggain}
\pgfmathsetmacro{\yone}{0.283*\yfinal}
\pgfmathsetmacro{\ytwo}{0.632*\yfinal}
\pgfmathsetmacro{\tone}{\tdelay + \Tcnst/3}
\pgfmathsetmacro{\two}{\tdelay + \Tcnst}

\begin{center}
  \begin{tikzpicture}
    \begin{axis}[
    width=14cm,
    height=4.5cm,
    grid = both,
    xtick = {0, \tdelay, \tone, \two},
    xticklabels = {0, $\theta$, $\theta+\frac{\tau}{3}$, $\theta + \tau$},
    ytick = {0, \yone, \ytwo, \uampl, \yfinal},
    yticklabels = {0, $0.283y_{f}$, $0.632y_f$, $u_f$, $y_f$},
    xmin = -0.2,
    %minor y tick num=9,
    %minor x tick num=9,
    %every major grid/.style={red, opacity=0.5},
    xlabel = {$t$},
    ]
      \addplot [thick, green!50!black, no marks, domain=0:10, samples=100] {\uampl*\ggain*(x>\tdelay)*(1 - exp(-(x-\tdelay)/\Tcnst)} node [coordinate, pos=0.9, pin=-90:{$y(t)$}] {};
      \addplot [const plot, thick, blue!80!black, no marks, domain=-1:10, samples=100] coordinates {(-1,0) (0,0) (0,\uampl) (10,\uampl)} node [coordinate, pos=0.9, pin=-90:{$u(t)$}] {};
    \end{axis}
  \end{tikzpicture}
\end{center}

\[ y_f = \lim_{t\to\infty} y(t) = u_f K \quad \Rightarrow \quad K = \frac{y_f}{u_f}. \]
\end{frame}

\begin{frame}[label={sec:orgce08b4c}]{First-order model with delay - example}
\[  \quad Y(s) = \frac{K\mathrm{e}^{-s\theta}}{s\tau + 1}U(s) \quad \overset{U(s) = \frac{u_f}{s}}{\Longrightarrow} \quad y(t) = u_f K\big( 1 - \mathrm{e}^{-\frac{t-\theta}{\tau}}\big)u_s(t-\theta)\]
\def\Tcnst{2.1}
\def\tdelay{1}
\def\ggain{2}
\def\uampl{0.8}
\pgfmathsetmacro{\yfinal}{\uampl*\ggain}
\pgfmathsetmacro{\yone}{0.283*\yfinal}
\pgfmathsetmacro{\ytwo}{0.632*\yfinal}
\pgfmathsetmacro{\tone}{\tdelay + \Tcnst/3}
\pgfmathsetmacro{\two}{\tdelay + \Tcnst}

\begin{center}
  \begin{tikzpicture}
    \begin{axis}[
    width=12cm,
    height=4cm,
    grid = both,
    %xtick = {0, \tdelay, \tone, \two},
    %xticklabels = {0, $\theta$, $\theta+\frac{\tau}{3}$, $\theta + \tau$},
    %ytick = {0, \yone, \ytwo, \uampl, \yfinal},
    %yticklabels = {0, $0.283y_{f}$, $0.632y_f$, $u_f$, $y_f$},
    xmin = -0.2,
    minor y tick num=9,
    minor x tick num=9,
    every major grid/.style={red, opacity=0.5},
    %xlabel = {$t$},
    clip = false,
    ]
      \addplot [thick, green!50!black, smooth, no marks, domain=0:10, samples=16] {\uampl*\ggain*(x>\tdelay)*(1 - exp(-(x-\tdelay)/\Tcnst)} node [coordinate, pos=0.9, pin=-90:{$y(t)$}] {};
      \addplot [const plot, thick, blue!80!black, no marks, domain=-1:10, samples=100] coordinates {(-1,0) (0,0) (0,\uampl) (10,\uampl)} node [coordinate, pos=0.9, pin=-90:{$u(t)$}] {};
      \draw[thick, green!70!black, dashed] (axis cs: 10, \yfinal) -- (axis cs: -1, \yfinal, -0.9) node[left, anchor=east] {$y_f = \yfinal$};
      \draw[blue!70!black, dashed] (axis cs: 0, \uampl) -- (axis cs: -1, \uampl, -0.9) node[left, anchor=east] {$u_f = \uampl$};
    \end{axis}
  \end{tikzpicture}
\end{center}
\end{frame}

\begin{frame}[label={sec:orga70f7de}]{First-order model with delay - example}
\[  \quad Y(s) = \frac{K\mathrm{e}^{-s\theta}}{s\tau + 1}U(s) \quad \overset{U(s) = \frac{u_f}{s}}{\Longrightarrow} \quad y(t) = u_f K\big( 1 - \mathrm{e}^{-\frac{t-\theta}{\tau}}\big)u_s(t-\theta)\]
\def\Tcnst{2.1}
\def\tdelay{1}
\def\ggain{2}
\def\uampl{0.8}
\pgfmathsetmacro{\yfinal}{\uampl*\ggain}
\pgfmathsetmacro{\yone}{0.283*\yfinal}
\pgfmathsetmacro{\ytwo}{0.632*\yfinal}
\pgfmathsetmacro{\tone}{\tdelay + \Tcnst/3}
\pgfmathsetmacro{\two}{\tdelay + \Tcnst}

\begin{center}
  \begin{tikzpicture}
    \begin{axis}[
    width=12cm,
    height=4cm,
    grid = both,
    %xtick = {0, \tdelay, \tone, \two},
    %xticklabels = {0, $\theta$, $\theta+\frac{T}{3}$, $\theta + T$},
    %ytick = {0, \yone, \ytwo, \uampl, \yfinal},
    %yticklabels = {0, $0.283y_{f}$, $0.632y_f$, $u_f$, $y_f$},
    xmin = -0.2,
    minor y tick num=9,
    minor x tick num=9,
    every major grid/.style={red, opacity=0.5},
    %xlabel = {$t$},
    clip = false,
    ]
      \addplot [thick, green!50!black, smooth, no marks, domain=0:10, samples=16] {\uampl*\ggain*(x>\tdelay)*(1 - exp(-(x-\tdelay)/\Tcnst)} node [coordinate, pos=0.9, pin=-90:{$y(t)$}] {};
      \addplot [const plot, thick, blue!80!black, no marks, domain=-1:10, samples=100] coordinates {(-1,0) (0,0) (0,\uampl) (10,\uampl)} node [coordinate, pos=0.9, pin=-90:{$u(t)$}] {};
      \draw[thick, orange, dashed] (axis cs: \two, \ytwo) -- (axis cs: \two, -0.9) node[below] {$t_2 = \two = \theta + \tau$};
      \draw[thick, orange, dashed] (axis cs: \two, \ytwo) -- (axis cs: -1, \ytwo, -0.9) node[left, anchor=east] {$0.632y_f = \ytwo$};
      \draw[thick, green!70!black, dashed] (axis cs: 10, \yfinal) -- (axis cs: -1, \yfinal, -0.9) node[left, anchor=east] {$y_f = \yfinal$};
      \draw[blue!70!black, dashed] (axis cs: 0, \uampl) -- (axis cs: -1, \uampl, -0.9) node[left, anchor=east] {$u_f = \uampl$};
    \end{axis}
  \end{tikzpicture}
\end{center}
\end{frame}

\begin{frame}[label={sec:orge8ca517}]{First-order model with delay - example}
\[  \quad Y(s) = \frac{K\mathrm{e}^{-s\theta}}{s\tau + 1}U(s) \quad \overset{U(s) = \frac{u_f}{s}}{\Longrightarrow} \quad y(t) = u_f K\big( 1 - \mathrm{e}^{-\frac{t-\theta}{\tau}}\big)u_s(t-\theta)\]
\def\Tcnst{2.1}
\def\tdelay{1}
\def\ggain{2}
\def\uampl{0.8}
\pgfmathsetmacro{\yfinal}{\uampl*\ggain}
\pgfmathsetmacro{\yone}{0.283*\yfinal}
\pgfmathsetmacro{\ytwo}{0.632*\yfinal}
\pgfmathsetmacro{\tone}{\tdelay + \Tcnst/3}
\pgfmathsetmacro{\two}{\tdelay + \Tcnst}

\begin{center}
  \begin{tikzpicture}
    \begin{axis}[
    width=12cm,
    height=4cm,
    grid = both,
    %xtick = {0, \tdelay, \tone, \two},
    %xticklabels = {0, $\theta$, $\theta+\frac{T}{3}$, $\theta + T$},
    %ytick = {0, \yone, \ytwo, \uampl, \yfinal},
    %yticklabels = {0, $0.283y_{f}$, $0.632y_f$, $u_f$, $y_f$},
    xmin = -0.2,
    minor y tick num=9,
    minor x tick num=9,
    every major grid/.style={red, opacity=0.5},
    %xlabel = {$t$},
    clip = false,
    ]
      \addplot [thick, green!50!black, smooth, no marks, domain=0:10, samples=16] {\uampl*\ggain*(x>\tdelay)*(1 - exp(-(x-\tdelay)/\Tcnst)} node [coordinate, pos=0.9, pin=-90:{$y(t)$}] {};
      \addplot [const plot, thick, blue!80!black, no marks, domain=-1:10, samples=100] coordinates {(-1,0) (0,0) (0,\uampl) (10,\uampl)} node [coordinate, pos=0.9, pin=-90:{$u(t)$}] {};
      \draw[thick, red, dashed] (axis cs: \tone, \yone) -- (axis cs: \tone, -0.45) node[below] {$t_1 = \tone = \theta + \frac{\tau}{3}$};
      \draw[thick, red, dashed] (axis cs: \tone, \yone) -- (axis cs: -1,\yone) node[left, anchor=east] {$0.283y_f = \yone$};
      \draw[thick, orange, dashed] (axis cs: \two, \ytwo) -- (axis cs: \two, -0.9) node[below] {$t_2 = \two = \theta + \tau$};
      \draw[thick, orange, dashed] (axis cs: \two, \ytwo) -- (axis cs: -1, \ytwo, -0.9) node[left, anchor=east] {$0.632y_f = \ytwo$};
      \draw[thick, green!70!black, dashed] (axis cs: 10, \yfinal) -- (axis cs: -1, \yfinal, -0.9) node[left, anchor=east] {$y_f = \yfinal$};
      \draw[blue!70!black, dashed] (axis cs: 0, \uampl) -- (axis cs: -1, \uampl, -0.9) node[left, anchor=east] {$u_f = \uampl$};
    \end{axis}
  \end{tikzpicture}
\end{center}
\end{frame}

\begin{frame}[label={sec:org50ebc80}]{First-order model with delay - example}
\[  \quad Y(s) = \frac{K\mathrm{e}^{-s\theta}}{s\tau + 1}U(s) \quad \overset{U(s) = \frac{u_f}{s}}{\Longrightarrow} \quad y(t) = u_f K\big( 1 - \mathrm{e}^{-\frac{t-\theta}{\tau}}\big)u_s(t-\theta)\]
\def\Tcnst{2.1}
\def\tdelay{1}
\def\ggain{2}
\def\uampl{0.8}
\pgfmathsetmacro{\yfinal}{\uampl*\ggain}
\pgfmathsetmacro{\yone}{0.283*\yfinal}
\pgfmathsetmacro{\ytwo}{0.632*\yfinal}
\pgfmathsetmacro{\tone}{\tdelay + \Tcnst/3}
\pgfmathsetmacro{\two}{\tdelay + \Tcnst}

\begin{center}
  \begin{tikzpicture}
    \begin{axis}[
    width=12cm,
    height=4cm,
    grid = both,
    %xtick = {0, \tdelay, \tone, \two},
    %xticklabels = {0, $\theta$, $\theta+\frac{T}{3}$, $\theta + T$},
    %ytick = {0, \yone, \ytwo, \uampl, \yfinal},
    %yticklabels = {0, $0.283y_{f}$, $0.632y_f$, $u_f$, $y_f$},
    xmin = -0.2,
    minor y tick num=9,
    minor x tick num=9,
    every major grid/.style={red, opacity=0.5},
    %xlabel = {$t$},
    clip = false,
    ]
      \addplot [thick, green!50!black, smooth, no marks, domain=0:10, samples=16] {\uampl*\ggain*(x>\tdelay)*(1 - exp(-(x-\tdelay)/\Tcnst)} node [coordinate, pos=0.9, pin=-90:{$y(t)$}] {};
      \addplot [const plot, thick, blue!80!black, no marks, domain=-1:10, samples=100] coordinates {(-1,0) (0,0) (0,\uampl) (10,\uampl)} node [coordinate, pos=0.9, pin=-90:{$u(t)$}] {};
      \draw[thick, red, dashed] (axis cs: \tone, \yone) -- (axis cs: \tone, -0.45) node[below] {$t_1 = \tone = \theta + \frac{\tau}{3}$};
      \draw[thick, red, dashed] (axis cs: \tone, \yone) -- (axis cs: -1,\yone) node[left, anchor=east] {$0.283y_f = \yone$};
      \draw[thick, orange, dashed] (axis cs: \two, \ytwo) -- (axis cs: \two, -0.9) node[below] {$t_2 = \two = \theta + \tau$};
      \draw[thick, orange, dashed] (axis cs: \two, \ytwo) -- (axis cs: -1, \ytwo, -0.9) node[left, anchor=east] {$0.632y_f = \ytwo$};
      \draw[thick, green!70!black, dashed] (axis cs: 10, \yfinal) -- (axis cs: -1, \yfinal, -0.9) node[left, anchor=east] {$y_f = \yfinal$};
      \draw[blue!70!black, dashed] (axis cs: 0, \uampl) -- (axis cs: -1, \uampl, -0.9) node[left, anchor=east] {$u_f = \uampl$};
    \end{axis}
  \end{tikzpicture}
\end{center}
\[ \begin{cases} \tone = \theta + \frac{\tau}{3}\\ \two = \theta + \tau \end{cases} \quad \Rightarrow \quad \begin{cases} \theta = \tdelay \\ \tau = \Tcnst \end{cases}, \qquad  K = \frac{y_f}{u_f} = \frac{\yfinal}{\uampl} = \ggain \]
\end{frame}

\section{Analytical PID design}
\label{sec:orgb34fb58}

\begin{frame}[label={sec:org7cae885}]{Analytical PID design}
\end{frame}
\begin{frame}[label={sec:orgdf78a2d}]{Analytical PID design}
   \begin{center}
   \begin{tikzpicture}[node distance=22mm, block/.style={rectangle, draw, minimum width=15mm}, sumnode/.style={circle, draw, inner sep=2pt}]
  {
  \node[coordinate] (input) {};
  \node[sumnode, right of=input] (sum) {\tiny $\sum$};
  \node[block, right of=sum, node distance=2.6cm] (reg) {$F(s)$};
  \node[block, right of=reg, node distance=2.6cm] (plant) {$G(s)$};
  \node[coordinate, right of=plant, node distance=2cm] (output) {};
  \node[coordinate, below of=plant, node distance=12mm] (feedback) {};

  \draw[->] (plant) -- node[coordinate, inner sep=0pt] (meas) {} node[near end, above] {$y(t)$} (output);
  \draw[->] (meas) |- (feedback) -| node[very near end, left] {$-$} (sum);
  \draw[->] (input) -- node[very near start, above] {$r(t)$} (sum);
  \draw[->] (sum) -- node[above] {$e(t)$} (reg);
  \draw[->] (reg) -- node[above] {$u(t)$}(plant);
}
\end{tikzpicture}
\end{center}

\alert{Activity}  Solve for \(F(s)\) in the closed-loop transfer function \[G_c(s) = \frac{G(s)F(s)}{1 + G(s)F(s)}\]
\end{frame}

\begin{frame}[label={sec:org3a440f1}]{Analytical PID design - Solution}
\end{frame}
\begin{frame}[label={sec:orgd79c818}]{Analytical PID design - Solution}
Solving for \(F(s)\) in the closed-loop transfer function \(G_c(s) = \frac{G(s)F(s)}{1 + G(s)F(s)}\)

\[ \big(1 + G(s)F(s)\big) G_c(s) = G(s)F(s)\]
\[ G_c(s) = \big( 1 - G_c(s)\big) G(s)F(s)\]
\[F(s) = \frac{\frac{G_c(s)}{G(s)}}{1 - G_c(s)}\]
\end{frame}

\begin{frame}[label={sec:org5fee8ef}]{Analytic PID tuning - first-order with delay}
   \begin{center}
   \begin{tikzpicture}[node distance=22mm, block/.style={rectangle, draw, minimum width=15mm}, sumnode/.style={circle, draw, inner sep=2pt}]
  {
  \node[coordinate] (input) {};
  \node[sumnode, right of=input] (sum) {\tiny $\sum$};
  \node[block, right of=sum, node distance=2.6cm] (reg) {$F(s)$};
  \node[block, right of=reg, node distance=2.6cm] (plant) {$G(s)$};
  \node[coordinate, right of=plant, node distance=2cm] (output) {};
  \node[coordinate, below of=plant, node distance=12mm] (feedback) {};

  \draw[->] (plant) -- node[coordinate, inner sep=0pt] (meas) {} node[near end, above] {$y(t)$} (output);
  \draw[->] (meas) |- (feedback) -| node[very near end, left] {$-$} (sum);
  \draw[->] (input) -- node[very near start, above] {$r(t)$} (sum);
  \draw[->] (sum) -- node[above] {$e(t)$} (reg);
  \draw[->] (reg) -- node[above] {$u(t)$}(plant);
}
\end{tikzpicture}
\end{center}

Given model \(G(s) = K \frac{\mathrm{e}^{-s\theta}}{\tau s + 1}\) of the process and desired closed-loop transfer function \(G_c(s) = \frac{\mathrm{e}^{-s\theta}}{\tau_c s + 1}\)

\alert{Activity}  Show that the controller becomes
\[ F(s) = \frac{1}{K} \left( \frac{\tau s + 1}{\tau_c s + 1 - \mathrm{e}^{-s\theta}} \right) \approx \frac{1}{K} \left( \frac{\tau s + 1}{(\tau_c+\theta) s}\right)  = \underbrace{\frac{\tau}{K(\tau_c+\theta)}}_{k_c} \left( 1 + \frac{1}{\underbrace{\tau}_{\tau_i} s} \right).\]
Which is a PI-controller with \(k_c = \frac{\tau}{K(\tau_c+\theta)}\) and \(\tau_i = \tau\).
\end{frame}

\begin{frame}[label={sec:org0d28ec6}]{Example}
\small

   \begin{center}
   \begin{tikzpicture}[node distance=22mm, block/.style={rectangle, draw, minimum width=15mm}, sumnode/.style={circle, draw, inner sep=2pt}]
  {
  \node[coordinate] (input) {};
  \node[sumnode, right of=input] (sum) {\tiny $\sum$};
  \node[block, right of=sum, node distance=2.6cm] (reg) {$k_c\frac{\tau_i s + 1}{\tau_i s}$};
  \node[block, right of=reg, node distance=2.6cm] (plant) {$\frac{200 \mathrm{e}^{-0.1s}}{0.1s + 1}$};
  \node[coordinate, right of=plant, node distance=2cm] (output) {};
  \node[coordinate, below of=plant, node distance=12mm] (feedback) {};

  \draw[->] (plant) -- node[coordinate, inner sep=0pt] (meas) {} node[near end, above] {$y(t)$} (output);
  \draw[->] (meas) |- (feedback) -| node[very near end, left] {$-$} (sum);
  \draw[->] (input) -- node[very near start, above] {$r(t)$} (sum);
  \draw[->] (sum) -- node[above] {$e(t)$} (reg);
  \draw[->] (reg) -- node[above] {$u(t)$}(plant);
}
\end{tikzpicture}
\end{center}

\(k_c = \frac{\tau}{K(\tau_c+\theta)}\) and \(\tau_i = \tau\).

\alert{Activity} Determine the controller for the choice \(\tau_c = \tau\)
\end{frame}
\end{document}


\hypertarget{fuzzy-control-system}{%
\chapter{Fuzzy Control System}\label{fuzzy-control-system}}
\section{Introduction}
A control system is an arrangement of physical components designed to
alter another physical system so that this system exhibits certain
desired characteristics.

\hypertarget{steps-in-designing-fuzzy-control-system}{%
\section{Steps in Designing Fuzzy Control
System}\label{steps-in-designing-fuzzy-control-system}}

Following are the steps involved in designing Fuzzy Control System -

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\item
  Identification of variables - Here, the input, output and state
  variables must be identified of the plant which is under
  consideration.
\item
  Fuzzy subset configuration - The universe of information is divided
  into number of fuzzy subsets and each subset is assigned a linguistic
  label. Always make sure that these fuzzy subsets include all the
  elements of universe.
\item
  Obtaining membership function - Now obtain the membership function for
  each fuzzy subset that we get in the above step. A fuzzy set
  \(\tilde{A}\) in the universe of information \(U\), where
  \(\mu_{\tilde{A}}(y)\) maps \(U\), with respect to \(y\), to
  membership space.
\end{enumerate}
\[
\Large\tilde{A} = \{(y,\mu_{\tilde{A}}(y)) | y \in U\}
\]

\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\setcounter{enumi}{3}
\item
  Fuzzy rule base configuration - Now formulate the fuzzy rule base by
  assigning relationship between fuzzy input and output.
\item
  Fuzzification - The fuzzification process is initiated in this step.
\item
  Combining fuzzy outputs - By applying fuzzy approximate reasoning,
  locate the fuzzy output and merge them.
\item
  Defuzzification - Finally, initiate defuzzification process to form a
  crisp output.
\end{enumerate}
\section{Implementation}
    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{1}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{k+kn}{import} \PY{n+nn}{numpy} \PY{k}{as} \PY{n+nn}{np}
\PY{k+kn}{import} \PY{n+nn}{skfuzzy} \PY{k}{as} \PY{n+nn}{fuzz}
\PY{k+kn}{from} \PY{n+nn}{skfuzzy} \PY{k+kn}{import} \PY{n}{control} \PY{k}{as} \PY{n}{ctrl}
\end{Verbatim}
\end{tcolorbox}

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{2}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{quality} \PY{o}{=} \PY{n}{ctrl}\PY{o}{.}\PY{n}{Antecedent}\PY{p}{(}\PY{n}{np}\PY{o}{.}\PY{n}{arange}\PY{p}{(}\PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mi}{11}\PY{p}{,} \PY{l+m+mi}{1}\PY{p}{)}\PY{p}{,} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{quality}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)}
\PY{n}{service} \PY{o}{=} \PY{n}{ctrl}\PY{o}{.}\PY{n}{Antecedent}\PY{p}{(}\PY{n}{np}\PY{o}{.}\PY{n}{arange}\PY{p}{(}\PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mi}{11}\PY{p}{,} \PY{l+m+mi}{1}\PY{p}{)}\PY{p}{,} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{service}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{3}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{tip} \PY{o}{=} \PY{n}{ctrl}\PY{o}{.}\PY{n}{Consequent}\PY{p}{(}\PY{n}{np}\PY{o}{.}\PY{n}{arange}\PY{p}{(}\PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mi}{26}\PY{p}{,} \PY{l+m+mi}{1}\PY{p}{)}\PY{p}{,} \PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{tip}\PY{l+s+s2}{\PYZdq{}}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{4}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{quality}\PY{o}{.}\PY{n}{automf}\PY{p}{(}\PY{l+m+mi}{3}\PY{p}{)} \PY{c+c1}{\PYZsh{} Poor, average, good}
\PY{n}{service}\PY{o}{.}\PY{n}{automf}\PY{p}{(}\PY{l+m+mi}{3}\PY{p}{)} \PY{c+c1}{\PYZsh{} Poor, average, good}
\end{Verbatim}
\end{tcolorbox}

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{5}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{c+c1}{\PYZsh{} Manual Functions}
\PY{n}{tip}\PY{p}{[}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{low}\PY{l+s+s1}{\PYZsq{}}\PY{p}{]} \PY{o}{=} \PY{n}{fuzz}\PY{o}{.}\PY{n}{trimf}\PY{p}{(}\PY{n}{tip}\PY{o}{.}\PY{n}{universe}\PY{p}{,} \PY{p}{[}\PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mi}{13}\PY{p}{]}\PY{p}{)}
\PY{n}{tip}\PY{p}{[}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{medium}\PY{l+s+s1}{\PYZsq{}}\PY{p}{]} \PY{o}{=} \PY{n}{fuzz}\PY{o}{.}\PY{n}{trimf}\PY{p}{(}\PY{n}{tip}\PY{o}{.}\PY{n}{universe}\PY{p}{,} \PY{p}{[}\PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mi}{13}\PY{p}{,} \PY{l+m+mi}{25}\PY{p}{]}\PY{p}{)}
\PY{n}{tip}\PY{p}{[}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{high}\PY{l+s+s1}{\PYZsq{}}\PY{p}{]} \PY{o}{=} \PY{n}{fuzz}\PY{o}{.}\PY{n}{trimf}\PY{p}{(}\PY{n}{tip}\PY{o}{.}\PY{n}{universe}\PY{p}{,} \PY{p}{[}\PY{l+m+mi}{13}\PY{p}{,} \PY{l+m+mi}{25}\PY{p}{,} \PY{l+m+mi}{25}\PY{p}{]}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{6}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{quality}\PY{o}{.}\PY{n}{view}\PY{p}{(}\PY{p}{)}
\PY{n}{service}\PY{o}{.}\PY{n}{view}\PY{p}{(}\PY{p}{)}
\PY{n}{tip}\PY{o}{.}\PY{n}{view}\PY{p}{(}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    \begin{center}
    \adjustimage{max size={0.9\linewidth}{0.9\paperheight}}{./figures/FCS1.png}
    \end{center}
    { \hspace*{\fill} \\}

    \begin{center}
    \adjustimage{max size={0.9\linewidth}{0.9\paperheight}}{./figures/FCS2.png}
    \end{center}
    { \hspace*{\fill} \\}

    \begin{center}
    \adjustimage{max size={0.9\linewidth}{0.9\paperheight}}{./figures/FCS3.png}
    \end{center}
    { \hspace*{\fill} \\}

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{7}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{c+c1}{\PYZsh{} Inference rule set}
\PY{n}{rules} \PY{o}{=} \PY{p}{[}
    \PY{n}{ctrl}\PY{o}{.}\PY{n}{Rule}\PY{p}{(}\PY{n}{quality}\PY{p}{[}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{poor}\PY{l+s+s1}{\PYZsq{}}\PY{p}{]} \PY{o}{\PYZam{}} \PY{n}{service}\PY{p}{[}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{poor}\PY{l+s+s1}{\PYZsq{}}\PY{p}{]}\PY{p}{,} \PY{n}{tip}\PY{p}{[}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{low}\PY{l+s+s1}{\PYZsq{}}\PY{p}{]}\PY{p}{)}\PY{p}{,}
    \PY{n}{ctrl}\PY{o}{.}\PY{n}{Rule}\PY{p}{(}\PY{n}{quality}\PY{p}{[}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{good}\PY{l+s+s1}{\PYZsq{}}\PY{p}{]} \PY{o}{\PYZam{}} \PY{n}{service}\PY{p}{[}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{good}\PY{l+s+s1}{\PYZsq{}}\PY{p}{]}\PY{p}{,} \PY{n}{tip}\PY{p}{[}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{high}\PY{l+s+s1}{\PYZsq{}}\PY{p}{]}\PY{p}{)}\PY{p}{,}
    \PY{n}{ctrl}\PY{o}{.}\PY{n}{Rule}\PY{p}{(}\PY{n}{quality}\PY{p}{[}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{average}\PY{l+s+s1}{\PYZsq{}}\PY{p}{]} \PY{o}{|} \PY{n}{service}\PY{p}{[}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{good}\PY{l+s+s1}{\PYZsq{}}\PY{p}{]}\PY{p}{,} \PY{n}{tip}\PY{p}{[}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{medium}\PY{l+s+s1}{\PYZsq{}}\PY{p}{]}\PY{p}{)}\PY{p}{]}
\end{Verbatim}
\end{tcolorbox}

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{8}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{tip\PYZus{}ct} \PY{o}{=} \PY{n}{ctrl}\PY{o}{.}\PY{n}{ControlSystem}\PY{p}{(}\PY{n}{rules}\PY{p}{)}
\PY{n}{tipping} \PY{o}{=} \PY{n}{ctrl}\PY{o}{.}\PY{n}{ControlSystemSimulation}\PY{p}{(}\PY{n}{tip\PYZus{}ct}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{9}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{tipping}\PY{o}{.}\PY{n}{input}\PY{p}{[}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{quality}\PY{l+s+s1}{\PYZsq{}}\PY{p}{]} \PY{o}{=} \PY{l+m+mi}{6}
\PY{n}{tipping}\PY{o}{.}\PY{n}{input}\PY{p}{[}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{service}\PY{l+s+s1}{\PYZsq{}}\PY{p}{]} \PY{o}{=} \PY{l+m+mi}{8}
\PY{n}{tipping}\PY{o}{.}\PY{n}{compute}\PY{p}{(}\PY{p}{)}
\PY{n+nb}{print}\PY{p}{(}\PY{l+s+s2}{\PYZdq{}}\PY{l+s+s2}{Tip should be around}\PY{l+s+s2}{\PYZdq{}}\PY{p}{,} \PY{n}{tipping}\PY{o}{.}\PY{n}{output}\PY{p}{[}\PY{l+s+s1}{\PYZsq{}}\PY{l+s+s1}{tip}\PY{l+s+s1}{\PYZsq{}}\PY{p}{]}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}

    \begin{Verbatim}[commandchars=\\\{\}]
Tip should be around 12.889795918367348
    \end{Verbatim}

    \begin{tcolorbox}[breakable, size=fbox, boxrule=1pt, pad at break*=1mm,colback=cellbackground, colframe=cellborder]
\prompt{In}{incolor}{10}{\boxspacing}
\begin{Verbatim}[commandchars=\\\{\}]
\PY{n}{tip}\PY{o}{.}\PY{n}{view}\PY{p}{(}\PY{n}{sim}\PY{o}{=}\PY{n}{tipping}\PY{p}{)}
\end{Verbatim}
\end{tcolorbox}
    \begin{center}
    \adjustimage{max size={0.9\linewidth}{0.9\paperheight}}{./figures/FCS4.png}
    \end{center}
    { \hspace*{\fill} \\}

MCA/Machine Learning/Project file/Tex source/Fuzzy Control System.tex