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

\chapter{Physical Modelling}
We will model two systems:
\begin{enumerate}
	\item 2D simulator for Earth-Moon system (cartesian coordinates, center-of-mass-centered).
	\item 3D simulator for Sun-Earth-Moon system (spherical coordinates, heliocentric)
\end{enumerate}

We will derive equations of motion using Hamiltonian mechanics, shortly describe symplectic integrators and integrate the equations of motion using symplectic integrators to obtain numerical algorithms.

Finally we will proceed to describe the complexities of Earth-Mars transfer orbits, a simplified Earth-Mars transfer orbit with patched conic approximation.

\section{Analytical Mechanics}
Newton's 2nd Law is a very well known physical law of classical mechanics governing all bodies having mass. It can be stated \cite{Knudsen2002}
\begin{align}
    \label{eq:newton2}
    \textbf{Newton's 2nd Law} \qquad \sum{\vec{F}} = \dfrac{d \vec{p}}{dt}.
\end{align}

where the left-hand side is the total force on the body and the right-hand side is the time derivative of the momentum vector defined as mass times the velocity vector \(\vec{p} = m \vec{v}\).

However we will use the more powerful Hamiltonian formalism from the field of analytical mechanics, which is \cite{Knudsen2002}

\begin{align}
\textbf{Generalized momenta} \qquad p_i(\vec{q},\dot{\vec{q}}) &= \dfrac{\partial L}{\partial \dot{q_i}}, \qquad i = 1,\cdots, n \ , \label{eq:generalized-momenta} \\[1cm]
\textbf{Hamiltonian} \qquad H(\vec{q}, \vec{p}, t) &= \sum\limits_{i=1}^n p_i \dot{q_i} - L \label{eq:hamiltonian}, \\[1cm]
\textbf{Hamilton's equations} \qquad
\begin{split}
\label{eq:hamiltons-equations}
\dot{q_i} &= +\dfrac{\partial H}{\partial p_i} ,
\\[0.2cm]
\dot{p_i} &= -\dfrac{\partial H}{\partial q_i}.
\end{split}
\end{align}
where $q_i$ and $p_i$ are the coordinates and the generalized momenta respectively, one for each degree of freedom, $i$ is the index of degrees of freedom (e.g. $x, y$ for a 2D cartesian coordinates or  $r, \theta, \phi$ for a 3D spherical coordinates), $n$ is the total degrees of freedom, $\dot{q}$ is the time-derivative of $q$, $L = T - V$ is the Lagrangian with $T$ being the system's kinetic energy and $V$ it's potential energy. The Hamiltonian, \cref{eq:hamiltonian} corresponds to the total mechanical energy of the system\footnote{For most physical systems with some exceptions, see \cref{ch:HvsE} for details} and \cref{eq:hamiltons-equations} constitutes the equations of motion, and when solved give the solutions $\vec{q}(t)$, $\vec{p}(t)$.

The reason for using Hamiltonian's equations instead of Newton's is primarily that it yields $2 n$ first order differential equations of motion for $n$ degrees of freedom (dimensions), where as Newton's 2nd Law yields $n$ 2nd order differential equations. As it turns out, first-order differential equations are more straight forward to discretize, i.e. turn into a numerical algorithm that can be typed into a computer program.

For a guide on how to use Hamilton's formalism in practice, see \cref{apx:using-hamilton-mechanics}.

\section{Numerical Algorithms}
Once we have derived our equations of motion in the form of Hamilton's equations \cref{eq:hamiltons-equations}, we need to solve them. In 1887 and 1889, mathematicians Heinrich Bruns and Henri Poincaré showed that there is no general analytical solution to the three-body problem given by simple algebraic expressions and integrals \cite{Gowers2008}. This means have have to solve them numerically to get time time evolution of the coordinates and velocity for particular initial conditions and delta-v impulses.

\subsection{Symplectic Integrators}
Symplectic integrators are a class of integrators typically used in Hamiltonian systems that have the property that they preserve the energy very well over longer time. Informally, ``\textit{a symplectic map is a map which preserves the sum of areas projected onto the set of $(p_i,q_i)$ planes. It is the generalization of an area-preserving map}'' \cite{Weisstein}, as illustrated in \cref{fig:symplectic-area}.

\begin{figure}[ht]
    \centering
    \includegraphics[width=0.90\linewidth]{fig/symplectic-area.png}
    \caption{Area preservation in phase space (corresponding to preservation of the Hamiltonian, $H$), for various symplectic numerical methods for a pendulum (Image: \cite{Hairer})}
    \label{fig:symplectic-area}
\end{figure}

Thus symplectic algorithms have been employed in fields such as molecular dynamics and astronomy. ``\textit{Symplectic methods belong to the larger class of geometric numerical integration algorithms. These algorithms are constructed so that they preserve certain geometrical properties inherent in the system. Symplectic methods are so named because, when applied to problems in Hamiltonian mechanics, the algorithms preserve the linear symplectic structure inherent in the phase space representation of the dynamics.}'' \cite{Donnelly2005}. We therefore only use Symplectic integrators for our equations.

\subsubsection{Symplectic Euler (1st Order Method)}
In the explicit Euler, all quantities from the Hamiltonian derivative on the right hand refer to the old time step $i$ (index 0). In the implicit, all quantities on the RHS refers to the new time step $i+1$ (index 1), which results in implicit equations that can typically only be solved numerically.

The 1st order symplectic Euler is mixture between explicit and implicit Euler \cite{Hairer}:

\begin{equation}
    \begin{split} \label{eq:symplectic-euler1}
        \vec{q}_1 = \vec{q}_0 + h \pd{H}{\vec{p}}(q_0, p_1) \\
        \vec{p}_1 = \vec{p}_0 - h \pd{H}{\vec{q}}(q_0, p_1)
    \end{split}
\end{equation}

or

\begin{equation}
    \begin{split} \label{eq:symplectic-euler2}
        \vec{q}_1 = \vec{q}_0 + h \pd{H}{\vec{p}}(q_1, p_0) \\
        \vec{p}_1 = \vec{p}_0 - h \pd{H}{\vec{q}}(q_1, p_0)
    \end{split}
\end{equation}

\subsubsection{Symplectic Störmer-Verlet (2nd Order Method)}

The 2nd order symplectic Störmer-Verlet (henceforth simply called Verlet) is given by \cite{Hairer}:

\begin{equation}
    \begin{split} \label{eq:symplectic-verlet1}
        \vec{p}_{1/2} &= \vec{p}_0 - \frac{h}{2} \pd{H}{\vec{q}}(q_0, p_{1/2}) \\
        \vec{q}_1 &= \vec{q}_0 + \frac{h}{2} \left( \pd{H}{\vec{p}}(q_0, p_{1/2}) + \pd{H}{\vec{p}}(q_1, p_{1/2}) \right) \\
        \vec{p}_{1} &= \vec{p}_{1/2} - \frac{h}{2} \pd{H}{\vec{q}}(q_1, p_{1/2}) \\
    \end{split}
\end{equation}

or

\begin{equation}
    \begin{split} \label{eq:symplectic-verlet2}
        \vec{q}_{1/2} &= \vec{q}_0 + \frac{h}{2} \pd{H}{\vec{p}}(q_{1/2}, p_0) \\
        \vec{p}_1 &= \vec{p}_0 - \frac{h}{2} \left( \pd{H}{\vec{q}}(q_{1/2}, p_0) + \pd{H}{\vec{q}}(q_{1/2}, p_1) \right) \\
        \vec{q}_{1} &= \vec{q}_{1/2} + \frac{h}{2} \pd{H}{\vec{p}}(q_{1/2}, p_1) \\
    \end{split}
\end{equation}

\section{CM-Centered Restricted 3-Body System (C-R3B), Earth-Moon}
The center-of-mass-centered 3-body system (C-R3B) with the Earth and Moon was explored in \cite{Saxe2015}; refer to this for detailed derivations of C-R3B. We will present the final nondimensionalized equations of motion, and numerical integration algorithms for reference. The model is set up as in \cref{fig:r3b}.

\begin{figure}[ht!]
    \centering
    \includegraphics[scale=0.75]{fig/r3b.pdf}
    \caption{Restricted three-body problem. Two coordinate systems both with center of mass as origin: $(\mathscr{X},\mathscr{Y})$ is a stationary inertial frame, $(x,y)$ is a co-rotating non-inertial frame that rotates with the moon at angular frequency $\omega$. $M =$ mass of Earth, $m =$ mass of Moon, $\vec{R} =$ vector from CM to earth, $\vec{r} =$ vector from CM to Moon, $m_s =$ mass of spacecraft, $r_{S,E} =$ distance from spacecraft to Earth, $r_{S,M} =$ distance from spacecraft to Moon. In the dimensionless variables we introduce later, unit distance is $R+r$, which makes the dimensionless constant $k = \text{the CM-Earth distance} = \dfrac{R}{R+r} = $ and $1-k = \text{the CM-Moon distance} = \dfrac{r}{R+r}$ (Image: \cite{Saxe2015}).}
    \label{fig:r3b}
\end{figure}

\subsection{C-R3B Equations of Motion}

\begin{empheq}[box=\widefbox]{align}
\label{eq:Xdot}
\dot{X} = P_x + Y
\end{empheq}

\begin{empheq}[box=\widefbox]{align}
\label{eq:Ydot}
\dot{Y} = P_Y - X
\end{empheq}

\begin{empheq}[box=\widefbox]{align}
\label{eq:Pdot_X}
\dot{P}_X = P_Y - \dfrac{(1-k)(X+k)}{((X+k)^2+Y^2)^{3/2}} - \dfrac{k(X-1+k))}{((X-1+k))^2+Y^2)^{3/2}}
\end{empheq}

\begin{empheq}[box=\widefbox]{align}
\label{eq:Pdot_Y}
\dot{P}_Y = -P_X - \dfrac{(1-k)Y}{((X+k)^2+Y^2)^{3/2}} - \dfrac{k Y}{((X-1+k))^2+Y^2)^{3/2}}
\end{empheq}
where \(T\), \(X\), \(Y\), \(P_x\) and \(P_y\) are our dimensionless variables for time, positions, generalized impulse, and \(k=\dfrac{M_{\Moon}}{M_{\Earth} + M_{\Moon}}\), that is the ratio of Moon's mass to the total Earth and Moon mass.

