%
% This is a borrowed LaTeX template file for lecture notes for CS267,
% Applications of Parallel Computing, UCBerkeley EECS Department.
%

\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 EE302 - Feedback Systems
	\hfill Spring 2019} }
       \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{12}{Asst. Prof. M. Mert Ankarali}
....

\section{Root Locus}

In control theory, root locus analysis is a graphical analysis method
for investigating the change of closed-loop poles/roots of a
system  with respect to the changes of a system parameter,
commonly a gain parameter $K > 0$.

In order to better understand the root locus and derive fundamental
rules, we start with the following basic feedback topology where
the controller is a P-controller with a gain $K$.

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

The closed loop transfer function of this basic control system is
%
\begin{align*}
\frac{Y(s)}{R(s)} = \frac{K G(s)}{1 + K G(s) H(s)} =\frac{K G(s)}{1 + K G_{OL}(s)}
\end{align*}
%
where the poles of the closed loop system are
the roots of the characteristic equation
%
\begin{align*}
1 + K G_{OL}(s) = 0
\\
1 + K \frac{n(s)}{d(s)} = 0
\end{align*}
%
The goal is deriving the qualitative and quantitive behavior of
closed-loop pole ``paths'' for \textbf{positive} gain $K$ that
solves the equation $1 + K G_{OL}(s) = 0$ (or $1 + K \frac{n(s)}{d(s)} = 0$).

\subsection{Angle and Magnitude Conditions}

Let's analyze the characteristic equation
%
\begin{align*}
K G_{OL}(s) = -1 \quad , \mathrm{or} \quad K \frac{n(s)}{d(s)} = -1
\quad , \mathrm{or} \quad
K \frac{(s - z_1) \cdots (s - z_M)}{(s - p_1) \cdots (s - p_N)}
&= -1
\end{align*}
%
Let's derive the \textbf{magnitude condition} given that $K > 0$,
%
\begin{align*}
K | G_{OL}(s) | = 1 \quad , \mathrm{or} \quad K  \left| \frac{n(s)}{d(s)} \right| = 1
\quad , \mathrm{or} \quad
K \frac{|s - z_1| \cdots |s - z_M|}{|s - p_1| \cdots |s - p_N|}  = 1
\end{align*}
%
Now let's derive the \textbf{angle condition} given that $K > 0$,
%
\begin{align*}
\angle [ G_{OL}(s) ] = \pi (2 k + 1)
\quad ,  &\mathrm{or} \quad
\angle [n(s)] - \angle [d(s)] = \pi (2 k + 1)
\\
\quad , &\mathrm{or} \quad
\angle [s - z_1] \cdots \angle [s - z_M] - \left( \angle [s - p_1] \cdots \angle [s - p_N] \right)
= \pi (2 k + 1),
\ \ k \in \mathbb{Z}
\end{align*}
%
For a given $K$, s values that satisfy both magnitude and angle
conditions are located on the root loci. These constitutes the most
fundamental knowledge regarding the root locus analysis.

How we can check whether a candidate $s^*$ is in the root -locus or not.
If we analyze the angle condition, we can see that it is independent
from the parameter $K$. However, If we focus on the magnitude condition,
we can see that
%
\begin{align*}
K = \frac{1}{| G_{OL}(s^*) |} = \left| \frac{d(s^*)}{d(s^*)} \right| =
\frac{|s^* - p_1| \cdots |s^* - p_N|}{|s^* - z_1| \cdots |s^* - z_M|}
\end{align*}
%
which implies that for every $s^*$ candidate (that is not a pole or zero),
we can indeed compute a gain $K$ value.

In conclusion, only angle condition is used for testing
whether a point is in the root-locus or not. On the other hand,
will use the magnitude condition to compute the
value of gain $K$, if we find that a candidate $s^*$
is in the root locus based on the angle condition.

\newpage

\textbf{Ex:} It is given that $G_{OL}(s) = \frac{1}{s (s+4)}$.
Determine if the following pole candidates are on the
root-locus or not
%
\begin{align*}
	p_1^* &= -2  \ , \  p_2^* = 2 \,\ p_3^* = -2 + 2 j
\end{align*}

 \textbf{Solution:}  We only test the angle
 condition. Solutions are illustrated on the $s-$planes
provided below

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

\subsection{Rules and procedure for constructing root loci}

\begin{enumerate}

\item Compute the zeros poles of the open-loop
transfer function and write the characteristic eqiation
of closed-loop system.
%
\begin{align*}
1 + K G_{OL}(s)  &= 0 \\
1 + K \frac{n(s)}{d(s)} &= 0 \\
1 + K \frac{(zs- z_1) \cdots (zs- z_M)}{(s - p_1) \cdots (s - p_N)} &= 0 \\
\end{align*}
%

\item Root loci has $N$ separate branches. Since,
%
\begin{align*}
\left[ (s - p_1) \cdots (s - p_N) \right] + K \left[ (zs- z_1) \cdots (zs- z_M) \right]  &= 0 \\
d(s) + K n(s) = 0
\end{align*}
%
has $N$ number of roots for all $K$.

\item Root loci starts from poles of $G_{OL}(s)$ and
%
\begin{enumerate}
   \item $M$ branches terminates at the zeros of $G_{OL}(s)$
   \item $N-M$ branches terminates at $\infty$ (implicit zeros of $G_{OL}(s)$)
\end{enumerate}

