%!TEX root = thesis.tex

\chapter{Deriving stellar parameters}
\label{cha:method}
\epigraph{The best way to learn is by doing. The only way to build a strong work ethic is getting
          your hands dirty.}{Alex Spanos}

There are different methods for obtaining stellar atmospheric parameters. Here follows a short
description of some of the most common methods, however the spectroscopic method will be explained
in much greater detail later in this chapter.

\section{Photometry}

Photometry can be used in different ways to estimate the effective temperature. In this section two
methods will be mentioned; a colour calibration, and asteroseismology. There are other methods, e.g.
SED fitting but this will not be discussed further.

\subsection{InfraRed Flux Method - IRFM}
\label{sec:irfm}

The InfraRed Flux Method (IRFM) was first described by \citet{Blackwell1977}. From IRFM it is
possible to measure the stellar radius and $T_\mathrm{eff}$ with a measurement of the angular
diameter, $\theta$, derived from infrared photometry. $T_\mathrm{eff}$ is derived from the angular
diameter from the simple relation
\begin{align}
  \sigma T_\mathrm{eff}^4 = \frac{4\mathcal{F}_E}{\theta^2}, \label{eq:irfm}
\end{align}
where $\mathcal{F}_E$ is the monochromatic flux measured at Earth, and $\sigma$ is Boltzmann's
constant. The angular diameter is calculated from the following equation:
\begin{align}
  \theta = 2\sqrt{\mathcal{F}_E/\mathcal{F}_S},
\end{align}
where $\mathcal{F}_S$ is the calculated monochromatic flux from the star. This flux is based on a
model atmosphere with an effective temperature based on the spectral energy distribution (SED). The
calculated flux show a strong dependence on $T_\mathrm{eff}$ in the visible, however this dependence
is much weaker in the infrared. Hence a poor first estimation of $T_\mathrm{eff}$ will lead to a
reliable angular diameter. From this new angular diameter $T_\mathrm{eff}$ can be re-derived, a new
model atmosphere can be used with a new set of $\mathcal{F}_S$ can be calculated. Iteratively the
angular diameter and $T_\mathrm{eff}$ can be calculated. If the distance $d$ is known of the star,
the stellar radius is $R_\ast = \frac{\theta d}{2}$. The solar flux, both measured and calculated,
from \citet{Blackwell1977} are shown in \fref{fig:IRFM}. Using the data provided the solar radius
and $T_\mathrm{eff}$ were derived using the equations above: $R=1.011R_\odot$ and
$T_\mathrm{eff}=\SI{5963}{K}$. This was simply done for each wavelength, and the results presented
here are just a simple average value.

\begin{figure}[htpb!]
    \centering
    \includegraphics[width=0.85\linewidth]{figures/IRFM.pdf}
    \caption{Measured and calculated flux from the Sun at infrared wavelengths. Data from Table 2 in
             \citet{Blackwell1977}. Mean solar radius from this data is $1.011R_\odot$, and mean
             solar $T_\mathrm{eff}=\SI{5963}{K}$ using \eref{eq:irfm}.}
    \label{fig:IRFM}
\end{figure}

The main drawbacks of the IRFM is the model dependence, a drawback many methods share, and the need
of high precision infrared photometry. For the model atmosphere a metallicity and surface gravity is
assumed which has an effect on $\mathcal{F}_S$, and hence on the final derived $T_\mathrm{eff}$ and
$R$. A more in-depth description of the IRFM can be see in e.g. \citet[][section 4]{Casagrande2006}.

\subsection{\texorpdfstring{$T_\mathrm{eff}$}{Teff}-colour-\texorpdfstring{$[\ion{Fe}/\ion{H}]$}{[Fe/H]} calibration}

Photometry can be used for deriving $T_\mathrm{eff}$ using existing colour calibrations like that of
for example \citet{Ramirez2005a} where adopted $T_\mathrm{eff}$ and $[\ion{Fe}/\ion{H}]$ in
combination with colours $X$ such as $B-V$, $V-S$, etc. are used to fit a polynomial such that the
$T_\mathrm{eff}$ can easily be estimated with a simple relation:
\begin{align}
  \theta_\mathrm{eff} = a_0+a_1 X+a_2 X^2+a_3X[\ion{Fe}/\ion{H}] + a_4[\ion{Fe}/\ion{H}] + a_5[\ion{Fe}/\ion{H}]^2, \label{eq:irfm}
\end{align}
where $\theta_\mathrm{eff}=5040/T_\mathrm{eff}$. A polynomial fit was performed on the residuals in
order to deal with spectroscopic features such as the Balmer lines, the Paschen jump, etc. This
polynomial fit, $P(X, [\ion{Fe}/\ion{H}])$ is added to \eref{eq:irfm} so the calibration finally
reads:
\begin{align}
  T_\mathrm{eff} = \frac{5040}{\theta_\mathrm{eff}} + P(X, [\ion{Fe}/\ion{H}]),
\end{align}

After obtaining the coefficients ($a_i$) for different combinations of colours, it is trivial to
obtain $T_\mathrm{eff}$ if the metallicity and a colour is known of the star.

\subsection{Asteroseismology}
\label{sec:asteroseismology}

Asteroseismology is the study of stellar pulsations. For main sequence stars (which will be the only
focus here), these pulsations propagate as sounds waves throughout a star, their origin and
amplitude is determined by the characteristics of the star. Hence the study of the pulsations, which
are seen on the surface, will thus be a study of the stellar properties. In order to to study these
a time series is needed. This can both be radial velocities as it was used in the recent results
from the SONG telescope \citep{Grundahl2017}, or photometry like the numerous results from e.g. the
space telescopes \emph{CoRoT} and \emph{Kepler} \citep[see
e.g.][]{Christensen-Dalsgaard2010,Chaplin2011,Huber2014}. The analysis is identical for either time
series, however the amplitudes in the power spectrum will be different.

After determining the frequencies of a range of pulsations from a power spectrum of the time series,
a pattern emerge at every $\Delta\nu$ (the so-called large frequency separation). A finer pattern
also occur described by $\delta\nu$. Last the frequency at maximum power is also measured from the
power spectrum, $\nu_\mathrm{max}$. The frequency at maximum power is used to obtain $\log g$ via
\begin{align}
  \nu_\mathrm{max} &\propto \frac{g}{\sqrt{T_\mathrm{eff}}} \\
                   &= \frac{M/M_\odot}{(R/R_\odot)^2 \sqrt{T_\mathrm{eff}/5777}}\; \SI{3.05}{mHz},\label{eq:scaling1}
\end{align}
where $\nu_{\mathrm{max},\odot}=\SI{3.05}{mHz}$ is the frequency of maximum amplitude for the Sun. A
similar equation exists for the determination of the stellar density:
\begin{align}
  \Delta\nu = (M/M_\odot)^{-1/2} (R/R_\odot)^{-3/2}\; \SI{134.9}{\micro\hertz}.
\end{align}
These simple scaling relation are described in detail in \citet{Kjeldsen1995} for main sequence
stars. These two equations can be used together to determine the mass and radius of the star, often
to a high precision. These scaling relations are applicable for stars which shows solar-like
oscillations, mostly found in main sequence FGK stars, but can also be found in red giant stars. The
mass and radius for a range of $\nu_\mathrm{max}$ and $\Delta\nu$ can be seen in \fref{fig:scaling},
where $T_\mathrm{eff}=\SI{5777}{K}$. The star located at $\{\Delta\nu=\SI{134.9}{\micro
Hz};\;\nu_\mathrm{max}=\SI{3.05}{mHz}\}$ is the Sun.

The small frequency separation, $\delta\nu$, is sensitive to the sound-speed gradient in the core
which in turn is sensitive to the composition. Thus the small frequency separation is a very
important diagnostic for stellar evolution. An interesting case is that of \citet{Bedding2011},
where it was shown it is possible to distinguish between hydrogen- and helium-burning cores in red
giant stars.

\begin{figure}[htpb!]
    \centering
    \includegraphics[width=0.85\linewidth]{figures/scaling_relation.pdf}
    \caption{Mass and radius from asteroseismic scaling relation. The colour is the mass and radius
             for the upper and lower panel, respectively. The location of the Sun is added for
             reference.}
    \label{fig:scaling}
\end{figure}

A drawback of asteroseismology is the dependence of $T_\mathrm{eff}$ in \eref{eq:scaling1} which has
to be provided from another method. Ideally this will come from spectroscopy for which the
determination of $T_\mathrm{eff}$ is often reliable. This drawback is minor compared to the weak
model dependence which is one of the strongest advantages of asteroseismology.

For both of the mentioned photometric methods to determine some atmospheric parameters a
disadvantage is the dependence on the knowledge of other atmospheric parameters which usually comes
from spectroscopy (e.g. metallicity). However, as will be discussed in
\sref{sec:method_spectroscopy} $\log g$ is often difficult to determine reliably and synergies are
welcomed between different methods.

\section{Spectroscopy}
\label{sec:method_spectroscopy}

A spectrum can be analysed with a range of different methods. The method finally chosen depend on
quality of the spectrum, i.e. high/low resolution and high/low S/N, the spectral type of the star,
the region that was observed, e.g. UV, optical, NIR, etc., some stellar properties, e.g.
fast-rotator, activity, etc. In practise, no methods work for all cases, but sometime several
methods can be used for on case.

Since this thesis is focused on FGKM stars, and mainly dwarfs, three methods will be described;
synthesis, spectral indices, and the EW method. The latter method will be described in a separate
section since this is the main method used for the analysis in this thesis.

\subsection{Synthesis}
\label{sec:synthesis}

The synthesis fitting method is a standard method for obtaining stellar atmospheric parameters from
a wide range of spectra, that is with different spectral resolution, spectral parameters such as
$T_\mathrm{eff}$, $\log g$, $v\sin i$ the projected rotational velocity, etc., and S/N  \citep[see
e.g.][]{Tsantaki2017}. The synthetic fitting method is in simple terms a comparison between the
observed spectrum and a synthetic spectrum, which is either calculated on the fly like Spectroscopy
Made Easy (SME) \citep{Valenti1996}, or using a pre-calculated grid like Starfish
\citep{Czekala2015}. By analysing the
\begin{align}
  \chi^2 = \sum_i^N\frac{(y_\mathrm{obs,i}-y_\mathrm{model,i})^2}{\sigma_i},
\end{align}
the synthetic spectrum that best match the observed spectrum can be found. Here the $y_\mathrm{obs}$
is the observed spectrum, $y_\mathrm{model}$ is the synthetic spectrum, and $\sigma$ is the error on
the measurement.

The synthetic fitting can be done by utilising small windows around sensitive spectral features such
as ionized lines or hydrogen lines for obtaining the surface gravity, iron lines for obtaining the
effective temperature, a series of different atomic lines for obtaining the overall metallicity.
This approach is used by \code{SME} and \code{FASMA}
\citep[][respectively]{Valenti1996,Tsantaki2017} and is a compromise between fitting the entire
spectral range and calculating the synthetic spectra on the fly which is time consuming. On the
other hand the entire spectrum can be fitted if a pre-calculated grid of synthetic spectra are
available. By masking small windows, one can also exclude different features that are troublesome,
this can be telluric lines, bad reduction of the spectra, or real spectral features where there
currently is poor atomic/molecular data such as the oscillator strength and thus it is not possible
to reliably fit this feature.

This method is affected by the different approaches one can use, that is which atmosphere models are
used (ATLAS, MARCS, etc.), atomic data and whether this has been calibrated, the radiative transfer
code in the case the synthetic spectra are calculated on the fly, and the minimization procedure
chosen. With these things in mind it is important to stress the wide range spectra and spectral
classes this method works with.

The advantage of the synthetic fitting method over the curve-of-growth analysis (see
\sref{sec:parameters}) is that it allows for the analysis of lower resolution and with a higher
rotational velocity, $v\sin i$.

\subsection{The EW method and \code{FASMA}}
\label{sec:parameters}

The EW method or curve-of-growth analysis is another standard method for obtaining stellar
atmospheric parameters from spectra as the synthetic fitting method (\sref{sec:synthesis}). Since
this is the method used throughout this thesis it will be explained in detail. This analysis follow
a chain of tasks, each has been made automatic in the software ``Fast Analysis of Spectra Made
Automatically" (\code{FASMA}\footnote{Greek for spectrum}) which was developed during this thesis
\citep{Andreasen2017a}. \code{FASMA} is made of three \code{drivers}:
\begin{enumerate}
  \item EW measurement driver
  \item Obtain stellar atmospheric parameters driver
  \item Abundance driver