\subsection{C-R3B Numerical Algorithms}
\subsubsection{C-R3B Symplectic Euler}
We refer to \cite{Saxe2015} for detailed derivations. The equations of motion of the other system in the next section will be derived in details. Based on \cref{eq:Xdot,eq:Ydot,eq:Pdot_X,eq:Pdot_Y}, a symplectic Euler algorithm is found:

\begin{align}
    X_1 &= \dfrac{X_0 + h (h P_{Y,0} + P_{X,0} + Y_0)}{1+h^2}, \\[0.4cm]
    Y_1 &= Y_0 + h (P_{Y,0} - X_1), \label{eq:symplectic-euler-Y_1}
\end{align}
\begin{align}
    \begin{aligned}
        P_{X,1} &= P_{X,0} \\
        &+ h \left(P_{Y,0} - \dfrac{(1-k)(k+X_1)}{((k+X_1)^2+Y_1^2)^{3/2}} + \dfrac{k(X_1-1+k)}{((X_1-1+k)^2+Y_1^2)^{3/2}}\right), \label{eq:symplectic-euler-PX_1}
    \end{aligned} \\[0.4cm]
    \begin{aligned}
        P_{Y,1} &= P_{Y,0} \\
        &+ h \left(-P_{X,0} - \dfrac{(1-k)Y_1}{((k+X_1)^2+Y_1^2)^{3/2}} - \dfrac{k Y_1}{((X_1-1+k)^2+Y_1^2)^{3/2}}\right), \label{eq:symplectic-euler-PY_1}
    \end{aligned}
\end{align}
where \(h\) is the step size, index \(0\) designates previous time step ($i$) and index \(1\) designates new time step ($i+1$). The algorithm is run in the same order as listed above.

\subsubsection{C-R3B Symplectic Störmer-Verlet}
Based on \cref{eq:Xdot,eq:Ydot,eq:Pdot_X,eq:Pdot_Y}, a symplectic Störmer-Verlet algorithm was also found (For detailed derivation, see \cite{Saxe2015}):

\begin{align}
    X_{1/2} &= \frac{h^2 P_{Y,0} + 2 h \dot{P}_{x,0} + 4 X_0}{4 + h^2} \label{eq:verlet-x_1/2} \\
    Y_{1/2} &= Y_0 + \dfrac{h}{2} (P_{Y,0} - X_{1/2}), \label{eq:verlet-y_1/2} \\
    P_{X,1} &= \frac{h^2 (2 \dot{P}_{Y,0} + P_{X,0}) + 4 h \dot{P}_{X,0} + 4 P_{X,0} }{4 + h^2} \label{eq:verlet-px_1} \\
    P_{Y,1} &= P_{Y,0} + \dfrac{h}{2} \left[-\dot{P}_Y(q_{1/2},p_0) -\dot{P}_Y(q_{1/2},p_1) \right], \label{eq:verlet-py_1} \\
    X_1 &= X_{1/2} + \dfrac{h}{2} (P_{X,1} + Y_{1/2}), \label{eq:verlet-x_1} \\
    Y_1 &= Y_{1/2} + \dfrac{h}{2} (P_{Y,1} - X_{1/2}), \label{eq:verlet-y_1}
\end{align}
where \(\dot{P}_X,\dot{P}_Y\) are \cref{eq:Pdot_X,eq:Pdot_Y}, \(h\) is the step size, index \(0\) designates previous time step ($i$) and \(1\) designates new time step ($i+1$). The algorithm is run in the same order as listed above.

\subsubsection{Adaptive Timesteps}
Furthermore the Verlet algorithm is made to take adaptive time steps. In areas relatively large accelerations, the step size is decreased and vice versa. The idea is to take both an Euler and a Verlet step, and estimate the error made in the step as the difference between the two.

Let $z$ denote a vector of the position variables $(X,y)$.
\begin{align}
\text{Euler step result:} \qquad z_1 = z + O(h), \\
\text{Verlet step result:} \qquad z_2 = z + O(h^2), \\
\end{align}
where O(h) denotes an error term of order h. Then we take the difference
\begin{align}
\|z_1 - z_2\| &= O(h) - O(h^2) \\
&\approx O(h)\ ,
\end{align}
since $O(h) \gg O(h^2)$. Thus we approximate the error difference between the Euler and Verlet method as the actual error we make at step size $h$. The idea is to make both an Euler and a Verlet step for every time-step to assess the error and adjust the step size accordingly. For every single step the step size is changed either up or down, depending on the errors and tolerance. As a result we always stay near the same error in every step, only taking as small steps as necessary in each iteration.

The non-adaptive algorithm is fixed in step size but varies in error per step.
The adaptive algorithm varies in step size in an attempt to fix the error per step.

For all simulations we have set $10^{-9}$ as the maximum tolerated error per step in the adaptive algorithm. For the non-adaptive algorithm we select a fixed step size, $10^{-6}$, to ensure that it's reasonably low most of the time. This approach was again taken directly from \cite{Saxe2015}.

\section{Heliocentric Restricted 4-Body System (H-R4B), Sun-Earth-Mars}
Simulating orbit from Earth to Mars requires a new model with at least four bodies: Sun, Earth, Mars and the Spacecraft. The coordinate system will be heliocentric and in spherical coordinates, and restricted\footnote{Meaning that the mass of the spacecraft is considered negligible compared to compared to the celestial masses and therefore is assumed not to affect them}, so we call this system the ``Heliocentric Restricted 4-Body System'', see \cref{fig:solar-system-model}. We will derive the equations of motion of the spacecraft by using Hamilton's equations, which gives us a set of coupled first-order differential equations, and a set of conserved quantities as ``generalized momenta''.

\begin{figure}[ht]
    \centering
    \includegraphics[width=0.80\linewidth]{fig/solar-system-model}
    \caption{ H-R4B model of spacecraft motion.The sun is assumed stationary at the origin of a spherical coordinate system, and the focus of two elliptical orbits of Earth and Mars. (Assets source: \cite{WikiSpherical,flaticon})}
    \label{fig:solar-system-model}
\end{figure}

\subsection{Spherical Coordinate System}
We adopt the spherical coordinate system which customary in astrodynamics, as shown in \cref{fig:solar-system-model}.

The coordinate transform equations to and from the cartesian coordinate system are:

\begin{align}
    x &= r \sin{\theta}\cos{\phi}, \label{eq:x(q)} \\
    y &= r \sin{\theta}\sin{\phi}, \label{eq:y(q)}\\
    z &= r \cos{\theta}, \label{eq:z(q)}
\end{align}

and

\begin{align}
    r &= \sqrt{x^2 + y^2 + z^2}, \label{eq:r(x,y,z)}\\
    \theta &= \arccos{\frac{z}{r}}, \label{eq:theta(x,y,z)}\\
    % \arctan{\frac{y}{x}}, \qquad \theta  \in [0, 2\pi]
    \phi &=
    \arctantwo{(y/x)} \\
    \label{eq:phi(x,y,z)}
\end{align}

where \(r \in [0, \infty]\), \(\theta \in [0, \pi]\) and \(\phi \in [0, 2\pi]\) and \(\arctan2\) denotes the two-argument version of \(\arctan\) function \footnote{\(\arctan{(a)} \in (-\frac{\pi}{2}, \frac{\pi}{2})\) just takes a slope as input and give an angle in quadrant 1 and 4 only, whereas \(\arctantwo{(y,x)} \in (-\pi, \pi]\) takes both \(x\) and \(y\) which also gives the correct quadrant 1–4.}\cite{WikiAtan2}.

For the calculation of kinetic in the next section we will need the position vector, unit vectors in spherical coordinates, and the Jacobians.

The position vector in spherical coordinates is
\begin{align}
    \vec{r}(r, \theta, \phi) &= x \xhat + y \yhat + z \zhat \\
    \Leftrightarrow \vec{r}(r, \theta, \phi) &= r \sin{\theta}\cos{\phi} \xhat + r \sin{\theta}\sin{\phi} \yhat + r \cos{\theta} \zhat. \label{eq:position-vec-spherical}
\end{align}

The various coordinate derivatives of the position vector is
\begin{align}
    \pd{\vec{r}}{r} &= \sin{\theta}\cos{\phi} \xhat + \sin{\theta}\sin{\phi} \yhat + \cos{\theta} \zhat \quad &\text{and} \quad &\left| \pd{\vec{r}}{r} \right| = 1 \label{eq:position-derived-r} \\
    \pd{\vec{r}}{\theta} &= r \cos{\theta}\cos{\phi} \xhat + r \cos{\theta}\sin{\phi} \yhat - r \sin{\theta} \zhat \quad &\text{and} \quad &\left| \pd{\vec{r}}{\theta} \right| = r \label{eq:position-derived-theta} \\
    \pd{\vec{r}}{\phi} &= -r\sin{\theta}\sin{\phi} \xhat + r\sin{\theta}\cos{\phi} \zhat \quad &\text{and} \quad &\left| \pd{\vec{r}}{\phi} \right| = r\sin{\theta} \label{eq:position-derived-phi}
\end{align}

So the unit vectors in spherical coordinates are
\begin{align}
    \rhat = \dfrac{\pd{\vec{r}}{r}}{\left| \pd{\vec{r}}{r} \right| } &= \sin{\theta}\cos{\phi} \xhat + \sin{\theta}\sin{\phi} \yhat + \cos{\theta} \zhat \label{eq:r-hat}\\
    \thetahat = \dfrac{\pd{\vec{r}}{\theta}}{\left| \pd{\vec{r}}{\theta} \right| } &= \cos{\theta}\cos{\phi} \xhat + \cos{\theta}\sin{\phi} \yhat - \sin{\theta} \zhat \label{eq:theta-hat}\\
    \phihat = \dfrac{\pd{\vec{r}}{\phi}}{\left| \pd{\vec{r}}{\phi} \right| } &= -\sin{\phi} \xhat + \cos{\phi} \zhat \label{eq:phi-hat}