It is relatively easy to understand this
%
\begin{align*}
d(s) + K n(s) = 0
\\
K \to 0 \ \Rightarrow \ [d(s) + K n(s) = 0 \ \to \ [d(s) = 0]
\\
K \to \infty \ \Rightarrow \ [d(s) + K n(s) = 0 \ \to  \ [n(s) = 0]
\end{align*}

\item Root loci on the real axis determined by open-loop zeros and
  poles. $s = \sigma \in \mathbb{R}$ then, based on the angle condition
  we have
%
\begin{align*}
\mathrm{Sign} [ G_{OL}(\sigma) ] &= -1
\end{align*}
%
Let's first analyze the effect of complex conjugate pole/zero (and double pole/zero on real axis)
pairs on the equation above. Let $\sigma^* \in \mathbb{R}$  is the candidate location and complex
conjugate poles has the following form $p_{1,2} = \sigma \pm j \omega$
\begin{align*}
\mathrm{Sign} [ ( \sigma^* + \sigma - j \omega) (\sigma^* + \sigma + j \omega) ] &= \mathrm{Sign} [ (\sigma^* + \sigma)^2 + \omega^2 ]
= 1
\end{align*}
%
We can see that complex conjugate zero/pole pairs have not effect
on angle condition for the roots on the real axis. Then for
the remaining ones we can derive the following condition
%
\begin{align*}
\mathrm{Sign} [ G_{OL}(\sigma) ] = \prod_{i=1}^{\bar{M}}  \mathrm{Sign} [\sigma - z_i]
  \prod_{j=1}^{\bar{N}}  \mathrm{Sign} [\sigma - p_j] = -1
\end{align*}
%
which means that for ODD number of poles $+$  zeros
$\mathrm{Sign} [\sigma - p_i ]$ and $\mathrm{Sign} [\sigma - z_i] $
must be negative for satisfying this condition for that particular $\sigma$
to be on the root-locus. We can summarize the rule as

\textbf{If the test point $\sigma$ on real axis has ODD numbers of
poles and zeros in its right, then this point is located
on the root-locus.}

\newpage

\textbf{Ex:} The figure below illustrates the root locus plots
of three different transfer functions.

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

\item Asymptotes

\begin{itemize}
  \item $N-M$ branches goes to infinity. Thus, there exist $N-M$ many
    asymptotes
  \item For large $s$ we can have the following approximation
 \begin{align*}
  &K \frac{(s - z_1) \cdots (s - z_M)}{(s - p_1) \cdots (s - p_N)}
   \approx \frac{K}{s^{N-M}}
\\
&\angle \left[ \frac{K}{s^{N-M}} \right] = -(N-M) \angle [ s ] = \pi (2 k + 1), \ k \in \mathbb{Z}
\\
&\phi_{a} = \frac{\pm \pi (2 k + 1)}{N-M}, \ k \in \lbrace 1, \cdots, N-M \rbrace
 \end{align*}
%
  \item Real axis intercept $\sigma_c$ can be computed as
 \begin{align*}
   \sigma_c = \frac{\sum p_i - \sum z_i}{N-M}
 \end{align*}
%
This can be derived via a different approximation (see textbook)
\end{itemize}

\newpage

\textbf{Ex:} The figure below illustrates the root locus plots
of two different transfer functions.

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

\item Breakaway and break-in points on real axis. When s is real
$s = \sigma, \ \sigma \in \mathbb{R}$, we have
%
\begin{align*}
1 + K G_{OL} (\sigma) &= 0
\end{align*}
%
Note that break-in and breakaway points corresponds to
double roots. Thus, if $\sigma_{b}$ is a break-away or break-in
point we have
%
\begin{align*}
1 + K G_{OL} (\sigma_b) &= 0
\\
K \left[ \frac{d}{d \sigma} G_{OL}(\sigma) \right]_{\sigma_b} &= 0
\end{align*}
%
Thus, we conclude that break-in or break-away points satisfy
the following conditions
%
%
\begin{align*}
\left[ \frac{d G_{OL}(\sigma)}{d \sigma} \right]_{\sigma = \sigma_b} = 0
\quad ,
K(\sigma_B) = \frac{-1}{G_{OL} (\sigma_b) }
\quad ,
K(\sigma_b) > 0
\end{align*}

We can derive two corollary conditions for computing $\sigma_b$ as
%
%
\begin{align*}
\left[ \frac{d}{d \sigma} \frac{1}{G_{OL}(\sigma)} \right]_{\sigma = \sigma_b} = 0
\quad or , \quad
\left[ \left( \frac{d}{d \sigma} N(\sigma) \right) D(\sigma)  -
\left( \frac{d}{d \sigma} D(\sigma) \right)  N(\sigma) \right]_{\sigma_b} = 0
\end{align*}

\newpage

\textbf{Ex:} Draw the root locus diagram for $G_{OL}(s) = \frac{1}{s (s+4)}$,
compute the real axis intercept $\sigma_c$ and break away point (with the associated gain value).

\begin{minipage}[h]{0.5\linewidth}
    \begin{center}
      \includegraphics[width=\textwidth]{break_away}
    \end{center}
\end{minipage}
\begin{minipage}[h]{0.5\linewidth}
	\begin{align*}
	\sigma_c &=  -2
	\\
	\sigma_{ba} &=  -2 \quad , \ K(\sigma_{ba}) = 4
	\end{align*}
\end{minipage}

\vspace{12pt}

\textbf{Ex:} Draw the root locus diagram for $G_{OL}(s) = \frac{s+4}{s (s+3)}$,
compute the break-away and break-in points (with the associated gain values).

\begin{minipage}[h]{0.5\linewidth}
    \begin{center}
      \includegraphics[width=\textwidth]{bin_baway}
    \end{center}
\end{minipage}
\begin{minipage}[h]{0.5\linewidth}
	\begin{align*}
	\sigma_{b}^2 + 8 \sigma_b + 12 = 0
	\\
	\sigma_{b-a} =  -2 \quad , \ K(\sigma_{b-a}) = 1
	\\
	\sigma_{b-in} =  -6 \quad , \ K(\sigma_{b-in}) = 9
	\end{align*}
\end{minipage}

\vspace{12pt}

\item Finding the imaginary axis crossings.

These crossings are particularly important, since at these crossings
(generally)   stability changes. At these points the poles become purely
imaginary $p_{1,2} = \pm j \omega$. If we insert this into characteristic
equation we get
%
\begin{align*}
1 + K G_{OL} (j \omega) &= 0 \\
D(j \omega) + K N(j \omega) &= 0 \\ ,
\\
\mathrm{Re} \lbrace D(j \omega) + K N(j \omega) \rbrace &= 0 \\
\mathrm{Im} \lbrace D(j \omega) + K N(j \omega) \rbrace &= 0
\end{align*}
%
Note that depending on the order of the system,
solving the above equation can be
most computationally very heavy.

\textbf{Second way} of finding the imaginary axis crossing is to
apply the Routh-Hurwitz criteria. Note that at these crossings the
system becomes unstable. Using this fact, we can first construct a
Routh table for the closed-loop characteristic equation and then
derive the $K$ values where a change of stability occurs. After that, we can
use the computed critical $K$ values to derive the pole locations
on the imaginary axis..

\vspace{6pt}

\textbf{Ex:} Draw the root locus diagram for $G_{OL}(s) = \frac{1}{s (s+2) (s+3)}$,
compute the break-away point and imaginary axis crossings
(with the associated gain values).

\vspace{12pt}

\begin{minipage}[h]{0.55\linewidth}
    \begin{center}
      \includegraphics[width=0.95\textwidth]{imag}
    \end{center}
\end{minipage}
\begin{minipage}[h]{0.45\linewidth}
	Break-away point
	\begin{align*}
	& 3 \sigma_{b}^2 + 10 \sigma_b + 6 = 0
	\\
	& \sigma_{b,1} = - 0.8  \quad , \ K(\sigma_{b,1}) = 2.1 > 0 \rightarrow \mathrm{OK}
	\\
	& \sigma_{b,2} =  -2.5 \quad , \ K(\sigma_{b,2}) = -0.6 < 0 \rightarrow \mathrm{NO}
	\end{align*}
	Imaginary axis crossing
	\begin{align*}
	& D(j \omega) + K (j \omega) = 0 \\
	& (j \omega)^3 + 5 (j \omega)^2 + 6 (j \omega) + K = 0 \\
	& (K - 5 \omega^2) + (6 \omega - \omega^3) j  = 0
	\\ & \Rightarrow \omega = \sqrt{6}  \quad , \ K = 30
	\end{align*}
\end{minipage}

\vspace{12pt}

Now let's find the imaginary axis crossings using the Routh table.
The characteristic equation for this system is $s^3 + 5 s^2 + 6 s +
K$, then the Routh table takes the form

\vspace{6pt}
\begin{minipage}[h]{1\linewidth}
\begin{center}
\begin{tabular}{|c || c || c | l |}
\hline
$s^3$ & 1 & 6 &
\\ \hline
$s^2$ & 5 & K &
\\ \hline
$s^1$ & $\frac{30 - K}{5}$ & 0 &
\\ \hline
$s^0$ & K & 0 &
\\ \hline
\end{tabular}
\end{center}
\end{minipage}
\vspace{6pt}

We know that in order for the system to stable $K \in (0 , 30)$,
since we only consider positive $K$ values, when $K = 30$
system stability changes (from stable to unstable). Let $K=30$
and re-form the Routh table.

\vspace{6pt}
\begin{minipage}[h]{1\linewidth}
\begin{center}
\begin{tabular}{|c || c || c | l |}
\hline
$s^3$ & 1 & 6 &
\\ \hline
$s^2$ & 5 & 30 & $\rightarrow A(s) = 5 s^2 + 30$
\\ \hline
$s^1$ & 10 & 0 &  $\leftarrow A'(s) = 10 s $
\\ \hline
$s^0$ & 30 & 0 &
\\ \hline
\end{tabular}
\end{center}
\end{minipage}
\vspace{6pt}

Based on the Routh table, we conclude that when $K = 30$, system
becomes unstable, the unstable poles are located on the imaginary
axis, and their locations can be find using the Auxiliary polynomial
as
%
\begin{align*}
A(s) = 0 \rightarrow p_{1,2} = \pm \sqrt{6} j
\end{align*}
%

\newpage

\subsection{Root-locus with respect to different parameters}

Let's consider the following feedback control system.
We wonder the location of closed-loop
poles with respect to the parameter $A$ which does not directly fit to
the classical form of root-locus.

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

Let's first compute the closed-loop TF and analyze the characteristic
equation.
%
\begin{align*}
  \frac{Y(s)}{R(s)} &= \frac{ K G(s) }{1 + K G(s) H(s)}
\\
  &= \frac{ s+A }{s (s+4) (s+A) + 4 A }
\\
 &= \frac{ s+A }{s^3 + (A + 4) s^2 + 4 A s + 4 A}
\end{align*}
%
Now let's organize the characteristic equation
%
\begin{align*}
(s^3 + 4 s^2) + A  (s^2 + 4 s + 4) = 0
\end{align*}
%
If we divide the characteristic equation by $(s^3 + 4 s^2)$ we obtain
%
\begin{align*}
1+ A \frac{s^2 + 4 s + 4}{s^3 + 4 s^2} &= 0
\\
1+ A \bar{G}_{OL}(s) &= 0
\end{align*}
%
Now if we consider $\bar{G}_{OL}(s)$ as the open-loop transfer
function and draw the root-locus,
then we would derive the dependence
of the roots to the parameter A.

Root-locus of the system w.r.t parameter $A > 0$ is given below.

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

\end{enumerate}

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


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Beamer Presentation
% LaTeX Template
% Version 1.0 (10/11/12)
%
% This template has been downloaded from:
% http://www.LaTeXTemplates.com
%
% License:
% CC BY-NC-SA 3.0 (http://creativecommons.org/licenses/by-nc-sa/3.0/)
%
% Modified by Jeremie Gillet in November 2015 to make an OIST Skill Pill template
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%----------------------------------------------------------------------------------------
%	PACKAGES AND THEMES
%----------------------------------------------------------------------------------------

\documentclass{beamer}

\mode<presentation> {

\usetheme{Madrid}

\definecolor{OISTcolor}{rgb}{0.65,0.16,0.16}
\usecolortheme[named=OISTcolor]{structure}

%\setbeamertemplate{footline} % To remove the footer line in all slides uncomment this line
%\setbeamertemplate{footline}[page number] % To replace the footer line in all slides with a simple slide count uncomment this line

\setbeamertemplate{navigation symbols}{} % To remove the navigation symbols from the bottom of all slides uncomment this line
}

\usepackage{graphicx} % Allows including images
\usepackage{booktabs} % Allows the use of \toprule, \midrule and \bottomrule in tables
\usepackage{textpos} % Use for positioning the Skill Pill logo
\usepackage{fancyvrb}
\usepackage{tikz}
\usepackage{hyperref}
\usepackage{listings}

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}

\lstset{frame=tb,
  language=python,
  aboveskip=3mm,
  belowskip=3mm,
  showstringspaces=false,
  columns=flexible,
  basicstyle={\small\ttfamily},
  numbers=none,
  numberstyle=\tiny\color{gray},
  keywordstyle=\color{blue},
  commentstyle=\color{dkgreen},
  stringstyle=\color{mauve},
  breaklines=true,
  breakatwhitespace=true,
  tabsize=3
}

\newcommand*{\lstitem}[1]{
  \setbox0\hbox{\lstinline{#1}}
  \item[\usebox0]
  % \item[\hbox{\lstinline{#1}}]
  \hfill \\
}

%----------------------------------------------------------------------------------------
%	TITLE PAGE
%----------------------------------------------------------------------------------------

\title[Skill Pill]{Skill Pill: Julia} % The short title appears at the bottom of every slide, the full title is only on the title page
\subtitle{Lecture 2: FFI and performance}

\author{James Schloss \and Valentin Churavy} % Your name
\institute[OIST] % Your institution as it will appear on the bottom of every slide, may be shorthand to save space
{
Okinawa Institute of Science and Technology \\ % Your institution for the title page
\textit{james.schloss@oist.jp}\\
\textit{valentin.churavy@oist.jp} % Your email address
}
\date{July 7, 2017} % Date, can be changed to a custom date

\begin{document}

\setbeamertemplate{background}{\includegraphics[width=\paperwidth]{SPbackground.png}} % Adding the background logo

\begin{frame}
\vspace*{1.4cm}
\titlepage % Print the title page as the first slide
\end{frame}

\setbeamertemplate{background}{} % No background logo after title frame

\addtobeamertemplate{frametitle}{}{% Adding the Skill Pill logo on the title screen after title frame
\begin{textblock*}{100mm}(.92\textwidth,-0.9cm)
\includegraphics[height=0.85cm]{julia.pdf}
\end{textblock*}}

\begin{frame}
  \tableofcontents
\end{frame}
\section{The foreign world, using Julia to reuse prior work}
\begin{frame}[fragile]{Using Fortran and C in Julia}

Julia allows you to use other languages (such as Fortran or C) by using the \texttt{ccall} function:

\begin{lstlisting}
    julia> t = ccall((:clock, "libc"), Int32, ())
    2292761
\end{lstlisting}

Here, we are calling the \texttt{clock} function from the \texttt{libc} library in C.

\end{frame}

\begin{frame}[fragile]{Your legacy code}
Let's say you want to use a simply multiply function in Fortran:
\begin{lstlisting}[language=fortran]
      !! We'll be using subroutines intead of functions
      subroutine multiply(A, B, C)
          REAL*8 :: A, B, C
          C = A * B
          return
      end
\end{lstlisting}
\vspace{0.5cm}
or C:
\begin{lstlisting}[language=c]
    // Nothing fancy here...
    double multiply(double A, double B){
        return A*B;
    }
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]{Preparing your legacy code}

In order to use your favorite C or Fortran code in Julia, you need to compile it into a library, like so:

\begin{lstlisting}
    gcc -shared -O2 multiply.c -fPIC -o c_multiply.so
    gfortran -shared -O2 multiply.f90 -fPIC -o
        fortran_multiply.so
\end{lstlisting}

\pause
These will create libraries with all of the necessary functions you could want, but beware:

\begin{center}
\textbf{C and Fortran compilers mangle function names!}
\end{center}
\end{frame}

\begin{frame}[fragile]{Using your legacy code}
There are 3 things to keep. Make sure you
\begin{enumerate}
\item Have the right mangled name
\item Are using the right type
\item Are using the function correctly.
\end{enumerate}
\pause

\vspace{0.5cm}
For example, in C:
\begin{lstlisting}
    # This function multiplies a and b into c by using the
    # created C library
    function call_c()
        a = Cdouble(1.0)
        b = Cdouble(3.0)
        c = ccall((:multiply, "/full/path/to/c_multiply"),
            Cdouble,(Cdouble, Cdouble),a,b)
        println(c)
    end
\end{lstlisting}
\end{frame}

\begin{frame}[fragile]{Using your legacy code}
Pointers are okay! For example, in Fortran:

\begin{lstlisting}
    # This function multiplies a and b into c by using
    # the created FORTRAN library
    function call_fortran()
        a = Cdouble[1.0]
        b = Cdouble[2.0]
        c = Cdouble[0.0]
        ccall((:multiply_, "/full/path/to/fortran_multiply"),
           Void,(Ptr{Float64},Ptr{Float64},Ptr{Float64}),
                 a,b,c)
        println(c[1])
    end
\end{lstlisting}

\pause
More information can be found here: \url{https://docs.julialang.org/en/stable/manual/calling-c-and-fortran-code/}
\end{frame}
\begin{frame}{Support for other languages}
  \begin{description}
    \item[Python] https://github.com/JuliaPy/PyCall.jl
    \item[R] https://github.com/JuliaInterop/RCall.jl
    \item[C++] https://github.com/Keno/Cxx.jl
    \item[Matlab] I have heard rumours of such a thing existing, but the horror $\cdots$
  \end{description}
  \begin{block}{Conclusion}
    Start writing Julia code now without being worried about losing your prior work!
  \end{block}
\end{frame}

\section{The Julia compiler}
\begin{frame}[fragile]{The stages of the compiler}
  \begin{enumerate}
    \item Surface syntax (the code you write)
    \item Desugared AST \lstinline{@code_lowered}
    \item Type-inferred AST \lstinline{@code_typed}
    \item LLVM IR \lstinline{@code_llvm}
    \item Native assembly \lstinline{@code_native}
  \end{enumerate}
\end{frame}

\begin{frame}[fragile,allowframebreaks]{Macros and metaprogramming}
  Julia has powerful metaprogramming facilities that allow you to manipulate the AST (abstract syntax tree) of a Julia program.
  \begin{lstlisting}
  prog = "1 + 1"
  ex1 = parse(prog) # Parse a Julia program and return the expression tree
  # This is equal to manually constructing the expr tree
  ex2 = Expr(:call, :+, 1, 1)
  # Or by using quote
  ex3 = :( 1 + 1)
  dump(ex1) # Inspect an expr tree
  \end{lstlisting}
  \vspace{-1em}
  \begin{block}{}
    The key point here is that Julia code is internally represented as a data structure that is accessible from the language itself.
  \end{block}
  \vspace{-1em}
  \begin{lstlisting}
  # use eval to execute an expression
  eval(ex1)
  \end{lstlisting}
  \framebreak
  \begin{block}{Macros}
    \lstinline{eval} works at runtime and you should avoid it as much as possible. You can use macros to do syntax transformations statically.
    Macros always start with an \verb|@|.
  \end{block}
  \begin{lstlisting}
  # simplistic implementation of the @assert macro in Julia
  macro assert(ex)
      return :( $ex ? nothing : throw(AssertionError($(string(ex)))) )
  end

  @assert 1 == 1.0
  @assert 1 == 0

  @macroexpand @assert 1 == 0
  \end{lstlisting}
\end{frame}
\section{Performance}
\begin{frame}[fragile]{Methodology}
  \pause
  \begin{block}{Measure first}
    Before you start iterating on your code establish a baseline performance.
    Computers are noisy system so we use the lowest runtime as a metric.
  \end{block}
  \pause
  \begin{itemize}
    \item Check for type-instabilities with \lstinline{@code_warntype}
    \item Measure runtime and allocations with \lstinline{@time}
    \item Benchmark using \lstinline{@btime}, and \lstinline{@benchmark} from \lstinline{BenchmarkTools.jl}
    \item Profiler and \lstinline{ProfileView.jl}
    \item Memory Allocation tracker
  \end{itemize}
  \pause
  Read the performance tips section of the Julia manual \url{https://docs.julialang.org/en/stable/manual/performance-tips/}
\end{frame}
\begin{frame}{About global scope}
  \begin{block}{Performance tips}
    A global variable might have its value, and therefore its type, change at any point. This makes it difficult for the compiler to optimize code using global variables. Variables should be local, or passed as arguments to functions, whenever possible.

    Any code that is performance critical or being benchmarked should be inside a function.
  \end{block}
  \pause
  \begin{block}{}
    The natural unit of compilation is a function. Code in global scope does not get compiled.
  \end{block}
\end{frame}
\begin{frame}[fragile]{Type instabilities}
  Julia relies on type-inference to generate optimal code, when your code has type-instabilities your code will execute slower.
  Some of these problems can be fixed by compiler improvements over time, but if you want the fast code now they are best avoided,
  especially in hot code. Sometimes introduction a function barrier can help generating optimal code.
  \pause
  \begin{lstlisting}
  typeinstable() = rand() < 0.5 ? 1.0 : 1
  \end{lstlisting}
\end{frame}
\begin{frame}[fragile]{Using BenchmarksTools.jl}
  \begin{block}{}
    BenchmarkTools provides a variety of tools to run benchmark and compare them. See \url{https://github.com/JuliaCI/BenchmarkTools.jl}
  \end{block}
  \begin{lstlisting}
julia> using BenchmarkTools
julia> @benchmark sin(1)
julia> @benchmark sum(rand(1000))
# Interpolate inputs and globals into your benchmark to measure the right thing.
julia> @benchmark sum(\$(rand(1000)))
  \end{lstlisting}
\end{frame}
\begin{frame}[fragile]{Using the Profiler}
  You can profile a piece of code with Julia's inbuilt profiler.
  \begin{description}
      \lstitem{@profile fun()} Profile a specific function
      \lstitem{Profile.clear()} Clear the recorded profile
      \lstitem{Profile.print()} Print the profile
      \lstitem{Profile.print(C=true)} Print the profile including stacktraces reaching into C.
  \end{description}
  \begin{block}{ProfileView.jl}
    The textual output of the profiler can be hard understand \lstinline{ProfileView.jl} gives a graphical representation.
    \begin{lstlisting}
    using ProfileView
    ProfileView.view()
    \end{lstlisting}
  \end{block}
\end{frame}
\begin{frame}[fragile]{Using the memory allocation tracker}
  To track memory allocations you have to start Julia with the memory allocation tracker enabled.
  \begin{lstlisting}
  # Track only allocation in user code
  julia --track-allocation=user
  # Track allocation in all code (includeing the Julia base)
  julia --track-allocation=all
  \end{lstlisting}
  After quiting Julia *.mem files are created that contain cumulative amounts of allocated memory.
  \begin{block}{Getting useful data}
    Since we have to start Julia with track allocations enable we will gather a lot of noisy data.
    To cut down the noise run your code in a session once and then use \lstinline{Profile.clear\_malloc\_data()}
    to reset the allocation counts and then run your code again only tracking revelant allocations.
  \end{block}
\end{frame}
\begin{frame}[fragile]{A simple example}
\begin{lstlisting}
function mysum(A)
  acc = 0
  for x in A
     acc += x
  end
  return acc
end
\end{lstlisting}
\end{frame}
\section{Performance analysis}
\begin{frame}[fragile]{A supposedly simple task}
  \begin{lstlisting}
  function myfun()
    s = 0.0
    N = 10000
    for i=1:N
        s+=det(randn(3,3))
    end
    s/N
  end
  \end{lstlisting}
\end{frame}

\begin{frame}{What is next?}
  \begin{description}
    \item[Next Session] Data Structures and Algorithms
    \item[Last Session] Parallel computing, threading, GPUs? Up to grabs.
  \end{description}
\end{frame}
\end{document}


\documentclass[journal, a4paper]{IEEEtran}

\usepackage{graphicx}
%\usepackage{subfigure}
\usepackage{url}
\usepackage{amsmath}
% Some useful/example abbreviations for writing math
\newcommand{\argmax}{\operatornamewithlimits{argmax}}
\newcommand{\argmin}{\operatornamewithlimits{argmin}}
\newcommand{\x}{\mathbf{x}}
\newcommand{\y}{\mathbf{y}}
\newcommand{\ypred}{\mathbf{\hat y}}

\begin{document}

% Define document title, do NOT write author names
\title{The Title of your Report}
\author{Anonymous Authors}
\maketitle

% Write abstract here
\begin{abstract}
	A short summary of your project. You should change also the title, but do \emph{not} enter any author names or anything that unnecessarily identifies any of the authors. It is suggested you use a similar structure (sections, etc.) as demonstrated in this document, but you can make the section headings more descriptive if you wish. Of course \emph{you should delete all the text in this template and write your own}! -- this text simply provides detailed instructions/hints on how to proceed.

\end{abstract}

% Each section begins with a \section{title} command
\section{Introduction}

Describe what you did. Provide access to your anonymized code\footnote{Our code is available here: \url{http://anonymouslinktoyourcode.zip}}.

Note that results should be reproducible using the technologies from the labs (i.e., Python, and selecting among Scikit-Learn, OpenAI Gym, TensorFlow, PyGame, \ldots).

Do not change the formatting (columns, margins, etc). Hint: shared tools like \texttt{http://sharelatex.com/} and \texttt{http://overleaf.com/} are great tools for collaborating on a multi-author report in latex. If you wish to use Word, base it on the IEEE template\footnote{\url{https://www.ieee.org/publications_standards/publications/conferences/2014_04_msw_a4_format.doc}} and convert to \texttt{pdf} for submission.

\section{Background and Related Work}

Elaborate (in your own words) the background material required to understand your work. It should cover a subset of the topics touched upon in the course. You are encouraged to cite topics in lectures, e.g., structured output prediction in \cite{LectureSOP}, book chapters, e.g., Chapter 9 from \cite{Barber}, or articles from the literature, e.g., \cite{Astar,DeepMindSC2}. Basically, you should prepare the reader to understand what you are about to present in the following sections. Eq.~\eqref{eq:MAP} shows a random equation.
\begin{equation}
	\label{eq:MAP}
	% Note the example \newcommand s defined above which make it faster to write latex math
	\ypred = \argmax_{\y \in \{0,1\}} p(\y|\x)
\end{equation}

\section{The Environment}

Describe your environment, either one you adapted/borrowed from somewhere, or designed yourself. Convince the reader that it is an interesting and/or challenging environment (could it potentially have real-world use or is based on real-world data? Or simply to provide an interesting/fun/challenging problem to tackle. In particular you should outline the particular challenges it poses as a RL problem.

\section{The Agent}

The agent you designed for your environment. Justify your choice and design and explain briefly how you implemented/configured it. Naturally, if you took a ready-made environment, you should invert relatively much more effort into this section than the previous one.

\section{Results and Discussion}

This is one of the most important sections. You put your agent to the test in the environment, you show -- and most importantly -- you interpret the results.

\subsection{Performance of your Agent in your Environment}

Show plots, graphs, tables (e.g., Table~\ref{a_table}), etc. You may wish to encourage readers to reproduce results for themselves, e.g., run \texttt{runDemo.py} in our source code. Show how your agent performs well, or, if it doesn't perform well, it is better to explain why (this is a result in itself!). In any case, you \emph{must} highlight the weaknesses of your agent as well as its strengths.

\begin{table}[h]
	\caption{\label{a_table}This table is just an example.}
	\centering
	\begin{tabular}{lll}
		\hline
		\textbf{Environment config.} & \textbf{Standard SARSA} & \textbf{Our Improved Agent}  \\
		\hline
		Simulation 1        & 10             & 15 \\
		Simulation 2        & 12             & 11 \\
		\hline
	\end{tabular}
\end{table}

\subsection{Performance of your Agent in the ALife Environment}

You deploy your agent in the ALife\footnote{\url{https://github.com/jmread/alife}} environment (a random screenshot shown in Figure~\ref{a_figure}). Does it work well? Why? Why not? Justify the adaptation you think is best.

\begin{figure}[h]
	\centering
	\includegraphics[width=0.8\columnwidth]{alife.png}
	\caption{\label{a_figure}An example figure}
\end{figure}

\section{Conclusion and Future Work}
	This section summarizes the paper: Your environment and agent, its strength and its weaknesses. Also remark about what would be the next steps you would take if you or someone else were to continue/extend this project.
	Note that for the initial submission you are limited strictly to 4 pages (double column), \emph{not including references}. An extra page will be allowed for final submission (after the initial reviews).

% The bibliography:
\begin{thebibliography}{4}

	\bibitem{Barber} % Book
	D.~Barber. Bayesian Reasoning and Machine Learning,
	{\em Cambridge University Press}, 2012.

	\bibitem{LectureSOP} % Web document
		J.~Read. Lecture III - Structured Output Prediction and Search. \textit{INF581 Advanced Topics in Artificial Intelligence}, 2018.

	\bibitem{Astar}
	D.~Mena et al. A family of admissible heuristics for A* to perform inference in probabilistic classifier chains.
	{\em Machine Learning}, vol. 106, no. 1, pp 143-169, 2017.

	\bibitem{DeepMindSC2}
	O.~Vinyals et al. StarCraft {II:} {A} New Challenge for Reinforcement Learning.
	\url{https://arxiv.org/abs/1708.04782}, 2017.

\end{thebibliography}

% Your document ends here!
\end{document}


\documentclass[twocolumn,prl,nobalancelastpage,aps,10pt]{revtex4-1}
%\documentclass[rmp,preprint]{revtex4-1}
\usepackage{graphicx,bm,times, amsmath}

\usepackage{float} %useful to structure the figures if they're not going where you'd like them to. When beginning a figure environment with "\begin{figure}" add An H to the end to make the placement more forceful like "\begin{figure}[H]"

\usepackage{gensymb} %has some extra symbols like the degree symbol

\graphicspath{{figures/}}

\begin{document}

\title{Investigating the motion of a rocket in orbit}

\author{Francis Taylor}

\affiliation{Level 5 Laboratory, School of Physics, University of Bristol.}

\date{\today}

\begin{abstract}
The motion of a satellite in orbit was modelled using Newton's Universal Law of Gravitation and solved using the 4th-order Runge-Kutta method for solving ordinary differential equations. The effect on varying the initial height and velocity of the satellite were investigated, and it was found that very small changes in these values could greatly affect the path taken. This model was expanded to simulate two paths of a rocket (figure-of-eight and elliptical) from Low Earth Orbit to the Moon, and back. The figure-of-eight path was found to have a shorter period (9 days, 20 hours and 6 minutes) than the elliptical path (12 days, 3 hours and 36 minutes) and was more stable, with a wider tolerance in the initial values that could give a successful path.
\end{abstract}

\maketitle

\section{INTRODUCTION AND THEORY}

A satellite in orbit around a body follows an elliptical path, with the barycenter at one of the foci of the ellipse, as described by Kepler's First Law \cite{Kepler1992}. This path is defined by the changing velocity of the satellite as it orbits, being lowest when it is at the apoapsis,  which is derived from Newton's laws of motion and gravitation. The force on two bodies with mass is given by the latter, being \cite{Newton1848}

\begin{equation}\label{universalGravitation}
	\bm{F_{12}} = - \frac{G m_{1}m_{2}}{\mid\bm{r_{12}}\mid^{2}}\bm{\hat{r}} ,
\end{equation}

where $\bm{F_{12}}$ is the force between objects 1 and 2, $G$ is the gravitational constant, $\bm{r_{12}}$ is the distance between the center of the objects (see figure \ref{gravitationFigure}), and $m_{1}$ and $m_{2}$ are the masses of the objects.

\begin{figure}[h!]
\includegraphics*[width=0.96\linewidth,clip]{newtonGravitation}
\caption{Newton's Law of Universal Gravitation} \label{gravitationFigure}
\end{figure}

From this, the equation of motion for an orbiting satellite can be derived using Newton's second law, giving \cite{Newton1848}

\begin{equation}\label{equationMotion}
m \bm{\ddot{r}} = - \frac{mMG}{\mid\bm{r}\mid^{3}}\bm{r} ,
\end{equation}

where $m$ is the mass of the satellite, $M$ is the mass of the large body, and $r$ is the position of the satellite relative to the centre of the large body. This is an ordinary differential equation, which can be solved using numerical analysis. One solution of this is using the Runge-Kutta family of iterative methods.

\subsection{Runge-Kutta Methods}

The Runge-Kutta methods are used to provide approximate solutions to ordinary differential equations. Such methods discretise a continuous function in both space and time, allowing for integration over discrete time intervals, a technique easily done using computer simulations \cite{DeVries2011}. For this simulation, the 4th-order Runge-Kutta (RK4) method was used. This method evaluates the slope of a function with known initial values at four different points in a given interval, shown in figure \ref{rk4SlopesFigure}, given by the following \cite{Press2007, Atkinson1989}

\begin{align}
k_{1}& = f(x_{n}, y_{n}) \\
k_{2}& = f(x_{n} + \frac{h}{2}, y_{n} + \frac{hk_{1}}{2})\\
k_{3}& = f(x_{n} + \frac{h}{2}, y_{n} + \frac{hk_{2}}{2})\\
k_{4}& = f(x_{n} + h, y_{n} + hk_{3})
\end{align}

\begin{figure}[ht]
	\includegraphics*[width=0.96\linewidth,clip]{rk4Slopes}
	\caption{Slopes used by the 4th-order Runge-Kutta method.} \label{rk4SlopesFigure}
\end{figure}

In the context of solving equation \ref{equationMotion}, $h$ is the time step, and $x$ and $y$ are the positional coordinates of the rocket. $k_{1}$ is the slope at the beginning of the interval, calculated using $y$. $k_{2}$ is the slope at the midpoint of the interval, calculated using $y$ and $k_{1}$. $k_{2}$ is also the slope at the midpoint of the interval, but is instead calculated using $k_{2}$ instead of $k_{1}$. Finally, $k_{4}$ is the slope at the end of the interval, calculated using $y$ and $k_{4}$.

The solution to this is then given by the weighted average of the increments \cite{Press2007}

\begin{align}
y_{n+1}& = y_{n} + \frac{h}{6}(k_{1} + 2k_{2} + 2k_{3} + k_{4}) , \\
t_{n+1}& = t_{n} + h .
\end{align}\label{rungeEquation}

As this method is of 4th-order, the global truncation error is of  4th-order, so this method is much more accurate than comparable methods, such as Euler's method, which is 2nd-order \cite{Atkinson1989, Suli2003}. Also of note is that if $f$ is independent of $y$, then RK4 simplifies to Simpson's rule \cite{Suli2003}.

A two-dimensional orbit with the larger body at the origin requires several variables that need to be evaluated in four functions, as RK4 couples the variables together:

\begin{align}
f_{1}(t, xx, y, v_{x}, v_{y})& = \frac{dx}{dt} = v_{x}\\
f_{2}(t, xx, y, v_{x}, v_{y})& = \frac{dy}{dt} = v_{y}\\
f_{3}(t, xx, y, v_{x}, v_{y})& = \frac{dv_{x}}{dt} = \frac{-GMx}{(x^{2} + y^{2})^{3/2}}\\
f_{4}(t, xx, y, v_{x}, v_{y})& = \frac{dv_{y}}{dt} = \frac{-GMy}{(x^{2} + y^{2})^{3/2}}
\end{align}

%NOTE THIS MAY NEED TO BE UPDATED
Ignoring the arguments that are not needed, we can apply these functions to each of equations 3-6 to obtain values for $x$, $y$, $v_{x}$ and $v_{y}$ at each increment in the interval. Using this, and applying it to equation 7, a series of time-stepping equations is obtained, where $y$ is replaced by each of the variables listed. These can easily be calculated using a computer, by iterating through a loop, with each increment in the interval stored in an array.

\section{RESULTS AND DISCUSSION}

A circular Medium Earth orbit was modelled for 60000s with a time step of 2s, an initial height of 3621km above the Earth's surface and initial velocity of 6313ms$^{-1}$, shown in figure \ref{circDefFig}. The orbit was stable, completing several revolutions in this time period, and this is also confirmed by the energy graph shown in \ref{circDefEn}, as the energy remains constant over time. It can be inferred from this that the rocket remained on an gravitational equipotential line, hence it's height above the surface is constant.

\begin{figure}[ht!]
\includegraphics*[width=0.96\linewidth,clip]{circularDefault}
\caption{Position of the orbit of a rocket with initial height of 3621km above the Earth's surface and velocity 6313ms$^{-1}$. The path of the rocket is shown in red, the red dot represents the current position of the rocket, and the Earth is represented by the green circle.} \label{circDefFig}
\end{figure}

\begin{figure}[ht]
	\includegraphics*[width=0.96\linewidth,clip]{circularDefaultEnergy}
	\caption{A plot of energy against time for the circular orbit shown in figure \ref{circDefFig}. Gravitational energy is shown by the blue line, kinetic energy by the orange line, and total energy by the green line in between.} \label{circDefEn}
\end{figure}

If the initial height or velocity was too high, the rocket would not enter into a stable orbit, instead moving along a straight path into space, as shown in figure \ref{circHighFig}, or crashing into the Earth. Here, the initial height was increased by 5km to 8621km above the surface of the Earth (15000km from the centre of the Earth), but the initial velocity was kept equal to that in figure \ref{circDefFig}. The rocket was deemed to have crashed if the magnitude of  $x$ and $y$ was less than the radius of the Earth. When simulating the Moon orbit, the same condition was used for the Earth, but a check added to ensure the y-coordinate of the rocket was less than half the distance between the surfaces of the Earth and Moon, $r$. For crashing into the Moon, the code checked if the y-coordinate of the rocket was greater than half the distance between the surfaces, and the magnitude of $x$ and $y-r$ was less than the radius of the Moon.

\begin{figure}[ht]
	\includegraphics*[width=0.96\linewidth,clip]{circularTooHigh}
	\caption{Position of the orbit of a rocket with initial height of 8621km above the Earth's surface and velocity 6313ms$^{-1}$. The path of the rocket is shown in red, the red dot represents the current position of the rocket, and the Earth is represented by the green circle.} \label{circHighFig}
\end{figure}

\begin{figure}[ht]
	\includegraphics*[width=0.96\linewidth,clip]{circularTooHighEnergy}
	\caption{A plot of energy against time for the circular orbit shown in figure \ref{circHighFig}. Gravitational energy is shown by the blue line, kinetic energy by the orange line, and total energy by the green line in between.} \label{circHighEn}
\end{figure}

An elliptical orbit was also modelled for the same time period and initial height, but with an initial velocity of 7500ms$^{-1}$, shown in figure \ref{elDefFig}. Again, this orbit was stable, completing several orbits in the time period, and this is also confirmed by figure \ref{elDefEn}, which shows the energy of the system over time. Several clear periods can be seen, and while the gravitational and kinetic energies oscillate, the total energy remains constant. The periodic nature of the energy is caused by the rocket coming closer and moving away from the Earth, and is a direct consequence of Kepler's Second Law \cite{Kepler1992}. The magnitude of the kinetic and gravitational energy is highest when the rocket is closest to the Earth, and the least when at apoapsis.

\begin{figure}[ht]
	\includegraphics*[width=0.96\linewidth,clip]{ellipticalDefault}
	\caption{Position of the orbit of a rocket with initial height of 3621km above the Earth's surface and initial velocity 7500ms$^{-1}$. The path of the rocket is shown in red, the red dot represents the current position of the rocket, and the Earth is represented by the green circle.} \label{elDefFig}
\end{figure}

\begin{figure}[ht]
	\includegraphics*[width=0.96\linewidth,clip]{ellipticalDefaultEnergy}
	\caption{A plot of energy against time for the elliptical orbit shown in figure \ref{elDefFig}. Gravitational energy is shown by the blue line, kinetic energy by the orange line, and total energy by the green line in between.} \label{elDefEn}
\end{figure}

For the slingshot around the moon, there were two options for a stable orbit; a figure-of-eight passing between the Earth and Moon twice, or an elliptical orbit, going round the outside of the Moon without crossing its previous path. The figure-of-eight was easier to implement, as a larger range of values were able to give a path that did not crash into the Moon, however a lot of trial and error was needed to find said values. Figure \ref{moonFoEFig} shows an example of such an orbit, with an initial height of 629km above the Earth's surface, and velocity 10559.9ms$^{-1}$. It was easiest to launch the rocket when it was in Low Earth orbit from the side of the Earth that was opposite the Moon. For the elliptical orbit around the Moon, an initial velocity of 10591ms$^{-1}$ was used. Figure \ref{moonEFig} shows the path taken for this orbit. It was quite surprising to see that a difference of only 31.1ms$^{-1}$ in the initial velocity was all that was needed to obtain each type of orbit, and upon further investigation, it was found that changing the initial velocity in the order of $\times$10$^{-1}$ decided whether the rocket crashed into the moon or not. The figure-of-eight orbit had a smaller period than the elliptical one, completing one orbit in about 850000 seconds, or 9 days, 20 hours and 6 minutes. The elliptical orbit took about 1050000 seconds to complete one period, or 12 days, 3 hours and 36 minutes. The magnitude of the gravitational and kinetic energy of the rocket on the elliptical path was lower than that of the figure-of-eight path, and this can be demonstrated by the smaller second peaks in figure \ref{moonEEn} than in figure \ref{moonFoEEn}, likely caused by the larger distance between the Earth and the rocket in the elliptical path. This is likely caused by the larger loop (seen at the top of figure \ref{moonEFig}) causing the rocket to enter orbit around the Earth again on a wider path. The rocket on the figure-of-eight path does not have this issue, and passes much closer to the Moon and Earth, evidenced by a small peak at roughly 400000 seconds in figure \ref{moonFoEEn} where it passes the Moon, and the large second peaks when it returns to the Earth.

\begin{figure}[ht]
	\includegraphics*[width=0.96\linewidth,clip]{moonFoE}
	\caption{Lunar slingshot orbit of a rocket with initial height of 629km above the Earth's surface and initial velocity 10559.9ms$^{-1}$. The path of the rocket is shown in red, the red dot represents the current position of the rocket, the Moon is represented by the grey dot, and the Earth is represented by the green circle. Both axes are of order 10$\times10^{8}$.} \label{moonFoEFig}
\end{figure}

\begin{figure}[ht]
	\includegraphics*[width=0.96\linewidth,clip]{moonFoEEnergy}
	\caption{A plot of energy against time for the lunar orbit shown in figure \ref{moonFoEFig}. Gravitational energy is shown by the blue line, kinetic energy by the orange line, and total energy by the green line in between.} \label{moonFoEEn}
\end{figure}

\begin{figure}[ht]
	\includegraphics*[width=0.96\linewidth,clip]{moonE}
	\caption{Lunar slingshot orbit of a rocket with initial height of 629km above the Earth's surface and initial velocity 10591ms$^{-1}$. The path of the rocket is shown in red, the red dot represents the current position of the rocket, the Moon is represented by the grey dot, and the Earth is represented by the green circle. Both axes are of order 10$\times10^{8}$.} \label{moonEFig}
\end{figure}

\begin{figure}[ht]
	\includegraphics*[width=0.96\linewidth,clip]{moonEEnergy}
	\caption{A plot of energy against time for the lunar orbit shown in figure \ref{moonEFig}. Gravitational energy is shown by the blue line, kinetic energy by the orange line, and total energy by the green line in between.} \label{moonEEn}
\end{figure}

\section{IMPROVEMENTS TO CODE}

As can be seen in the energy graphs (for example figure \ref{circDefEn}), at the end of the simulation, all the energy values jump to 0. This may be because there is an extra time value included in the array, where none exists in the energy arrays, but this could be fixed to remove this, by using validation checks to remove extra zeros at the end of the array. As well as this, the animation used for the graphs crashes when the end of the array is reached. I spent several hours trying to debug this, but could not understand why an extra index was being used for $N$, the total number of iterations, when this was the same as the size of the $x$ and $y$ arrays being passed to the function controlling the animation. An improvement would be to ensure that the animation function was being passed the correct number of iterations. Another improvement to the code would be to make the size of the Earth and Moon on the graphs to scale, as often when the rocket crashes, it is displayed as being in space, when in actuality it has hit the Moon/Earth, and this isn't displayed very well. To improve the simulation, the equation of motion could be adapted to account for relativistic gravitational effects, however as this is outside my area of knowledge I did not feel comfortable implementing this. One final improvement that could be made would be to move a large amount of the code into more functions, as several sections are reused very often. This is not difficult, and was only restricted by time, but an example of a useful function to help remove repetition would be implementing a function to handle all the Pythagoras, which would make the code easier to read.

\section{CONCLUSIONS}

%What have we learnt from this experiment?  How might we do things better?
The 4th-order Runge-Kutta method has been shown to be invaluable in numerically evaluating ordinary differential equations and modelling a satellite in orbit using Newton's gravity. The figure-of-eight Moon orbit is easier to perform, with a larger tolerance in the initial height and velocity values being able to produce a successful orbit. The code could have been improved to include gravitational effects from general relativity.

\section{REFERENCES}
%Do not include material in your reference list that is not specifically referred to in the text.  It is not a list of
%things you have read but rather a list of things you refer to in the text. Only refer to web based material if
%absolutely necessary. You should refer, where ever possible, to the original material in a textbook or academic
%journal.   A good report should have many references - showing that the subject has been widely researched.  The style
%to be used for the references is shown below.

\bibliographystyle{report}
\bibliography{C:/Users/Francis/Documents/Bibtex/Coding-Report-4}

\end{document}