\end{enumerate}
An additional driver is under development; a synthetic fitting driver \citep{Tsantaki2017}.
\code{FASMA} has been made available to the community via a web application at
\url{http://www.iastro.pt/fasma/}.

\subsubsection{Ingredients}

\code{FASMA} is written in the Python programming language and glue together other software and
model atmospheres necessary for obtaining stellar atmospheric parameters from high quality spectra.
These software and models are described in greater detail in the following sections. In short, the
curve-of-growth analysis require measured EWs where the latest version\footnote{The latest version
can be found here: \url{https://github.com/sousasag/ARES}} of \code{ARES} is used
\citep{Sousa2015a}. These EWs are used to derive line abundances using model atmosphere like the
ATLAS9 \citep{Kurucz1993}, MARCS models \citep{Gustafson2008}, or PHOENIX models\footnote{The
PHOENIX models are currently not a part of \code{FASMA}, however it is planned to implement these
models with \code{MOOG}.} \citep{Husser2013} to mention the most popular for this analysis. Note that the
PHOENIX models are relative new and not as widely used yet. In tandem with model atmospheres a
radiative transfer code is also needed. \code{FASMA} uses \code{MOOG} \citep{Sneden1973} for this.
The model atmosphere usually comes in a pre-calculated grid in the $\{T_\mathrm{eff},\,\log
g,\,[\ion{Fe}/\ion{H}]\}$ parameter space. These are interpolated in order to access the requested
combination of parameters. Last, \code{FASMA} consist of a minimization routine which looks for the
best matching parameters given a spectrum.

\subsubsection{Wrapper for \code{ARES}}
\label{sec:measureEW}

There are two ways to measure the EW of an absorption line, ``manually'' or automatically. There are
advantages and disadvantages for both approaches: For the manual, an advantage is that we can
inspect the lines and try to measure lines in different ways (which is useful if a absorption line
is blended). We have more control over how blended lines are fitted, and which profiles are used.
Disadvantages are that it is very time consuming, and it is prone to errors, as a measurement might
change drastically by the eyes measuring it. Even for the same person, the measurement can change.
By mentioning the advantages and disadvantages of the manual method, it should be clear that the
advantages and disadvantages of the automatic method is the opposite of those. Especially the time
to measure the lines are orders of magnitudes faster, which is crucial when dealing with more than a
handful of spectra. However, most important is the fact that the EWs of the lines are measured
consistently throughout the entire spectral range, allowing a homogeneous analysis of the lines.

When a line is measurement by hand (manually) it is in this thesis done using the \code{splot}
command in \code{IRAF}. Here the deblending mode is used whenever necessary. It is often necessary
to fit one spectral lines with several Gaussians, as neighbouring lines might contaminate the line
of interest.

Throughout this thesis line EWs are automatically measured with \code{ARES}
\citep{Sousa2007,Sousa2015a}. When using \code{ARES} it is important to use a correct value of the
\code{rejt} parameter. This parameter is used for placing the continuum level, and is thus directly
related to the final measurement EW. It is difficult to get this parameter right, however the newest
version of \code{ARES} has the option to analyse a few absorption free regions and measure the S/N.
The \code{rejt} is then calculated as: \begin{align*} \mathtt{rejt} = 1 - \frac{1}{\mathrm{S/N}}.
\end{align*}

\code{ARES} is used via the first driver of \code{FASMA}. All the options available for \code{ARES}
can be accessed by \code{FASMA}. The options are
\begin{itemize}
  \item Setting the spectral window, $\lambda_\mathrm{min}$ and $\lambda_\mathrm{max}$
  \item RV correction to be applied or a mask to measure the RV and automatic make this correction
  \item Minimum and maximum EW to be considered ($\SI{5}{m\angstrom}$ and $\SI{150}{m\angstrom}$
        respectively by default)
  \item Minimum acceptable distance between two consecutive lines
  \item Smoothing applied with a \code{boxcar} filter before measuring the EWs. This is only for
        automatic line identification.
\end{itemize}
An in-depth description of these options can be found in
\citet{Sousa2007,Sousa2015a}.

Rarely \code{ARES} crash when measuring an absorption line. The reason is not clear, however when
dealing with a large amount of spectra, it is important that the analysis moves on. However, a
closer inspection usually reveal strange spectral features such as zero flux, sharp peaks in the
spectra, etc. To deal with this problem, \code{FASMA} finds the last line which \code{ARES} tried to
measure in the log file. This line is temporarily removed from the line list and \code{ARES} is
restarted. The line list used for deriving parameters consists of numerous iron lines, thus removing
one line will have a negligible effect on the final derived parameters.

\subsubsection{Interpolation of atmosphere models}
\label{sec:interpolation}

\code{FASMA} has access to both ATLAS9 models by \citet{Kurucz1993} and MARCS models by
\citet{Gustafson2008}, both are in a pre-calculated grid as described above. Let this grid be
described by $\{T_\mathrm{eff,g},\, \log g_g,\, [\ion{Fe}/\ion{H}]_g\}$, where subscript $g$ is one
of the grid points. Such a grid can be seen in \fref{fig:grid} for $[\ion{Fe}/\ion{H}]=0.00$ in the
$T_\mathrm{eff}$ range; \SIrange{3000}{10000}{K}. For visualisation the location of the Sun is
shown as well. The colour scale corresponds to the temperature in the first layer of each model
atmosphere, i.e. the uppermost layer. The requested value will be $\{T_\mathrm{eff,r},\,\log
g_r,\,[\ion{Fe}/\ion{H}]_r\}$. The task is now to find the surrounding grid points in the parameter
space of the requested parameters. For $\log g$ and $[\ion{Fe}/\ion{H}]$ two neighbouring grid point
are used, and for $T_\mathrm{eff}$ four surrounding grid point are used, in total
$4\times2\times2=16$ model atmospheres for the interpolation. \code{FASMA} use the four surrounding
grid points for $T_\mathrm{eff}$ instead of two, since the model atmosphere changes most with
$T_\mathrm{eff}$. This is common in other interpolations as well \citep[see e.g.][]{Valenti1996}.

\begin{figure}[htpb!]
    \centering
    \includegraphics[width=0.85\linewidth]{figures/model_atmosphere.pdf}
    \caption{Model atmosphere grid from \citet{Kurucz1993} at $[\ion{Fe}/\ion{H}]=0.00$ between
             \SI{3000}{K} and \SI{10000}{K}. The grid extends to higher $T_\mathrm{eff}$, but these
             are not considered in this thesis.}
    \label{fig:grid}
\end{figure}

When the 16 model atmosphere have been located, the interpolation goes through each layer of the
model atmosphere, where there typical are 72 layers, and each column of which there are six. The
columns are described in \sref{sec:atmospheremodels}. The interpolation are done using the
\code{griddata} function from \code{SciPy}\footnote{\url{https://scipy.org/}}. The interpolation is
linear in the parameter space. After the interpolation, the result is saved to a file in the format
expected by \code{MOOG}.

\subsubsection{Minimization}
\label{sec:minimization}

With the measured EWs for all the lines in the line list, we choose an atmosphere model to determine
the abundances. If there is no prior knowledge of the star it is common simply to choose an
atmosphere model with solar parameters as a starting point. Once the line abundances of all the iron
lines has been determined, the linear correlation between the abundances and the reduced EWs (RW)
$a_\mathrm{RW}$, and the abundances and the excitation potential $a_\mathrm{EP}$ is calculated. If
there is a correlation it means the model atmosphere used is wrong. Moreover, we also have to check
if the mean abundance of \ion{Fe}{I} and \ion{Fe}{II} lines are equal, and last if mean abundance of
the \ion{Fe}{I} lines is equal to the input $[\ion{M}/\ion{H}]$ of the atmosphere model\footnote{We
use \ion{Fe}{I} instead of \ion{Fe}{II} lines for this, since they are more numerous.}. If one of
these four criteria does not pass, then the atmosphere model is wrong, and we have to search for a
new one. A common way to do this, is by combining the indicators into a scalar value:
\begin{align}
  f(\{T_\mathrm{eff}, \log g, [\ion{Fe}/\ion{H}], \xi_\mathrm{micro}\}) &= \sqrt{a_\mathrm{EP}^2 + a_\mathrm{RW}^2 + \Delta\ion{Fe}{}^2},
\end{align}
where $a_\mathrm{EP}$ is the correlation between abundances and excitation potential,
$a_\mathrm{RW}$ is the correlation between abundances and RW, and $\Delta\ion{Fe}{}$ is the
difference between the mean abundances of \ion{Fe}{I} and \ion{Fe}{II}. This scalar function can be
minimized using standard minimization procedures as the simplex downhill among others. However,
there is another approach that takes into the account the information stored in these indicators.
For example, if $a_\mathrm{EP}$ is positive it means $T_\mathrm{eff}$ has to be increased by an
amount correlated by the numerical value of $a_\mathrm{EP}$. In the same way, a non-zero
$a_\mathrm{RW}$ means $\xi_\mathrm{micro}$ has to be changed, and $\Delta\ion{Fe}{}$ is an indicator
for $\log g$. In the end it is a vector function being minimized which are more difficult, however
we are not minimizing this using standard mathematical methods, but rather using the physical
knowledge. This minimization is useless for anything else, but it is excellent for this. The vector
function has the form:
\begin{align}
    f(\{T_\mathrm{eff}, \log g, [\ion{Fe}/\ion{H}], \xi_\mathrm{micro}\}) = \{a_\mathrm{EP}, a_\mathrm{RW}, \Delta\ion{Fe}, \ion{Fe}{I}\}.
\end{align}

The abundances of \ion{Fe}{I} lines versus EP and RW are shown in \fref{fig:eprw} for the planet
host star HATS-1. The three rows are for three different model atmospheres. From upper to lower:
\begin{itemize}
  \item Converged: $T_\mathrm{eff}=\SI{5959}{K}$,
                   $\log g=4.59$,
                   $[\ion{Fe}/\ion{H}]=-0.04$, and
                   $\xi_\mathrm{micro}=\SI{1.05}{km/s}$.
  \item Converged with \SI{0.5}{km/s} added to $\xi_\mathrm{micro}$.
  \item Converged with \SI{500}{K} added to $T_\mathrm{eff}$.
\end{itemize}
Left column show the abundances against the EP, and the right column is abundances against RW.

\begin{figure}[htpb!]
    \centering
    \includegraphics[width=0.85\linewidth]{figures/EP_RW_vs_abundance.pdf}
    \caption{The abundances of \ion{Fe}{I} for the planet host star: HATS-1.
             Upper plot: Converged parameters (see text for stellar parameters for this star).
             Middle plot: Converged parameters with \SI{0.5}{km/s} added to $\xi_\mathrm{micro}$.
             Lower plot: Converged parameters with \SI{500}{K} added to $T_\mathrm{eff}$.}
    \label{fig:eprw}
\end{figure}

\paragraph{Option: outliers - }

The minimization with the different options is depicted in \fref{fig:minimization}. In each
iteration where convergence is not reached, the input metallicity is changed to that of the average
output metallicity using the \ion{Fe}{I} lines. \code{FASMA} is able to set one or all of the four
atmospheric parameters to a fixed value, and when it reach convergence it checks if there are any
outliers in the abundances. These outliers are likely to come from a bad measurement of the EW of
the given line. This can be due to line blending or a poor spectral reduction. These can be removed,
either:
\begin{itemize}
  \item All outliers above $3\sigma$ once; minimization routine is restarted after removal of
        outliers.
  \item All outliers above $3\sigma$ iteratively; minimization routine is restarted after removal of
        outliers each time.
  \item One outlier above $3\sigma$ (with the highest deviation) is removed iteratively;
        minimization routine is restarted after removal of outliers each time.
\end{itemize}
It is optional to remove any outliers, but recommended.

All restarts of the minimization will start at the previous best found parameters. For the latter
two where outliers are removed iteratively, this will continue until no outliers are present. An
optical line list like the ones by \citet{Sousa2008a,Tsantaki2013} have been tested thoroughly and
due to the large amount of lines it is safe to remove a larger amount of lines and still obtain
reliable parameters, thus using the first option is common here. However, with a less tested line
list, like the one by \citet{Andreasen2016} (and refined in \citet{Andreasen2017b}), one should
remove outliers more carefully, and it is recommended that one outlier is removed iteratively.

\paragraph{Option: fix $\xi_\mathrm{micro}$ - }

Sometimes the minimization can not reach convergence with all parameters free. The first approach to
progress is to fix $\xi_\mathrm{micro}$ to a value. This parameter is known to depend on the
spectral type \citep[see e.g.][and references therein]{Tsantaki2013}. This is also shown in
\fref{fig:vtRelation} for a sample of 583 stars analysed with \code{FASMA}. \code{FASMA} use one of
two empirical relations to fix $\xi_\mathrm{micro}$ if this is close to either $0\si{km/s}$ or
$5\si{km/s}$ and $|a_\mathrm{RW}| > 0.050$ at the end of the minimization. The empirical relations
are:
\begin{align}
  \xi_\mathrm{micro} =
  \begin{cases}
    6.935 \cdot 10^{-4}\; T_\mathrm{teff} - 0.348 \log g - 1.437     & \text{For $\log g \ge 3.95$} \\
    2.72 - 0.457 \log g + 0.072 \cdot [\ion{Fe}/\ion{H}]             & \text{For $\log g < 3.95$},
  \end{cases}
\end{align}
where the first case is from \citet{Tsantaki2013} and the latter case is from \citet{Adibekyan2015}.
In this way $\xi_\mathrm{micro}$ is changed in each iteration according to one of these relations.
This option is called \code{autofixvt} in \fref{fig:minimization}.

\begin{figure}[htpb!]
    \centering
    \includegraphics[width=0.8\linewidth]{figures/vtRelation.pdf}
    \caption{$\xi_\mathrm{micro}$ dependence on $T_\mathrm{eff}$ and $\log g$ for a sample of 583
             stars.}
    \label{fig:vtRelation}
\end{figure}

\paragraph{Option: Change line list - }

After the minimization, it happens that the derived $T_\mathrm{eff}$ is below \SI{5200}{K}. At this
temperature it is well known that the line list by \citet{Sousa2008a} does not work well. Some of
the lines start to be blended for lower $T_\mathrm{eff}$, thus giving poor measurements of some EWs.
This is something that was corrected by \citet{Tsantaki2013}, who created a subset of the previous
iron line list. This smaller line list are able to successfully derive $T_\mathrm{eff}$ below
\SI{5200}{K}. Therefore, if the option \code{teffrange} is on (which is highly recommended), the
line list by \citet{Sousa2008a} will simply be converted to the smaller line list by
\citet{Tsantaki2013} if $T_\mathrm{eff}$ is below this threshold, and the minimization will be
restarted. This conversion happens after the convergence.

\paragraph{Option: Refine results - }

Last there is an option, \code{refine}. This apply more strict criteria for the indicators to reach
convergence, thus making the minimization less sensitive to the initial guess since it could
otherwise reach convergence from one ``side'' of the parameter space. The default criteria are:
\begin{align*}
  a_\mathrm{EP}     &= 0.001\\
  a_\mathrm{RW}     &= 0.003\\
  \Delta\mathrm{Fe} &= 0.001.
\end{align*}
The criteria for $a_\mathrm{RW}$ is not as strict as $a_\mathrm{EP}$ since this indicator can change
rapidly with small changes in $\xi_\mathrm{micro}$, thus a very strict criteria might never lead to
convergence. Convergence is reached once all of the above criteria are met, and the input and output
metallicity are identical. If one or more of the parameters are fixed, the corresponding criterion
is simply set to 0 and effectively ignored, thus not changing the parameter.

\paragraph{Stepping in each iteration - }

For each iteration, the change to be applied for the atmospheric parameters are defined by adding
the following:
\begin{align}
  T_\mathrm{eff}     &: \SI{2000}{K} \cdot a_\mathrm{EP}   \\
  \xi_\mathrm{micro} &: \SI{1.5}{km/s} \cdot a_\mathrm{RW} \\
  \log g             &: -\Delta\mathrm{Fe}
\end{align}
to each parameter. Note again that metallicity is simply changed to the the output metallicity of
the previous iteration. These are empirical relations. Note that by changing e.g. $T_\mathrm{eff}$
not only is $a_\mathrm{EP}$ affected, but the other indicators as well as seen in \fref{fig:eprw}.
This inter-dependency between the parameters is ignored by \code{FASMA} as it is not a simple
problem to solve. The stepping presented above is chosen to rapidly reach convergence, without
causing problems for the inter-dependency. This minimization is thus build to apply a standard
method, curve-of-growth analysis, using as few calls to the time consuming part, which is the
calculation of the abundances with \code{MOOG}.

\begin{figure}[htpb!]
    \centering
    \includegraphics[width=0.85\linewidth]{figures/FASMA_minimization.pdf}
    \caption{Overview of the minimization for \code{FASMA}. Credit: \citet{Andreasen2017a}.}
    \label{fig:minimization}
\end{figure}

\subsubsection{Error estimate}
\label{sec:error_estimate}

The error estimate is based on the same method presented in \citet{Neuforge1997}. The error on
$\xi_\mathrm{micro}$ corresponds to the $1\sigma$ statistical error on the slope of the linear
regression between \ion{Fe}{I} abundances and RW. The error on $T_\mathrm{eff}$ is the statistical
error on the slope between \ion{Fe}{I} abundances and EP as well as the uncertainty in
$\xi_\mathrm{micro}$. The error for $[\ion{Fe}/\ion{H}]$ corresponds to the dispersion of the
\ion{Fe}{I} abundances as well as the uncertainties in $\xi_\mathrm{micro}$ and $T_\mathrm{eff}$.
The error in $\log g$ corresponds to the dispersion in the pressure sensitive \ion{Fe}{II}
abundances.

\subsubsection{Testing \code{FASMA}}
\label{sec:fasma_test}

Parameters were derived for a 582 sample presented in \citet{Sousa2011} with \code{FASMA} as a test.
The results were compared with \citet{Sousa2011} since the method is the one previously used in the
Porto group, thus making a fair test.

\code{ARES} was used to measure the EWs. \code{ARES} can give an estimate of the S/N by analysing
the continuum in certain intervals. For solar-type stars the following intervals work well:
5764-5766 \AA{}, 6047–6053 \AA{}, and 6068–6076 \AA{}. From the estimated S/N, \code{ARES} can give
an estimate on the very important \code{rejt} parameters \citep[see][for more information]{Sousa2015a}.

After measuring the EWs with \code{ARES}, \code{FASMA} was used  to determine the stellar
atmospheric parameters. The results are presented in \fref{fig:fasma_test} which shows
$T_\mathrm{eff}$, $\log g$, $[\ion{Fe}/\ion{H}]$, and $\xi_\mathrm{micro}$ for \code{FASMA} against
those of \citet{Sousa2011}. The sample contains stars with $T_\mathrm{eff}$ too cold for the line
list used. As described in \sref{sec:minimization} the line list by \citet{Sousa2008a} should be
converted to the line list presented in \citet{Tsantaki2013}. However, since this line list was not
available when \citet{Sousa2011} derived parameters, the \code{teffrange} option was left off in
order to make a fair comparison for \code{FASMA}.

\begin{figure}[htpb!]
    \centering
    \includegraphics[width=1.0\linewidth]{figures/FASMAtest.pdf}
    \caption{Stellar atmospheric parameters derived by \code{FASMA} compared to the sample by
             \citet{Sousa2011}. The x-axis in all plots shows the results from \code{FASMA}, while
             the y-axis shows the parameters derived by \citet{Sousa2011}.}
    \label{fig:fasma_test}
\end{figure}

The mean of the difference between parameters from \citet{Sousa2011} and those by \code{FASMA} are
presented in \tref{tab:FASMATest}. The comparison is very consistent, as expected, and the small
offsets are within the errors except for metallicity. This can be due to different versions of
\code{MOOG}, measured line lists (i.e. using slightly different settings/version of ARES to measure
the EWs), interpolation of atmosphere grid, and minimization routine. Most likely the difference
will be due to the different \code{rejt} parameters used in \code{ARES}, which can alter the EWs
systematically and hence the metallicity. To test this hypothesis 20 randomly stars with different
$T_\mathrm{eff}$ were selected and the EWs directly from \citet{Sousa2011} were used to derive
parameters. The results are presented in the last column of \tref{tab:FASMATest}. Note that the
$\log \mathit{gf}$ values from the original line lists by \citet{Sousa2011}, which used the
\code{MOOG} 2002 version, were not changed for the 2014 version of \code{MOOG}. This might lead to
some errors as well. However, the offsets are very small and are compatible with the errors on
parameters normally obtained from high-quality spectra.

\begin{table}[htb!]
    \caption{Difference in derived parameters by \citet{Sousa2011} and \code{FASMA}. The second
             column is the mean difference with EWs measured by \code{ARES} in \code{FASMA}, while
             the third column is the mean difference using 20 randomly stars with the exact same EWs
             from \citet{Sousa2011}.}
    \label{tab:FASMATest}
    \centering
    \begin{tabular}{lrr}
      \hline\hline
      Parameter             &  Mean difference         & Same line list        \\
      \hline
      $T_\mathrm{eff}$      &  $\SI{16(36)}{K}$        & $\SI{21(11)}{K}$      \\
      $\log g$              &  $\SI{-0.04(7)}{dex}$    & $\SI{-0.007(9)}{dex}$ \\
      $[\ion{Fe}/\ion{H}]$  &  $\SI{0.03(2)}{dex}$     & $\SI{0.004(9)}{dex}$  \\
      $\xi_\mathrm{micro}$  &  $\SI{-0.04(14)}{km/s}$  & $\SI{0.04(2)}{km/s}$  \\
      \hline
    \end{tabular}
\end{table}


\documentclass{article}

\usepackage[french]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{graphicx}
\usepackage{algorithm}
\usepackage{algorithmic}
\usepackage{amsmath}
\usepackage{systeme}
\usepackage{float}
\usepackage{amssymb}
\usepackage{mathrsfs}
\usepackage{color}
\usepackage{fancyhdr}
\usepackage{pdfpages}
\usepackage{layout}
\usepackage{multicol}
\usepackage{setspace}
\usepackage{csvsimple}
\usepackage[table]{xcolor}
\usepackage[colorlinks=true]{hyperref}
\usepackage{tikz, tkz-tab}
\usepackage[top=2cm,bottom=2cm,left=2cm,right=2cm]{geometry}
\usepackage{amsthm}

\usepackage{multicol}

%%%%%%%%%%%%%%%% Lengths %%%%%%%%%%%%%%%%
\setlength{\textwidth}{15.5cm}
\setlength{\evensidemargin}{0.5cm}
\setlength{\oddsidemargin}{0.5cm}

%%%%%%%%%%%%%%%% Variables %%%%%%%%%%%%%%%%
\def\project{4}
\def\title{Non-linear systems of equations and the Newton-Raphson method}
\def\group{3}
\def\team{3}
\def\manager{Lucas Guédon}
\def\secretary{Salim Bekkari}
\def\others{Mohammed Boudali, Imad Boudroua, Simon Bullot}
\singlespace
\begin{document}

%%%%%%%%%%%%%%%% Header %%%%%%%%%%%%%%%%
\noindent\begin{minipage}{0.98\textwidth}
  \vskip 0mm
  \noindent
  { \begin{tabular}{p{6.5cm}}
      {\bfseries \sffamily
        Project \project} \\
      {\itshape \title}
    \end{tabular}}
  \hfill
  \fbox{\begin{tabular}{p{8.4cm}}
      {~\hfill \bfseries \sffamily Group \group\ - Team \team
        \hfill~} \\[2mm]
      Manager: \manager \\
      Secretary: \secretary \\
      Programmers: \others
    \end{tabular}}
  \vskip 4mm ~

  ~~~\parbox{0.95\textwidth}{\small \textit{Abstract~:} \sffamily The aim of the project is to implement the Newton-Raphson method to solve non-linear systems of equations and apply it to problems.}
  \vskip 1mm ~
\end{minipage}

%%%%%%%%%%%%%%%% Main part %%%%%%%%%%%%%%%%

\section{Newton-Raphson method}
\begin{flushright}
By Lucas Guédon
\end{flushright}

The goal of this section is to implement a solver of non-lineal systems of equations. The equations are in the form of $f(X) = 0$, where $f: R^n \rightarrow R^m$ is differentiable. To achieve this, the Newton-Raphson method was used.

\subsection{Algorithm}

Given a starting point $U_n$, the objective is to find a point supposedly closer to a root than $U_n$. The new point $U_{n+1} = U_n + V$ is a good approximation of the root, so $f(U_{n+1}) \approx 0$.
The Taylor expansion of $f$ around $U_n$ allowed us to find that $f(U_{n+1}) = f(U_n) + H(U_n) \times V$, thus $H(U_n) \times V = -f(U_n)$.
This linear system of equations could be solved using numpy.linalg.solve, but the latter was replaced with numpy.linalg.lstsq in order to avoid numerical problems related to singular matrices.
The process is repeated until $||f(U_n)|| < \epsilon$ where $\epsilon$ is the desired precision.

\subsection{Backtracking}

In some cases, the previously shown algorithm diverges, backtracking avoids this.
The function $\Phi(U) = U^TU$ was added to evaluate the approximations.
When computing $U_{n+1}$, if $\Phi(U_n + V) < \Phi(U_n)$ then $U_n + V$ is an approximation worse than $U_n$. In this case, V is multiplied by the value $step$, with $0 < step < 1$. This process is repeated until the approximation is better than $U_n$.

\subsection{Testing}

In order to test the algorithm, two simple test cases were created. The first finds one root of the second-degree polynomial $f(x) = x^2 - 2x + 1$ using our algorithm. The expected result is 1. The result given by the algorithm is $0.998$, which has a relative error of $0.002$.

The second case solves a system with multiple variables and equations:
\[\begin{cases}
x^2 + y = 0 \\
x + 3y + 4 = 0
\end{cases}\]
This system is represented by the function $f(x, y) = (x^2 + y, x + 3y + 4)$. The algorithm found the solution $x = 1.333, y = -1.778$.
\\
To verify whether this is a correct solution, $f$ was applied to $x$ and $y$ : $f(x, y) = (1.930 \times 10^{-12}, 4.441 \times 10^{-16})$. The result was almost $(0, 0)$ so the root was correct.

To test the efficiency of backtracking, the algorithm was applied to the function $f(x) = x^3 - 2x^2 + 1$ with a starting point of $0.01$. This is a special edge case because f(0.01) is close to 1 and f'(0.01) is close to 0. During the first iteration, $V = \frac{f(0.01)}{f'(0.01)}$ which means that $U_1$ moved away from the solution. This can be seen in figure 1.

\begin{figure}[!htb]
    \centering
    \includegraphics[width=0.5\textwidth]{backtracking_testing.png}
    \caption{Comparison of the value of $\Phi(U_n)$ with and without backtracking}
\end{figure}

\newpage
\section{Computation of the Lagrangian points}

\begin{flushright}
By Simon Bullot
\end{flushright}

In celestial mechanics, the Lagrangian points are points near two objects where a lighter object will remain at the same place relatively to the two other masses. The Newton-Raphson method is used to find these points.

\subsection{Forces implementation}

First, a function which takes two parameters will return another function representing centrifugal, elastic or gravitational force. For instance, a function g will return the centrifugal force function corresponding to the parameters $k$ and $X_0$.
\[g:k,X_{0} \mapsto f with f : X = (x,y) \mapsto (k(x-x_{0}),k(y-y_{0}))\]

Then another function would return Jacobian matrix for any of the forces needed.

\subsection{Newton-Raphson method application}

A third function will represent the sum of the forces by adding the desired gravitational and centrifugal forces. The same is done by another function for the matrix representing Jacobian forces sum matrix. Then the Newton-Raphson algorithm is used to find a point where forces are equal to zero. A point where sum forces are equal to zero will be an equilibrium point. In fact, due to Newton's first law, if an object does not have any velocity, and the forces acting on it are compensating themselves, then it will not move.

\subsection{Obtaining the Lagrangian points}

By running the Newton-Raphson algorithm with different $U_{0}$ points, different Lagrangian points can be found.
For instance, if those points are used :
\[U_0 = (0.5, 0), U_1 = (1.5, 0), U_2 = (-1.5, 0) \]
The algorithm will respectively find these points:
\[L_0 = ( 0.85927766, 0 ), L_1 =  (1.15775715, 0 ), L_2 = (-0.99754112, 0 ).\]

\newpage
\section{Electrostatic equilibrium}
\begin{flushright}
By Imad Boudroua and  Mohammed Boudali
\end{flushright}

The purpose of this section is to show the equilibrium position of N charges placed in an electrostatic field. The movement studied is limited between -1 and 1 on the real axis.
\subsection{Algorithm to compute the Jacobian matrix}
The first step is to  compute the derivative of total  electrostatic energy function:
\begin{equation}
    E(x_1,..,x_N) = \sum_{i=1}^{N} ( \log(|x_i + 1| + \log|x_i - 1| + \frac{1}{2}\sum_{\substack{j=1 \\ j\neq i}}^{N} \log|x_i - x_j| )
\end{equation}
    After derivation, the expression found is:
\begin{equation}
\frac{\partial  E(x_1, ..,x_N)}{\partial x_i}
   = \frac{1}{x_i - 1}
      + \frac{1}{x_i + 1}
      + \sum_{\substack{j=1 \\ j\neq i}}^{N}\frac{1}{x_i - x_j}
      \end{equation}
Therefore, the elements of Jacobian matrix: $(\frac{\partial \nabla E_i}{\partial x_j})_{_{i,j}}$ can be simplified to:

\vspace{5pt}

\[J_{i,j} = \begin{cases} -\frac{1}{(x_i - 1)^2} -\frac{1}{(x_i + 1)^2} - \sum_{\substack{j=1 \\ j\neq i}}^{N}\frac{1}{(x_i - x_j)^2} &\mbox{if } i\neq j \\
-\frac{1}{(x_i - x_j)^2} & \mbox{if }  i = j \end{cases}
\]

\subsection{Application of Newton-Raphson method}
    After using the Newton-Raphson method to solve the equation: $\nabla E(x_1, ..,x_n) = 0$,  the variation of energy gradient was plotted (Figure 2):

    \begin{figure}[!htb]
    \centering
    \includegraphics[width=0.7\textwidth]{equilibrium.png}
    \caption{Electrostatic equilibrium with 10 charges}
\end{figure}
\vspace{1cm}

\newpage
In Figure 2, the curve converges to 0. This result proves that the system studied reached an electrostatic equilibrium. This property is more noticeable in Figure 3:
\vspace{1cm}

    \begin{figure}[!h]
    \centering
    \includegraphics[width=0.7\textwidth]{position_real_axis.png}
    \caption{Position of the charges on the real axis}
\end{figure}

\subsection{Comparison with the Legendre polynomial derivative roots }
In this section, the functions \textbf{legder}, \textbf{leg2poly} and \textbf{poly1d} were used to draw the curve of Legendre polynomial. Equilibrium positions were plotted in the same figure in order to compare the results (Figure 4):

    \begin{figure}[!h]
    \centering
    \includegraphics[width=0.8\textwidth]{legendre_comparaison.png}
    \caption{Legendre polynomials and equilibrium positions}
\end{figure}

This figure shows that Legendre polynomial derivative roots coincide with the equilibrium positions.
\subsection{The nature of the extremum}
The results show that electrostatic equilibrium positions reached a critical point. To determine its nature, the curve in Figure 5 was drawn.
    \begin{figure}[!h]
    \centering
    \includegraphics[width=0.7\textwidth]{energy_position.png}
    \caption{Energy variation of one charge on  [-1;1]}

\end{figure}
\newpage
   This curve is characterized by one global maximum reached in $x=0$.

\section{Conclusion}
\begin{flushright}
By Simon Bullot, Imad Boudroua and Lucas Guédon
\end{flushright}

The Newton-Raphson method can be a useful and powerful tool especially with the problems encountered in this project. In those cases, the Jacobian matrix of each function can be easily calculated. This enables the use of this method which works in any dimension. Moreover, the convergence of the algorithm studied is quadratic because roots are non stationary points and do not have multiplicity greater than one.

\end{document}


%% \listfiles
\documentclass[apj]{emulateapj}
%\documentclass[preprint2,12pt]{emulateapj}
%% \usepackage{natbib}
\usepackage{graphicx}
\usepackage{epsfig}
\usepackage{amssymb,amsmath}
\usepackage{array}
\usepackage{threeparttable}

\doublespace

%definitions
\newcommand{\Msol}{${\rm M_{\sun}}$}

%% Editing markup...
\usepackage{color}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% WARNING: This LaTeX block was generated automatically by authors.py
% Do not change by hand: your changes will be lost.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% --------------------- Ancillary information ---------------------
\shortauthors{SURP et al.}
\shorttitle{my short-title}
\slugcomment{Draft: \today}

\begin{document}

\title{Assignment 2}
 %% ---------

\author{Yumna Arshad}
%\altaffiltext{1}{CITA, University of Toronto}

\section{Question 1}

\subsection{Method}
To solve this question, I first created a grid of points along the x and y axis of the complex plane domain (-2,2) x (-2,2) using nested for-loops.
Then, I used a for loop to cycle through each point along the grid and iterate through the series at each position in the complex plane c.
Using a while loop I was able to track the iteration number of each diverging point and save that to a list.
As a result, each point had a corresponding iteration number associated with it; if that iteration number was equal to the given $#$ of required iterations then the point was added to a list associated with the key 'bound' in a dictionary (indicating the point remained bounded throughout all iterations), else the point was added to a list associated with the key 'diverge' in that same dictionary.
A bounded point was defined as remaining within the given square domain, i.e absolute value of both the real part and imaginary part of value $z_i$ were less than 2, after all iterations.
Finally, I plotted both a normal scatter plot with 2 different colors (one indicating diverging and another indicating bounded points) as well as a color map indicating the iteration number at which each diverging point left the domain.

\subsection{Analysis of Results}

\begin{figure}
    \centering
    \includegraphics[width=1.0\columnwidth]{A2Q1_plot1_final.png}
    \caption{Plot of the bounded points shown in orange and diverging points shown in blue. The domain shown is 4x4 square domain of complex plane centered at 0.}
    \label{fig:Q1plot1}
\end{figure}

The results shown in Fig.\ref{fig:Q1plot1} indicate the plot resembles that of a Mandelbrot set which is what we expect from the analysis of the iterated points in the plane.
The figure appears to be symmetric about the horizontal line y = 0 (x/real axis).
If we were to zoom into the edges of the image in the plot, we would see that the structure is repeating on smaller and smaller scales indicating that the boundary of this Mandelbrot set is a fractal curve.

\begin{figure}
    \centering
    \includegraphics[width=1.0\columnwidth]{A2Q1_plot2_final.png}
    \caption{Plot of diverging and bounded points using a color map to indicate iteration number at which diverging points become unbounded. The total number of iterations is 100.}
    \label{fig:Q1plot2}
\end{figure}

The color map equivalent of the plot in Fig.\ref{fig:Q1plot1} is pictured in Fig.\ref{fig:Q1plot2}. The colour bar indicates the iteration number at which each diverging points becomes diverging and leaves the given domain.
We see that the majority of diverging points diverge at very small number of iterations ($\approx$ 2 iterations) while the points nearest the edges of the plot require the most iterations to become diverging.
The closer you get to the bounded points, the more iterations are required to cause diverging of points.
If the number of iterations in code were increased we would expect the edges of the plot to become more finely distinguished in terms of their physical structure and geometry.

\section{Question 2}

\subsection{Method}
To do this question I used the solve ivp method in scipy's integrate module to solve a set of 3 ODE's (the SIR model) which model the spread of disease in a population.
This involved defining a function that returns the right hand side of the 3 differential equations: $\frac{dS}{dt}$, $\frac{dI}{dt}$ and $\frac{dR}{dt}$.
Using the initial values, the differential equations were numerically solved and plotted as a function of time for a given value of beta and gamma.
For the bonus part: I added a DE to model the death as: $\frac{dD}{dt} = \alpha I(t)$ and changed the equation for I(t) as follows: $\frac{dI}{dt} = \frac{\beta}{N}SI - \gamma I - \alpha I$.
Note: $\beta$ = average number of contacts per person/day, $\gamma$ = 1/D where D is $#$ of days an individual is infectious and $\alpha$ = fraction of infected individuals that die.

\subsection{Analysis of Results}

In Fig.\ref{fig:Q2plot1a}, Fig.\ref{fig:Q2plot1b} and Fig.\ref{fig:Q2plot1c} 3 plots for 3 different gamma and beta parameters are shown.

\begin{figure}
    \centering
    \includegraphics[width=0.7\columnwidth]{A2Q2_plot1a.png}
    \caption{Plot indicating relative populations of: susceptible (S), infected (I) and recovered (R) individuals of a given population using SIR Model of disease spread and numerical integration. The figure shows results for a relatively high period of infectiousness and low contact rate.}
    \label{fig:Q2plot1a}
\end{figure}

\begin{figure}
    \centering
    \includegraphics[width=0.7\columnwidth]{A2Q2_plot1b.PNG}
    \caption{Plot indicating relative populations of: susceptible (S), infected (I) and recovered (R) individuals of a given population using SIR Model of disease spread and numerical integration. The figure shows results for a relatively low period of infectiousness and high contact rate.}
    \label{fig:Q2plot1b}
\end{figure}

\vspace{5mm} %5mm vertical space
\begin{figure}
    \centering
    \includegraphics[width=0.7\columnwidth]{A2Q2_plot1c.PNG}
    \caption{Plot indicating relative populations of: susceptible (S), infected (I) and recovered (R) individuals of a given population using SIR Model of disease spread and numerical integration. The figure shows results for relatively high contact rate and long period of infectiousness contributing to an overall high infection rate.}
    \label{fig:Q2plot1c}
\end{figure}
In the first plot the number of infected people gets to a little over half the total population due to a relatively long period of infectiousness (20 days) but recovery rate is good since there is little contact between individuals. In the second plot the number of infected people only reaches 80-90 despite a higher contact rate because the period of infectiousness is very low (2 days). As a result, it doesn't take long for the disease to die out (only about 40 days). Finally, the 3rd plot shows the greatest spike in infected individuals, reaching a max of approx 900 individuals and a longer recovery rate because both the number of contacts and period of infectiousness are large. As such, it takes the disease almost 200 days to die out from the population.

%\vspace{5mm} %5mm vertical space
\newpage
For the bonus part, death is included in the model and the plot for given $\beta, \alpha and \gamma$ parameters is shown in Fig.\ref{fig:Q2plot2}.
The plot in Fig.\ref{fig:Q2plot2} differs from the other plots in that the total population does not remain the same anymore due to death of infected people. For a contact rate of 1 person per day and a period of infectiousness of 8 days with mortality rate of 0.3 (i.e. 3/10 infected people die) the total population is reduced by about 600 individuals due to the disease with about 400 individuals remaining.

\begin{figure}
    \centering
    \includegraphics[width=0.9\columnwidth]{A2Q2_plot2.png}
    \caption{Plot indicating relative populations of: susceptible (S), infected (I), recovered (R) and deceased (D) individuals of a given population using SIRD Model of disease spread and numerical integration.}
    \label{fig:Q2plot2}
\end{figure}

\end{document}


\documentclass[a4paper, fleqn, twoside, notitlepage]{scrartcl}
\input{settings}

\begin{document}

\maketitle
\vspace{-3em}
\tableofcontents

\vfill
\vspace{1em}
\noindent
This document describes the algorithms used for the fermionic action in the Hubbard model.
They are implemented by these classes:
\begin{itemize}
\item \texttt{HubbardFermiMatrix[Dia|Exp]}
\item \texttt{HubbardFermiAction<\ldots>}
\end{itemize}
and can be found int the following files:
\begin{itemize}
\item \texttt{src/isle/cpp/action/hubbardFermiMatrix[Dia|Exp].[hpp|cpp]}
\item \texttt{src/isle/cpp/action/hubbardFermiAction.[hpp|cpp]}
\end{itemize}
For usage information, see the source documentation of the classes.\\

\noindent
There are several different ways to formulate a field theory for the Hubbard Model.
This document covers two different discretizations of the fermion action, see Sections~\ref{sec:dia_disc} and~\ref{sec:exp_disc}.
In addition, the operators can be expressed in the particle/hole (labelled by $\alpha=1$) or spin (labelled by $\alpha=0$) basis.
The former gives an imaginary Hubbard-Stratonovich transformation and a contribution of auxiliary fields of $e^{\i\phi}$.
This is used in this documentation.
A calculation in the spin basis yields $e^\phi$.
All expressions for this basis can be obtained from the ones quoted here via a simple substitution.

\clearpage
\section{Definitions}

The fermionic action is
\begin{align}
  S_\text{ferm} &= - \log \det M(\phi, \tilde{\kappa}, \tilde{\mu}) M^T(-\phi, \sigma_{\tilde{\kappa}}\tilde{\kappa}, -\tilde{\mu}) \equiv - \log \det M_p M_h^T\label{eq:ferm_action_v1}\\
  &\equiv - \log \det Q(\phi, \tilde{\kappa}, \tilde{\mu}, \sigma_{\tilde{\kappa}}).\label{eq:ferm_action_v2}
\end{align}
where $\phi$ is the auxiliary field that is integrated over. In general $\sigma_{\tilde{\kappa}} = -1$ but for bipartite lattices, a particle-hole transformation can be used to get $\sigma_{\tilde{\kappa}} = +1$. Subscripts $p$ and $h$ indicate matrices for particles and holes, respectively.
The parameters are
\begin{itemize}
\item $\delta = \beta / N_t$ and $\beta$ the inverse temperature,
\item $\tilde{\kappa} = \delta\kappa$ and $\kappa$ the hopping matrix,
\item $\tilde{\mu} = \delta\mu$ and $\mu$ the chemical potential.
\end{itemize}

\subsection{Diagonal Discretization}\label{sec:dia_disc}

The main discretization of the fermion action used in Isle puts the hopping matrix on the diagonal and approximates the exponential used in Section~\ref{sec:exp_disc}.
In this case, the fermion matrix is
\begin{align}
  {M(\phi, \tilde{\kappa}, \tilde{\mu})}_{x't';xt}
  &= (1+\tilde{\mu})\delta_{x'x}\delta_{t't} - \tilde{\kappa}_{x'x}\delta_{t't} - \mathcal{B}_{t'} e^{\i\phi_{xt}}\delta_{x'x}\delta_{t'(t+1)}\label{eq:def_m}\\
  &\equiv {K(\tilde{\kappa}, \tilde{\mu})}_{x'x}\delta_{t't} - \mathcal{B}_{t'}{F_{t'}(\phi)}_{x'x}\delta_{t'(t+1)}.
\end{align}
where
\begin{align}
  {K(\tilde{\kappa}, \tilde{\mu})}_{x'x} &= (1+\tilde{\mu})\delta_{x'x} - \tilde{\kappa}_{x'x},\\
  {F_{t'}(\phi)}_{x'x} &= e^{\i\phi_{x(t'-1)}}\delta_{x'x}.
\end{align}
Note that the second $M$ in~\eqref{eq:ferm_action_v1} can not be expressed as $M^*(\phi)$ even for bipartite lattices and $\mu=0$ because $\phi$ is potentially complex valued.
Anti-periodic boundary conditions are encoded by
\begin{align}
  \mathcal{B}_t =
  \begin{cases}
    +1,\quad 0 < t < N_t\\
    -1,\quad t = 0
  \end{cases}
\end{align}
and periodicity in Kronecker deltas.
In time-major layout (time index is slowest) $M$ is a cyclic lower block bidiagonal matrix:
\begin{align}
  M =
  \begin{pmatrix}
    K    &      &        &        & F_0 \\
    -F_1 & K    &        &        &     \\
         & -F_2 & K      &        &     \\
         &      & \ddots & \ddots &     \\
         &      &        &-F_{N_t-1}&K   \\
  \end{pmatrix}.\label{eq:ferm_mat_block_v1}
\end{align}

\noindent
The matrix $Q$ in eq.~\eqref{eq:ferm_action_v2} is easy to calculate and can be expressed as
\begin{align}
  {Q(\phi, \tilde{\kappa}, \tilde{\mu}, \sigma_{\tilde{\kappa}})}_{x't',xt}
  &= {M(\phi, \tilde{\kappa}, \tilde{\mu})}_{x't',x''t''} {M^T(-\phi, \sigma_{\tilde{\kappa}}\tilde{\kappa}, -\tilde{\mu})}_{x''t'',xt}\\
  &= \big[(1+\tilde{\mu})\delta_{x'x''}\delta_{t't''} - \tilde{\kappa}_{x'x''}\delta_{t't''} - \mathcal{B}_{t'}e^{\i\phi_{x''t''}}\delta_{x'x''}\delta_{t'(t''+1)}\big] \nonumber\\
  &\quad\times \big[(1-\tilde{\mu})\delta_{x'' x}\delta_{t'' t} - \sigma_{\tilde{\kappa}}\tilde{\kappa}_{x'' x'}\delta_{t'' t} - \mathcal{B}_{t}e^{-\i\phi_{x''t''}}\delta_{x'' x}\delta_{t(t''+1)}\big]\\
  &\equiv \delta_{t't}{(P)}_{x'x} + \delta_{t'(t+1)}{(T^+_{t'})}_{x'x} + \delta_{t(t'+1)}{(T^-_{t'})}_{x'x}
\end{align}
with
\begin{align}
  {P(\phi, \tilde{\kappa}, \tilde{\mu}, \sigma_{\tilde{\kappa}})}_{x'x} &= (2-\tilde{\mu}^2)\delta_{x'x} - (\sigma_{\tilde{\kappa}}(1+\tilde{\mu}) + (1-\tilde{\mu}))\tilde{\kappa}_{x'x} + \sigma_{\tilde{\kappa}}{(\tilde{\kappa}^2)}_{x'x}\\
  {T^+_{t'}(\phi, \tilde{\kappa}, \tilde{\mu}, \sigma_{\tilde{\kappa}})}_{x'x} &= \mathcal{B}_{t'}e^{\i\phi_{x'(t'-1)}}[\sigma_{\tilde{\kappa}}\tilde{\kappa}_{x'x} - (1-\tilde{\mu})\delta_{x'x}]\\
  {T^-_{t'}(\phi, \tilde{\kappa}, \tilde{\mu}, \sigma_{\tilde{\kappa}})}_{x'x} &= \mathcal{B}_{t'+1}e^{-\i\phi_{xt'}}[\tilde{\kappa}_{x'x} - (1+\tilde{\mu})\delta_{x'x}]
\end{align}
In time major layout (time is slowest running index) $Q$ assumes a cyclic block tridiagonal form:
\begin{align}
  Q(\phi, \tilde{\kappa}, \tilde{\mu}, \sigma_{\tilde{\kappa}}) =
  \begin{pmatrix}
    P         & T^-_0 &       &         &           &              & T^+_0    \\
    T^+_1     & P     & T^-_1 &         &           &              &          \\
              & T^+_2 & P     & T^-_2   &           &              &          \\
              &       & T^+_3 & P      & \ddots         &              &          \\
              &       &       & \ddots     & \ddots         & T^-_{N_t-3}    &          \\
              &       &       &        & T^+_{N_t-2} & P            & T^-_{N_t-2}\\
    T^-_{N_t-1} &       &       &        &           & T^+_{N_t-1}    & P
  \end{pmatrix}
\end{align}

\subsection{Exponential Discretization}\label{sec:exp_disc}

The fermion matrix discussed in the previous section is an approximation of
\begin{align}
  {\hat{M}(\phi, \tilde{\kappa}, \tilde{\mu})}_{x't';xt}
  &\equiv {\hat{K}}_{x'x}\delta_{t't} - \mathcal{B}_{t'}{\hat{F}_{t'}(\phi, \tilde{\kappa}, \tilde{\mu})}_{x'x}\delta_{t'(t+1)},\label{eq:def_m_exp}
\end{align}
with
\begin{align}
  {\hat{K}}_{x'x} &\equiv \delta_{x'x}\\
  {\hat{F}_{t'}(\phi, \tilde{\kappa}, \tilde{\mu})}_{x'x} &\equiv {(e^{\tilde{\kappa}-\tilde{\mu}})}_{x'x} e^{\i \phi_{x(t'-1)}} = {(e^{\tilde{\kappa}-\tilde{\mu}} F_{t'}(\phi) )}_{x'x}.
\end{align}
And the combination
\begin{align}
  {\hat{Q}(\phi, \tilde{\kappa}, \tilde{\mu}, \sigma_{\tilde{\kappa}})}_{x't',xt}
  \equiv \delta_{t't}{(\hat{P})}_{x'x} + \delta_{t'(t+1)}{(\hat{T}^+_{t'})}_{x'x} + \delta_{t(t'+1)}{(\hat{T}^-_{t'})}_{x'x}
\end{align}
with
\begin{align}
  {\hat{P}(\tilde{\kappa}, \tilde{\mu}, \sigma_{\tilde{\kappa}})}_{x'x} &= \delta_{x'x} + {(e^{\tilde{\kappa}-\tilde{\mu}} e^{\sigma_{\tilde{\kappa}}\tilde{\kappa} + \tilde{\mu}})}_{x'x}\\
  {\hat{T}^+_{t'}(\phi, \tilde{\kappa}, \tilde{\mu}, \sigma_{\tilde{\kappa}})}_{x'x} &= - \mathcal{B}_{t'} {\hat{F}_{t'}(\phi, \tilde{\kappa}, \tilde{\mu})}_{x'x} = - \mathcal{B}_{t'} {(e^{+\tilde{\kappa}-\tilde{\mu}})}_{x'x} e^{\i \phi_{x(t'-1)}}\\
  {\hat{T}^-_{t'}(\phi, \tilde{\kappa}, \tilde{\mu}, \sigma_{\tilde{\kappa}})}_{x'x} &= - \mathcal{B}_{t'+1} {\hat{F}^T_{t'+1}(-\phi, \sigma_{\tilde{\kappa}}\tilde{\kappa}, -\tilde{\mu})}_{x'x} = - \mathcal{B}_{t'+1} e^{-\i \phi_{x't'}} {(e^{\sigma_{\tilde{\kappa}}\tilde{\kappa}+\tilde{\mu}})}_{x'x}
\end{align}
Since both $\hat{M}$ and $\hat{Q}$ have the same block structure as $M$ and $Q$, most of the following derivations treat only the diagonal discretization from Section~\ref{sec:dia_disc}.
Based on those, results for the exponential discretization are derived where they differ.

\section{DIRECT\_SINGLE}\label{sec:direct_square}

This is the main version of the algorithm implemented in Isle.
It treats the fermion matrices for particles $M_p$ and holes $M_h$ separately and uses direct solves.

\subsection{LU-Decomposition}\label{sec:lu_v1}

In order to calculate the determinant of $M$ and solve systems of equations, compute the LU-decomposition of $M$. This can be done analytically in terms of spacial matrices given the specific structure in equation~\eqref{eq:ferm_mat_block_v1}.
Use the following ansatz\footnote{This is an adaptation of the algorithm presented in~\cite{zivkovic:2013} and is a simplified version of the algorithm presented in~\cref{sec:lu_decomposition_v2}}:
\begin{align}
  L =
  \begin{pmatrix}
    1   &     &    &        &        &\\
    l_0 & 1   &    &        &        &\\
        & l_1 & \ddots &        &        &\\
        &     & \ddots & 1      &        &\\
        &     &   & l_{n-3} & 1      &\\
        &     &   &        & l_{n-2} & 1
  \end{pmatrix},
  \; U =
  \begin{pmatrix}
    d_0 &     &      &   &        & v_0\\
        & d_1 &     &    &        & v_1\\
        &     & d_2 &    &        & \vdots \\
        &     &     & \ddots &        & v_{n-3} \\
        &     &     &    & d_{n-2} & v_{n-2} \\
        &     &     &    &        & d_{n-1}
  \end{pmatrix}
\end{align}
Note that each $l$, $d$, and $v$ is an $N_x \times N_x$ matrix. Multiplying this out and comparing sides of the equation $M = LU$ leads to a set of recursive equations:
\begin{itemize}
\item $d_i = K$ for $0 \le i \le N_t-2$;\hspace{2em} $d_{N_t-1} = K - l_{N_t-2}v_{N_t-2}$
\item $l_i d_i = -F_{i+1}$ for $0 \le i \le N_t-2$
\item $v_0 = F_0$;\hspace{2em} $l_{i-1} v_{i-1} + v_i = 0$ for $1 \le i \le N_t-2$
\end{itemize}

\noindent
Using $d_i = K$ for $i < N_t-1$, we get
\begin{align}
  l_i = - F_{i+1} K^{-1}\label{eq:mlu_l}
\end{align}
and from this
\begin{align}
  v_i &= -l_{i-1}v_{i-1} = F_i K^{-1} v_{i-1} = F_i K^{-1} F_{i-1} K^{-1} v_{i-2}\\
      &\equiv K A_{0i}\label{eq:mlu_v}
\end{align}
with
\begin{align}
  A_{tt'} \equiv K^{-1} F_{t'} K^{-1} F_{i-1} \cdots K^{-1} F_t.\label{eq:def_partial_A}
\end{align}
Finally, we get the non trivial $d$:
\begin{align}
  d_{N_t-1} = K + F_{N_t-1} K^{-1} K A_{0(n-2)} = K (1 + A),
\end{align}
where $A$ without index is
\begin{align}
  A &\equiv K^{-1}F_{N_t-1}K^{-1} F_{N_t-2} \cdots K^{-1}F_{1}K^{-1} F_{0}.\label{eq:def_A}
\end{align}

\subsection{Action}

Given the LU-decomposition of the previous section, we can write
\begin{align}
  \det M = \det L \det U = (\prod_{i=0}^{N_t-1}\,1) (\prod_{i=0}^{N_t-1}\,d_i)
\end{align}
Remultiplying the equations for the non-trivial $d$ gives
\begin{align}
  d_{N_t-1} = K + F_{N_t-1}K^{-1} F_{N_t-2}K^{-1} \cdots F_{1}K^{-1} F_{0}.
\end{align}
Hence the determinant is
\begin{align}
  \det M &= {(\det\,K)}^{N_t-1} \det (K + F_{N_t-1}K^{-1} F_{N_t-2} \cdots K^{-1}F_{1}K^{-1} F_{0})\\
         &= {(\det\,K)}^{N_t} \det(1 + K^{-1}F_{N_t-1}K^{-1} F_{N_t-2} \cdots K^{-1}F_{1}K^{-1} F_{0}).\label{eq:det_M_2}
\end{align}
It is possible to move the inverse to $F$ by factoring in one $K$ and factoring out one $F$ and iterating:
\begin{align}
  \det M &= {(\det\,K)}^{N_t-1} \det F_{N_t-1} \det(F_{N_t-1}^{-1} K + K^{-1} F_{N_t-2} \cdots K^{-1}F_{1}K^{-1} F_{0})\\
         &= {(\det\,K)}^{N_t-2} \det (F_{N_t-1} F_{N_t-2}) \det(F_{N_t-2}^{-1} K F_{N_t-1}^{-1} K + K^{-1} F_{N_t-3} \cdots K^{-1} F_{0})\\
         &= \Big(\prod_{t=0}^{N_t-1} \det F_{t}\Big) \det(1 + F_0^{-1} K F_1^{-1} K \cdots F_{N_t-2}^{-1} K F_{N_t-1}^{-1} K)\\
         &= e^{\i \Phi} \det(1 + A^{-1}),\label{eq:det_M_1}
\end{align}
where $\Phi \equiv \sum_{x,t} \phi_{x,t}$.
Apply the logarithm to~\eqref{eq:det_M_1} to get the final result:
\begin{resultbox}
  \vspace{-\baselineskip}
  \begin{align}
    \log \det M &= \i \Phi  + \log \det (1 + A^{-1}),\label{eq:det_M}\\
    A^{-1} &\equiv F_0^{-1}K F_1^{-1}K \cdots F_{N_t-1}^{-1}K.\label{eq:def_Ainv}
  \end{align}
\end{resultbox}
\noindent
The action~\eqref{eq:ferm_action_v1} is the sum of contributions from particles and holes, i.e.
\begin{align}
  S_\text{ferm} = - \log \det (1 + A_p^{-1}) - \log \det (1 + A_h^{-1}).
\end{align}
In general those contributions are not related by a simple equation meaning that both need to be computed from scratch. Equation~\eqref{eq:det_M_1} is the better choice here compared to~\eqref{eq:det_M_2} because all individual matrices that contribute to the former are sparse.\\

\noindent
The result for the exponential discretization, Section~\ref{sec:exp_disc}, can be obtained via the substitutions
\begin{align}
  K &\mapsto \hat{K} = \mathds{1}\label{eq:exp_subs_K}\\
  F_{t'} &\mapsto \hat{F}_{t'}\label{eq:exp_subs_F}
\end{align}
Applying them to~\eqref{eq:det_M_2} gives
\begin{resultbox}
  \vspace{-\baselineskip}
  \begin{align}
    \log \det \hat{M}_p &= \log \det (1 + \hat{A}),\label{eq:det_M_exp}\\
    \hat{A} &\equiv \hat{F}_{N_t-1} \hat{F}_{N_t-2} \cdots \hat{F}_{1} \hat{F}_{0}.\label{eq:def_B}
  \end{align}
\end{resultbox}

\noindent
Numerical experiments have shown that using equation~\eqref{eq:det_M_exp} can be unstable for holes and large values of $\beta$.
For this reason, the hole determinant should be calculated using the following, which is based on~\eqref{eq:det_M_1} with appropriate substitutions for the exponential discretization:
\begin{resultbox}
  \vspace{-\baselineskip}
  \begin{align}
    \log \det \hat{M}_h &= -\i \Phi  - N_t \log \det e^{-\sigma_{\tilde{\kappa}}\kappa-\mu} + \log \det (1 + \hat{A}^{-1}),\label{eq:det_M_exp_h}\\
    \hat{A}^{-1} &\equiv \hat{F}_0^{-1} \hat{F}_1^{-1} \cdots \hat{F}_{N_t-1}^{-1}
  \end{align}
\end{resultbox}

\subsection{Force}

The fermionic force at spacetime point $\mu\tau$ is given by
\begin{align}
  {(\dot{\pi}_{\text{ferm}})}_{\mu\tau} = -\dpd{H_{\text{ferm}}}{\phi_{\mu\tau}} = -\dpd{S_\text{ferm}}{\phi_{\mu\tau}} =  \dpd{}{\phi_{\mu\tau}} \Big[\log \det M(\phi, \tilde{\kappa}, \tilde{\mu}) + \log \det M(-\phi, \sigma_{\tilde{\kappa}}\tilde{\kappa}, -\tilde{\mu})\Big].
\end{align}
$\dot{\pi}$ is complex valued in general. For now we will ignore this fact and compute the force from the above equation.
Using~\eqref{eq:det_M} and Jacobi's formula, the derivative can be expressed as
\begin{resultbox}
  \vspace{-\baselineskip}
  \begin{align}
    {(\dot{\pi}_{\text{ferm}})}_{\mu\tau} &= \Tr \Big[{(1+A_p^{-1})}^{-1}\dpd{}{\phi_{\mu\tau}}A_p^{-1} + {(1+A_h^{-1})}^{-1}\dpd{}{\phi_{\mu\tau}}A_h^{-1}\Big],\label{eq:force_v1}\\
    A^{-1} &\equiv F_0^{-1}K F_1^{-1}K \cdots F_{N_t-1}^{-1}K
  \end{align}
\end{resultbox}
\noindent
For reference, write out $\dot{\pi}_{\text{ferm}}$ explicitly. First, in order to improve readability denote partial products of $A^{-1}$ as
\begin{align}
  A^{-1}_{tt'} \equiv F_t^{-1}K \cdots F_{t'}^{-1}K, \qquad A^{-1}_{tt} \equiv F_t^{-1}K
\end{align}
which is the inverse of~\eqref{eq:def_partial_A}.
The general case:
\begin{align}
  {(\dot{\pi}_{\text{ferm}})}_{\mu\tau} = -\i {\big(A^{-1}_{p,(\tau+1)(N_t-1)} {(1+A_p^{-1})}^{-1} A^{-1}_{p,0\tau}\big)}_{\mu\mu} + \i {\big(A^{-1}_{h,(\tau+1)(N_t-1)} {(1+A_h^{-1})}^{-1} A^{-1}_{h,0\tau}\big)}_{\mu\mu}
\end{align}
Elements near the boundary:
\begin{align}
  {(\dot{\pi}_{\text{ferm}})}_{\mu0} &= -\i {\big(A^{-1}_{p,1(N_t-1)} {(1+A_p^{-1})}^{-1} A^{-1}_{p,00}\big)}_{\mu\mu} + \i {\big(A^{-1}_{h,1(N_t-1)} {(1+A_h^{-1})}^{-1} A^{-1}_{h,00}\big)}_{\mu\mu}\\
  {(\dot{\pi}_{\text{ferm}})}_{\mu(N_t-1)} &= -\i {\big(A^{-1}_{p} {(1+A_p^{-1})}^{-1}\big)}_{\mu\mu} + \i {\big(A^{-1}_{h} {(1+A_h^{-1})}^{-1}\big)}_{\mu\mu}\\
  {(\dot{\pi}_{\text{ferm}})}_{\mu(N_t-2)} &= -\i {\big(A^{-1}_{p,(N_t-1)(N_t-1)} {(1+A_p^{-1})}^{-1} A^{-1}_{p,0(N_t-2)}\big)}_{\mu\mu} \nonumber\\
                                 &\quad + \i {\big(A^{-1}_{h,(N_t-1)(N_t-1)} {(1+A_h^{-1})}^{-1} A^{-1}_{h,0(N_t-2)}\big)}_{\mu\mu}
\end{align}
Keep in mind that $\mu$ is \emph{not} summed over, even when it appears twice in one term.\\

\noindent
The force in the exponential discretization can be calculated by applying the same substitutions as for the action itself, Eqs.~\eqref{eq:exp_subs_K} and~\eqref{eq:exp_subs_F}.
\begin{resultbox}
  \vspace{-\baselineskip}
  \begin{align}
    {(\dot{\hat{\pi}}'_{\text{ferm}})}_{\mu\tau} &= \Tr \Big[{(1+\hat{A}_p^{-1})}^{-1}\dpd{}{\phi_{\mu\tau}}\hat{A}_p^{-1} + {(1+\hat{A}_h^{-1})}^{-1}\dpd{}{\phi_{\mu\tau}}\hat{A}_h^{-1}\Big],\\
    \hat{A}^{-1} &\equiv \hat{F}_0^{-1} \hat{F}_1^{-1} \cdots \hat{F}_{N_t-1}^{-1}
  \end{align}
\end{resultbox}
\noindent
This form is essentially identical to~\eqref{eq:force_v1} and thus the same algorithm can be used.

\subsection{Solver}\label{sec:solver_v1}

Linear systems of equations of the form
\begin{align}
  M x = b\label{eq:linear_system_v1}
\end{align}
can be solved using an LU-decomposition and forward-/back-substitution.
It is straight-forward to derive expressions for the result using the algorithm presented in Section~\ref{sec:lu_v1}.

\paragraph{Diagonal Discretization}
Begin by solving the auxiliary system $L y = b$:
\begin{align}
  y_0 &= b_0\label{eq:y0_v1}\\
  y_i &= b_i + F_i K^{-1}y_{i-1} \quad \text{for} \quad i > 0\label{eq:recursion_y_v1}
\end{align}
And then solve the original equations by solving $U x = y$:
\begin{align}
  x_{N_t-1} &= {(1+A)}^{-1} K^{-1} y_{N_t-1}\\
  x_i &= K^{-1} y_i - A_{0i}x_{N_t-1} \quad \text{for} \quad i < N_t-1,
\end{align}
where the definitions for $A$ and $A_{tt'}$ are given in~\eqref{eq:def_A} and~\eqref{eq:def_partial_A}, respectively.

\noindent
Those equations, while formally a valid solution to equation~\eqref{eq:linear_system_v1}, are sub optimal because they involve $K^{-1}$ which is dense.
Both for speed and numerical accuracy it is beneficial to modify those equations as follows.
Expand the recursion~\eqref{eq:recursion_y_v1} to get
\begin{align}
  y_i &= b_i + (F_i K^{-1}) b_{i-1} + (F_{i} K^{-1} F_{i-1}K^{-1}) b_{i-2} + \cdots + (F_i K^{-1} \cdots F_1 K^{-1}) b_0\\
      &= K A_{0i} \big[(F_0^{-1} K F_1^{-1} K \cdots F_i^{-1}) b_i + (F_0^{-1} K F_1^{-1} K \cdots F_{i-1}^{-1}) b_{i-1} + \cdots + (F_0^{-1}) b_0\big]\\
  &\equiv K A_{0i} z_i.
\end{align}
Now substitute this for $y$ in equations~\eqref{eq:y0_v1} and~\eqref{eq:recursion_y_v1} to get the relations
\begin{align}
  z_0 &= F_0^{-1} b_0\\
  z_i &= A_{0(i-1)}^{-1} F_i^{-1} b_i + z_{i-1} \quad \text{for} \quad i > 0.
\end{align}
Finally, the solution $x$ can be obtained as
\begin{align}
  (1 + A^{-1}) x_{N_t-1} &= z_{N_t-1}\\
  A_{0i}^{-1} x_i &= z_i - x_{N_t-1} \quad \text{for} \quad i < N_t-1.
\end{align}
$x$ can best be calculated by constructing $A_{0i}^{-1}$ and $A^{-1}$ and solving the equations using a standard dense solver in order to avoid calculating the numerically less stable $A_{0i}$.
In this form of the equations, $K^{-1}$ is no longer needed.

\paragraph{Exponential Discretization}
The equations for the exponential discretization can again be obtained via substitutions~\eqref{eq:exp_subs_K} and~\eqref{eq:exp_subs_F}.
A solver based on the rearranged equations for the diagonal discretization was found to be more stable than based on the initial equations even though here $K \equiv \mathds{1}$.
The result is
\begin{align}
  z_0 &= \hat{F}_0^{-1} b_0\\
  z_i &= \hat{A}_{0i}^{-1} b_i + z_{i-1} \quad \text{for} \quad i > 0
\end{align}
and
\begin{align}
  (1 + \hat{A}^{-1}) x_{N_t-1} &= z_{N_t-1}\\
  \hat{A}_{0i}^{-1} x_i &= z_i - x_{N_t-1} \quad \text{for} \quad i < N_t-1.
\end{align}

\clearpage
\section{DIRECT\_SQUARE}

This version uses the combined matrix $Q = M(\phi, \tilde{\kappa}, \tilde{\mu}) M^T(-\phi, \sigma_{\tilde{\kappa}}\tilde{\kappa}, -\tilde{\mu})$ to treat particles and holes together.
This algorithm is implemented as well because it is more stable in the spin basis ($\alpha=0$).
It is however much slower than DIRECT\_SINGLE.\@

\subsection{LU-Decomposition}\label{sec:lu_decomposition_v2}

The determinant of $Q$ can be computed via an LU-decomposition using an ansatz similar to~\cite{zivkovic:2013}.
\begin{align}
  L =
  \begin{pmatrix}
    1   &     &        &        &        &\\
    l_0 & 1   &        &        &        &\\
        & l_1 & \ddots &        &        &\\
        &     & \ddots & 1      &        &\\
        &     &        & l_{n-3} & 1      & \\
    h_0 & h_1 & \cdots  & h_{n-3} & l_{n-2} & 1
  \end{pmatrix},
  \; U =
  \begin{pmatrix}
    d_0 & u_0 &        &        &        & v_0    \\
        & d_1 & u_1    &        &        & v_1    \\
        &     & d_2    & \ddots &        & \vdots \\
        &     &        & \ddots & u_{n-3} & v_{n-3} \\
        &     &        &        & d_{n-2} & u_{n-2} \\
        &     &        &        &        & d_{n-1}
  \end{pmatrix}
\end{align}
Note that written like this, each component of $L$ and $U$ is an $N_x \times N_x$ matrix, meaning they do not commute.
It is straight forward to derive an iteration procedure to calculate all elements of $L$ and $U$. Most of those relations can be read off immediately, the others can be proven using
simple induction.\\

\noindent
Compute all except last $d, u, l$:
\begin{align}
  \begin{matrix}
    d_0 = P                & u_0 = T^-_0 & l_0 = T^+_1 d_{0}^{-1} & \\
    d_i = P - l_{i-1}u_{i-1} & u_i = T^-_i & l_i = T^+_{i+1} d_{i}^{-1} & \forall i \in [1, N_t-3] \\
    d_{N_t-2} = P - l_{N_t-3} u_{N_t-3} & & &
  \end{matrix}
\end{align}
And all $v, h$:
\begin{align}
  \begin{matrix}
    v_0 = T^+_0           & h_0 = T^-_{N_t-1} d_0^{-1} & \\
    v_i = - l_{i-1} v_{i-1} & h_i = - h_{i-1} u_{i-1} d_{i}^{-1} & \forall i \in [1, N_t-3]
  \end{matrix}
\end{align}
Finally, compute the remaining blocks:
\begin{align}
  u_{N_t-2} &= T^-_{N_t-2} - l_{N_t-3} v_{N_t-3}\\
  l_{N_t-2} &= (T^+_{N_t-1} - h_{N_t-3} u_{N_t-3}) d_{N_t-2}^{-1}\\
  d_{N_t-1} &= P - l_{N_t-2}u_{N_t-2} - \sum_{i=0}^{N_t-3}\, h_i v_i
\end{align}

\noindent
Even though all $P$, $T^+$, and $T^-$ are sparse, the inversions of $d_i$ produce dense matrices in general meaning that this algorithm uses mostly dense algebra.

\subsection{Action}

The action is easy to evaluate once the LU-decomposition of $Q$ is known. It is (see~\eqref{eq:ferm_action_v2})
\begin{resultbox}
  \vspace{-\baselineskip}
  \begin{align}
    S_\text{ferm} = - \log \det Q = -\log \prod_{i=0}^{N_t-1} \det (d_i) = -\sum_{i=0}^{N_t-1}\, \log \det (d_i)
  \end{align}
\end{resultbox}
\noindent
Since the $d$'s are dense, a standard algorithm for computing $\log\det d_i$ can be used.

\subsection{Force}

The fermionic force at spacetime point $\mu\tau$ is given by
\begin{align}
  {(\dot{\pi}_{\text{ferm}})}_{\mu\tau} = -\dpd{H_{\text{ferm}}}{\phi_{\mu\tau}} = -\dpd{S_\text{ferm}}{\phi_{\mu\tau}} =  \dpd{}{\phi_{\mu\tau}} \log \det Q(\phi, \tilde{\kappa}, \tilde{\mu}, \sigma_{\tilde{\kappa}}).
\end{align}
$\dot{\pi}$ is complex valued in general. For now we will ignore this fact and compute the force from the above equation.
Since we have no closed form solution for $\det Q$, we use Jacobi's formula to compute the derivative of $Q$:
\begin{align}
  {(\dot{\pi}_{\text{ferm}})}_{\mu\tau} = \Tr Q^{-1} \dpd{}{\phi_{\mu\tau}} Q
\end{align}
The derivative acts only on $T^\pm$, thus
\begin{align}
  {(\dot{\pi}_{\text{ferm}})}_{\mu\tau}
  &= Q^{-1}_{xt,x't'} \dpd{}{\phi_{\mu\tau}} Q_{x't',xt}\\
  &= \i Q^{-1}_{xt,x't'} \big[\delta_{t'(t+1)}\delta_{\tau(t'-1)}\delta_{x'\mu}  {(T^+_{t'})}_{x'x} - \delta_{t(t'+1)}\delta_{\tau t}\delta_{x\mu} {(T^-_{t'})}_{x'x}\big]
\end{align}
and finally
\begin{resultbox}
  \vspace{-\baselineskip}
  \begin{align}
    {(\dot{\pi}_{\text{ferm}})}_{\mu\tau} = \i \left[{(T^+_{\tau+1})}_{\mu x}Q^{-1}_{x\tau,\mu(\tau+1)} - Q^{-1}_{\mu(\tau+1),x\tau}{(T^-_\tau)}_{x\mu}\right].
  \end{align}
\end{resultbox}
\noindent
Note that $\mu$ and $\tau$ are \emph{not} summed over even though they are repeated on the right hand sides.\\

\noindent
The internal structure of the $\hat{T}^{\pm}$ for the exponential discretization is different from $T^\pm$ such that the derivative acts in a different way.
Here, the force is
\begin{align}
  {(\dot{\hat{\pi}}_{\text{ferm}})}_{\mu\tau} = \i \hat{Q}^{-1}_{xt,x't'} \big[{(\hat{T}^+_{\tau+1})}_{x'x} \delta_{x\mu} \delta_{t\tau} \delta_{t'(\tau+1)} - {(\hat{T}^-_\tau)}_{x'x} \delta_{x'\mu} \delta_{t'\tau} \delta_{t(t'+1)}\big]
\end{align}
And thus
\begin{resultbox}
  \vspace{-\baselineskip}
  \begin{align}
    {(\dot{\hat{\pi}}_{\text{ferm}})}_{\mu\tau} = \i \left[\hat{Q}^{-1}_{\mu\tau,x'(\tau+1)} {(\hat{T}^+_{\tau+1})}_{x'\mu} - {(\hat{T}^-_\tau)}_{\mu x} \hat{Q}^{-1}_{x(\tau+1),\mu\tau}\right].
  \end{align}
\end{resultbox}

\subsection{Solver}

A linear system of equations $Q x = b$ can be solved via an LU-decomposition and forward-/back-substitution.

\paragraph{Matrix-Vector Equation}
Solve a system of equations for a single right hand side, i.e. $x$ and $b$ are vectors. Start by solving the auxiliary system $L y = b$:
\begin{align}
  y_0 &= b_0\\
  y_i &= b_i - l_{i-1} y_{i-1}\quad \text{for}\quad i = 1, \ldots,  N_t-2\\
  y_{N_t-1} &= b_{N_t-1} - l_{N_t-2} y_{N_t-2} - \textstyle\sum_{j=0}^{N_t-3}\, h_j y_j
\end{align}
Then solve $Ux = y$:
\begin{align}
  x_{N_t-1} &= d_{N_t-1}^{-1} y_{N_t-1}\\
  x_{N_t-2} &= d_{N_t-2}^{-1} (y_{N_t-2} - u_{N_t-2} x_{N_t-1})\\
  x_i &= d_i^{-1} (y_i - u_i x_{i+1} - v_i x_{N_t-1}) \quad \text{for} \quad i = 0, \ldots, N_t-3
\end{align}

\paragraph{Inversion}
Invert $Q$ by solving $Q X = \mathds{1}$ for $X \equiv Q^{-1}$, where $\mathds{1}$ is the $N_t N_x \times N_t N_x$ unit matrix.
Start by solving the auxiliary equation $L Y = \mathds{1}$:
\begin{align}
  y_{0j} &= \delta_{0j}\\
  y_{ij} &= \begin{cases}
    \textstyle\prod_{k=j}^{i-1} (-l_{k}) & \mathrm{for}\quad i > j\\
    \delta_{ij} & \mathrm{for}\quad i\leq j
  \end{cases}, \quad\text{for}\quad i = 1, \ldots, N_t-2\\
  y_{(N_t-1)j} &= \delta_{(N_t-1)j} - \textstyle\sum_{k=j}^{N_t-3} h_{k} y_{kj} - l_{N_t-2} y_{(N_t-2)j}
\end{align}
Then solve $U X = Y$:
\begin{align}
  x_{(N_t-1)j} &= d_{N_t-1}^{-1}y_{(N_t-1)j}\\
  x_{(N_t-2)j} &= d_{N_t-2}^{-1}(y_{(N_t-2)j} - u_{N_t-2}x_{(N_t-1)j})\\
  x_{ij} &= d_{i}^{-1}(y_{ij} - u_{i}x_{(i+1)j} - v_{i}x_{(N_t-1)j}) \quad\text{for}\quad i = 0, \ldots, N_t-3
\end{align}
Like in the LU-decomposition itself, those relations can be read off, or proven using simple induction.
Apart from the last row, the $y$'s are independent from each other while the $x$'s have to be computed iterating over rows from $N_t-1$ though 0. However, different columns never mix.

\clearpage
\bibliographystyle{unsrt}
\bibliography{references}

\end{document}


\documentclass{article}

\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{placeins}

\usepackage[margin=1in]{geometry}
\usepackage{float}

\def\hwtitle{Computational Physics HW4}
\def\hwauthor{Ethan Rooney}
\def\hwdate{2020-02-26}

\usepackage{fancyhdr}
\lhead{\hwauthor}
\chead{\hwtitle}
\rhead{\hwdate}
\lfoot{\hwauthor}
\cfoot{}
\rfoot{\thepage}
\renewcommand{\footrulewidth}{0.4pt}
\pagestyle{fancy}

\author{\hwauthor}
\title{\hwtitle}
\date{\hwdate}

\begin{document}

\maketitle
\thispagestyle{fancy}

\section{Introduction}

This weeks homework is the continuation of Week 3. We are extending and generalizing from 2 body interactions to $n$-body systems. The math is essentially unchanged from last weeks assignment. We are utilizing the RK2 integrator, with more bodies.

\section{Results}

\subsection{Question 0}

Here we check the validity of our generalized RK2 integrator with a 2 body problem. A "earth-like" object is put into motion 1 AU away from a "sun-like" object with the same velocity as the earth. We see a circular orbit. This is a good start.

\begin{figure}[!htb]
	\begin{center}
		\includegraphics[width=0.6\textwidth]{images/p0.pdf}
	\end{center}
	\caption{Qualitative Check of the Integrator. An Earth-like object completes an orbit in 1 year.}
\label{fig:qual}
\end{figure}
\FloatBarrier

\subsection{Question 1}

We continue the extension of last weeks work with a trial run of a 3-body system much like the one we find in our own solar system.

\subsubsection{Part 1}

The Moon's orbit is close enough to Earth's that the orbit of Earth is obscured.

\begin{figure}[!htb]
	\begin{center}
		\includegraphics[width=0.6\textwidth]{images/p1-1a.pdf}
	\end{center}
	\caption{3 Body system of Sun, Earth, and Moon.}
\label{fig:qual}
\end{figure}
\FloatBarrier

Over 8 Years the Moon slowly drifts towards, and away from the earth with a slight net movement away.

\begin{figure}[!htb]
	\begin{center}
		\includegraphics[width=0.6\textwidth]{images/p1-1b.pdf}
	\end{center}
	\caption{8 Years of the Moon's Orbit. The thick ring of blue is overlapping Sinusoidal orbits.}
\label{fig:qual}
\end{figure}
\FloatBarrier

We can see the ebb and flow of the moons orbit clearly in the Graphic below. There is a gentle trend overall for the moon to drift away from the earth.

\begin{figure}[!htb]
	\begin{center}
		\includegraphics[width=0.6\textwidth]{images/p1-1c.pdf}
	\end{center}
	\caption{The Moon's orbit is perturbed by the Sun's gravity.}
\label{fig:qual}
\end{figure}
\FloatBarrier

\begin{figure}[!htb]
	\begin{center}
		\includegraphics[width=0.6\textwidth]{images/p1-1d.pdf}
		\includegraphics[width=0.6\textwidth]{images/p1-1e.pdf}
		\includegraphics[width=0.6\textwidth]{images/p1-1f.pdf}
	\end{center}
	\caption{Through the oscillations of the Moon's Orbit Kinetic energy is exchanged for Potential Energy, but Total Energy is conserved in this simulation.}
\label{fig:qual}
\end{figure}
\FloatBarrier

\subsubsection{Part 2}

What Happens if we change the parameters of the Earth Sun and Moon?
Below are similar plots, but instead of the actual values of the Sun, Earth and Moon, I plotted $M_1 = 1, M_2 = 10^{-2}, M_3 = 10^{-4}, r_{23} = 0,06$ AU.

\begin{figure}[!htb]
	\begin{center}
		\includegraphics[width=0.6\textwidth]{images/p1-2a.pdf}
	\end{center}
	\caption{With a more massive planet orbiting the Sun we see a noticeable drift of the system. Furthermore the initial conditions are such that the sun plays a larger roll in shaping the orbit of the Moon.}
\label{fig:qual}
\end{figure}
\FloatBarrier

\begin{figure}[!htb]
	\begin{center}
		\includegraphics[width=0.6\textwidth]{images/p1-2b.pdf}
	\end{center}
	\caption{The Perturbations Caused by the Sun are much more Pronounced.}
\label{fig:qual}
\end{figure}
\FloatBarrier

\begin{figure}[!htb]
	\begin{center}
		\includegraphics[width=0.6\textwidth]{images/p1-2c.pdf}
	\end{center}
	\caption{Interesting Harmonics Form in the Moon's orbit.}
\label{fig:qual}
\end{figure}
\FloatBarrier

\begin{figure}[!htb]
	\begin{center}
		\includegraphics[width=0.6\textwidth]{images/p1-2d.pdf}
		\includegraphics[width=0.6\textwidth]{images/p1-2e.pdf}
		\includegraphics[width=0.6\textwidth]{images/p1-2f.pdf}
	\end{center}
	\caption{Total Energy of the system is conserved.}
\label{fig:qual}
\end{figure}
\FloatBarrier

\subsubsection{Part 3}

Plotted for $M_1 = 1, M_2 = 10^{-2}, M_3 = 10^{-4}, r_{23} = 0,08$ AU.

\begin{figure}[!htb]
	\begin{center}
		\includegraphics[width=0.6\textwidth]{images/p1-3a.pdf}
	\end{center}
	\caption{The Moon is Pulled Free from the Earths Orbit, and Establishes a Planetary orbit.}
\label{fig:qual}
\end{figure}
\FloatBarrier

\begin{figure}[!htb]
	\begin{center}
		\includegraphics[width=0.6\textwidth]{images/p1-3b.pdf}
	\end{center}
	\caption{The Moon Leaving Orbit from the Planet}
\label{fig:qual}
\end{figure}
\FloatBarrier

\begin{figure}[!htb]
	\begin{center}
		\includegraphics[width=0.6\textwidth]{images/p1-3c.pdf}
	\end{center}
	\caption{Moon Ejected from Orbiting the Planetary Body, but remains in the system.}
\label{fig:qual}
\end{figure}
\FloatBarrier

\begin{figure}[!htb]
	\begin{center}
		\includegraphics[width=0.6\textwidth]{images/p1-3d.pdf}
		\includegraphics[width=0.6\textwidth]{images/p1-3e.pdf}
		\includegraphics[width=0.6\textwidth]{images/p1-3f.pdf}
	\end{center}
	\caption{Exchange of Energy Types, Total Energy Conserved}
\label{fig:qual}
\end{figure}
\FloatBarrier

\subsubsection{Part 4}

Plotted for $M_1 = 1, M_2 = 10^{-1}, M_3 = 10^{-4}, r_{23} = 0.2$ AU.

\begin{figure}[!htb]
	\begin{center}
		\includegraphics[width=0.6\textwidth]{images/p1-4a.pdf}
	\end{center}
	\caption{Moon like object ejected from system.}
\label{fig:qual}
\end{figure}
\FloatBarrier

Considerable drift is now occurring in the system, and the "Moon" is rapidly ejected from orbit.

\begin{figure}[!htb]
	\begin{center}
		\includegraphics[width=0.6\textwidth]{images/p1-4b.pdf}
	\end{center}
	\caption{The "Moon" Completes a few orbits before being Ejected from the system.}
\label{fig:qual}
\end{figure}
\FloatBarrier

\begin{figure}[!htb]
	\begin{center}
		\includegraphics[width=0.6\textwidth]{images/p1-4c.pdf}
	\end{center}
	\caption{Bye Moon!}
\label{fig:qual}
\end{figure}
\FloatBarrier

\begin{figure}[!htb]
	\begin{center}
		\includegraphics[width=0.6\textwidth]{images/p1-4d.pdf}
		\includegraphics[width=0.6\textwidth]{images/p1-4e.pdf}
		\includegraphics[width=0.6\textwidth]{images/p1-4f.pdf}
	\end{center}
	\caption{Energy of the system Dominated by they Sun/Earth interactions.}
\label{fig:qual}
\end{figure}
\FloatBarrier

\subsection{Question 2}

Plotted for $M_1 = M_2 = M_3 = 1, x_1=(0,0), v_1 = (1,-1), x,2 = (1,0), v_2 = (0,6), x_3=(2,0), v_3=(0,6)$

Below we see the motion of the 3 bodies relative to the center of mass for the system. It appears very chaotic.

\begin{figure}[!htb]
	\begin{center}
		\includegraphics[width=0.6\textwidth]{images/p2-1a.pdf}
	\end{center}
	\caption{4 Body system Evolution over 10 years. Corrected for Center of Mass Motion}
\label{fig:qual}
\end{figure}
\FloatBarrier

With out correcting for the drift of the system, the net momentum of the system causes a drift in the positive x and negative y directions.

\begin{figure}[!htb]
	\begin{center}
		\includegraphics[width=0.6\textwidth]{images/p2-1b.pdf}
	\end{center}
	\caption{Overall motion of system. Notice down and left trend.}
\label{fig:qual}
\end{figure}
\FloatBarrier

We can see the ebb and flow of the moons orbit clearly in the Graphic below. There is a gentle trend overall for the moon to drift away from the earth.

\begin{figure}[!htb]
	\begin{center}
		\includegraphics[width=0.6\textwidth]{images/p2-1c.pdf}
		\includegraphics[width=0.6\textwidth]{images/p2-1d.pdf}
		\includegraphics[width=0.6\textwidth]{images/p2-1e.pdf}
	\end{center}
	\caption{Energy Exchange Of 3 equal mass bodies in a system. Total Energy is conserved until $\sim$9 year. This discontinuity occurs when 2 of the bodies had a near collision and where ejected. The System is no longer gravitationally bound.}
\label{fig:qual}
\end{figure}
\FloatBarrier

In the images of energy above, when the bodies get very close, the rate of change of the position and velocity are high enough that the linear approximation over dt is no longer valid. The actual effects of these small separation vectors would most likely caused a collision of the bodies, or of a breakup of one or both of the bodies into smaller chunks. One possible solution is to add collision detection, and allow for a perfectly inelastic collision of the 2 bodies to form 1 large gravitationally bound body, or to implement a "monitoring system" that could dial up or down the time step for systems when 2 bodies get very close.

\subsection{Question 3}
For the $n$-body problem the compute resources required scales as $\mathcal{O}(n^2)$. This is because for each body every other body has to be computed against it. To reduce steps you could compute the effects of $i$ on $j$ at the same time you compute the effects of $j$ on $i$ then you wouldn't need to loop over the full list every time, just loop through  $i>j$. But this doesn't change the  $\mathcal{O}(n^2)$, it would just save some time in the looping. Alternatively, you could threshold each body, and then only compute the effects of nearby objects. To do this you would need to establish a hierarchy of bodies, for example you would compute the effects of the Sun on the Moon, but you could neglect the effects of the Moon on the Sun, while still computing the effect of the Moon on Earth. You could also compute the effects of the CoM on each object. So you would find the CoM for bodies that are relatively close to each other then use the CoM to compute the effects on bodies that are far enough away to treat a cluster of objects as one body.

\subsection{Bonus Question 4}

See the Picture below.

\begin{figure}[!htb]
	\begin{center}
		\includegraphics[width=0.6\textwidth]{images/p4-1a.pdf}
	\end{center}
	\caption{4 Body system with 2 Sun sized objects and an Earth and Moon like Object. Near the end of the Simulation we see the Moon Ejected from the system.}
\label{fig:qual}
\end{figure}
\FloatBarrier

Here we have created a binary star system with 2 one Sol mass objects.

Around one of the Stars, a Earth/Moon system is put into orbit. The orbits of the Planet/Moon system is complicated immensely by the existence of the second star. Because of this the orbits they follow are highly irregular and eventually the Moon is ejected from the system when it ventures too close to its host planet.

The Dominant Forces are the 2 main stars, while the Planets gravity is subdominant on the moon.

\section{Conclusions}

This Project was rather neat. One tweak I made to the code was to ignore interactions between objects if the length of the separation vector was zero. This prevented a significant number of \texttt{-nan} errors, this was especially useful when working with 2 body systems to suppress errors caused by nonexistent bodies that had mass of zero.

\end{document}


\chapter{Normal forms}\setcounter{ProbPart}{0}

\problempart
\label{pr.DNF}
Consider the following sentences:
	\begin{earg}
		\item $(A \eif \enot B)$
		\item $\enot (A \eiff B)$
		\item $(\enot A \eor \enot (A \eand B))$
		\item $(\enot (A \eif B ) \eand (A \eif C))$
		\item $(\enot (A \eor B) \eiff ((\enot C \eand \enot A) \eif \enot B))$
		\item $((\enot (A \eand \enot B) \eif C) \eand \enot (A \eand D))$
	\end{earg}
For each sentence, find an equivalent sentence in DNF and one in CNF.
\myanswer{We give a solution for (2). The truth table for $\enot (A \eiff B)$ is:
\begin{center}
\begin{tabular}{c c | l}
$A$ & $B$ & $\enot (A \eiff B)$\\
\hline
T & T & F \\
T & F & T \\
F & T & T \\
F & F & F \\
\end{tabular}
\end{center}
A sentence in DNF can be read off from lines 2 and 3:
\[(A \eand \enot B) \eor (\enot A \eand B)\]
and one in CNF from lines 1 and~4:
\[(\enot A \eor \enot B) \eand (A \eor B).\]}

\stepcounter{chapter} % Functional completeness

\chapter{Proving equivalences}\setcounter{ProbPart}{0}

\problempart
\label{pr.DNF2}
Consider the following sentences:
\begin{earg}
	\item $(A \eif \enot B)$
	\item $\enot (A \eiff B)$
	\item $(\enot A \eor \enot (A \eand B))$
	\item $(\enot (A \eif B ) \eand (A \eif C))$
	\item $(\enot (A \eor B) \eiff ((\enot C \eand \enot A) \eif \enot B))$
	\item $((\enot (A \eand \enot B) \eif C) \eand \enot (A \eand D))$
\end{earg}
For each sentence, find an equivalent sentence in DNF and one inCNF by giving a chain of equivalences. Use (Id), (Absorp), and (Simp) to simplify your sentences as much as possible.
\myanswer{We give a solution for (2). Removing `$\eiff$' and pushing negations inward is common to both:
\begin{align*}
	& \enot (A \eiff B)\\
	& \enot((A \eif B) \eand (B \eif A)) && \text{Bicond}\\
	& \enot((\enot A \eor B) \eand (B \eif A)) && \text{Cond}\\
	& \enot((\enot A \eor B) \eand (\enot B \eor A)) && \text{Cond}\\
	& \enot(\enot A \eor B) \eor \enot(\enot B \eor A) && \text{DeM}\\
	& (\enot\enot A \eand \enot B) \eor (\enot\enot B \eand \enot A) && \text{DeM}\\
	& (A \eand \enot B) \eor (\enot\enot B \eand \enot A) && \text{DN}\\
	& (A \eand \enot B) \eor (B \eand \enot A) && \text{DN}
\intertext{The result is now in DNF. To obtain a CNF, we keep going, using (Comm) and (Dist):}
& ((A \eand \enot B) \eor B) \eand ((A \eand \enot B) \eor \enot A) && \text{Dist}\\
& (B \eor (A \eand \enot B)) \eand ((A \eand \enot B) \eor \enot A) && \text{Comm}\\
& ((B \eor A) \eand (B \eor \enot B)) \eand ((A \eand \enot B) \eor \enot A) && \text{Dist}\\
& ((B \eor A) \eand (B \eor \enot B)) \eand (\enot A \eor (A \eand \enot B)) && \text{Comm}\\
& ((B \eor A) \eand (B \eor \enot B)) \eand ((\enot A \eor A) \eand (\enot A \eor \enot B)) && \text{Dist}\\
\intertext{The result can be simplified using (Simp):}
& (B \eor A) \eand ((\enot A \eor A) \eand (\enot A \eor \enot B)) && \text{Simp}\\
& (B \eor A) \eand (\enot A \eor \enot B) && \text{Simp}
\end{align*}}

\stepcounter{chapter} % Soundness