\end{align}

\subsection{H-R4B Equations of Motion}
We will follow the 5-step process of arriving at Hamilton's equations outlined in \cref{apx:using-hamilton-mechanics}.

\subsubsection{Step 1: Lagrangian \(L\)}
The kinetic energy of the system is
\begin{align}
    T = \frac{1}{2} m_s v^2.
\end{align}

In general the total derivative of a vector is:

\begin{align}
    \vec{v} &= \od{\vec{r}}{t} = \sum\limits_{j} \pd{\vec{r}}{q_j} \od{q_j}{t}, \\
      &= \sum\limits_{j} \left|\pd{\vec{r}}{q_j}\right| \frac{\pd{\vec{r}}{q_j}}{\left|\pd{\vec{r}}{q_j}\right|} \od{q_j}{t}, \\
      &= \sum\limits_{j} \left|\pd{\vec{r}}{q_j}\right| \od{q_j}{t} \unitvector{q}_j, \\
      &= \sum\limits_{j} \left|\pd{\vec{r}}{q_j}\right| \dot{q_j} \unitvector{q}_j,
    \end{align}

where we have used that a unit vector for any coordinate system is \(\frac{\pd{\vec{r}}{q_j}}{\left|\pd{\vec{r}}{q_j}\right|} = \unitvector{q}_j\).
We can now use the derivatives of the position vector we found earlier in \cref{eq:position-derived-r,eq:position-derived-theta,eq:position-derived-phi}, to substitute for \(\left|\pd{\vec{r}}{q_j}\right|\). We don't have to substitute in \(\unitvector{q}_j\) since our unit vectors are orthogonal, so they will yield either 0 or 1 when we square \(v\). So for the spherical coordinates we get:

\begin{align}
    \vec{v} = \dot{r}\rhat + r\dot{\theta}\thetahat + r\sin{\theta}\,\dot{\phi}\phihat
\end{align}

so

\begin{align}
    v^2 = \dot{r}^2 + r^2\dot{\theta}^2 + r^2\sin^2{\theta}\,\dot{\phi}^2
\end{align}

so we get

\begin{align}
    T = \frac{1}{2} m_s (\dot{r}^2 + r^2\dot{\theta}^2 + r^2\sin^2{\theta}\,\dot{\phi}^2).
\end{align}

The gravitational potential is \cite{WikiGravPotential}

\begin{align}
    V = -G m_s \sum\limits_{k} \frac{M_k}{\left| \vec{r} - \vec{r_k} \right|},
\end{align}
where \(i\) denotes the celestial bodies acting on the spacecraft, i.e. in our system Sun, Earth and Mars.

So we finally get the Lagrangian

\begin{align}
    L &= T - V \\
    \Leftrightarrow L &= \frac{1}{2} m_s (\dot{r}^2 + r^2\dot{\theta}^2 + r^2\sin^2{\theta}\,\dot{\phi}^2) + G m_s \sum\limits_{k} \frac{M_k}{\left| \vec{r} - \vec{r_k} \right|}
\end{align}

\subsubsection{Step 2: Generalized Momenta \(p_j\)}
\begin{align}
    p_r &= \pd{L}{\dot{r}} = m_s \dot{r} \label{eq:pr} \\
    p_\theta &= \pd{L}{\dot{\theta}} = m_s r^2 \dot{\theta} \label{eq:ptheta} \\
    p_\phi &= \pd{L}{\dot{\phi}} = m_s r^2 \sin^2{\theta} \dot{\phi} \label{eq:pphi}
\end{align}

We recognize the three generalized momenta as linear momentum (\cref{eq:pr}) and angular momentum (\cref{eq:ptheta,eq:pphi}), respectively.

\subsubsection{Step 3: \(\dot{q} = \dot{q}_j(\vec{q}, \vec{p}, t)\)}
\begin{align}
    \dot{r} &= \frac{p_r}{m_s} \\
    \dot{\theta} &= \frac{p_\theta}{m_s r^2} \\
    \dot{\phi} &= \frac{p_\phi}{m_s r^2 \sin^2{\theta}}
\end{align}

We can now rewrite \(L\) to make it independent of \(\dot{q}\) by substituting the expressions above:

\begin{align}
    T &= \frac{1}{2} m_s (\dot{r}^2 + r^2\dot{\theta}^2 + r^2\sin^2{\theta}\,\dot{\phi}^2) \\
    &= \frac{1}{2} m_s \left(\frac{p_r^2}{m_s^2} + r^2\frac{p_\theta^2}{m_s^2 r^4} + r^2\sin^2{\theta}\frac{p_\phi^2}{m_s^2 r^4 \sin^4{\theta}} \right) \\
    &= \frac{p_r^2}{2 m_s} + \frac{p_\theta^2}{2 m_s r^2} + \frac{p_\phi^2}{2 m_s r^2 \sin^2{\theta}} \\
\end{align}

So using \(L = T - V\) we have

\begin{align}
    L = \frac{p_r^2}{2 m_s} + \frac{p_\theta^2}{2 m_s r^2} + \frac{p_\phi^2}{2 m_s r^2 \sin^2{\theta}} + G m_s \sum\limits_{k} \frac{M_k}{\left| \vec{r} - \vec{r_k} \right|}
\end{align}

\subsubsection{Step 4: Hamiltonian \(H\)}
\begin{align}
    H(\vec{q}, \vec{p}, t) &= \sum\limits_{j}p_j \dot{q_j} - L \\
    &= \sum\limits_{j}p_j \dot{q_j}(\vec{q}, \vec{p}, t) - L \\
    &= p_r \frac{p_r}{m_s} + p_\theta \frac{p_\theta}{m_s r^2} + p_\phi + \frac{p_\phi}{m_s r^2 \sin^2{\theta}} \notag \\
    &- \left( \frac{p_r^2}{2 m_s} + \frac{p_\theta^2}{2 m_s r^2} + \frac{p_\phi^2}{2 m_s r^2 \sin^2{\theta}} + G m_s \sum\limits_{k} \frac{M_k}{\left| \vec{r} - \vec{r_k} \right|} \right) \\
    \Leftrightarrow H &= \frac{p_r^2}{2 m_s} + \frac{p_\theta^2}{2 m_s r^2} + \frac{p_\phi^2}{2 m_s r^2 \sin^2{\theta}} - G m_s \sum\limits_{k} \frac{M_k}{\left| \vec{r} - \vec{r_k} \right|}
\end{align}

At this point we want to expand the expression \(\left| \vec{r} - \vec{r_k} \right|\) in anticipation of needing to differentiate \(H\) with respect to the coordinates. Using the equations for the \(x, y, z\) coordinates \cref{eq:x(q),eq:y(q),eq:z(q)} we get

\begin{align}
    d_k = \left|\vec{r} - \vec{r}_k \right| &= \sqrt{(x-x_k)^2 + (y-y_k)^2 + (z-z_k)^2}
\end{align}
and (colors added to show which terms factor into each other in next step)
\begin{align}
    (x-x_k)^2 &= (r\sin{\theta}\cos{\phi} - r\sin{\theta}\cos{\phi})^2 \notag \\
    &= \tikz[baseline]{
        \node[fill=orange!20,anchor=base]
        {\(r^2\sin^2{\theta}\cos^2{\phi}\)}
    } + \tikz[baseline]{
        \node[fill=red!20,anchor=base]
        {\(r_k^2\sin^2{\theta_k}\cos^2{\phi_k}\)}
    } - 2r r_k \sin{\theta}\sin{\theta_k}\cos{\phi}\cos{\phi_k} \label{eq:x-dist-squared} \\
    (y-y_k)^2 &= (r\sin{\theta}\sin{\phi} - r\sin{\theta}\sin{\phi})^2 \notag \\
    &= \tikz[baseline]{
        \node[fill=orange!20,anchor=base]
        {\(r^2\sin^2{\theta}\sin^2{\phi}\)}
    } + \tikz[baseline]{
        \node[fill=red!20,anchor=base]
        {\(r_k^2\sin^2{\theta_k}\sin^2{\phi_k}\)}
    } - 2r r_k \sin{\theta}\sin{\theta_k}\sin{\phi}\sin{\phi_k} \label{eq:y-dist-squared} \\
    (z-z_k)^2 &= (r\cos{\theta} - r\cos{\theta})^2 \notag \\
    &= \tikz[baseline]{
        \node[fill=orange!20,anchor=base]
        {\(r^2\cos^2{\theta}\)}
    } + \tikz[baseline]{
        \node[fill=red!20,anchor=base]
        {\(r_k^2\cos^2{\theta_k}\)}
    } - 2r r_k \cos{\theta}\cos{\theta_k}. \label{eq:z-dist-squared}
\end{align}
Now adding all three \cref{eq:x-dist-squared,eq:y-dist-squared,eq:z-dist-squared} the orange terms factor into \(r^2\) and the red terms factor into \(r_k^2\) and we get

\begin{align}
    d_k &= \sqrt{
        \tikz[baseline]{\node[fill=orange!20,anchor=base]{\(r^2\)}}
        + \tikz[baseline]{\node[fill=red!20,anchor=base]{\(r_k^2\)}}
        -2r r_k(\cos{\theta}\cos{\theta_k}+\sin{\theta}\sin{\theta_k}(
            \tikz[baseline]{\node[fill=blue!20,anchor=base]{
                \(\cos{\phi}\cos{\phi_k} + \sin{\phi}\sin{\phi_k}\)}}
        ))
    } \\
    &= \sqrt{r^2 + r_k^2 - 2 r r_k(\cos{\theta}\cos{\theta_k}+\sin{\theta}\sin{\theta_k}\cos{(\phi - \phi_k}))},
\end{align}
where the blue factor was simplified using the sum rule: \\
\(\cos{\alpha}\cos{\beta} + \sin{\alpha}\sin{\beta} = cos{(\alpha-\beta)}\) \cite{WeissteinTrig}.

