\documentclass[]{article}

\usepackage{graphicx}

\usepackage[margin=1in]{geometry}

\setlength\parindent{0pt}

\usepackage{physics}
\usepackage{amsmath, amsfonts, amssymb, amsthm}

\usepackage{listings}

\usepackage{enumitem}
\renewcommand{\theenumi}{\alph{enumi}}
\renewcommand*{\thesection}{Problem \arabic{section}}
\renewcommand*{\thesubsection}{\alph{subsection})}
\renewcommand*{\thesubsubsection}{\quad \quad \roman{subsubsection})}

%Custom Commands
\newcommand{\Rel}{\mathcal{R}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\C}{\mathbb{C}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\Z}{\mathbb{Z}}
\newcommand{\Q}{\mathbb{Q}}

\newcommand{\toI}{\xrightarrow{\textsf{\tiny I}}}
\newcommand{\toS}{\xrightarrow{\textsf{\tiny S}}}
\newcommand{\toB}{\xrightarrow{\textsf{\tiny B}}}

\newcommand{\divisible}{ \ \vdots \ }
\newcommand{\st}{ \ \vline \ }

% Theorem Definition
\newtheorem{definition}{Definition}
\newtheorem{assumption}{Assumption}
\newtheorem{theorem}{Theorem}

%opening

\title{MATH 5301 Elementary Analysis - Midterm Exam}

\author{Jonas Wagner}

\date{2021, October 8}

\begin{document}

\maketitle

% Problem 1
\section{}
\textbf{Problem:} 100 soldiers stayed in a rank in front of the corporal.
The corporal ordered all of them to turn left, but the soldiers were newbies,
so they were not certain was it left from their perspective or from the corporal’s
point of view. So, some of them turned left and some turned right. After that
at every second if two neighboring soldiers find themselves facing each other,
they rotate by 180 degrees. Show that this process will not last forever.

\subsection*{Problem Formulation}
The state of the 100 soldiers represented as arrows pointed to the right or left
for each time step, $n$, is visualized by the following drawing:\\
\begin{figure}[h]
    \centering
    \includegraphics[width=0.7\textwidth]{MidExam1_pblm1_a.png}
\end{figure}

For each time step all of the individual pairs of soldiers facing each other
flip and it progresses until all soldiers on the left face left and soldiers
on the right face right.\\

This can be similarly described as a sequence of binary states and a simplistic update
operation.\\
Let $1$ denote $\rightarrow$.\\
Let $0$ denote $\leftarrow$.\\
Define and randomly populate sequence
$$\qty{x_n \st x_n \in \{0,1\} }_{n \in [1,100]}^{(k)}, k \in \N$$
The dependent update operation can be defined as
$$\qty{x_n}^{(k+1)}
    := \qty{x_n \st x_n =
    \begin{cases}
        1   & (x_n \land x_{n+1}) \lor (\lnot x_n \land x_{n-1})\\
        0   & (\lnot x_n \land \lnot x_{n-1}) \lor (x_n \land \lnot x_{n+1})
    \end{cases}
    \
    }
$$
where $x_0 = 0$ and $x_{101} = 1$ get referenced.\\

\newpage
\subsection*{Stopping Demonstration}
An example demonstrating this progression on the left is shown in the following drawing:
\begin{figure*}[h]
    \centering
    \includegraphics[width = 0.7 \textwidth]{MidExam1_pblm1_b.png}
\end{figure*}

It is clear that after each update, the system will begin to converge so that
$$\exists_{a \in [1,100]} : \forall_{i < a} x_i = 0$$ and
$$\exists_{b \in [1,100]} : \forall_{i > b} x_i = 1$$

Therefore, and since $\qty{x_n}^{(k)}$ is a finite set, the following must be true:
$$\exists_{n \in \N} \forall_{k > n} \implies \qty{X_n}^{(k)} = \qty{X_n}^{(k+1)}$$

Which means that the dependent operation will no longer change the state of the system.

% Problem 2
\newpage
\section{}
Let $\R^\infty$ be the set of all sequences of real numbers
$$\R^\infty = \qty{a_1,a_2,\cdots \ | \ a_j \in \R}$$
Define the relation $\Rel$ on $\R^\infty$ as follows:
$a\Rel b$ if for some $j \in \N : a_j > b_j$ and for all $k < j \implies a_k = b_k$.

Prove that such a relation is an order relation on $R^\infty$. Is it a total order?

\begin{definition}
    Define the relation $\Rel$ as
    $$\Rel \subset \R^\infty \cross \R^\infty \qty{(a,b) \st
        \exists_{j\in\N} : \qty(\forall_{k < j} \ a_k = b_k) \land (a_j > b_j)}$$
\end{definition}

\begin{theorem}
    The relation $a \Rel b$ is a strict total order relation over $\R^\infty$
    \begin{proof}
        Over $\R^\infty$ the ordered relation $a \Rel_1 b$
        satisfies the 3 strict order properties:

        \subsubsection{Irreflective: $\lnot x \Rel x$}
        \begin{align*}
            \exists_{j\in\N} : \qty(\forall_{k < j} \ a_k = b_k) \land (a_j > b_j) &\implies a \Rel b\\
            \lnot  \exists_{j\in\N} : \qty(\forall_{k < j} \ x_k = x_k) \land (x_j > x_j) &\implies \lnot  (x \Rel x)\\
            \forall_{j\in\N} \qty(\lnot \qty(\forall_{k < j} \ x_k = x_k) \lor \lnot (x_j > x_j)) &\implies \lnot  (x \Rel x)\\
            \forall_{j\in\N} \qty(\qty(\exists_{k < j} \ x_k \neq x_k) \lor (x_j \geq x_j)) &\implies \lnot  (x \Rel x)\\
        \end{align*}
        Since $(x_j \geq x_j)$ is always true, $\lnot x \Rel x$.

        \subsubsection{Transivity: $x \Rel y \land y \Rel z \implies x \Rel z$}
        \begin{align*}
            x \Rel y \land y \Rel z
                &\implies x \Rel z\\
        \exists_{j\in\N} : \qty(\forall_{k < j} \ x_k = y_k) \land (x_j > y_j)\land&\\
            \land \exists_{j\in\N} : \qty(\forall_{k < j} \ y_k = z_k) \land (y_j > z_j)
            &\implies \exists_{j\in\N} : \qty(\forall_{k < j} \ x_k = z_k) \land (x_j >z_j)\\
        \exists_{j_1\in\N} : \qty(\forall_{k < j_1} \ x_k = y_k) \land (x_{j_1} > y_{j_1})\land&\\
            \land \exists_{j_2\in\N} : \qty(\forall_{k < j_2} \ y_k = z_k) \land (y_{j_2} > z_{j_2})
            &\implies \exists_{j_3\in\N} : \qty(\forall_{k < j_3} \ x_k = z_k) \land (x_{j_3} >z_{j_3})\\
        \end{align*}
        Clearly, for $j_3 \leq \min\qty{j_1,j_2}$ this is always true.

        \subsubsection{Connectivity: $x \neq y \implies x \Rel y \lor y \Rel x$}
        \begin{align*}
            x \neq y &\implies x \Rel y \lor y \Rel x\\
            x \neq y &\implies \qty(\exists_{j\in\N} : \qty(\forall_{k < j} \ x_k = y_k) \land (x_j > y_j))
                \lor \qty(\exists_{j\in\N} : \qty(\forall_{k < j} \ y_k = x_k) \land (y_j > x_j))\\
            \lnot(x \neq y)
                &\implies \lnot \qty(\qty(\exists_{j\in\N} : \qty(\forall_{k < j} \ x_k = y_k) \land (x_j > y_j))
                \lor \qty(\exists_{j\in\N} : \qty(\forall_{k < j} \ y_k = x_k) \land (y_j > x_j)))\\
            x = y &\implies \lnot \qty(\exists_{j\in\N} : \qty(\forall_{k < j} \ x_k = y_k) \land (x_j > y_j))
                \land \lnot \qty(\exists_{j\in\N} : \qty(\forall_{k < j} \ y_k = x_k) \land (y_j > x_j)))\\
            x = y &\implies \qty(\forall_{j\in\N} : \lnot \qty(\forall_{k < j} \ x_k = y_k) \lor \lnot (x_j > y_j))
                \land \qty(\forall_{j\in\N} : \lnot \qty(\forall_{k < j} \ y_k = x_k) \lor \lnot (y_j > x_j)))\\
            x = y &\implies \qty(\forall_{j\in\N} : \qty(\exists_{k < j} \ x_k \neq y_k) \lor (x_j \geq y_j))
                \land \qty(\forall_{j\in\N} : \qty(\exists_{k < j} \ y_k \neq x_k) \lor (y_j \geq x_j)))\\
            x = y &\implies (x_j \geq y_j) \land (y_j \geq x_j)\\
        \end{align*}
        Therefore, $\Rel$ has connectivity.
    \end{proof}
\end{theorem}

% Problem 3
\newpage
\section{}
Alice wrote some finite sequence of zeros and ones on the paper (e.g. 010010).
Bob is allowed to replace any pair ``10'' by ``00···01'' with any (but finite)
amount of zeros in front of 1. Bob can repeat this procedure as many times as
he wants (if he will find ``10'' in the resulting sequence).
Prove that Bob can perform such operation only finitely many times.

\textbf{Solution:}
\begin{figure*}[h]
    \centering
    \includegraphics[width=\textwidth]{MidExam1_pblm3_e.png}
\end{figure*}

The proof of this follows a similar structure to that of problem 1.

% Problem 4
\newpage
\section{}
Present two essentially different total orderings of the field
$\Q(\sqrt{2}) := \qty{a + b \sqrt{2} \st a,b \in \Q}$.
\subsection*{Problem Formulation}
\begin{definition}
    The field $\mathbb{F} = \langle\Q(\sqrt{2}),+,0,\cdot,1\rangle$ is defined with the set
    $$\Q(\sqrt{2}) := \qty{a + b \sqrt{2} \st a,b \in \Q}$$
    and the operators
    \begin{align*}
        + : \Q(\sqrt{2}) \cross \Q(\sqrt{2}) \to \Q(\sqrt{2})
            &:= (a_1,a_2) + (b_1,b_2) = (a_1 + b_1, a_2 + b_2)\\
        \cdot : \Q(\sqrt{2}) \cross \Q(\sqrt{2}) \to \Q(\sqrt{2})
            &:= (a_1,a_2) \cdot (b_1,b_2) = (a_1 b_1 + 2 a_2 b_2, a_1 b_2 + a_2 b_1)
    \end{align*}
    It is also assumed that the standard field properties all apply.
\end{definition}

% Part 1
\subsection{Ordering 1: $\Rel_1$}
\begin{definition}
    An relation $a \Rel_1 b$ %$(a,b) \Rel_1 (b_1,b_2)$ can be defined as
    $$\Rel \subset \Q(\sqrt{2}) \cross \Q(\sqrt{2})
        := \qty{(a,b) \st a_1^2 + 2 a_2^2 \leq b_1 + 2 b_2^2}$$
\end{definition}
\begin{theorem}
    The relation $a \Rel_1 b$ is an ordered relation over $\mathbb{F}$
    \begin{proof}
        Over $\mathbb{F}$ the ordered relation $(a_1,a_2) \Rel_1 (b_1,b_2)$ can be defined by
        $$(a, b) \Rel_1 (b_1,b_2)
            := \qty{((a_1,a_2),(b_1,b_2)) \st (a_1 \cdot a_1 + 2 \cdot a_2 \cdot a_2)
                \leq (b_1 \cdot b_1 + 2 \cdot b_2 \cdot b_2)}
        $$
        $(a_1, a_2) \Rel_1 (b_1,b_2)$ satisfies the 3 ordered properties:
        \subsubsection{Reflective: $x \Rel_1 x$}
        \begin{align*}
            (a_1 \cdot a_1 + 2 \cdot a_2 \cdot a_2)
                \leq (b_1 \cdot b_1 + 2 \cdot b_2 \cdot b_2)
                &\implies a \Rel_1 b\\
            (x_1 \cdot x_1 + 2 \cdot x_2 \cdot x_2)
                \leq (x_1 \cdot x_1 + 2 \cdot x_2 \cdot x_2)
                &\implies x \Rel_1 x\\
            x_1^2 + 2 x_2^2 \leq x_1^2 + 2 x_2^2 &\implies x \Rel_1 x
        \end{align*}

        \subsubsection{Anti-Symmetry: $x \Rel_1 y \land y \Rel_1 x \implies x = y$}
        \begin{align*}
            x \Rel_1 y \land y \Rel_1 x &\implies x = y\\
            (x_1,x_2) \Rel_1 (y_1,y_2) \land (y_1,y_2) \Rel_1 (x_1,x_2)
                &\implies (x_1,x_2)=(y_1,y_2)\\
            \qty((x_1 \cdot x_1 + 2 \cdot x_2 \cdot x_2)
                    \leq (y_1 \cdot y_1 + 2 \cdot y_2 \cdot y_2))
            &\land\\
                \land \qty((y_1 \cdot y_1 + 2 \cdot y_2 \cdot y_2)
                    \leq (x_1 \cdot x_1 + 2 \cdot x_2 \cdot x_2))
                &\implies (x_1,x_2) = (y_1,y_2)\\
            \qty(x_1^2 + 2 x_2^2 \leq y_1^2 + 2 y_2^2)
                \land \qty(y_1^2 + 2 y_2^2 \leq x_1^2 + 2 x_2^2)
                &\implies (x_1,x_2) = (y_1,y_2)\\
            % (x \cdot x \leq y \cdot y) \land (y \cdot y \leq x \cdot x)
            %     &\implies x = y
        \end{align*}

        \subsubsection{Transivity: $x \Rel_1 y \land y \Rel_1 z \implies x \Rel_1 z$}
        \begin{align*}
            x \Rel_1 y \land y \Rel_1 z
                &\implies x \Rel_1 z\\
            (x_1,x_2) \Rel_1 (y_1,y_2) \land (y_1,y_2) \Rel_1 (z_1,z_2)
                &\implies (x_1,x_2) \Rel_1 (z_1,z_2)\\
            \qty((x_1 \cdot x_1 + 2 \cdot x_2 \cdot x_2)
                    \leq (y_1 \cdot y_1 + 2 \cdot y_2 \cdot y_2))
                \land&\\
                \land \qty((y_1 \cdot y_1 + 2 \cdot y_2 \cdot y_2)
                    \leq (z_1 \cdot z_1 + 2 \cdot z_2 \cdot z_2))
                &\implies \qty((x_1 \cdot x_1 + 2 \cdot x_2 \cdot x_2)
                    \leq (z_1 \cdot z_1 + 2 \cdot z_2 \cdot z_2))\\
            \qty(x_1^2 + 2 x_2^2 \leq y_1^2 + 2 y_2^2)
                    \land \qty(y_1^2 + 2 y_2^2 \leq z_1^2 + 2 z_2^2)
                &\implies \qty(x_1^2 + 2 x_2^2 \leq z_1^2 + 2 z_2^2)\\
            x_1^2 + 2 x_2^2 \leq y_1^2 + 2 y_1^2 \leq z_1^2 + 2 z_2^2
                &\implies x \Rel_1 z
        \end{align*}
    \end{proof}
