%!TEX root = ../notes.tex
\section{February 10, 2022}

\subsection{Congruences \emph{continued}}
\recall that for $m\in\ZZ_+$, $a,b\in \ZZ$, the linear congruence
\[ax\equiv b\mod{m}\]
has a solution if and only if $(a, m)\mid b$. Unwinding this gives Bezout's identity.

\textbf{Q: How do we actually find a solution?}

\textbf{A:} Either guess and check; or apply the following algorithm:
\begin{enumerate}[1)]
    \item Divide all terms in the congruence by $d = (a, m)$.
    \item If step $1$ yields
          \[a'x\equiv b'\mod m'\]
          with $(a', m') = 1$, then $d:= (a', b')$ is a \ul{unit} mod $m'$, so we can divide both sides by $d'$.
          \[a'd'^{-1}x\equiv b'd'^{-1}\mod m'\]
    \item Let $a''x\equiv b''\mod m$ be the result so far. We replace $b''$ by some $b''+km'$\footnote{Since made $a'$ and $m'$ coprime, we have $(a', m')=1$ so we can indeed solve congruence $a''q \equiv b''+km'$ gives noncoprime pairs.} such that $(a'', b''+km')>1$ allows us to repeat step $2$. This results in some $a'''$ such that $|a'''| < |a''|$.

          Given that we repeat this process, this must eventually terminate, since the absolute values of the $a$ terms are strictly decreasing each time.
\end{enumerate}
\begin{example}
    Let $10x\equiv 6\mod{14}$.

    \begin{enumerate}[1)]
        \item $(a, m) = (10, 14) = 2$ so we divide through by $2$. \[5x\equiv 3\mod{7}\]
        \item Irrelevant since $(5, 3)$ are coprime.
        \item Consider integers of form $3+7k$, and see which are divisible by $5$. We can take $k=1$. We get
              \[5x\equiv 10\mod 7\]
        \item[2)] Divide by $(5, 10) = 5$ so we have $x\equiv 2\mod{7}$.
    \end{enumerate}
\end{example}

\subsection{Simultaneous Linear Congruences}
\recall the CRT/Sun-tzu's theorem.
\begin{theorem}[Sun-tzu's Theorem / Chinese Remainder Theorem]\label{thm:crt}
    Suppose that $m = m_1m_2\cdots m_t$ with $(m_i, m_j) = 1 \forall i\neq j$.

    Let $b_1, b_2\dots, b_t$ be integers, and consider the system of congruences
    \begin{align}
        x_1 & \equiv b_1\mod{m_1}        \label{eqn:crt-congruences} \tag{$*$} \\
        x_2 & \equiv b_2\mod{m_2} \nonumber                                    \\
            & \qquad \vdots \nonumber                                          \\
        x_t & \equiv b_t\mod{m_t} \nonumber
    \end{align}
    Then this system has a unique solution modulo $m$\footnote{That is, we have at least one solution, and we can shift it by \emph{any} multiple of $m$.}.
\end{theorem}
\begin{proof}
    Let $n_i = m_1m_2\cdots \cancel{m_i}\cdots m_t = \frac{m}{m_i}$ for each $i$. Since $m_i$ is coprime to $m_j,\ \forall j\neq i$, we have $(n_i, m_i) = 1 \forall i$. Then, there exists solutions $r_i, s_i\in \ZZ$ such that
    \[r_im_i + s_in_i = 1\]
    Let $e_i = s_in_i$. Then for each $i$,
    \[e_i\equiv 1\mod m_i\]
    and $e_i\equiv 0\mod m_j,\ \forall j\neq i$.

    Our goal is to ultimately show
    \[\ZZ/m\ZZ\simeq \ZZ/m_1\ZZ\times \ZZ/m_2\ZZ\times\cdots\times\ZZ/m_t\ZZ\]
    with each $e_i$ generating the ``$\ZZ/m_i\ZZ$ piece''.

    Set \[x_0 = \sum_{i=1}^t b_ie_i\] so that $x_0\equiv b_i\pmod{m_i}\ \forall i$, so $x_0$ is a solution to \cref{eqn:crt-congruences}.

    Suppose $x_1$ is another solution. Then we have
    \[x_1-x_0\equiv 0\mod{m_i}\ \forall i, 1\leq i\leq t\]
    Since the $m_i$ are pairwise coprime, we get $m\mid x_1-x_0$.
\end{proof}

\subsection{Structure of Unit Groups}
\recall in Math 1530, we learned Lagrange's theorem
\begin{theorem}[Lagrange's Theorem]
    If $G$ is a finite group, then for every subgroup $H$ of $G$, we have $|H| \bigm\vert |G|$.
\end{theorem}
\begin{corollary}
    If $G$ is a finite group of order $n$, and $a\in G$, then $a^n = e$, where $e$ is the identity of the group.
\end{corollary}
We've seen that $\left\vert U(m) \right\vert = \phi(m)$.\footnote{For notation, we use $U(m) := (\ZZ/m\ZZ)^\times$.} Applying Lagrange's theorem, we have Euler's theorem:
\begin{theorem}[Euler's Theorem]\label{thm:eulers-thm}
    For any $a\in \ZZ$ with $(a, m) = 1$, we have $a^{\phi(m)}\equiv 1\mod{m}$.
\end{theorem}
\begin{definition}
    A subset $R$ of $\ZZ$ is said to be a \ul{reduced set of residues mod $m$} if $R$ contains exactly one element from each of the $\phi(m)$ congruence classes that are units mod $m$.
\end{definition}
\begin{proof}[Alternate proof of \cref{thm:eulers-thm}]
    Let $R = \{r_1, r_2, \dots, r_{\phi(m)}\}$ be a reduced set of residues mod $m$. If $(a, m) = 1$, then $aR$ is also a reduced set of residues mod $m$. Thus, if $x_1, x_2, \dots, x_{\phi(m)}\in aR$ (pairwise distinct), then
    \begin{align*}
        x_1x_2\cdots x_{\phi(m)}               & \equiv r_1r_2\cdots r_{\phi(m)}\mod{m}  \\
        (ar_1)(ar_2)\cdots (ar_{\phi(m)})      & \equiv r_1r_2\cdots r_{\phi(m)}\mod{m}  \\
        a^{\phi(m)} (r_1r_2\cdots r_{\phi(m)}) & \equiv r_1r_2\cdots r_{\phi(m)} \mod{m} \\
        \intertext{since all the $r_i$ are units mod $m$, we divide through}
        a^{\phi(m)}                            & \equiv 1\mod{m}
    \end{align*}
    Which is as desired.
\end{proof}

We'll be studying roots of polynomials over $\ZZ/m\ZZ$, especially polynomials of the form $x^d - a$.

By Sun-tzu's theorem, the case of $m$ being a prime power is especially important. This turns out to have a lot to do with the case that $m=p$ is a prime itself.

First something further \emph{afield}:
\begin{proposition}\label{prop:d-roots-in-fp}
    If $p$ is a prime and $p\nmid d$ for $d\in\ZZ_+$, then the polynomial
    \[x^d - a\in (\ZZ/p\ZZ)[x],\qquad a\not\equiv 0\mod{p}\]
    has exactly $d$ roots in some extension of $\FF_p$.

    Conversely, if $p\mid d$, then there are fewer than $d$ roots in any extension of $\FF_p = \ZZ/p\ZZ$.
\end{proposition}

The proof uses the following proposition:
\begin{proposition*}
    A nonzero polynomial $f\in K[x]$ is \ul{separable} if and only if it is relatively prime to its derivative $f'$. (A separable polynomial whose roots in its algebraic closure $\overline{K}$ whose roots are all distinct).
\end{proposition*}
\begin{proof}
    ~\begin{description}
        \item[$\Rightarrow$ Right Direction:]
            Suppose $f$ is separable and $\alpha$ be any root of $f$. Then $f(x) = (x-\alpha)h(x)$, where $h(\alpha)\neq 0$ since $\alpha$ is a non-repeated root.

            We have $f'(\alpha) = h(\alpha)\neq 0$, so $\alpha$ is not a root of $f'$. Thus $f$ and $f'$ have no common roots, so they are coprime.

        \item[$\Leftarrow$ Left Direction:]
            Prove by contrapositive. Suppose $f$ is not separable. i.e. it has some repeated root which we call $\alpha$.

            Then $f(x) = (x-\alpha)^2g(x)$, so $f'(x) = (x-\alpha)^2 g'(x) + 2(x-\alpha)g(x)$. We see that $x-\alpha$ divides both $f$ and $f'$ so $(f, f')\neq 1$.
    \end{description}
    Which concludes the bidirectional.
\end{proof}
\begin{proof}[Proof of \cref{prop:d-roots-in-fp}]
    We have $f(x) = x^d - a$, $a\not\equiv 0\pmod{p}$ has $d$ distinct solutions in some extension of $\FF_p = \ZZ/p\ZZ$, because \[f'(x) = dx^{d-1}\pmod{p}\] and with $0$ as its only root but $0$ is not a root of $f$. By above we have that $f$ is separable.

    Conversely, if $p\mid d$, then
    \[f'(x)\equiv 0\pmod{p},\]
    so $(f, f')\neq 1$, meaning that $f$ is not separable.
\end{proof}

\begin{proposition}[4.1.2 of text]\label{prop:d-roots}
    If $p$ is a prime and if $d\mid p-1$, then the polynomial
    \[x^{d-1}\in (\ZZ/p\ZZ)[x]\] has exactly $d$ roots in the base field $\FF_p = \ZZ/p\ZZ$.
\end{proposition}
\begin{proof}
    We know this is true in the case of $d = p-1$ because of Fermat's Little Theorem (also Euler's Theorem).

    We also note that $(x^d - 1)\mid (x^{p-1} - 1)$. Since $x^{p-1} - 1$ has all roots in the base field by FLT, $x^d - 1$ had better also retain its roots in the base field $\FF_p$ by contradiction.
\end{proof}


\section{Attitude} \label{Ch:Attitude}

The attitude of a spacecraft can be defined qualitatively as how the
spacecraft is oriented in inertial space, and how that orientation
changes in time.  GMAT has the ability to model the orientation and
rate of rotation of a spacecraft using several different
mathematical models. Currently, GMAT assumes that a spacecraft is a
rigid body.

There are many ways to quantitatively describe the orientation and
rate of rotation of a spacecraft, just like there are many ways we
can quantitatively describe an orbit state.  Let's define any set of
numbers that can uniquely define the spacecraft attitude as an
attitude parameterization. GMAT allows the users to use several
common attitude parameterizations including quaternions, Euler
angles, the Direction Cosine Matrix (DCM), Euler angle rates, and
the angular velocity vector. Given an initial attitude state, GMAT
can propagate the attitude using one of several kinematic attitude
propagation models.

In this chapter, we discuss the attitude parameterizations supported
in GMAT, and how to convert between the different types.  We discuss
the internal state parameterization that GMAT uses.   Next we
investigate the types of attitude modes in GMAT and discuss in
detail how GMAT propagates the spacecraft attitude in all of the
Kinematic attitude modes.  We conclude the chapter with a discussion
of how GMAT converts between different attitude parameterizations.

\subsection{Attitude Propagation}

Given a set of initial conditions that define the attitude, GMAT can
propagate the attitude using several methods.  Currently, GMAT only
supports kinematic attitude propagation.  In Kinematic mode, the
attitude is defined by describing the desired orientation with
respect to other objects such as spacecraft or celestial bodies.
With this information, GMAT can calculate the required attitude to
satisfy the desired geometrical configuration.  This section
presents the different Kinematic attitude modes, and how GMAT
calculates the attitude state in each mode.  Let's begin by looking
at the internal attitude state representation and how the user can
define initial conditions.

\vspace{- .1 in} \subsubsection{Internal State Representation and
Attitude Initial Conditions}

Certain attitude parameterizations are more useful for attitude
propagation, while other attitude parameterizations are more
intuitive for providing attitude initial conditions or output. GMAT
uses different internal parameterizations of the attitude
orientation depending upon the attitude mode.  The type of
parameterization is chosen to make the attitude propagation
algorithms natural and convenient.  For the kinematic modes, GMAT
uses the DCM that represents the rotation from the inertial system
to the body axes as the attitude orientation parameterization. In
the future, when 6 degree of freedom attitude modelling is
implemented, GMAT will use the quaternion that represents the
rotation from the inertial system to the body axes. GMAT uses the
angular velocity of the body with respect to the inertial frame,
expressed in the body frame, $\{\boldsymbol\omega_{IB}\}_B$, as the
rate portion of the state vector.

For convenience, the user can choose a coordinate system in which to
define the initial attitude state.  Let's call this system
$\mathcal{F}_i$.  The user can define the initial attitude
orientation with respect to $\mathcal{F}_i$ using Euler angles, the
DCM, or quaternions.  The user can define the body rate with respect
to $\mathcal{F}_i$ by defining the angular velocity in
$\mathcal{F}_i$, $\{\boldsymbol\omega_{IB}\}_i$, or by defining the
Euler angle rates.  Note that not all attitude modes require these
three pieces of information.  The specific inputs for each attitude
mode are discussed below, along with details about how attitude
propagation is performed in each mode.

\subsubsection{Kinematic Attitude Propagation}

The Kinematic attitude mode allows a user to define a geometrical
configuration based on the relative position of a spacecraft with
respect to other spacecraft or celestial bodies, and with respect to
different coordinate systems.  In Kinematic mode, GMAT does not
integrate the attitude equations of motion, but rather calculates
the attitude based on the geometrical definition provided by the
user.  There are several Kinematic modes to choose from.  The
different modes allow the user to conveniently define the spacecraft
attitude depending on the type of attitude profile needed for a
specific mission.  To begin, let's look at how GMAT calculates the
attitude state in the Coordinate System Fixed attitude mode
(CSFixed).

\subsubsection{Coordinate System Fixed Mode}

In the CSFixed attitude mode, the user supplies two pieces of
information. They first specify a coordinate  system in which to fix
the attitude, $\mathcal{F}_i$.  $\mathcal{F}_i$ can be any of the
default coordinate systems or any user defined coordinate system.
Secondly, the user specifies how the body axis system,
$\mathcal{F}_B$ is oriented with respect to $\mathcal{F}_i$ by
defining $\mathbf{R}_{Bi}$ or an equivalent parameterization. With
this information, GMAT calculates the rotation from the inertial to
the body axes and the angular velocity of the body with respect to
the inertial frame, expressed in the body frame,
$\{\mathbf{\boldsymbol\omega}_{IB}\}_B$.

GMAT calculates the rotation matrix from $\mathcal{F}_i$  to
$\mathcal{F}_B$, $\mathbf{R}_{Bi}$, from the initial conditions
provided by the user.  For CSFixed mode, $\mathbf{R}_{Bi}$ is
constant and is stored for use in the equations below.  Knowing
$\mathbf{R}_{Bi}$, we can calculate the rotation matrix from the
inertial frame to the body frame, $\mathbf{R}_{BI}$, using the
following equation
%
\begin{equation}
     \mathbf{R}_{BI} = \mathbf{R}_{Bi}\mathbf{R}_{iI}
     \label{Eq:CSFixedRotationMatrix}
\end{equation}
%
$\mathbf{R}_{iI}$ is the rotation matrix from $\mathcal{F}_I$ to
$\mathcal{F}_i$ and GMAT knows how to calculate this matrix for all
allowable $\mathcal{F}_i$.  For details on the calculation of this
matrix for all coordinate systems in GMAT  see
Ch.~\ref{Ch:CoordinateSystems}.

To calculate $\{\mathbf{\boldsymbol\omega}_{IB}\}_B$, we start from
Eulers' equation:
%
\begin{equation}
   \dot{\mathbf{R}}_{BI} =
   -\{\mathbf{\boldsymbol\omega^\times}_{IB}\}_B\mathbf{R}_{BI}
   \label{Eq:CSFixedKinematics}
\end{equation}
%
where
%
\begin{equation}
       \{ \mathbf{\boldsymbol\omega^\times}_{IB}\}_B = \begin{pmatrix}
     0 & -\omega_3 & \omega_2\\
     \omega_3 & 0 & -\omega_1\\
     -\omega_2 & \omega_1 & 0\\
     \end{pmatrix}
\end{equation}
%
and $\{\mathbf{\boldsymbol\omega}_{IB}\}_B$ is the rotation of
$\mathcal{F_B}$ with respect to $\mathcal{F}_I$, expressed in
$\mathcal{F}_B$.
%
Solving Eq.~\ref{Eq:CSFixedKinematics} for
$\{\mathbf{\boldsymbol\omega}_{IB}^\times\}_B$ we obtain
%
\begin{equation}
   \{\mathbf{\boldsymbol\omega^\times}_{IB}\}_B =  -\dot{\mathbf{R}}_{BI}
   \mathbf{R}_{BI}^{T}
   \label{Eq:CSFixedKinematics2}
\end{equation}
%
Taking the derivative of Eq.~(\ref{Eq:CSFixedRotationMatrix}) with
respect to time yields
%
\begin{equation}
     {\mathbf{\dot R}_{BI}} = \mathbf{R}_{Bi}{\mathbf{\dot
     R}_{iI}}\label{Eq:CSFixedTimeDerivative}
\end{equation}
%
because by definition, for the CSFixed mode,  $ \mathbf{\dot R}_{Bi}
= \mathbf{0}$. Substituting Eq.~(\ref{Eq:CSFixedTimeDerivative})
into Eq.~(\ref{Eq:CSFixedKinematics2}) we obtain
%
%
\begin{equation}
   \{\mathbf{\boldsymbol\omega^\times}_{IB}\}_B =  -\mathbf{R}_{Bi}{\mathbf{\dot
     R}_{iI}} \mathbf{R}_{BI}^{T}
   \label{Eq:CSFixedKinematics3}
\end{equation}
%
where $\mathbf{R}_{Bi}$ is known from user input, and
$\mathbf{R}_{BI}$ is known from Eq.~(\ref{Eq:CSFixedRotationMatrix})
. GMAT knows how to calculate $\mathbf{\dot{R}}_{iI}$ for all
allowable $\mathcal{F}_i$ and details are contained in
Ch.~\ref{Ch:CoordinateSystems}.

In summary, in CSFixed mode, Eq.(\ref{Eq:CSFixedRotationMatrix}) is
used to calculate $\mathbf{R}_{BI}$, and
Eq.~(\ref{Eq:CSFixedKinematics3}) is used to calculate
$\{\mathbf{\boldsymbol\omega}_{IB}^x\}_B$.  If another attitude
parameterization is required, GMAT uses the algorithms in
Sec.~\ref{Sec:AttitudeParameterizations} to transform from
$\mathbf{R}_{BI}$ and $\{\mathbf{\boldsymbol\omega}_{IB}\}_B$ to the
required parameterization.  Now let's look at the spinning
spacecraft mode.

\subsubsection{Spinning Spacecraft Mode}

In spinning spacecraft mode, GMAT propagates the attitude by
assuming the spin axis direction is fixed in inertial space.  The
spacecraft attitude at some time, $t$, is determined from the
attitude initial conditions, the angular velocity vector, and the
elapsed time from the initial spacecraft epoch.  Let's take a closer
look at the calculations.

In the spinning spacecraft mode, the user provides three pieces of
information.  They first choose a coordinate system,
$\mathcal{F}_i$, in which to define the initial conditions.
Secondly, they define the initial orientation with respect to
$\mathcal{F}_i$ by providing $\mathbf{R}_{Bi}$ or an equivalent
parameterzation that is then converted to the DCM.   The user also
provides the angular velocity of the body axes with respect to the
inertial axes expressed in $\mathcal{F}_i$, $\{
\boldsymbol\omega_{IB}\}_i$.

To calculate $\mathbf{R}_{BI}(t)$ where $t$ is an arbitrary epoch,
we begin by calculating $\mathbf{R}_{B_{o}I}$ where
$\mathbf{R}_{B_{o}I} = \mathbf{R}_{BI}(t_o)$.  We calculate
$\mathbf{R}_{B_{o}I}$ using
%
\begin{equation}
     \mathbf{R}_{B_{o}I} =  \mathbf{R}_{Bi}\mathbf{R}_{iI}(t_o)
\end{equation}
%
where $\mathbf{R}_{Bi}$ comes from user provided data, and
$\mathbf{R}_{iI}(t_o)$ is calculated by GMAT and is dependent upon
$\mathcal{F}_i$.  See Ch.~\ref{Ch:CoordinateSystems} for details on
how GMAT calculates $\mathbf{R}_{iI}$ for all allowable coordinate
systems in GMAT.

Before calculating $\mathbf{R}_{BI}(t)$ we must determine the spin
axis in the body frame, $\{\boldsymbol\omega_{IB}\}_B$.  The user
provides $\{\boldsymbol\omega_{IB}\}_i$.  In spinning mode we assume
the spin axis direction is constant in inertial space and in the
body frame so $\{ \boldsymbol\omega_{IB} \}_B (t)$ $ = \{
\boldsymbol\omega_{IB} \}_B (t_o) = \{ \boldsymbol\omega_{IB} \}_B
$.  We can find the spin axis in the body frame using
$\mathbf{R}_{Bi}$ as follows
%
\begin{equation}
      \{ \boldsymbol\omega_{IB}\}_B = \mathbf{R}_{Bi} \{ \boldsymbol\omega_{IB}\}_i
\end{equation}
%
Once calculated, GMAT saves $\mathbf{R}_{B_{o}I}$ and $\{
\boldsymbol\omega_{IB}\}_B$ for use in calculating the attitude
orientation and rate at other epochs.

GMAT calculates $\mathbf{R}_{BI}(t)$ using the Euler axis/angle
rotation algorithm in Sec. \ref{Sec:AttitudeParameterizations}. The
Euler axis is simply the unitized angular velocity vector or,
%
\begin{equation}
     \mathbf{a} =   \frac{  \{ \boldsymbol\omega_{IB} \}_B  }{\omega_{IB} }
\end{equation}
%
where
%
\begin{equation}
     \omega_{IB} = \| \{\boldsymbol{\omega}_{IB} \}_B \|
\end{equation}
%
The Euler angle $\phi$ is calculated using
%
\begin{equation}
    \phi(t) = \omega_{IB}(t -t_o)
\end{equation}
%
where $t$ is the current epoch, and $t_o$ is the spacecraft's
initial epoch.  Let's define the rotation matrix that results from
the Euler axis/angle rotation using $\mathbf{a}$ and $\phi(t)$, as
$\mathbf{R}_{BB_{o}}(t)$.  We can calculate $\mathbf{R}_{BI}(t)$
using
%
\begin{equation}
     \mathbf{R}_{BI}(t) =
     \mathbf{R}_{BB_{o}}(t)\mathbf{R}_{B_{o}I}
\end{equation}
%

To summarize, in spinning mode the user provides $\mathbf{R}_{Bi}$
and  $\{ \boldsymbol\omega_{IB}\}_i$.  GMAT assumes that that the
spin axis direction is constant, and uses the Euler axis/angle
method to propagate the attitude to find $\mathbf{R}_{BI}$.

Now let's look at how GMAT performs conversions between the
different attitude parameterizations.

\subsection{6 DOF Modelling}

\begin{equation}
   \dot{\mathbf{X}} = \left[ \hspace{.05 in} \dot{\mathbf{r}}^T \hspace{.1 in} \dot{\mathbf{v}}^T \hspace{.1 in}  \dot{\mathbf{h}}^T \hspace{.1 in}  \dot{\mathbf{q}}^T  \hspace{.05 in} \right]^T
\end{equation}
%
\begin{equation}
   \dot{\mathbf{h}} = \mathbf{T} - \mathbf{\boldsymbol\omega}\times\mathbf{h}
\end{equation}
%
\begin{equation}
   \dot{\mathbf{q}} = \frac{1}{2}\mathbf{\Omega}\mathbf{q}
\end{equation}
%
where
%
\begin{equation}
    \mathbf{\Omega} = \left(\begin{array}{ccccc}
     0 & \omega_z  & -\omega_y & \omega_x\\
     -\omega_z & 0 & \omega_x & \omega_y\\
     \omega_y & -\omega_x & 0 & \omega_z\\
     -\omega_x & -\omega_y & -\omega_z & 0\\
  \end{array}\right)
\end{equation}
%
\begin{equation}
    \mathbf{\boldsymbol\omega} = \mathbf{I}^{-1}\mathbf{h}
\end{equation}

\subsection{Attitude Parameterizations \\ and Conversions}
\label{Sec:AttitudeParameterizations}

This section details how GMAT converts between different attitude
parameterizations.  For each conversion type, any singularities that
may occur are addressed.  The orientation parameterizations in GMAT
include the DCM, Euler Angles, quaternions, and Euler axis/angle.
The body rate parameterizations include Euler angle rates and
angular velocity.  We begin with the algorithm to transform from the
quaternions to the DCM.

\subsubsection{Conversion:  Quaternions to DCM}\label{sec:AttQuattoR}
\index{Attitude Parameterization!Quaternions to DCM}

Given:  $\mathbf{q}$, $q_4$

\noindent Find:  $\mathbf{R}$

\noindent Name:  \emph{ToCosineMatrix}

\begin{equation}
    \mathbf{q} = \left( q_1 \hspace{.1 in} q_2 \hspace{.1 in} q_3 \right)^T
\end{equation}
%
\begin{equation}
     \mathbf{q}^{\times} = \begin{pmatrix}
     0 & -q3 & q2\\
     q3 & 0 & -q1\\
     -q2 & q1 & 0\\
     \end{pmatrix}
\end{equation}
%
\begin{equation}
    c = \frac{1}{q_1^2 + q_2^2 + q_3^2 + q_4^2}
\end{equation}
%
\begin{equation}
     \mathbf{R} = c\left[ (q_4^2 - \mathbf{q}^T\mathbf{q})\mathbf{I}_3 +
      2\mathbf{q}\mathbf{q}^T -2q_4\mathbf{q}^{\times}\right]
\end{equation}

\subsubsection{Conversion:  DCM to Quaternions} \label{sec:DCMtoQuat}
\index{Attitude Parameterization!DCM to Quaternions}

Given:  $\mathbf{R}$

\noindent Find: $\mathbf{q}$, $q_4$

Define following vector
%
\begin{equation}
   \mathbf{v} = [ \hspace{.02 in} R_{11} \hspace{.1 in} R_{22}\hspace{.1 in}
   R_{33} \hspace{.1 in}  \mbox{trace}(\mathbf{R}) \hspace{.02 in}]
\end{equation}
%
Define $i_m$ as the index of the maximum component of $\mathbf{v}$
%

\noindent if $i_m = 1$
%
\begin{equation}
     \mathbf{q}''  = \begin{pmatrix}
     2v_{i_m} + 1 - \mbox{trace}(\mathbf{R})\\
     R_{12} + R_{21}\\
     R_{13} + R_{31}\\
     R_{23} - R_{32}\\
     \end{pmatrix}
\end{equation}
%
if $i_m = 2$
%
\begin{equation}
     \mathbf{q}''  = \begin{pmatrix}
     R_{21} + R_{12}\\
     2v_{i_m} + 1 - \mbox{trace}(\mathbf{R})\\
     R_{23} + R_{32}\\
     R_{31} - R_{13}\\
     \end{pmatrix}
\end{equation}
%
if $i_m = 3$
%
\begin{equation}
     \mathbf{q}''  = \begin{pmatrix}
     R_{31} + R_{13}\\
     R_{32} + R_{23}\\
     2v_{i_m} + 1 - \mbox{trace}(\mathbf{R})\\
     R_{12} - R_{21}\\
     \end{pmatrix}
\end{equation}
%
if $i_m = 4$
%
\begin{equation}
     \mathbf{q}''  = \begin{pmatrix}
     R_{23} - R_{32}\\
     R_{31} - R_{13}\\
     R_{12} - R_{21}\\
     1 + \mbox{trace}(\mathbf{R})\\
     \end{pmatrix}
\end{equation}
%
We normalize $\mathbf{q}''$ using
%
\begin{equation}
    \mathbf{q}' = \frac{\mathbf{q}''}{\| \mathbf{q}'' \|}
\end{equation}
%
Finally,
%
\begin{equation}
   \mathbf{q} = [\hspace{.05 in} q_{1}' \hspace{.2 in} q_{2}' \hspace{.2 in} q_3'
   \hspace{.05 in}]^T
\end{equation}
%
and
%
\begin{equation}
     q_4 = q_4'
\end{equation}
%Note: There is not a unique quaternion for a given DCM.  GMAT
%assumes that the ``+" sign is used in Eq.~(\ref{Eq:q_4}).

\subsubsection{Conversion:  DCM to Euler\\ Axis/Angle}

\index{Attitude Parameterization!DCM to Axis/Angle}

Given:  $\mathbf{R}$

\noindent Find: $\mathbf{a}$, $\phi$

\begin{equation}
     \mathbf{R}  = \begin{pmatrix}
     R_{11} & R_{12} & R_{13}\\
     R_{21} & R_{22} & R_{23}\\
     R_{31} & R_{32} & R_{33}\\
     \end{pmatrix}
\end{equation}
%
\begin{equation}
   \phi = \cos^{-1}\left( \frac{1}{2}\left(\mbox{trace}(\mathbf{R}) -
   1 \right)\right)
\end{equation}
%
\begin{equation}
    \mathbf{a} = \frac{1}{2\sin{\phi}}\begin{pmatrix}
     R_{23} - R_{32}\\
     R_{31} - R_{13}\\
     R_{12} - R_{21}\\
     \end{pmatrix}
\end{equation}
%
If $\|\sin{\phi} \| < 10^{-14}$, then we assume
%
\begin{equation}
    \mathbf{a} = \left[\hspace{.05 in} 1 \hspace{.1 in} 0 \hspace{.1 in}
    0 \hspace{.05 in} \right]^T
\end{equation}
%
Note that if $\|\sin{\phi} \| < 10^{-14}$ then $\cos{\phi} \approx
1 $ and we arrive at a DCM of $\mathbf{I}_3$.

\subsubsection{Conversion:  Euler Axis/Angle to DCM} \index{Attitude
Parameterization!Axis/Angle to DCM} \label{Sec:EulerAxis/AngletoDCM}

Given:  $\mathbf{a}$, $\phi$

\noindent Find: $\mathbf{R}$

\begin{equation}
     \mathbf{a}^{\times} = \begin{pmatrix}
     0 & -a_3 & a_2\\
     a_3 & 0 & -a_1\\
     -a_2 & a_1 & 0\\
     \end{pmatrix}
\end{equation}
%
\begin{equation}
    \mathbf{R} = \cos{\phi}\mathbf{I}_3 + (1 -
    \cos{\phi})\mathbf{a}\mathbf{a}^T -
    \sin{\phi}\mathbf{a}^{\times}
\end{equation}

\subsubsection{Conversion:  Euler Angles to DCM}
\label{sec:AttEulerAnglestoDCM}

Given:  Sequence order  ( i.e. 123, 121, .... 313),  $\theta_1$,
$\theta_2$, $\theta_3$

\noindent Find: $\mathbf{R}$

We'll give an example for a 321 rotation, and then present results
for the remaining 11 Euler angle sequences.  First, let's define
$\mathbf{R}_3(\theta_1)$, $\mathbf{R}_2(\theta_2)$, and
$\mathbf{R}_1(\theta_3)$.
%
\begin{equation}
    \mathbf{R}_3(\theta_1) =  \begin{pmatrix}
      \cos{\theta_1}  & \sin{\theta_1} & 0    \\
      -\sin{\theta_1} & \cos{\theta_1} & 0    \\
      0               & 0              & 1
     \end{pmatrix}
\end{equation}
%
\begin{equation}
    \mathbf{R}_2(\theta_2) =  \begin{pmatrix}
      \cos{\theta_2}  & 0              & -\sin{\theta_2}    \\
      0               & 1              & 0    \\
      \sin{\theta_2}  & 0              & \cos{\theta_2}
     \end{pmatrix}
\end{equation}
%
\begin{equation}
    \mathbf{R}_1(\theta_3) =  \begin{pmatrix}
      1               & 0                & 0    \\
      0               & \cos{\theta_3}   & \sin{\theta_3}    \\
      0               & -\sin{\theta_3}  & \cos{\theta_3}
     \end{pmatrix}
\end{equation}
%
Now we can write
%
\begin{eqnarray}
       &\mathbf{R}_{321} =  \mathbf{R}_1(\theta_3)\mathbf{R}_2(\theta_2)\mathbf{R}_3(\theta_1) = \nonumber\\
      &=         \begin{pmatrix}
      1               & 0                & 0    \\
      0               & c_3   & s_3    \\
      0               & -s_3  & c_3
     \end{pmatrix}
     %
      \begin{pmatrix}
      c_2  & 0              & -s_2    \\
      0               & 1              & 0    \\
      s_2  & 0              & c_2
     \end{pmatrix}
     %
     \begin{pmatrix}
      c_1   & s_1 & 0    \\
      -s_1  & c_1 & 0    \\
      0               & 0              & 1
     \end{pmatrix}\nonumber\\
\end{eqnarray}
%
where $c_1 =\cos{\theta_1}$, $s_1 = \sin{\theta_1}$ etc.  We can
rewrite $\mathbf{R}_{321} $ as
%
\begin{equation}
       \mathbf{R}_{321}  =\begin{pmatrix}
      c_2c_1              &  c_2s_1             & -s_2 \\
     -c_3s_1 + s_3s_2c_1  &  c_3c_1 + s_3s_2s_1 & s_3c_2 \\
     s_3s_1 + c_3s_2c_1   &  -s_3c_1 +c_3s_2s_1 & c_3c_2
     \end{pmatrix} \label{Eq:R321}
\end{equation}
%

The approach is similar for the remaining 11 Euler angle sequences.
Rather than derive the DCM matrices for the remaining 11 sequences,
we present them in Table \ref{table:EulerAnglestoDCM}.

\subsubsection{Conversion:  DCM to Euler Angles}
\label{sec:AttDCMtoEulerAngles}

Given:  Sequence order  ( i.e. 123, 121, .... 313), $\mathbf{R}$

\noindent Find:  $\theta_1$, $\theta_2$, $\theta_3$

We'll give an example for a 321 rotation, and then present results
for the remaining 11 Euler angle sequences.  Examining,
Eq.~(\ref{Eq:R321}), we see that
%
\begin{equation}
     \frac{ R_{21} }  { R_{11}  } = \frac{  \cos{\theta_2}\sin{\theta_1}     }
                                 {  \cos{\theta_2}\cos{\theta_1}    }
\end{equation}
%
From this we can see that
%
\begin{equation}
    \theta_1 =  \tan^{-1}{\frac{ R_{21} }  { R_{11}  }}
\end{equation}
%
Further inspection of Eq.~(\ref{Eq:R321}) shows us that
%
\begin{equation}
    \theta_2 = \sin^{-1}{R_{13}}
\end{equation}
%
At first glance, we may choose to calculate $\theta_3$ using
$\theta_3 = \tan^{-1}{(R_{23}/R_{33})}$.  However, in the case that
$\theta_2 = 90^\circ$, this would result in the indeterminate case,
$\theta_3 =$ $\tan^{-1}(R_{23}/R_{33})$ $= \tan^{-1}(0/0)$.  An
improved method, found in the ADEAS mathematical specifications
document, is to determine $\theta_3$ using
%
\begin{equation}
    \theta_3 = \tan^{-1} \left(\frac{ R_{31} \sin{\theta_1} - R_{32} \cos{\theta_1} }
    { -R_{21} \sin{\theta_1} + R_{22} \cos{\theta_1}} \right)
    \label{Eq:Rtotheta3}
\end{equation}
%
Substituting values from Eq.~(\ref{Eq:R321}) into
Eq.~(\ref{Eq:Rtotheta3}), and using abbreviated notation, we see
that
%
\begin{equation}
     \theta_3 = \tan^{-1} \left(  \frac{ s_1( s_3s_1 + c_3s_2c_1) - c_1(-s_3c_1 + c_3s_2s_1 )}
    { s_1(c_3s_1 - s_3s_2c_1  ) + c_1( c_3c_1 + s_3s_2s_1 ) }  \right)
\end{equation}
%
Now, if $\theta_2 = 90^\circ$, and we substitute $c_2 = 0$ and $s_2
= 1$ into the above equation, we see we get a determinate form.
Results for all twelve Euler Sequences are shown in Table
\ref{table:DCMtoEulerAngles}.

\noindent Note:  All $\tan^{-1}$ use a quadrant check ( equaivalent
to atan2 ) to make sure the the correct quadrant is chosen.

\subsubsection{Conversion:  Angular Velocity to \\ Euler Angles
Rates}

Given:  Sequence ( i.e. 123, 121, .... 313), $\theta_2$,
$\theta_3$ $\boldsymbol\omega$

\noindent Find: $\dot\theta_1$, $\dot\theta_2$, $\dot\theta_3$
%
\begin{equation}
    \begin{pmatrix}
         \dot\theta_1\\
         \dot\theta_2\\
         \dot\theta_3
    \end{pmatrix}
    %
    = \mathbf{S}^{-1}(\theta_2,\theta_3)\boldsymbol\omega
\end{equation}
%
$\mathbf{S}^{-1}(\theta_2,\theta_3)$ is dependent upon the Euler
sequence.  Table \ref{table:EulerAngleKinematics} contains the
different expressions for $\mathbf{S}^{-1}(\theta_2,\theta_3)$ for
each of the 12 unique Euler sequences.

Note:  Each of the forms of $\mathbf{S}^{-1}$ have a possible
singularity due to the appearance of either $\sin{\theta_2}$ or
$\cos{\theta_2}$ in the denominator.  If GMAT encounters a
singularity, an error message is thrown, and the zero vector is
returned.

\subsubsection{Conversion:  Euler Angles Rates to Angular Velocity}

\noindent Given: Sequence ( i.e. 123, 121, .... 313), $\theta_2$,
$\theta_3$, $\dot\theta_1$, $\dot\theta_2$, $\dot\theta_3$

\noindent Find: $\boldsymbol\omega$
%
\begin{equation}
    \boldsymbol\omega = \mathbf{S}(\theta_2,\theta_3)
    %
        \begin{pmatrix}
         \dot\theta_1\\
         \dot\theta_2\\
         \dot\theta_3
    \end{pmatrix}
\end{equation}
%
$\mathbf{S}(\theta_2,\theta_3)$ is dependent upon the Euler
sequence.  Table \ref{table:EulerAngleKinematics} contains the
different expressions for $\mathbf{S}^{-1}(\theta_2,\theta_3)$ for
each of the 12 unique Euler sequences.

\subsubsection{Conversion:  Quaternions to Euler Angles}

\noindent Given: $\mathbf{q}$, $q_4$, Euler Sequence

\noindent Find: $\theta_1$, $\theta_2$, and $\theta_3$

There is not a direct transformation to convert from the quaternions
to the Euler Angles.  GMAT first converts from the quaternion to the
DCM using the algorithm in Sec. \ref{sec:AttQuattoR}.  The DCM is
then used to calculate the Euler Angles for the given Euler angle
sequence using the algorithm in Sec. \ref{sec:AttDCMtoEulerAngles}.
%\section{Attitude Initial Conditions}

\subsubsection{Conversion:  Euler Angles to Quaternions}

\noindent Given: $\theta_1$, $\theta_2$, and $\theta_3$, Euler
Sequence

\noindent Find: $\mathbf{q}$, $q_4$

There is not a direct transformation to convert from Euler Angles to
quaternions.  GMAT first converts from the Euler Angles to the DCM
using the algorithm in Sec. \ref{sec:AttEulerAnglestoDCM}.  The DCM
is then used to calculate the quaternions using the algorithm in
Sec. \ref{sec:DCMtoQuat}.
%\section{Attitude Initial Conditions}

\subsubsection{Euler Angles to $\mathbf{R}$ Matrix}

\onecolumn
 \begin{table}[h]
        \centering
        \vspace{0 pt}
        \caption{Rotation Matrices for 12 Unique Euler Angle Rotation Sequences}
        \begin{tabular}{clccccccc}  \hline \hline
        \\
     %------------------------------------
     %------------------------------------
     $\mathbf{R}_3(\theta_3)\mathbf{R}_2(\theta_2)\mathbf{R}_1(\theta_1)$
     &
     $=\begin{pmatrix}
     c_3c_2    & c_3 s_2 s_1 + s_3 c_1  & -c_3 s_2 c_1 + s_1 s_3\\
     -s_3 c_2  & -s_3 s_2 s_1 + c_3 c_1 & s_3 s_2 c_1 + c_3 s_1\\
     s_2       & -c_2 s_1               & c_2 c_1\\
     \end{pmatrix}  \vspace{.1 in}$\\
     %
     %------------------------------------
     %------------------------------------
     $\mathbf{R}_2(\theta_3)\mathbf{R}_3(\theta_2)\mathbf{R}_1(\theta_1)$
     &
     $=\begin{pmatrix}
     c_3c_2   &   c_3 s_2 c_1 + s_1 s_3  &  c_3s_2s_1-s_3c_1 \\
      -s_2    &   c_2c_1                 &  c_2s_1\\
      s_3c_2  &   s_3s_2c_1 - c_3s_1     &  s_3s_2s_1 + c_3c_1 \\
     \end{pmatrix}   \vspace{.1 in}$\\
     %
     $\mathbf{R}_1(\theta_3)\mathbf{R}_3(\theta_2)\mathbf{R}_2(\theta_1)$
     &
     $=\begin{pmatrix}
     c_2c_1               &    s_2     &   -c_2s_1\\
     -c_3s_2c_1 + s_3 s_1 & c_3c_2     & c_3s_2s_1 + s_3c_1     \\
     s_3s_2c_1 +c_3s_1    &  -s_3c_2   &  -s_3s_2s_1 + c_3 c_1  \\
    \end{pmatrix}   \vspace{.1 in}$\\
     %
     %------------------------------------
     %------------------------------------
     $\mathbf{R}_3(\theta_3)\mathbf{R}_1(\theta_2)\mathbf{R}_2(\theta_1)$
     &
     $=\begin{pmatrix}
      c_3c_1 + s_3s_2s_1     &  s_3c_2    & -c_3s_1 + s_3s_2c_1    \\
      -s_3c_1 + c_3 s_2s_1   &  c_3c_2    & s_3s_1 + c_3s_2c_1    \\
      c_2s_1                 &  -s_2      & c_2c_1
     \end{pmatrix}  \vspace{.1 in}$\\
     %
     $\mathbf{R}_2(\theta_3)\mathbf{R}_1(\theta_2)\mathbf{R}_3(\theta_1)$
     &
     $=\begin{pmatrix}
      c_3c_1 - s_3s_2s_1     & c_3s_1 + s_3s_2c_1 & -s_3c_2 \\
      -c_2s_1                & c_2c_1             &  s_2  \\
      s_3c_1 + c_3s_2s_1     & s_3s_1 - c_3s_2c_1 & c_3c_2
     \end{pmatrix}  \vspace{.1 in}$\\
     %
     %------------------------------------
     %------------------------------------
     $\mathbf{R}_1(\theta_3)\mathbf{R}_2(\theta_2)\mathbf{R}_3(\theta_1)$
     &
     $=\begin{pmatrix}
      c_2c_1              &  c_2s_1             & -s_2 \\
     -c_3s_1 + s_3s_2c_1  &  c_3c_1 + s_3s_2s_1 & s_3c_2 \\
     s_3s_1 + c_3s_2c_1   &  -s_3c_1 +c_3s_2s_1 & c_3c_2
     \end{pmatrix}  \vspace{.1 in}$\\
     %
     $\mathbf{R}_1(\theta_3)\mathbf{R}_2(\theta_2)\mathbf{R}_1(\theta_1)$
     &
     $=\begin{pmatrix}
     c_2                  & s_2s_1              & -s_2c_1 \\
     s_3s_2               & c_3c_1 - s_3c_2s_1  & c_3s_1 + s_3c_2c_1\\
     c_3s_2               & -s_3c_1 - c_3c_2s_1 & -s_3s_1 +c_3c_2c_1
     \end{pmatrix}  \vspace{.1 in}$\\
          %
     %------------------------------------
     %------------------------------------
     $\mathbf{R}_1(\theta_3)\mathbf{R}_3(\theta_2)\mathbf{R}_1(\theta_1)$
     &
     $=\begin{pmatrix}
      c_2                  & s_2c_1              & s_2s_1 \\
     -c_3s_2              & c_3c_2c_1 - s_3s_1  & c_3c_2s_1 + s_3c_1\\
     s_3s_2               & -s_3c_2c_1 - c_3s_1 & -s_3c_2s_1 +   c_3c_1
     \end{pmatrix}  \vspace{.1 in}$\\
               %
     $\mathbf{R}_2(\theta_3)\mathbf{R}_1(\theta_2)\mathbf{R}_2(\theta_1)$
     &
     $=\begin{pmatrix}
     c_3c_1 - s_3c_2s_1   &  s_3s_2    &  -c_3s_1 -s_3c_2c_1\\
     s_2s_1               &  c_2       &  s_2c_1            \\
     s_3c_1 + c_3c_2s_1   &  -c_3s_2   &  -s_3s_1 + c_3c_2c_1
     \end{pmatrix}  \vspace{.1 in}$\\
                    %
     %------------------------------------
     %------------------------------------
     $\mathbf{R}_2(\theta_3)\mathbf{R}_3(\theta_2)\mathbf{R}_2(\theta_1)$
     &
     $=\begin{pmatrix}
     c_3c_2c_1 - s_3s_1   &  c_3s_2  & -c_3c_2s_1 - s_3c_1\\
     -s_2c_1              &  c_2     & s_2s_1             \\
     s_3c_2c_1 + c_3s_1   &  s_3s_2  & -s_3c_2s_1 + c_3c_1
     \end{pmatrix}  \vspace{.1 in}$\\
                         %
     $\mathbf{R}_3(\theta_3)\mathbf{R}_1(\theta_2)\mathbf{R}_3(\theta_1)$
     &
     $=\begin{pmatrix}
      c_3c_1 - s_3c_2s_1 & c_3s_1 + s_3c_2c_1  &  s_3s_2\\
     -s_3c_1 - c_3c_2s_1 & -s_3s_1 + c_3c_2c_1 &  c_3s_2\\
      s_2s_1             &  -s_2c_1            &  c_2
     \end{pmatrix}  \vspace{.1 in}$\\
                              %
     %------------------------------------
     %------------------------------------
     $\mathbf{R}_3(\theta_3)\mathbf{R}_2(\theta_2)\mathbf{R}_3(\theta_1)$
     &
     $=\begin{pmatrix}
      c_3c_2c_1-s_3s_1     & c_3c_2s_1 + s_3c_1  &  -c_3s_2\\
      -s_3c_2c_1 - c_3s_1  & -s_3c_2s_1 + c_3c_1 &  s_3s_2\\
      s_2c_1               & s_2s_1              &  c_2
     \end{pmatrix}  \vspace{.1 in}$\\
         \hline \hline
        \end{tabular}
        \label{table:EulerAnglestoDCM}
\end{table}
\twocolumn

\subsubsection{Euler Rates and Angular Velocity} \onecolumn
 \begin{table}[h]
        \centering
        \vspace{0 pt}
        \caption{Kinematics of Euler Angle Rotation Sequences}
        \begin{tabular}{cllcccccc}  \hline \hline
        Euler Sequence & $\mathbf{S}(\theta_2,\theta_3)$ &
        $\mathbf{S}^{-1}(\theta_2,\theta_3)$\\
        \hline
        \\
        %------------------
        %------------------
     $\mathbf{R}_3(\theta_3)\mathbf{R}_2(\theta_2)\mathbf{R}_1(\theta_1)$
     &
     $\begin{pmatrix}
        c3c2&          s3&                0\\
       -s3c2&          c3&                0\\
          s2&                0&                1\\
     \end{pmatrix}  \vspace{.1 in}$
     &
     $\begin{pmatrix}
                  c3/c2&         -s3/c2&                        0\\
                  s3&                  c3&                        0\\
                  -s2c3/c2&  s3s2/c2& 1\\
     \end{pmatrix}  \vspace{.1 in}$\\
     %
        %------------------
        %------------------
     $\mathbf{R}_2(\theta_3)\mathbf{R}_3(\theta_2)\mathbf{R}_1(\theta_1)$
     &
     $\begin{pmatrix}
         c3c2&        -s3&               0\\
        -s2&               0&               1\\
         s3c2&         c3&               0\\
     \end{pmatrix}  \vspace{.1 in}$
     &
     $\begin{pmatrix}
         c3/c2&                       0&         s3/c2\\
           -s3&                       0&                 c3\\
          s2c3/c2&                       1& s3s2/c2\\
     \end{pmatrix}  \vspace{.1 in}$\\
     %
     %------------------
     %------------------
     $\mathbf{R}_1(\theta_3)\mathbf{R}_3(\theta_2)\mathbf{R}_2(\theta_1)$
     &
     $\begin{pmatrix}
        s2&                0&                1\\
       c3c2&          s3&                0\\
      -s3c2&          c3&                0\\
     \end{pmatrix}  \vspace{.1 in}$
     &
     $\begin{pmatrix}
                        0&          c3/c2&         -s3/c2\\
                        0&                  s3&                  c3\\
                        1& -s2c3/c2&  s3s2/c2\\
     \end{pmatrix}  \vspace{.1 in}$\\
     %
     %------------------
     %------------------
     $\mathbf{R}_3(\theta_3)\mathbf{R}_1(\theta_2)\mathbf{R}_2(\theta_1)$
     &
     $\begin{pmatrix}
         s3c2&         c3&               0\\
         c3c2&        -s3&               0\\
        -s2&               0&               1\\
     \end{pmatrix}  \vspace{.1 in}$
     &
     $\begin{pmatrix}
         s3/c2&         c3/c2&                       0\\
                 c3&                -s3&                       0\\
 s3s2/c2& s2c3/c2&                       1\\
     \end{pmatrix}  \vspace{.1 in}$\\
     %
     %------------------
     %------------------
     $\mathbf{R}_2(\theta_3)\mathbf{R}_1(\theta_2)\mathbf{R}_3(\theta_1)$
     &
     $\begin{pmatrix}
       -s3c2&          c3&                0\\
          s2&                0&                1\\
        c3c2&          s3&                0\\
     \end{pmatrix}  \vspace{.1 in}$
     &
     $\begin{pmatrix}
         -s3/c2&                        0&          c3/c2\\
                  c3&                        0&                  s3\\
         s3s2/c2&                        1& -s2c3/c2\\
     \end{pmatrix}  \vspace{.1 in}$\\
     %------------------
     %------------------
     $\mathbf{R}_1(\theta_3)\mathbf{R}_2(\theta_2)\mathbf{R}_3(\theta_1)$
     &
     $\begin{pmatrix}
                -s2&               0&               1\\
       s3c2&         c3&               0\\
       c3c2&        -s3&               0\\
     \end{pmatrix}  \vspace{.1 in}$
     &
     $\begin{pmatrix}
                       0&         s3/c2&         c3/c2\\
                       0&                 c3&                -s3\\
                       1& s3s2/c2& s2c3/c2\\
     \end{pmatrix}  \vspace{.1 in}$\\
          %
     %------------------
     %------------------
     $\mathbf{R}_1(\theta_3)\mathbf{R}_2(\theta_2)\mathbf{R}_1(\theta_1)$
     &
     $\begin{pmatrix}
                c2&               0&               1\\
       s3s2&         c3&               0\\
      c3s2&        -s3&               0\\
     \end{pmatrix}  \vspace{.1 in}$
     &
     $\begin{pmatrix}
                        0&          s3/s2&          c3/s2\\
                        0&                  c3&                 -s3\\
                        1& -s3c2/s2& -c3c2/s2\\
     \end{pmatrix}  \vspace{.1 in}$\\
                    %
     %------------------
     %------------------
     $\mathbf{R}_1(\theta_3)\mathbf{R}_3(\theta_2)\mathbf{R}_1(\theta_1)$
     &
     $\begin{pmatrix}
          c2&                0&                1\\
       -c3s2&          s3&                0\\
        s3s2&          c3&                0\\
     \end{pmatrix}  \vspace{.1 in}$
     &
     $\begin{pmatrix}
                        0&         -c3/s2&          s3/s2\\
                        0&                  s3&                  c3\\
                        1&  c3c2/s2& -s3c2/s2\\
     \end{pmatrix}  \vspace{.1 in}$\\
                    %
     %------------------
     %------------------
     $\mathbf{R}_2(\theta_3)\mathbf{R}_1(\theta_2)\mathbf{R}_2(\theta_1)$
     &
     $\begin{pmatrix}
        s3s2&          c3&                0\\
          c2&                0&                1\\
       -c3s2&          s3&                0\\
     \end{pmatrix}  \vspace{.1 in}$
     &
     $\begin{pmatrix}
          s3/s2&                        0&         -c3/s2\\
                  c3&                        0&                  s3\\
         -s3c2/s2&                        1&  c3c2/s2\\
     \end{pmatrix}  \vspace{.1 in}$\\
                              %
     %------------------
     %------------------
     $\mathbf{R}_2(\theta_3)\mathbf{R}_3(\theta_2)\mathbf{R}_2(\theta_1)$
     &
     $\begin{pmatrix}
       c3s2&        -s3&               0\\
         c2&               0&               1\\
       s3s2&         c3&               0\\
     \end{pmatrix}  \vspace{.1 in}$
     &
     $\begin{pmatrix}
          c3/s2&                        0&          s3/s2\\
                 -s3&                        0&                  c3\\
        -c3c2/s2&                        1& -s3c2/s2\\
     \end{pmatrix}  \vspace{.1 in}$\\
                              %
     %------------------
     %------------------
     $\mathbf{R}_3(\theta_3)\mathbf{R}_1(\theta_2)\mathbf{R}_3(\theta_1)$
     &
     $\begin{pmatrix}
       s3s2&         c3&               0\\
       c3s2&        -s3&               0\\
         c2&               0&               1\\
     \end{pmatrix}  \vspace{.1 in}$
     &
     $\begin{pmatrix}
          s3/s2&          c3/s2&                        0\\
                  c3&                 -s3&                        0\\
      -s3c2/s2& -c3c2/s2&                        1\\
     \end{pmatrix}  \vspace{.1 in}$\\
     %
          %------------------
     %------------------
     $\mathbf{R}_3(\theta_3)\mathbf{R}_2(\theta_2)\mathbf{R}_3(\theta_1)$
     &
     $\begin{pmatrix}
        -c3s2&          s3&                0\\
         s3s2&          c3&                0\\
           c2&                0&                1\\
     \end{pmatrix}  \vspace{.1 in}$
     &
     $\begin{pmatrix}
         -c3/s2&          s3/s2&                        0\\
                  s3&                  c3&                        0\\
        c3c2/s2& -s3c2/s2&                        1\\
     \end{pmatrix}  \vspace{.1 in}$\\
         \hline \hline
        \end{tabular}
        \label{table:EulerAngleKinematics}
\end{table}

\begin{table}[h]
        \centering
        \vspace{0 pt}
        \caption{ Computation of Euler Angles from DCM}
        \begin{tabular}{llllllll}  \hline \hline \\
        Euler Sequence & Euler Angle Computations \\
        \hline \\
     $\mathbf{R}_3(\theta_3)\mathbf{R}_2(\theta_2)\mathbf{R}_1(\theta_1)$
     \hspace{.1 in}
     &
      $\theta_1 =  \tan^{-1}(-R_{32}/R_{33})$ &
      $\theta_2 =  \sin^{-1}(R_{31})$ &
      $\theta_3  = \tan^{-1}\left(\displaystyle\frac{R_{13}\sin{\theta_1}+
      R_{12}\cos{\theta_1}}{R_{23}\sin{\theta_1}+R_{22}\cos{\theta_1}}\right )$\vspace{.15 in}
     \\
     %
     $\mathbf{R}_2(\theta_3)\mathbf{R}_3(\theta_2)\mathbf{R}_1(\theta_1)$ \hspace{.1 in}
     &
     $\theta_1 =  \tan^{-1}(R_{23}/R_{22})$ &
         $\theta_2 =  \sin^{-1}(-R_{21})$ &
         $\theta_3  = \tan^{-1}\left(\displaystyle\frac{R_{12}\sin{\theta_1}- R_{13}\cos{\theta_1}}{-R_{32}\sin{\theta_1}+
         R_{33}\cos{\theta_1}}\right)$ \vspace{.15 in}
     \\
     $\mathbf{R}_1(\theta_3)\mathbf{R}_3(\theta_2)\mathbf{R}_2(\theta_1)$
     &
    $\theta_1 =  \tan^{-1}(-R_{13}/R_{11})$ &
         $\theta_2 =  \sin^{-1}(R_{12})$ &
         $\theta_3  = \tan^{-1}\left(\displaystyle\frac{R_{21}\sin{\theta_1}+ R_{23}\cos{\theta_1}}{R_{31}\sin{\theta_1}+
         R_{33}\cos{\theta_1}}\right)$ \vspace{.15 in}
     \\
     %
     $\mathbf{R}_3(\theta_3)\mathbf{R}_1(\theta_2)\mathbf{R}_2(\theta_1)$
     &
         $\theta_1 =  \tan^{-1}(R_{31}/R_{33})$ &
         $\theta_2 =  \sin^{-1}(-R_{32})$ &
         $\theta_3  = \tan^{-1}\left(\displaystyle\frac{R_{23}\sin{\theta_1}- R_{21}\cos{\theta_1}}{-R_{13}\sin{\theta_1}+
         R_{11}\cos{\theta_1}}\right)$ \vspace{.15 in}
     \\
     $\mathbf{R}_2(\theta_3)\mathbf{R}_1(\theta_2)\mathbf{R}_3(\theta_1)$
     &
     $\theta_1 =  \tan^{-1}(-R_{21}/R_{22})$ &
     $\theta_2 =  \sin^{-1}(R_{23})$ &
     $\theta_3  = \tan^{-1}\left(\displaystyle\frac{R_{32}\sin{\theta_1}+ R_{31}\cos{\theta_1}}{R_{12}\sin{\theta_1}+
     R_{11}\cos{\theta_1}}\right)$ \vspace{.15 in}
     \\
     %
     $\mathbf{R}_1(\theta_3)\mathbf{R}_2(\theta_2)\mathbf{R}_3(\theta_1)$
     &
     $\theta_1 =  \tan^{-1}(R_{12}/R_{11})$ &
     $\theta_2 =  \sin^{-1}(-R_{13})$ &
     $\theta_3  = \tan^{-1}\left(\displaystyle\frac{R_{31}\sin{\theta_1}- R_{32}\cos{\theta_1}}{-R_{21}\sin{\theta_1}+
     R_{22}\cos{\theta_1}}\right)$ \vspace{.15 in}\\
     %
     $\mathbf{R}_1(\theta_3)\mathbf{R}_2(\theta_2)\mathbf{R}_1(\theta_1)$
     &
     $\theta_1 =  \tan^{-1}(R_{12}/(-R_{13}))$ &
     $\theta_2 =  \cos^{-1}(R_{11})$ &
     $\theta_3  = \tan^{-1}\left(\displaystyle\frac{-R_{33}\sin{\theta_1}- R_{32}\cos{\theta_1}}{R_{23}\sin{\theta_1}+
     R_{22}\cos{\theta_1}}\right)$ \vspace{.15 in}\\
     %
     $\mathbf{R}_1(\theta_3)\mathbf{R}_3(\theta_2)\mathbf{R}_1(\theta_1)$
     &
     $\theta_1 =  \tan^{-1}(R_{13}/(R_{12}))$ &
     $\theta_2 =  \cos^{-1}(R_{11})$ &
     $\theta_3  = \tan^{-1}\left(\displaystyle\frac{-R_{22}\sin{\theta_1} + R_{23}\cos{\theta_1}}{-R_{32}\sin{\theta_1}+
     R_{33}\cos{\theta_1}}\right)$ \vspace{.15 in}\\
     %
     $\mathbf{R}_2(\theta_3)\mathbf{R}_1(\theta_2)\mathbf{R}_2(\theta_1)$
     &
     $\theta_1 =  \tan^{-1}(R_{21}/(R_{23}))$ &
     $\theta_2 =  \cos^{-1}(R_{22})$ &
     $\theta_3  = \tan^{-1}\left(\displaystyle\frac{-R_{33}\sin{\theta_1} + R_{31}\cos{\theta_1}}{-R_{13}\sin{\theta_1}+
     R_{11}\cos{\theta_1}}\right)$ \vspace{.15 in}\\
     %
     $\mathbf{R}_2(\theta_3)\mathbf{R}_3(\theta_2)\mathbf{R}_2(\theta_1)$
     &
     $\theta_1 =  \tan^{-1}(R_{23}/(-R_{21}))$ &
     $\theta_2 =  \cos^{-1}(R_{22})$ &
     $\theta_3  = \tan^{-1}\left(\displaystyle\frac{-R_{11}\sin{\theta_1} - R_{13}\cos{\theta_1}}{R_{31}\sin{\theta_1}+
     R_{33}\cos{\theta_1}}\right)$ \vspace{.15 in}\\
     %
     $\mathbf{R}_3(\theta_3)\mathbf{R}_1(\theta_2)\mathbf{R}_3(\theta_1)$
     &
     $\theta_1 =  \tan^{-1}(R_{31}/(-R_{32}))$ &
     $\theta_2 =  \cos^{-1}(R_{33})$ &
     $\theta_3  = \tan^{-1}\left(\displaystyle\frac{-R_{22}\sin{\theta_1} - R_{21}\cos{\theta_1}}{R_{12}\sin{\theta_1}+
     R_{11}\cos{\theta_1}}\right)$ \vspace{.15 in}\\
     %
     $\mathbf{R}_3(\theta_3)\mathbf{R}_2(\theta_2)\mathbf{R}_3(\theta_1)$
     &
     $\theta_1 =  \tan^{-1}(R_{32}/(R_{31}))$ &
     $\theta_2 =  \cos^{-1}(R_{33})$ &
     $\theta_3  = \tan^{-1}\left(\displaystyle\frac{-R_{11}\sin{\theta_1} + R_{12}\cos{\theta_1}}{-R_{21}\sin{\theta_1}+
     R_{22}\cos{\theta_1}} \right)$ \vspace{.15 in}\\
         \hline \hline
        \end{tabular}
        \label{table:DCMtoEulerAngles}
\end{table}


\documentclass[preview, border=12pt, varwidth]{standalone}
\usepackage{array,tabularx, mathtools, amsmath}

\newenvironment{conditions*}
  {\par\vspace{\abovedisplayskip}\noindent
   \tabularx{\columnwidth}{>{$}l<{$} @{\ : } >{\raggedright\arraybackslash}X}}
  {\endtabularx\par\vspace{\belowdisplayskip}}

\begin{document}

\section*{(3) Evaporative cooling:}

\begin{multline}
  \tag{4.1} \label{eq:4.1}
    (bd \rho_w C_w \frac{\partial T_w}{\partial t} + \dot{m}_w C_w \frac{\partial T_w}{\partial y}) dy = [\tau_1 S(t) \\ -Q_r -Q_c -Q_e + h_1 (\left. \theta_1 \right\vert_{x=0} - T_w)]bdy
\end{multline}

\begin{align}
  \tag{4.2} \label{eq:4.2}
    Q_r ={}& h_r(T_w-T_A)  \\
  \tag{4.3} \label{eq:4.3}
    Q_c ={}& h_c(T_w-T_A)  \\
  \tag{4.4} \label{eq:4.4}
    Q_e ={}& 0.013 h_e [p(\bar{T}_w - \gamma p(\bar{T}_A)]
\end{align}

where:
\begin{conditions*}
    \enspace \tau_1  &  fraction of solar radiation absorbed by water \\
    \enspace Q_r  &  radiative heat \\
    \enspace Q_c  &  convective heat flux \\
    \enspace \rho  &  saturated partial pressure \\
    \left.
        \begin{array}{l}
            h_r \\ h_e \\ h_c \\
        \end{array}
    \right \}  &  respective heat transfer coefficient
\end{conditions*} \medskip

\begin{align}
  \tag{4.5} \label{eq:4.5}
    P(T) &= R_{1}T + R
\end{align}

on simplification,
\begin{align*}
  \tag{4.6} \label{eq:4.6}
    M_w \frac{\partial T_w}{\partial t} + \dot{m_w} C_w \frac{\partial T_w}{\partial y} &= bH(T_S-T_w) + bh_1 (\left. \theta_1 \right\vert_{x=0} - T_w) \\
  \tag{4.7} \label{eq:4.7}
    T_s &= \frac{1}{H} (\tau_1S(t) + H_1T_A(t) - R_0R_2(1-r)) \\
  \tag{4.8} \label{eq:4.8}
    H &= h_r + h_c + R_0R_1 \\
  \tag{4.9} \label{eq:4.9}
    H_1 &= h_r+h_c+rR_0R_1 \\
  \tag{4.10} \label{eq:4.10}
    R_0 &= 0.013h_c \\
  \tag{4.11} \label{eq:4.11}
    M_w &= bdC_wS_w \\
  \tag{4.12} \label{eq:4.12}
    \dot{Q}(y,t) &= h_i[\theta|_{x=x_4} - T_R]
\end{align*}

\end{document}


%************************************************
\chapter{Methods}\label{ch:methods}
%************************************************

This thesis relies on a previously described model of a neuron. It is a single-compartment, conductance-based neuron which is based on the Hogkin-Huxley formalism\cite{hodgkin_components_1952,hodgkin_measurement_1952,hodgkin_quantitative_1952}. Previous work has utilized this model neuron as a backbone for exploration of homeostatic compensatory mechanisms\cite{oleary_correlations_2013,oleary_cell_2014,liu_model_1998}.
This model represents the neuron as a circuit\cite{hodgkin_quantitative_1952}. Figure \ref{fig:hhcircuit} details the circuit diagram of the neuron as a representation of the cell membrane acting as a capacitor with various ion channels as resistors. The membrane takes in current from the outside, which can be described in more detail as the neuron's neuromodulatory environment.

\begin{figure}[h]
    \centering
    \includegraphics[angle=0.75,width=\linewidth]{gfx/hodgkinhuxleycircuit.png}
    \caption[Circuit diagram representing membrane of a neuron.]{Circuit diagram representing the membrane of a neuron. \ac{Cm} represents the total capacitance of the neuron. Resistors represent \(1/\acs{gNaV}\), \(1/\acs{gK}\), and \(1/\ac{gleak}\)\cite{hodgkin_quantitative_1952}.}
    \label{fig:hhcircuit}
\end{figure}

The model neuron has eight transmembrane currents and associated conductances: \acf{INaV}, \acf{IA}, \acf{IH}, \acf{IKCa}, \acf{IKd}, \acf{ICaS}, \acf{ICaT}, and \acf{Ileak}.
The associated maximal conductances of the first seven transmembrane currents are regulated by the integral control scheme mentioned in Section \ref{homeostaticrules}. \ac{gleak} is not regulated in this model.

\section{Neurodynamics} \label{neurodynamics}

Neurons are considered to consist of a single compartment, acting as a capacitor, with various ionic currents acting as resistors in parallel, and a specific surface area (in units $mm^2$).
The compartment has a \acf{Vm}, which evolves according to the equation:

\begin{equation} \label{eq:voltage}
C_m \frac{\mathrm{d}V_m}{\mathrm{d}t} = \sum_{i} I_i
\end{equation}

where $\acs{Cm}$ is the specific capacitance of the membrane and $I_i$ is each ionic current in the neuron\cite{gorur-shandilya_xolotl_2018}. Current is given by the equation

\begin{equation} \label{eq: current}
    I_i = g_i(V)(V - E_i)
\end{equation}

where $g_i(V)$ is instantaneous conductance and $E_i$ is the reversal potential (Nernst potential) for each ion channel\cite{gorur-shandilya_xolotl_2018}. Reversal potentials for each ion channel, excluding Ca\textsuperscript{2+}-dependent channels, are considered constant and are described in Section \ref{modelparameters}. This simplification is justified by the law of large numbers -- intracellular and extracellular ion concentration are saturated, resulting in effectively constant flux of ions in and out of the cell\cite{liu_model_1998}. Instantaneous conductance $g_i(V)$ is defined by the equation:

\begin{equation} \label{eq: conductance}
    g_i(V) = \bar{g_i} m_i^{p_i} h_i^{q_i}
\end{equation}

where

\begin{tabular}{ll}
    $\bar{g_i}$     & maximal conductance for each ion channel \\
    $m$             & activation gating variable \\
    $h$             & inactivation gating variable \\
    $p, q$          & integers \\
\end{tabular}

The integers $p$ and $q$ are bound by an interval $[0,1]$\cite{gorur-shandilya_xolotl_2018}. The activation and inactivation variables are themselves defined by an ordinary differential equation which depends on \ac{Vm}, and is specific to each conductance. The general form is:

\begin{align}
    \tau_m \frac{\mathrm{d}m}{\mathrm{d}t} = m_\infty - m \\
    \tau_h \frac{\mathrm{d}h}{\mathrm{d}t} = h_\infty - h
\end{align}

where $\tau_m$ and $\tau_h$ are time constants and $m_\infty$ and $h_\infty$ are steady-state values of the activation and inactivation variables\cite{prinz_alternative_2003}. Each conductance has a characteristic steady-state density and time constant which represent its function in the overall neurodynamics of the neuron.

\section{Homeostatic Rules}\label{homeostaticrules}

Regulation of firing rate activity in this thesis will rely on an activity-dependent feedback mechanism for regulating ion channel maximal conductances\cite{oleary_correlations_2013,oleary_cell_2014,liu_model_1998,prinz_alternative_2003}.
This mechanism relies on calcium as a sensor, by which neurons may regulate their activity. Section \ref{corr_homeostasis} provides an explanation of the use of calcium ions as an indirect representation of \ac{Vm} activity.
Calcium dynamics in this model evolve according to the equation:

\begin{equation} \label{eq:calcium}
    \tau_{Ca} \frac{\mathrm{d}[Ca^{2+}]}{\mathrm{d}t} = -f(I_{CaT} + I_{CaS}) - [Ca^{2+}] + [Ca^{2+}]_0
\end{equation}

where $\tau_{Ca}$ is the time constant, $f$ translates $Ca^{2+}$ current into an intracellular concentration change, and $[Ca^{2+}]_0$ is the steady state intracellular $Ca^{2+}$ concentration if there is no flux of calcium ions across the membrane\cite{prinz_alternative_2003,liu_model_1998}.

The activity-dependent feedback mechanism is based on regulation by a calcium-dependent homeostatic rule, first implemented by O'Leary \textit{et al.}\cite{oleary_cell_2014}.
Regulation of maximal conductances is simulated by two integral control equations:

\begin{align} \label{eq:integralregulation}
    \tau_i \dot{m_i} &= [Ca^{2+}] - Ca_{tgt} \\
    \label{eq:integralregulation2}
    \tau_g \dot{g_i} &= m_i - g_i
\end{align}

where $\tau_i$ represents the transcription timescale for each ion channel, $\tau_g$ represents global transcription timescale, $m_i$ represents mRNA levels for each ion channel, and $Ca_{tgt}$ represents global Ca\textsuperscript{2+} target concentration. Variables are bounded at 0 to ensure that conductances do not become negative.

In this model, $Ca^{2+}$ acts as a feedback control signal. It is purposefully simple, focusing on the essential biological principles underlying formation and degradation of ion channel proteins. Obviously, neurons possess a significant number of other mechanisms of regulation, such as co-trafficking of ion channels, cotranslational interaction, RNA interference, and the use of promoters\cite{oleary_cell_2014,shi_subunits_1996,vanoye_kcnq1/kcne1_2010,arcangeli_ion_2011,frank_endogenous_2011,zhang_recovery_2011}. Occam's razor dictates that "the simplest solution is most likely the right one," and we will use this as a guideline moving forward.

Steady-state values of $m_i$ are found to be dependent on the time integral of average $[Ca^{2+}]$ and scaled by the inverse of the time constant for the channel $\tau_i$. We can then estimate steady state $g_i$ for very small initial conductance values and positive time constants. We can see this by taking the integral of Equation \ref{eq:integralregulation}:

\begin{equation} \label{eq:steadystateg}
    g_i \approx m_i = \frac{1}{\tau_i} \int_{0}^{t_{ss}} ([Ca^{2+}] - Ca_tgt)dt
\end{equation}

Figure \ref{eq:steadystateg} also shows how correlations between ion channels emerge when $m_i$ converges to the value dependent on the time integral of average $[Ca^{2+}]$. When one takes ratios between channels, the integrals cancel out, so that:

\begin{equation}
    \frac{g_i}{g_j} \approx \frac{\tau_j}{\tau_i}
\end{equation}

It can be seen here that different ratios of the time constants determine the correlations between ion channel conductances. These ratios can determine the electrophysiological character of the neuron, as is further discussed in Section \ref{corr_homeostasis}.

\section{Model Parameters} \label{modelparameters}

Model parameters are adapted from previous work, and are used as the basis for variation in each experiment presented in this thesis\cite{prinz_alternative_2003,oleary_cell_2014,liu_model_1998}.

Each ionic current has its own reversal potential. These values dictate the membrane potential at which ions begin to move in the opposite direction.

\begin{table}[h]
    \centering
    \begin{tabular}{ccccccccc}
         \textsc{Current} & \ac{INaV} & \ac{IA} & \ac{IH} & \ac{IKd} & \ac{IKCa} & \ac{Ileak} \\
         \hline
         $E \mathrm{(mV)}$ & +50 & -80 & -20 & -80 & -80 & -50
    \end{tabular}
    \caption[Reversal potentials for each ion channel.]{Reversal potential for each ionic current. Reversal potential of \ac{ICaT} and \ac{ICaS} are not constant and derived from the Nernst potential.\cite{prinz_alternative_2003}}
    \label{tab:revpotential}
\end{table}

\ac{Vm} evolves with time according to Equation \ref{eq:voltage} where specific membrane capacitance $C_m = 0.628nF$\cite{hodgkin_quantitative_1952}.

Ca\textsuperscript{2+} concentration is dynamic, evolving according to Equation \ref{eq:calcium}, where

   \begin{tabular}{l}
     $\tau_{Ca} = 200ms$ \\
     $f = 14.96 \mu M/nA$
    \end{tabular}

$\tau_{Ca}$ is the calcium buffering time constant, and $f$ is the factor which converts calcium current into intracellular calcium concentration change. This factor depends on the ratio of the surface area of the cell to the volume wherein Ca\textsuperscript{2+} concentration is measured\cite{liu_model_1998}. In this case, volume is considered to be a narrow shell inside the membrane and approximates the neuron by a cylinder $50 \mu m$ in diameter and $400 \mu m$ long. Surface area of the cell is 0.0628 mm\textsuperscript{2}\cite{liu_model_1998}.

Reversal potentials of Ca\textsuperscript{2+} current types are dependent on voltage and calcium, and are dynamically updated by the Nernst equation\cite{liu_model_1998}:

\begin{equation}
    E_{Ca} = \frac{RT}{zF} ln(\frac{[Ca^{2+}]_{ext}}{[Ca^{2+}]_{in}}
\end{equation}

where $E_{Ca}$ refers to reversal potentials for \ac{ICaS} and \ac{ICaT}, gas constant $R = 8.314 J*mol/K$, temperature $T = 284.15 K$, ion charge $z = 2$ for calcium ions, Faraday constant $F = 96485 C/mol$, $[Ca^{2+}]_{in}$ is the current intracellular calcium concentration, and the extracellular calcium concentration $[Ca^{2+}]_{ext} = 0.05 \mu M$\cite{prinz_alternative_2003}.

Voltage-dependent equations for the model parameters for each current were adapted from \cite{prinz_alternative_2003}. Each ionic current has its own specific $p$, steady-state activation variable $m_\infty$, steady-state inactivation variable $h_\infty$, and time constants $\tau_m$ and $\tau_h$, respectively. Ionic currents which are not inactivating do not have values for $h_\infty$ or $\tau_h$.

Maximal conductances are dependent on Ca\textsuperscript{2+} concentration and are regulated by Equations \ref{eq:integralregulation} and \ref{eq:integralregulation2}. The time constant for transcriptional rate $\tau_{m_i}$ is unique for each maximal conductance and defined by:

\begin{equation}
    \tau_{m_i} = \frac{5e6}{g_{\infty_i}}
\end{equation}

where $tau_m$ is in milliseconds and $g_{i\infty}$ represents the steady-state maximal conductance for each ionic channel. Steady-state maximal conductances were originally adapted from Prinz \textit{et al.}\cite{prinz_alternative_2003}.

\begin{table}[H]
    \centering
    \begin{tabular}{ccccccccc}
        \textsc{Ion channel} & NaV & CaT & CaS & A & H & KCa & Kd & Leak \\
        \hline
    $g_\infty$ $(\mu S/mm^2)$ & $1e3$ & $25$ & $60$ & $5e2$ & $0.1$ & $50$ & $1e3$ & $*$ \\
        \hline
        $\tau_m$ $(ms)$ & $5e3$ & $2e5$ & $\approx 8.33e4$ & $1e4$ & $5e7$ & $1e5$ & $5e3$ & $*$ \\
        \hline
        $\tau_g$ $(ms)$ & $5e3$ & $5e3$ & $5e3$ & $5e3$ & $5e3$ & $5e3$ & $5e3$ & $*$
    \end{tabular}
    \caption[Integral control model parameters]{Default integral control model parameters. $g_\infty$ adapted from Prinz \textit{et al.} to determine transcriptional rate time constants $\tau_m$ for each ionic current\cite{prinz_alternative_2003}. Translation rate time constant $\tau_g$ is equal across all ionic currents. *Leak is unregulated in this model and thus does not have $\tau_m$ or $\tau_g$ time constants.}
    \label{tab:integralparameters}
\end{table}

Table \ref{tab:integralparameters} describes the regulation time constants used in both Equations \ref{eq:integralregulation} and \ref{eq:integralregulation2}. Steady state maximal conductances ($g_\infty$) were computed by simulating a known working set of eight channel maximal conductances adapted from Prinz. \textit{et al.} for $t = 400s$ with a time-step of $dt = 0.1ms$ to ensure all maximal conductances converged at their respective steady state densities\cite{prinz_alternative_2003}. These values are only used to compute the transcription and translation rate time constants, and are not used at any point after these values are calculated. It should be of note that these time constants are the default for all experiments, except in cases where the time constants are being varied themselves. In that case, these default values are used as a median point upon which to build a distribution. Target Ca\textsuperscript{2+} concentration was established for a bursting neuron through this same method. Average Ca\textsuperscript{2+} concentration, averaged over 200ms, was obtained from the model after simulation. Initialization of Ca\textsuperscript{2+} target concentration and regulation time constants was repeated at the beginning of each experiment.

\section{Simulating Neurons}

Model neurons are simulated according to the exponential Euler method,\cite{gorur-shandilya_xolotl_2018,dayan2001theoretical} which approximates \ac{Vm} at each discrete time step by the equation:

\begin{equation} \label{eq:expeuler}
V(t + \mathrm{d}t) = V_\infty + (V(t) - V_\infty)exp(-\frac{\mathrm{d}t}{\tau_V})
\end{equation}

Neurons are simulated for $t = 200s$ with a timestep of $dt = 0.1ms$. Voltage traces used for figures and metrics are computed by simulating model neurons for $t = 6s$ with a timestep of $dt = 0.1ms$.

The homeostatic mechanism is simulated by the Euler method\cite{gorur-shandilya_xolotl_2018}:

\begin{align} \label{eq:integraleuler}
    Ca_{err} &= Ca_{tgt} - Ca_{prev} \\
    m_i(t+\mathrm{d}t) &= m_i + \frac{\mathrm{d}t}{\tau_{m_i}}Ca_{err} \\
    \bar{g}_i(t+\mathrm{d}t) &= \bar{g}_i + \frac{\mathrm{d}t}{\tau_{g_i}}(m_i - \bar{g}A)
\end{align}

where $m_i$ represents ion channel mRNA concentration for each current, $\bar{g}_i$ represents maximal conductance density for each current, $Ca_{tgt}$ represents target average Ca\textsuperscript{2+} concentration, $Ca_{prev}$ represents Ca\textsuperscript{2+} concentration at the previous time-step, and $A$ represents the surface area of the model neuron.

\section{Initial Conditions} \label{initialconditions}

Model compartments are initialized with parameters $\ac{Vm} = -60 mV$ and $[Ca^{2+}_{in}] = 0.05 \mu M$.
Initial maximal conductances ($\mu S/mm^2$) for each channel (excluding \ac{gleak}) and compartment mRNA concentration ($\mu M$) are varied across all experiments. \ac{gleak} is held fixed at $\bar{g}_{leak} = 0.05$, except in Figure \ref{fig:integralvariation_Leak}.
Values are generated for all simulations at the beginning of each experiment by the use of a random number generator scaled by a noise factor.
For the first experiment, where initial conditions are varied alone, the noise factor is $20$ for channel maximal conductances and $0.004$ for compartment mRNA concentration. For all other experiments, the noise factor is $5$ for channel maximal conductances and $0.001$ for mRNA levels.

\section{Activity Metrics} \label{electrophys}

For each simulation, metrics were computed on model parameters to determine if models were working or not. Models were considered to have converged if

\begin{equation}
    \mid \frac{([Ca_{tgt}] - [Ca_{avg}])}{[Ca_{tgt}]} \mid > 0.1
\end{equation}

where $Ca_{tgt}$ is the target Ca\textsuperscript{2+} concentration and $Ca_{avg}$ is the average Ca\textsuperscript{2+} concentration after homeostatic regulation.

Models were then checked against reference model with parameters described in Table \ref{tab:integralparameters}. Burst periods and duty cycle were checked against reference model. If the burst period of the tested model deviated from reference by more than 20\% or the duty cycle deviated from reference more than 10\%, the model was considered nonfunctional.

\section{Model Implementation} \label{implementation}

Model neurons were implemented using \texttt{xolotl} v.20.4.22 and v.20.2.26, an open-source neuron simulator written in C++ for MATLAB\cite{gorur-shandilya_xolotl_2018}. Credit to Srinivas Gorur-Shandilya for development and implementation of the modeling platform. Simulation data was retrieved through the use of MATLAB ver. R2020a.


\section{The Divergence Theorem}\label{sec:DivergenceTheorem}

The third version of Green's Theorem (Equation~\ref{eq:greens theorem third form}) we saw was:
$$\int_{\partial D} \vect{f}\cdot\vect{N}\,ds=\iint_{D} \nabla\cdot\vect{f}\,dA.$$ With minor changes this turns into another equation, the
Divergence Theorem:

\begin{theorem}{Divergence Theorem}{}
Under suitable conditions, if $E$ is a
region of three dimensional space and $D$ is its boundary surface,
oriented outward, then
$$\iint_{D} \vect{f}\cdot\vect{N}\,dS=\iiint_{E} \nabla\cdot\vect{f}\,dV.$$\index{divergence!theorem}
\end{theorem}
\goodbreak
\begin{proof}
Again this theorem is too difficult to prove here, but a special case
is easier. In the proof of a special case of Green's Theorem, we
needed to know that we could describe the region of integration in
both possible orders, so that we could set up one double integral
using $dx\,dy$ and another using $dy\,dx$. Similarly here, we need to
be able to describe the three-dimensional region $E$ in different
ways.

We start by rewriting the triple integral:
$$\iiint_{E} \nabla\cdot\vect{f}\,dV = \iiint_{E} ({\partial f_1 \over \partial x}+{\partial f_2 \over \partial y}+{\partial f_3 \over \partial z})\,dV
=\iiint_{E} {\partial f_1 \over \partial x}\,dV + \iiint_{E} {\partial f_2 \over \partial y}\,dV + \iiint_{E} {\partial f_3 \over \partial z}\,dV.$$
The double integral may be rewritten:
$$\iint_{D} \vect{f}\cdot\vect{N}\,dS
=\iint_{D} (f_1\vect{i}+f_2\vect{j}+f_3\vect{k})\cdot\vect{N}\,dS
=\iint_{D} f_1\vect{i}\cdot\vect{N}\,dS+\iint_{D} f_2\vect{j}\cdot\vect{N}\,dS+
\iint_{D} f_3\vect{k}\cdot\vect{N}\,dS.$$
To prove that these give the same value it is sufficient to prove that
\begin{align}\label{eq:divergence proof}
\iint_{D} f_1\vect{i}\cdot\vect{N}\,dS&=\iiint_{E} {\partial f_1 \over \partial x}\,dV,	\\
\iint_{D} f_2 \vect{j}\cdot\vect{N}\,dS&=\iiint_{E} {\partial f_2 \over \partial y}\,dV,\;\hbox{and}	\\
\iint_{D} f_3 \vect{k}\cdot\vect{N}\,dS&=\iiint_{E} {\partial f_3 \over \partial z}\,dV.
\end{align}
% $$
% \iint{D} P\vect{i}\cdot\vect{N}\,dS=\iiint{E} P_x\,dV,\;
% \iint{D} Q\vect{j}\cdot\vect{N}\,dS=\iiint{E} Q_y\,dV,\;\hbox{and}\;
% \iint{D} R\vect{k}\cdot\vect{N}\,dS=\iiint{E} R_z\,dV.$$
Not surprisingly, these are all pretty much the same; we'll do the
first one.

We set the triple integral up with $dx$ innermost:
$$\iiint_{E} {\partial f_1 \over \partial x}\,dV=\iint_{B}\int_{g_1(y,z)}^{g_2(y,z)} {\partial f_1 \over \partial x}\,dx\,dA=
\iint_{B} f_1(g_2(y,z),y,z)-f_1(g_1(y,z),y,z)\,dA,$$
where $B$ is the region in the $y$-$z$ plane over which we integrate.
The boundary surface of $E$ consists of a ``top'' $x=g_2(y,z)$, a
``bottom'' $x=g_1(y,z)$, and a ``wrap-around side'' that is vertical
to the $y$-$z$ plane. To integrate over the entire boundary surface,
we can integrate over each of these (top, bottom, side) and add the
results. Over the side surface, the vector $\vect{N}$ is perpendicular to
the vector $\vect{i}$, so
$$
\iint_{\hbox{\scriptsize side}} f_1\vect{i}\cdot\vect{N}\,dS =
\iint_{\hbox{\scriptsize side}}
0\,dS=0.$$
Thus, we are left with just the surface integral over the top plus the
surface integral over the bottom. For the top, we use the vector
function
$\vect{r}=\langle g_2(y,z),y,z\rangle$ which gives
$\vect{r}_y\times\vect{r}_z=\langle 1,-g_{2y},-g_{2z}\rangle$; the dot
product of this with $\vect{i}=\langle 1,0,0\rangle$ is 1. Then
$$
\iint_{\hbox{\scriptsize top}} f_1\vect{i}\cdot\vect{N}\,dS
=\iint_{B} f_1(g_2(y,z),y,z)\,dA.$$
In almost identical fashion we get
$$
\iint_{\hbox{\scriptsize bottom}} f_1\vect{i}\cdot\vect{N}\,dS
=-\iint_{B} f_1(g_1(y,z),y,z)\,dA,$$
where the negative sign is needed to make $\vect{N}$ point in the
negative $x$ direction. Now
$$\iint_{D} f_1\vect{i}\cdot\vect{N}\,dS
=\iint_{B} f_1(g_2(y,z),y,z)\,dA-\iint_{B}
    f_1(g_1(y,z),y,z)\,dA,$$ which is the same as the value of the
    triple integral above.
\end{proof}

It is worth noting that this theorem is also referred to as \textit{Gauss' Theorem}. We now compute an example. \index{Gauss' Theorem}

\begin{example}{}{}
Let $\vect{f}=\langle 2x,3y,z^2\rangle$, and consider the
three-dimensional volume inside the cube with faces parallel to the
principal planes and opposite corners at
$(0,0,0)$ and $(1,1,1)$. Compute the two integrals of the
divergence theorem.
\end{example}

\begin{solution}
The triple integral is the easier of the two:
$$\int_0^1\int_0^1\int_0^1 2+3+2z\,dx\,dy\,dz=6.$$
The surface integral must be separated into six parts, one for each
face of the cube. One face is $z=0$ or $\vect{r}=\langle u,v,0\rangle$, $0\le
u,v\le 1$. Then $\vect{r}_u=\langle 1,0,0\rangle$,
$\vect{r}_v=\langle 0,1,0\rangle$, and $\vect{r}_u\times\vect{r}_v=
\langle 0,0,1\rangle$. We need this to be oriented downward (out of
the cube), so we use
$\langle 0,0,-1\rangle$ and the corresponding integral is
$$\int_0^1\int_0^1 -z^2\,du\,dv=\int_0^1\int_0^1 0\,du\,dv=0.$$

Another face is $y=1$ or $\vect{r}=\langle u,1,v\rangle$. Then $\vect{r}_u=\langle 1,0,0\rangle$, $\vect{r}_v=\langle 0,0,1\rangle$, and
$\vect{r}_u\times\vect{r}_v= \langle 0,-1,0\rangle$. We need a normal in
the positive $y$ direction, so we convert this to $\langle
0,1,0\rangle$, and the corresponding integral is
$$\int_0^1\int_0^1 3y\,du\,dv=\int_0^1\int_0^1 3\,du\,dv=3.$$

The remaining four integrals have values 0, 0, 2, and 1, and the sum
of these is 6, in agreement with the triple integral.
\end{solution}

\begin{example}{}{}
Let $\vect{f}=\langle x^3,y^3,z^2\rangle$, and consider the
cylindrical volume $x^2+y^2\le9$, $0\le z\le2$.
Compute the two integrals of the divergence theorem.
\end{example}

\begin{solution}
The triple integral (using cylindrical coordinates) is
$$\int_0^{2\pi}\int_0^3\int_0^2 (3r^2+2z)r\,dz\,dr\,d\theta=279\pi.$$

For the surface we need three integrals. The top of the cylinder can
be represented by
$\vect{r}=\langle v\cos u,v\sin u,2\rangle$;
$\vect{r}_u\times\vect{r}_v=\langle 0,0,-v\rangle$, which points down
into the cylinder,
so we convert it to $\langle 0,0,v\rangle$. Then
$$\int_0^{2\pi}\int_0^3 \langle v^3\cos^3u,v^3\sin^3u,4\rangle\cdot
\langle 0,0,v\rangle\,dv\,du=
\int_0^{2\pi}\int_0^3 4v\,dv\,du=36\pi.$$
The bottom is
$\vect{r}=\langle v\cos u,v\sin u,0\rangle$;
$\vect{r}_u\times\vect{r}_v=\langle 0,0,-v\rangle$ and
$$\int_0^{2\pi}\int_0^3 \langle v^3\cos^3u,v^3\sin^3u,0\rangle\cdot
\langle 0,0,-v\rangle\,dv\,du=
\int_0^{2\pi}\int_0^3 0\,dv\,du=0.$$
The side of the cylinder is $\vect{r}=\langle 3\cos u,3\sin u,v\rangle$;
$\vect{r}_u\times\vect{r}_v=\langle 3\cos u,3\sin u,0\rangle$ which does
point outward, so
\begin{align*}
\int_0^{2\pi}\int_0^2 &\langle 27\cos^3 u,27\sin^3 u,v^2\rangle\cdot
\langle 3\cos u,3\sin u,0\rangle \,dv\,du	\\
&=\int_0^{2\pi}\int_0^2 81\cos^4 u+81\sin^4u\,dv\,du=243\pi.
\end{align*}
The total surface integral is thus $36\pi+0+243\pi=279\pi$.
\end{solution}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\Opensolutionfile{solutions}[ex]
\section*{Exercises for \ref{sec:DivergenceTheorem}}

\begin{enumialphparenastyle}

\begin{ex}
Using $\ds\vect{f}=\langle 3x,y^3,-2z^2\rangle$ and the
region bounded by $\ds x^2+y^2=9$, $z=0$, and $z=5$, compute both
integrals from the Divergence Theorem.
\begin{sol}
	both are $-45\pi/4$
\end{sol}
\end{ex}

\begin{ex}
Let $E$ be the volume described by
$0\le x\le a$, $0\le y\le b$, $0\le z\le c$, and
$\vect{f}= \langle x^2,y^2,z^2\rangle$. Compute
$\ds\iint_{\partial E} \vect{f}\cdot \vect{N}\,dS$.
\begin{sol}
	$a^2bc+ab^2c+abc^2$
\end{sol}
\end{ex}

\begin{ex}
Let $E$ be the volume described by
$0\le x\le 1$, $0\le y\le 1$, $0\le z\le 1$, and
$\vect{f}= \langle 2xy,3xy,ze^{x+y}\rangle$. Compute
$\ds\iint_{\partial E} \vect{f}\cdot \vect{N}\,dS$.
\begin{sol}
	$e^2-2e+7/2$
\end{sol}
\end{ex}

\begin{ex}
Let $E$ be the volume described by
$0\le x\le 1$, $0\le y\le x$, $0\le z\le x+y$, and
$\vect{f}= \langle x,2y,3z\rangle$. Compute
$\ds\iint_{\partial E} \vect{f}\cdot \vect{N}\,dS$.
\begin{sol}
	$3$
\end{sol}
\end{ex}

\begin{ex}
Let $E$ be the volume described by
$x^2+y^2+z^2\le 4$, and
$\vect{f}= \langle x^3,y^3,z^3\rangle$. Compute
$\ds\iint_{\partial E} \vect{f}\cdot \vect{N}\,dS$.
\begin{sol}
	$384\pi/5$
\end{sol}
\end{ex}

\begin{ex}
Let $E$ be the hemisphere described by
$0\le z\le \sqrt{1-x^2-y^2}$, and
$\vect{f}= \langle \sqrt{x^2+y^2+z^2},\sqrt{x^2+y^2+z^2},
\sqrt{x^2+y^2+z^2}\rangle$. Compute
$\ds\iint_{\partial E} \vect{f}\cdot \vect{N}\,dS$.
\begin{sol}
	$\pi/3$
\end{sol}
\end{ex}

\begin{ex}
Let $E$ be the volume described by
$x^2+y^2\le1$, $0\le z\le4$, and
$\vect{f}= \langle xy^2,yz,x^2z\rangle$. Compute
$\ds\iint_{\partial E} \vect{f}\cdot \vect{N}\,dS$.
\begin{sol}
	$10\pi$
\end{sol}
\end{ex}

\begin{ex}
Let $E$ be the solid cone above the $x$-$y$ plane and
inside $z=1-\sqrt{x^2+y^2}$, and
$\vect{f}= \langle x\cos^2 z,y\sin^2z,\sqrt{x^2+y^2}z\rangle$. Compute
$\ds\iint_{\partial E} \vect{f}\cdot \vect{N}\,dS$.
\begin{sol}
	$\pi/2$
\end{sol}
\end{ex}

\begin{ex}
Prove the other two equations in the
display~\ref{eq:divergence proof}.
\end{ex}

\begin{ex}
Suppose $D$ is a closed surface, and that $D$ and $F$ are
sufficiently nice. Show that
$$\iint_{D} (\nabla \times \vect{f}) \cdot \vect{N}\, dS = 0$$
where $\vect{N}$ is the outward pointing unit normal.
\end{ex}

\begin{ex}
Suppose $D$ is a closed surface, $D$ is sufficiently nice,
and $F=\langle a,b,c\rangle$ is a constant vector field.
Show that
$$\iint_{D} \vect{f} \cdot \vect{N}\, dS = 0$$
where $\vect{N}$ is the outward pointing unit normal.
\end{ex}

\begin{ex}
We know that the volume of a region $E$ may often be computed as
$\ds \iiint_{E}dx\,dy\,dz$. Show that this volume may also be computed as
$\ds{1\over3} \iint_{\partial E} \langle x,y,z\rangle \cdot \vect{N}\,dS$
where $\vect{N}$ is the outward pointing unit normal to $\partial E$.
\end{ex}

\end{enumialphparenastyle}