\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[margin=1in]{geometry}
\usepackage{amsmath}
\usepackage{amssymb} %need this for boldface C
\usepackage{verbatim} %for inline code
\usepackage{indentfirst}

%For images
\usepackage{graphicx}
\usepackage{subcaption}
\usepackage[export]{adjustbox}
\usepackage{wrapfig}

\usepackage{float}

\setlength{\parindent}{4em}
\setlength{\parskip}{0.5em}

\title{CTA200 Assignment 2}
\author{Matthew Leung}
\date{May 8, 2020}

\begin{document}

\maketitle

\section{Introduction}

In this investigation, we will look at the Mandelbrot set and a system of ordinary differential equations (ODEs) used for infectious disease modelling in epidemiology.

\section{Mandelbrot set}
\subsection{Background}
Consider the set of points $S=\{c=x+iy \in \mathbb{C} \mid x \in (-2,2), y \in (-2,2), x,y \in \mathbb{R} \}$. For each point $c \in S$, let us iterate over the recurrence relation $z_{i+1} = z_i^2+c$, with $z_0 = 0$. If $|z| > 2$, then the sequence diverges. The set of points such that $z$ does not diverge is called the Mandelbrot set.

\subsection{Methods}
The Mandelbrot set was plotted on a 2-dimensional plot, with colours representing the iteration number $i$ at at which the sequence $z$ diverged for every point $c$. Using Python, and the Numpy and Matplotlib libraries, the Mandelbrot set was plotted. The set $S$ was represented using two $1 \times n$ Numpy arrays, for the values of $x$ and $y$, with $n=1000$. The Numpy  \verb|meshgrid()| function was applied to these two arrays to create a 2-dimensional mesh of the coordinates. This was then passed into a function to generate the iteration number $i$ for divergence for each $c$. Using a \verb|for| loop, the recurrence relation was repeatedly applied to every point using Numpy matrix operations on a $n \times n$ array representing $S$. Another $n \times n$ Numpy array was initialized, which stored boolean values of whether or not a certain point diverged. If it did, then the corresponding $i$ for each point that diverged would be stored in another $n \times n$ Numpy array, which the function would return. Note that the maximum number of iterations for the recurrence relation was selected to be 100. The results were then plotted using the Matplotlib library.

\subsection{Results}

The following figures show the Mandelbrot set, plotted using Python by the above method. Figure 1a shows the Mandelbrot set for $x \in (-2,2)$ and $y \in (-2,2)$, while Figure 1b shows the Mandelbrot set for $x \in (-2,0)$ and $y \in (-1,1)$. The colours show the number of iterations for divergence. From these colour plots, we can see that there is a fractal pattern. The points with darker colours are those that do not diverge. Figure 1c and Figure 1d show a binary black and white image of the Mandelbrot set. The points in black diverge, whereas the points in white are a part of the the Mandelbrot set. With a higher $n$, the fractal patterns will be more evident, with more detail.

\begin{figure}[H]
\centering
\begin{subfigure}{.5\textwidth}
  \centering
  \includegraphics[width=\textwidth]{Mandelbrot_1.png}
  \caption{Colour map, $x \in (-2,2)$ and $y \in (-2,2)$}
  \label{fig:sub1}
\end{subfigure}%
\begin{subfigure}{.5\textwidth}
  \centering
  \includegraphics[width=\textwidth]{Mandelbrot_2.png}
  \caption{Colour map, $x \in (-2,0)$ and $y \in (-1,1)$}
  \label{fig:sub2}
\end{subfigure}
\begin{subfigure}{.5\textwidth}
  \centering
  \includegraphics[width=\textwidth]{Mandelbrot_3.png}
  \caption{Black and white, $x \in (-2,2)$ and $y \in (-2,2)$}
  \label{fig:sub3}
\end{subfigure}%
\begin{subfigure}{.5\textwidth}
  \centering
  \includegraphics[width=\textwidth]{Mandelbrot_4.png}
  \caption{Black and white, $x \in (-2,0)$ and $y \in (-1,1)$}
  \label{fig:sub4}
\end{subfigure}

\caption{Mandelbrot set plotted in Python}
\label{fig:test1}
\end{figure}

\section{Epidemiology Models}
\subsection{Background}
The epidemiology model we will investigate is called the SIR model. In its most basic form, it consists of three system of ODEs, shown below. This models an infectious disease over time.
\begin{align}
    \frac{dS}{dt} &= -\frac{\beta S I}{N}\\
    \frac{dI}{dt} &= \frac{\beta S I}{N} - \gamma I\\
    \frac{dR}{dt} &= \gamma I
\end{align}
$S(t)$ is the number of people susceptible to the disease, who have not yet been infected. $I(t)$ is the number of infected people. $R(t)$ is the number of people who have recovered or who are deceased. $N$ is the total population size, $\beta$ is the average number of contacts per person per time, and $\gamma$ is the transition rate for people who are infected to become either recovered or deceased.

\subsection{Methods}
To solve this system of ODEs, and to plot each of the functions $S(t)$, $I(t)$, and $R(t)$, Python was used, along with the SciPy and Matplotlib libraries. SciPy's \verb|integrate.odeint()| function was used to solve this system of ODEs, with various values of $\beta$ and $\gamma$. The initial conditions were $I(0) = 1$, $S(0)=999$, and $R(0) = 0$. $N$ was set to be 1000 people, and the time $t$ was in the interval $t \in [0,200]$. The solutions to the ODEs were plotted using Matplotlib.

\subsection{Results}

\begin{figure}[H]
\centering
\begin{subfigure}{.5\textwidth}
  \centering
  \includegraphics[width=\textwidth]{SIR_1.png}
  \caption{$\beta = 0.2, \gamma = 0.1$}
  \label{fig:sub5}
\end{subfigure}%
\begin{subfigure}{.5\textwidth}
  \centering
  \includegraphics[width=\textwidth]{SIR_2.png}
  \caption{$\beta = 0.5, \gamma = 0.1$}
  \label{fig:sub6}
\end{subfigure}
\begin{subfigure}{.5\textwidth}
  \centering
  \includegraphics[width=\textwidth]{SIR_3.png}
  \caption{$\beta = 0.12, \gamma = 0.07$}
  \label{fig:sub7}
\end{subfigure}%
\begin{subfigure}{.5\textwidth}
  \centering
  \includegraphics[width=\textwidth]{SIR_4.png}
  \caption{$\beta = 0.4, \gamma = 0.3$}
  \label{fig:sub8}
\end{subfigure}

\caption{SIR model with various $\beta$ and $\gamma$}
\label{fig:test2}
\end{figure}

Figure 2 above shows the solution of the system of ODEs for various $\beta$ and $\gamma$. Consider Figure 2b in comparison with Figure 2a. In Figure 2b, $\beta$ is larger, meaning that people have contact with others more frequently. This results in a larger amount of people being infected. If people have less contact with each other, then less people will become infected, as shown in Figure 2c. If $\gamma$ is higher, then that means that people will recover faster, and there will be less infections overall, as shown in Figure 4d. Figure 4c and Figure 4d show the concept of ``flattening the curve''. Decreasing $\beta$ and increasing $\gamma$ would cause less infections, with infections being spread out over a longer period of time.

This is especially relevant at the time of this report, during the current global COVID-19 pandemic. The SIR model here shows that social distancing (decreasing $\beta$) is effective in lowering the amount of people who are infected. In addition, if a vaccine is developed quickly, or if more people recover (increasing $\gamma$), then there will be less infections. However, the SIR model is very simple, and does not account for many other factors. The SIR model can be greatly elaborated.

\subsection{SIRD Model}
A slightly more elaborate version of the SIR model is the SIRD model. It is similar to the SIR model but with a few differences. We introduce a new function $D(t)$, which is the number of deaths in the population. $R(t)$ is strictly now the number of people who have recovered. $\gamma$ is now the recovery rate, and we introduce a new constant, $\mu$, which is the mortality rate. The SIRD model has the following system of ODEs.
\begin{align}
    \frac{dS}{dt} &= -\frac{\beta S I}{N}\\
    \frac{dI}{dt} &= \frac{\beta S I}{N} - \gamma I - \mu I\\
    \frac{dR}{dt} &= \gamma I\\
    \frac{dD}{dt} &= \mu I
\end{align}
Note that equations (4) and (6) are the same as equations (1) and (3) respectively. Equation (5) is the same as equation (2), but with an additional $-\mu I$ term. The number of infected people will also decrease if more people become deceased. Equation (7) is a new equation. Figure 3 below shows the solution for a SIRD model with the same initial conditions as the plots in Figure 2, but with $\beta=0.2$, $\gamma = 0.1$, and $\mu=0.02$. Note that $D(0)=0$.

\begin{figure}[H]
\centering
\includegraphics[width=0.5\textwidth]{SIRD_1.png}
\caption{SIRD model with $\beta=0.2, \gamma = 0.1, \mu=0.02$}
\label{fig:figure3}
\end{figure}

This model could tell us more information since the deaths in the population are separately accounted for. However, in reality, infectious diseases are much more complex, with many more factors, some of which cannot be truly accounted for. The SIR and SIRD models are just a basic picture of what could happen. A model is only as good as its assumptions.

\end{document}


% !TEX root =../thesis-letomes.tex