\end{theorem}
\begin{theorem}
    The ordered relation $x \Rel_1 y$ forms a total order over $\mathbb{F}$.
    \begin{proof}
        $x \Rel_1 y$ satisfies the totality condition:
        \subsubsection{Totality:
        $\forall x,y \in \mathbb{F} \implies x \Rel_1 y \lor y \Rel_1 x$}
        \begin{align*}
            \forall x,y \in \mathbb{F} &\implies x \Rel_1 y \lor y \Rel_1 x\\
            \forall_{x \in \Q(\sqrt{2})}
                    \forall_{y \in \Q(\sqrt{2})}
                &\implies \qty(x \cdot x \leq y \cdot y)
                    \lor \qty(y \cdot y \leq x \cdot x)\\
            \forall_{(x_1,x_2) \in \Q(\sqrt{2}))}
                    \forall_{(y_1,y_2) \in \Q(\sqrt{2})}
                &\implies \qty((x_1 \cdot x_1 + 2 \cdot x_2 \cdot x_2)
                    \leq (y_1 \cdot y_1 + 2 \cdot y_2 \cdot y_2)) \lor\\
                &\qquad \lor \qty((y_1 \cdot y_1 + 2 \cdot y_2 \cdot y_2)
                    \leq (x_1 \cdot x_1 + 2 \cdot x_2 \cdot x_2))\\
            % \forall_{x_1 \in \N} \forall_{x_2\in \N}
            %         \forall_{y_1 \in \N} \forall_{y_2 \in \N}
            \forall_{x_1, x_2, y_1, y_2 \in \Q}
                &\qty(\qty(x_1^2 + 2 x_2^2 \leq y_1^2 + 2 y_2^2)
                    \lor \qty(y_1^2 + 2 y_2^2 \leq x_1^2 + 2 x_2^2))\\
            % \forall_{x_1, x_2, y_1, y_2 \in \Q(\sqrt{2})}
            %     &\qty(x_1^2 + 2 x_2^2 \leq y_1^2 + 2 y_2^2)
            %         \lor \qty(y_1^2 + 2 y_2^2 \leq x_1^2 + 2 x_2^2)
        \end{align*}
    \end{proof}
\end{theorem}

% Part 2
\subsection{Ordering 2: $\Rel_2$}
\begin{definition}
    An relation $a \Rel_2 b$
    $$\Rel \subset \Q(\sqrt{2}) \cross \Q(\sqrt{2})
        := \qty{(a,b) \st a \cdot a \leq b \cdot b}$$
\end{definition}

% not done yet
\begin{theorem}
    The relation $a \Rel_2 b$ is an ordered relation over $\mathbb{F}$
    \begin{proof}
        Over $\mathbb{F}$ the ordered relation $(a_1,a_2) \Rel_2 (b_1,b_2)$ can be defined by
        $$(a, b) \Rel_2 (b_1,b_2)
            := \qty{((a_1,a_2),(b_1,b_2)) \st (a_1 \cdot a_1 + \cdot a_2 \cdot a_2)
                \leq (b_1 \cdot b_1 + \cdot b_2 \cdot b_2)}
        $$
        $(a_1, a_2) \Rel_2 (b_1,b_2)$ satisfies the 3 ordered properties:
        \subsubsection{Reflective: $x \Rel_2 x$}
        \begin{align*}
            (a_1 \cdot a_1 + 2 \cdot a_2 \cdot a_2)
                \leq (b_1 \cdot b_1 + \cdot b_2 \cdot b_2)
                &\implies a \Rel_2 b\\
            (x_1 \cdot x_1 + 2 \cdot x_2 \cdot x_2)
                \leq (x_1 \cdot x_1 + \cdot x_2 \cdot x_2)
                &\implies x \Rel_2 x\\
            x_1^2 + x_2^2 \leq x_1^2 + x_2^2 &\implies x \Rel_2 x
        \end{align*}

        \subsubsection{Anti-Symmetry: $x \Rel_2 y \land y \Rel_2 x$}
        \begin{align*}
            x \Rel_2 y \land y \Rel_2 x &\implies x = y\\
            (x_1,x_2) \Rel_2 (y_1,y_2) \land (y_1,y_2) \Rel_2 (x_1,x_2)
                &\implies (x_1,x_2)=(y_1,y_2)\\
            \qty((x_1 \cdot x_1 + 2 \cdot x_2 \cdot x_2)
                    \leq (y_1 \cdot y_1 + \cdot y_2 \cdot y_2))
            &\land\\
                \land \qty((y_1 \cdot y_1 + \cdot y_2 \cdot y_2)
                    \leq (x_1 \cdot x_1 + \cdot x_2 \cdot x_2))
                &\implies (x_1,x_2) = (y_1,y_2)\\
            \qty(x_1^2 + x_2^2 \leq y_1^2 + y_2^2)
                \land \qty(y_1^2 + y_2^2 \leq x_1^2 + x_2^2)
                &\implies (x_1,x_2) = (y_1,y_2)\\
            % (x \cdot x \leq y \cdot y) \land (y \cdot y \leq x \cdot x)
            %     &\implies x = y
        \end{align*}

        \subsubsection{Transivity: $x \Rel_2 y \land y \Rel_2 z \implies x \Rel_2 z$}
        \begin{align*}
            x \Rel_2 y \land y \Rel_2 z
                &\implies x \Rel_2 z\\
            (x_1,x_2) \Rel_2 (y_1,y_2) \land (y_1,y_2) \Rel_2 (z_1,z_2)
                &\implies (x_1,x_2) \Rel_2 (z_1,z_2)\\
            \qty((x_1 \cdot x_1 + 2 \cdot x_2 \cdot x_2)
                    \leq (y_1 \cdot y_1 + \cdot y_2 \cdot y_2))
                \land&\\
                \land \qty((y_1 \cdot y_1 + \cdot y_2 \cdot y_2)
                    \leq (z_1 \cdot z_1 + \cdot z_2 \cdot z_2))
                &\implies \qty((x_1 \cdot x_1 + \cdot x_2 \cdot x_2)
                    \leq (z_1 \cdot z_1 + \cdot z_2 \cdot z_2))\\
            \qty(x_1^2 + 2 x_2^2 \leq y_1^2 + 2 y_2^2)
                    \land \qty(y_1^2 + y_2^2 \leq z_1^2 + z_2^2)
                &\implies \qty(x_1^2 + x_2^2 \leq z_1^2 + z_2^2)\\
            x_1^2 + x_2^2 \leq y_1^2 + y_1^2 \leq z_1^2 + z_2^2
                &\implies x \cdot x \leq y \cdot y \leq z \cdot z
        \end{align*}
    \end{proof}
\end{theorem}

\begin{theorem}
    The ordered relation $x \Rel_1 y$ forms a total order over $\mathbb{F}$.
    \begin{proof}
        $x \Rel_2 y$ satisfies the totality condition:
        \subsubsection{Totality:
        $\forall x,y \in \mathbb{F} \implies x \Rel_2 y \lor y \Rel_2 x$}
        \begin{align*}
            \forall x,y \in \mathbb{F} &\implies x \Rel_2 y \lor y \Rel_2 x\\
            \forall_{x \in \Q(\sqrt{2})}
                    \forall_{y \in \Q(\sqrt{2})}
                &\implies \qty(x \cdot x \leq y \cdot y)
                    \lor \qty(y \cdot y \leq x \cdot x)\\
            \forall_{(x_1,x_2) \in \Q(\sqrt{2}))}
                    \forall_{(y_1,y_2) \in \Q(\sqrt{2})}
                &\implies \qty((x_1 \cdot x_1 + \cdot x_2 \cdot x_2)
                    \leq (y_1 \cdot y_1 + \cdot y_2 \cdot y_2)) \lor\\
                &\qquad \lor \qty((y_1 \cdot y_1 + \cdot y_2 \cdot y_2)
                    \leq (x_1 \cdot x_1 + \cdot x_2 \cdot x_2))\\
            % \forall_{x_1 \in \N} \forall_{x_2\in \N}
            %         \forall_{y_1 \in \N} \forall_{y_2 \in \N}
            \forall_{x_1, x_2, y_1, y_2 \in \N}
                &\qty(\qty(x_1^2 + x_2^2 \leq y_1^2 + y_2^2)
                    \lor \qty(y_1^2 + y_2^2 \leq x_1^2 + x_2^2))\\
            % \forall_{x_1, x_2, y_1, y_2 \in \Q(\sqrt{2})}
            %     &\qty(x_1^2 + 2 x_2^2 \leq y_1^2 + 2 y_2^2)
            %         \lor \qty(y_1^2 + 2 y_2^2 \leq x_1^2 + 2 x_2^2)
        \end{align*}
    \end{proof}
\end{theorem}

% Problem 5
\newpage
\section{}
Infinitely many wizards $W_1, W_2,\dots$ stay in the line.
Each wizard wears a hat of one the three colors: Red, Yellow or Green.
Every wizard $W_n$ can see the hats of all the next wizards in line, $W_{n+1}, W_{n+2}, \dots$,
Starting  with  the  wizard $W_1$ every one has to guess the color of his own hat.
If the wizard guesses correctly, he can go free. Otherwise he got dematerialized.
Wizards  discussed  their  strategy  before  this  event.
Show that if the wizards were smart enough, then only finitely many of them will disappear.

\subsection*{Assumptions}
\begin{assumption}\label{asmp:selfless}
    It is assumed that the Wizards are not only all smart, but also selfless to attempt to
    save the most number of wizards instead of just themselves.
\end{assumption}
\begin{assumption}
    Although it is not stated in the problem, it is assumed that the Wizards
    are unable to directly communicate the color of the hats to someone else,
    but it is also assumed that an infinite memory of past guesses exists.
\end{assumption}

\subsection*{Sequence Definitions}
\begin{definition}
    Let the sequence $\qty{W_n}$ be defined as
    $$\qty{W_n} := \qty{W_n \st W_n \in \{1,2,3\}}$$
    with index $n$ related to each of the wizards in order and
    $1,2,3$ represent `Red', `Yellow', and `Green' respectively.
\end{definition}
\begin{definition}
    Let the updating sequence $\qty{G_n}$ be defined as the guess that each Wizard makes.
\end{definition}
\begin{definition}
    An updating sequence $\qty{Q_n}$ is defined as a queue of causal memory that each wizard
    maintains according to the same set of rules.
\end{definition}
\subsection*{Decision Procedure}
The procedure described is one of many potential decision criterion that can be used to
make guesses and minimize the number of incorrect hat color guesses. The basic idea
is that following the initialization of the procedure, each Wizard will maintain their own
queue based on what previous Wizards guessed and what they perceive from the following
Wizards hat colors. This queue will be long enough that when a wizard is reached it should
know what color their own hat is along with future hat colors. They then make a decision based
on the queue and the wizard following the end of the queue as to what hat to guess in order
to either share information to the future Wizards or to save themselves. The procedure is
designed so that repeated colors are used to expand the length of the queue and allow
for wizards to use the information to save themselves without creating a deficit.
By Assumption , the decision will always be according to the procedure
that will do the best for everyone and not just save themselves.

\newpage
The following pseudo-code outlines the decision procedure that all the wizards will use.
Let $N=\infty$, time index $n$, queue length $k$, and arbitrarily set minimum queue length $K = 20$.
\begin{lstlisting}
% Initialization
for i = 5:N % min start is 5
    if W(i+1) == W(i) % First set of same color hat
        k = i;
        break
    end
end
% Initialize Queue
for n = 1:k
    G(n) = W(n + k);
    Q(n + k) = G(n);
end
% Standard Decision Procedure
while n < N
    if Q(n) == Q(n+1)
        G(n) = Q(n);
    else if k > K && G(n) \lnot = W(n+k)
        G(n) = Q(n);
    else
        G(n) = W(n + k)
        Q(n + k) = G(n);
    end
    while W(n + k) == W(n + k + 1)
        Q(n + k + 1) = W(n + k + 1);
        k = k + 1;
    end
    n = n + 1;
end
\end{lstlisting}
% look at that else if statement again... and make sure it'll still work

\subsection*{Effectiveness of Procedure}
A proof of Effectiveness is probably easily demonstrated through mathematical induction.
Essentially, the finite loss of Wizards can be demonstrated using the size of $k$
being bounded from below and increasing regardless of the color of the next hat.
Although I'm not proving this method is the optimal way to minimize the loss of wizards,
it is clear that the expanding queue-based method will eventually, within a finite number of
iterations, ensure that the queue is large and robust enough to prevent the continued
dematerialization of wizards.

% Problem 6
\newpage
\section{}
Show that $A$ is an infinite set if and only if $A$ has a proper subset
$B \ : \ (B \subsetneq A)$ with the same cardinality $(\abs{A} = \abs{B})$.\\

\begin{theorem}
    $$\abs{A} \geq \aleph_0 \iff \exists {B\subsetneq A} : \abs{A} = \abs{B}$$
    \begin{proof}
        Proof by contradition.\\
        Assume $\abs{A} < \aleph_0$,
        \begin{align*}
            \abs{A} < \aleph_0 &\iff \exists {B\subsetneq A} : \abs{A} = \abs{B}\\
            &\iff \exists {B\subsetneq A} : \abs{A} = \abs{B}\\
        \end{align*}
    \end{proof}
\end{theorem}

\includegraphics[width=\textwidth]{MidExam1_pblm6.png}

\end{document}


\chapter{Kalman Filter}
\label{chp:kalman}

The Kalman filter algorithm for localization is a spacial case of Markov localization already described in chapter \ref{chp:local}.
The Kalman filter is different in the sense that instead of calculating the probability discrete for every tile on the map, the position is modelled with a continuous Gaussian distribution.
A Gaussian is a well known and often used distribution in probability theory.
The Gaussian function is shown in equation \ref{eq:Gaussian}, and is defined by two values, the mean \emph{$\mu$} that describe the middle of the distribution, and the variance \emph{$\sigma^2$} that describe spread of the distribution.

\begin{equation}
\label{eq:Gaussian}
f(x) = \frac{1}{\sqrt{2*\pi*\sigma^2}} * exp^{-\frac{1}{2}*\frac{(x-\mu)^2}{\sigma^2}}
\end{equation}

When having a multivariate Gaussian distribution in more dimensions the mean becomes a vector \emph{$\vec{\mu}$}, and the variance becomes a covariance matrix \emph{$\Sigma$} defining the spread in all dimensions.
Figure \ref{fig:Gaussian} show a multivariate Gaussian distribution, this could be an example of the probability of a robot being in different positions in a x,y plane.

\myFigure{Theory/KalmanFilter/Gaussian}{Multivariate Gaussian distribution in two dimensions.}{fig:Gaussian}{0.6}

The Kalman filter is based on a state space model, example seen on figure \fref{fig:StateSpaceModel}, that is used to describe and predict the behaviour of linear systems.
The state space model describe how a system transform some input/movement \emph{$\vec{u}$} to a hidden state \emph{$\vec{x}$}, and generate an output/measurement \emph{$\vec{y}$}, with the use of matrices. The most impotent is the input matrix \emph{$G$} that transform the input to the hidden state, the state transmission matrix \emph{$F$} that predict the next hidden state from the present and the output matrix \emph{$H$} that transform the hidden state to the output. Noise is added on both input and output of the system.

An example of a state space model could be a robot that apply an acceleration as input, measure a noisy position, and tries to estimate its velocity and true position.

\myFigure{Theory/KalmanFilter/StateSpaceModel}{An example of a state space model, that is used describe linear system with a set of matrices. This model describe how a system transform some input \emph{$\vec{u}$} to a hidden state \emph{$\vec{x}$}, and generate an output \emph{$\vec{y}$}.}{fig:StateSpaceModel}{0.8}

\pagebreak

Just as the Markov localization the Kalman filter is separated in two steps.
The prediction step is trying to predict the robots next position based on some movement, and the update step is estimating the robot position based on the prediction and a number of measurements.
The system continuously switch between predicting and updating to localize the robot.