So we can finally express \(H\) as
\begin{equation}
    \begin{aligned}
        H &= \frac{p_r^2}{2 m_s} + \frac{p_\theta^2}{2 m_s r^2} + \frac{p_\phi^2}{2 m_s r^2 \sin^2{\theta}} \\
        &- G m_s \sum\limits_{k} \frac{M_k}{\sqrt{r^2 + r_k^2 - 2 r r_k \left[\cos{\theta}\cos{\theta_k}+\sin{\theta}\sin{\theta_k}\cos{(\phi - \phi_k})\right]}}
    \end{aligned}
\end{equation}

\subsubsection{Step 5: Hamilton's Equations}
\begin{align}
    \dot{r} = \pd{H}{p_r} &= \frac{p_r}{m_s} \label{eq:rdot} \\[0.3cm]
    \dot{\theta} = \pd{H}{p_\theta} &= \frac{p_\theta}{m_s r^2} \label{eq:thetadot} \\[0.3cm]
    \dot{\phi} = \pd{H}{p_\phi} &= \frac{p_\phi}{m_s r^2 \sin^2{\theta}} \label{eq:phidot}  \\[0.3cm]
    \begin{split}
        \dot{p}_r = -\pd{H}{r} &= \frac{p_\theta^2}{m_s r^3} + \frac{p_\phi^2}{m_s r^3 \sin^2{\theta} } \\
        &+ G m_s \sum\limits_{k} M_k \frac{-r + r_k \left(\cos{\theta}\cos{\theta_k} + \sin{\theta}\sin{\theta_k}\cos{(\phi - \phi_k)}\right)}{\left[r^2 + r_k^2 - 2 r r_k \left(\cos{\theta}\cos{\theta_k} + \sin{\theta}\sin{\theta_k}\cos{(\phi - \phi_k)} \right) \right]^{3/2}} \label{eq:prdot}
    \end{split} \\[0.3cm]
    \begin{split}
        \dot{p}_\theta = -\pd{H}{\theta} &= \frac{p_\phi^2}{m_s r^2 \sin^2{\theta} \tan{\theta}} \\
        &+ G m_s \sum\limits_{k} M_k \frac{r r_k \left[-\sin{\theta}\cos{\theta_k} + \cos{\theta}\sin{\theta_k}\cos{(\phi - \phi_k)} \right]}{\left[r^2 + r_k^2 - 2 r r_k \left(\cos{\theta}\cos{\theta_k} + \sin{\theta}\sin{\theta_k}\cos{(\phi - \phi_k)} \right) \right]^{3/2}} \label{eq:pthetadot}
    \end{split} \\[0.3cm]
    \begin{split}
        \dot{p}_\phi = -\pd{H}{\phi} &= G m_s \sum\limits_{k} M_k \frac{- r r_k \sin{\theta}\sin{\theta_k}\sin{(\phi - \phi_k)}}{\left[r^2 + r_k^2 - 2 r r_k \left(\cos{\theta}\cos{\theta_k} + \sin{\theta}\sin{\theta_k}\cos{(\phi - \phi_k)} \right) \right]^{3/2}} \label{eq:pphidot}
    \end{split}
\end{align}

Those are our equations of motion. However before we try to solve them, we will remove all units.

\subsection{H-R4B Equations Nondimensionalized}
We will now choose suitable characteristic units, which has a number of benefits:
\begin{itemize}
    \item The equations gets slightly simplified
    \item The order of the effects of different forces in the system becomes more apparent.
    \item Many of the calculations in numerical algorithms happens at an order of about 1, which is desireable for decreasing round-off error due to finite machine precision.
\end{itemize}

The characteristic units are chosen as:
\begin{align}
    \text{Unit length: } k_r &= a_{\Earth}\ {\color{gray} \approx \SI{1.50e8}{\km}\ \text{(1 Earth orbit semi-major axis)}}  \\[0.2cm]
    \text{Unit time: } k_t &= T_{\Earth} = \frac{2\pi}{\omega_\Earth} = 2\pi \sqrt{\frac{a_\Earth^3}{G M_\Sun}}\ {\color{gray} \approx \SI{3.16e7}{\s}\ \text{ (1 year)}} \\[0.2cm]
    \text{Unit speed: } k_v &= \frac{k_r}{k_t} = \frac{a_\Earth \omega_\Earth}{2\pi} = \frac{1}{2\pi} \sqrt{\frac{G M_\Sun}{a_\Earth}}\ {\color{gray} \approx \SI{4.74}{\km/\s}\ \text{(1 AU/y)}}
\end{align}
where \(a_\Earth\) is the semi-major axis of Earth's orbit in kilometers, see \cref{fig:earth-semi-major-axis}, \(T_\Earth\) is Earth's orbital period (i.e. 1 year) in seconds and the characteristic speed thus becomes Earth's average orbital speed with respect to the sun.

We don't need a characteristic mass \(m_s\) since the mass cancels out in the equations of motion for the quantity we care about, delta-v.

The unit for time is expressed in seconds because we found heuristically we needed time steps on the order of \(\SI{1}{\s}\) to maintain an error per step of about \num{10e-9}.

The unit for length is expressed in kilometers because it is customary to use \si{\km/\s} as unit of speed for celestial objects.

\begin{figure}[ht]
    \centering
    \includegraphics[width=0.60\linewidth]{fig/earth-semi-major-axis}
    \caption{Semi-major axis of Earth's elliptical orbit, used as the characteristic length of the system}
    \label{fig:earth-semi-major-axis}
\end{figure}

\clearpage

The nondimensionalization is done in \cref{apx:hr4b-nondimensionalization}. The resulting equations and the nondimensionalized Hamiltonian is:

\begin{equation} \tag{2.67}
    \boxed{
            \dot{R} = B_R
    }
\end{equation}

\begin{equation} \tag{2.72}
    \boxed{
            \dot{\theta} = \frac{B_\theta}{R^2}
    }
\end{equation}

\begin{equation} \tag{2.76}
    \boxed{
            \dot{\phi} = \frac{B_\phi}{R^2 \sin^2{\theta}}
    }
\end{equation}

\begin{equation} \tag{2.86}
    \boxed{
        \!\begin{aligned}
            \dot{B}_r = &\frac{B_\theta^2}{R^3} + \frac{B_\phi^2}{R^3 \sin^2{\theta}} \\
            & + \sum\limits_{k} \eta_k \frac{-R + R_k \left(\cos{\theta}\cos{\theta_k} + \sin{\theta}\sin{\theta_k}\cos{(\phi - \phi_k)}\right)}{\left[R^2 + R_k^2 - 2 R R_k \left(\cos{\theta}\cos{\theta_k} + \sin{\theta}\sin{\theta_k}\cos{(\phi - \phi_k)} \right) \right]^{3/2}}
        \end{aligned}
    }
\end{equation}

\begin{equation} \tag{2.87}
    \boxed{
        \!\begin{aligned}
            \dot{B}_\theta = &\frac{B_\phi^2}{R^2 \sin^2{\theta} \tan{\theta}} \\
            &+ \sum\limits_{k} \eta_k \frac{R R_k \left[-\sin{\theta}\cos{\theta_k} + \cos{\theta}\sin{\theta_k}\cos{(\phi - \phi_k)} \right]}{\left[R^2 + R_k^2 - 2 R R_k \left(\cos{\theta}\cos{\theta_k} + \sin{\theta}\sin{\theta_k}\cos{(\phi - \phi_k)} \right) \right]^{3/2}}
        \end{aligned}
    }
\end{equation}

\begin{equation} \tag{2.88}
    \boxed{
        \!\begin{aligned}
            \dot{B}_\phi = &\sum\limits_{k} \eta_k \frac{- R R_k \sin{\theta}\sin{\theta_k}\sin{(\phi - \phi_k)}}{\left[R^2 + R_k^2 - 2 R R_k \left(\cos{\theta}\cos{\theta_k} + \sin{\theta}\sin{\theta_k}\cos{(\phi - \phi_k)} \right) \right]^{3/2}}
        \end{aligned}
    }
\end{equation}

\vspace{0.2cm}
where we renamed $p$ to $B$ because we divided by spacecraft mass $m_s$ on both sides, thus $\dot{B}_r$ is now linear velocity along r-coordinate and $\dot{B}_\theta$ and $\dot{B}_\phi$ are now angular velocities.

For reference, the nondimensionalized Hamiltonian per spacecraft mass:

\begin{equation} \tag{2.105}
    \begin{aligned}
        \mathcal{H}_m &= \frac{B_r^2}{2} + \frac{B_\theta^2}{2 R^2} + \frac{B_\phi^2}{2 R^2 \sin^2{\theta}} \\
        &- \sum\limits_{k} \eta_k \frac{1}{\sqrt{R^2 + R_k^2 - 2 R R_k \left[\cos{\theta}\cos{\theta_k}+\sin{\theta}\sin{\theta_k}\cos{(\phi - \phi_k})\right]}}
    \end{aligned}
\end{equation}

\subsection{H-R4B Numerical Integration Algorithm}

\subsubsection{Symplectic Euler for H-R4B System}

One can choose whichever of the two Symplectic Euler versions \cref{eq:symplectic-euler1,eq:symplectic-euler2} is easier to solve for the equations at hand. We choose the latter form \cref{eq:symplectic-euler2} so with our Hamiltonian \cref{eq:HH_m} we get
\begin{align}
    R_1 &= R_0 + h B_{r,0} \label{eq:symplectic-euler-R_1}, \\[0.4cm]
    \theta_1 &= \theta_0 + h \frac{B_{\theta,0}}{R_1^2}, \label{eq:symplectic-euler-theta_1} \\[0.4cm]
    \phi_1 &= \phi_0 + h \frac{B_{\phi,0}}{R_1^2 \sin^2{\theta_1}}, \label{eq:symplectic-euler-phi_1}
\end{align}
\begin{align}
    \begin{aligned}
        B_{r,1} = & B_{r,0} + h \left[ \frac{B_{\theta,0}^2}{R_1^3} + \frac{B_{\phi,0}^2}{R_1^3 \sin^2{\theta_1}} \right. \\
        & \hspace{-21.0pt} + \left. \sum\limits_{k} \eta_k \frac{-R_1 + R_{k,1} \left(\cos{\theta_1}\cos{\theta_{k,1}} + \sin{\theta_1}\sin{\theta_{k,1}}\cos{(\phi_1 - \phi_{k,1})}\right)}{\left[R_1^2 + R_{k,1}^2 -2 R_1 R_{k,1}^2 \left(\cos{\theta_1}\cos{\theta_{k,1}} + \sin{\theta_1}\sin{\theta_{k,1}}\cos{(\phi_1 - \phi_{k,1})} \right) \right]^{3/2}} \right] , \label{eq:symplectic-euler-Br_1}
    \end{aligned} \\[0.4cm]
    \begin{aligned}
        B_{\theta,1} = & B_{\theta,0} + h \left[ \frac{B_{\phi,0}^2}{R_1^2 \sin^2{\theta_1} \tan{\theta_1}} \right. \\
        & \hspace{-21.0pt} + \left. \sum\limits_{k} \eta_k \frac{R_1 R_{k,1} \left[-\sin{\theta_1}\cos{\theta_{k,1}} + \cos{\theta_1}\sin{\theta_{k,1}}\cos{(\phi_1 - \phi_{k,1})} \right]}{\left[R_1^2 + R_{k,1}^2 -2 R_1 R_{k,1}^2 \left(\cos{\theta_1}\cos{\theta_{k,1}} + \sin{\theta_1}\sin{\theta_{k,1}}\cos{(\phi_1 - \phi_{k,1})} \right) \right]^{3/2}} \right], \label{eq:symplectic-euler-Btheta_1}
    \end{aligned} \\[0.4cm]
    \begin{aligned}
        B_{\phi,1} = & B_{\phi,0} + h \left[ \vphantom{\frac12} \right.\\
        & \hspace{-21.0pt} \hphantom{+} \left. \sum\limits_{k} \eta_k \frac{- R_1 R_{k,1} \sin{\theta_1}\sin{\theta_{k,1}}\sin{(\phi_1 - \phi_{k,1})}}{\left[R_1^2 + R_{k,1}^2 -2 R_1 R_{k,1}^2 \left(\cos{\theta_1}\cos{\theta_{k,1}} + \sin{\theta_1}\sin{\theta_{k,1}}\cos{(\phi_1 - \phi_{k,1})} \right) \right]^{3/2}} \right]. \label{eq:symplectic-euler-Bphi_1}
    \end{aligned}
\end{align}
As we can see, we are lucky with this Hamiltonian since we can simply run all steps in the order above, \cref{eq:symplectic-euler-R_1,eq:symplectic-euler-theta_1,eq:symplectic-euler-phi_1,eq:symplectic-euler-Br_1,eq:symplectic-euler-Btheta_1,eq:symplectic-euler-Bphi_1} without needing to solve any implicit equations.

\subsubsection{Symplectic Verlet for H-R4B System}

These equations of motion follow the procedures of \cref{eq:symplectic-verlet2}. This algorithm was derived and implemented, but not thoroughly tested. In the interest of time and space, we refer to \cref{apx:symplectic-verlet-derivations} for derivation and equations.

\section{Interplanetary Transfer Orbits}

\subsection{Transfer Orbit to Mars: 2D Patched Conic Approximation} \label{sec:2d-patched-conic}
Get get a sense of the initial conditions that will bring us on a transfer orbit from Earth to Mars, we are interested in the following key numbers:
\begin{enumerate}
	\item Delta-v for Mars Orbit Insertion (MOI) at Earth.
	\item Delta-v for arrival into circular orbit at Mars.
	\item Relative positions of Earth and Mars for optimal Hohmann trajectory.
	\item Travel time for optimal Hohmann trajectory.
\end{enumerate}

Knowing this we have a first good guess on initial conditions to put into the interplanetary 3D simulator, for which we can search for optimal parameters nearby for some good low-delta-v trajectories to mars. To approximate these numbers we can use the \emph{patched conic approximation}.

First we recall that there are four possible orbit types in a two-body system that are all conic sections: circular, elliptic, parabolic and hyperbolic (and the circular is actually a special case of the elliptic), see \cref{fig:conics-and-orbits}.

\begin{figure}[ht]
    \centering
    \subfloat[Conic sections. The parabola is created from an intersecting plane parallel to the cone (Image: \cite{MagisterMathematicae} (modified)).]{
        \includegraphics[width=0.47\linewidth]{fig/Conic_Sections2.pdf}
        \label{fig:conic-sections}
    }
    \hfill
    \subfloat[Examples of the four orbit types of a two-body system and their eccentricities (Image: \cite{Seahen} (modified)).]{
        \includegraphics[width=0.40\linewidth]{fig/Eccentricity.pdf}
        \label{fig:orbit-types}
    }
    \caption{The four types of conic sections, corresponding to the four kinds of orbits for a two-body gravitational system. Some authors count three types, with the circle being a special case of the ellipse.}
    \label{fig:conics-and-orbits}
\end{figure}

These four orbit types are characterized by the orbit specific energy (mechanical energy per mass) or equivalently by the ranges of eccentricity, see table in \cref{tab:orbit-type-properties}. As the table shows, a negative mechanical energy corresponds to a closed orbit (either circular or elliptical), zero energy corresponds to a parabolic orbit, i.e. an escape velocity orbit that occurs when a satellite is moving with \emph{just} enough speed with respect to a central body that it's speed goes towards zero at infinity. Finally, positive mechanical energy corresponds to hyperbolic orbits.

\begin{table}[tbp]
    \centering
    \begin{tabular}{@{}llll@{}}
    \toprule
    Conic Section & Eccentricity    & Semi-major axis & Energy  \\ \midrule
    Circle        & $0$             & = radius        & $<0$    \\
    Ellipse       & $0 < e < 1$     & $>0$            & $<0$    \\
    Parabola      & 1               & infinity        & $0$     \\
    Hyperbola     & $>1$            & $<0$            & $>0$    \\ \bottomrule
    \end{tabular}
    \caption{Properties of the four orbit types, characterized by their specific energy and eccentricity (Source: \cite{Braeunig}).}
    \label{tab:orbit-type-properties}
\end{table}

For a interplanetary transfer orbit we can use the Hohmann transfer orbit as the ``base transfer orbit'' and make some modifications. We recall that the Hohmann transfer is an elliptical orbit that brings a satellite between two different circular orbits around a central body by applying an instantaneous burn twice as depicted back in \cref{fig:hohmann}. This assumes only one central body. However in our system, we have three central bodies of interest: Earth, Sun and Mars. Each is the dominant body at various points of the journey. We start in a circular orbit around Earth, burn into an elliptical orbit around the Sun and arrive to Mars, burning to a circular orbit again. We can model the transfer orbit to Mars by patching three conic sections:

\begin{enumerate}
	\item Hyperbolic departure orbit (Geocentric reference frame).
	\item Elliptical transfer orbit (Heliocentric reference frame).
	\item Hyperbolic arrival orbit (Mars-centric reference frame).
\end{enumerate}
\cref{fig:Hohmann-to-mars-heliocentric} show necessary speeds on Hohmann orbit and the orbit speed of Earth and Mars.
\begin{figure}[ht]
    \centering
    \includegraphics[width=0.7\linewidth]{fig/Hohmann-to-mars-heliocentric.png}
    \caption{Hohmann transfer to Mars with necessary departure speed (periapsis at Earth) and arrival speed (apoapsis at Mars) on Hohmann orbit relative to Sun (Image: \cite[p.~127]{Rapp2016} (modified)).}
    \label{fig:Hohmann-to-mars-heliocentric}
\end{figure}
Why are the departure and arrival orbits modelled as hyperbolic? We need extra speed from LEO to enter a transfer orbit that intersects Mars, meaning we need some velocity in excess to escape velocity. Likewise we \emph{always} arrive at other bodies in a hyperbolic orbit as seen from that body; if we started with zero speed approaching from infinitely far away and waited for infinity, we would get the escape velocity when reaching the planet (this is just escaping with the exact escape velocity in reverse). But since we in practice always come speed already, relative to the body we approach (here Mars), we arrive with speed in excess to the escape velocity, hence along a hyperbolic orbit, as shown in table in \cref{tab:orbit-type-properties}.

As \cref{fig:Hohmann-to-mars-heliocentric} shows, we arrive at Mars' orbit with less speed than Mars \emph{if Mars wasn't there}, but actually need to slow down due to the attraction of Mars that results in higher speed to necessary for a close circular orbit at target altitude \SI{125}{\km}. Thus overall we get a hyperbolic departure orbit, patched with an elliptical orbit, patched with another hyperbolic arrival orbit, with required speeds as illustrated in figure \cref{fig:Hohmann-to-mars-geocentric-areocentric}.

\begin{figure}[ht]
    \centering
    \includegraphics[width=0.7\linewidth]{fig/Hohmann-to-mars-geocentric-areocentric.png}
    \caption{Hohmann transfer to Mars with necessary departure, arrival and parking speeds relative to Earth on the left and Mars on the right (Image: \cite[p.~133]{Rapp2016} (modified)).}
    \label{fig:Hohmann-to-mars-geocentric-areocentric}
\end{figure}

All the numbers of \cref{fig:Hohmann-to-mars-heliocentric,fig:Hohmann-to-mars-geocentric-areocentric} are derived in \cref{apx:mars-hohmann-derivations}, answering our four questions from the beginning of the chapter:

\begin{enumerate}
	\item \SI{3.62}{\km\per\s} needed to depart Earth circular orbit at altitude 160 km on Mars Orbit Insertion (MOI).
	\item \SI{-2.11}{\km\per\s} needed at Mars arrival for circular orbit at \SI{125}{\km} altitude. Just for illustration, we could also apply less burn at Mars at the expense of entering a highly elliptical orbit as \cref{fig:mars-arrival-orbit} shows.
	\item Mars must be $44^\degree$ behind Earth at MOI in order for Mars to be at Hohmann orbit apoapsis at the same time, see \cref{fig:Hohmann-angle}. This happens about every 26 months.
	\item Travel time for optimal Hohmann trajectory is around 260 days, or around 8.5 months.
\end{enumerate}

These numbers are an important approximate reference point to have when attempting to find low energy transfer orbits to Mars.

\begin{figure}[ht]
    \centering
    \includegraphics[width=0.35\linewidth]{fig/Hohmann-angle.png}
    \caption{The angle that Mars travels during the Mars Hohmann transfer orbit can easily be calculated as roughly \(136^\degree\), meaning the optimal launch is when Mars is \(44^\degree\) ahead of Earth in orbit (Image: \cite{Stern}).}
    \label{fig:Hohmann-angle}
\end{figure}

\begin{figure}[ht]
    \centering
    \includegraphics[width=0.85\linewidth]{fig/mars-arrival-orbit.png}
    \caption{Various closed orbits upon arrival depending on applied $\Delta v$ (Image: \cite[p.~137]{Rapp2016} (modified)).}
    \label{fig:mars-arrival-orbit}
\end{figure}

\clearpage

\subsection{Earth-Mars Transfer in 3D}
\subsubsection{Ignored Complications}
In the section above there are a number of complications that we have ignored. For example, Earth and Mars have different eccentricities (0.0167 vs. 0.0934) meaning that its angular velocity changes considerably depending on whether it is near perihelion or aphelion at the time of transfer. For a better estimate we can no longer consider the orbit to be circular. \cite{Braeuniga} gives a great analysis of the Earth-Mars Hohmann transfer orbit in 3D.

\subsubsection{Launch into The Right Plane – Actual Hohmann Transfer Orbit in 3D}
In the previous section we detailed the important patched conic sections model in 2D. In our 3D simulator we will attempt something similar, but take the inclination of Mars' orbital plane into account.

Mars orbital inclination is $1.85^\degree$ to the ecliptic plane. Therefore if one naively tried to rendezvous with Mars in the ecliptic plane it would be \( 1.52\ \text{AU} \cdot 1.85^\degree \pi / 180 = \SI{7.4e6}{\km} \) away. Instead we will attempt a 3D Hohmann transfer orbit as illustrated in \cref{fig:hohmann-transfer-orbit-3D} using the same launch parameters as was found in the 2D hohmann model in \cref{sec:2d-patched-conic}, but allowing the \(\phi\) angle to vary a bit to get slightly out of the plane.

\begin{figure}[ht]
    \centering
    \includegraphics[width=0.80\linewidth]{fig/hohmann-transfer-orbit-3D.png}
    \caption{Hohmann orbit in a 3D model needs a small $\Delta v$ in the $\phi$ direction to get out of the plane in such a way that it will be in the orbital plane at Mars at apoapsis (Image: \cite{Daedalis.de})}
    \label{fig:hohmann-transfer-orbit-3D}
\end{figure}


%!TEX root = ../dokumentation.tex

\chapter{Background and Theory}\label{cha:Background}

This chapter introduces the technologies and background that will be utilized in the following chapters. First, an introduction into formal grammars is given and the \acf{BNF} is described. Following that, the \ac{TPTP} language is introduced. Then the foundations of lexing and parsing are outlined. After that, Python and relevant Python packages that are used in the implementation of \ac{Synplifier} are presented.

\section{Formal languages}\label{sec:BackgroundFormalLanguage}

This section introduces terms and concepts in the area of formal languages that are necessary to understand \ac{Synplifier}.

\subsection{Alphabet}
An alphabet is a finite, non-empty set of symbols usually represented by the uppercase sigma $\sum$.
An example is the binary alphabet $\sum = \{0,1\}$. \cite{AutomataTheory.2007}

\subsection{String}
A string, also called word, is a finite sequence of symbols from some alphabet. For example, the string \textit{101} is a string from the binary alphabet $\sum = \{0,1\}$.
The set of all strings over an alphabet $\sum$ is denoted as $\sum ^{*}$. \cite{AutomataTheory.2007}

\subsection{Language}
If $\sum$ is an alphabet and $L$ is a subset of $\sum ^*$, then $L$ is a language over $\sum$ \cite{AutomataTheory.2007}.
If sigma is the \ac{ASCII} alphabet $\sum = \{a-zA-Z,0-9,\enspace,!,=,\#,\%,\$,\&,',(,),*,+,,,-,.,/,:,;,<,>,=,?,@,[,],\backslash ,\textasciicircum ,\textunderscore ,`,\{,\},\mid,\sim\}$, then for example the language where words can consist of $a-zA-Z$ is one language over the \ac{ASCII} alphabet.\\
In the example the abbreviations $a-zA-Z$ and $0-9$ are used. The first abbreviation represents all alphabetic letters in lower and upper case. The second abbreviation represents all one-digit numbers.

\subsection{Finite automata}\label{sec:BackgroundAutomata}
A finite automaton can recognize words of a language.
The language represented by an automaton is the set of all accepted words.
An automaton consists of a set of states, a set of input symbols and a transition function.
The transition function takes a state and an input symbol as input and returns a state based on these input arguments.
States are usually represented graphically by circles and transitions by labelled arrows.
There are two special states: The start state and final state(s).
The start state is the state in which the automaton starts processing the input symbols.
It is represented graphically by an arrow pointing to the start state.
Final states are represented graphically by a second circle embedded in the circle of a state.
Starting from the start state the automaton processes the first input symbol by evaluating the transition function that takes the start state and the first input symbol as arguments. The automaton continues to process the input data by evaluating transition functions until the automaton has read every input symbol. The input word is part of the language the automaton represents if the automaton has reached a final state. If the automaton has not reached a final state, the input words is not part of the language the automaton represents. \cite{AutomataTheory.2007}\\
Deterministic and non-deterministic automata can be distinguished.
Non-deterministic automata can have multiple transitions for the same input.
Deterministic automata have unambiguous transitions for a given input.
The concept of finite automata be applied to a lexical analysers for recognizing tokens (see section \ref{sec:BackgroundLexer}). \cite{AutomataTheory.2007}\\
Figure \ref{fig:FiniteAutomaton} shows an automaton consisting of the set of states $\{z\textsubscript{0}, z\textsubscript{1}, z\textsubscript{2}\}$ and several transitions. For convenience not all transitions are printed.
If the first input symbol would be a zero, the automaton would transit in a so called error state which cannot be left regardless the input symbols.\\
The automaton accepts words consisting of a one, an arbitrary number of zeros and a one after that.

\begin{figure}[H]
\centering
\begin{tikzpicture}[->, >=stealth', shorten >= 5pt,auto, node distance = 2.5cm, semithick]

\node[initial, state] (R) {z\textsubscript{0}};
\node[state] (S) [right of=R] {z\textsubscript{1}};
\node[state, accepting] (T) [right of=S] {z\textsubscript{2}};

\path (R) edge [below] node {1}(S)
	  (S) edge [loop, above] node{0} (S)
	  (S) edge [below] node {1} (T)
	  ;
\end{tikzpicture}
\caption{Finite automaton}
\label{fig:FiniteAutomaton}
\end{figure}

\subsection{Regular expression}\label{sec:BackgroundRegEx}

A regular expression is an algebraic description of a regular language.
Regular expressions declare strings that are part of the language and can describe the same words that can also be represented by a finite automaton.
In comparison to finite automata, the words can be described in an algebraic way.
Due to the similarities between regular expressions and finite automata, it is possible to convert regular expressions to finite automata and backwards.
Regular expressions are often used to describe tokens that should be recognized by a lexer (see section \ref{sec:BackgroundLexer}). \cite{AutomataTheory.2007}\\
For example, the regular expression \textbf{10*} denotes the language consisting of words that are made up a single 1 followed by an arbitrary number of 0's.\\
Given an alphabet $\sum$, regular expressions are formally defined as followed \cite{AutomataTheory.2007}:
\begin{enumerate}
\item The constants $\epsilon$ and $\emptyset$ are regular expressions denoting the empty string and the empty set.
\item If $a$ is a symbol of the alphabet $\sum$, then $a$ is a regular expression.
\item If $a$ and $b$ are regular expressions, then the alternation $a|b$ (either $a$ or $b$), the concatenation $ab$ ($a$ followed by $b$) and the Kleene star $a*$ (an arbitrary number of $a's$) are regular expressions.
\end{enumerate}

\subsection{Formal grammars}\label{sec:BackgroundGrammar}

Formal grammars describe how words of a language can be generated. In comparison to regular expressions, more classes of languages can be described. While regular expressions only describe regular grammars, formal grammars are able to describe all types of grammars of the Chomsky hierarchy.
%Grammars process data using a recursive structure.

%-s Ok, aber for allem können sie viel größer Klassen von Sprachen beschreiben!
%-...und wenn ich schon dabei bin: Machen REs das nicht auch? Nur halt anders.

The description of a grammar consists of four components:

\begin{itemize}

\item A set of symbols that defines the alphabet of the language. These symbols are called terminal symbols. The words of a language only consist of terminal symbols.

\item A set of variables called nonterminal symbols. Each nonterminal symbol represents a set of words.

\item One nonterminal symbol represents the language that is being defined. This nonterminal symbol is called the start symbol. Other nonterminal symbols help to define the language of the start symbol.

\item A set of productions/rules. They recursively describe the language. Each production consists of a nonterminal symbol on the left-hand side that is being substituted by a sequence of terminal symbols and/or other nonterminal symbols on the right-hand side. There can also be empty productions, meaning a nonterminal symbol is substituted by an empty string.
\end{itemize}

%- ambigious grammars
%- precedence rules?

\cite{AutomataTheory.2007}

%A grammar is a list of rules that defines the relationships between tokens \cite{LexYacc.1992}.
%These rules are also referred to as production rules.
%Given a start symbol, this symbol can be replaced by other symbols using the production rules.
%Using a recursive notation, production rules define derivations for words. The derived symbols can then once again be replaced until the derivation
%
%s Hier sauber trennen:
%- Die Ableitung ist eine Folge von Regelanwendungen
%- Das abgeleitete Wort
%- Terminale und nicht-terminale Symbole
%Speziell: Das abgeleitete Wort ist im allgemeinen kein Symbol
%(weder terminal noch nicht-terminal), sondern es besteht aus
%Symbolen (und wenn die Ableitung fertig ist, nur aus Terminalsymbolen
%
%is a terminal symbol.
%Terminal symbols describe symbols that cannot be further derived. The alphabet of the described language is built by the set of terminal symbols.
%Nonterminal symbols however can be further derived and todo ?build  merged? with the terminal symbols the vocabulary of a grammar. Nonterminal symbols and terminal symbols are disjoint.
%\\
%- Beispiel

\subsubsection{Context-free grammars}

A context-free grammar is a grammar that only allows productions rules that are in the form of $V \rightarrow \beta$ with V being a nonterminal symbol and $\beta$ being a sequence of nonterminal and terminal symbol with an arbitrary length.
Context-free grammars are often the basis of a parser (see section \ref{sec:BackgroundParser}). \cite{AutomataTheory.2007}

\subsubsection{Reduced grammars}

Grammars are called reduced if each nonterminal symbol is terminating and reachable \cite{Cremers75}.\\
Given the set of terminal symbols $\sum$, a nonterminal symbol $A$ is called terminating if there are productions $A \underrightarrow{*} z$ (meaning some sequence of productions can be applied) so that $z$ can be derived from $A$ in one or multiple steps and $z \epsilon \sum$*. \cite{Cremers75}\\
In other words, a nonterminal symbol $A$ is terminating if there exist production rules so that  $A$ can be replaced by a string of terminal symbols.\\
Given the set of terminal symbols $\sum$ and the start symbol $S$, a nonterminal symbol $A$ is called reachable if there are production rules $S \underrightarrow{*} uAv$ so that $S$ can be derived to $uAv$ and $u,v \epsilon \sum$*. \cite{Cremers75}\\
In other words, a nonterminal symbol $A$ is reachable if there exist production rules so that the start symbol can be replaced by a word containing $A$.

%todo beispiel

\section{\acf{BNF}}\label{sec:BackgroundBNF}

The \acf{BNF} is a language to describe context-free grammars.
In the \acf{BNF} nonterminal symbols are distinguished from terminal symbols by being enclosed by  angle brackets, e. g. <$TPTP\_File$> denotes the nonterminal symbol $TPTP\_File$.
Productions are described using the $"::="$ symbol and alternatives are specified using the $"|"$ symbol. \cite{BNF.1964}
Alternative symbols are used to conveniently describe several productions with the same left-hand side nonterminal symbol.
An example for a \ac{BNF} production using an alternative symbol would be
\begin{verbatim}
<TPTP_File> ::= <TPTP_Input> | <comment>
\end{verbatim}
Without using alternative symbols it would be
\begin{verbatim}
<TPTP_File> ::= <TPTP_Input>
<TPTP_File> ::= <comment>
\end{verbatim}
which is longer than the previous notation.
Using the \ac{BNF} pattern of notation whole grammars can be specified.\\
The \ac{EBNF} extends the \ac{BNF} with following rules:

\begin{itemize}%[noitemsep]
	\item Optional expressions are surrounded by square brackets.
	\item Repetition is denoted by curly brackets.
	\item Parentheses are used for grouping.
	\item Terminals are enclosed in quotation marks.
\end{itemize}
\label{itemize:BackgroundBNF}
\cite{EBNF.1977}

\section{TPTP language}\label{sec:BackgroundTPTP}

The \acf{TPTP} is a library of problems for \ac{ATP}.
Problems within the library are described in the \ac{TPTP} language.
The \ac{TPTP} language is a formal language and its syntax is specified in an \ac{EBNF}. \cite{Sut17}

Originally the \ac{TPTP} used only \ac{CNF} \cite{Sut09}, but over the years expanded to various other types of logics including include \ac{FOF} \cite{Sut09}, Typed \ac{FOF} \cite{SS+12, BP:CADE-2013} and \ac{THF} \cite{SB10, KSR:PAAR-2016}.

The \ac{TPTP} syntax uses an extended \ac{BNF}, with distinct rules for syntax, semantic constraints, tokens, and character classes which will be described in more detail in section \ref{sec:ConceptLexer}.
A small tool chain based on Lex/Yacc for building a basic parser from the syntax is available \cite{VS06}.
While the \ac{TPTP} is a major success story, the resulting syntax is large and complex.
Understanding the syntax is non-trivial.
For new developers who want to implement ``only'' a first theorem prover for \ac{CNF}, identifying the relevant parts of the syntax and implementing a parser is a significant barrier to entry.

\section{Lexing}\label{sec:BackgroundLexer}

Lexing or a so-called lexical analysis is the division of input into units called tokens~\cite{LexYacc.1992}.\\
The input is a string containing a sequence of characters.
The lexer groups characters of the input string into sequences that are meaningful. These sequences are called lexemes. From each lexeme, the lexer produces a token, which consists of a token name and the lexeme string. The token name is an abstract symbol which is used during parsing. \cite{Aho.2007} \\
The tokens are then passed to the parser for syntax analysis.
A lexer needs to distinguish different types of tokens and furthermore decide which token to use if there are multiple ones that fit the input \cite{Mogensen.2017}.\\
A simple approach to build a lexer would be building an automaton for each token definition and then test to which automata the input corresponds.
However, this would be inefficient because in the worst case the input needs to pass all automata before the belonging automata is identified.
Building a single automaton that tests each token simultaneously is more suitable.
This automaton can be built by combining all regular expressions by disjunction.
Each final state from each regular expression is marked to know which token has been identified.\\
Potentially final states overlap as a consequence of one token being a substring of another token.
For solving such conflicts, a lexer is separating the input in order to divide it into tokens.
Per convention the lexer chooses the longest input that matches any token.~\cite{Mogensen.2017} \\
Furthermore, a precedence of tokens can be declared. Usually the token that is being defined first has a higher precedence and thus will be chosen if possible token matches have the same length. \cite{Mogensen.2017}

Besides of writing a lexer manually it can also be generated by a lexer generator.
A lexer generator takes a specification of tokens as input and generates the lexer automatically.
The specification of the tokens is usually written using regular expressions.

\section{Parsing}\label{sec:BackgroundParser}

Parsing is the process of analysing the grammatical structure of a sequence of tokens and can be used to verify that the input of the parser is part of the language that the parser accepts \cite{Aho.2007}.
To do so, a parser builds a parse tree out of the tokens~\cite{Mogensen.2017}.\\
Parsers can also be generated automatically.
A parser generator takes a description of the relationship among tokens in form of a formal grammar (see section \ref{sec:BackgroundGrammar}) as input. The output is the generated parser. \cite{LexYacc.1992}\\
During the syntax analysis a parser takes a string of tokens and forms a syntax tree by finding the matching derivations. The matching derivation can be found using various approaches. The approaches that are used in this report will be introduced in the following.

\subsection{Bottom-up parsing}\label{sec:BackgroundParserBottomUp}

In bottom-up parsing a parse tree for an input string is constructed beginning at the leaves working up to the root. The idea is to reduce a string to the start symbol of a grammar, constructing a deviation in reverse.
\cite{Aho.2007}\\
At each reduction step, a specific substring matching the body of a production is replaced by the nonterminal at the head of that productions (reduction is reverse step of deviation).
The key decisions are when to reduce and what production to apply.
These decisions depend on the parser implementation.
An example for bottom-up are shift-reduce parsers that are described in the following.
\cite{Aho.2007}
\subsection{Shift-reduce parsing}\label{sec:BackgroundParserShiftReduce}

Shift-reduce parsers are a class of bottom-up parsers. In shift-reduce parsing a stack is used to hold grammar symbols and an input buffer holds the rest of the input string.
During parsing the input is scanned from left to right. The parser shifts zero or more input symbols on the stack until it can reduce the elements at the top of the stack. The elements at the top of the stack are reduced to the head of the corresponding production. This procedure is repeated until the stack only contains the start symbol and the input buffer is empty or an error is detected. \cite{Aho.2007}\\
There are four actions a shift-reduce parser can perform:
\begin{itemize}%[noitemsep]
	\item \textbf{Shift}: Shift next input symbol on top of stack.
	\item \textbf{Reduce}: With the right end of a string to be reduced being at top of the stack and the left end of the string located within the stack, the string is replaced with a  chosen nonterminal.
	\item \textbf{Accept}: Declare that input was successfully parsed.
	\item \textbf{Error}: Detect a syntax error.
\end{itemize}
\cite{Aho.2007}

%-use justified by, handle will appear on top of stack not inside, proof ...

There are two kinds of conflicts that can occur during shift-reduce parsing. A shift-reduce conflict occurs if the parser cannot decide whether to shift or reduce.
A reduce-reduce conflict occurs if the parser cannot decide which of multiple reductions to make. \cite{Aho.2007}

%-these grammars are not in LR(k) class of grammars (non-LR grammars)
%for example: ambiguous grammar can never be LR (if else)
%\cite{Aho.2007}

\subsection{LR($k$) parsing}\label{sec:BackgroundParserLR}

LR($k$) parsing is the most common type of bottom-up parsing. The L stands for left-to-right, meaning the input is read from left to right, and R stands for constructing a rightmost derivation in reverse.
In the context of LR parsing, the rightmost derivation refers to reducing the input to the start symbol.
The parameter $k$ refers to the number of inputs symbols that are used as a lookahead. When $k$ is omitted, $k = 1$ is assumed.
An LR parser makes shift-reduce decisions based on transitioning to different parsing states.
An LR parser consists of a stack and a parsing table defining parsing-action functions and goto functions.
An action function takes a state and an input symbol as input and defines which action to perform (Shift, Reduce, Accept, Error).
A goto function takes a state and a nonterminal symbol as input and is used to find the next state after a reduction.
States represent set of items.
An item is a production of the grammar that the parser is based on with a dot at some position on the right-hand side of the production.
An item indicates how much of a right-hand side has already been seen and what the parser is expecting next to apply the production (lookahead).
For example $A \rightarrow B\cdot C$ indicates that the parsers has already read the input $B$ and expects $C$ in order to reduce the right-hand side to $A$. \cite{Aho.2007}\\
LR parsers are complex to implement, but LR parser generators can be used, which significantly reduces the effort for creating an LR parser.
\cite{Aho.2007}

\subsubsection{LALR parsing}\label{sec:BackgroundParserLALR}

An LALR parser (Look-Ahead LR parser) is a simplified LR parser.
The simplification consists of merging items that have identical cores.
A core is the part of the right-hand side of a production that is left of the dot.
For example, an LR parser would separate the items $A \rightarrow B\cdot C$ and $A \rightarrow B\cdot D$ as their core is identical but their lookahead is different.
An LALR parser merges the two items to one item
$A \rightarrow B\cdot \{C,D\}$.
Not considering the lookahead reduces the power of the parser because it might lead to reduce/reduce conflicts.
LALR parsers are nevertheless often used because the parsing table is much smaller comparing to other LR parsing techniques  due to merging items.
\cite{Aho.2007}

\section{Lex and Yacc}\label{sec:BackgroundLexYacc}

Lex and Yacc are tools for writing lexers and parsers. They often work together, meaning the parser uses the generated tokens from the lexer as input.

\subsection{Lex}\label{sec:BackgroundLex}

Lex is a tool written in C that generates a lexer by specifying regular expressions and corresponding code fragments. The specified regular expressions are translated into a program that reads an input stream. The program partitions the input stream into strings matching the regular expressions. Once an expression is recognized the corresponding code is executed. For recognizing expressions, Lex builds a deterministic finite automaton. The automaton chooses the longest possible match if the input fits multiple specified regular expressions. \cite{Lex}\\
Flex is an open-source alternative to Lex and also compatible to Lex. \cite{Flex}

\subsection{Yacc}\label{sec:BackgroundYacc}
Yacc is a tool written in C that can be used for parsing the input of a computer program.
For generating a parser using Yacc, a specification that specifies the structure of the input has to be given. In addition to the specification of the structure, the input to the parser generator also consists of code that is invoked once a structure has been recognized.
Yacc turns the input specification into a routine that handles the input. This routine calls Lex to get tokens from the input stream and arranges them based on the specification. \cite{Yacc}\\
Bison is an open-source general-purpose parser generator that can be used as an alternative to Yacc and also accepts Yacc grammars. \cite{Bison}

\section{Python}\label{sec:BackgroundPython}

Since the use cases of \ac{Synplifier} are all hand-created grammars of relatively small size (dozens to thousands of rules), and the algorithms do not have high complexity, we decided to implement the tool in Python 3.
Python is a simple but powerful modern multi-paradigm language with good support and excellent libraries.
Python is easy to learn and its power allows it to create complex applications using Python.
Libraries that are used in the implementation \ac{Synplifier} are introduced in the following sections.

\subsection{PLY}\label{sec:BackgroundPythonPLY}

\acf{PLY} is an implementation of Lex and Yacc in Python. It is compatible with Python 2 and 3 and is fully implemented in Python. The goal of \ac{PLY} is to offer a pure-Python implementation of Lex and Yacc. The implementation aims to rebuild the functionalities of Lex and Yacc and thus includes:
\begin{itemize}
\item Support for LALR parsing (see chapter \ref{sec:BackgroundParserLALR})
\item Support for empty productions
\item Support for ambiguous grammars
\item Precedence rules
\item Error checking and recovery
\end{itemize}

\ac{PLY} consists of the two modules $lex.py$ and $yacc.py$ that are meant be to execute together. I. e. $yacc.py$ retrieves tokens from the input stream provided by $lex.py$. $Yacc.py$ returns an abstract syntax tree by default. However, it is possible for the user to change the output of $yacc.py$ according to his demands. \cite{PLY}
\ac{PLY} is used to generate the lexer and parser for the \ac{TPTP} syntax files.

\subsection{PyQt}\label{sec:BackgroundPytonPyQt}

PyQt is a Python binding for the cross-platform GUI framework Qt.
It is licensed under the GNU GPL version 3.
Qt offers a set of C++ libraries and development tools for various things including tools for building GUIs, defining regular expressions, setting up SQL databases, using NFC or interacting via Bluetooth.
PyQt implements over 1000 of these Qt tools in Python.
A simple GUI can be created by subclassing the QMainWindow, which provides a main window for an application.
The QTreeView class provides an implementation of a tree view, which can be used to display data in a hierarchically structured way. \cite{PyQt}

\subsection{argparse}\label{sec:BackgroundArgparse}

The Python module argparse is a module for creating command-line argument parsers.
It provides the means to specify input arguments and automatically creates help and usage messages.
It also checks if the given arguments are valid.
From the specified input arguments, the module will automatically create a parser for the specified arguments. \cite{argparse}\\
This module is used in the implementation of the command-line interface of \ac{Synplifier}.

\subsection{Beautiful Soup}\label{sec:BackgroundBeautifulSoup}

Beautiful Soup is a Python library for extracting data out of HTML and XML files.
That makes it especially useful for getting information from web pages. \cite{BeautifulSoup,BeautifulSoupDoku}
In \ac{Synplifier} Beautiful soup is used to automatically extract the latest \ac{TPTP} syntax from the HTML version on the \ac{TPTP} website.


\subsection{Special lnear groups \(SL(n, F)\)}

The special linear group, \(SL(n,F)\), is the subgroup of \(GL(n,F)\) where the determinants are \(1\).

That is, \(|M|=1\)

These are endomorphisms, not forms.


\documentclass[12pt, titlepage, oneside]{article}

\input{settings}

\begin{document}

	\textbf{ELECENG 3TQ3}\\
	\textbf{Elston A.}

\section{Lecture 2}
\subsection{Counting}

We need to sometimes count the number of favorable outcomes in order to determine the probability in many cases. These types of problems can be quite complex and require combinatorics; permutations, combinations, and variations.

\b{Combinations and permuatations} are different arrangements of set elements so that with \b{combinations the order does not matter} while with \b{permutations the order does matter}.

\subsection{Permutations}

Permutation is defined as all the possible arrangements of the elements.

\ex given a set \{1,2,3\} all the possible combinations are 123,132,213,231,312,321. There are 3*2*1 permutations of a set with 3 elements.

The total number of permutations for a set with $n$ elements is $n! = n * (n-1) * \dots * 2 * 1$

To think about this logically, since there are no repetition once an element has been used, it cannot be repeated. So if at first there are $n$ choices as we pick from $n$ elements, the next choice we can only pick from $n-1$ elements. So the total number of combinations we can have is $n!$.

If we were allowed to have repetitions, then we would have $n^n$ possibilities. However in general, we may only have a set of $n$ elements and need to make $m$ choices.

\ex If you have a box with 26 balls and each ball has one letter of the English alphabet. Then you try to pick 3 balls from the box without returning them to the box, many different words can you construct.

The first time you choose, you can get 26 different balls, the next you can only choose 25, and lastly you can only choose 24 different balls. So the total different types of combinations you can choose from is $26*25*24$.

In the example above we have a special case called k-permutations of $n$ or simply

\begin{align}
n * (n-1) * \dots * (n-k+1) = \dfrac{n!}{(n-k)!}
\end{align}

If the balls were placed back into the box whenever we picked one, then we would have a total of $26*26*26 = 26^3$ or simply $n^k$.

\subsection{Combinations}

For combinations \b{order does not matter}!.

Given $n$ objects pick $k$ regardless of order.

For example pick $k$ shirts in the closet. How many different shirt combinations are there?

The answer is given by:

\begin{align*}
{n \choose k} = \frac{n!}{k!(n-k)!} = \frac{n(n-1)\dots (n-k+1)}{k(k-1)\dots1}
\end{align*}

There are $n * (n-1) * \dots * (n-k+1)$ ways to pick $k$ elements out of $n$ where the order does matter. If the order does not matter then we can divide by $k!$ since there are $k!$ different orders of k elements.

When we say the order does not matter it means that we see $"abc" = "bac"$ since they contain the same letters. So given a set of 26 lower case characters and we had to pick 3 unique characters, then we would get $"abc"$ or $"bac"$ which is simply 26*25*24, but now if we said that it does not matter which way the order is then we have
\begin{align*}
 \frac{26*25*24}{3!}
\end{align*}

\subsection{Combinations with Repetition}

Let's say you have 5 different ice cream and 3 scoops, how many different flavors options do you have?

Since the question does not mention distinct scoops, we always assume it is not distinct. Since the ice cream flavors do not disappear after being chosen, we have a problem with repetition. The formula for such a condition is

\begin{align}
{n + k - 1 \choose k}  = \frac{(n+k-1)!}{k! (n-1)!} =
 \frac{ (n + k -1)*\dots*n (n-1)!}{k! (n-1)!} = \frac{(n+k-1)*\dots*n}{k!}
\end{align}

\subsection{Definitions}

\b{Outcome} is any possible observation

\b{Sample Space} is all the possible distinct outcomes

\b{Procedure} is what you do in the experiment

\b{Observation} is what you record

\b{Event} is a subset of the sample space that contains favorable outcomes

\b{Event Space} is the space that contains specific outcomes. \ex instead of recording heads or tails of two coins being thrown \{hh,ht,th,tt\} we could instead record the outcome based on an event. Let B0 be the event that 0 tails are present, B1 be the event that one coin is tails, B2 be the event that both coins are tails. By recording event space, you are losing resolution (meaning you know the event B1 means one coin was tails, but you cannot say th or ht), but are gaining a better understanding of your data for what you want to analyze.

\subsection{Mutually Exclusive }

Consider an example of tossing a coin 4 times:

\{tttt,ttth,ttht,tthh,thtt,thth,thht,thhh,httt,htth,htht,hthh,hhtt,hhth,hhht,hhhh\}

Let $Bi$ be the event of having $i$ number of heads.

So to find the event $A$ which is the number of heads is less than or equal to 3 we simply have
\begin{align*}
A = B0 \u B1 \u B2 \u B3
\end{align*}

\end{document}