\chapter{Literature Overview}
\section{Background: Transfer Orbits}
In traditional Hohmann transfer orbits, the spacecraft is moved between two orbits around a central body along an elliptical orbit as in \cref{fig:hohmann}. However when rendezvousing with another body at the arrival orbit, a breaking maneuver is required (as opposed to a speeding up maneuver as depicted in \cref{fig:hohmann}). This is simply due to the fact that the escape velocity is $\sqrt{2}$ times the circular orbit speed at a given altitude. It is this breaking maneuver that low energy transfer orbits seek to avoid. This is done by travelling between bodies along regions\footnote{Also called a ``weak stability boundary'', this is a region around any large body where objects will be ``weakly captured'' by the body orbited. Therefore LETOs are also known a ``weak stability boundary trajectories''} near their Lagrange points\footnote{Lagrange points are points of equilibrium in a two-body system in their rotating non-inertial frame of reference. For a great animation showing these, see the bottom of \url{https://leancrew.com/all-this/2016/08/lagrange-points-redux/}} of minimum energy, see \cref{fig:lagrange-points-energy-contour-lines}. LETOs allow us to trade a longer trip time with lower delta-v (and fuel) requirements by intentionally passing through these regions and by coming in at just at just the right angle and velocity vector.

\begin{figure}[ht]
    \centering
    \includegraphics[width=0.37\linewidth]{fig/hohmann.pdf}
    \caption{Hohmann Transfer orbit that brings the satellite from one circular orbit around a central body to another circular orbit using two burns (Image: \cite{Leafnode} (modified)).}
    \label{fig:hohmann}
\end{figure}

\begin{figure}[ht]
    \centering
    \includegraphics[width=0.70\linewidth]{fig/lagrange-points-energy-contour-lines.png}
    \caption{Contour lines of potential energy and Lagrange points of a two-body system. LETOs go through the region along L1 and L2 for capture at the smaller body or lowest energy travel to the solar system beyond (Image: \cite{Drang}).}
    \label{fig:lagrange-points-energy-contour-lines}
\end{figure}
\section{1988-2014: LETOs to the Moon and Speculations on an Interplanetary Network of LETOs}
Low energy transfer orbits (also known as ballistic capture) can be traced back to conference proceedings in in 1987, where Edward Belbruno first discovered it as a cheaper way to the Moon under the constraint of using just electric propulsion, a class of engines known for their high efficiency but low thrust. It turned out to be possible in theory, albeit the trip would take nearly 2 years \cite{Belbruno1987, Benson}.

The concept of LETOs was first tried in 1990 to manuever the Japanese spacecraft Hiten to the Moon. Japan had launched two spacecraft into Earth's orbit, Hiten and Hagoromo. Hiten was just meant as a relay satellite and Hagoromo was supposed to go to the Moon via a standard Hohmann transfer (takes about 4 days), but never made it. The Hiten only had 10\% of the fuel needed for a Hohmann transfer. The Japanese team was desperate to try anything to make something interesting out of their mission, so they contacted Belbruno, who then designed an orbit with colleague James K. Miller from JPL that could bring Hiten to the Moon in 4 months down from 2 years by taking the Sun's constant force into consideration. The maneuver was a success \cite{Belbuno1990} \cite{Benson}. \cref{fig:hiten} shows the trajectory.

\begin{figure}[ht]
    \centering
    \includegraphics[width=0.60\linewidth]{fig/hiten.png}
    \caption{The Hiten spacecraft performed the first attempt at a LETO to the Moon ever in 1990, a 4 month trip that was a success (Image: \cite{Belbuno1990}).}
    \label{fig:hiten}
\end{figure}

After Hiten, a small number of missions have utilized LETOs in their missions \cite{Wikipedia}:
\begin{itemize}
	\item SMART-1 (ESA, 2003--2006): Was used to test solar electric propulsion and other deep-space technologies, while performing scientific observations of the Moon before deliberately crashing into the surface \cite{ESA}. Used electric propulsion to gradually make it's way to the moon.
    \item Genesis (NASA, 2001--2004): Collected solar wind samples at the Earth-Sun L1 Lagrange point and returned them to Earth for study \cite{NASAa}.
    \item GRAIL (NASA, 2011-2012): A pair of lunar orbiters that measured the gravitational field of the Moon very precisely, which could be used to infer the internal structure of the Moon. Entered into lunar orbit via a LETO very similar to Hiten \cite{NASA2013,Wikipediaa}.
\end{itemize}

Since their discovery and usage in the 90's, the existence of a network of LETOs between planets in the solar system have been studied under the name ``Interplanetary Transport Network'' \cite{Ross2006}, although no concrete suggestions for actual advantageous LETO strategies to Mars was suggested until 2014.

\section{2014: Earth-Mars LETOs}
Earth-Mars LETOs have long thought to not be possible, but in 2014 this topic was explored in \cite{Topputo2014}. They proposed going to Mars in two steps:
\begin{enumerate}
	\item First a transfer orbit to a point $\mathrm{x}_c$ somewhere in the vicinity of Mars on the order of \SIrange{e6}{e7}{\km}, see \cref{fig:TransferStructure,fig:earth-mars-ballistic}.
	\item Then small delta-v $\Delta V_c$ is applied at $\mathrm{x}_c$ to inject the spacecraft into the ballistic capture orbit.
\end{enumerate}

\begin{figure}[ht]
    \centering
    \includegraphics[width=0.70\linewidth]{fig/TransferStructure.eps}
    \caption{Structure of the ballistic capture transfers to Mars proposed by Topputo and Belbruno: First transfer to a point near mars, then make a ballistic capture (Image: \cite{Topputo2014}).}
    \label{fig:TransferStructure}
\end{figure}

\begin{figure}
    \centering
    \subfloat[An Earth-Mars ballistic capture in something resenbling a standard Hohmann transfer.]{
        \includegraphics[width=0.47\linewidth]{fig/Sol1New.eps}
        \label{fig:earth-mars-ballistic-standard-hohmann}
    }
    \hfill
    \subfloat[An Earth-Mars ballistic capture at a different launch time with a much longer capture.]{
        \includegraphics[width=0.47\linewidth]{fig/Sol12New.eps}
        \label{fig:earth-mars-ballistic-long-capture}
    }
    \caption{Two possible Earth-Mars LETOs, demonstrating the expanded launch window from Earth, an advantage over the standard Hohmann transfer where Mars must be $44^\degree$ ahead of Earth (see \cref{fig:Hohmann-angle}) which happens every 26 months (Image: \cite{Topputo2014}).}
    \label{fig:earth-mars-ballistic}
    \end{figure}

However it is clear that this is still very early work, and the authors came to a couple of main conclusions:
\begin{enumerate}
	\item \textbf{Lower delta-v than Hohmann to Mars can be achieved}, but only for high-altitude orbits with apoapsis higher than \SI{22000}{\km}. The authors then simply postulate that goods can be delivered to low orbit or the surface easily, without any detail on how. It is not clear whether they consider aerobraking\footnote{A spacecraft maneuver that decreases the high point (apoapsis) of the orbit by flying through the atmosphere at the low point (periapsis) on the orbit, thereby slowing due to friction with the atmosphere.} a realistic option.
	\item \textbf{Expanded launch windows}, since the transfer orbit does not target one particular moving point (Mars) but many possible regions close to Mars. With a Hohmann transfer orbit, launches are limited to a few days every 26 months, but this type of Earth-Mars LETOs can be launched any time of the year, with varying trip times of up to a few years. However this is still faster than waiting more than 2 years for the next launch window for a traditional Hohmann \footnote{Incidentally, Belbruno have jokingly mocked the  movie ``The Martian'' for not taking LETOs to Mars into account when astronaut Mark Watney was stuck on Mars and badly in need of supplies from Earth \url{https://www.space.com/30749-the-martian-faster-way-to-mars.html}}.
	\item \textbf{Safer arrival conditions.} Topputo and Belbruno argues that their LETOs to Mars are in some sense safer than a Hohmann transfer due to the precise timing and high impulse required for the latter, whereas LETOs are more gradual and forgiving in nature and can tolerate temporary problems without being hurled into space or smashed into the surface of Mars.
\end{enumerate}
Whether these are convincing arguments to the wider community and organizations such as NASA and SpaceX remains to be seen. The first point above in particular seems to lack detail, and the authors admit that there is lots of work to be done in future studies on the topic.

However the paper did encourage us to proceed and try to find LETOs to Mars.

\section{1980's-Today: Searching for LETOs Using Neural Networks and Evolution Strategies}
The literature on combining modern machine learning methods, such as neural networks or evolution strategies, goes back many years, even decades. \cite{Izzo2018} does a good job of providing an overview of the field of AI techniques applied to finding trajectories. The field of AI as a whole has experienced a renaissance in the second decade of the 21st century, and the ever-increasing computing power have unlocked the potential of methods that once seemed intractable, such as Evolution Strategies \cite{Salimans2017} (to be introduced in \cref{sec:ES}).

``\textit{The use of machine learning (ML) algorithms to aid the design of interplanetary trajectory is not as widely researched as that of evolutionary techniques and is limited to fewer works. The reasons are to be found in the less obvious applicability of these methods to the problems encountered in the design of interplanetary trajectories and in the lack of data sets produced and made available by the aerospace community}.'' \cite{Izzo2018}.

In 2016 \cite{Sanchez-Sanchez2016} explored using a deep neural network to learn the optimal control action for \emph{landing} a spacecraft, assuming perfect information.

According to \cite{Izzo2018}, the latest works in the cross-field of ML and interplanetary trajectories have all been limited to landing problems and the applicability of these ideas to interplanetary trajectories has not been studied before now. The authors concluded that ``\textit{It appears that a deep network is able to represent the optimal guidance structure of the Earth-Mars transfer quite satisfactorily introducing errors that are, on average, rather small.}''.

In conclusion, the field of using evolutionary algorithms in the space of optimal trajectory search was surprisingly rich, but so was the variety of genetic algorithms applied. We therefore still found it worthwhile to experiment with the version treated in \cite{Salimans2017} (to be introduced in \cref{sec:ES}) and see what we could find.


% $Header: /cvsroot/latex-beamer/latex-beamer/solutions/conference-talks/conference-ornate-20min.en.tex,v 1.6 2004/10/07 20:53:08 tantau Exp $

\documentclass{beamer}
%\documentclass[handout]{beamer}
%\usepackage{pgfpages}
%\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm]

% This file is a solution template for:

% - Talk at a conference/colloquium.
% - Talk length is about 20min.
% - Style is ornate.

% Copyright 2004 by Till Tantau <tantau@users.sourceforge.net>.
%
% In principle, this file can be redistributed and/or modified under
% the terms of the GNU Public License, version 2.
%
% However, this file is supposed to be a template to be modified
% for your own needs. For this reason, if you use this file as a
% template and not specifically distribute it as part of a another
% package/program, I grant the extra permission to freely copy and
% modify this file as you see fit and even to delete this copyright
% notice.

\mode<presentation>
{
%  \usetheme{Warsaw}
%  \usetheme{Boadilla}
%  \usetheme{Goettingen}
%  \usetheme{Hannover}
%  \usetheme{Madrid}
%  \usetheme{Marburg}
%  \usetheme{Montpellier}
%  \usetheme{Pittsburgh}
  \usetheme{Hawke}
  % or ...

  \setbeamercovered{transparent}
  % or whatever (possibly just delete it)
}

\usepackage[english]{babel}
% or whatever

\usepackage[latin1]{inputenc}
% or whatever

\usepackage{times}
\usepackage[T1]{fontenc}
% Or whatever. Note that the encoding and the font should match. If T1
% does not look nice, try deleting the line with the fontenc.

\usepackage{multimedia}

%%%%%%
% My Commands
%%%%%%

\newcommand{\ml}{{\sc matlab}}
\newcommand{\bb}{{\boldsymbol{b}}}
\newcommand{\bx}{{\boldsymbol{x}}}
\newcommand{\by}{{\boldsymbol{y}}}
\newcommand{\bfm}[1]{{\boldsymbol{#1}}}

%%%%

\title[Lecture 15] % (optional, use only with long paper titles)
{Lecture 15 - Initial Value Problems}

% \subtitle
% {Include Only If Paper Has a Subtitle}

\author[I. Hawke] % (optional, use only with lots of authors)
{I.~Hawke}
% - Give the names in the same order as the appear in the paper.
% - Use the \inst{?} command only if the authors have different
%   affiliation.

\institute[University of Southampton] % (optional, but mostly needed)
{
%  \inst{1}%
  School of Mathematics, \\
  University of Southampton, UK
}
% - Use the \inst command only if there are several affiliations.
% - Keep it simple, no one is interested in your street address.

\date[Semester 1] % (optional, should be abbreviation of conference name)
{MATH3018/6141, Semester 1}
% - Either use conference name or its abbreviation.
% - Not really informative to the audience, more for people (including
%   yourself) who are reading the slides online

\subject{Numerical methods}
% This is only inserted into the PDF information catalog. Can be left
% out.

% If you have a file called "university-logo-filename.xxx", where xxx
% is a graphic format that can be processed by latex or pdflatex,
% resp., then you can add a logo as follows:

\pgfdeclareimage[height=0.5cm]{university-logo}{mathematics_7469}
\logo{\pgfuseimage{university-logo}}

% Delete this, if you do not want the table of contents to pop up at
% the beginning of each subsection:
%  \AtBeginSubsection[]
%  {
%    \begin{frame}<beamer>
%      \frametitle{Outline}
%      \tableofcontents[currentsection,currentsubsection]
%    \end{frame}
%  }
\AtBeginSection[]
{
  \begin{frame}<beamer>
    \frametitle{Outline}
    \tableofcontents[currentsection]
  \end{frame}
}

% If you wish to uncover everything in a step-wise fashion, uncomment
% the following command:

%\beamerdefaultoverlayspecification{<+->}

\begin{document}

\begin{frame}
  \titlepage
\end{frame}

\section{Ordinary Differential Equations}

\subsection{Ordinary Differential Equations}

\begin{frame}
  \frametitle{Ordinary Differential Equations}

  Consider first order systems of ordinary differential
  equations (ODEs), written
  \begin{equation*}
    \by'(x) = \bfm{f}(x, \by(x)).
  \end{equation*}
  Solution: any function $\by(x)$ that satisfies the equation. \pause

  \vspace{1ex}

  There are many possible solutions; for a \emph{unique} solution need
  to specify $n$ independent conditions (for system size $n$). \pause

  \vspace{1ex}

  When the $n$ conditions all specified at one point, e.g.
  \begin{equation*}
    \by(0) = \by_0,
  \end{equation*}
  this is an \emph{initial value problem}.

\end{frame}

\begin{frame}
  \frametitle{Getting to the first order form}

  The ODE need not be written in first order form, e.g.\
  \begin{equation*}
    z'' + z' + 1 = 0, \quad z(0) = a, \,\, z'(0) = b.
  \end{equation*} \pause

  Can recast the problem using \emph{auxilliary variables}, such as $w
  = z'$. \pause Result:
  \begin{equation*}
    \begin{pmatrix}
      w \\ z
    \end{pmatrix}' =
    \begin{pmatrix}
      -w - 1 \\ w
    \end{pmatrix}, \quad z(0) = a, \, \, w(0) = b.
  \end{equation*} \pause

  It is not always best to use simple derivatives as auxilliary
  variables. Example:
  \begin{equation*}
    \tfrac{1}{r^2} \left( r^2 P' / \rho \right)' = - 4 \pi \rho.
  \end{equation*}
  Use $P(r)$ and auxilliary variable
  \begin{equation*}
    \Pi(r) = r^2 P' / \rho.
  \end{equation*}

\end{frame}

\subsection{Numerical differentiation}

\begin{frame}
  \frametitle{Numerical differentiation}

  Assume value of $f(x)$ known at points $\{ x_j \}$. One approach
  uses these values to compute the derivative. \pause Simple example:
  assume $f(x)$ is known at $x_0$ and $x_0 \pm h$ for some small step
  $h$. \pause

  \vspace{1ex}

  Fit a linear polynomial $g$ (a straight line) to the values of $f(x)$ at
  $\{x_0, x_0 + h\}$: the derivative in this interval is the slope of
  the line
  \begin{equation*}
    f'(x_0) \simeq g'(x_0) = \frac{f(x_0 + h) - f(x_0)}{h}.
  \end{equation*}
  This is the \emph{forward difference} estimate of the derivative.
  \pause

  \vspace{1ex}

  Similarly, a linear fit in $[x_0 - h, x_0]$ gives
  \begin{equation*}
    f'(x_0) \simeq g'(x_0)  = \frac{f(x_0) - f(x_0 - h)}{h}.
  \end{equation*}
  This is the \emph{backward difference} estimate of the derivative.

\end{frame}

\begin{frame}
  \frametitle{Numerical differentiation - accuracy}

  \begin{equation*}
    f'(x_0) \simeq g'(x_0) = \frac{f(x_0 + h) - f(x_0)}{h}.
  \end{equation*}
  The accuracy of the approximation depends on the choice of
  interpolating polynomial. \pause

  \vspace{1ex}

  Check accuracy using Taylor expansion:
  \begin{align*}
    && f(x_0 + h) & = f(x_0) + h f'(x_0) + \tfrac{h^2}{2!} f''(x_0) +
    {\cal O}(h^3) \\
    \Rightarrow && \frac{f(x_0 + h) - f(x_0)}{h} & = \frac{f(x_0) + h
      f'(x_0) + \tfrac{h^2}{2!} f''(x_0) + {\cal O}(h^3) - f(x_0)}{h}
    \\
    &&& = f'(x_0) + {\cal O}(h).
  \end{align*} \pause

  \vspace{1ex}

  The same accuracy result holds for
  \begin{equation*}
    f'(x_0) = \frac{f(x_0) - f(x_0 - h)}{h}.
  \end{equation*}

\end{frame}

\begin{frame}
  \frametitle{Example}

  Compute the derivative of $f(x) = e^{x}$ at $x=0$ using forward
  differencing,
  \begin{equation*}
    f'(x_0) = \frac{f(x_0 + h) - f(x_0)}{h},
  \end{equation*}
  checking the error to the exact result $f'(0) \equiv 1$.
  \begin{center}
    \begin{tabular}{c|c c c c}
      $h$ & Formula & $f'_h(0)$ & $e_h$ & Error ratio \\[0.5ex] \hline
      0.5 & \rule{0pt}{1.5em} $\frac{\exp(0.5) - \exp(0)}{0.5}$ & 1.297 & 0.297 & -- \\
      0.05 & \rule{0pt}{1.5em} $\frac{\exp(0.05) - \exp(0)}{0.05}$ & 1.0254 & 0.0254 &
      0.085 \\
      0.005 & \rule{0pt}{1.5em} $\frac{\exp(0.005) - \exp(0)}{0.005}$ & 1.00250 & 0.0025 &
      0.098
    \end{tabular}
  \end{center}

  \vspace{1ex}

  The error ratio is (asymptotically) the same as the step-size ratio.

\end{frame}

\begin{frame}
  \frametitle{Central differencing}

  For more accuracy use a quadratic through the points $\{x_0 - h,
  x_0, x_0 + h\}$:
  \begin{equation*}
    f(x) \simeq g(x) = \alpha (x - x_0)^2 + \beta (x - x_0) + \gamma.
  \end{equation*}
  Compute the derivative at $x_0$:
  \begin{equation*}
    f'(x_0) = \beta = \frac{f(x_0 + h) - f(x_0 - h)}{2 h}.
  \end{equation*} \pause

  \vspace{1ex}

  This \emph{central differencing} estimate is more accurate than
  forward/backward differencing:
  \begin{equation*}
    \frac{f(x_0 + h) - f(x_0 - h)}{2 h} = f'(x_0) + {\cal O}(h^2).
  \end{equation*}

\end{frame}

\begin{frame}
  \frametitle{Example}

  Compute the derivative of $f(x) = e^{x}$ at $x=0$ with central
  differencing,
  \begin{equation*}
    f'(x_0) = \frac{f(x_0 + h) - f(x_0 - h)}{2 h},
  \end{equation*}
  checking the error to the exact result $f'(0) \equiv 1$.
  \begin{center}
    \begin{tabular}{c|c c c}
      $h$ & Formula & $f'_h(0)$  & Error ratio \\ \hline
      0.5 & \rule{0pt}{1.5em} $\frac{\exp(0.5) - \exp(-0.5)}{1}$ & 1.042  & -- \\
      0.05 & \rule{0pt}{1.5em} $\frac{\exp(0.05) - \exp(-0.05)}{0.1}$ & 1.0004167 &
      0.00988 \\
      0.005 & \rule{0pt}{1.5em} $\frac{\exp(0.005) - \exp(-0.005)}{0.01}$ & 1.0000004180
      & 0.0100
    \end{tabular}
  \end{center}
  Central differencing is much more accurate, and the error ratio is
  (asymptotically) the \emph{square} of the step-size ratio.

\end{frame}

\begin{frame}
  \frametitle{Other numerical differencing formulas}

  \begin{itemize}
  \item Intuitively, use of information from both sides of $x_0$ leads
    to higher accuracy (e.g.\ central differencing). \pause
  \item Can build differencing formulas directly from Taylor
    series. No interpolating function, but are equally valid.  \pause
  \item Estimates of higher derivatives can use interpolation approach
    or repeated numerical differencing. For example, estimate $f''(x)$
    using forward and backward differencing:
    \begin{align*}
      f''(x_0) & \simeq \frac{f'(x_0) - f'(x_0 - h)}{h} \\
               & \simeq \frac{\left(f(x_0 + h) - f(x_0) \right) -
                 \left(f(x_0) - f(x_0 - h) \right)}{h^2} \\
               & = \frac{f(x_0 + h) + f(x_0 - h) - 2 f(x_0)}{h^2}.
    \end{align*}
  \end{itemize}
\end{frame}

\subsection{Euler's method}

\begin{frame}
  \frametitle{Euler's method}

  Using differencing construct the simplest method for solving
  \begin{equation*}
    \by'(x) = \bfm{f}(x, \by(x)), \quad \by(0) = \by_0.
  \end{equation*}
  Using step size $h$, apply forward differencing to $\by$:
  \begin{equation*}
    \frac{\by_{n+1} - \by_n}{h} = \bfm{f}(x_n, \by_n) \quad
    \Rightarrow \quad \by_{n+1}  = \by_n + h \bfm{f}(x_n, \by_n).
  \end{equation*} \pause
  Given initial data $\by_0$, for each stage we can compute
  $\by_{n+1}$ from data $\by_n$. Methods of this sort are called
  \emph{explicit}. \pause

  \vspace{1ex}

  Compute the error using Taylor expansion:
  \begin{equation*}
    \by_{n+1} \equiv \by \left(x_0 + (n+1) h \right) = \by_n + h
    \bfm{f}(x_n, \by_n) + {\cal O}(h^2).
  \end{equation*} \pause
  This means the \emph{local} error is ${\cal O}(h^2)$, not the global
  error.

\end{frame}

\begin{frame}
  \frametitle{Example}

  Apply Euler's method to
  \begin{equation*}
    y'(x) = - \sin(x), \quad y(0) = 1
  \end{equation*}
  up to $x = 0.5$. Using step size $h = 0.1$ gives
  \begin{center}
    \begin{tabular}{c|c c c c}
      $n$ & $x_n$ & $y_n$ & $f(x_n, y_n) = -\sin(x_n)$ & $\cos(x_n)$ \\
      \hline
      0 & 0.0 & 1.00  &  0.000 & 1.000 \\
      1 & 0.1 & 1.00  & -0.100 & 0.995 \\
      2 & 0.2 & 0.99  & -0.199 & 0.980 \\
      3 & 0.3 & 0.97  & -0.296 & 0.955 \\
      4 & 0.4 & 0.94  & -0.389 & 0.921 \\
      5 & 0.5 & 0.90  &        & 0.878
    \end{tabular}
  \end{center} \pause
  The error is $2.4\%$; using $h = 0.01$ the final result is $y_{50} =
  0.880$, an error of $0.24\%$: first order convergence.

\end{frame}

\begin{frame}
  \frametitle{Convergence}

  \begin{center}
    \includegraphics[height=0.7\textheight]{figures/EulerSinConvergence}
  \end{center}
   More conclusive evidence that the convergence of Euler's method is
   first order: the global error is $\propto h$.

\end{frame}

\begin{frame}
  \frametitle{Example: 2}

  Consider the system
  \begin{equation*}
    \left\{
      \begin{aligned}
        \dot{x} & = -y \\ \dot{y} & = x
      \end{aligned} \right., \quad x(0) = 1, \, \, y(0) = 0.
  \end{equation*}
  In polar coordinates this is $\dot{r} = 0$, $\dot{\phi} = 1$.
  \begin{columns}
    \begin{column}{0.5\textwidth}
      \begin{overlayarea}{\textwidth}{0.4\textheight}
        \only<2-3|handout:1>
        {
          Use Euler's method with $h=0.1$. At $t=50$ the result is
          slightly out of phase and the radius has grown
          significantly.

        }
        \only<3|handout:1>
        {
          \vspace{1ex}

          With $h = 0.01$ the errors are smaller, but still visible.
        }
      \end{overlayarea}
    \end{column}
    \begin{column}{0.5\textwidth}
      \begin{overlayarea}{\textwidth}{0.6\textheight}
        \only<2|handout:0>
        {
          \begin{center}
            \includegraphics[height=0.5\textheight]{figures/EulerPlot1_crop}
          \end{center}
        }
        \only<3|handout:1>
        {
          \begin{center}
            \includegraphics[height=0.5\textheight]{figures/EulerPlot2_crop}
          \end{center}
        }
      \end{overlayarea}
    \end{column}
  \end{columns}

\end{frame}

\begin{frame}
  \frametitle{Convergence: 2}

  \begin{center}
    \includegraphics[height=0.7\textheight]{figures/EulerCircleConvergence}
  \end{center}
  More conclusive evidence that the convergence of Euler's method is
  (asymptotically) first order: the global error is $\propto h$.

\end{frame}

\section{Summary}

\subsection{Summary}

\begin{frame}
  \frametitle{Summary}

  \begin{itemize}
  \item Initial value problems are ODEs specified by an initial
    condition $\by(0) = \by_0$.
  \item Numerical differentiation using finite differences can easily produce
    \begin{itemize}
    \item first order accurate approximations to $f'(x)$ using
      \emph{forward} or \emph{backward} differencing, or
    \item second order accurate approximations to $f'(x)$ using
      central differencing.
    \end{itemize}
  \item General central differencing about $x_i$ uses the closest
    points to $x_i$ in a symmetric fashion to approximate whichever
    derivative is required.
  \item To prove the convergence behaviour Taylor's theorem is used.
  \item Approximations to higher derivatives are also easy to compute.
  \item Using forward differencing in the IVP gives Euler's method.
  \item Euler's method has a local error $\propto h^2$, and a global
    error $\propto h$.
  \end{itemize}

\end{frame}

\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:


\section{Next steps}

In this section we will discuss our next steps, what we think we will try based on the current finding. According to the current result, textual content of each request provide a good source of information that we want to explore further. We have tried to used a simple bag of word model as input for our logistic regression, however, this is not the best possible approach.\\

Recently more effective un-supervised techniques such as topic modeling or vector space representation (word2vec) have emerged. Not only such techniques could improve our prediction accuracy, they would also help us under stand more in details the kind of request that have the most successful rate. For example, topic modeling would help us cluster word or phrases into different topics and by analyze the weight assigned by logistic regression to each topic, we will see which of the topics are more important in creating a successful request.\\

Vector space representation has been applied very successful in the field of natural language processing. In particular, word2vec has gained popularity due to it simplify and the fact that the tool was made available by Google. The idea is that word2vec will encored each word as a \q{semantic} vector, with the similar vectors represent similar word (e.g. apple and orange will be similar because they are both fruits), not only that these vector also display very interesting linear sub-structure (e.g. vector(queen)-vector(woman)+vector(man) is similar to vector(king)). We hope that with this \q{semantic} encoding our prediction will be much stronger.\\

Last but not least, we plan to use a more sophisticate classifier, namely Support Vector Machine (SVM). This classifier will provide much more generalized model that will help increase the overall accuracy.


%
% This is a borrowed LaTeX template file for lecture notes for CS267,
% Applications of Parallel Computing, UCBerkeley EECS Department.
% Now being used for CMU's 10725 Fall 2012 Optimization course
% taught by Geoff Gordon and Ryan Tibshirani.  When preparing
% LaTeX notes for this class, please use this template.
%
% To familiarize yourself with this template, the body contains
% some examples of its use.  Look them over.  Then you can
% run LaTeX on this file.  After you have LaTeXed this file then
% you can look over the result either by printing it out with
% dvips or using xdvi. "pdflatex template.tex" should also work.
%

\documentclass[twoside]{article}
\setlength{\oddsidemargin}{0.25 in}
\setlength{\evensidemargin}{-0.25 in}
\setlength{\topmargin}{-0.6 in}
\setlength{\textwidth}{6.5 in}
\setlength{\textheight}{8.5 in}
\setlength{\headsep}{0.75 in}
\setlength{\parindent}{0 in}
\setlength{\parskip}{0.1 in}

%
% ADD PACKAGES here:
%

\usepackage{amsmath,amsfonts,graphicx}

%
% The following commands set up the lecnum (lecture number)
% counter and make various numbering schemes work relative
% to the lecture number.
%
\newcounter{lecnum}
\renewcommand{\thepage}{\thelecnum-\arabic{page}}
\renewcommand{\thesection}{\thelecnum.\arabic{section}}
\renewcommand{\theequation}{\thelecnum.\arabic{equation}}
\renewcommand{\thefigure}{\thelecnum.\arabic{figure}}
\renewcommand{\thetable}{\thelecnum.\arabic{table}}

%
% The following macro is used to generate the header.
%
\newcommand{\lecture}[4]{
   \pagestyle{myheadings}
   \thispagestyle{plain}
   \newpage
   \setcounter{lecnum}{#1}
   \setcounter{page}{1}
   \noindent
   \begin{center}
   \framebox{
      \vbox{\vspace{2mm}
    \hbox to 6.28in { {\bf EE402 - Discrete Time Systems
	\hfill Spring 2018} }
       \vspace{4mm}
       \hbox to 6.28in { {\Large \hfill Lecture #1 \hfill} }
       \vspace{2mm}
       \hbox to 6.28in { {\it Lecturer: #2 \hfill } }
      \vspace{2mm}}
   }
   \end{center}
   \markboth{Lecture #1}{Lecture #1}

   \vspace*{4mm}
}
%
% Convention for citations is authors' initials followed by the year.
% For example, to cite a paper by Leighton and Maggs you would type
% \cite{LM89}, and to cite a paper by Strassen you would type \cite{S69}.
% (To avoid bibliography problems, for now we redefine the \cite command.)
% Also commands that create a suitable format for the reference list.
\renewcommand{\cite}[1]{[#1]}
\def\beginrefs{\begin{list}%
        {[\arabic{equation}]}{\usecounter{equation}
         \setlength{\leftmargin}{2.0truecm}\setlength{\labelsep}{0.4truecm}%
         \setlength{\labelwidth}{1.6truecm}}}
\def\endrefs{\end{list}}
\def\bibentry#1{\item[\hbox{[#1]}]}

%Use this command for a figure; it puts a figure in wherever you want it.
%usage: \fig{NUMBER}{SPACE-IN-INCHES}{CAPTION}
\newcommand{\fig}[3]{
			\vspace{#2}
			\begin{center}
			Figure \thelecnum.#1:~#3
			\end{center}
	}
% Use these for theorems, lemmas, proofs, etc.
\newtheorem{theorem}{Theorem}[lecnum]
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem{claim}[theorem]{Claim}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{definition}[theorem]{Definition}
\newenvironment{proof}{{\bf Proof:}}{\hfill\rule{2mm}{2mm}}

% **** IF YOU WANT TO DEFINE ADDITIONAL MACROS FOR YOURSELF, PUT THEM HERE:

\begin{document}

% Lecture Details
\lecture{9}{Asst. Prof. M. Mert Ankarali}
....

\section*{Steady-Sate (DC) Response Analysis}

Let's remember the final value theorem. Given a discrete time signal
$x[k]$ and its z-transform $X(z)$, if $x[k]$ is convergent sequence
final value theorem states that
%
\begin{align*}
\lim_{k\to \infty} x[k] &= \lim_{z \to 1} \left[ \left( 1 - z^{-1} \right)
  X(z) \right]
\\
x_{ss} &= \lim_{z \to 1} \left[ \frac{z-1}{z} X(z) \right]
\end{align*}

    \begin{center}
\begin{minipage}[h]{0.99\linewidth}
    \begin{center}
      \includegraphics[width=\textwidth]{digitalblock}
    \end{center}
\end{minipage}
    \end{center}

Now let's find the pulse transfer function from the reference signal
$r[k]$ to the error signal $e[k]$, to further analyze the steady-state
error response.
%
\begin{align*}
E(z) &= R(z) - E(z) \left( G_c(z) G(z) \right) , \quad \mathrm{where} \
  G(z) = \mathcal{Z} \lbrace G(s) \rbrace
\\
\frac{E(z)}{R(z)} &= \frac{1}{1 + G_c(z) G(z) }
\end{align*}
%
Note that $G_c(z) G(z)$ is the pulse transfer function from the error
signal $E(z)$ to the signal which is fed to the negative terminal of
the main difference operator, i.e. $F(z)$. This transfer function is
called feed-forward or open-loop  pulse transfer function of the
closed-loop digital control system. For this system,
%
\begin{align*}
\frac{F(z)}{E(z)} = G_{OL} = G_c(z) G(z)
\end{align*}
%
Then $E(z)$ can be written as
%
\begin{align*}
E(z) = R(z) \frac{1}{1 + G_{OL} (z) }
\end{align*}
%
It is obvious that first requirement on m steady-state error
performance is that closed-loop system have to be stable.
Now let's analyze specific but fundamental input scenarios.

\subsection*{Unit-Step Input}

We know that $r[k] = u[k]$ and $R(z) = \frac{1}{1 - z^-1}$ then
we have
%
%
\begin{align*}
e_{ss} &= \lim_{z \to 1} \left[ \left(1 - z^{-1} \right) R(z) \frac{1}{1
         + G_{OL} (z) } \right]
\\
&= \lim_{z \to 1} \left[ \left(1 - z^{-1} \right) \frac{1}{1-z^{-1}} \frac{1}{1
         + G_{OL} (z) } \right]
\\
e_{ss} &= \frac{1}{1 + \lim_{z \to 1} G_{OL} (z) }
\end{align*}
%
If the DC gain of the system (also called static error constant) is
constant, i.e. $G_{OL}(1) = K_{DC}$ then the steady state error can be
computed as
%
\begin{align*}
e_{ss} &= \frac{1}{1 + K_{DC}}
\end{align*}
%
It is obvious that
%
\begin{align*}
e_{ss} &\neq 0 \quad \mathrm{if} \quad |K_{DC}| < \infty
\\
e_{ss} &\to 0 \quad \mathrm{if} \quad K_{DC} \to \infty
\end{align*}
%
Based on these results, we can have the following conclusions
%
\begin{itemize}
\item If $G_{OL} (1) = 0$, then $e_{ss} = 1$. These are \textbf{type
  \textit{negative}} systems, and the steady-state error of step
response type signals are always $100 \%$.
\item If $G_{OL} (1) = K_{DC}$ , $0 < | K_{DC} | < \infty$, then $e_{ss} =
  1/(1 + K_{DC})$. These are \textbf{type 0} systems. We observe a
  bounded steady-state error and it is possible to reduce the by increasing the static gain
constant $K_P$.
\item  If $G_{OL} (1) = \infty$, then $e_{ss} = 0$. These are
  \textbf{type \textit{positive}} systems. The steady-state error
  is perfectly zero for such systems.
\end{itemize}

Now let's generalize the \textit{type} of systems. An \textit{N type}
closed loop system has the following form of open-loop pulse transfer
function
%
\begin{align*}
G_{OL}(z) &= \frac{1}{(z-1)^N} G_{DC}(z) \\
| G_{DC}(1) | &= K_{DC} \quad \mathrm{where} \ 0 < | K_{DC} | < \infty
\end{align*}
%
It is easy to see that for unit-step response
%
\begin{itemize}
\item Type $N < 0$: $e_{ss} = 1$ (or $e_{ss} =100 \%$)
\item Type $N = 0$: $e_{ss} =  1/(1 + K_{DC})$
\item Type $N > 0$: $e_{ss} = 0$
\end{itemize}

\subsection*{Unit-Ramp Input}
%
We know that $r[k] = k u[k]$ and $R(z) = \frac{z^{-1}}{(1 - z^{-1})^2}$ then
we have

\begin{align*}
e_{ss} &= \lim_{z \to 1} \left[ \left(1 - z^{-1} \right) R(z) \frac{1}{1
         + G_{OL} (z) } \right]
\\
&= \lim_{z \to 1} \left[ \left(1 - z^{-1} \right)
  \frac{z^{-1}}{(1 - z^{-1})^2} \frac{1}{1 +\frac{1}{(z-1)^N} G_{DC}(z) } \right]
\\
&= \lim_{z \to 1} \left[ \frac{1}{z - 1} \frac{1}{1 +\frac{1}{(z-1)^N}
  G_{DC}(z) } \right]
\\
&= \lim_{z \to 1} \left[ \frac{1}{(z-1) +\frac{1}{(z-1)^{N-1}}
  G_{DC}(z) } \right]
\\
e_{ss} &=  \frac{1}{ \lim_{z \to 1} \left[ \frac{1}{(z-1)^{N-1}}
  G_{DC}(z) \right] }
\end{align*}
%
Based on this result we can have the following steady-state
error conditions for the unit-ramp input based on the type
condition of the system
%
\begin{itemize}
\item Type $N < 1$: $e_{ss} \to  \infty$
\item Type $N = 1$: $e_{ss} = \frac{1}{K_{DC}}$
\item Type $N > 1$: $e_{ss} = 0$
\end{itemize}
%
\subsection*{Unit-Quadratic (Acceleration) Input}
%
We know that $r[k] = \frac{1}{2} k^2 u[k]$ and $R(z) = \frac{z^{-1} (1
  + z^{-1})}{2 (1 - z^{-1})^3}$ then
we have
\begin{align*}
e_{ss} &= \lim_{z \to 1} \left[ \left(1 - z^{-1} \right) R(z) \frac{1}{1
         + G_{OL} (z) } \right]
\\
&= \lim_{z \to 1} \left[ \left(1 - z^{-1} \right)
 \frac{z^{-1} (1 + z^{-1})}{2 (1 - z^{-1})^3}
  \frac{1}{1 +\frac{1}{(z-1)^N} G_{DC}(z) } \right]
\\
&= \lim_{z \to 1} \left[ \frac{(z+1)}{2 (z - 1)^2} \frac{1}{1 +\frac{1}{(z-1)^N}
  G_{DC}(z) } \right]
\\
&= \lim_{z \to 1} \left[ \frac{(z+1)/2}{(z-1)^2 +\frac{1}{(z-1)^{N-2}}
  G_{DC}(z) } \right]
\\
e_{ss} &=  \frac{1}{ \lim_{z \to 1} \left[ \frac{1}{(z-1)^{N-2}}
  G_{DC}(z) \right] }
\end{align*}
%
\begin{itemize}
\item Type $N < 2$: $e_{ss} \to  \infty$
\item Type $N = 2$: $e_{ss} = \frac{1}{K_{DC}}$
\item Type $N > 2$: $e_{ss} = 0$
\end{itemize}
%

\paragraph{Example 1:} $G(z) = \frac{z-1}{z-0.5}$ and $G_C(z) =
K$. Compute the steady-state erro to unit-step, unit-ramp, a
and unit-quadratic inputs.

\paragraph{Solution:}
%
\begin{align*}
G_{OL}(z) &= K \frac{z-1}{z-0.5} = \frac{1}{(z-1)^{-1}} \frac{K}{z-0.5}
\\
G_{DC}(1) &= 2 K \quad, \mathrm{Type} \ -1
\end{align*}
%
Then the steady-state errors are computed as
\begin{itemize}
\item Unit-step: $e_{ss} = 1$
\item Unit-ramp: $e_{ss} = \infty$
\item Unit-acceleration: $e_{ss} = \infty$
\end{itemize}

\paragraph{Example 2:} $G(z) = \frac{z-1}{z-0.5}$ and $G_C(z) =
K \frac{z}{z-1}$. Compute the steady-state error to unit-step, unit-ramp, a
and unit-quadratic inputs.

\paragraph{Solution:}
%
\begin{align*}
G_{OL}(z) &= \frac{K z}{z-0.5}
\\
G_{DC}(1) &= 2 K \quad, \mathrm{Type} \ 0
\end{align*}
%
Then the steady-state errors are computed as
%
\begin{itemize}
\item Unit-step: $e_{ss} = \frac{1}{1 + 2 K}$
\item Unit-ramp: $e_{ss} = \infty$
\item Unit-acceleration: $e_{ss} = \infty$
\end{itemize}

\paragraph{Example 3:} $G(z) = \frac{z-1}{z-0.5}$ and $G_C(z) =
K \frac{z^2}{(z-1)^2}$. Compute the steady-state erro to unit-step, unit-ramp, a
and unit-quadratic inputs.

\paragraph{Solution:}
%
\begin{align*}
G_{OL}(z) &= \frac{K z^2}{(z-1) (z-0.5)} = \frac{1}{z-1} \frac{K
            z^2}{z - 0.5}
\\
G_{DC}(1) &= 2 K \quad, \mathrm{Type} \ 1
\end{align*}
%
Then the steady-state errors are computed as
%
\begin{itemize}
\item Unit-step: $e_{ss} = 0$
\item Unit-ramp: $e_{ss} = \frac{1}{2 K}$
\item Unit-acceleration: $e_{ss} = \infty$
\end{itemize}

\subsection*{Open-Loop Transfer Function for Different Topologies}

When computing the steady-state error it is important to
carefully analyze the topology of the control system.

Compute the $G_{OL}(z)$ for the following system

\begin{center}
\begin{minipage}[h]{0.6\linewidth}
    \begin{center}
      \includegraphics[width=\textwidth]{closed}
    \end{center}
\end{minipage}
\end{center}

\begin{align*}
F(s) &= E^*(s) G(s) H(s)
\\
F^*(s) &= E^*(s) [ G(s)H(s) ]^* = E^*(s) GH^*(s)
\\
G_{OL}(z) &= GH(z)
\end{align*}

Now let's compte the $G_{OL}(z)$ for the following system

\begin{center}
\begin{minipage}[h]{0.6\linewidth}
    \begin{center}
      \includegraphics[width=\textwidth]{closed2}
    \end{center}
\end{minipage}
\end{center}

\begin{align*}
F(s) &= [ E^*(s) G(s) ]* H(s) = E^*(s) G^*(s) H(s)
\\
F^*(s) &= E^*(s) G^*(s) H^*(s)
\\
G_{OL}(z) &= G(z) H(z)
\end{align*}

Now let's compte the $G_{OL}(z)$ for the following system

\begin{center}
\begin{minipage}[h]{\linewidth}
    \begin{center}
      \includegraphics[width=\textwidth]{closed3}
    \end{center}
\end{minipage}
\end{center}

From last week we know that
%
\begin{align*}
U^*(s) = \frac{G_1^*(s)}{1 + G_2^*(s) + G_1^*(s) GH^*(s)} R^*(s)
\end{align*}
%
Then we can
%
\begin{align*}
U(s) = E^*(s) G_1(s) - U^*(s) G_2(s) \quad &\rightarrow \quad U^*(s) =
       E^*(s) G_1^*(s) - U^*(s) G^*_2(s)
\\
U^*(s) &= \frac{G_1^*(s)}{1 + G^*_2(s)} E^*(s)
\\
E^*(s) &= \frac{1 + G_2^*(s)}{1 + G_2^*(s) + G_1^*(s) GH^*(s)} R^*(s)
\\
\frac{E(z)}{R(z)} &= \frac{1 + G_2(z)}{1 + G_2(z) + G_1(z) GH(z)}
\end{align*}
%
This transfer function form does not (directly) fit to the form we analyzed,
i.e. $\frac{E(z)}{R(z)} = \frac{1}{1 + G_{OL}(z) }$, so we can not
directly used the conditions and formulaes for this form. One way of
comuting the steady-state errors is directly applying the final-value theorem.

The other way is we can simply convert the computed pulse transer
function $E(z) / R(z)$ such that it fits the form $\frac{E(z)}{R(z)} = \frac{1}{1 + G_{OL}(z) }$.
If we carefully analyze the transer function we can obtain
%
\begin{align*}
\frac{E(z)}{R(z)} &= \frac{1}{1 + \frac{G_1(z) G_2H(z)}{1 + G_2(z)}}
\\
G_{OL}(z) &= \frac{G_1(z) G_2H(z)}{1 + G_2(z)}
\end{align*}
%
It is also possible to derive $G_{OL}(z)$ via direct computation
of $F(z) / E(z)$.

\section*{Response to Disturbances}

When analyzing response of a system in addition to
the desired response to the reference input, it is also important
to analyze the response (both steady-state, transient, and frequency)
to unwanted disturbances and noises.

\subsection*{Process Disturbance/Uncertainty/Noise}

Let's analyze a type of important disturbance on the fundamental
discrete-time block diagram topology.

\begin{center}
\begin{minipage}[h]{\linewidth}
    \begin{center}
      \includegraphics[width=\textwidth]{disturb}
    \end{center}
\end{minipage}
\end{center}

In order to analyze the response to the disturbance $n[k]$, we assume
$r[k] = 0$ (which is just fine due to the linearity). Let's first find
the pulse transfer function from $N(z)$ to $Y(z)$.

\begin{align*}
Y(z) &= ( -Y(z) G_c(z) + N(z) ) G(z)
\\
\frac{Y(z)}{N(z)} &= \frac{G(z)}{1 + G_c(z) G(z)}
\end{align*}

Technically, we want $\frac{Y(z)}{N(z)} = 0$, while also tracking
the reference signal. Since it is not perfectly possible to
achieve $\frac{Y(z)}{N(z)} = 0$ while satisfying other constraints,
we want $\frac{Y(z)}{N(z)}$ to be ``small''. If $|G_C(z) G(z)| \gg 1$
then we have

\begin{align*}
\frac{Y(z)}{N(z)} \approx \frac{1}{G_c(z)}
\end{align*}

Now let's consider a specific type of disturbance. An important
class of process disturbance/uncertainty is in the form of DC
bias, i.e. $n(t) = N u(t)$ and $N(z) = \frac{N}{1 - z^{-1}}$. Let's
analyze DC steady state response using final value theorem.

\begin{align*}
y_{ss} &= \lim_{z\to1} \left[ \left( 1 - z^{-1} \right) Y(z) \right]
= \lim_{z\to1} \left[ \left( 1 - z^{-1} \right) N(z) \frac{G(z)}{1 +
  G_c(z) G(z)} \right]
\\
&= \lim_{z\to1} \left[ \left( 1 - z^{-1} \right) \frac{N}{1 - z^{-1}} \frac{G(z)}{1 +
  G_c(z) G(z)} \right]
= \lim_{z\to1} \left[ N \frac{G(z)}{1 +
  G_c(z) G(z)} \right]
\\
&= N \frac{\lim_{z\to1} G(z)}{1 + \lim_{z\to1} G_C(z) G(z) }
\end{align*}

Let's analyze the steady-state disturbance response

\begin{itemize}

  \item If plant is a type $< 0$ system (high pass filter plant)
    then $G(1) = 0$ and $y_{ss} = 0$.

   \item If plant is a type $0$ system, then
%
\begin{align*}
y_{ss} &= \frac{N G(1)}{1 + G(1) \lim_{z\to1} G_C(z) }
\end{align*}
%
Now let's analyze the response based on the type of $G_c(z)$

\begin{itemize}

\item Type $< 0$, then
%
\begin{align*}
y_{ss} &= N G(1)
\end{align*}
%
In this case, controller has no control
on the steady-state disturbance rejection
performance.

\item Type $0$, then
%
\begin{align*}
y_{ss} &= \frac{N G(1)}{1 + G(1) G_C(1) }
\end{align*}
%
Obviously in order to ``filter'' the disturbance
we should select a $G_C(z)$ such that
$| G_C(1) G(1) | \gg 1$ then
%
\begin{align*}
y_{ss} &= \frac{N}{G_C(1)}
\end{align*}
%
Large gain $G_C(z)$ can effectively filter the disturbance (but not completely).

\item Type $> 0$, then
%
\begin{align*}
y_{ss} &= \frac{N G(1)}{1 + G(1) \lim_{z\to1} G_C(z) }
\\
&= 0
\end{align*}
%
Integral action on $G_C(z)$ can perfectly
reject the DC disturbance on steady state.

\end{itemize}

\item If plant is a type $m > 0 $ then
%
\begin{align*}
y_{ss} &= \frac{N \lim_{z\to1} G(z)}{1 + \lim_{z\to1} G(z) G_C(z) }
\\
\lim_{z\to1} G(z) &= \infty
\end{align*}
%
Depending on the type of $G_C(z)$, we can conclude that
%
\begin{itemize}
 \item Type $< 0$, $y_{ss} = \infty$
 \item Type $0$, $y_{ss} = C$, where $0 < C < \infty$
 \item Type $> 0$, $y_{ss} = 0$
\end{itemize}
%

\end{itemize}

\paragraph{Example 4:} $G(z) = \frac{z-1}{z-0.5}$ and $G_C(z) =
K$. Compute the steady-state response to a unit step
process disturbance/noise.

\paragraph{Solution:}
%
\begin{align*}
y_{ss} = \frac{\lim_{z\to1}  \frac{z-1}{z-0.5}}{1 + \lim_{z\to1} K
  \frac{z-1}{z-0.5}} = 0
\end{align*}
%
Plant perfectly rejects disturbance.
%

\paragraph{Example 5:} $G(z) = \frac{z}{z-0.5}$ and $G_C(z) =
K$. Compute the steady-state response to a unit step
process disturbance/noise.

\paragraph{Solution:}
%
\begin{align*}
y_{ss} = \frac{\lim_{z\to1}  \frac{z}{z-0.5}}{1 + \lim_{z\to1} K
  \frac{z}{z-0.5}} = \frac{2}{1 + 2 K}
\end{align*}
%
Large gain $K$ can be effective solution to reject disturbance
%

\paragraph{Example 6:} $G(z) = \frac{z}{z-0.5}$ and $G_C(z) = K_P +
K_I \frac{z}{z-1}$. Compute the steady-state response to a unit step
process disturbance/noise.

\paragraph{Solution:}
%
\begin{align*}
y_{ss} = \frac{2}{1 + \lim_{z\to1} 2 \left(K_P + K_I  \frac{z}{z-1} \right)} = 0
\end{align*}
%
A PI controller can perfectly reject the DC process
disturbance.
%

\subsection*{Measurement Disturbance/Uncertainty/Noise}

Let's analyze a different type of important disturbance on the fundamental
discrete-time block diagram topology.

\begin{center}
\begin{minipage}[h]{\linewidth}
    \begin{center}
      \includegraphics[width=\textwidth]{noise}
    \end{center}
\end{minipage}
\end{center}

In order to analyze the response to the disturbance $w[k]$, we assume
$r[k] = 0$ and $n[k] = 0$

\begin{align*}
Y(z) &= ( W(z) -Y(z) ) G_c(z) G(z)
\\
\frac{Y(z)}{W(z)} &= \frac{G(z) G_C(z)}{1 + G_c(z) G(z)}
\\
&= \frac{G_{OL} (z)}{1 + G_{OL}(z)}
\end{align*}

Technically, we want $\frac{Y(z)}{N(z)} = 0$, while also tracking
the reference signal. Thus, practically we should design $G_C(z)$ such
that $|G_C(z) G(z)| \ll 1$, to eliminate measurement
noises/disturbances. (??????)

\begin{align*}
\frac{Y(z)}{N(z)} \approx {G_{OL}(z)}
\end{align*}

This ``requirement'' obviously contradicts with
requirements on stead-state tracking error performance
and process noise/disturbance rejection performance. Most well
known limitation of feedback control systems.

Now let's consider a specific type of measurement noise, i.e. DC measurement bias.
$w(t) = W u(t)$ and $W(z) = \frac{W}{1 - z^{-1}}$. Let's
analyze DC steady state response using final value theorem.
%
\begin{align*}
y_{ss} &= \lim_{z\to1} \left[ (1 - z^{-1}) R(z) \frac{ G_{OL}(z) } {
  1 + G_{OL}(z) } \right] = \lim_{z\to1} \left[ (1 - z^{-1}) \frac{W}{1-z^{-1}} \frac{ G_{OL}(z) } {
  1 + G_{OL}(z) } \right]
\\
&= \lim_{z\to1} \left[ \frac{ W G_{OL}(z) }{1 + G_{OL}(z) } \right]
\end{align*}

Using the form $G_{OL}(z) = \frac{1}{(z-1)^N} G_{DC}(z)$, $y_{ss}$
takes the form
%
\begin{align*}
  y_{ss} &= \lim_{z\to1} \left[ \frac{ W G_{DC}(1) \frac{1}{(z-1)^N}} {1 + G_{DC}(1) \frac{1}{(z-1)^N}}\right]
\end{align*}
%
Based on the type of the open-loop transfer function, $G_{OL}(z)$,
we can conclude
%
\begin{itemize}
 \item Type $N < 0$: $y_{ss} = 0$. Perfect rejection of measurement
   bias, but we know that this is unacceptable from reference tracking
   point of view.
 \item Type $N = 0$
%
\begin{align*}
  y_{ss} &= \frac{ W G_{DC}(1) } {1 + G_{DC}(1) }
\end{align*}
%
It seems that in order to ``filter'' the measurement bias
$G_{DC}(1)$ should be selected very small.

\item Type $N > 0$
%
\begin{align*}
  y_{ss} &= W
\end{align*}
%
The disturbance is directly transfered to the output.

\end{itemize}
%

\paragraph{Example 7:} $G(z) = \frac{z-1}{z-0.5}$ and $G_C(z) =
K$. Compute the steady-state response to a unit step
measurement disturbance/noise.

\paragraph{Solution:}
%
\begin{align*}
G_{OL}(z) &= K \frac{z-1}{z-0.5} \quad \textrm{Type} \ -1
\\
y_{ss} &= 0
\end{align*}
%
Plant perfectly rejects measurement bias.
%

\paragraph{Example 8:} $G(z) = \frac{z}{z-0.5}$ and $G_C(z) =
K$. Compute the steady-state response to a unit step
process disturbance/noise.

\paragraph{Solution:}
%
\begin{align*}
G_{OL}(z) &= K \frac{z}{z-0.5} \quad \textrm{Type} \ 0
\\
y_{ss} &= \frac{ 2K  } {1 +2K }
\end{align*}
%
Small gain $K$ can be effective solution to reject measurement bias.
%

% **** This ENDS THE EXAMPLES. DON'T DELETE THE FOLLOWING LINE:
\end{document}


\toclesssection{Cache Efficiency}

\toclesssubsection{Introduction}

\begin{frame}{Cache Efficiency}{Introduction}
  \textbf{Background:}
  \begin{itemize}
    \item<2->
      Up to now we always counted the {\color{MainA}number of operations}
    \item<2->
      Assuming this is a good measure for the runtime of an algorithm/tool
    \item<3->
      Today we will see examples where this is not suitable
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Cache Efficiency}{Introduction}
  \textbf{Example:}
  \begin{itemize}
    \item
      We sum up all elements of an array {\color{MainA}$a$} of size
      {\color{MainA}$n$} in $\ldots$
    \begin{itemize}
      \item
        natural order:
        \begin{displaymath}
          {\color{MainA}\mathrm{sum}(a)} =
          {\color{MainA}a[1]} +
          {\color{MainA}a[2]} +
          \dots +
          {\color{MainA}a[n]}
        \end{displaymath}
      \item
        random order:
        \begin{displaymath}
          {\color{MainA}\mathrm{sum}(a)} =
          {\color{MainA}a[21]} +
          {\color{MainA}a[5]} +
          \dots +
          {\color{MainA}a[8]}
        \end{displaymath}
    \end{itemize}
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\codeslide{python}{
\begin{frame}{Cache Efficiency}{Linear Order - Python}
  \textbf{Python:}
  \lstinputlisting[
    language=Python,
    basicstyle=\small,
    tabsize=4,
    style={python-idle-code},
    escapechar={@},
    emph={init},
    emphstyle=\color{blue}
  ]{Code/Caching/CacheLinear_Part1.py}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Cache Efficiency}{Linear Order - Python}
  \textbf{Python:}
  \lstinputlisting[
    language=Python,
    basicstyle=\small,
    tabsize=4,
    style={python-idle-code},
    escapechar={@},
    emph={run},
    emphstyle=\color{blue}
  ]{Code/Caching/CacheLinear_Part2.py}
\end{frame}
}

%TODO: Implement algorithm in Java / C++
%%-------------------------------------------------------------------------------
%
%\codeslide{java}{
%\begin{frame}{Cache Efficiency}{Linear Order - Java}
%  \textbf{Java:}
%  \lstinputlisting[
%    language=Java,
%    basicstyle=\small,
%    tabsize=4,
%    style={java-eclipse-code},
%    escapechar={@},
%    emph={words},
%    emphstyle=\color{java_variable}
%  ]{Code/Caching/CacheLinear.java}
%\end{frame}
%}
%
%%-------------------------------------------------------------------------------
%
%\codeslide{cpp}{
%\begin{frame}{Cache Efficiency}{Linear Order - C++}
%  \textbf{C++:}
%  \lstinputlisting[
%    language=C++,
%    basicstyle=\small,
%    tabsize=4,
%    style={cpp-eclipse-code},
%    morekeywords={endl},
%    escapechar={@}
%  ]{Code/Caching/CacheLinear.cpp}
%\end{frame}
%}

%-------------------------------------------------------------------------------

\begin{frame}{Cache Efficiency}{Linear Order}
  \begin{figure}
    \begin{center}
      \includegraphics[width=0.7\textwidth]{Images/Caching/sumlinear-plot.pdf}
    \end{center}
    \caption{summing elements in linear order}
    \label{fig:caching:sum_linear_order}
  \end{figure}
\end{frame}

%-------------------------------------------------------------------------------

\codeslide{python}{
\begin{frame}{Cache Efficiency}{Random Order - Python}
  \lstinputlisting[
    language=Python,
    basicstyle=\small,
    tabsize=4,
    style={python-idle-code},
    escapechar={@},
    emph={init},
    emphstyle=\color{blue},
    breaklines=false
  ]{Code/Caching/CacheRandom.py}
\end{frame}
}

%TODO: Implement algorithm in Java / C++
%%-------------------------------------------------------------------------------
%
%\codeslide{java}{
%\begin{frame}{Cache Efficiency}{Random Order - Java}
%  \textbf{Java:}
%  \lstinputlisting[
%    language=Java,
%    basicstyle=\small,
%    tabsize=4,
%    style={java-eclipse-code},
%    escapechar={@},
%    emph={words},
%    emphstyle=\color{java_variable}
%  ]{Code/Caching/CacheLinear.java}
%\end{frame}
%}
%
%%-------------------------------------------------------------------------------
%
%\codeslide{cpp}{
%\begin{frame}{Cache Efficiency}{Random Order - C++}
%  \textbf{C++:}
%  \lstinputlisting[
%    language=C++,
%    basicstyle=\small,
%    tabsize=4,
%    style={cpp-eclipse-code},
%    morekeywords={endl},
%    escapechar={@}
%  ]{Code/Caching/CacheLinear.cpp}
%\end{frame}
%}

%-------------------------------------------------------------------------------

\begin{frame}{Cache Efficiency}{Random Order}
  \begin{figure}
    \begin{center}
      \includegraphics[width=0.7\textwidth]{Images/Caching/sumrandom-plot.pdf}
    \end{center}
    \caption{summing elements in random order}
    \label{fig:caching:sum_random_order}
  \end{figure}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Cache Efficiency}{Algorithm Comparision}
  \textbf{Conclusion:}
  \begin{itemize}
    \item<2->
      The number of operations is identical for both algorithms
    \item<3->
      Accessing elements in random order takes a lot longer (factor 10)
      \onslide<3-| handout: 0>{{\color{MainA}Why?}\\}
    \item<3->
      The costs in terms of memory access are very different
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\subsection{Cache Organization}

\begin{frame}{Cache Efficiency}{CPU Cache}
  \vspace{-1.5em}
  \begin{figure}
    \begin{adjustbox}{width=\linewidth}
      \input{Images/Caching/CacheHirarchy.tikz}
    \end{adjustbox}
    \label{fig:caching:cache_hirarchy}
  \end{figure}
  \onslide<2->
  \textbf{Principle / organization:}
  \begin{itemize}
    \item<3->
      Accessing one byte of the main memory takes
      $\approx\SI{100}{\nano\second}$
    \item<4->
      Accessing one byte of (L1-)cache takes
      $\approx\SI{1}{\nano\second}$
    \item<5->
      Accessing one or more byte/s of main memory loads a whole
      block $\approx\SI{100}{\byte}$ into the cache
    \item<6->
      As long as this block is in the cache, it is not neccessary to
      access the memory for bytes of this block
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Cache Efficiency}{CPU Cache}
  \vspace{-1.5em}
  \begin{figure}
    \begin{adjustbox}{width=\linewidth}
      \input{Images/Caching/CacheHirarchy.tikz}
    \end{adjustbox}
    \label{fig:caching:cache_hirarchy2}
  \end{figure}
  \vspace{-0.3cm}
  \onslide<2->
  \textbf{Cache organization:}
  \begin{itemize}
    \item<3->
      The (L1-)cache can hold multiple memory blocks
      \begin{itemize}
        \item<4->
          Cache lines $\approx\SI{100}{\kilo\byte}$
        \end{itemize}
    \item<5->
      If the capacity is reached unused blocks are discarded
      \begin{itemize}
        \item<6->
          {\color{MainA}Least recently used (LRU)}
        \item<7->
          {\color{MainA}Least frequently used (LFU)}
        \item<8->
          {\color{MainA}First in first out (FIFO)}
      \end{itemize}
      \item<9-> Details of discarding not discussed today
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Cache Efficiency}{Block Operations}
  \vspace{-2.0em}
  \begin{figure}%
    \begin{adjustbox}{width=\linewidth}%
      \input{Images/Caching/CacheBlocks.tikz}%
    \end{adjustbox}%
    \label{fig:caching:cache_quanting}
  \end{figure}%
  \textbf{Terminology:}
  \begin{itemize}
    \item<2->
      The system consists of slow and fast memory
    \item<3->
      The {\color{MainA}slow memory} is divided in
      {\color{MainA}blocks of size $B$}
    \item<4->
      The {\color{MainA}fast cache} has size $M$ an can store $M/B$
      blocks
    \item<5->
      If data is not in fast memory, the corresponding block is loaded into the {\color{MainA}cache}
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Cache Efficiency}{Block Operations}
  \vspace{-2.0em}
  \begin{figure}%
    \begin{adjustbox}{width=\linewidth}%
      \input{Images/Caching/CacheBlocks.tikz}%
    \end{adjustbox}%
    \label{fig:caching:cache_quanting2}
  \end{figure}%
  \textbf{Terminology:}
  \begin{itemize}
    \item<2->
      The program defines which blocks are held in the
      {\color{MainA}cache}
    \item<3->
      We use the number of {\color{MainA}block operations} as runtime
      estimation
    \item<4->
      We ignore runtime costs of cache access / management
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Cache Efficiency}{Block Operations}
  \vspace{-2.0em}
  \begin{figure}%
    \begin{adjustbox}{width=\linewidth}%
      \input{Images/Caching/MemoryLocality.tikz}%
    \end{adjustbox}%
    \caption{comparison good / bad locality}
    \label{fig:caching:memory_locality}
  \end{figure}%
  \textbf{Accessing the cache {\color{MainA}$B$} times:}
  \begin{itemize}
    \item
      {\color{MainA}Best case}:
      1 block operation $\rightarrow$ good locality
    \item
      {\color{MainA}Worst case}:
      {\color{MainA}$B$} block operations $\rightarrow$ bad locality
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Cache Efficiency}{Block Operations}
  \textbf{Additional factors:}\\
  \begin{itemize}
    \item<2->
    The following settings change only a small constant factor in number of block operations
    \begin{itemize}
    \item<3-> Partionining of the slow memory into blocks
    \item<4-> Regardless of the block size: {\color{MainA}1 bytes} or
      {\color{MainA}4 bytes} or {\color{MainA}8 bytes}
    \end{itemize}
  \end{itemize}
  \vspace{1.0em}
  \onslide<5->
  \textbf{Note:}\\
  \begin{itemize}
    \item<6->
      If the {\color{MainA}input size} is
      {\color{MainA}smaller than $M$} we load the complete data chunk
      directly into the cache
    \item<7->
      Cache handling is only interesting when the
      {\color{MainA}input size} is
      {\color{MainA}greater than $M$}
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Cache Efficiency}{Block Operations}
  \textbf{Typical values:}
  (\texttt{Intel$\copyright$ i7-4770} Haswell,
  \texttt{WD$\copyright$ Blue} $\SI{2}{\tera\byte}$)
  \begin{itemize}
    \item<2->
      CPU L1 Cache: {\color{MainA}$B = \SI{64}{\byte}$},
      {\color{MainA}$M = 4 \times (\SI{32}{\kilo\byte} + \SI{32}{\kilo\byte})$}
    \item<3->
      CPU L2 Cache: {\color{MainA}$B = \SI{64}{\byte}$}, {\color{MainA}$M = 4 \times \SI{256}{\kilo\byte}$}
    \item<4->
      CPU L3 Cache: {\color{MainA}$B = \SI{64}{\byte}$}, {\color{MainA}$M = \SI{8}{\mega\byte}$}
    \item<4->
      Disk Cache: {\color{MainA}$B = \SI{64}{\kilo\byte}$}, {\color{MainA}$M = \SI{64}{\mega\byte}$}
    \begin{itemize}
      \item<5->
        Many operating systems use free system memory as disk cache
    \end{itemize}
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Cache Efficiency}{Block Operations}
  \textbf{Terminology:}
  \begin{itemize}
    \item<2->
      Block loads on CPU cache are called {\color{MainA}cache~misses}
    \item<3->
      Block operations on disk cache are called {\color{MainA}IOs}\\
      (input / output operations)
    \item<4->
      These also fall under the term {\color{MainA}cache~efficiency} or
      {\color{MainA}IO~efficiency}
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Cache Efficiency}{Block Operations - Linear Order}
  \vspace{-1.0em}
  \textbf{Example 1 - Linear order:}
  \begin{itemize}
    \item<2->
      We sum up all elements in {\color{MainA}natural order}
      \begin{displaymath}
        {\color{MainA}\mathrm{sum}(a)} =
        {\color{MainA}a[1]} +
        {\color{MainA}a[2]} +
        \dots +
        {\color{MainA}a[n]}
      \end{displaymath}
    \item<3->
      The number of block operations is
      {\color{MainA}$\mathrm{ceil}\left(\frac{n}{B}\right)$}
  \end{itemize}
  \vspace{-1.0em}
  \onslide<4->
  \begin{figure}%
    \begin{adjustbox}{width=\linewidth}%
      \input{Images/Caching/MemoryLocality_Linear.tikz}%
    \end{adjustbox}%
    \caption{good locality of sum operation}
    \label{fig:caching:memory_locality_linear}
  \end{figure}%
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Cache Efficiency}{Block Operations - Random Order}
  \textbf{Example 2 - Random order:}
  \begin{itemize}
    \item<2->
      We sum up all elements in {\color{MainA}random order}
      \begin{displaymath}
        {\color{MainA}\mathrm{sum}(a)} =
        {\color{MainA}a[21]} +
        {\color{MainA}a[5]} +
        \dots +
        {\color{MainA}a[8]}
      \end{displaymath}
    \item<3->
      The number of block operations is {\color{MainA}$n$} in the
      {\color{MainA}worst case}
    \item<4->
      This leads to a runtime factor difference of {\color{MainA}$B$}
  \end{itemize}
  \onslide<5->
   \vspace{-1.0em}
  \begin{figure}%
    \begin{adjustbox}{width=\linewidth}%
      \input{Images/Caching/MemoryLocality_Random.tikz}%
    \end{adjustbox}%
    \caption{bad locality of sum operation}
    \label{fig:caching:memory_locality_random}
  \end{figure}%
  \vspace{-1.0em}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Cache Efficiency}{Block Operations}
  \textbf{Generally the factor is substantially {\color{MainA}$< B$}}
  \begin{itemize}
    \item<2->
      Even with a {\color{MainA}random order} we access 4
      neighboring bytes at once per \texttt{int} (\text{int32\_t})
    \item<3->
      The next element might already be loaded into the cache
    \item<4->
      If {\color{MainA}not $n \gg M$} this might occur with a high probability
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Cache Efficiency}{Block Operations - Quicksort}
  \textbf{Quicksort:}\\
  \begin{itemize}
    \item<2->
      \textbf{Strategy:} Divide and Conquer
    \item<3->
      Divide the data into two parts where the \enquote{left} part contains
      all values {\color{MainA}$\leq$} the values in the right part
    \item<4->
      Choose one element (e.g the first one) as
      {\color{MainA}\enquote{pivot} element}
    \item<5->
      Ideally both parts are the same size
    \item<6->
      Both parts are sorted recursively
  \end{itemize}
  \onslide<7->
  \vspace{-1em}
  \begin{figure}%
    \begin{adjustbox}{width=\linewidth}%
      \input{Images/Caching/QuickSort_Pivot.tikz}%
    \end{adjustbox}%
    \caption{\textit{Quicksort} with pivot element}
    \label{fig:caching:quicksort_pivot}
  \end{figure}%
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}
  \frametitle{Idea of Quicksort}
  \begin{itemize}
  \item \textbf{At start:} pivot in first position, first re-arrange list such
    that left part contains smaller and right part larger elements
  \item Do required changes \emph{in place}
  \end{itemize}
  \hspace*{1cm}\onslide<1>{\rlap{\includegraphics[width=0.55\textwidth]{Images/Caching/quicksort-idea-o1.pdf}}}%
  \onslide<2>{\rlap{\includegraphics[width=0.55\textwidth]{Images/Caching/quicksort-idea-o2.pdf}}}%
  \onslide<3>{\rlap{\includegraphics[width=0.55\textwidth]{Images/Caching/quicksort-idea-o3.pdf}}}%
  \onslide<4>{\rlap{\includegraphics[width=0.55\textwidth]{Images/Caching/quicksort-idea-o4.pdf}}}%
  \onslide<5>{\rlap{\includegraphics[width=0.55\textwidth]{Images/Caching/quicksort-idea-o5.pdf}}}%
  \onslide<6>{\rlap{\includegraphics[width=0.55\textwidth]{Images/Caching/quicksort-idea-o6.pdf}}}%
  \onslide<7->{\rlap{\includegraphics[width=0.55\textwidth]{Images/Caching/quicksort-idea-o7.pdf}}}%
  \begin{itemize}
  \item \textbf{End point:} $k$ is left to left-most element greater than pivot
    \begin{center}
     \textit{swap position 0 (pivot) with $k$ (smaller than pivot)}
    \end{center}
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\codeslide{python}{
\begin{frame}{Cache Efficiency}{Block Operations - Quicksort - Python}
  \textbf{Python:}
  \lstinputlisting[
    language=Python,
    basicstyle=\normalsize,
    tabsize=2,
    style={python-idle-code},
    breaklines=false,
    escapechar={@},
    emph={quicksort},
    emphstyle=\color{blue}
  ]{Code/Caching/QuickSort_Part1.py}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Cache Efficiency}{Block Operations - Quicksort - Python}
  \vspace{-1em}
  \lstinputlisting[
    language=Python,
    basicstyle=\normalsize,
    tabsize=2,
    style={python-idle-code},
    breaklines=false,
    escapechar={@},
    emph={quicksort},
    emphstyle=\color{blue}
  ]{Code/Caching/QuickSort_Part2.py}
\end{frame}
}

%TODO: Implement algorithm in Java / C++
%-------------------------------------------------------------------------------

\begin{frame}{Cache Efficiency}{Block Operations - Quicksort}
  \textbf{Number of operations for Quicksort:}
  \begin{itemize}
    \item<2->Let {\color{MainA}$T(n)$} be the runtime for the
      {\color{MainA}input size $n$}
  \end{itemize}
  \vspace{1em}
  \onslide<3->\textbf{Assumptions:}
  \begin{itemize}
    \item<4->
      Arrays are always separated perfectly in the middle
    \item<5->
      {\color{MainA}$n$} is a power-of-two and recursion depth is
      {\color{MainA}$k = \log_2 n$}
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Cache Efficiency}{Block Operations - Quicksort}
  \begin{eqnarray*}
    {\color{MainA}T(n)} &\leq\hspace{1.5em}&
      \underbrace{
        A \cdot {\color{MainA}n}
        \vphantom{\frac{n}{2}}
      }_{\text{splitting in two parts}}
      +
      \underbrace{
        2 \cdot {\color{MainA}T\left(\frac{n}{2}\right)}
      }_{\text{recursive sort}}\\
    {} &\leq&
      A \cdot {\color{MainA}n} + 2 \left(
        A \cdot \frac{\color{MainA}n}{2}
        + 2 \cdot {\color{MainA}T\left(\frac{n}{4}\right)}
      \right)\\
    {} &=&
      2\,A \cdot {\color{MainA}n}
      + 4 \cdot {\color{MainA}T\left(\frac{n}{4}\right)}\\
    {} &\leq&
      3\,A \cdot {\color{MainA}n}
      + 8 \cdot {\color{MainA}T\left(\frac{n}{8}\right)}\\
    {} &\leq&\hspace{1.5em}\cdots\\
    {} &\leq&
      {\color{MainA}k} \cdot A \cdot {\color{MainA}n}
      + 2^{\color{MainA}k}
      \cdot {\color{MainA}T(1)}\\
    {} &=&
      \log_2 {\color{MainA}n} \cdot A \cdot {\color{MainA}n}
      + {\color{MainA}n \cdot T(1)}\\
    {} &\leq&
      \log_2 {\color{MainA}n} \cdot A \cdot {\color{MainA}n}
      + {\color{MainA}n} \cdot A
      \in \mathcal{O}(n \, \log_2 n)
  \end{eqnarray*}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Cache Efficiency}{Block Operations - Quicksort}
  \begin{figure}%
    \begin{adjustbox}{width=\linewidth}%
      \input{Images/Caching/MemoryLocality_QuickSort.tikz}%
    \end{adjustbox}%
    \caption{locality of Quicksort}
    \label{fig:caching:memory_locality_quicksort}
  \end{figure}%
  \begin{itemize}
    \item<2->
      Let {\color{MainA}$IO(n)$} be the number of
      {\color{MainA}block operations} for input size {\color{MainA}$n$}
    \item<3->
      Assumptions as before but recursion depth is
      {\color{MainA}$k = \log_2 \frac{n}{B}$}
      \onslide<3-| handout: 0>{\\\color{MainA}Why?}
  \end{itemize}
\end{frame}

%-------------------------------------------------------------------------------

\begin{frame}{Cache Efficiency}{Block Operations - Quicksort}
  \begin{eqnarray*}
    {\color{MainA}IO(n)} &\leq\hspace{1em}&
      \underbrace{
        A \cdot {\color{MainA}n/B}
      }_{\text{splitting in two parts}}
      +
      \underbrace{
        2 \cdot {\color{MainA}IO\left(n/2 \right)}
      }_{\text{recursive sort}}\\
    {} &\leq&
      A \cdot {\color{MainA} n/B} \hspace{1em}+ \hspace{1em}2 \left(
        A \cdot {\color{MainA} n/2B}
        + 2 \cdot {\color{MainA}IO\left(n/4\right)}
      \right)\\
    {} &\leq&
      2 \cdot A \cdot {\color{MainA}n/B}
      \hspace{1em}+ \hspace{1em}4 \cdot {\color{MainA}IO\left(n/4\right)}\\
    {} &\leq&
      3 \cdot A \cdot {\color{MainA}n/B}
      \hspace{1em}+ \hspace{1em}8 \cdot {\color{MainA}IO\left(n/8\right)}\\
    {} &\leq&\hspace{1.5em}\cdots\\
    {} &\leq&
      {\color{MainA}k} \cdot A \cdot {\color{MainA}n/B}
      \hspace{1em}+\hspace{1em} 2^{\color{MainA}k}
      \cdot {\color{MainA}IO(n / 2^k)}\\
    {} &=&
      \log_2 {\color{MainA} (n / B)} \cdot A \cdot {\color{MainA}(n / B)}
      \hspace{1em}+\hspace{1em} {\color{MainA} n /B \cdot IO(B)}\\
    {} &\leq&
      \log_2 {\color{MainA} (n /B)} \cdot A \cdot {\color{MainA}(n / B)}
      \hspace{1em}+\hspace{1em} A \cdot {\color{MainA} n/B}
      \hspace{1em} \in \hspace{1em} \mathcal{O}\left(
        \frac{n}{B} \cdot \log_2 \left(\frac{n}{B}\right)
      \right)
  \end{eqnarray*}
\end{frame}


\documentclass{beamer}
\usetheme{Antibes}
\useinnertheme{rectangles}
\useoutertheme{infolines}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

% patch the look of +, = in arev
\usefonttheme{serif}

\usepackage{arev}
\usepackage{amsmath}
\usepackage{amssymb}

\setbeamertemplate{footline}{%
\begin{beamercolorbox}[ht=3.0ex,dp=1ex]{title in head/foot}
\hfill\footnotesize\insertpagenumber\enspace\enspace\end{beamercolorbox}}

\newcommand{\ee}{\mathrm e}
\newcommand{\ui}{\mathrm i}
\newcommand{\real}{\operatorname{Re}}
\newcommand{\imag}{\operatorname{Im}}
\newcommand{\uv}[1]{\underline{#1}}
\newcommand{\bv}[1]{\mathbf{#1}}

\newcommand{\N}{\mathbb N}
\newcommand{\Z}{\mathbb Z}
\newcommand{\Q}{\mathbb Q}
\newcommand{\R}{\mathbb R}
\newcommand{\C}{\mathbb C}

\newcommand{\id}{\operatorname{id}}
\newcommand{\sgn}{\operatorname{sgn}}
\newcommand{\Abb}{\operatorname{Abb}}
\newcommand{\unit}[1]{\mathrm{#1}}
\newcommand{\chem}[1]{\mathrm{#1}}
\newcommand{\strong}[1]{\textsf{\textbf{#1}}}

\title{Titel}
\date{}

\begin{document}
\maketitle

\begin{frame}[t]
\frametitle{Inhaltsverzeichnis}

\tableofcontents

\end{frame}

\section{Abschnitt 1}
\subsection{Unterabschnitt 1.1}
\begin{frame}
\frametitle{Folientitel}
\framesubtitle{Untertitel}
Text.
\begin{Definition}[Ableitung]
\[f'(x) := \lim_{h\to 0}\frac{f(x+h)-f(x)}{h}\]
\end{Definition}

\begin{Beispiel}
Text.
\end{Beispiel}

\end{frame}

\subsection{Unterabschnitt 1.2}
\begin{frame}

\begin{itemize}
\item Minze
\item Hagebutte
\end{itemize}

\begin{enumerate}
\item Minze
\item Hagebutte
\end{enumerate}

\end{frame}

\section{Abschnitt 2}
\begin{frame}

\begin{block}{Blocktitel}
Blocktext.
\end{block}

\end{frame}

\end{document}


\documentclass[11pt]{article}
\newcommand\tab[1][1cm]{\hspace*{#1}}
\usepackage{graphicx}
\graphicspath{ {C:/Users/yedkk/Desktop/CS465/HW5} }
\begin{document}
\section{Homework 4}
Name: Kangdong Yuan

\subsection{problem1}
a).I did not work in a group.
\\b).I did not consult without anyone my group members
\\c).I did not consult any non-class materials.

\subsection{problem2}
a). \\
graph1: A,B,D,E,G,F \\
graph2: C,H,I\\
b). \\
$node \tab pre-number \tab post-number\\
A \tab \tab 1 \tab \tab \tab 12 \\
B \tab \tab 2 \tab \tab \tab 11 \\
D \tab \tab 3 \tab \tab \tab 6 \\
E \tab \tab 4 \tab \tab \tab 5 \\
G \tab \tab 8 \tab \tab \tab 9 \\
F \tab \tab 7 \tab \tab \tab 10 \\
C \tab \tab 13 \tab \tab \tab 18 \\
H \tab \tab 14 \tab \tab \tab 17 \\
I \tab \tab 15 \tab \tab \tab 16 \\$
c).edges and labels \\
$\{A,B\} \ Tree, \ \{B,D\} \ Tree, \ \{D,E\} \ Tree, \ \{E,D\} \ Back, \ \{A,E\} \ Forward, \ \\
\{B,G\} \ Tree, \ \{G,F\} \ Tree, \  \{G,D\} \ Cross, \ \{C,H\} \ Tree,\ \{H,I\} \ Tree, \\ \{C,I\} \ Forward $

\subsection{problem3}
a). The v is the ancestor of u, if we explore the v before u, which means $pre(v)<pre(u)$.\\
Given $post(u)<post(v)$, there are two cases. \\
first case is $pre(u)<post(u)<pre(v)<post(v)$, but this case is not possible. Because, the dfs need to visit all the neighbors of a vertex before mark it as visited and return the post number. \\
The second case is $pre(v)<pre(u)<post(u)<post(v)$, this is the only one possible arrangement for given condition $post(u)<post(v)$. And in this case, the $pre(v)<pre(u)$, so the v is the ancestor of the u.\\
Finally we can conclude that give $post(u)<post(v)$, v is the ancestor of u. \\
b).First, traverse the graph by dfs order and record the pre and post number of each vertex. The time complexity of this algorithm is $O(|V|+|E|)$. Then we check the pre-number and post-number of u and v, if $pre(u)<post(u)<pre(v)<post(v)$, we can say that u is the ancestor of v. We define this comparison take constant O(C) time. And, this algorithm can be done in linear time $O(|V|+|E|+C)$

\subsection{problem4}
First, If we want to find which vertex can reach the vertex i, we can do it inversely. So, for graph G, we convert it to reversed graph $G^R$, the time complexity of reverse graph algorithm is $O(|V|+|E|)$. Then we use this reversed graph to find $m(i)$, because we have reversed the graph, the vertex can be reached is the vertex that reached from previously.
\\ To find $m(i)$, use the dfs to traverse all the vertex that i can reach, then return the smallest one.\\
\\
program find $m(i)$: \\
Input:  graph: $G$ and integer i\\
Output: integer j \\
$find-mi(G,i)$\\
j=i \\
stack.push(i)\\
while stack not empty\\
\tab u pop from stack \\
\tab if u is not visited\\
\tab \tab mark u is visited\\
\tab \tab	for every neighbor vertex w of vertex u\\
\tab \tab \tab stack.push(w) \\
\tab \tab \tab if $w<j$\\
\tab \tab \tab \tab j=w \\
return j \\
\\
This algorithm go through all the vertex that i can reach by dfs order, then it compare the vertices to find the smallest, finally it return the smallest vertex as a integer. It time complexity is $O(|V|+|E|)$, because it is the same time complexity as dfs algorithm.
\\The total time complexity is the running time of reverse graph and $find-m(i)$, which is the $2(|V|+|E|)=O(|V|+|E|)$. So, all m(i) can be computed in $O(|V|+|E|)$

\end{document}