Equation \ref{eq:KalmanPrediction1} and \ref{eq:KalmanPrediction2} show the Kalman filter prediction step.
The basic principle of the prediction step is to move the Gaussian distribution of the estimated position to a new location based on some movement.
There will always be noise in the movement procedure, this mean the Gaussian distribution of the hidden state will always have a bigger spread after movement that before, do to the loss of information.
The first equation \ref{eq:KalmanPrediction1} is predicting the hidden state \emph{$\vec{x'}$} from the previous state and the movement \emph{$\vec{u}$}.
The second equation \ref{eq:KalmanPrediction2} is predicting the covariance matrix of the hidden state from the previous covariance matrix and the system noise from the movement.

\begin{equation}
\label{eq:KalmanPrediction1}
\vec{x}'_{k} = \boldsymbol{F} * \vec{x}_{k-1} + \boldsymbol{G} * \vec{u}_{k}
\end{equation}

\begin{equation}
\label{eq:KalmanPrediction2}
\boldsymbol{P}'_{k} = \boldsymbol{F} * \boldsymbol{P}_{k-1} * \boldsymbol{F}^T + \boldsymbol{G} * \boldsymbol{Q} * \boldsymbol{G}^T
\end{equation}

Equation \ref{eq:KalmanUpdate1} to \ref{eq:KalmanUpdate4} show the Kalman filter update step.
The basic principle of the update step is to combine one or more Gaussian distributions from the state estimate and sensor measurements, to get a new more precise estimate.
When combining two Gaussian distributions, the result distribution will always have a smaller spread than any of the two original distributions, this is do to information gain in the system.
The first equation \ref{eq:KalmanUpdate1} is calculating the error $ \varepsilon_{k} $ of the predicted state and the true measurement.
Equation \ref{eq:KalmanUpdate2} is the Kalman gain $ \boldsymbol{G}_{k} $, this is a factor determining how big impact the measurement has on the state prediction, depending on the ratio of the state covariance matrix $\boldsymbol{P}'_{k}$ and the sensor noise $\boldsymbol{R}$.
In case of relative small sensor noise, meaning that the measurements are accurate, the state estimate depends mostly on the measurements, and in case of relative high sensor noise, meaning that the measurements are inaccurate, the state estimate instead depends mostly on the previous state.
In the end equation \ref{eq:KalmanUpdate3} and \ref{eq:KalmanUpdate4} calculate the new state estimate and covariance matrix, based on the sensor error, Kalman gain and predicted covariance matrix.

\begin{equation}
\label{eq:KalmanUpdate1}
\varepsilon_{k} = \vec{z}_{k} - \boldsymbol{H} * \vec{x}'_{k}
\end{equation}

\begin{equation}
\label{eq:KalmanUpdate2}
\boldsymbol{G}_{k} = \boldsymbol{P}'_{k} * \boldsymbol{H}^T * (\boldsymbol{H} * \boldsymbol{P}'_{k} * \boldsymbol{H}^T + \boldsymbol{R})^-1
\end{equation}

\begin{equation}
\label{eq:KalmanUpdate3}
\vec{x}_{k} = \vec{x}'_{k} + \boldsymbol{G}_{k} * \varepsilon_{k}
\end{equation}

\begin{equation}
\label{eq:KalmanUpdate4}
\boldsymbol{P}_{k} = (\boldsymbol{I} - \boldsymbol{K}_{k} * \boldsymbol{H}) * \boldsymbol{P}'_{k}
\end{equation}

The Kalman filter is a very useful tool in the field of localization and autonomous robots.
It is often used to estimate characteristics in the surrounding environment of a robot, as predicting the speed of another car, without being able to measure speed directly.

The Kalman filter have some drawbacks though.
As the state estimate is modelled with a Gaussian distributions, with only one peak, is it not possible to model a scenario where there is a probability of the robot being in two different positions.
The system also have to be linear to be able to make the state space model that is necessary for the Kalman filter to work.
The term Extended Kalman filter originates from this problem, in the sense that Extended Kalman filters is about ways of transforming nonlinear systems to linear systems so that Kalman filters can be used.


\chapter{Dynamics Modelling}

In this chapter, we
present the mathematical forumulation of the orbit and attitude dynamcis models in GMAT.
One of the fundamental capabilities of GMAT is to model the motion
of spacecraft in different flight regimes.  The flight regime,
such as low Earth, libration point, or lunar, are determined by the
forces and perturbations that dominate the dynamics.
The chapter begins with an overview of
the orbital equations of motion and their variational equations.  Next, we
discuss the formulation for orbital perturbations and the equations used to model
spacecraft thrust.  The second half of the chapter is devoted to attitude modeling and
we present the specifications for  attitude conversions and kinematic attitude models.

\section{Orbit Dynamics}

\subsection{Orbital Equations of Motion}

The orbital equations of motion come from an application of Newton's laws of motion to a spacecraft in orbit.  From Newton's Second Law we know that
%
\begin{equation}
     \frac{d (m\mathbf{v})}{d t} = \sum F_{ext}
\end{equation}
%
where $m$ is the total mass of the spacecraft, $\mathbf{r}$ is the position vector of the spacecraft, $t$ is time, and the right hand side of the equation represents the total sum of external forces.  Solving for the acceleration gives us the
second order differential equation
%
\begin{equation}
   \frac{d^2 \mathbf{r}}{d^2 t} = \sum \frac{F_{ext}}{m} - \frac{\dot{m}}{m}\frac{\partial \mathbf{r}}{\partial t}
   \label{Eq:SCAcc}
\end{equation}
%

The terms included in the RHS of the equations of motion can be selected by the user, and the form of several terms (and whether they appear at all) are dependent upon the coordinate system of integration.    If we include
all of the possible forces GMAT can model in the summation on the
RHS of Eq.~(\ref{Eq:SCAcc}), and we assume the origin of integration is a celestial body (the barycentric form is slightly different), then the orbital equations of motion are
%
\begin{equation} \begin{split}
    \frac{d^2\mathbf{r}}{dt^2}
    =  &-\frac{\mu}{r^3}\mathbf{r} +  \nabla \phi_{sj}^o +
    %
    G\sum_{\stackrel{k=1}{k \neq j}}^{n_b}m_k \left(\frac{\mathbf{r}_{ks}}{r_{ks}^3} -
     \frac{\mathbf{r}_{kj} }{r_{kj}^3}   \right)
     %
    + %\sum_{\stackrel{k=1}{k \neq j}}^{n_b}\left( \nabla
    %\phi_{ks}^o +
     %\nabla\phi_{kj}^o
     %\right)+
     \frac{\dot{m}_s }{m}\frac{d\mathbf{r}}{dt}- \frac{1}{2}\rho v_{rel}^2 \frac{C_d A}{m_s}\hat{\mathbf{v}}_{rel}
     +\frac{   P_{SR}C_R A_{\odot}   }{m_s}\hat{\mathbf{r}}_{s\odot} + \\ &
         \frac{\mu}{c^2 r^3}\left(  \left( 4\frac{\mu}{r} - v^2\right)\mathbf{r} +   4(\mathbf{r}\cdot\mathbf{v})\mathbf{v}\right) + 2 (\boldsymbol{\Omega} \times \mathbf{v}) + 2 \frac{\mu}{c^2 r^3}
    \left( \frac{3}{r^2}(\mathbf{r} \times \mathbf{v})(\mathbf{r} \cdot \mathbf{J}) +(\mathbf{v}\times\mathbf{J}) \right)
     \label{Eq:CompleteEOM}
     \end{split}
\end{equation}
%
Table \ref{Eq:AllForces} below describes each force in the equation above.
%
\begin{table}[h]
\centering \caption{ Force Models Available in GMAT } \label{Eq:AllForces}
\begin{tabular}{p{2.0 in} p{2.75 in} }
  \hline\hline
  % after \\: \hline or \cline{col1-col2} \cline{col3-col4} ...
  Description & Term \\
  \hline
  Central Body Point Mass & $ -\displaystyle\frac{\mu}{r^3}\mathbf{r}$ \\
  & \\
  Central Body Direct Nonspherical &  $\nabla \phi_{sj}^o$  \\
  & \\
  Direct Third Body Point Mass & $G\displaystyle\sum_{\stackrel{k=1}{k \neq j}}^{n_b}m_k \left(\frac{\mathbf{r}_{ks}}{r_{ks}^3}\right)$ \\
   & \\
  Indirect Third Body Point Mass &  $G\displaystyle\sum_{\stackrel{k=1}{k \neq j}}^{n_b}m_k \left( -
     \frac{\mathbf{r}_{kj} }{r_{kj}^3}   \right)$ \\
      & \\
   %Third Body Direct Nonspherical & $\displaystyle\sum_{\stackrel{k=1}{k \neq j}}^{n_b}\left( \nabla
   % \phi_{ks}^o\right)$  \\
   % &\\
   % Third Body Indirect Nonspherical &  $\sum_{\stackrel{k=1}{k \neq j}}^{n_b}\left(
   %  \nabla\phi_{kj}^o \right)$\\
   % & \\
   Spacecraft Thrust &$\displaystyle\frac{\dot{m}_s }{m}\frac{d\mathbf{r}}{dt}$\\
   &\\
   Atmospheric Drag & $- \displaystyle\frac{1}{2}\rho v_{rel}^2 \displaystyle\frac{C_d
   A}{m_s}\hat{\mathbf{v}}_{rel}$\\
   &\\
   Solar Radiation Pressure & $\displaystyle\frac{   P_{SR}C_R A_{\odot}   }{m_s}\hat{\mathbf{r}}_{s\odot}$\\
   &\\
   Schwarzschild solution & $\displaystyle\frac{\mu}{c^2 r^3}\left(  \left( 4\frac{\mu}{r} - v^2\right)\mathbf{r} +            4(\mathbf{r}\cdot\mathbf{v})\mathbf{v}\right) + 2 (\boldsymbol{\Omega} \times \mathbf{v})$\\
      &\\
   Geodesic Precession & $2 (\boldsymbol{\Omega} \times \mathbf{v}) $\\
         &\\
   Lense-Thirring Precession & $2 \displaystyle\frac{\mu}{c^2 r^3}
    \left( \frac{3}{r^2}(\mathbf{r} \times \mathbf{v})(\mathbf{r} \cdot \mathbf{J}) +(\mathbf{v}\times\mathbf{J}) \right)$\\
  \hline\hline
\end{tabular}
\end{table}
%
\subsection{Coordinate Systems for Integration of the Equations of Motion}

\subsection{Orbit Variational Equations and the State Transition Matrix}

Estimation and optimization problems require first derivatives of the solution
to the orbit final value problem with respect to orbital initial conditions.  Those
derivatives are provided by the orbit variational equations.  The variatial equations are
obtained by expanding the orbital equations of motion in a Taylor series and retaining
only the linear terms.
%
Assume the nonlinear dynamics have the following form
%
\begin{equation}
    \dot{\mathbf{x}} = \mathbf{f}(\mathbf{x},t)
\end{equation}
%
where
%
\begin{equation}
    \mathbf{x} = \left[\mbr^T \hspace{.2 in} \mbv^T  \right]^T = \left[x \hspace{.2 in} y \hspace{.2 in} z \hspace{.2 in} \dot{x} \hspace{.2 in}
    \dot{y} \hspace{.2 in} \dot{z}  \right]^T
\end{equation}
%
and
%
\begin{equation}
    \dot{\mathbf{x}} = \left[\dot{\mbr}^T \hspace{.2 in} \dot{\mbv}^T  \right]^T = \left[\dot{x} \hspace{.2 in}
    \dot{y}
    \hspace{.2 in} \dot{z} \hspace{.2 in} \ddot{x} \hspace{.2 in}
    \ddot{y} \hspace{.2 in} \ddot{z}  \right]^T
\end{equation}
%
Expanding the dynamics equations and retaining only the first order terms yields
%
\begin{equation}
    \dot{\mathbf{x}} \approx  \dot{\mathbf{x}}|_{ref} + \frac{\partial \mathbf{f}}
    {\partial \mathbf{x}}\left( \mathbf{x} - \mathbf{x}|_{ref} \right)\label{Eq:VarTaylorSeries}
\end{equation}
%
%
We can rewrite Eq.~(\ref{Eq:VarTaylorSeries}) by defining $\delta \mathbf{x} = \dot{\mathbf{x}} - \dot{\mathbf{x}}|_{ref}$ as follows.
%
\begin{equation}
    \delta \dot{\mathbf{x}} = \mathbf{A}\delta \mathbf{x}\label{Eq:LinearizedOEM}
\end{equation}
%
where
%
the Jacobian of the state equations is defined as the ``A" matrix as follows:
%
\begin{equation}
    \mathbf{A} = \frac{\partial \mathbf{f}}{\partial \mathbf{x}}
\end{equation}

From linear systems theory, the solution to Eq.~(\ref{Eq:LinearizedOEM}) has the form
%
\begin{equation}
     \delta \mathbf{x}(t_f) = \boldsymbol{\Phi}(t,t_o)\delta \mathbf{x}(t_o)
\end{equation}
%
where $\dot{\boldsymbol{\Phi}}$ is governed by the following set of 36 differential equations:
%
\begin{equation}
     \dot{\boldsymbol{\Phi}} = \mathbf{A}\boldsymbol{\Phi}
\end{equation}
%
GMAT simultaneously integrates the variational equations along with the non-linear state equations from $t_o$ to $t_f$ to obtain the State Transition Matrix (STM):
%
\begin{equation}
     \boldsymbol{\Phi}(t_o,t_f) = \int_{t_o}^{t_f}\mathbf{A}\boldsymbol{\Phi} dt \label{Eq:STMSolution}
\end{equation}
%
subject to the initial conditions
%
\begin{equation}
     \boldsymbol{\Phi}(t_o,t_o) = \mathbf{I}_{6\times6}
\end{equation}
%
To perform the integration in Eq.~(\ref{Eq:STMSolution}) we require the partial derivatives contained in the matrix $\mathbf{A}$:
%
\begin{equation}
     \mathbf{A} = \frac{\partial \dot{\mathbf{x}}}{\partial
     \mathbf{x}}=
     \left(\begin{array}{ccc}
              \displaystyle\frac{\partial \mathbf{v}}{\partial \mathbf{r}} & \displaystyle\frac{\partial \mathbf{v}}{\partial
              \mathbf{v}}\vspace{.1 in}\\
              %
              \displaystyle\frac{\partial \mathbf{a}}{\partial \mathbf{r}} & \displaystyle\frac{\partial \mathbf{a}}{\partial
              \mathbf{v}}
     \end{array}\right)
\end{equation}
%
Two of the derivatives are trivial:
%
\begin{equation}
     \displaystyle\frac{\partial \mathbf{v}}{\partial \mathbf{r}} = \mathbf{0}_{3x3}
\end{equation}
%
%
\begin{equation}
     \displaystyle\frac{\partial \mathbf{v}}{\partial \mathbf{v}} = \mathbf{I}_{3x3}
\end{equation}
%
The remaining two terms, $\partial\mathbf{a}/\partial\mathbf{r}$
and $\partial \mathbf{a}/\partial \mathbf{v}$, are dependent upon the specific perturbations included in the force model.  The partial derivatives of perturbations are provided in the sections containing the specific formulation of the perturbing force.

\subsection{Multiple Spacecraft Propgation and Coupled Propagation of the Equations of Motion}

\section{Force Modelling}

\subsection{$n$-Body Point Mass Gravity}

The gravitational perturbation due to $n$ point masses is well know.
However, we will derive the governing differential equation here, as
well as the componenents of the sensitivity matrix.
 Let's begin by defining some notation referring to
Fig.\ref{fig:NBody}. Assume the $j^{\mbox{th}}$ body is the central
body of the integration.
%
\begin{figure}[h!]
\centerline{
\begin{picture}(100,500)
\special{psfile= Images/NBodyDiagram.eps hoffset= -135 voffset= -45
hscale=85 vscale=85} \makebox(-20,585){$\hat{\mathbf{x}}_{I}$}
\makebox(270,700){$\hat{\mathbf{y}}_{I}$} \makebox(-330,770){$
\tilde{\mathbf{r}}_{s}$} \makebox(-330,877){$\mathbf{r}$}
\makebox(-270,900){$\mathbf{r}_{sk}$}
\makebox(-390,964){$\mathbf{r}_{k}$}
\makebox(-500,814){$\tilde{\mathbf{r}}_{j}$}
\makebox(-500,980){Central Body} \makebox(-320,995){$k^{th}$ Body}
\end{picture}}\vskip -4.0 in  \caption{ N-Body Illustration} \label{fig:NBody}
\end{figure}
%
%
\begin{itemize}
   %
   \item  $\tilde{\mathbf{r}}_s$ is the position of the spacecraft with respect
   a hypothesized inertial frame.
   %
   \item  $\tilde{\mathbf{r}}_j$ is the position of the central body with respect
   a hypothesized inertial frame.
   %
   \item  $\tilde{\mathbf{r}}_k$ is the position of the $k^{th}$ gravitational body with respect
   a hypothesized inertial frame.
   %
   \item  $\mathbf{r}$ is the position of the spacecraft with respect
   to the central body of integration ($j^{th}$ body).
   %
   \item  $\mathbf{r}_k$ is the position of the $k^{th}$ gravitational body with respect
   to the central body.
   %
\end{itemize}

We need the governing differential equation that describes the
motion of the spacecraft with respect to the central body.  However,
we know that we must apply Newton's 2nd Law in an inertial frame.
So, we begin by defining the relative position of the spacecraft
with respect to the central body.  From inspection of
Fig.\ref{fig:NBody} we see that
%
\begin{equation}
     \tilde{\mathbf{r}}_j +  \mathbf{r} = \tilde{\mathbf{r}}_s
\end{equation}
%
By reordering and taking the second derivative with respect to time
we obtain
%
\begin{equation}
     \ddot{\mathbf{r}} = \ddot{\tilde{\mathbf{r}}}_s - \ddot{\tilde{\mathbf{r}}}_j
     \label{Eq:SCRelativeODE}
\end{equation}
%
We can apply Newton's 2nd Law to the spacecraft and obtain
%
\begin{equation}
     m_s \ddot{\tilde{\mathbf{r}}}_s = \sum_{k=1}^n F_k =
     G\sum_{k=1}^n \frac{m_s m_k}{\| \mathbf{r}_{k} - \mathbf{r}\|^3} \left(\mathbf{r}_{k} -
     \mathbf{r}\right)
\end{equation}
%
where $\left(\mathbf{r}_{k} - \mathbf{r}\right)$ is a vector from
the spacecraft to the $k^{th}$ body, $m_s$ is the mass of the
spacecraft, and $m_k$ is the mass of the $k^{th}$ body.  We can
write $\ddot{\tilde{\mathbf{r}}}_s$ as simply
%
\begin{equation}
    \ddot{\tilde{\mathbf{r}}}_s =
     G\sum_{k=1}^n \frac{m_k}{\| \mathbf{r}_{k} - \mathbf{r}\|^3} \left(\mathbf{r}_{k} -
     \mathbf{r}\right) \label{Eq:SCInertialODE}
\end{equation}
%
We can apply Newton's 2nd Law to the $j^{th}$ body and obtain
%
\begin{equation}
     m_j \ddot{\tilde{\mathbf{r}}}_j = \frac{G m_s
     m_j}{r^3}\mathbf{r} +
     G\sum_{\stackrel{k=1}{k \neq j}}^{n} \frac{m_j m_k}{\| \mathbf{r}_{k}\|^3}\mathbf{r}_{k}
\end{equation}
%
where the first term is the influence of the spacecraft on the
central body, and the second term is the influence of the $k$ point
mass gravitational bodies.  We can write
$\ddot{\tilde{\mathbf{r}}}_j$ as simply
%
\begin{equation}
     \ddot{\tilde{\mathbf{r}}}_j = \frac{G m_s
     }{r^3}\mathbf{r} +
     G\sum_{\stackrel{k=1}{k \neq j}}^{n} \frac{ m_k}{\|
     \mathbf{r}_{k}\|^3}\mathbf{r}_{k} \label{Eq:CentalBodyInertialODE}
\end{equation}
%
Substituting Eq.~(\ref{Eq:SCInertialODE}) and
(\ref{Eq:CentalBodyInertialODE}) into (\ref{Eq:SCRelativeODE}) we
get
%
\begin{equation}
     \ddot{\mathbf{r}} =      G\sum_{k=1}^n \frac{m_k}{\| \mathbf{r}_{k} - \mathbf{r}\|^3} \left(\mathbf{r}_{k} -
     \mathbf{r}\right) - \frac{G m_s
     }{r^3}\mathbf{r} -
     G\sum_{\stackrel{k=1}{k \neq j}}^{n} \frac{ m_k}{\|
     \mathbf{r}_{k}\|^3}\mathbf{r}_{k}
\end{equation}
%
Finally, collecting terms yields
%
\begin{equation}
     \mathbf{a}_{pm} = \ddot{\mathbf{r}} =   \underbrace{- \frac{\mu_j
     }{r^3}\mathbf{r}}_1  +  G \sum_{\stackrel{k=1}{k \neq j}}^{n} m_k\left( \underbrace{\frac{\mathbf{r}_{k} -
     \mathbf{r}}{\| \mathbf{r}_{k} - \mathbf{r}\|^3}}_2  -
     \underbrace{
      \frac{ \mathbf{r}_{k}}{\|
     \mathbf{r}_{k}\|^3}}_3\right)
\end{equation}
%
We can break down the acceleration in the equation above into three
physical categories.   The first term is the acceleration on the
spacecraft due to a point mass central body.   The second type of
terms are called direct terms.  They account for the force of the
$k^{th}$ body on the spacecraft.  The third type of terms are called
indirect.  They account for the force of the $k^{th}$ body on the
central body.

Let's look at the contributions to the sensitivity matrix due to
point mass perturbations.  We notice that $\mathbf{a}_{pm}$ is not a
function of velocity.  So,
%
\begin{equation}
    \mathbf{A}_{pm} = \mathbf{D}_{pm}  = \mathbf{0}_{3\times3}
\end{equation}
%
We also know that
%
\begin{equation}
    \mathbf{B}_{pm} = \mathbf{I}_{3\times3}
\end{equation}
%
This leaves $\mathbf{C}_{pm}$ as the only non-trivial term for point
mass gravitational effects.  Let's look first at the derivatives of
the point mass term.  We can use the vector identity in
Eq.~(\ref{Eq:vecIDaveca3}) to arrive at
%
\begin{equation}
     \frac{\partial }{\partial \mathbf{r}} \left(- \frac{\mu_j
     }{r^3}\mathbf{r}\right) =  -\frac{\mu_j}{r^3} \mathbf{I}_3
     + 3\mu_j\frac{\mathbf{r}\mathbf{r}^T}{r^5}
\end{equation}

Similarly, applying Eq.~(\ref{Eq:vecIDaveca3}) to the direct terms
we see that
%
\begin{equation}
     \frac{\partial }{\partial \mathbf{r}} \left( \sum_{\stackrel{k=1}{k \neq j}}^{n} \mu_k \frac{\mathbf{r}_{k} -
     \mathbf{r}}{\| \mathbf{r}_{k} - \mathbf{r}\|^3}\right) =  -\sum_{\stackrel{k=1}{k \neq j}}^{n}
     \frac{\mu_k}{\| \mathbf{r}_{k} - \mathbf{r}\|^3}\mathbf{I}_3 + 3\sum_{\stackrel{k=1}{k \neq
     j}}^{n}\mu_k \left( \frac{\left( \mathbf{r}_{k} - \mathbf{r} \right)\left( \mathbf{r}_{k} -
     \mathbf{r} \right)^T}{\left( \|\mathbf{r}_{k} - \mathbf{r} \right)\|^5}  \right)
\end{equation}
%
Finally, the derivative of the indirect terms are zero and we have
%
\begin{equation}
   \mathbf{C}_{pm} =  \underbrace{-\frac{\mu_j}{r^3} \mathbf{I}_3
     + 3\mu_j\frac{\mathbf{r}\mathbf{r}^T}{r^5}}_{ 1 }
     \underbrace{
     %
     -  \sum_{\stackrel{k=1}{k \neq j}}^{n}
     \frac{\mu_k}{\| \mathbf{r}_{k} - \mathbf{r}\|^3}\mathbf{I}_3 + 3\sum_{\stackrel{k=1}{k \neq
     j}}^{n} \mu_k \left( \frac{\left( \mathbf{r}_{k} - \mathbf{r} \right)\left( \mathbf{r}_{k} -
     \mathbf{r} \right)^T}{\left( \|\mathbf{r}_{k} - \mathbf{r} \right)\|^5}  \right)
       }_{2}
     %
\end{equation}
%
Combining similar terms we can express the result as
%
\begin{equation}
   \mathbf{C}_{pm} =  -  \left( \frac{\mu_j}{r^3} + \sum_{\stackrel{k=1}{k \neq j}}^{n}
     \frac{\mu_k}{\| \mathbf{r}_{k} - \mathbf{r}\|^3} \right)\mathbf{I}_3
     %
     + 3 \left( \mu_j\frac{\mathbf{r}\mathbf{r}^T}{r^5}
       + \sum_{\stackrel{k=1}{k \neq
     j}}^{n} \mu_k \left( \frac{\left( \mathbf{r}_{k} - \mathbf{r} \right)\left( \mathbf{r}_{k} -
     \mathbf{r} \right)^T}{\left( \|\mathbf{r}_{k} - \mathbf{r} \right)\|^5}
     \right) \right)
\end{equation}

\subsection{Non-Spherical Gravity }

GMAT integrates all spacecraft equations of motion using the
Earth's Mean J2000 axis system. However, the user can choose
central bodies other than the Earth as the origin of the
coordinate system of integration.  Gravitational forces are
conservative and only a function of position.  To calculate the
gravitational force due to a non-spherical body, we need to
determine the position of the spacecraft in the body fixed frame
$\mathcal{F}_F$.  However, the equations of motion are expressed
in terms of the position of the spacecraft in the inertial frame.

We know from dynamics that the acceleration in an inertial frame
can be calculated using
%
\begin{equation}
   \mathbf{a}_{cb} = \nabla U \label{Eq:a_cb}
\end{equation}
%
where $U$ is the gravitational potential.  The potential for a
nonspherical body comes from the solution to Laplace's equation:
%
\begin{equation}
     \nabla^2 U= 0
\end{equation}
%
The solution to this equation is most easily expressed in spherical,
body-fixed coordinates because it allows for a convenient separation
of variables.

In spherical coordinates the gradient of the gravitational potential
is
%
\begin{equation}
   \nabla U = \frac{\partial U}{\partial r}\mathbf{u}_r +
   \frac{1}{r}\frac{\partial U}{\partial \phi} \mathbf{u}_\phi
   + \frac{1}{r \cos{\phi}}\frac{\partial U}{\partial
   \lambda}\mathbf{u}_\lambda \label{Eq:SphericalGradient}
\end{equation}
%
We see that there are two singularities in
Eq.~(\ref{Eq:SphericalGradient}).  The first is when $r = 0$, which
is a nonphysical case and we will not discuss it further.  The
second singularity occurs when $ \phi = \pm 90^\circ$.  Pines
\cite{Pines:73} developed a uniform expression of the gravitational
potential that avoids the singularity at the poles:
%
\begin{equation}\begin{split}
    U = & \frac{\mu}{r}  \biggl[   1 + \sum_{n=1}^\infty \left(\frac{R_{\otimes}}{r}\right)^n
    \sum_{m=0}^{n} A_{nm}(u)[C_{nm} cos{(m \lambda)} \cos^m{\phi} \\
    %
    & +S_{nm}sin{(m \lambda)} \cos^m{\phi} ]    \biggr]\end{split}
    \label{Eq:Pines1}
\end{equation}
%
Examining this form of the potential it is easy to see that there is
not a singularity at the poles when taking the gradient in spherical
coordinates.  Pines rewrites Eq.~(\ref{Eq:Pines1}) as
%
\begin{equation}\begin{split}
    U = & \frac{\mu}{r}  \biggl[   1 + \sum_{n=1}^\infty \left(\frac{R_{\otimes}}{r}\right)^n
    \sum_{m=0}^{n} A_{nm}(u)[C_{nm} r_m(s,t) \\
    %
    & +S_{nm} i_m(s,t) ]    \biggr]\end{split}
    \label{Eq:PinesPotential}
\end{equation}
%
where $C_{nm}$ and $S_{nm}$ are the gravitational coefficients, $s$,
$t$, and $u$ are given by
%
\begin{equation}
     s = x/r, \hspace{.2 in} t = y/r, \hspace{.2 in} u = z/r =
     \sin{\phi} \nonumber
\end{equation}
%
and $r_m(s,t)$ and $i_m(s,t)$ are calculated using the recursive
relationships
%
\begin{equation}
    \begin{split}
        r_0 = 1, \hspace{.2 in} r_1 = s, \hspace{.2 in} i_0 = 0,
        \hspace{.2 in} i_1 = t \nonumber\\
        %
        r_m = sr_{m-1} - ti_{m-1}, \hspace{ .2 in} i_m = si_{m-1} +
        tr_{m-1} \nonumber
    \end{split}
\end{equation}

The coefficients $A_{nm}(u)$ are called ``derived" Legendre
functions and are given by
%
\begin{equation}
    A_{nm}(u) = \frac{d^m}{du^m}(P_n(u))
\end{equation}
%
where we know from Rodrigues' \cite{Lundberg:88} formula that
%
\begin{equation}
    P_{n0}(u) = P_n(u) = \frac{1}{2^n n!}\frac{d^n}{du^n}(u^2 - 1)^n
\end{equation}
%
and
%
\begin{equation}
    P_{nm}(u) = (1 - u^2)^{m/2}\frac{d^m}{du^m}P_n(u)
\end{equation}
%

For numerical reasons it is useful to normalize some of the terms in
the potential function, $U$.  By normalizing the spherical
coefficients and the derived Legendre polynomials we can improve the
stability of recursive algorithms used to calculate the Legendre
polynomials and improved numerical problems.  We use the
nondimensionalization approach and described by
Lundberg\cite{Lundberg:88}.  Lundberg chooses the normalization
factor so that the normalized spherical harmonics $\bar{C}_{nm}$ and
$\bar{S}_{nm}$ will have a mean square value of one on the unit
sphere.  The normalized Legendre functions, $\bar{P}_{nm}$, are
defined so that the product of the spherical harmonic coefficients
and the corresponding Legendre functions remain constant, or
%
\begin{equation}
   \bar{P}_{nm}\bar{C}_{nm} = P_{nm}C_{nm}  \hspace{.5 in}  \bar{P}_{nm}\bar{S}_{nm} = P_{nm}S_{nm}
\end{equation}
%
GMAT uses the normalization factor $N_{nm}$ given by
%
\begin{equation}
    N_{nm} =  \left[ \frac{(n-m)!(2n+1)!}{(n+m)!} \right]^{1/2}
\end{equation}
%
The non-dimensional spherical harmonic coefficients and Legendre
functions are
%
\begin{equation}
     \bar{P}_{nm} = N_{nm}P_{nm} \hspace{.25 in} \bar{C}_{nm} =
     \frac{C_{nm}}{N_{nm}} \hspace{.25 in} \bar{S}_{nm} =
     \frac{S_{nm}}{N_{nm}}
\end{equation}
%
The derived Legendre polynomials are normalized using
%
\begin{equation}
    \bar{A}_{nm} = N_{nm} A_{nm} \label{Eq:LegendreNormalization}
\end{equation}
%
where $\bar{A}_{nm}$ are the normalized Legendre polynomials.
Lundberg\cite{Lundberg:88} showed that there are several recursive
algorithms to compute $\bar{A}_{nm}$  but that only two are stable.
GMAT uses the following algorithm to recursively calculate the
derived Legendre polynomicals
%
\begin{equation}
     \begin{split}
     \bar{A}_{nm} = & u\left[ \frac{(2n+1)(2n-1)}{(n-m)(n+m)}
     \right]^{1/2}\bar{A}_{n-1,m}  \\
     %
     & - \left[ \frac{(2n+1)(n-m-1)(n+m-1)}{(2n-3)(n+m)(n-m)}
     \right]^{1/2}\bar{A}_{n-2,m}
     \end{split}
\end{equation}
%
The recursive algorithm is started using
%
\begin{eqnarray}
     \bar{A}_{11} & = & \sqrt{3} \cos{\phi}\\
     \bar{A}_{nn} & = & \cos{\phi}\sqrt{\frac{2n+1}{2n}}\bar{A}_{n-1,n-1}
\end{eqnarray}
%
The above equations are normalized using
Eq.~(\ref{Eq:LegendreNormalization}) and used in

The acceleration due to nonspherical gravity can be written as
%
\begin{equation}
   \begin{split}
   \mathbf{a}_g = & \left( \frac{\partial U}{\partial r} - \frac{s}{r}\frac{\partial U}{\partial s}
    -\frac{t}{r}\frac{\partial U}{\partial t} -\frac{u}{r}\frac{\partial U}{\partial u}
    \right) \hat{\mathbf{r}}\\
    %
    & + \left( \frac{1}{r}\frac{\partial U}{\partial s} \hspace{.1
    in} \frac{1}{r}\frac{\partial U}{\partial t}  \hspace{.1
    in} \frac{1}{r}\frac{\partial U}{\partial u}\right)^T
    \end{split} \label{Eq:NonSphericalAcc}
\end{equation}
%

To simplify the partial derivatives in
Eq.~(\ref{Eq:NonSphericalAcc}), Pines defines some intermediate
variables as follows
%
\begin{eqnarray}
     \rho   & = & a/r \\
     \rho_0 & = & \mu/r \nonumber \\
     \rho_1 & = & \rho \rho_0 \\
     \rho_n & = & \rho \rho_{n-1} \hspace{.2 in} \mbox{for $n>1$}\nonumber
\end{eqnarray}
%
Using Lundberg's nondimensionalization approach, we can write
%
\begin{eqnarray}
     \bar{D}_{nm}(s,t) & = & \bar{C}_{nm}r_m(s,t) + \bar{S}_{nm}i_m(s,t) \nonumber\\
     \bar{E}_{nm}(s,t) & = & \bar{C}_{nm}r_{m-1}(s,t) + \bar{S}_{nm}i_{m-1}(s,t) \nonumber\\
     \bar{F}_{nm}(s,t) & = & \bar{S}_{nm}r_{m-1}(s,t) - \bar{C}_{nm}i_{m-1}(s,t) \nonumber\\
     \bar{G}_{nm}(s,t) & = & \bar{C}_{nm}r_{m-2}(s,t) + \bar{S}_{nm}i_{m-2}(s,t) \nonumber\\
     \bar{H}_{nm}(s,t) & = & \bar{S}_{nm}r_{m-2}(s,t) - \bar{C}_{nm}i_{m-2}(s,t) \nonumber\\
\end{eqnarray}
%
The partial derivatives in Eq.~(\ref{Eq:NonSphericalAcc}) can be
written as
%
\begin{equation}
    \begin{split}
   &\frac{\partial U}{\partial r} - \frac{s}{r}\frac{\partial U}{\partial s}
    -\frac{t}{r}\frac{\partial U}{\partial t} -\frac{u}{r}\frac{\partial U}{\partial u}
     =\\
     -\sum_{n=0}^{\infty}&\frac{\rho_{n+1}}{R_\otimes}\sum_{m=0}^{n}c_{n+1,m+1}\bar{A}_{n+1,m+1}\bar{D}_{nm}
    \end{split} \label{Eq:a4}
\end{equation}
%
\begin{equation}
    \frac{1}{r}\frac{\partial U}{\partial s} =
    \sum_{n=0}^{\infty}\frac{\rho_{n+1}}{R_\otimes}\sum_{m=0}^{n}\bar{A}_{nm}(u) m \bar{E}_{nm}
\end{equation}
%
\begin{equation}
    \frac{1}{r}\frac{\partial U}{\partial t} =
    \sum_{n=0}^{\infty}\frac{\rho_{n+1}}{R_\otimes}\sum_{m=0}^{n}\bar{A}_{nm}(u) m \bar{F}_{nm}
\end{equation}
%
\begin{equation}
    \frac{1}{r}\frac{\partial U}{\partial s} =
    \sum_{n=0}^{\infty}\frac{\rho_{n+1}}{R_\otimes}\sum_{m=0}^{n}c_{n,m+1}\bar{A}_{n,m+1}(u)\bar{D}_{nm}
\end{equation}
%
where
%
\begin{eqnarray}
    c_{n,m+1}   & = & \left[(n-m)(n+m+1) \right]^{1/2}  \nonumber\\
    c_{n+1,m+1} & = & \left[\frac{( n + m + 2) (n + m + 1 )}{(2n+3)(2n+2)}
    \right]^{1/2}  \nonumber
\end{eqnarray}

To calculate the nonzero portion of the sensitivity matrix, we begin
by calcluting the following 9 terms:
%
\begin{eqnarray}
    a_{11} & = & \sum_{n=0}^{\infty}\frac{\rho_{n+2}}{R_\otimes^2}\sum_{m=0}^{n}m(m-1)\bar{A}_{nm}\bar{G}_{nm}\\
    %
    a_{12} & = & \sum_{n=0}^{\infty}\frac{\rho_{n+2}}{R_\otimes^2}\sum_{m=0}^{n}m(m-1)\bar{A}_{nm}\bar{H}_{nm}\\
    %
    a_{13} & = & \sum_{n=0}^{\infty}\frac{\rho_{n+2}}{R_\otimes^2}\sum_{m=0}^{n}mc_{n,m+1}\bar{A}_{n,m+1}\bar{E}_{nm}\\
    %
    a_{14} & = & -\sum_{n=0}^{\infty}\frac{\rho_{n+2}}{R_\otimes^2}\sum_{m=0}^{n}mc_{n+1,m+1}\bar{A}_{n+1,m+1}\bar{E}_{nm}\\
    %
    a_{23} & = & \sum_{n=0}^{\infty}\frac{\rho_{n+2}}{R_\otimes^2}\sum_{m=0}^{n}mc_{n,m+1}\bar{A}_{n,m+1}\bar{F}_{nm}\\
    %
    a_{24} & = & -\sum_{n=0}^{\infty}\frac{\rho_{n+2}}{R_\otimes^2}\sum_{m=0}^{n}mc_{n+1,m+1}\bar{A}_{n+1,m+1}\bar{F}_{nm}\\
    %
    a_{33} & = & \sum_{n=0}^{\infty}\frac{\rho_{n+2}}{R_\otimes^2}\sum_{m=0}^{n}c_{n,m+2}\bar{A}_{n,m+2}\bar{D}_{nm}\\
    %
    a_{34} & = & -\sum_{n=0}^{\infty}\frac{\rho_{n+2}}{R_\otimes^2}\sum_{m=0}^{n}c_{n+1,m+2}\bar{A}_{n+1,m+2}\bar{D}_{nm}\\
    %
    a_{44} & = &
    \sum_{n=0}^{\infty}\frac{\rho_{n+2}}{R_\otimes^2}\sum_{m=0}^{n}c_{n+2,m+2}\bar{A}_{n+2,m+2}\bar{D}_{nm}
\end{eqnarray}
%
where
%
\begin{eqnarray}
    c_{n+1,m+2} &=& c_{n+1,m+1}  \left[(n-m)( n + m + 3)
    \right]^{1/2} \nonumber\\
    %
    c_{n,m+2} &=& c_{n,m+1} \left[( n - m - 1)(n + m +2 )
    \right]^{1/2} \nonumber\\
    %
    c_{n+2,m+2} &=&
    c_{n+1,m+1}\left[\frac{(n+m+4)(n+m+3)}{(2n+5)(2n+4)}\right]^{1/2} \nonumber
\end{eqnarray}

Finally,
%
\begin{equation}
   \mathbf{C}_g = \frac{\partial \mathbf{a}_g}{\partial \mathbf{r}}
\end{equation}
%
where $\mathbf{C}_g$ is a symmetric matrix with components given by
%
\begin{eqnarray}
   c_{11} & = & a_{11} + s^2a_{44} + a_4/r + 2sa_{14} \\
%
   c_{12} & = & c_{21} = a_{12}+ sta_{44} + sa_{24} + ta_{14}\\
%
   c_{13} & = & c_{31} = a_{13}+ sua_{44} + sa_{34} + ua_{14}\\
%
   c_{22} & = & -a_{11} +t^2a_{44} + a_4/r +2ta_{24}\\
%
   c_{23} & = & c_{32} = a_{23} + tua_{44} + ua_{24} + ta_{34}\\
%
   c_{33} & = & a_{33} + u^2a_{44} + a_4/r + 2*u*a_{34}
\end{eqnarray}
%
Note that
%
\begin{equation}
    a_4 = \frac{\partial U}{\partial r} - \frac{s}{r}\frac{\partial U}{\partial s}
    -\frac{t}{r}\frac{\partial U}{\partial t} -\frac{u}{r}\frac{\partial U}{\partial u}
\end{equation}
%
and is given in Eq.~(\ref{Eq:a4}).

\subsection{Atmospheric Drag}

The acceleration due to drag is given by
%
\begin{equation}
  \mathbf{a}_d = - \displaystyle\frac{1}{2}\rho v_{rel}^2 \displaystyle\frac{C_d
   A}{m_s}\hat{\mathbf{v}}_{rel}
\end{equation}
%
where
%
\begin{equation}
    \mathbf{v}_{rel} = \mathbf{v} - \boldsymbol{\omega}_\otimes \times
    \mathbf{r} + \mathbf{v}_w
\end{equation}
%
and $\boldsymbol{\omega}_\otimes$ is the central bodies angular velocity vector, $\mathbf{v}_w$ is the local wind velocity, $C_d$ is the drag coefficient,
$A$ is the cross sectional area normal to $\mathbf{v}_{rel}$, $\rho$ is the atmospheric
density, and $m_s$ is the
spacecraft mass.

The partial derivatives of the drag force with respect to position and velocity are:
%
\begin{equation}
   \frac{\partial \mathbf{a}_d }{\partial \mathbf{r}} = -\frac{1}{2}\frac{C_d A}{m_s}\left( v_{rel}\mathbf{v}_{rel}\frac{\partial \rho}{\partial \mathbf{r}} + \rho \mathbf{v}_{rel}\hat{\mathbf{v}}_{rel}^T \left(\frac{\partial \mathbf{v}_{w}}{\partial \mathbf{r}}  - \boldsymbol{\omega}_B^{x}\right) +
   \rho v_{rel} \left( \frac{ \partial \mathbf{v}_w}{\partial \mathbf{r}} - \boldsymbol{\omega}_B^{x} \right)\right)
\end{equation}
%
\begin{equation}
    \frac{\partial \mathbf{a}_d }{\partial \mathbf{v}} = -\frac{1}{2}\frac{\rho C_d A}{m_s}\left(  \mathbf{v}_{rel}\hat{\mathbf{v}}_{rel}^T + v_{rel}\mathbf{I}_3\right)
\end{equation}
%
%where
%
%\begin{equation}
%    \frac{\partial \mathbf{v}_{rel} }{\partial \mathbf{r}} = \frac{\partial \mathbf{v}_w}{\partial \mathbf{r}} - %\boldsymbol{\omega}_B^{I_x}
%\end{equation}

%\begin{equation}
%    \frac{\partial \mathbf{v}_{rel} }{\partial \mathbf{v}} = \mathbf{I}_3
%\end{equation}

\subsection{Solar Radiation Pressure}

\begin{equation}
     \mathbf{a}_s = -P_{SR}\displaystyle\frac{   C_R A    }{m_s}\hat{\mathbf{s}}
\end{equation}
%
where $\hat{\mathbf{s}}$ is a unitized vector pointing from the
spacecraft to the sun
%
\begin{equation}
    \mathbf{s} = \mathbf{r}_s - \mathbf{r}
\end{equation}
%
where $\mathbf{r}_s$ is the Sun's position vector and $\mathbf{r}$
is the spacecrafts position vector.

\begin{equation}
    \mathbf{A}_{s} = \mathbf{D}_{s}  = \mathbf{0}_{3\times3}
\end{equation}
%
\begin{equation}
    \mathbf{B}_{s} = \mathbf{I}_{3\times3}
\end{equation}
%
\begin{equation}
    \mathbf{C}_{s} = P_{SR}\displaystyle\frac{   C_R A
    }{m_s}\left( \frac{1}{s^3}\mathbf{I}_3 - 3 \frac{ \mathbf{s}\mathbf{s}^T}{s^5}\right)
\end{equation}
%
where
%
\begin{equation}
    s = \| \mathbf{s} \|
\end{equation}

\subsection{Relativistic Corrections}

The form of the relativitic correction to the Newtonian equations of motion depends upon the coordinate system in which they are expressed.  The treatment in GMAT follows Huang\cite{Huang:90} \emph{et. al.}. For celestial-body-centered motion (not sun or solarsystem barycenter) expressed in the local J2000 axis system, the relativistic correction takes the following form:
%
\begin{equation}
    \mathbf{a}_r = \frac{\mu}{c^2 r^3}\left(  \left( 4\frac{\mu}{r} - v^2\right)\mathbf{r} +   4(\mathbf{r}\cdot\mathbf{v})\mathbf{v}\right) + 2 (\boldsymbol{\Omega} \times \mathbf{v}) + 2 \frac{\mu}{c^2 r^3}
    \left( \frac{3}{r^2}(\mathbf{r} \times \mathbf{v})(\mathbf{r} \cdot \mathbf{J}) +(\mathbf{v}\times\mathbf{J}) \right)
    \label{Eq:RelativisticCorrection}
\end{equation}
%
where
%
\begin{equation}
   \boldsymbol{\Omega} = \frac{3}{2} \mathbf{v}_{B/S} \times \left( \frac{-\mu \mathbf{r}_{B/S}}{c^2r_{B/S}^3} \right)
\end{equation}
%
and
%
\begin{itemize}
   \item  $\mu$ is the gravitational parameter of the central body expressed in the local celestial body J2000 frame
   \item  $c$ is the speed of light
   \item  $\mathbf{J}$ is the central body's angular momentum per unit mass.
   \item  $\mathbf{r}$ is the vehicle's position in the local J000 system.
   \item  $\mathbf{v}$ is the vehicle's velocity in the local J000 system.
   \item  $\mathbf{r}_{B/S}$ is the central body's position with respect to the Sun.
   \item  $\mathbf{v}_{B/S}$ is the central body's velocity with respect to the Sun.
\end{itemize}
%
The first term in Eq.~\ref{Eq:RelativisticCorrection} is the Schwarzschild solution, the second term is geodesic precesion, and the third term is Lense-Thirring precession.  The vector $J$ is computed using
%
\begin{equation}
    \mathbf{J} = \mathbf{R}_B^{I/F}\left[ 0   \hspace{0.15 in} 0  \hspace{0.15 in}   \frac{2}{5}R_B^2 \omega_B \right]^T
\end{equation}
%
where $R_B$ is the central body's mean equatorial radius,$\omega_B$ is the spin rate, and $\mathbf{R}_B^{I/F}$ is the
body's fixed to inertial rotation matrix.

Eq.~{\ref{Eq:RelativisticCorrection}} is used for all bodies except the sun.  In the case that the sun is the central body, the geodesic precesion term is omitted from the correction.

\subsection{Spacecraft Thrust}

\begin{footnotesize}

\begin{equation}
\begin{split}
    F_T(T,P) = C_1 + C_2 P +  (C_3 + C_4 P + C_5 P^2 + C_6 P^{C_7}\\ +  C_8P^{C_9}
     + C_{10}P^{C_{11}}
    + C_{12}(C_{13})^{C_{14}P})\left(\frac{T}{T_{ref}}\right)^{1 + C_{15} + C_{16}P }
    \label{Eq:ThrustPolynomial}
     \end{split}
\end{equation}

%
\begin{equation}
\begin{split}
    I_{sp}(T,P) = K_1 + K_2 P + (K_3 + K_4 P + K_5 P^2 + K_6 P^{K_7} \\ + K_8P^{K_9} + K_{10}P^{K_{11}}
    + K_{12}(K_{13})^{K_{14}P})\left(\frac{T}{T_{ref}}\right)^{1 + K_{15} + K_{16}P }
         \end{split}
\end{equation}
\end{footnotesize}

\begin{equation}
    \dot{m} =  f_d\frac{F_T(T,P)}{I_{sp}(T,P)g}
\end{equation}

\begin{equation}
    \mathbf{T} = f_s f_d F_T(T,P) \mathbf{R}_{iT}\hat{\mathbf{T}}_d
\end{equation}
%
where $F_T(T,P)$ is given in equation \ref{Eq:ThrustPolynomial},
$f_s$ and $f_d$ are the thrust scale factor and duty cycle
respectively, $\mathbf{R}_{iT}$ is the rotation matrix from the
thruster coordinate system to the EarthMJ2000 equatorial system, and
$\hat{\mathbf{T}_d}$ is the unitized thrust direction in the
thruster coordinate system.

\begin{table}[h!]
\centering \caption{ Thrust and Isp Coefficient Units }
      \begin{tabular}{llll}
      \hline\hline
         Coeff. & Unit \\
         \hline
         %---New Row---%
         $C_1$ & N & $K_1$ & s \\
         %---New Row---%
         $C_2$ & N/kPa & $K_2$ & s/kPa\\
         %---New Row---%
         $C_3$ & N  & $K_3$ & s  \\
         %---New Row---%
         $C_4$ & N/kPa & $K_4$ & s/kPa \\
         %---New Row---%
         $C_5$ & N/kPa$^2$ & $K_5$ & s/kPa$^2$ \\
         %---New Row---%
         $C_6$ & N/kPa$^{C_7}$ & $K_6$ & s/kPa$^{C_7}$ \\
         %---New Row---%
         $C_7$ & None & $K_7$ & None  \\
         %---New Row---%
         $C_8$ & N/kPa$^{C_9}$ & $K_8$ & s/kPa$^{C_9}$ \\
         %---New Row---%
         $C_9$ & None & $K_9$ & None \\
         %---New Row---%
         $C_{10}$ & N/kPa$^{C_{11}}$  & $K_{10}$ & s/kPa$^{C_{11}}$  \\
         %---New Row---%
         $C_{11}$ & None & $K_{11}$ & None \\
         %---New Row---%
         $C_{12}$ & N & $K_{12}$ & s\\
         %---New Row---%
         $C_{13}$ & None & $K_{13}$ & None \\
         %---New Row---%
         $C_{14}$ & 1/kPa  & $K_{14}$ & 1/kPa  \\
         %---New Row---%
         $C_{15}$ & None & $K_{15}$ & None \\
         %---New Row---%
         $C_{16}$ & 1/kPa & $K_{16}$ & 1/kPa  \\
      \hline\hline
      \label{Table:OE_RigorouslyJ2Inv}
\end{tabular} \normalsize
\end{table}

\textbf{VNB Thruster System}

It is possible to specify thrust with respect to rotating
coordinates systems.  Then, during integration of the equations of
motion, GMAT uses the coordinate system definition to determine the
thrust in the inertial system being used for numerical integration
of the equations of motion.    One of the coordinate systems useful
in mission analysis is the Velocity-Normal-Binormal (VNB) system
based on the motion of a spacecraft with respect to a reference
origin. One way to configure a thruster to use a local VNB system is
to set \st{CoordinateSystem} to local.  Internally, GMAT creates a
coordinate system based on the \st{Axes} and \st{Origin} specified
chosen for the thruster. We illustrate this below by example. The
thruster named \st{Thruster1} is configured to use a local VNB
coordinate system based on the motion of the owner-spacecraft and
the Earth.

\noindent\st{Create Thruster Thruster1}\\
\st{Thruster1.CoordinateSystem = Local;}\\
\st{Thruster1.Origin = Earth;}\\
\st{Thruster1.Axes   = VNB;}\\

To convert the thrust from the requested local VNB system to the
inertial system, GMAT creates a coordinate system configured as
shown below. The \st{Origin} field on the thruster is used in two
places on the coordinate system: as both the \st{Origin} and the
\st{Primary}. The axes are set to ObjectReferenced, and the $x$-axis
and $y$-axis are respectively set to ``V'' and ``N''.

\noindent\st{ Create CoordinateSystem SATVNB;}\\
\st{ GMAT SATVNB.Origin = DefaultSC; }\\
\st{ GMAT SATVNB.Axes = ObjectReferenced;}\\
\st{ GMAT SATVNB.Primary = Earth;}\\
\st{ GMAT SATVNB.Secondary = DefaultSC;}\\
\st{ GMAT SATVNB.XAxis = V; }\\
\st{ GMAT SATVNB.YAxis = N; }\\

Using this system, the $x$-axis is in the velocity direction, the
$n$-axis is in the velocity direction, and the $z$-axis completes
the right-handed set. Note, the secondary body is not set until the
thruster is assigned to a spacecraft, and then, the secondary is set
to be the owner- spacecraft.  The script snippet above shows the
configuration after the thruster has been attached to spacecraft
``DefaultSC'', using, for example, the script line
%
\st{DefaultSC.Thrusters = \{Thruster1\}};

\noindent \textbf{LVLH Thruster System}\\

The LVLH system, similarly to the VNB system, is a local system that
is constructed based on the motion of the owner spacecraft with
respect to an origin and axes system chosen by the user.  As an
example, below the thruster named \st{Thruster1} is configured to
use a local LVLH coordinate system based on the motion of the owner
spacecraft ``MySat'' and the moon.

\noindent\st{Create Thruster Thruster1}\\
\st{Thruster1.CoordinateSystem = Local;}\\
\st{Thruster1.Origin = Luna;}\\
\st{Thruster1.Axes   = LVLH;}\\

Internally, GMAT creates a coordinate system configured as shown
below.  The \st{Origin} field on the thruster is used in two places
on the coordinate system, as both the \st{Origin} and the
\st{Primary}.  The axes are set to  \st{ObjectReferenced}.

\noindent\st{ Create CoordinateSystem SATLVLH;}\\
\st{ GMAT SATLVLH.Origin = MySat; }\\
\st{ GMAT SATLVLH.Axes = ObjectReferenced;}\\
\st{ GMAT SATLVLH.Primary = Luna;}\\
\st{ GMAT SATLVLH.Secondary = MySat;}\\
\st{ GMAT SATLVLH.XAxis = -R; }\\
\st{ GMAT SATLVLH.YAxis = -N; }\\

Using this system, the $y$-axis is opposite of orbit normal, the
$z$-axis points towards the origin, and the $x$-axis completes the
right-handed set.

\newpage
\input{AttitudeD5}
\input{SpacecraftModel}
\input{EnvironmentModels}

%\section{Appendix 1:  Derivation of the Orbital Equations of
%Motion}
%
%
%
%\section{Notation}
%
%
%%\begin{tabbing}
%%12345678 \= Reynolds number based on length $s$ \kill
%%$\mathbf{r}$        \> Position vector \\
%%$\mathbf{v}$        \> Velocity vector \\
%%$m$              \> Number of spacecraft in formation \\
%%$n_k$              \> Number of maneuvers along $k^{th}$ trajectory \\
%%$t$                 \> Time \\
%%$\boldsymbol\Phi$   \> State transition matrix \\
%%$\mathbf{A}$   \> Upper left 3x3 partition of $\boldsymbol\Phi$ \\
%%$\mathbf{B}$   \> Upper right 3x3 partition of $\boldsymbol\Phi$ \\
%%$\mathbf{C}$   \> Lower left 3x3 partition of $\boldsymbol\Phi$\\
%%$\mathbf{D}$   \> Lower right 3x3 partition of $\boldsymbol\Phi$
%%\\
%%$\Delta\mathbf{v}_{jk}$   \> $j^{th}$ impulsive maneuver on $k^{th}$ trajectory\\
%%$\Delta v_{jk}$   \> Magnitude of $j^{th}$  maneuver on $k^{th}$ trajectory\\
%%$\mathcal{P}_{ok}$  \>  Initial trajectory of $k^{th}$ spacecraft\\
%%$\mathcal{P}_{fk}$  \>  Final trajectory of $k^{th}$ spacecraft\\
%%$N$                  \>  Number of Boundary Value Problems\\
%%$\mathbf{X}$     \>  Vector of independent variables\\
%%$\mathbf{C}$     \>  Vector of constants\\
%%\end{tabbing}
%%
%%\subsection{Subscripts}
%%\begin{tabbing}
%%12345678 \= \kill
%%$i$   \> Maneuver location index , ($2 \leq i \leq n_k-1$) \\
%%$j$   \> Maneuver epoch index, ($1 \leq j \leq n_k$) \\
%%$k$   \> Trajectory index , ($1 \leq k \leq m$) \\
%%$o$   \> Initial conditions \\
%%$f$   \> Final conditions \\
%%$I$  \>  Inertial Frame
%%\end{tabbing}
%%
%%\subsection{Superscripts}
%%\begin{tabbing}
%%12345678  \= \kill
%%$r$       \> Position solution \\
%%$v$       \> Velocity solution \\
%%$+$       \> Post-maneuver condition\\
%%$-$       \> Pre-maneuver condition
%%\end{tabbing}
%
%\section{Spacecraft Equations of Motion}
%
%
%%
%\begin{figure}[!]
%\centerline{
%\begin{picture}(100,500)
%\special{psfile= NBodyDiagram.eps hoffset= -135 voffset= -45
%hscale=85 vscale=85} \makebox(-20,585){$\hat{\mathbf{x}}_{I}$}
%\makebox(270,700){$\hat{\mathbf{y}}_{I}$}
%\makebox(-330,770){$\mathbf{r}_{s}$}
%\makebox(-330,877){$\mathbf{r}$}
%\makebox(-270,900){$\mathbf{r}_k$}
%\makebox(-390,964){$\mathbf{r}_{kj}$}
%\makebox(-500,814){$\mathbf{r}_{j}$} \makebox(-500,980){Central
%Body} \makebox(-320,995){$k^{th}$ Body}
%\end{picture}}\vskip -4.0 in  \caption{ N Body Illustration} \label{fig:NBody}
%\end{figure}
%
%\section{General Form}
%
%
%
%In general, the equations of motion can be written as
%%
%\begin{equation}
%  \frac{d}{dt}\left( m_s \displaystyle\frac{d \mathbf{r}_s}{dt}\right) = \sum
%  \mathbf{F}_k
%\end{equation}
%%
%where $\mathbf{r}_s$ is the spacecraft's position with respect to
%an inertial frame, and $m_s$ is the spacecraft mass.  Expanding
%the left hand side we get
%%
%\begin{equation}
%  \dot{m}_s\displaystyle\frac{d \mathbf{r}_s}{dt} + m_s \frac{d^2
%  \mathbf{r}_s}{dt^2}= \mathbf{F}_G + \mathbf{F}_T + \mathbf{F}_D
%  + \mathbf{F}_S + \mathbf{F}_O\label{Eq:EOMLHSExp}
%\end{equation}
%%
%where $\mathbf{F}_g$ is the force due to gravity, $\mathbf{F}_T$
%is the force due to thrust, $\mathbf{F}_D$ is the force due to
%drag, $\mathbf{F}_S$ is the force due to solar radiation pressure,
%and $\mathbf{F}_O$ are other forces. We usually want the equations
%of motion of the spacecraft expressed with respect to a central
%body. So, we note that
%%
%\begin{equation}
%     \mathbf{r}_s = \mathbf{r}_j + \mathbf{r}
%\end{equation}
%%
%Taking the first and second time derivatives gives us
%%
%%\begin{equation}
%%     \frac{d\mathbf{r}_s}{dt} = \frac{d\mathbf{r}_j}{dt} +
%%     \frac{d\mathbf{r}}{dt} \label{Eq:InertDeriv1}
%%\end{equation}
%%
%\begin{equation}
%     \frac{d^2\mathbf{r}_s}{dt^2} = \frac{d^2\mathbf{r}_j}{dt^2} +
%     \frac{d^2\mathbf{r}}{dt^2} \label{Eq:InertDeriv2}
%\end{equation}
%%
%We can substitute Eq.~(\ref{Eq:InertDeriv2}) into
%Eq.~(\ref{Eq:EOMLHSExp}) to get
%%
%\begin{equation}
%       \dot{m}_s\displaystyle\frac{d \mathbf{r}_s}{dt} + m_s \left( \frac{d^2\mathbf{r}_j}{dt^2} +
%     \frac{d^2\mathbf{r}}{dt^2}\right)= \mathbf{F}_G + \mathbf{F}_T + \mathbf{F}_D
%  + \mathbf{F}_S + \mathbf{F}_O \label{Eq:EOMAllExpanded}
%\end{equation}
%%
%The term that contains $\dot{m}_s$ appears to be problematic.
%However, if $\dot{m}_s \neq 0$, then we have a force due to
%thrust, $\mathbf{F}_T$, acting on the spacecraft.  In an inertial
%frame, this thrust is written as
%%
%\begin{equation}
%     \mathbf{F}_T = \dot{m}_s \left(\frac{d \mathbf{r}_s}{dt} +
%     \mathbf{v}_e\right)\label{Eq:InertialFt}
%\end{equation}
%%
%where $\mathbf{v}_e$ is the velocity of the exhaust with respect
%to the spacecraft.  Substituting Eq.~(\ref{Eq:InertialFt}) into
%Eq.~(\ref{Eq:EOMAllExpanded}) we arrive at.
%%
%\begin{equation}
%          \frac{d^2\mathbf{r}}{dt^2} =
%      \underbrace{\frac{\mathbf{F}_G}{m_s}-\frac{d^2\mathbf{r}_j}{dt^2}}_{gravity terms} +
%      \frac{\mathbf{F}_D}{m_s}
%  + \frac{\mathbf{F}_S}{m_s} + \frac{\mathbf{F}_O}{m_s}  +
%  \frac{\dot{m}_s}{m_s}\mathbf{v}_e\label{Eq:EOMGeneral}
%\end{equation}
%
%\subsection{Gravitational Acceleration}
%
%\begin{itemize}
%     \item  $\mathbf{r}_{j}$ = Position vector of central body
%     in inertial frame
%     %
%     \item  $ m_s $ =  Spacecraft mass
%     %
%     \item  $\mathbf{r}_{s}$ =  Spacecraft position w/r/t inertial
%     frame
%     %
%     \item $\mathbf{r}$ = Spacecraft position vector w/r/t central
%     body
%     %
%     \item $\mathbf{r}_{kj}$ = Position vector from central body to  $k^{th}$ body
%     %
%     \item $\mathbf{r}_sk$ = Vector from the $k^{th}$ body to
%     the spacecraft
%     %
%     \item $\mathbf{F}_k$ = Force of $k^{th}$ body on s/c
%     %
%     \item $n_b$ = Number of secondary bodies
%\end{itemize}
%
%Let's take a look at the gravitational terms in
%Eq.~(\ref{Eq:EOMGeneral})
%%
%\begin{equation}
%          \frac{d^2\mathbf{r}}{dt^2}
%          =\frac{\mathbf{F}_G}{m_s}-\frac{d^2\mathbf{r}_j}{dt^2}\label{Eq:GravEOM}
%\end{equation}
%%
%$\mathbf{F}_g$ is the force on the spacecraft due to gravity.  It
%is useful to start by assuming that the Earth and spacecraft are
%point masses.  Then we know from Newton's Law of Gravitation that
%%
%\begin{equation}
%     \mathbf{F}_G = -\frac{G m_s m_j}{r^3}\mathbf{r}
%\end{equation}
%%
%and
%%
%\begin{equation}
%    \frac{d^2\mathbf{r}_j}{dt^2} = \frac{G m_s }{r^3}\mathbf{r}
%\end{equation}
%%
%Substituting these equations into Eq.~(\ref{Eq:GravEOM}) yields
%%
%\begin{equation}
%   \frac{d^2\mathbf{r}}{dt^2} = -\frac{G m_s }{r^3}\mathbf{r} -
%   \frac{G m_j }{r^3}\mathbf{r} = -\frac{G \left(m_s +m_j
%     \right)}{r^3}\mathbf{r}
%\end{equation}
%%
%This is the well known two-body orbital equation of motion
%%
%\begin{equation}
%        \frac{d^2\mathbf{r}}{dt^2} = -\frac{\mu}{r^3}\mathbf{r}
%        \label{Eq:TwoBodyEOM}
%\end{equation}
%%
%where $\mu = G \left(m_s +m_j \right)$
%
%Now lets consider the case when there are other gravitational
%bodies in the system and that all of the gravitational bodies are
%non-spherical.  However, we'll assume the mass distribution of the
%spacecraft is negligible.  In this case we can write the forces on
%the spacecraft that are in addition to the central body force as
%%
%\begin{equation}
%   \frac{ \mathbf{F}_G}{m_s} = \nabla \phi_{sj}^o + \sum_{\stackrel{k=1}{k \neq j}}^{n_b} \nabla \left(\phi_{sk}^s + \phi_{sk}^o
%    \right)
%\end{equation}
%%
%where $\nabla$ is the gradient operator, $\phi$ a bodies
%gravitational potential, a superscript ``$s$" denotes the
%spherical portion of gravitational potential, and a superscript
%``$o$" denotes the oblate portion the gravitational potential.
%From physics we know that
%%
%\begin{equation}
%    \frac{d^2 \mathbf{r}}{dt^2} = \nabla \phi
%\end{equation}
%%
%where we choose to define the potential $\phi$ according to
%%
%\begin{equation}
%     \phi = \frac{\mu}{r}
%\end{equation}
%%
%Then, for the spherical terms, we know that
%%
%\begin{equation}
%    \nabla \phi_{sk}^s = -\frac{G m_k }{r_{sk}^3}\mathbf{r}_{sk} =
%    \frac{G m_k }{r_{ks}^3}\mathbf{r}_{ks}
%\end{equation}
%%
%%
%\begin{equation}
%    \frac{\mathbf{F}_G}{m_s} =  \nabla \phi_{sj}^o + \sum_{\stackrel{k=1}{k \neq j}}^{n_b}\left(\frac{G m_k }{r_{ks}^3}\mathbf{r}_{ks}   + \nabla
%    \phi_{k}^o\right)      \label{Eq:FG}
%\end{equation}
%
%
%We can apply Newton's laws to the central body to arrive at
%%
%\begin{equation}
%     \frac{d^2\mathbf{r}_j}{dt^2} = \frac{1}{m_j}\sum_{\stackrel{k=1}{k \neq j}}^{n_b}\nabla\left(
%     \phi_{kj}^s + \phi_{kj}^o
%     \right) = \sum_{\stackrel{k=1}{k \neq j}}^{n_b}\left(-
%     \frac{G m_k}{r_{kj}^3}\mathbf{r}_{kj} +
%     \frac{\nabla\phi_{kj}^o}{m_j}
%     \right) \label{Eq:CBEOM}
%\end{equation}
%%
%Eqs.~(\ref{Eq:FG}) and(\ref{Eq:CBEOM}) represent the forces in
%addition to the two-body point mass force in the equations of
%motion.  Augmenting Eq.~(\ref{Eq:TwoBodyEOM}) with
%Eqs.~(\ref{Eq:FG}) and(\ref{Eq:CBEOM}) we arrive at
%%
%\begin{equation}\begin{split}
%    \frac{d^2\mathbf{r}}{dt^2}
%    = &-\frac{\mu}{r^3}\mathbf{r} +
%    %
%    \nabla \phi_{sj}^o + \sum_{\stackrel{k=1}{k \neq j}}^{n_b}\left(\frac{G m_k }{r_{ks}^3}\mathbf{r}_{ks}   + \nabla
%    \phi_{ks}^o\right)\\
%     &+
%     %
%    \sum_{\stackrel{k=1}{k \neq j}}^{n_b}\left(-
%     \frac{G m_k}{r_{kj}^3}\mathbf{r}_{kj} +
%     \nabla\phi_{kj}^o
%     \right)
%\end{split}\end{equation}
%%
%Grouping similar terms we arrive at
%%
%\begin{equation}\begin{split}
%    \frac{d^2\mathbf{r}}{dt^2}
%    =  &-\frac{\mu}{r^3}\mathbf{r} +  \nabla \phi_{sj}^o +
%    %
%    G\sum_{\stackrel{k=1}{k \neq j}}^{n_b}m_k \left(\frac{\mathbf{r}_{ks}}{r_{ks}^3} -
%     \frac{\mathbf{r}_{kj} }{r_{kj}^3}   \right)\\
%     %
%   & + \sum_{\stackrel{k=1}{k \neq j}}^{n_b}\left( \nabla
%    \phi_{ks}^o +
%     \nabla\phi_{kj}^o
%     \right)
%\end{split} \end{equation}
%
%
%%\section{Spacecraft Equations of Motion}
%%
%%
%%%
%%\begin{figure}[!]
%%\centerline{
%%\begin{picture}(100,500)
%%\special{psfile= NBodyDiagram.eps hoffset= -155 voffset= -45
%%hscale=85 vscale=85} \makebox(-20,585){$\hat{\mathbf{x}}_{I}$}
%%\makebox(270,700){$\hat{\mathbf{y}}_{I}$}
%%\makebox(-330,770){$\mathbf{r}_{s}$}
%%\makebox(-330,877){$\mathbf{r}$}
%%\makebox(-270,900){$\mathbf{r}_k$}
%%\makebox(-390,964){$\mathbf{r}_{kj}$}
%%\makebox(-500,814){$\mathbf{r}_{j}$} \makebox(-500,980){Central
%%Body} \makebox(-320,995){$k^{th}$ Body}
%%\end{picture}}\vskip -4.0 in  \caption{ N Body Illustration} \label{fig:NBody}
%%\end{figure}
%%
%%\section{General Form}
%%
%%
%%
%%In general, the equations of motion can be written as
%%%
%%\begin{equation}
%%  \frac{d}{dt}\left( m_s \displaystyle\frac{d \mathbf{r}_s}{dt}\right) = \sum
%%  \mathbf{F}_k
%%\end{equation}
%%%
%%where $\mathbf{r}_s$ is the spacecraft's position with respect to
%%an inertial frame, and $m_s$ is the spacecraft mass.  Expanding
%%the left hand side we get
%%%
%%\begin{equation}
%%  \dot{m}_s\displaystyle\frac{d \mathbf{r}_s}{dt} + m_s \frac{d^2
%%  \mathbf{r}_s}{dt^2}= \mathbf{F}_G + \mathbf{F}_T + \mathbf{F}_D
%%  + \mathbf{F}_S + \mathbf{F}_O\label{Eq:EOMLHSExp}
%%\end{equation}
%%%
%%where $\mathbf{F}_g$ is the force due to gravity, $\mathbf{F}_T$
%%is the force due to thrust, $\mathbf{F}_D$ is the force due to
%%drag, $\mathbf{F}_S$ is the force due to solar radiation pressure,
%%and $\mathbf{F}_O$ are other forces. We usually want the equations
%%of motion of the spacecraft expressed with respect to a central
%%body. So, we note that
%%%
%%\begin{equation}
%%     \mathbf{r}_s = \mathbf{r}_j + \mathbf{r}
%%\end{equation}
%%%
%%Taking the first and second time derivatives gives us
%%%
%%%\begin{equation}
%%%     \frac{d\mathbf{r}_s}{dt} = \frac{d\mathbf{r}_j}{dt} +
%%%     \frac{d\mathbf{r}}{dt} \label{Eq:InertDeriv1}
%%%\end{equation}
%%%
%%\begin{equation}
%%     \frac{d^2\mathbf{r}_s}{dt^2} = \frac{d^2\mathbf{r}_j}{dt^2} +
%%     \frac{d^2\mathbf{r}}{dt^2} \label{Eq:InertDeriv2}
%%\end{equation}
%%%
%%We can substitute Eq.~(\ref{Eq:InertDeriv2}) into
%%Eq.~(\ref{Eq:EOMLHSExp}) to get
%%%
%%\begin{equation}
%%       \dot{m}_s\displaystyle\frac{d \mathbf{r}_s}{dt} + m_s \left( \frac{d^2\mathbf{r}_j}{dt^2} +
%%     \frac{d^2\mathbf{r}}{dt^2}\right)= \mathbf{F}_G + \mathbf{F}_T + \mathbf{F}_D
%%  + \mathbf{F}_S + \mathbf{F}_O \label{Eq:EOMAllExpanded}
%%\end{equation}
%%%
%%The term that contains $\dot{m}_s$ appears to be problematic.
%%However, if $\dot{m}_s \neq 0$, then we have a force due to
%%thrust, $\mathbf{F}_T$, acting on the spacecraft.  In an inertial
%%frame, this thrust is written as
%%%
%%\begin{equation}
%%     \mathbf{F}_T = \dot{m}_s \left(\frac{d \mathbf{r}_s}{dt} +
%%     \mathbf{v}_e\right)\label{Eq:InertialFt}
%%\end{equation}
%%%
%%where $\mathbf{v}_e$ is the velocity of the exhaust with respect
%%to the spacecraft.  Substituting Eq.~(\ref{Eq:InertialFt}) into
%%Eq.~(\ref{Eq:EOMAllExpanded}) we arrive at.
%%%
%%\begin{equation}
%%          \frac{d^2\mathbf{r}}{dt^2} =
%%      \underbrace{\frac{\mathbf{F}_G}{m_s}-\frac{d^2\mathbf{r}_j}{dt^2}}_{gravity terms} +
%%      \frac{\mathbf{F}_D}{m_s}
%%  + \frac{\mathbf{F}_S}{m_s} + \frac{\mathbf{F}_O}{m_s}  +
%%  \frac{\dot{m}_s}{m_s}\mathbf{v}_e\label{Eq:EOMGeneral}
%%\end{equation}
%%
%%\subsection{Gravitational Acceleration}
%%
%%\begin{itemize}
%%     \item  $\mathbf{r}_{j}$ = Position vector of central body
%%     in inertial frame
%%     %
%%     \item  $ m_s $ =  Spacecraft mass
%%     %
%%     \item  $\mathbf{r}_{s}$ =  Spacecraft position w/r/t inertial
%%     frame
%%     %
%%     \item $\mathbf{r}$ = Spacecraft position vector w/r/t central
%%     body
%%     %
%%     \item $\mathbf{r}_{kj}$ = Position vector from central body to  $k^{th}$ body
%%     %
%%     \item $\mathbf{r}_{sk}$ = Vector from the $k^{th}$ body to
%%     the spacecraft
%%     %
%%     \item $\mathbf{F}_k$ = Force of $k^{th}$ body on s/c
%%     %
%%     \item $n_b$ = Number of secondary bodies
%%\end{itemize}
%%
%%Let's take a look at the gravitational terms in
%%Eq.~(\ref{Eq:EOMGeneral})
%%%
%%\begin{equation}
%%          \frac{d^2\mathbf{r}}{dt^2}
%%          =\frac{\mathbf{F}_G}{m_s}-\frac{d^2\mathbf{r}_j}{dt^2}\label{Eq:GravEOM}
%%\end{equation}
%%%
%%$\mathbf{F}_g$ is the force on the spacecraft due to gravity.  It
%%is useful to start by assuming that the Earth and spacecraft are
%%point masses.  Then we know from Newton's Law of Gravitation that
%%%
%%\begin{equation}
%%     \mathbf{F}_G = -\frac{G m_s m_j}{r^3}\mathbf{r}
%%\end{equation}
%%%
%%and
%%%
%%\begin{equation}
%%    \frac{d^2\mathbf{r}_j}{dt^2} = \frac{G m_s }{r^3}\mathbf{r}
%%\end{equation}
%%%
%%Substituting these equations into Eq.~(\ref{Eq:GravEOM}) yields
%%%
%%\begin{equation}
%%   \frac{d^2\mathbf{r}}{dt^2} = -\frac{G m_s }{r^3}\mathbf{r} -
%%   \frac{G m_j }{r^3}\mathbf{r} = -\frac{G \left(m_s +m_j
%%     \right)}{r^3}\mathbf{r}
%%\end{equation}
%%%
%%This is the well known two-body orbital equation of motion
%%%
%%\begin{equation}
%%        \frac{d^2\mathbf{r}}{dt^2} = -\frac{\mu}{r^3}\mathbf{r}
%%        \label{Eq:TwoBodyEOM}
%%\end{equation}
%%%
%%where $\mu = G \left(m_s +m_j \right)$
%%
%%Now lets consider the case when there are other gravitational
%%bodies in the system and that all of the gravitational bodies are
%%non-spherical.  However, we'll assume the mass distribution of the
%%spacecraft is negligible.  In this case we can write the forces on
%%the spacecraft that are in addition to the central body force as
%%%
%%\begin{equation}
%%   \frac{ \mathbf{F}_G}{m_s} = \nabla \phi_{sj}^o + \sum_{\stackrel{k=1}{k \neq j}}^{n_b} \nabla \left(\phi_{sk}^s + \phi_{sk}^o
%%    \right)
%%\end{equation}
%%%
%%where $\nabla$ is the gradient operator, $\phi$ a bodies
%%gravitational potential, a superscript ``$s$" denotes the
%%spherical portion of gravitational potential, and a superscript
%%``$o$" denotes the oblate portion the gravitational potential.
%%From physics we know that
%%%
%%\begin{equation}
%%    \frac{d^2 \mathbf{r}}{dt^2} = \nabla \phi
%%\end{equation}
%%%
%%where we choose to define the potential $\phi$ according to
%%%
%%\begin{equation}
%%     \phi = \frac{\mu}{r}
%%\end{equation}
%%%
%%Then, for the spherical terms, we know that
%%%
%%\begin{equation}
%%    \nabla \phi_{sk}^s = -\frac{G m_k }{r_{sk}^3}\mathbf{r}_{sk} =
%%    \frac{G m_k }{r_{ks}^3}\mathbf{r}_{ks}
%%\end{equation}
%%%
%%%
%%\begin{equation}
%%    \frac{\mathbf{F}_G}{m_s} =  \nabla \phi_{sj}^o + \sum_{\stackrel{k=1}{k \neq j}}^{n_b}\left(\frac{G m_k }{r_{ks}^3}\mathbf{r}_{ks}   + \nabla
%%    \phi_{k}^o\right)      \label{Eq:FG}
%%\end{equation}
%%
%%
%%We can apply Newton's laws to the central body to arrive at
%%%
%%\begin{equation}\begin{split}
%%     \frac{d^2\mathbf{r}_j}{dt^2} = \frac{1}{m_j}\sum_{\stackrel{k=1}{k \neq j}}^{n_b}\nabla\left(
%%     \phi_{kj}^s + \phi_{kj}^o
%%     \right) = \\\sum_{\stackrel{k=1}{k \neq j}}^{n_b}\left(-
%%     \frac{G m_k}{r_{kj}^3}\mathbf{r}_{kj} +
%%     \frac{\nabla\phi_{kj}^o}{m_j}
%%     \right) \label{Eq:CBEOM}
%%     \end{split}
%%\end{equation}
%%%
%%Eqs.~(\ref{Eq:FG}) and(\ref{Eq:CBEOM}) represent the forces in
%%addition to the two-body point mass force in the equations of
%%motion.  Augmenting Eq.~(\ref{Eq:TwoBodyEOM}) with
%%Eqs.~(\ref{Eq:FG}) and(\ref{Eq:CBEOM}) we arrive at
%%%
%%\begin{equation}\begin{split}
%%    \frac{d^2\mathbf{r}}{dt^2}
%%    = -\frac{\mu}{r^3}\mathbf{r} +
%%    %
%%    \nabla \phi_{sj}^o + \sum_{\stackrel{k=1}{k \neq j}}^{n_b}\left(\frac{G m_k }{r_{ks}^3}\mathbf{r}_{ks}   + \nabla
%%    \phi_{ks}^o\right) +\\
%%     %
%%    \sum_{\stackrel{k=1}{k \neq j}}^{n_b}\left(-
%%     \frac{G m_k}{r_{kj}^3}\mathbf{r}_{kj} +
%%     \nabla\phi_{kj}^o
%%     \right)\end{split}
%%\end{equation}
%%%
%%Grouping similar terms we arrive at
%%%%
%%%\begin{equation}\begin{split}
%%%    \frac{d^2\mathbf{r}}{dt^2}
%%%    =  -\frac{\mu}{r^3}\mathbf{r} +  \nabla \phi_{sj}^o +
%%%    %
%%%    G\sum_{\stackrel{k=1}{k \neq j}}^{n_b}m_k \left(\frac{\mathbf{r}_{ks}}{r_{ks}^3} -
%%%     \frac{\mathbf{r}_{kj} }{r_{kj}^3}   \right)
%%%     %
%%%   + \\\sum_{\stackrel{k=1}{k \neq j}}^{n_b}\left( \nabla
%%%    \phi_{ks}^o +
%%%     \nabla\phi_{kj}^o
%%%     \right)\end{split}
%%%\end{equation}
%%
%%
%%
%% Let's
%%return to equation Eq.~(\ref{Eq:a_cb}). We need to calculate
%%$\mba_{cb}$ in cartesian coordinates.  However, we know the
%%potential in spherical coordinates.  So, we need to use the chain
%%rule to take the derivative and we see that
%%%
%%\begin{equation}\begin{split}
%%   \{\mathbf{a}_{cb}\}_F = \nabla U(r,\phi,\lambda)  = \frac{\partial U(r,\phi,\lambda)}{\partial \mbr_F}
%%   =\\\frac{\partial U}{\partial r}\frac{\partial r}{\partial \mbr_F} +
%%   \frac{\partial U}{\partial \phi}\frac{\partial \phi}{\partial
%%   \mbr_F}+ \frac{\partial U}{\partial \lambda}\frac{\partial \lambda}{\partial \mbr_F}
%%   \label{Eq:a_cb2}\end{split}
%%\end{equation}
%%%
%%The six partial derivatives in Eq.~(\ref{Eq:a_cb2}) are given in the
%%GTDS\cite{GTDS} math spec as
%%%
%%\begin{equation}\begin{split}
%%     \frac{\partial U}{\partial r} = &-\frac{\mu}{r^2}\sum_{\ell =
%%     2}^{\infty}\sum_{m=0}^{\ell}\left(
%%     \frac{R_{\otimes}}{r}\right)^\ell(\ell + 1)P_{\ell m} \left[sin{\phi}  \right]\\
%%    %
%%      &\left( C_{\ell m}\cos{m \lambda}+ S_{\ell m}\sin{m \lambda}  \right)
%%\end{split}\end{equation}
%%%
%%\begin{equation}\begin{split}
%%     \frac{\partial U}{\partial \phi} = &\frac{\mu}{r}\sum_{\ell =
%%     2}^{\infty}\sum_{m=0}^{\ell}\left(
%%     \frac{R_{\otimes}}{r}\right)^\ell\left( P_{\ell, m+1} \left[sin{\phi}  \right] - m \tan{\phi}P_{\ell m}\left[\sin{\phi}\right]\right)\\ &
%%    %
%%      \left( C_{\ell m}\cos{m \lambda}+ S_{\ell m}\sin{m \lambda}
%%     \right)
%%\end{split}\end{equation}
%%\begin{equation}\begin{split}
%%     \frac{\partial U}{\partial \lambda} = & \frac{\mu}{r}\sum_{\ell =
%%     2}^{\infty}\sum_{m=0}^{\ell}\left(
%%     \frac{R_{\otimes}}{r}\right)^\ell m  P_{\ell, m} \left[sin{\phi}  \right]
%%     \\
%%     %
%%     &  \left( S_{\ell m}\cos{m \lambda}+ C_{\ell m}\sin{m \lambda}
%%     \right)
%%\end{split}\end{equation}
%%


% =============================================================================

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

\paragraph{Syntax.}

As a block cipher, AES defines two algorithms
\[
\begin{array}{lcl}
\ALG{Enc} &:& \SET{ 0, 1 }^{8 \cdot 4 \cdot Nk} \times \SET{ 0, 1 }^{8 \cdot 4 \cdot Nb} \rightarrow \SET{ 0, 1 }^{8 \cdot 4 \cdot Nb} \\
\ALG{Dec} &:& \SET{ 0, 1 }^{8 \cdot 4 \cdot Nk} \times \SET{ 0, 1 }^{8 \cdot 4 \cdot Nb} \rightarrow \SET{ 0, 1 }^{8 \cdot 4 \cdot Nb} \\
\end{array}
\]
such that
$
m = \ALG{Dec}( k, c = \ALG{Enc}( k, m ) ) .
$
That is, given a plaintext $m$ and cipher key $k$, \ALG{Enc} encrypts $m$
under $k$; given the same $k$, \ALG{Dec} will invert \ALG{Enc} and so the
{\em same} $m$ can be recovered from the associated ciphertext $c$.
In addition, it defines an algorithm
\ALG{KeyExp}
that expands~\cite[Section 5.2]{FIPS:197} the cipher key into a sequence
of round keys then used by
\ALG{Enc}
or
\ALG{Dec};
where appropriate, we use
\[
\begin{array}{lcl}
\ALG{Enc-KeyExp} &:& \SET{ 0, 1 }^{8 \cdot 4 \cdot Nk} \rightarrow \SET{ 0, 1 }^{( 8 \cdot 4 \cdot Nb ) \times ( Nr + 1 )} \\
\ALG{Dec-KeyExp} &:& \SET{ 0, 1 }^{8 \cdot 4 \cdot Nk} \rightarrow \SET{ 0, 1 }^{( 8 \cdot 4 \cdot Nb ) \times ( Nr + 1 )} \\
\end{array}
\]
to denote said algorithm as specialised to suit
\ALG{Enc}
and
\ALG{Dec}
respectively.

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

\paragraph{Parameterisation.}

An AES parameter set~\cite[Figure 4]{FIPS:197}
is a triple
$
\TUPLE{ Nk, Nb, Nr }
$
where
$Nk$ dictates the number of $32$-bit words in $k$,
$Nb$ dictates the number of $32$-bit words in $m$ or $c$ (i.e., a block),
and
$Nr$ dictates the number of rounds.  The standard AES parameter sets are
\[
\begin{array}{lcl}
\mbox{AES-128} &\mapsto& \TUPLE{ 4, 4, 10 } \\
\mbox{AES-192} &\mapsto& \TUPLE{ 6, 4, 12 } \\
\mbox{AES-256} &\mapsto& \TUPLE{ 8, 4, 14 } \\
\end{array}
\]
such that the number of bits in a plaintext (resp. ciphertext) block is fixed to
$
8 \cdot 4 \cdot Nb = 128 .
$
From here on, we focus wlog. on encryption using AES-128 (other parameter
sets are catered for naturally, and decryption with minor differences) so
use the terms AES and AES-128 synonymously.

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

\paragraph{Design.}

The mathematics underpinning AES are described in ~\cite[Section 4]{FIPS:197}.
In particular, it can be defined in terms of
operations in the finite field $\F_{2^{  8}}$ constructed as
$
\F_{2}[\IND{x}] / ( \IND{x}^{8} + \IND{x}^{4} + \IND{x}^{3} + \IND{x} + 1 ) .
$
A hexadecimal short-hand~\cite[Section 3.2]{FIPS:197} is used to represent
field literals, e.g.,
$
\AESCONST{13} ~\mapsto~ \RADIX{13}{16} ~\equiv~ \RADIX{00010011}{2} ~\mapsto~ \IND{x}^4 + \IND{x} + 1 .
$
Field
      addition,
multiplication,
and
      division
are denoted by
$\AESADD$,
$\AESMUL$,
and
$\AESINV$
respectively,
with the multiplication-by-$\IND{x}$ operation~\cite[Section 4.2.1]{FIPS:197}
denoted \AESFUNC{xtime}.
Elements of $\F_{2^8}$ are collected into $( 4 \times 4 )$-element state
and round key matrices; the $i$-th row and $j$-th column of such a matrix
relating to round $r$ is denoted
$\AESRND {s}{r}_{i,j}$
and
$\AESRND{rk}{r}_{i,j}$
respectively, with super- and/or subscripts omitted whenever irrelevant.

AES is an iterative block cipher, based on a substitution-permutation network.
This means encryption using AES can be described~\cite[Section 5.2]{FIPS:197}
as follows:
1)    the  input  plaintext is pre-whitened to yield
      $\AESRND {s}{  0} = m \AESADD \AESRND{rk}{0} = m \AESADD k$,
2)    each $r$-th round, for $1 \leq r \leq Nr$, demands computation of
      $\AESRND {s}{r+1} = \ALG{P-layer}( \ALG{S-layer}( \AESRND{s}{r}                        ) ) \AESADD \AESRND{rk}{r}$,
      and therefore use of round key
      $\AESRND{rk}{r  }$,
3)    the output ciphertext is
      $c = \AESRND{s}{Nr}$.
Note that an alternative round definition, namely
      $\AESRND {s}{r+1} = \ALG{P-layer}( \ALG{S-layer}( \AESRND{s}{r} \AESADD \AESRND{rk}{r} ) )                       $ ,
is plausible: this shifts the
 pre-whitening step {\em before} 2)
into an analogous
post-whitening step {\em  after} 2)
to yield an equivalent result.
At a  low(er) level,
the computation of each round is specified via four round functions (each of
which has an inverse, to support decryption):

\begin{itemize}

\item \AESFUNC{SubBytes}
      ~\cite[Section 5.1.1]{FIPS:197}
      operates element-wise,
      computing
      $\AESRND{s}{r+1}_{i,j} = \ALG{S-box}( \AESRND{s}{r}_{i,j} )$
      via application of the S-box:
      given an element $x$, this component can be described as
      \[
      \begin{array}{lcl}
      \ALG{S-Box} &:& \left\{\begin{array}{ccc}
                             \F_{2^8} &\rightarrow& \F_{2^8} \\
                             x        &\mapsto    & f(g(x))  \\
                             \end{array}
                      \right.
      \end{array}
      \]
      where
      $g$ is an inversion,
      and
      $f$ is a specially selected affine transformation.
      Where appropriate,
      we overload \AESFUNC{SubBytes} by allowing it to denote application
      of the S-box to {\em any} collection,
      e.g., a row, column, or, more generally, a sequence,
      of elements.

\item \AESFUNC{ShiftRows}
      ~\cite[Section 5.1.2]{FIPS:197}
      operates     row-wise,
      rotating each
      $i$-th row
      of
      $\AESRND{s}{r  }$
      by $i$ elements
      to form
      the associated row    of
      $\AESRND{s}{r+1}$,
      i.e.,
      $\AESRND{s}{r+1}_{i,j} = \AESRND{s}{r}_{i,j + i \pmod{Nb}}$.
      Where appropriate,
      we use
      \AESFUNC{ShiftRow}
      to denote
      the operation applied to a single
      row
      within \AESFUNC{ShiftRows}.

\item \AESFUNC{MixColumns}
      ~\cite[Section 5.1.3]{FIPS:197}
      operates  column-wise,
      multiplying each
      $j$-th column
      of
      $\AESRND{s}{r  }$
      with a constant MDS matrix
      to form
      the associated column of
      $\AESRND{s}{r+1}$.
      Where appropriate,
      we use
      \AESFUNC{MixColumn}
      to denote
      the operation applied to a single
      column
      within \AESFUNC{MixColumns}, i.e., multiplication of a $4$-element
      column vector by the constant MDS matrix.

\item \AESFUNC{AddRoundKey}
      ~\cite[Section 5.1.4]{FIPS:197}
      operates element-wise,
      computing
      $\AESRND{s}{r+1}_{i,j} = \AESRND{s}{r}_{i,j} \AESADD \AESRND{rk}{r}_{i,j}$
      and thereby mixing a round key into the state.

\end{itemize}

\noindent
Note that
$
\ALG{S-layer} = \AESFUNC{SubBytes} ,
$
and
\[
\ALG{P-layer} = \left\{\begin{array}{l@{\;}c@{\;}l lr}
                       \AESFUNC{MixColumns} &\circ& \AESFUNC{ShiftRows} & \mbox{in rounds} & 1 \leq r < Nr \\
                                            &     & \AESFUNC{ShiftRows} & \mbox{in round } &            Nr \\
                       \end{array}
                \right.
\]
i.e., the last, $Nr$-th round differs from the initial $Nr - 1$ rounds.  As
such, a round as defined above is constructed via
$
\AESFUNC{AddRoundKey} \circ \AESFUNC{MixColumns} \circ \AESFUNC{ShiftRows} \circ \AESFUNC{SubBytes}
$
or
$
\AESFUNC{AddRoundKey} \circ                            \AESFUNC{ShiftRows} \circ \AESFUNC{SubBytes}
$
respectively, where, because \AESFUNC{ShiftRows} and \AESFUNC{SubBytes}
commute, the order they are applied in can be selected to suit.


\documentclass[../../main.tex]{subfiles}
\begin{document}

\subsection{Rate Of}
The Operation $rateOf$ calculates the number of times something occurs
within an interval of time given a unit of time.
$$rateOf(nOccurances, start, end, unit)$$
Where the output translates to: the rate of occurrence per unit within interval
\begin{itemize}
\item $nOccurances$ is the number of times something happened and should be an Integer (called $nO?$ bellow)
\item $start$ is an ISO 8601 timestamp which serves as the first timestamp within the interval
\item $end$ is an ISO 8601 timestamp which servers as the last timestamp within the interval
\item $unit$ is a String Enum representing the unit of time
\end{itemize}
This can be seen in the definition of $rateOf$ bellow.
\begin{schema}{RateOf[\nat, TIMESTAMP, TIMESTAMP, TIMEUNIT]}
  nO? : \nat \\
  rate! : \num \\
  start?, end? : TIMESTAMP \\
  unit? : TIMEUNIT \\
  rateOf~\_ : \nat \cross TIMESTAMP \cross TIMESTAMP \cross TIMEUNIT \fun \num
  \where
  rate! = rateOf(nO?, start?, end?, unit?) @ \\
  \t1 let \ \ ~~ interval == isoToUnix(end) - isoToUnix(start) \\
  \t2 unitS == toSeconds(unit?) \\
  \t1 \ = nO? \div (interval \div units)
\end{schema}
The only other functionality required by $rateOf$ is supplied via basic arithmetic
\begin{argue}
  start = 2015-11-18T12:17:00Z \\
  end = 2015-11-18T14:17:00Z \\
  unit = second \\
  nO? = 10 \\
  \t1 startN = isoToUnix(start) = 1447849020 \\
  \t1 endN = isoToUnix(end) = 1447856220 \\
  \t1 interval = endN - StartN = 7200\\
  \t1 unitN = toSeconds(unit) = 60 \\
  0.001389 = rateOf(nO?, start, end, unit) \implies 10 \div (7200 \div 60) \\
  5 = rateOf(nO?, start, end, hour) \implies 10 \div (7200 \div 3600)
\end{argue}
\end{document}