\documentclass[ 12pt, a4paper]{article}
% Use the option doublespacing or reviewcopy to obtain double line spacing
% \documentclass[doublespacing]{elsart}

% MARGIN SETTINGS
\usepackage{geometry}
 \geometry{
 a4paper,
 left=30mm,
 right=30mm
 }

\setlength{\topmargin}{1cm}
% \addtolength{\textheight}{0cm}

\usepackage{graphicx}
\usepackage{amssymb,amsmath,mathtools}

\usepackage[mathscr]{eucal} %just for the font \mathscr
\usepackage{mathdots}
\usepackage{setspace}
% \usepackage{stmaryrd}
\usepackage{hyperref}

\newcommand{\be}{\begin{equation}}
\newcommand{\en}{\end{equation}}
\def\bga#1\ega{\begin{gather}#1\end{gather}} % suggested in technote.tex

\def\bal#1\eal{\begin{align}#1\end{align}} % suggested in technote.tex
\def\bals#1\eals{\begin{align*}#1\end{align*}}

\renewcommand{\thefootnote}{\fnsymbol{footnote}}

\DeclareMathOperator{\sign}{sgn}
\DeclareMathOperator{\Ord}{\mathcal{O}}

\newcommand{\filler}{\hspace*{\fill}}
\newcommand{\T}{\mathscr T}
\newcommand{\ii}{\textrm{i}}
\newcommand{\ee}{\textrm{e}}

\renewcommand{\vec}[1]{\boldsymbol{#1}}
\def \bm#1{\mbox{\boldmath{$#1$}}}   % this is used to write boldface Greek

% \doublespacing

\begin{document}

\title{Calculating scattering coefficients from a cylinder}
\author{
Artur L. Gower$^{1}$  \\[12pt]
$^1$ School of Mathematics, University of Manchester \\
Oxford Road, Manchester M13 9PL, UK
}
\date{\today}
\maketitle

\begin{abstract}
Here we derive some of the theory used in the Mathematica package  \href{https://github.com/arturgower/MultipleScattering-Mathematica}{MultipleScattering2D}. Specifically the function \texttt{ScatteringCoefficientOperator}, which calculates the scattering coefficients for any source/exciting function, is given by combing equations~(\ref{eqn:ScatteringOpDirichlet},\ref{eqn:PsiTaylor},\ref{eqn:aDirichlet}) for Dirichlet boundary conditions. Similar equations are given for Neumann too.
\end{abstract}

\noindent
{\textit{Keywords:} Multi-pole method, multiple scattering, scattering operator }

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

\section{The General Setup}
For a review on multiple scattering from a finite number of obstacles see \cite{martin_2006}.

Consider a homogeneous isotropic medium that can propagate wave according to the scalar Helmholtz equation
\be
(\Delta + k^2)\psi(\vec r) =0,
\en
where $k$ is real for acoustics and possibly imaginary for fluids. Let there be $N$ scatterers, with the centre located at $\vec r_1$, $\vec r_1$, ..., $\vec r_N$. If we excite the scatterers by sending an incident wave $\psi^I(\vec r)$, we can then write the total wave field $\psi(\vec r| \vec r_1, \ldots, \vec r_N) = \psi^I(\vec r) + \sum_{j=1}^N \psi^S(\vec r; \vec r_j|  \vec r_1, \ldots, \vec r_N)$, where $\psi^S_j(\vec r) := \psi^S(\vec r; \vec r_j|  \vec r_1, \ldots, \vec r_N)$ is the outward going wave\footnote{We are only concerned with what the scatterer is emitting, and not waves that might bounce around inside.} being emitted from the boundary of the $j$-th scatterer. The term $\vec r_1, \ldots, \vec r_N$ indicates the dependence of $\psi$ and $\psi^S_j$'s on the position of all the scatterers. Each scattered wave $\psi^S_j$ is excited by
\be
\psi^E_j(\vec r) = \psi^I(\vec r) + \sum_{i \not = j} \psi^S_i(\vec r),
\label{eqn:ExcitedWave}
\en
which we can relate to $\psi^S_j$ through the boundary condition on the $j$-th scatterer to give
\be
\psi^S_j(\vec r) =  \T_j \left \{ \psi^E_j \right \}(\vec r),
\label{eqn:LinearScattering}
\en
where $\T_j\{\circ\}(\vec r)$ is the linear \emph{scattering operator} which acts on the whole function $\circ$. Dirchlett and Neumann boundary condtions are examples of when $\T_j$ is a linear operator. By expanding $\psi^S_j$ as in Eq.\eqref{eqn:LinearScattering}, then substituting $\psi^E_j$ from  Eq.\eqref{eqn:ExcitedWave} and then repeating the process by expanding $\psi^S_i$ with eq.\eqref{eqn:LinearScattering} again we obtain
\be
\psi^S_j(\vec r) =   \T_j \{ \psi^I \}(\vec r) + \sum_{i \not = j}  \T_j\circ \T_i \left \{ \psi^I \right \}(\vec r) + \sum_{i \not = j}   \sum_{n \not = i} \T_j \circ \T_i \circ \T_n \left \{  \psi^I\right \}(\vec r)    + \ldots.
\en
For two scatterers this becomes,
\be
\psi^S_1(\vec r) =   \T_1 \{ \psi^I \}(\vec r) + \T_1\circ \T_2 \left \{ \psi^I \right \}(\vec r) +  \T_1 \circ \T_2 \circ \T_1 \left \{  \psi^I\right \}(\vec r)    + \ldots.
\en
If on the other hand we stopped expanding the series at
\bga
\psi^S_1(\vec r) =  \T_1 \left \{ \psi^I \right \}(\vec r)  + \T_1 \left \{ \psi^S_2  \right \}(\vec r), \notag \\
\psi^S_2(\vec r) =  \T_2 \left \{ \psi^I \right \}(\vec r)  + \T_2 \left \{ \psi^S_1  \right \}(\vec r).
\label{eqn:TwoScatterers}
\ega
If we could expand each $\psi^S_j$ into a general outgoing wave
\[
\psi^S_j(\vec r) = \sum_{n=-\infty}^\infty s_{jn} \mathcal C_n ( r) \mathcal A_n(\theta, \phi),
\]
then we could solve for the coefficients $s_{jn}$ by substituting into Eqs.\eqref{eqn:TwoScatterers}.

\section{Cylindrical Scatterers}

The outgoing wave from a scatterer at $\vec r_j$ be expanded as
\be
\psi^{S_j}(\vec r) = \sum_{n=-\infty}^\infty a_{jn} H_n (k \|\vec r - \vec r_j \|) \ee^{\ii n \alpha_j},
\label{eqn:OutingSj}
\en
where $H_n:= H_n^{(1)}$ is a Hankel function of the first kind and $\alpha_j$ is the angle between $\vec r - \vec r_j$ and the $x$--axis.

We can expand the outgoing waves~\eqref{eqn:OutingSj} by using Graf's addition theorem\footnote{I double checked this with both \url{http://dlmf.nist.gov/10.23} and \url{http://www.wikiwaves.org/Graf's_Addition_Theorem}.}:
%\be
%\mathscr C_\nu (k \|  \vec r - \vec r_1\|) \ee^{\pm \ii \nu (\alpha_1 -\theta_{12})} =
           %\sum_{m=-\infty}^\infty \mathscr C_{\nu +m} (k \| \vec r_1 - \vec r_2 \|) J_m(k \|\vec r- \vec r_2 \|) \ee^{ \pm \ii m (\pi +\theta_{ij} - \alpha_2)},
%\label{eqn:Graf}
%\en
%I calculate this $\pm$ above thinking that the angle $\alpha_{12}$ between to vectors $\vec r_1$ and $\vec r_2$ satisfies $\alpha_{12} = \alpha_{21}$, but that is not true. In fact $\alpha_{12} = 2 \pi - \alpha_{21}$.
\be
\mathscr C_\nu (k \|  \vec r - \vec r_1\|) \ee^{ \ii \nu (\alpha_1 -\theta_{12})} =
           \sum_{m=-\infty}^\infty \mathscr C_{\nu +m} (k \| \vec r_1 - \vec r_2 \|) J_m(k \|\vec r- \vec r_2 \|) \ee^{ \ii m (\pi +\theta_{12} - \alpha_2)},
\label{eqn:Graf}
\en
provided $\|\vec r- \vec r_2 \|  < \|\vec r_1- \vec r_2 \|$, where $\theta_{12}$ and $\alpha_2$ are respectively the angular cylindrical coordinate of $\vec r_1 - \vec r_2$ and $\vec r -\vec r_2$.
%\bga
%\cos \chi = \frac{(\vec r_i-\vec r_j)\cdot \vec r_i}{\|\vec r_i-\vec r_j \| r_i} = \frac{r_i}{\|\vec r_i -\vec r_j\|} - \cos \theta_{ij} \frac{r_j}{\|\vec r_i -\vec r_j\|},
%\\
%\sin \chi = \frac{\|(\vec r_i-\vec r_j)\times \vec r_i \|}{\|\vec r_i-\vec r_j \| r_i} = \sin \theta_{ij} \frac{r_j}{\|\vec r_i -\vec r_j\|} \implies
%\\
 %\ee^{ \ii \nu \chi} = \left ( \frac{r_i - r_j \ee^{- \ii \theta_{ij} }}{\|\vec r_i -\vec r_j\|} \right )^\nu.
%\ega
In the addition theorem $\mathscr C_\nu$ can be substituted with any of the Bessel functions $J_\nu,Y_\nu,H_\nu^{(1)}$ and $H_\nu^{(2)}$, or linear combinations of them.

Using Eq.\eqref{eqn:Graf} we write $\psi^{S_j}$ in terms of an origin centered at the $i$-th scatterer by substituting
\[
H_n (k \|  \vec r - \vec r_j\|) \ee^{ \ii n \alpha_j} =
            \sum_{m=-\infty}^\infty H_{n -m} (k \| \vec r_j - \vec r_i \|) J_m(k \|\vec r- \vec r_i \|) \ee^{ \ii m \alpha_i +\ii (n - m) \theta_{ij}},
\]
into the $\psi^{S_j}$ to arrive at
\bga
\psi^{S_j}(\vec r) = \sum_{n=-\infty}^\infty \sum_{m=-\infty}^\infty a_{jn} H_{n -m} (k \| \vec r_j - \vec r_i \|) J_m(k \|\vec r- \vec r_i \|) \ee^{ \ii m \alpha_i +\ii (n - m) \theta_{ij}}.
%\\
%\psi^{S_j}(\vec r) = \sum_{n=-\infty}^\infty \sum_{m=-\infty}^\infty a_{jn}   H_{n +m} (k r_j) J_m(k r) \ee^{\ii ( n-m) \theta + \ii ( m \theta_j -  n \chi )}.
%\\= \sum_{n=-\infty}^\infty \sum_{p=-\infty}^\infty a_{jn}   H_{2 n -p} (k r_j) J_{n-p}(k r) \ee^{\ii p \theta + \ii ( (n-p) \theta_j -  n \chi )},
\ega
%where to get the last equation we renamed $m = n- p$.

In Section~\ref{sec:BoundaryConditions} we derive some explicit forms for the scattering operator $\T$, given by
Eqs.\eqref{eqn:ScatteringOpDirichlet} and \eqref{eqn:ScatteringOpNeumann}. Assuming that the scatterers are small compared with the wavelength $k r_S << 1$, where $r_S$ is the radius of the scatterer, then the most general form for outgoing waves from the $j$-th scatterer is
\bga
\psi^{S_j}(\vec r) = a_{j} H_0 (k \|\vec r - \vec r_j \|) + (c_{j} \cos (\vec r - \vec r_j) + s_{j} \sin (\vec r - \vec r_j) ) H_1 (k \|\vec r - \vec r_j \|) ,
\ega
with $c_{j}=s_{j} =0$ for Dirichlet boundary conditions, where $\cos(\vec r - \vec r_j) = \cos \vartheta$ and $\vartheta$ is the angle between $\vec r - \vec r_j$ and the $x$-axis.
In which case, using the results in Section~\ref{sec:BoundaryConditions}, the scattering operator for Neumann boundary conditions can be written as
\begin{multline}
\T_j : \psi^E(r, \theta) \to  \frac{ \ii \pi r_S^2}{4}  (\psi^E_{\vec r_j,xx} + \psi^E_{\vec r_j,yy}) H_0(k \| \vec r - \vec r_j \|)
\\
+ \frac{i \pi  r^{2}_S}{2} k( \psi^E_{\vec r_j,x} \cos ( \vec r - \vec r_j ) +  \psi^E_{\vec r_j,y} \sin ( \vec r - \vec r_j )) H_1(k \| \vec r - \vec r_j \|),
\label{eqn:TjOpNeumann}
\end{multline}
accurate up to order $r^{3}_S$ in the scatterer radius $r_S$, where the subscript $\vec r_j$ on $\psi^E_{\vec r_j}$ means that $\psi^E$ and its derivatives are evaluated at $\vec r = \vec r_j$ after differentiation. This implies that to solve Eqns.~\eqref{eqn:TwoScatterers} for every $\vec r$ we need to equate the coefficients of $H_0(k \| \vec r - \vec r_j \|)$, $\cos ( \vec r - \vec r_j ) H_1(k \| \vec r - \vec r_j \|)$ and $\sin ( \vec r - \vec r_j ) H_1(k \| \vec r - \vec r_j \|)$ each to be zero, from which we get
\bga
a_{i} = \frac{ \ii \pi r_S^2}{4}  (\psi^I_{\vec r_i,xx} + \psi^I_{\vec r_i,yy}+ \psi^{S_j}_{\vec r_i,xx} + \psi^{S_j}_{\vec r_i,yy}),
\\
c_i = \frac{i \pi  r^{2}_S}{2} k( \psi^{I}_{\vec r_i,x}+ \psi^{S_j}_{\vec r_i,x}), \;\; s_i = \frac{i \pi  r^{2}_S}{2} k( \psi^{I}_{\vec r_i,y}+ \psi^{S_j}_{\vec r_i,y}).
\ega
To simplify we choose,  without loss of generality, $x_2 = x_1$ and $- y_1 = y_2 = Y/2$ which we use to reduce Eqns.~\eqref{eqn:TwoScatterers} to
\bga
 \frac{k^2}{2}  (H_0 - H_2) a_1 + \frac{k^2}{4} (3 H_1 - H_3)s_1 - \frac{4 \ii }{\pi r_S^2} a_{2} = \psi^I_{\vec r_2,xx} + \psi^I_{\vec r_2,yy},
\\
\frac{H_1}{Y}c_1 +\frac{2 i }{\pi  k r_S^2}c_2 =-\psi^I_{\vec r_2, x}, \;\;
 k H_1 a_1 -\frac{2 i}{\pi  k r_S^2}s_2-\frac{k }{2} (H_0-H_2) s_1 =\psi^I_{\vec r_2, y}
\ega
where the Hankel functions $H_0$, $H_1$, $H_2$ and $H_3$ are evaluated at $k Y$, with $Y = \| \vec r_1 - \vec r_2 \|$, and to reach the above equations we have used some recurrence relations to calculate the derivatives of $H_0$ and $H_1$.

%We need
%\bga
%\psi^S_{0,xx} + \psi^S_{0,yy} = a_{2} \left (\frac{k}{r_j}H_0'(k r_j) +k^2 H_0''( k r_j)\right ) + (c_{2} \cos (\vec r - \vec r_2)
%\\
%+ s_{2} \sin (\vec r - \vec r_2) ) \left( \frac{k}{r_j}H_1'(k r_j) +k^2 H_1''( k r_j) \right)
%\ega

Apply $\T_1$ on $\psi^S_2$ with the centre of scatterer 1 as the origin we get
\be
\T_1 \psi^S_2(\vec r) = a_{2} H_0 (k \|\vec r - \vec r_2 \|) + (c_{2} \cos (\vec r - \vec r_2) + s_{2} \sin (\vec r - \vec r_2) ) H_1 (k \|\vec r - \vec r_2 \|) ,
\en

The following formulas will be useful, for $f(\|\vec r - \vec r_j\|)$ we have
\bga
f_{,x} (k r_j) =  \frac {k x_j}{r_j} f'( k r_j) , \; \;
 f_{,y} (k r_j) =  \frac {k y_j}{r_j} f' ( k r_j), \\
 f_{,yy} (k r_j) +  f_{,xx} (k r_j) =  \frac{k}{r_j}f'(k r_j) +k^2 f''( k r_j).
\ega
% and
% \bga
% \partial_x \cos(\vec r - \vec r_j) |_{\vec r =0} = \frac{y_j^2}{r_j^{3}}, \; \; \; \partial_x \sin(\vec r - \vec r_j) |_{\vec r=0} = -\frac{x_j y_j}{r_j^3}, \\
% \partial_y \cos(\vec r - \vec r_j) |_{\vec r =0} = - \frac{x_j y_j}{r_j^{3}}, \; \; \; \partial_y \sin(\vec r - \vec r_j) |_{\vec r =0} = \frac{x_j^2}{r_j^3}, \\
% \left ( \partial_{xx}+ \partial_{yy} \right )\cos(\vec r - \vec r_j) |_{\vec r =0} = \frac{x_j}{r_j^3}, \;\;  \left ( \partial_{xx}+ \partial_{yy} \right )\sin(\vec r - \vec r_j) |_{\vec r =0} = \frac{y_j}{r_j^3}.
% \ega

\subsection{Boundary conditions and point scatterers}
 \label{sec:BoundaryConditions}
 Here we develop expressions for the scattering operator $\T$ and establish how point scatterers behave for different boundary conditions. Given an incident wave $\psi^E$ and outgoing cylindrical wave $\psi^S = a_n H_n(k r) \ee^{\ii n \theta}$, together $\psi^E + \psi^S$ must satisfy the boundary condition on the cylinder with radius $r = r_S$. To simplify we choose the origin of our coordinate system to be the centre of the scatterer and will use $\bar r := r k$.

For a Dirichlet boundary conditions we have
%\bga
%\psi^E(r_S, \theta) + \psi^S(r_S, \theta) =0 \;\; \text{ for } \; 0\leq \theta < 2 \pi  \implies
  %a_n   = - H_n(r_S)^{-1} \mathcal C_n(r_S).
%\ega
\bga
\psi^E(\bar r_S, \theta) + \psi^S(\bar r_S,\theta) =0 \;\; \text{ for } \; 0\leq \theta < 2 \pi  \implies
  a_n  = - \frac{H_n(\bar r_S)^{-1}}{2\pi}\int_{0}^{2 \pi}\psi^E(\bar r_S, \theta)\ee^{-\ii n \theta} d \theta,
\label{eqn:aDirichlet}
\ega
noting that $\ee^{\ii n \theta}$ forms a complete basis for functions on $\theta \in [0, 2\pi]$.

For a Neumann boundary conditions we have
\bga
\frac{\partial \psi^E}{\partial \bar r}(\bar r_S, \theta) + \frac{\partial \psi^S}{\partial \bar r}(\bar r_S, \theta) =0  \;\; \text{ for } \; 0\leq \theta < 2 \pi  \implies
\\
  a_n  = - \frac{\partial_{\bar r} H_n( \bar r_S)^{-1}}{2\pi}\int_{0}^{2 \pi}\partial_{\bar r} \psi^E(\bar r_S, \theta)\ee^{-\ii n \theta} d \theta,
\label{eqn:aNeumann}
\ega

Our main interest is to model point scatterers for which $\bar r_S \to 0$. In this limit the incident wave should converge in an open ball to its Taylor series at $r_S$  (as the wave field should be perfectly smooth), so we can write
\begin{multline}
\psi^E(\bar r_S, \vartheta) =  \sum_{k= 0}^\infty \sum_{m= 0}^p \frac{\partial^p \psi^E_0}{\partial_{x}^m \partial_{y}^{p-m}}   \frac{ r_S^p (\cos \vartheta)^m (\sin \vartheta)^{p-m} }{m! (p-m)!}
\\
= \sum_{p= 0}^\infty \sum_{m= 0}^p \frac{\partial^p \psi^E_0}{\partial_{x}^m \partial_{ y}^{p-m}}  \frac{ r_S^p  }{m! (p-m)!} \sum_{m_1= 0}^m \sum_{p_1= 0}^{p-m} \frac{\ee^{\ii (p-2 m_1-2 p_1)\vartheta}   }{(-1)^{p_1} 2^p \ii^{p-m} } \binom{m}{m_1} \binom{p-m}{p_1},
%Formula checked from scratch in Mathematica!
\label{eqn:PsiTaylor}
\end{multline}
where the subscript $0$ on $\psi^E_0$ and its derivatives means that the expression was evaluated at $x=y=0$ after differentiation. When substituting this expression into the integral in Eq.\eqref{eqn:aDirichlet}, only terms with $p-2 m_1-2 p_1 =n$ will have a non-zero contribution, because every other term  after multiplying with $\ee^{-\ii n \vartheta}$ and integrating in $\vartheta$ over $0$ to $2 \pi$ will be zero. Likewise, only terms with $p-2 m_1-2 p_1 =n$ will have a non-zero contribution to Eq.\eqref{eqn:aNeumann}.
So letting $p_1 =  j-n/2- m_1$ and $p = 2 j$, so that  $p-2 m_1-2 p_1 =n$, and integrating over $\vartheta$ we conclude that
%So letting $p_1 = (|n|- n)/2 +j- m_1$ and $p = |n|+ 2 j$, so that  $p-2 m_1-2 k_1 =n$, and integrating over $\vartheta$ we conclude that
%\[
%\sum_{j= 0}^\infty \sum_{m= 0}^{|n|+ 2 j} \frac{\partial^{ |n|+ 2 j} \psi^E_0}{\partial_{\bar x}^m \partial_{\bar y}^{{ |n|+ 2 j}-m}}  \frac{\bar r_S^{ |n|+ 2 j}  }{m! (k-m)!} \sum_{m_1= 0}^m \frac{(-1)^{(|n|- n)/2 +j- m_1} }{ 2^{ |n|+ 2 j} \ii^{ |n|+ 2 j -m} } \binom{m}{m_1} \binom{ |n|+ 2 j-m}{(|n|- n)/2 +j- m_1},
%\]
%So letting $k_1 = (k- n)/2- m_1$ and $k = n+ 2 j$ in Eq.~\eqref{eqn:PsiTaylor} and integrating over $\vartheta$ we can conclude
%\begin{multline}
%p_n= \frac{1}{2 \pi}\int_{0}^{2 \pi} \psi^E(\bar r_s, \vartheta)\ee^{- \ii n \vartheta} d \vartheta =
  %\sum_{j= \text{max} \{0, - \ceil{n/2} \} }^\infty \left (  \frac{\bar r_S}{2} \right )^{2 j+n} \sum_{m= 0}^{n+ 2 j} \frac{\partial^{2 j+n} \psi^E_0}{\partial_{\bar x}^m \partial_{\bar y}^{{ 2 j+n} -m}}  \frac{C^m_{j, 2 j +n} \ii^{m{-2 j - n}}   }{m! ({2 j +n}-m)!} ,
%%Formula checked from scratch in Mathematica for the first two C's just below!
%\label{eqn:PsiTaylor}
%\end{multline}
%where
%\be
%C^m_{j, 2 j +n} =  \sum_{m_1= \text{max} \{0, -n -j\} }^{\text{min}\{m, j\} } (-1)^{j-m_1} \binom{m}{m_1} \binom{{2 j +n} -m}{j-m_1}
%\en
%\be
%C^m_{j, p} =  \sum_{m_1= \text{max} \{0, j-p\} }^{\text{min}\{m, j\}} (-1)^{j-m_1} \binom{m}{m_1} \binom{p -m}{j-m_1}.
%\en
%Note that because $k = n+ 2 j$, $n$ can be negative and the minimal value for $k$ is $0$, then $2 j \geq -n $. Also, as $k_1$ was substituted for $j-m_1$, then $-n -j \leq m_1 \leq j$, which combined with the restriction $0 \leq m_1 \leq m$, becomes
%$
%\text{max } \{0, -n -j\} \leq m_1 \leq \text{ min } \{m, j\}.
%$
%\begin{multline}
%p_n= \frac{1}{2 \pi}\int_{0}^{2 \pi} \psi^E(\bar r_s, \vartheta)\ee^{- \ii n \vartheta} d \vartheta = \sum_{j= 0}^\infty \left (\frac{\bar r_S}{2}\right)^{ |n|+ 2 j} \sum_{m= 0}^{|n|+ 2 j} \frac{\partial^{ |n|+ 2 j} \psi^E_0}{\partial_{\bar x}^m \partial_{\bar y}^{{ |n|+ 2 j}-m}}  \frac{\ii^{ m-|n|- 2 j } C^m_{n,j}  }{m! (|n|+ 2 j -m)!} ,
%%Formula checked from scratch in Mathematica for the C's just below!
%\label{eqn:PsiTaylor}
%\end{multline}
%where
%\be
%C^m_{n,j} = \sum_{m_1= m_1^\text{min}}^{m_1^\text{max}} (-1)^{\frac{|n|- n}{2} +j- m_1} \binom{m}{m_1} \binom{ |n|+ 2 j-m}{(|n|- n)/2 +j- m_1},
%\en
\be
\mathcal P_n\{\psi^E \}= \frac{1}{2 \pi}\int_{0}^{2 \pi} \psi^E(\bar r_s, \vartheta)\ee^{- \ii n \vartheta} d \vartheta = \sum_{j= |n|/2}^\infty \left (\frac{r_S}{2}\right)^{ 2 j} \sum_{m= 0}^{2 j} \frac{\partial^{ 2 j} \psi^E_0}{\partial_{x}^m \partial_{y}^{{ 2 j}-m}}  \frac{\ii^{ m-2 j} C^m_{n,  j }  }{m! (2 j -m)!} ,
%Formula checked from scratch in Mathematica for the C's just below!
\label{eqn:PsiTaylor}
\en
where
\be
C^m_{n,j} = \sum_{m_1= \text{max } \{0,  m -j -n/2 \}}^{\text{ min } \{m, j -n/2\}} (-1)^{-n/2 +j- m_1} \binom{m}{m_1} \binom{ 2 j -m}{ j  -n/2- m_1},
\en
%where as $p_1$ was substituted for $(|n|- n)/2 +j- m_1$, then from $0\leq p_1 \leq p-m $ we conclude that $m_1 \leq  (|n|- n)/2 +j $ and $-(|n|+ n)/2 -j +m \leq m_1$, which combined with the restriction $0 \leq m_1 \leq m$ implies that $m_1^\text{min} = \text{max } \{0, -(|n|+ n)/2 -j +m\}$ and $m_1^\text{max } = \text{ min } \{m, (|n|- n)/2 +j\}$.
where $j$ (same as all the dummy indexes) increases in increments of 1, and as $p_1$ was substituted for $j -n/2- m_1$, then from $0\leq p_1 \leq p-m $ we conclude that $m_1 \leq  j -n/2 $ and $-j -n/2 +m \leq m_1$, which combined with the restriction $0 \leq m_1 \leq m$ implies that $ \text{max } \{0,  m -j -n/2 \}\leq m_1 \leq \text{ min } \{m, j -n/2\}$.
%For only $j= |n|/2$ the above can be reduced to
%\bga
%p_n= \frac{1}{2 \pi}\int_{0}^{2 \pi} \psi^E(\bar r_s, \vartheta)\ee^{- \ii n \vartheta} d \vartheta =   \sum_{m= 0}^{|n|} \frac{\partial^{|n|} \psi^E_0}{\partial_{\bar x}^m \partial_{\bar y}^{{|n|}-m}}   \frac{(\ii\sign n)^{m-{|n|}}  }{m! ({|n|}-m)!}\frac{\bar r_S^{|n|}}{2^{|n|}} + \mathcal O(\bar r_S^{|n|+2}),
%%\\
%%dp_n= \frac{1}{2 \pi}\int_{0}^{2 \pi} \partial_{\bar r}\psi^E(\bar r_s, \vartheta)\ee^{- \ii n \vartheta} d \vartheta =   \sum_{m= 0}^n \frac{\partial^n \psi^E_0}{\partial_{\bar x}^m \partial_{\bar y}^{n-m}}   \frac{\ii^{m-n}  }{m! (n-m)!}\frac{\bar r_S^n}{2 ^n},
%\ega

For the Neumann boundary condition we need
\be
\frac{1}{2 \pi}\int_{0}^{2 \pi} \partial_{\bar r}\psi^E(\bar r_s, \vartheta)\ee^{- \ii n \vartheta} d \vartheta = k^{-1} \partial_{r_S}\mathcal P_n\{\psi^E \}.
\label{eqn:DPsiTaylor}
\en

The terms~\eqref{eqn:PsiTaylor} suggest that the series of the scattering operators converge absolutely if $r_S \leq 2$, assuming the derivatives of $\psi^E$
%, $H_n^{(1)}(\bar r)/ H_n^{(1)}(\bar r_S)$ and $H_n^{(1)}(\bar r)/ \partial_{\bar r} H_n^{(1)}(\bar r_S)$
are uniformly bounded for every $n$ and $\bar r> \bar r_S$ and $k> \delta>0$ (as $w^E$ will often have a singularity at $k=0$). So for $r_S \leq 2$ we can truncate the series~\eqref{eqn:PsiTaylor} at $j = N_j/2$ for every $n$, which we denote by $\mathcal P_n^{N_j} := \mathcal P_n + \mathcal O(r_s^{N_j +2}) $.

Eq.~\eqref{eqn:PsiTaylor} together with Eq.~\eqref{eqn:aDirichlet} imply that the scattering operator becomes
\be
\T :  \psi^E( \bar r, \theta) \to \psi^S(\bar r, \theta) =  - \sum_{n = -\infty}^\infty\frac{H_n(\bar r)}{ H_n(\bar r_S)} \mathcal P_n\{ \psi^E \} \ee^{\ii n \theta} ,
\label{eqn:ScatteringOpDirichlet}
\en
which is indeed linear in $\psi^E$. Similarly for the Neumann boundary condition the scattering operator becomes
%\be
%\T :  \mathcal C_n(r) \ee^{\ii n \theta} \to  - \frac{ H_n(r)}{H_n(r_S)} \mathcal C_n(r_S) \ee^{\ii n \theta} \implies \T\{ \}
%\en
\be
\T : \psi^E(\bar r, \theta) \to \psi^S(\bar r, \theta) = - \sum_{n = -\infty}^\infty \frac{H_n(\bar r)}{\partial_{\bar r} H_n( \bar r_S)}  k^{-1} \partial_{r_S} \mathcal P_n\{ \psi^E  \} \ee^{\ii n \theta},
\label{eqn:ScatteringOpNeumann}
\en
which is also linear in $\psi^E$.

To examine the limit where the particles radius $r_S$ is small in comparison to the wavelength $k r_S \to 0$, we first note that
\be
\frac{H_n(\bar r)}{H_n( \bar r_S)} = H_n(\bar r)  \left\{
	\begin{array}{ll}
	 \frac{\ii \pi (k r_S)^n }{2^{n} (n-1)!} + \mathcal O( k r_S)^{n +2} ,  \quad \quad \quad \; \;\; n > 0, \\
	 \frac{\pi}{ \pi +2 \ii (\gamma_0 +\log(k r_S/2))} + \mathcal O( k r_S)^{2}, \quad  n =0,
	\end{array}
\right.
\label{eqn:DirchlettHankelExpansion}
\en
and
\be
\frac{H_n(\bar r)}{\partial_{\bar r_S} H_n( \bar r_S)} = H_n(\bar r)  \left\{
	\begin{array}{ll}
	 - \frac{\pi \ii  (k r_S)^{n +1} }{2^{n} n!}  + \mathcal O( k r_S)^{n +3} ,  \quad n > 0, \\
	 - \frac{\pi \ii }{2} k r_S  + \mathcal O( k r_S)^{3}, \quad \quad \quad \; \, n =0,
	\end{array}
\right.
\label{eqn:NeumannHankelExpansion}
\en
here $\gamma_0 = 0.5772$, the term $H_n(\bar r)$ has not been asymptotically expanded as $\bar r$ can be of any size, and note that for $n <0$ we have $H_n(\bar r_S) = (-1)^n H_{|n|}(\bar  r_S)$. So, for example, if we want to asymptotically expand the scattered wave $w^S$ up too $\mathcal O (\bar r_S^2)$ for the Dirchlett boundary condition, we need to expand
%$\mathcal P_{0} \{ \psi^E\}, \, \mathcal P_{-1} \{ \psi^E\}$ and $ \mathcal P_{1} \{ \psi^E\}$ up too $\mathcal O(\bar r_S^2)$, $\mathcal O(\bar r_S^1)$ and $\mathcal O(\bar r_S^1)$, respectively, for a Dirchlett boundary condition
\bal
\psi^S(\bar r, \theta) = & - \frac{H_1(\bar r)}{ H_1( \bar r_S)}   \mathcal P_{-1}\{ \psi^E  \} \ee^{- \ii \theta} - \frac{H_0(\bar r)}{ H_0( \bar r_S)}\mathcal P_0\{ \psi^E  \} - \frac{H_1(\bar r)}{ H_1( \bar r_S)}   \mathcal P_1\{ \psi^E  \} \ee^{\ii \theta}  + \mathcal O (\bar r_S^2)
\\
= & - H_0(k r) \frac{\pi  \psi^E_0 }{2 \ii \log (\bar r_S /2 )+\pi +2 \ii \gamma_0 } + \mathcal O (\bar r_S^2),
\eal
while to expand $w^S$ up too $\mathcal O (\bar r_S^3)$ for the Neumann boundary condition, we need
\bal
\psi^S(\bar r, \theta) = &- \frac{k^{-1} H_1(\bar r)}{\partial_{\bar r} H_1( \bar r_S)}  \partial_{r_S} \mathcal P_1\{ \psi^E  \} \ee^{\ii \theta} - \frac{k^{-1} H_0(\bar r)}{\partial_{\bar r} H_0( \bar r_S)}  \partial_{r_S} \mathcal P_{0}\{ \psi^E  \}
\notag \\
& - \frac{k^{-1} H_1(\bar r)}{\partial_{\bar r} H_1( \bar r_S)}  \partial_{r_S} \mathcal P_{-1}\{ \psi^E  \} \ee^{-\ii \theta} + \mathcal O (\bar r_S^3)
\notag \\
 = &  \frac{i \pi  r^{2}_S}{2} k( \partial_x \psi^E_{0} \cos \theta +  \partial_y \psi^E_{0} \sin \theta) H_1(k r )
 \notag \\
  & +\frac{ \ii \pi r_S^2}{4}  (\partial_x^2 \psi^E_{0} + \partial_y^2 \psi^E_{0}) H_0 (k r) +\mathcal O(\bar r_S^3).
\eal

If we are interested in lower frequencies for $k$, then we must be careful with the dependence that $\psi^E$ has on the wavenumber $k$. In general $\psi^E$ will be a sum of terms of the form
\be
 \psi^E_q=  H_q (k \|\vec r-\vec r_E\|) \ee^{\ii q \arctan(\vec r - \vec r_E) },
\label{eqn:PsiETerm}
\en
where $\arctan(x,y) = \arctan (y/x)$, $\vec r_E$ is a constant vector, $c_q$ is determined by boundary conditions and we assume they are uniformly bounded for every $k$. If we are to approximate $\mathcal P_n \{\psi^E_q\}$($k \partial_{r_S}\mathcal P_n \{\psi^E_q\}$) up too $\mathcal O ( r_s^{N_j +2})$($\mathcal O ( r_s^{N_j +1})$) by truncating at $j = N_j/2$,  then we should investigate the term left out $j= N_j/2 +1$. Using~\eqref{eqn:PsiETerm} and expanding the term $j= N_j/2+1$ in $\mathcal P_n\{\psi^E_q\}$  in a series of powers of $k$, the lowest order term will be
\be
\mathcal P_n\{\psi^E_q\} -\mathcal P_n^{N_j}\{\psi^E_q\}  = k^{-|q|} r_S^{N_j + 2 } \mathcal O(1) + \mathcal O(r_S^{N_j+2} k^{2 -|q|}),
\en
which is multiplied with Eq.~\eqref{eqn:DirchlettHankelExpansion}
%\be
%\frac{H_n(\bar r)}{H_n( \bar r_S)} =  \left(\frac{r_S}{r}\right)^{|n|}+ \mathcal O (r_S^{|n|}) \mathcal O (k^2) + \mathcal O (r_S^{|n|+1}) ,
%\en
to get the scattering operator.
For the Neumann boundary condition,
\be
k^{-1} \left (\partial_{r_S} \mathcal P_n\{\psi^E_q\} - \partial_{r_S} \mathcal P_n^{N_j}\{\psi^E_q\} \right)  = k^{-q-1} r_S^{N_j + 1 } \mathcal O(1) + \mathcal O(r_S^{N_j+1})\mathcal O( k^{1 - q}),
\en
which is multiplied with Eq.~\eqref{eqn:NeumannHankelExpansion}.
%\be
%\frac{H_n(\bar r)}{\partial_{\bar r} H_n( \bar r_S)} = -\frac{k r_S }{|n|} \left(\frac{r_S}{r}\right)^{|n|}+ \mathcal O (r_S^{|n|}) \mathcal O (k^3).
%\en

\begin{thebibliography}{9}

\bibitem{martin_2006}
  Martin, Paul A. Multiple scattering: interaction of time-harmonic waves with N obstacles. Vol. 107. Cambridge University Press, 2006.

\end{thebibliography}

\end{document}


\section{Question 1}

The data are shown on \autoref{q1_two_observations}.

\begin{figure}[H]
  \centering
  \includegraphics[width=0.9\textwidth]{figures/q1.pdf}
  \caption{The input data showing two types of observations with circles and triangles. Plotting code: \url{https://github.com/evgenyneu/ASP5020_data_analysis_problem_sets/blob/master/ps5/code/q1.py}.}
  \label{q1_two_observations}
\end{figure}

The diagram of the neural network is shown on \autoref{q1_network_diagram}. I choose the sigmoid activation function for the hidden layer nodes, because it's a common one in use. I will see how it work and switch to \emph{tanh} or \emph{ReLU} if needed. This is a classification task with just a single binary output (0 or 1). Therefore, for simplicity, I chose a single output node with no activation function (i.e. $f(x) = x$). If there were more than one output nodes than I could try a softmax function, but I will start simple and see how it goes.

\begin{figure}[H]
  \centering
  \includegraphics[width=0.7\textwidth]{figures/q1_neural_network.pdf}
  \caption{Diagram of the neural network containing two inputs ($x_1$ and $x_2$), single hidden layer with three nodes ($h_1$, $h_2$ and $h_3$) and a single output layer ($o_1$). The red circles with $1$ correspond to biases. Hidden layer nodes use sigmoid activation function. The labels $w1$ through $w13$ corredpond to the weight values, which are initialized by drawing random numbers from normal distribution $N(0, 1)$. Image source: \url{https://github.com/evgenyneu/ASP5020_data_analysis_problem_sets/blob/master/ps5/report/figures/q1_neural_network.sketch}}
  \label{q1_network_diagram}
\end{figure}

\subsection{Why I don't use sigmoid in the output layer?}

I do not use the sigmoid activation function in the output neuron because of the problem I found in the the Lecture 13 example code (\url{http://astrowizici.st/teaching/phs5000/13/}). I modified the code and replaced the sigmoid with $f(x) = x$ function. This was done because the output of the sigmoid is between 0 and 1, and it is used as predicted value from our model. However, in the loss function, we are comparing the predicted values with real values, which fall outside the $[0, 1]$ range. Therefore, the predicted value can never, even in theory, match the data. This mismatch is shown on \autoref{q1_lecture13_loss_compare}, where I compare the loss function of the original and the modified models. We can see that the modified model converges faster. The loss function for the original model will never approach zero, even if we increase the number of interations because of the range difference between the sigmoid and the data.

\begin{figure}[H]
  \centering
  \includegraphics[width=1\textwidth]{figures/lecture13_loss_compared.pdf}
  \caption{Loss function for neural network from Lecture 13. The original code (left) contains sigmoid activation function in the output node, while the modified code (right) has no activation function ($f(x) = x$). Both codes are otherwise identical, including the same seeding of random number generators. The loss function of the original code (left) converges more slowly and never approaches zero. This is caused by mismatch between the range $[0, 1]$ of the sigmoid function and the data values, which are normalized to zero mean and standard deviation of 1.}
  \label{q1_lecture13_loss_compare}
\end{figure}

The comparison of real and predicted values for the two codes are shown on \autoref{q1_lecture13_data_prediction_compare}. We can see that the original model can not be used for generating predicted values, since its range is limited.

\begin{figure}[H]
  \centering
  \includegraphics[width=1\textwidth]{figures/lecture13_results_compared.pdf}
  \caption{Comparison of model prediction (y axis) with data (x axis) for neural network code from Lecture 13. The original code (left) performs worse because it's output range is limited to $[0, 1]$ multiplied by standard deviation and added mean of the data.}
  \label{q1_lecture13_data_prediction_compare}
\end{figure}

Original code form the lecture notes: \\ \url{https://github.com/evgenyneu/ASP5020_data_analysis_problem_sets/blob/master/ps5/code/lecture13_original_with_signoid_output.py}

My modified code without the output sigmoid (changed lines 99, 140 and 153): \\ \url{https://github.com/evgenyneu/ASP5020_data_analysis_problem_sets/blob/master/ps5/code/lecture13_remove_sigmoid_from_output.py}

The code changes are shown on \autoref{q1_lecture13_removing_loss_function}.

\begin{figure}[H]
  \centering
  \includegraphics[width=0.8\textwidth]{figures/q1_lecture13_code_changes.png}
  \caption{Code changes that show removal of the sigmoid activation function from the output node.}
  \label{q1_lecture13_removing_loss_function}
\end{figure}

\subsection{Equations for the neuron inputs and outpus}

I will use $a$ and $b$ for input and output value of a neuron respectively, so $a_{h2}$, for example is the input value of the $h_2$ neuron, and $b_{h2}$ is it's output value. Then, for the hidden layer the inputs are
\begin{align}
  \label{eq_q1_hidden_neurons_inputs}
  \begin{split}
    a_{h1} &= w_1 + x_1 w_4 + x_2 w_7 \\
    a_{h2} &= w_2 + x_1 w_5 + x_2 w_8 \\
    a_{h3} &= w_3 + x_1 w_6 + x_2 w_9.
  \end{split}
\end{align}
The outputs are
\begin{align}
  \label{eq_q1_hidden_neurons_outputs}
  \begin{split}
    b_{h1} &= \text{sigmoid}(a_{h1}) \\
    b_{h2} &= \text{sigmoid}(a_{h2}) \\
    b_{h3} &= \text{sigmoid}(a_{h3}),
  \end{split}
\end{align}
where $\text{sigmoid}$ is my chosen activation function
\[
\text{sigmoid}(x) = \frac{1}{1 + e^{-x}}.
\]
The input and output of the output node are the same, since I chose not to use an activation function:
\begin{equation}
  a_{o1} = w_{10} + b_{h1} w_{11} + b_{h2} w_{12} + b_{h3} w_{13}.
  \label{eq_q1_output_value}
\end{equation}

\subsection{The loss function}

The loss function is
\begin{align}
  E = (y - a_{o1})^2,
  \label{eq_q1_loss_function}
\end{align}
where $y$ is the observed binary value (0 or 1). Note that I'm not using factor $0.5$ because it's not present in the default ``mean square error'' function from Pytorch or Tensorflow, and I'm trying to achieve the results close to what I get from those libraries.

\subsection{Derivatives of the loss function}

Next, I calculate the derivatives of the loss function with respect to all the weights. For the weight $w$ coming from the hidden layer, the derivatives are calculated using equation
\begin{align*}
  \frac{\partial E}{\partial w} = \frac{\partial E}{\partial a_o} \frac{\partial a_o} {\partial w}.
\end{align*}
Next, I substitute $E$ and $a_o$ from Equations \ref{eq_q1_output_value} and \ref{eq_q1_loss_function} and calculate the derivatives for all the hidden layer weights:
\begin{align*}
  \frac{\partial E}{\partial w_{10}} &= 2 (a_{o1} - y) \\
  \frac{\partial E}{\partial w_{11}} &= 2 (a_{o1} - y) b_{h1} \\
  \frac{\partial E}{\partial w_{12}} &= 2 (a_{o1} - y) b_{h2} \\
  \frac{\partial E}{\partial w_{13}} &= 2 (a_{o1} - y) b_{h3}.
\end{align*}
For a weight coming form the input layer, the derivative is calculated with equation
\begin{align*}
  \frac{\partial E}{\partial w} = \frac{\partial E}{\partial a_o} \frac{\partial a_o} {\partial b_h} \frac{\partial b_h} {\partial a_h} \frac{\partial a_h} {\partial w}.
\end{align*}
Here I use \autoref{eq_q1_hidden_neurons_outputs} for $b_h$ and \autoref{eq_q1_hidden_neurons_inputs} for $a_h$ and calculate the derivatives for all the input weights:
\begin{align*}
  \frac{\partial E}{\partial w_{1}} &= 2 (a_{o1} - y) w_{11} b_{h1} (1 - b_{h1}) \\
  \frac{\partial E}{\partial w_{2}} &= 2 (a_{o1} - y) w_{12} b_{h2} (1 - b_{h2}) \\
  \frac{\partial E}{\partial w_{3}} &= 2 (a_{o1} - y) w_{13} b_{h3} (1 - b_{h3}) \\
  \frac{\partial E}{\partial w_{4}} &= 2 (a_{o1} - y) w_{11} b_{h1} (1 - b_{h1}) x_1 \\
  \frac{\partial E}{\partial w_{5}} &= 2 (a_{o1} - y) w_{12} b_{h2} (1 - b_{h2}) x_1 \\
  \frac{\partial E}{\partial w_{6}} &= 2 (a_{o1} - y) w_{13} b_{h3} (1 - b_{h3}) x_1 \\
  \frac{\partial E}{\partial w_{7}} &= 2 (a_{o1} - y) w_{11} b_{h1} (1 - b_{h1}) x_2 \\
  \frac{\partial E}{\partial w_{8}} &= 2 (a_{o1} - y) w_{12} b_{h2} (1 - b_{h2}) x_2 \\
  \frac{\partial E}{\partial w_{9}} &= 2 (a_{o1} - y) w_{13} b_{h3} (1 - b_{h3}) x_2.
\end{align*}

\subsection{Updating the weights}

The equation for updating a weight is
\[
  w^{i+1} = w^{i} - e \sum_{j=1}^{N} \frac{\partial E(y_j)}{\partial w},
\]
where $w^{i+1}$ and $w^{i}$ are the weight values for the current and the previous iterations respectively, $e$ is a learning rate (a small number like $0.001$), $N$ is the number of observations and $y_j$ is the $j-th$ observation.


\clearpage
\begin{appendices}

\chapter{Principal Component Analysis} \label{app:pca}
Descriptors should be as discriminative as possible, whilst also being as compact as possible. This means that each descriptor should encode encode as much information allowing it to be differentiated from other images as possible, whilst also containing the minimum amount of data allowing it to do this. The descriptors investigated in the main body of the report focus on creating discriminative descriptors, but do not place as much importance on generating compact descriptors. It is possible that many of the descriptors contain information which is common to all images, and therefore acts as wasted data.

\Gls{pca} analyses these descriptors and, via construction of an eigenmodel, allows for them to be projected into a lower dimensional space. This discards the `wasted' data in the descriptors.

Figures \ref{fig:pca-grid-color-sheep} and \ref{fig:pca-grid-color-books} show the results of projecting the descriptors into a lower dimensional space.  The $E$ value refers to the proportion of total eigenvalue energy to preserve. Eigenvalue energy is equal to the sum of all of the Eigenvalues which make up the descriptor. Therefore, for $E=0.5$, the sum of the eigenvalues of the dimensions which the data is projected into, is greater than or equal to half of the total sum of the eigenvalues.

\begin{figure}[ht]
	\begin{minipage}[]{0.3\linewidth}
		\centering
		\includegraphics[width = 0.9\linewidth]{figs/msrc/9_23_s}
		\subcaption{Query image}
	\end{minipage}
	\begin{minipage}[]{0.7\linewidth}
		\centering
		\begin{tikzpicture}
		\prplot{data/pca/grid_color_hist/9_23_s/pr_1.txt}{E=0.1}
		\prplotadd{data/pca/grid_color_hist/9_23_s/pr_3.txt}{E=0.3}
		\prplotadd{data/pca/grid_color_hist/9_23_s/pr_5.txt}{E=0.5}
		\prplotadd{data/pca/grid_color_hist/9_23_s/pr_7.txt}{E=0.7}
		\prplotadd{data/pca/grid_color_hist/9_23_s/pr_9.txt}{E=0.9}
		\prplotadd{data/pca/grid_color_hist/9_23_s/pr_9.9.txt}{E=0.99}
		\prplotadd{data/pca/grid_color_hist/9_23_s/pr_11.txt}{No PCA}
		\prplotadd{data/gridding/color_hist/9_23_s/pr_2.txt}{No EM}
		\prplotclose
		\end{tikzpicture}
		\subcaption{Resultant graph}
	\end{minipage}
	\caption{Gridded RGB histogram, using PCA for query image 9\_23\_s}
	\label{fig:pca-grid-color-sheep}
\end{figure}

\begin{figure}[ht]
	\begin{minipage}[]{0.3\linewidth}
		\centering
		\includegraphics[width = 0.9\linewidth]{figs/msrc/13_1_s}
		\subcaption{Query image}
	\end{minipage}
	\begin{minipage}[]{0.7\linewidth}
		\centering
		\begin{tikzpicture}
		\prplot{data/pca/grid_color_hist/13_1_s/pr_1.txt}{E=0.1}
		\prplotadd{data/pca/grid_color_hist/13_1_s/pr_3.txt}{E=0.3}
		\prplotadd{data/pca/grid_color_hist/13_1_s/pr_5.txt}{E=0.5}
		\prplotadd{data/pca/grid_color_hist/13_1_s/pr_7.txt}{E=0.7}
		\prplotadd{data/pca/grid_color_hist/13_1_s/pr_9.txt}{E=0.9}
		\prplotadd{data/pca/grid_color_hist/13_1_s/pr_9.9.txt}{E=0.99}
		\prplotadd{data/pca/grid_color_hist/13_1_s/pr_11.txt}{No PCA}
		\prplotadd{data/gridding/color_hist/13_1_s/pr_2.txt}{No EM}
		\prplotclose
		\end{tikzpicture}
		\subcaption{Resultant graph}
	\end{minipage}
	\caption{Gridded RGB histogram, using PCA for query image13\_1\_s}
	\label{fig:pca-grid-color-books}
\end{figure}

Both Figures \ref{fig:pca-grid-color-sheep} and \ref{fig:pca-grid-color-books} show the expected result that, the higher the value of $E$, the better the performance of the distance measure. The `No \glsentryshort{pca}' plot shows the result of generating the eigenmodel and projecting the descriptors into this space, but without reducing the dimensionality of the descriptor. This plot is identical to the `No EM' plot, which is the results obtained without projecting the descriptors into the eigenmodel space. This is the expected result since, if all dimensions are maintained, the data has not been changed, and the Euclidean distance between descriptors in the two spaces should be identical.

Table \ref{tbl:pca-dim-red} shows the number of dimensions in the resultant descriptors when performing \gls{pca} on a gridded global colour histogram at different energy values. The table shows that even for $E=0.99$, i.e. 99 \% of the total eigenvalue engery, the descriptor dimensions can be reduced from 384 dimensions to 104. This shows that there are many dimensions with almost no variation in the original descriptor space which can be removed without reducing performance by a large amount. This is demonstrated in the test data, where the plot for $E=0.99$ is almost identical to the plot for `No \glsentryshort{pca}'. This reduces the size of the descriptor to only 26\% of it's size without \gls{pca}. Reducing the value of $E$ even further to $0.9$ means that the descriptor is only 8.9\% of its original size, whilst still maintaining good performance.

\begin{table}[ht]
	\rowcolors{2}{gray!25}{white}
	\caption{\glsentryshort{pca} Dimension reduction for Gridded RGB Histogram}
	\label{tbl:pca-dim-red}
	\centering
	\pgfplotstabletypeset[
	col sep=comma,
	string type,
	columns/e/.style={column name={\textbf{Energy}}, column type={r}},
	columns/dims/.style={column name={\textbf{\shortstack{Descriptor\\dimensions}}}, column type={r}},
	columns/percent/.style={column name={\textbf{\shortstack{Proportion of\\total dimensions}}}, column type={r}},
	every head row/.style={before row=\toprule, after row=\midrule},
	every last row/.style={after row=\bottomrule},
	]{data/pca/grid_color_hist/dim_report.txt}
\end{table}

\chapter{Distance Measures} \label{app:dist-measures}

The results presented in the body of this report use the L2 Norm, otherwise known as Euclidean distance, to measure the distance between descriptors. However despite this being the most common choice for distance measure, other distance measures do exist, these will be compared in this Appendix.

The distance measured investigated are:
\begin{enumerate}
	\item $L_1$ norm (Manhattan distance)
	\item $L_2$ norm (Euclidean distance)
	\item $L_2$ norm squared
	\item $L_\infty $ norm (Chebyshev distance)
	\item Mahalanobis distance
\end{enumerate}

A vector norm, $|\mathbf{x}|$ refers to a scaler property that, in perhaps an abstract way, describes the length of a vector, $\mathbf{x}$. Mathematically this is defined as shown in Equation \ref{eq:norm-def} \cite[p. 1081]{gradshteyn2007}.
\begin{align}
|\mathbf{x}| > 0 \quad &\text{when } \mathbf{x} \neq \mathbf{0} \nonumber \\
|\mathbf{x}| = 0 \quad &\text{when } \mathbf{x} = \mathbf{0} \nonumber \\
|k\mathbf{x}| = |k||\mathbf{x}| \quad &\text{for any scalar } k \nonumber \\
|\mathbf{x}+\mathbf{y}| \geq |\mathbf{x}|+|\mathbf{y}| \quad & \label{eq:norm-def}
\end{align}

The $L_1$, $L_2$ and $L_3$ norms describe this distance in different ways. The $L_2$ norm is the most common norm because it describes the point to point distance in Euclidean geometry. It is defined in Equation \ref{eq:L2-norm-def} \cite[p.1081]{gradshteyn2007}.

\begin{equation}
|\mathbf{x}|_2 = \sqrt{\sum_{r=1}^{n} |x_r|^2} \quad \text{where } \mathbf{x} = [ x_1 , x_2, ... , x_n] \label{eq:L2-norm-def}
\end{equation}

The $L_1$ norm describes distance in a slightly different way. It is the sum of the magnitudes of the vector components. This is the reason it is given the name `Manhattan' or `city block' distance, because in a two dimensional vector space, the $L_2$ norm would be the hypotenuse of a right angle triangle joining two nodes, whilst the $L_1$ norm would be the sum of the other two sides, analogous to walking along city blocks to go between two points. It is defined in Equation \ref{eq:L1-norm-def} \cite[p.1081]{gradshteyn2007}.

\begin{equation}
|\mathbf{x}|_1 = \sum_{r=1}^{n} |x_r| \quad \text{where } \mathbf{x} = [ x_1 , x_2, ... , x_n] \label{eq:L1-norm-def}
\end{equation}

The $L_\infty $ norm describes distance as the magnitude of the largest component of the vector. This therefore ignores the magnitudes of all smaller components. It is defined in Equation \ref{eq:L-inf-norm-def} \cite[p.1081]{gradshteyn2007}.

\begin{equation}
|\mathbf{x}|_\infty = \max_{i} |x_i| \quad \text{where } \mathbf{x} = [ x_1 , x_2, ... , x_n] \label{eq:L-inf-norm-def}
\end{equation}

These norms can be summarised in one equation which can define the $L_p$ norm for any $p$. However using the $L_p$ norm for values other than $p=1,2,\infty$ is not common.
\begin{equation}
|\mathbf{x}|_p = \sqrt[p]{\sum_{r=1}^{n} |x_r|^p} \quad \text{where } \mathbf{x} = [ x_1 , x_2, ... , x_n] \label{eq:Lp-norm-def}
\end{equation}

The only other distance measures under consideration are the $L_2$ norm squared, and the Mahalanobis distance. The $L_2$ norm squared is equal to the $L_2$ norm definition, but only the sum of the squared components is considered, this sum is not square rooted. This measure therefore places an increasing weight on larger components, as they will contribute a larger amount to the sum compared to their contribution to the square root of the number.

The Mahalanobis distance is slightly different to the other distance measures presented. Instead of considering distance as a function of the differences between different vector dimensions in a standard Euclidean space, the Mahalanobis distance instead first calculates the spread of the data in each dimension, and then measures difference in terms of how many standard deviations each linear difference represents. This can provide a normalising factor to the difference function because a difference in a dimension with a large spread of data will represent a much smaller contribution than the same linear difference in a dimension with a small spread. This effect can be thought of as the distance function fitting itself to the shape of the data. The Mahalanobis distance is implemented in the system by weighting the Euclidean distance with the eigenvalue for that dimension. This is shown mathematically in Equation \ref{eq:mahan-def}, where $v_r$ represents the eigenvalue for each dimension.

\begin{equation}
|\mathbf{x}|_2 = \sqrt{\sum_{r=1}^{n} \frac{|x_r|^2}{v_r} } \quad \text{where } \mathbf{x} = [ x_1 , x_2, ... , x_n] \label{eq:mahan-def}
\end{equation}

\begin{figure}[ht]
	\begin{minipage}[]{0.3\linewidth}
		\centering
		\includegraphics[width = 0.9\linewidth]{figs/msrc/9_23_s}
		\subcaption{Query image}
	\end{minipage}
	\begin{minipage}[]{0.7\linewidth}
		\centering
		\begin{tikzpicture}
		\prplot{data/dist_measure/9_23_s/pr_L1.txt}{$L_1$ norm}
		\prplotadd{data/dist_measure/9_23_s/pr_L2.txt}{$L_2$ norm}
		\prplotadd{data/dist_measure/9_23_s/pr_L2_sq.txt}{$L_2$ norm squared}
		\prplotadd{data/dist_measure/9_23_s/pr_L_Inf.txt}{$L_\infty $ norm}
		\prplotadd{data/dist_measure/9_23_s/pr_Mahal.txt}{Mahalanobis distance}
		\prplotclose
		\end{tikzpicture}
		\subcaption{Resultant graph}
	\end{minipage}
	\caption{Gridded RGB histogram results for query image 9\_23\_s}
	\label{fig:dist-meas-sheep}
\end{figure}

\begin{figure}[ht]
	\begin{minipage}[]{0.3\linewidth}
		\centering
		\includegraphics[width = 0.9\linewidth]{figs/msrc/13_1_s}
		\subcaption{Query image}
	\end{minipage}
	\begin{minipage}[]{0.7\linewidth}
		\centering
		\begin{tikzpicture}
		\prplot{data/dist_measure/13_1_s/pr_L1.txt}{$L_1$ norm}
		\prplotadd{data/dist_measure/13_1_s/pr_L2.txt}{$L_2$ norm}
		\prplotadd{data/dist_measure/13_1_s/pr_L2_sq.txt}{$L_2$ norm squared}
		\prplotadd{data/dist_measure/13_1_s/pr_L_Inf.txt}{$L_\infty $ norm}
		\prplotadd{data/dist_measure/13_1_s/pr_Mahal.txt}{Mahalanobis distance}
		\prplotclose
		\end{tikzpicture}
		\subcaption{Resultant graph}
	\end{minipage}
	\caption{Gridded RGB histogram results for query image 13\_1\_s}
	\label{fig:dist-meas-books}
\end{figure}

Figures \ref{fig:dist-meas-sheep} and \ref{fig:dist-meas-books} show the results of applying different distance measures to two test images. In both cases the $L_2$ norm squared has performed exactly as well as the $L_2$ norm. This is an interesting result, it implies that, in the test data, no matrix dimension difference magnitude had a much larger magnitude than the other significant dimensions.

The results for the $L_1$ norm and $L_\infty$ norm showed much greater difference in the test using image 13\_1\_s than 9\_23\_s. It can be seen that whilst the $L_1$ and $L_2$ norm showed generally similar performance, the $L_\infty$ norm showed significantly poorer performance. This confirms that only considering the most significant vector dimension is not sufficient to accurately determine differences between the images. The $L_2$ norm also outperformed the $L_1$ norm in the average case for the books. The image of the sheep however showed similar performance between distance measures.

The Mahalanobis distance performed very poorly for image 13\_1\_s, which is a surprising result. This implies that it representing difference in terms of the standard deviations moved, rather than linear distance, does not translate well into the output of the descriptor.

It is worth noting that Figures \ref{fig:dist-meas-sheep} and \ref{fig:dist-meas-books} were performed on descriptors which had had an eigenmodel generated from them, and then had the descriptors projected into that space. This was performed in order to allow the Mahalanobis distance to be compared also. However one effect this had is that it artificially increased the performance of the $L_\infty$ norm. The $L_\infty$ norm only compares the largest dimension of difference between two points, therefore when the descriptors are projected into the eigenmodel space, the direction of largest variation is aligned with the first dimension of the descriptor. This gives the $L_\infty$ norm will often use this dimension of largest variation as its differentiator, leading to artificially higher results than if the descriptors were naturally aligned with the dimensions. This is illustrated in Figure{fig:dist-meas-nopca-books}.

\begin{figure}[ht]
	\begin{minipage}[]{0.3\linewidth}
		\centering
		\includegraphics[width = 0.9\linewidth]{figs/msrc/13_1_s}
		\subcaption{Query image}
	\end{minipage}
	\begin{minipage}[]{0.7\linewidth}
		\centering
		\begin{tikzpicture}
		\prplot{data/dist_measure/13_1_s/pr_L_Inf.txt}{$L_\infty $ norm}
		\prplotadd{data/dist_measure_no_pca/13_1_s/pr_L_Inf.txt}{$L_\infty $ norm (No EM)}
		\prplotclose
		\end{tikzpicture}
		\subcaption{Resultant graph}
	\end{minipage}
	\caption{Gridded RGB histogram results for query image 13\_1\_s}
	\label{fig:dist-meas-nopca-books}
\end{figure}

\chapter{Test Methodology} \label{app:test-methodology}
In order to test the system, a unit test has been written for all of the descriptor and distance measure functions, as well as several other key functions. Each of these tests complies with \gls{matlab}'s unit testing framework, so can be executed using the \texttt{runtests} command. A description of the tests for each function is detailed in Table \ref{tbl:list-of-tests}.

\begin{table}[h]
	\rowcolors{2}{gray!25}{white}
	\centering
	\caption{List of tests implemented}
	\label{tbl:list-of-tests}
	\begin{tabular}{ l p{0.6\linewidth} }
		\toprule
		\bfseries Function Tested & \bfseries Tests implemented \\
		\midrule
		\texttt{vs\_compute\_histogram} &
		\begin{minipage}[t]{\linewidth}
			\begin{itemize}[leftmargin=*]
				\item Two tests against manually calculated data sets.
			\end{itemize}
		\end{minipage} \\
%
		\texttt{vs\_compute\_rgb\_histogram} &
		\begin{minipage}[t]{\linewidth}
			\begin{itemize}[leftmargin=*]
				\item Four tests against test images with known colour distributions, against manually calcualated histograms.
			\end{itemize}
		\end{minipage} \\
%
		\texttt{vs\_construct\_eigenmodel} &
		\begin{minipage}[t]{\linewidth}
			\begin{itemize}[leftmargin=*]
				\item Test eigenmodel dimensions and against Eigen\_Build.
				\item Test projected descriptors against Eigen\_Project.
				\item Test \gls{pca} applied eigenmodel against Eigen\_Deflate.
				\item Test \gls{pca} applied projected descriptors against Eigen\_Project.
			\end{itemize}
		\end{minipage} \\
%
		\texttt{vs\_edge\_detect} &
		\begin{minipage}[t]{\linewidth}
			\begin{itemize}[leftmargin=*]
				\item Test operation against library function with three test images.
				\item Test operation against library function for a sample image.
			\end{itemize}
		\end{minipage} \\
%
		\texttt{vs\_grid} &
		\begin{minipage}[t]{\linewidth}
			\begin{itemize}[leftmargin=*]
				\item Test 1x1 grid output using computation function that returns entire image.
				\item Test 2x2 grid output using computation function that returns entire image.
			\end{itemize}
		\end{minipage} \\
%
		\texttt{vs\_L1\_norm} &
		\begin{minipage}[t]{\linewidth}
			\begin{itemize}[leftmargin=*]
				\item Test output of zero vector against itself.
				\item Test output of random vector against itself.
				\item Test two manually calculated results.
				\item Test against library function for random vectors.
			\end{itemize}
		\end{minipage} \\
%
		\texttt{vs\_L\_Inf\_norm} &
		\begin{minipage}[t]{\linewidth}
			\begin{itemize}[leftmargin=*]
				\item Test output of zero vector against itself.
				\item Test output of random vector against itself.
				\item Test two manually calculated results.
				\item Test against library function for random vectors.
			\end{itemize}
		\end{minipage} \\
%
		\texttt{vs\_L\_2\_norm} &
		\begin{minipage}[t]{\linewidth}
			\begin{itemize}[leftmargin=*]
				\item Test output of zero vector against itself.
				\item Test output of random vector against itself.
				\item Test two manually calculated results.
				\item Test against library function for random vectors.
			\end{itemize}
		\end{minipage} \\
%
		\texttt{vs\_L\_2\_norm\_squared} &
		\begin{minipage}[t]{\linewidth}
			\begin{itemize}[leftmargin=*]
				\item Test output of zero vector against itself.
				\item Test output of random vector against itself.
				\item Test two manually calculated results.
				\item Test against library function for random vectors.
			\end{itemize}
		\end{minipage} \\
%
		\texttt{vs\_mahalanobis\_distance} &
		\begin{minipage}[t]{\linewidth}
			\begin{itemize}[leftmargin=*]
				\item Test output of zero vector against itself.
				\item Test output of random vector against itself.
				\item Test three manually calculated results.
			\end{itemize}
		\end{minipage} \\
		\bottomrule
	\end{tabular}
\end{table}

\section{Test pattern generation} \label{sec:test-pattern-gen}
Some tests require test patterns in order to test their functionality correctly. For this purpose a suite of test pattern generation programs was written to create image which contain:
\begin{itemize}
	\item A uniform colour.
	\item A uniform pattern.
	\item A combination of the above.
	\item A grid of several of the above images.
\end{itemize}
These test patterns are used internally to the unit tests, as well as to perform system level testing.

A sample of the test images is shown in Figure \ref{fig:example-test-images}

\begin{figure}[ht]
	\begin{minipage}[]{0.5\linewidth}
		\centering
		\includegraphics[width = 0.6\linewidth]{figs/test_imgs/color_5}
		\subcaption{A uniform colour test image}
	\end{minipage}
	\begin{minipage}[]{0.5\linewidth}
		\centering
		\includegraphics[width = 0.6\linewidth]{figs/test_imgs/freq_1}
		\subcaption{A uniform pattern test image}
	\end{minipage}

	\begin{minipage}[]{0.5\linewidth}
		\centering
		\includegraphics[width = 0.6\linewidth]{figs/test_imgs/color_cat_1}
		\subcaption{A grid of uniform colour images}
	\end{minipage}
	\begin{minipage}[]{0.5\linewidth}
		\centering
		\includegraphics[width = 0.6\linewidth]{figs/test_imgs/color_freq_cat_4}
		\subcaption{A grid of transposed colour and pattern test images}
	\end{minipage}
	\caption{Example test images}
	\label{fig:example-test-images}
\end{figure}

\end{appendices}


\documentclass[]{subfiles}

\begin{document}
\section{Introduction}\label{sec:Introduction}

Consider binary classification of text documents.  Each document is represented by two random variables: independent feature vector~${\X \in \domainX}$ and dependent label~${\y \in \domainY}$. The document population is generated from an unknown, joint probability distribution such that

\begin{equation}
  \underbrace{\pDist(\X,\y)}_{\joint} = \pDist(\X) \posterior \text{.} \label{eq:TrainDist}
\end{equation}

\noindent
By Bayes' Rule, the marginal can be reformulated as

\begin{equation}
  \underbrace{\pDist(\X)}_{\marginal} = \underbrace{\pDist(\y = \pcls)}_{\prior} \underbrace{\pDist(\X \vert \y = \pcls)}_{\pcond} + \underbrace{\pDist(\y = \ncls)}_{1-\prior} \underbrace{\pDist(\X \vert \y = \ncls)}_{\ncond} \label{eq:Joint:Bayes}
\end{equation}

\noindent
where $\prior$~is the positive\-/class prior while $\pcond$~and $\ncond$~are the positive and negative class-conditional distributions respectively.

Supervised binary classification's training set,~$\train$, consists of samples generated from~$\joint$. By Eq.~\eqref{eq:Joint:Bayes}, $\train$~partitions as ${\ptrain \sqcup \ntrain}$ where ${\ptrain \sim \pcond}$ are the positive-valued examples and ${\ntrain \sim \ncond}$ are negative-valued.  That is why supervised learning is alternatively referred to as \textit{positive\-/negative}~(PN) learning.

Ideal supervised learning often does not apply in practice.  Labeling may be expensive or difficult resulting in few labeled examples but numerous unlabeled ones.  Additionally, the labeled set may not be representative of~$\joint$.  For example, labeled examples may only characterize a small subset of $\joint$'s support, including there being no labeled data at all for one class.

One of the most common types of training set bias is \textit{covariate shift}. Define $\joint$~\eqref{eq:TrainDist} and ${\joint'}$~\eqref{eq:TestDist} as the training and test joint distributions respectively.  Under covariate shit, the joint distributions share posterior,~${\posterior}$. Rather, the distributions only differ in their respective marginals, i.e.,~${\marginal}$ and~$\marginal'$.~\cite{Huang:2006}  This paper assumes covariate shift for only one class --~the negative one.

\begin{equation}\label{eq:TestDist}
  \joint'(\X,\y) = \underbrace{\pDist'(\X)}_{\marginal'} \posterior
\end{equation}

The \textit{empirical risk minimization}~(ERM) training framework assumes that a low training-set expected risk correlates to low inference error. Training set bias, such as those aforementioned, undermine that assumption and can lead to large, unpredictable test set error rates.

Previous work has taken different approaches to overcome negative covariate shift's effects on text classification.  Li\etal~\cite{Li:2010} ignored the biased-negative training data entirely and learned a classifier using only the positive and unlabeled sets -- a paradigm known as \textit{\underline{p}ositive\-/\underline{u}nlabeled}~(PU) learning.  Fei \&~Liu~\cite{Fei:2015} took an alternate approach by ignoring any unlabeled data and learned a traditional supervised classifier using just the positive and biased-negative training examples.  Hsieh\etal~\cite{Hsieh:2018} combined the \underline{p}ositive, \underline{u}nlabeled, and \underline{b}iased \underline{n}egative~(PUbN) training sets into a single risk estimator that fits into the ERM~framework.

The primary contribution of this work is an empirical comparison of the PN, PU, and~PUbN estimators on 20~newsgroups~\cite{20newsgroups} classification under negative covariate shift.  The remainder of this document is structured as follows.  Section~\ref{sec:RiskEstimators} describes the PN, PU, and~PUbN risk estimation functions. Section~\ref{sec:Architectures} describes the two neural network architectures used in our experiments. Section~\ref{sec:20newsgroups} provides a brief overview of the 20~newsgroups dataset including how it is used in this paper. Section~\ref{sec:ExperimentalResults} describes our experimental setup, baselines, and results.  We provide brief concluding comments in Section~\ref{sec:Conclusions}.
\end{document}


\chapter{Simulation Environment}
\section{Introduction}
Small-scale helicopters are highly nonlinear systems with complex coupling. Analyzing velocity fields around the rotor requires complicated experiments and numerical methods, which differ in each flight regime such as hover, stall, etc. \cite{doerffer2008numerical,crittenden2004combustion,patterson1990computational}. There are numerous studies on mathematical models of the helicopter dynamics and governing equations of the forces and moments applied to it \cite{padfield2008helicopter,marques2017advanced, seddon2011basic}. For this study, we have used the model already developed for the Evolution-EX helicopter in \cite{pourrezaei2014control}. Here we briefly discuss the model development of this helicopter.
\section{Governing equations}
A combination of four subsystems describes the Evolution-EX helicopter's (EEH) dynamics: the rigid-body dynamics of the fuselage, the main rotor, the tail rotor, and the empennage. Two frameworks are defined like other dynamic problems: the body (B) and the Inertia (I)  framework.
\subsection{States and control input}
The states regarding the UAV dynamics include the velocity vector $[3\times1]$:

\begin{equation}
	V=[u,v,w]^T
\end{equation}

In this equation, u denotes the velocity in the x direction, v represents the velocity in the y direction, and w is the velocity in the z direction.
and the angular velocity vector $[3\times1]$:

\begin{equation}
	\omega=[p,q,r]^T
\end{equation}

In this equation, p represents represent the angular velocity in the p direction, q denotes the angular velocity in the y direction, and r is the angular velocity in the z direction.
with respect to body coordinates {B} and the position vector $[3\times1]$:

\begin{equation}
	p=[x,y,z]^T
\end{equation}

and the Euler angles vector which is roll pitch and yaw $[3\times1]$:

\begin{equation}
	\Theta=[\phi,\theta,\psi]^T
\end{equation}

with respect to inertia framework {I} and the input vector $[4\times1]$:

\begin{equation}
	U=[\delta_{col},\delta_{lat},\delta_{lon},\delta_{ped}]^T
\end{equation}

the $\delta_{col}$ is the collective input which is responsible for increasing the angle of attack in all the angles of the blade plane, the lateral input $\delta_{lat}$ is responsible to increase the angle of attack for a lateral movement of the helicopter and $\delta_{lon}$ on the other hand is doing the same thing for a longitudinal movement. The $\delta_{ped}$ increases the angle of attack of the blades for the tail rotor in the same way that $\delta_{col}$ does for the main rotor blades.
In the next section the governing equations regarding the states are discussed

\subsection{State-space equations}

The Newton-Euler equations of motion of the helicopter fuselage are defined as:

\begin{equation}\label{eq1}
	\dot{V}=\frac{1}{m} F-\omega\times V
\end{equation}

\begin{equation}\label{eq2}
	\dot{\omega}=I^{-1}M-I^{-1}(\omega \times I \omega)
\end{equation}

\begin{equation}
	\dot{\Theta}=\Phi(\Theta)\omega
\end{equation}

\begin{equation}
	\dot{p}=R_{b}^I(\Theta)V
\end{equation}

F and M are defined as vector of external forces and moments respectively. Derivation of F and M are elaborated in \ref{force section} and \ref{Moment section} respectively. $R_b^I$ and $\Phi$ are linear and angular velocity transformation matrices given as follows:
\begin{gather}
	R_b^I
	=
	\begin{bmatrix}
		s(\theta)c(\psi) &
		-c(\phi)sin(\psi)+s(\phi)s(\theta)c(\psi)&
		s(\phi)s(\psi)+c(\phi)s(\theta)c(\psi) \\
		c(\theta)s(\psi) &
		c(\phi)c(\psi)+s(\phi)s(\theta)s(\psi)&
		-s(\phi)c(\psi)+c(\phi)s(\theta)s(\psi)\\
		-s(\theta)&
		s(\phi)c(\theta)&
		c(\phi)c(\theta)
	\end{bmatrix}
\end{gather}

\begin{gather}
	\Phi
	=
	\begin{bmatrix}
		1 &
		s(\phi)t(\theta)&
		c(\phi)t(\theta) \\
		0 &
		c(\phi)&
		-s(\phi)\\
		0&
		\frac{s(\phi)}{c(\theta)} &
		\frac{c(\phi)}{c(\theta)}
	\end{bmatrix}
\end{gather}

in which s, c and t stands for "sin", "cos" and "tan" respectively.
The I is the moment of inertia in which the off-diagonal terms are neglected:

\begin{equation}
	I=
	\begin{bmatrix}
		I_{xx} &
		0&
		0 \\
		0 &
		I_{yy}&
		0\\
		0&
		0&
		I_{zz}
	\end{bmatrix}
\end{equation}

The $I_{xx}$, $I_{yy}$ and $I_{zz}$ are the rolling, pitching and yawning moment of inertia.\\

In the following sections, equations regarding the derivation of forces and moments in \ref{eq1} and \ref{eq2} are introduced.
\subsection{Blade flapping}

The dynamics of main rotor and stabilizer bar of the EEH is modeled by \textit{hybrid model approach} \cite{mettler2002system}. In this approach $\dot{a}$ and $\dot{b}$ are  tip-path-plane (TPP) longitudinal and lateral flapping angles respectively and the coefficients of first harmonic approximation in the Fourier series form. The rotor flapping state equations are:

\begin{equation}
	\dot{a}=-q-\frac{a}{\tau_{f}}+\frac{1}{\tau_{f}}(K_u\mu_x+K_w\mu_z)+\frac{A_{lon}}{\tau_{f}} (\delta_{lon}+K_c c)+A_b\frac{b}{\tau_{f}}
\end{equation}

\begin{equation}
	\dot{b} = -p-\frac{b}{\tau_{f}}+\frac{1}{\tau_{f}}(K_v\mu_y)+\frac{B_{lat}}{\tau_{f}}(\delta_{lat}+K_d d)+B_a \frac{a}{\tau_{f}}
\end{equation}

$K_u$ is the flapping due to the forward velocity factor, the $K_v$ on the other hands denotes the flapping due to the side-way velocity factor. In the same way, $K_w$ is considered to be te flapping due to downward velocity factor. $K_c$ reflects the longitudinal flapping due to the stabilizer bar factor and $K_d$ depicts the lateral flapping due to the stabilizer bar factor. $A_{lon}$ denotes the longitudinal cyclic to flap gain at nominal rpm and $B_{lat}$ is the lateral cyclic to flap gain at nominal rpm. Last but not least, the $\tau_f$ is the main rotor flapping time-constant.\\
$\mu_x,\mu_y$ and $\mu_z $ are the non-dimensional airflow components defined as:

\begin{equation}
	\begin{aligned}
		\mu_x&=\frac{u-u_{wind}}{\Omega R_{mr}}\\
		\mu_y&=\frac{v-v_{wind}}{\Omega R_{mr}} \\
		\mu_z&= \frac{w-w_{wind}}{\Omega R_{mr}}\\
	\end{aligned}
\end{equation}

And the $K_{u}$, $K_v$ and $K_w$ are given by:

\begin{equation}
	K_{u}=2K_{\mu}(\frac{4}{3}\delta_{col}-\frac{Vi}{\omega R_{mr}})
\end{equation}

\begin{equation}
	K_v=-K_u
\end{equation}

\begin{equation}
	K_w=16K_{\mu} \mu_{mr}^2 \frac{sign(\mu_{mr})}{(1-\mu_{mr}^2/2)*(8sign(\mu_{mr})+CL_\alpha \sigma)}
\end{equation}

The stabilizer bar state equations c and d are TPP longitudinal and lateral flapping angles of the stabilizer bar given by:

\begin{equation}
	\dot{c}=-q-\frac{c}{\tau_s}+\frac{C_{lon}}{\tau_s}\delta_{lon}
\end{equation}

\begin{equation}
	\dot{d}=-p-\frac{d}{\tau_s}+\frac{D_{lat}}{\tau_s}\delta_{lat}
\end{equation}

\subsection{Force derivation} \label{force section}
 \tikzset{
	every label/.append style={font=\scriptsize},
	my edge labels/.style={font=\scriptsize},
	dominant/.append style={label=below:$dominant$},
}

The force is derived as follows:

\begin{equation}
	F=
	\begin{bmatrix}
		F_x \\
		F_y\\
		F_z\\
	\end{bmatrix}
	+R_b^I \begin{bmatrix}
		0 \\
		0\\
		mg\\
	\end{bmatrix}
\end{equation}

in which $F_x$, $F_y$ and $F_z$ are defined as:

\begin{equation}
	F_x=-a\ T_{mr}+F_{x,fus}
\end{equation}

\begin{equation}
	F_y=b\ T_{mr}+T_{tr}+F_{y,fus} + F_{y,vt}
\end{equation}

\begin{equation}
	F_z=-T_{mr}+F_{z,fus} + F_{z,ht}
\end{equation}

$T_{mr}$ is the main rotor thrust and $T_{tr}$ denotes the tail rotor thrust. $T_{mr}$ is given by:

\begin{equation}
	T_{mr} = \textbf{f}_{T_{mr}}+\textbf{b}_{T_{mr}}U;
	\label{T_mr}
\end{equation}

in which $\textbf{f}_{T_{mr}}$ is the autonomous term of the main rotor thrust:

\begin{equation}\label{f_t}
	\textbf{f}_{T_{mr}}= \frac{1}{4} \rho \pi R_{mr}^4\Omega^2\sigma_{mr}(C_{L_{0}}(\frac{2}{3}+\mu_x^2+\mu_y^2)+C_{L_{\alpha}}(\mu_z-\lambda_0))
\end{equation}

In which $R_{mr}$ is the main rotor radius and $\Omega$ represents the nominal main rotor speed. $\sigma_{mr}$ depicts the main rotor solidity factor and $C_{L0}$ and $C_{L\alpha}$ are the main rotor blade zero lift curve and blade lift curve slope respectively.\\
$\lambda_0$ is the inflow ratio expressed as:

\begin{equation}
	\lambda_0=\frac{V_i}{\Omega R_{mr}}
\end{equation}

The solidity factor derived by:

\begin{equation}
	\sigma_{mr}=\frac{Nc_{mr}}{\pi R_{mr}}
\end{equation}

and $b_{T_{mr}}$ in \ref{T_mr} is control input coefficient term given by:

\begin{equation}\label{b_T}
	\textbf{b}_{T_{mr}}= \frac{1}{4} \rho \pi R_{mr}^4 \Omega^2 \sigma_{mr} C_{L_{\alpha}}\begin{bmatrix}
		\mu_x^2+\mu_y^2+\frac{2}{3}&
		-\mu_y&
		\mu_x&
		0
	\end{bmatrix}
\end{equation}

Similarly, it is possible to derive the tail rotor thrust $T_{tr}$:

\begin{equation}
	T_{tr} = \textbf{f}_{T_{tr}}+\textbf{b}_{T_{tr}}U;
\end{equation}

In which $\textbf{f}_{T_{tr}}$ is the autonomous term of the tail rotor thrust:

\begin{equation}\label{f_t_tr}
	\textbf{f}_{T_{tr}}=- \frac{1}{4} \rho \pi R_{tr}^4 n_{tr}^2\Omega^2\sigma_{tr}C_{L\alpha_{tr}}v_{tail}
\end{equation}

And input coefficients $b_{T_{tr}}$ is given by:

\begin{equation}\label{b_Ttr}
	\textbf{b}_{T_{tr}}= -\frac{1}{4} \rho \pi R_{tr}^4 n_{tr}^2 \Omega^2 \sigma_{tr} C_{L\alpha_{tr}}\begin{bmatrix}
		0&
		0&
		0&
		u_{tail}^2+w_{tail}^2+\frac{2}{3}
	\end{bmatrix}
\end{equation}

The normalized velocities at tail rotor can be given as:

\begin{equation}
	\begin{aligned}
		u_{tail}&=\frac{u-u_{wind}}{\Omega_{tr} R_{tr} }\\
		v_{tail}&=\frac{v-v_{wind}-V_{i_{tr}}+x_{fus}r}{\Omega_{tr} R_{tr}} \\
		w_{tail}&= \frac{w-w_{wind}-K_{\lambda}V_i+x_{fus}q}{\Omega_{tr} R_{tr}}\\
	\end{aligned}
\end{equation}

The tail rotor nominal speed is given by:

\begin{equation}
			\Omega_{tr}=n_{tr}\Omega
\end{equation}

In which the $n_{tr}$ is gear ratio of tail rotor to main rotor. $F_{y,vt}$ is the vertical tail force derived by:

\begin{equation}\label{Yvt}
	F_{y,vt}=\frac{1}{2} \rho S_{vt} \Big( C_{L_\alpha}^{vt}V_{vt}(v-v_{wind})+v_{tail}^2 \Big)
\end{equation}

In the above equation, the $\rho$ is the air density. $S_{vt}$ denotes the vertical tail area,
And horizontal tail force $F_{z,ht}$ is:

\begin{equation} \label{Z_ht}
	F_{z,ht}=\frac{1}{2} S_{ht} \Big( C_{L_{\alpha}}^{ht} \mid u-u_{wind} \mid w_{ht} +w_{ht}^2 \Big)
\end{equation}

In equation \ref{Yvt} $V_{vt}$ and $v_{tail}$ are axial and normal velocities in vertical tale defined as:

\begin{equation}
	\begin{aligned}
		V_{vt}&=\sqrt{(u-u_{wind})^2+(w- w_{wind} +x_{vt}q-K_\lambda V_i)^2}\\
		v_{tail}&=v-v_{wind}+x_{vt}r-V_{i_{tr}} \\
	\end{aligned}
\end{equation}

Similarly in equation $w_{ht}$ is the horizontal tail velocity in the z direction \ref{Z_ht}:

\begin{equation}
	w_{ht}=w-w_{wind}-x_{ht}q-K_{\lambda}V_i
\end{equation}

The $K_{\lambda}$ is the main rotor downwash factor at fuselage.\\

$F_{z,fus} , F_{y,fus}$ and $F_{x,fus}$ are drag forces derived by:

\begin{equation}
	F_{x,fus}=-\frac{1}{2} \rho S_x^{fus} V_{fus} (u-u_{wind})
	\label{X_fus}
\end{equation}

\begin{equation}\label{Y_fus}
	F_{y,fus}=-\frac{1}{2} \rho S_y^{fus} V_{fus} (v-v_{wind})
\end{equation}

\begin{equation}\label{Z_fus}
	F_{z,fus}=-\frac{1}{2} \rho S_z^{fus} V_{fus} (w-w_{wind}+V_i)
\end{equation}

the dynamic pressure of the fuselage $V_{fus}$ in expression \ref{X_fus} is defined as:

\begin{equation}
	V_{fus}=\sqrt{(u-u_{wind})^2+(v-v_{wind})^2+(w-w_{wind}+V_i)^2}
\end{equation}

\subsection{Moment derivation} \label{Moment section}

Moment includes 3 terms roll, pitch and yaw:

\begin{equation}
	M =\begin{bmatrix}
		M_{roll}\\
		M_{pitch}\\
		M_{yaw}\\
	\end{bmatrix}
\end{equation}

The three terms in the above equation are given as:

\begin{equation}
	M_{roll} = (K_{\beta}-T_{mr}Z_{cg})b;%-Ttail*z_fus;
\end{equation}

\begin{equation}
	M_{pitch} = (K_\beta-T_{mr}Z_{cg})a;
\end{equation}

\begin{equation}
	M_{yaw} = Q_{mr}+T_{tr}x_{fus};
\end{equation}

$Z_{cg}$ is the Main rotor hub height from center of gravity and $x_{fus}$ is the tail rotor hub offset from center of gravity along x-axis.
Main rotor drag torque $Q_{mr}$ is derived by:
\begin{equation}
	Q_{mr} = \textbf{f}_{Q_{mr}}+\textbf{b}_{Q_{mr}}U;
\end{equation}
In which the main rotor drag torque terms are given as:
\begin{equation} \label{f_Q}
	\textbf{f}_{Q_{mr}}=\frac{1}{8} \rho \pi R_{mr}^5 \Omega^2 \sigma_{mr} C_{L_{\alpha}}  \bigg( \frac{C_{D0}}{C_{L_{\alpha}}} \Big( \mu_x^2+\mu_y^2+1 \Big)-2(\mu_{z}-\lambda_0)^2 \bigg)
\end{equation}
\begin{equation} \label{b_Q}
	\textbf{b}_{Q_{mr}}=\frac{1}{8} \rho \pi R_{mr}^5 \Omega^2 \sigma_{mr} C_{L_{\alpha}} (\lambda_0-\mu_z)  \begin{bmatrix}
		\frac{4}{3}
		-\mu_y&
		\mu_x &
		0
	\end{bmatrix}
\end{equation}

\subsection{Induced velocity}

As indicated in \cite{johnson2012helicopter} and \cite{leishman2006principles} the blade element analysis considers each blade
element as a two-dimensional airfoil. The aerodynamic behavior of neighboring
blade elements is independent of each other. An induced inflow velocity on each
blade element should be accounted for, which is a product of the rotor wake. Analytical
ways of calculating the induced velocity may be found using momentum theory,
vortex theory, or nonuniform inflow calculations \cite{johnson2012helicopter}.\\

In general, the inflow velocity calculation is a challenging task due to its non-uniformity across the blade span; mathematical simplifications should be applied to minimize the complexity of the analysis. Finally, after determining the velocity components
of the blade element, the aerodynamic forces acting on this element are calculated.
The complete dynamic behavior of the blade is obtained by integrating the applied
forces of the individual elements throughout the blade span. Here we use an experimental approach for induced velocity. \\

$V_i$ and $V_{i_{tr}}$ are induced velocity in main rotor and tail rotor respectively. $V_i$ is given by:

\begin{equation}
	V_i=\frac{v_a}{\sqrt{1+\bar{\mu}^2}}
\end{equation}

In which:

\begin{equation}
	V_h=\sqrt{mg/(2\rho \pi R_{mr}^2)}
\end{equation}

\begin{equation}
	\mu=\sqrt{\mu_x^2+\mu_y^2}
\end{equation}

\begin{equation}
	\bar{\mu}=\frac{\mu}{V_h/(\Omega R_{mr})}
\end{equation}

\begin{equation}
	V_a=-\frac{w-w_{wind}}{V_h}
\end{equation}

\begin{equation}
	v_a = \left\{
	\begin{array}{ll}
		-\frac{1}{2} V_a-\sqrt{\frac{V_a^2}{4}-1} & \mbox{if } \ V_a 	\leqslant -2 \\
		1-\frac{1}{2} V_a+\frac{25}{12} V_a^2 +\frac{7}{6} V_a^3 & \mbox{if } \ -2<V_a<0 \\
		-\frac{1}{2} V_a+\sqrt{\frac{V_a^2}{4}+1} & \mbox{if } \ V_a	\geqslant 0 \\
	\end{array}
	\right.
	\label{vamr}
\end{equation}

Similarly, $V_{i_{tr}}$ is:

\begin{equation}
	V_{i_{tr}}=\frac{v_{a_{tr}}}{\sqrt{1+\overline{\mu_{tr}}^2}}
\end{equation}

In which:

\begin{equation}
	V_{h_{tr}}=\sqrt{f_{F_{y,mr}}/(2\rho \pi R_{tr}^2 x_{fus} )}
\end{equation}

\begin{equation}
	\mu_{tr}=\sqrt{u_{tail}^2+w_{tail}^2}
\end{equation}

The $\overline{\mu}_{tr}$ is the advance ratio normalized by $V_{h_{tr}}/(\Omega R_{tr}$:

\begin{equation}
	\overline{\mu}_{tr}=\frac{\mu_{tr}}{V_{h_{tr}}/(\Omega R_{tr})}
\end{equation}

\begin{equation}
	V_{a_{tr}}=-\frac{v-v_{wind}+x_{fus}r}{V_h}
\end{equation}

\begin{equation}
	v_{a_{tr}} = \left\{
	\begin{array}{ll}
		-\frac{1}{2} V_{a_{tr}}-\sqrt{\frac{V_{a_{tr}}^2}{4}-1} & \mbox{if } \ V_{a_{tr}} 	\leqslant -2 \\
		1-\frac{1}{2} V_{a_{tr}}+\frac{25}{12} V_{a_{tr}}^2 +\frac{7}{6} V_{a_{tr}}^3 & \mbox{if } \ -2<V_{a_{tr}}<0 \\
		-\frac{1}{2} V_{a_{tr}}+\sqrt{\frac{V_{a_{tr}}^2}{4}+1} & \mbox{if } \ V_{a_{tr}}	\geqslant 0 \\
	\end{array}
	\right.
	\label{vatr}
\end{equation}

Instead of using equation \ref{vamr} and \ref{vatr}, we use the following approximate functions by regression as they provide a faster calculation time in simulations:

\begin{equation}
	v'_a = \frac{4.055}{{\left(1.28V_{a}+1.45\right)}^2+1.7}+0.066
\end{equation}

\begin{equation}
	v'_{a_{tr}} = \frac{4.055}{{\left(1.28V_{a_{tr}}+1.45\right)}^2+1.7}+0.066
\end{equation}

Figure \ref{fig_va} depicts the two functions $v_a$ and $v'_a$ based on $V_a$.

\begin{figure}
	\begin{center}
		{\includegraphics[scale=0.3]{va.eps}}

		\caption{Function approximation of $v_a$ by $v'_a$, $R^2=0.947$.}
		\label{fig_va}
	\end{center}
\end{figure}

The constant parameters in the above modeling is given in the table \ref{helicopter_parameters}.
\begin{longtable}{c|c|c||c|c|c}
	\caption {Constant parameters in the helicopter modeling}\label{helicopter_parameters}\\\toprule
	\endfirsthead
	\caption* {\textbf{Table \ref{helicopter_parameters} Continued:} }\\\toprule
	\endhead
	\endfoot
	\bottomrule
	\endlastfoot
	\textbf{Parameter} & \textbf{Value} & \textbf{Dimension} & \textbf{Parameter} & \textbf{Value} & \textbf{Dimension} \\ \hline \hline
	$\alpha_{1}$ & $53$ &  $[-]$ & $\alpha_{2}$ & $55$ &  $[-]$ \\  \hline
	$ \Omega$ & $115 $ &  $[rad/s]$ & $\rho $ & $1.107 $ &  $[kg/m^3]$ \\ \hline
	$ \tau_f$ & $0.04 $ &  $[s]$ & $\tau_s $ & $ 0.2$ &  $[s]$ \\ \hline
	$\tau_s $ & $ 0.2$ &  $[s]$ & $A_b $ & $-0.1 $ &  $ [-]$ \\ \hline
	$A_{lon} $ & $ 1$ &  $[-]$ & $B_a $ & $0.1 $ &  $ [-]$ \\ \hline
	$B_{lat} $ & $0.9875 $ &  $[-]$ &$C_{D0_{tr}} $ & $0.06 $ &  $[-]$  \\ \hline
	$C_{D0} $ & $0.01 $ &  $[-]$ & $C_{L0} $ & $0.008 $ &  $[-]$ \\ \hline
	$C_{L\alpha_{tr}} $ & $4.95 $ &  $[rad^{-1}]$ & $C_{L\alpha} $ & $5.49 $ &  $[rad^{-1}]$ \\ \hline
	$c_{mr} $ & $0.082 $ &  $[m]$ & $c_{tr} $ & $0.025 $ &  $[m]$ \\ \hline
	$I_{xx} $ & $ 0.3$ &  $[kg.m^2] $  & $I_{yy}$ & $1.6$ & $[kg.m^2]$ \\ \hline
	$I_{zz} $ & $2.0 $ &  $[kg.m^2]$ & $K_\lambda $ & $1 $ &  $[-] $ \\ \hline
	$K_s $ & $0.3 $ &  $[-] $  & $K_\beta $ & $ 255$ &  $[N.m]$ \\ \hline
	$K_{lat} $ & $0.98 $ &  $[-] $  & $K_{lon} $ & $1 $ &  $[-] $ \\ \hline
	$m $ & $11.5 $ &  $[kg] $  & $n_{tr} $ & $6 $ &  $[-] $ \\ \hline
	$R_{mr} $ & $0.95 $ &  $[m]$  & $R_{tr} $ & $0.15 $ &  $[m] $ \\ \hline
	$S_x^{fus} $ & $0.1 $ &  $[m^2] $  & $S_y^{fus} $ & $0.83 $ &  $[m^2] $ \\ \hline
	$S_z^{fus} $ & $0.51 $ &  $[m^2] $  & $x_{fus} $ & $-1.22 $ &  $[m] $ \\ \hline
	$z_{CG}$ & $-0.32 $ &  $[m] $  &  &  & \\
\end{longtable}

\section{Sliding mode control}
In order to compare the results obtained by the RL method to a traditional control method, we here provide the details of a sliding mode controller as a nonlinear method. As it is an under actuated system which means that there are only have 4 inputs and 6 states to control. Based on \cite{slotine1991applied,pourrezaei2014control} in order to use sliding mode controller on a helicopter, first we have to change it to a square and affine in control form  so  we discuss how this is achieved.

\subsection{Force derivation in control affine form} \label{force section_affine}
In order to provide a control input by sliding mode controller, each of the force and moment should be linearized based on the control input. Control-affine form of each term is given as a input coefficient "b" and a "f" term which is related to autonomous response of the system. Force is given as:

\begin{equation}\label{Force eq}
	F=\textbf{f}_F+\textbf{b}_FU
\end{equation}

$f_F$ is the autonomous term of the force expressed as:
\begin{gather}\label{F_0}
	\textbf{f}_F
	=
	\begin{bmatrix}
		\textbf{f}_{F_{x,mr}}+F_{x,fus} \\
		\textbf{f}_{F_{y,mr}}+\textbf{f}_{F_{y,tr}}+F_{y,vt}+F_{y,fus}\\
		\textbf{f}_{F_{z,mr}}+F_{z,ht}+F_{z,fus} \\
	\end{bmatrix}
	+R_b^{I^{-1}}\begin{bmatrix}
		0 \\
		0\\
		mg \\
	\end{bmatrix}
\end{gather}

in which:

\begin{equation}\label{f_X_MR}
	\textbf{f}_{F_{x,mr}}=\textbf{f}_T(\tau_f q-a_v)
\end{equation}

\begin{equation}
	F_{x,fus}=-\frac{1}{2} \rho S_x^{fus} V_{fus} (u-u_{wind})
	\label{X_fus}
\end{equation}

\begin{gather}\label{f_Y_MR}
	\textbf{f}_{F_{y,mr}}=(\tau_fp+b_v)\textbf{f}_T
\end{gather}

\begin{equation}
	\textbf{f}_{F_{y,tr}}=\textbf{f}_{T_{tr}}
\end{equation}

\begin{equation}
	\textbf{f}_{F_{z,mr}}=-\textbf{f}_T
\end{equation}

$a_v$ and $b_v$ in \ref{f_X_MR} and \ref{f_Y_MR} are respectively longitudinal and lateral translational velocity contributions to the flapping of the main rotor  and defined by the following terms:

\begin{equation}
	\begin{aligned}
		a_v&=\frac{\partial a_1}{\partial \mu_x} \mu_x+\frac{\partial a_1}{\partial \mu_z} \mu_z \\
		b_v&=\frac{\partial a_1}{\partial \mu_y}\mu_y
	\end{aligned}
\end{equation}

the dynamic pressure of the fuselage in expression \ref{X_fus} is defined as:

\begin{equation}
	V_{fus}=\sqrt{(u-u_{wind})^2+(v-v_{wind})^2+(w-w_{wind}+V_i)^2}
\end{equation}

$\textbf{f}_{T_{mr}}$ is the main rotor thrust autonomous term in the control affine form given by \ref{f_t}.
and $\lambda_0$ is the inflow ratio expressed as:

\begin{equation}
	\lambda_0=\frac{V_i}{\Omega R_{mr}}
\end{equation}

$\sigma_{mr}$ is the solidity factor derived by:

\begin{equation}
	\sigma_{mr}=\frac{Nc_{mr}}{\pi R_{mr}}
\end{equation}

The velocities at tail rotor can be normalized given as:

\begin{equation}
	\begin{aligned}
		u_{tail}&=\frac{u-u_{wind}}{\Omega_{tr} R_{tr} }\\
		v_{tail}&=\frac{v-v_{wind}-V_{i_{tr}}+x_{fus}r}{\Omega_{tr} R_{tr}} \\
		w_{tail}&= \frac{w-w_{wind}-K_{\lambda}V_i+x_{fus}q}{\Omega_{tr} R_{tr}}\\
		\Omega_{tr}&=n_{tr}\Omega
	\end{aligned}
\end{equation}

similarly velocities at vertical tail or horizontal tail can be defined.\\
In equation \ref{Yvt} $V_vt$ and $v_{tail}$ are axial and normal velocities in vertical tale defined as:

\begin{equation}
	\begin{aligned}
		V_{vt}&=\sqrt{(u-u_{wind})^2+(w- w_{wind} +x_{vt}q-K_\lambda V_i)^2}\\
		v_{tail}&=v-v_{wind}+x_{vt}r-V_{i_{tr}} \\
	\end{aligned}
\end{equation}

Similarly in equation \ref{Z_ht}:

\begin{equation}
	w_{ht}=w-w_{wind}-x_{ht}q-K_{\lambda}V_i
\end{equation}

$b_F$ in equation \ref{Force eq} is the input coefficient of the force which is defined as:

\begin{gather}\label{F_U}
	\textbf{b}_F
	=
	\begin{bmatrix}
		\textbf{b}_{F_{x,mr}}\\
		\textbf{b}_{F_{y,mr}}+\textbf{b}_{F_{y,tr}}\\
		\textbf{b}_{F_{z,mr}}\\
	\end{bmatrix}
\end{gather}

In which the following terms are used:

\begin{equation}
	\textbf{b}_{F_{x,mr}}=(\tau_f q-a_v)\textbf{b}_T-\begin{bmatrix}
		0&0&K_{lon}\textbf{f}_T&0
	\end{bmatrix}
\end{equation}

\begin{gather}
	\textbf{b}_{F_{y,mr}}=(-\tau_fp+b_v)\textbf{b}_T- \begin{bmatrix}
		0&K_{lat}\textbf{f}_T&0&0
	\end{bmatrix}
\end{gather}

\begin{equation}
	\textbf{b}_{F_{y,tr}}=\textbf{b}_{T_{tr}}
\end{equation}

\begin{equation}
	\textbf{b}_{ F_{z,mr}}=-\textbf{b}_T
\end{equation}

$b_T$ and $b_{T_{tr}}$ is the main rotor thrust input coefficient in the control affine form given as \ref{b_T} and \ref{b_Ttr}.

\subsection{Moment derivation in control affine form} \label{Moment section_affine}
Control affine form of moment is given as:

\begin{equation} \label{M}
	M=\textbf{f}_M+\textbf{b}_M U
\end{equation}

$\textbf{f}_M$ is calculated by:

\begin{gather}\label{M_0}
	\textbf{f}_M
	=
	\begin{bmatrix}
		\textbf{f}_{ M_{x,mr}}+\textbf{f}_{ M_{x,tr}}+ M_{x,vt} \\
		\textbf{f}_{ M_{y,mr}}+\textbf{f}_{ M_{y,tr}}+ M_{y,ht}\\
		\textbf{f}_{ M_{z,mr}}+\textbf{f}_{ M_{z,tr}}+ M_{z,vt} \\
	\end{bmatrix}
\end{gather}

in which:

\begin{equation}
	\textbf{f}_{ M_{x,mr}}=(-\tau_fp+b_v)(K_{\beta}-\textbf{f}_T*z_{cg})
\end{equation}

\begin{equation}
	\textbf{f}_{ M_{x,tr}}=-z_{fus}\textbf{f}_{T_{tr}}
\end{equation}

\begin{equation}
	M_{x,vt}=-F_{y,vt}z_{vt}
\end{equation}

\begin{equation}
	\textbf{f}_{ M_{y,mr}}=(-\tau_fq+a_v)(K_{\beta}-\textbf{f}_T z_{cg})
\end{equation}

\begin{equation}\label{f_M_TR}
	\textbf{f}_{ M_{y,tr}}=\textbf{f}_{Q_{tr}}
\end{equation}

\begin{equation}
	M_{y,ht}=- F_{z,ht}x_{ht}
\end{equation}

\begin{equation} \label{f_N_MR}
	\textbf{f}_{ M_{z,mr}}=\textbf{f}_Q
\end{equation}

\begin{equation}
	\textbf{f}_{ M_{z,tr}}=x_{fus}\textbf{f}_{T_{tr}}
\end{equation}

\begin{equation}
	M_{z,vt}=F_{y,vt}x_{vt}
\end{equation}

$\textbf{f}_{Q_{tr}}$ in equation \ref{f_M_TR} is the autonomus term of the tail rotor drag torque:

\begin{equation}
	\textbf{f}_{Q_{tr}}=\frac{1}{8} \rho \pi R_{tr}^5 n_{tr}^2 \Omega^2 \sigma_{tr} C_{L\alpha_{tr}}  \bigg( \frac{C_{D0_{tr}}}{C_{L\alpha_{tr}}} \Big( u_{tail}^2+w_{tail}^2+1 \Big)-2v_{tail}^2 \bigg)
\end{equation}

similarly $\textbf{f}_Q$ in \ref{f_N_MR} is derived from \ref{f_Q}.\\

$b_M$ in equation \ref{M} is:
\begin{gather}
	\textbf{b}_M=\begin{bmatrix}
		\textbf{b}_{ M_{x,mr}}+\textbf{b}_{ M_{x,tr}}\\
		\textbf{b}_{ M_{y,mr}}+\textbf{b}_{ M_{y,tr}}\\
		\textbf{b}_{ M_{z,mr}}+\textbf{b}_{ M_{z,tr}} \\
	\end{bmatrix}
\end{gather}

in which:

\begin{gather}
	\textbf{b}_{ M_{x,mr}}=z_{cg}(\tau_fp-b_v)b_T+ \begin{bmatrix}
		0&K_{lat}(K_{\beta}-\textbf{f}_{T_{mr}}z_{cg}&0&0
	\end{bmatrix}
\end{gather}

\begin{equation}
	\textbf{b}_{ M_{x,tr}}=-z_{fus}b_{T_{tr}}
\end{equation}

\begin{gather}
	\textbf{b}_{ M_{y,mr}}=z_{cg}(\tau_fq-a_v)b_T+ \begin{bmatrix}
		0&0&K_{lon}(K_{\beta}-\textbf{f}_{T_{mr}}z_{cg})&0
	\end{bmatrix}
\end{gather}

\begin{equation}\label{b_M_tr}
	\textbf{b}_{ M_{y,tr}}=\textbf{b}_{Q_{tr}}
\end{equation}

\begin{equation}\label{b_N_mr}
	\textbf{b}_{ M_{z,mr}}=\textbf{b}_Q
\end{equation}

\begin{equation}
	\textbf{b}_{ M_{z,tr}}=x_{fus} \textbf{b}_{T_{tr}}
\end{equation}

in equation \ref{b_M_tr} the $b_{Q_{tr}}$ is defined as:

\begin{equation}
	\textbf{b}_{Q_{tr}}=\frac{1}{8} \rho \pi R_{tr}^5 n_{tr}^2 \Omega^2 \sigma_{tr} C_{L\alpha_{tr}}  \begin{bmatrix}
		0&
		0&
		0 &
		v_{tail}
	\end{bmatrix}
\end{equation}

Similarly for expression \ref{b_N_mr} the $b_Q$ is derived from \ref{b_Q}. if we combine all the state space equations we have:

\begin{equation}\label{eq22}
	\underbrace{\left[
		\begin{array}{c}
			\dot{V} \\
			\hline
			\dot{\omega}
		\end{array}
		\right]
	}_{\dot{x} _{6\times 1}}
	=
	\underbrace{\left[
		\begin{array}{c}
			\frac{\textbf{f}_F}{m}-\omega \times V \\
			\hline
			\ I^{-1} (\textbf{f}_{M}-\omega \times I \omega)
		\end{array}
		\right]
	}_{f _{6\times 1}}
	+\underbrace{\left[
		\begin{array}{c}
			\frac{\textbf{f}_U}{m }\\
			\hline
			\ I^{-1} \textbf{b}_M
		\end{array}
		\right]
	}_{f _{6\times 4}} U
\end{equation}

\subsection{Control point state space equations}
 the control point is set to be a point other than CG. This point is a point in the negative direction of z axis in the body coordinates of the helicopter. So by controlling this new control point position and yaw of center of gravity it is possible to control the UAV by using the following set of equations:

\begin{equation}\label{eq222}
	\underbrace{\Biggl[\frac{\ddot{X}_{CP}}{\ddot{\psi}}\Biggr]}_{\ddot{Y}_{4\times 1}}
	=
	\underbrace{\left[
		\begin{array}{c}
			\textbf{f}_{1_{3\times1}} \\
			\hline
			\textbf{f}_{2_{1\times1}}
		\end{array}
		\right]
	}_{f _{6\times 1}}
	+\underbrace{\left[
		\begin{array}{c}
			\textbf{b}_{1_{3\times 4}}\\
			\hline
			\textbf{b}_{2_{1\times 4}}
		\end{array}
		\right]
	}_{b_{4\times 4}} U
\end{equation}

in which:

\begin{equation}
	\textbf{f}_1=R_b^I \big(\frac{\textbf{f}_F}{m} +DI^{-1}(\textbf{f}_M-\omega \times (I \omega))\big)
\end{equation}

\begin{equation}
	\textbf{f}_2 = \textbf{f}_s(I^{-1} f_M -I^{-1} \omega \times (I \omega))+\textbf{f}_qq+\textbf{f}_rr;
\end{equation}
In which:
\begin{equation}
	\textbf{f}_q = \dot{\phi} \cos \phi \sec \theta + \dot{\theta} \sin \phi \tan \theta \sec \theta
\end{equation}

\begin{equation}
	\textbf{f}_r = -\dot{\phi} \sin \phi \sec \theta + \dot{\theta} \cos \phi \tan \theta \sec \theta
\end{equation}

\begin{equation}
	\textbf{f}_s = [0 \quad \sec \theta \sin \phi \quad \sec\theta \cos \phi]
\end{equation}
The $\textbf{b}_1$ and $\textbf{b}_2$ are the control input coefficients in the control point state space equations given as:
\begin{equation}
	\textbf{b}_1=R_b^I \big(\frac{F_U}{m} +DI^{-1}\textbf{b}_M\big)
\end{equation}

\begin{equation}
	\textbf{b}_2= f_s(I^{-1} \textbf{b}_M);
\end{equation}

\subsection{Control point position}
In this part of the simulation the current position $X_{CP}$ and velocities $\dot{X}_{CP}$ of the control point system $Y$ is calculated using the current states of the UAV center of gravity $X_{CG}$:

\begin{equation}
	X_{CP}=X_{CG}+R_b^Id_B
\end{equation}

In which:

\begin{align}
	d_B=[0,0,-d] \\
	X_{CG}=[x,y,z]
\end{align}

d is the distance from the center of gravity to the control point which is 1 meter in this study. First order derivative of the control point position can be derived by:

\begin{equation}
	\dot{X}_{CP}=R_b^I(V+(\omega \times d_B))
\end{equation}

\begin{equation}
	\dot{\Theta}=\Phi(\Theta)\omega
\end{equation}

the yaw of the UAV which is the forth row of the Y is the third row of the angular velocity in inertia coordinates:

\begin{align}
	\dot{\Theta}=\Phi(\Theta)\omega
\end{align}

so we have:

\begin{align} \label{Y_sliding}
	\dot{Y}=[\frac{\dot{X}_{CP}}{\dot{\psi}}]_{4\times 1}\\
	Y=[\frac{X_{CP}}{\psi}]_{4\times 1}
\end{align}

\subsection{Implementation of sliding mode controller}

By having \ref{Y_sliding} it is now possible to use sliding mode controller on the helicopter:

\begin{equation}
	s_i=\dot{y}_i-\dot{y}_{d,i}+\lambda_i y_i-\lambda_i y_d
\end{equation}

The $\lambda_i$ are the convergence rates which is supposed to be strictly positive.

\begin{equation}\label{lambda}
	\lambda=
	\begin{bmatrix}
		1& 0 & 0 & 0\\
		0 &1 & 0 & 0\\
		0 & 0 & 2 & 0\\
		0 & 0 & 0 & 2
	\end{bmatrix}
\end{equation}

The objective is to control the \ref{Y_sliding} instead of the state space equations of \ref{eq22}:

\begin{equation}
	\dot{y}_r= \dot{y}_{d,i}-\lambda_i y_i+\lambda_i y_d
\end{equation}

The parameters for sliding mode controller are given next. The first one is the surface reach time given by the following equation:

\begin{equation} \label{eta}
	\eta =
	\begin{bmatrix}
		1&
		1&
		1&
		1
	\end{bmatrix}^T
\end{equation}

The $F_e$ is a vector used as a bound on F:

\begin{equation}\label{F_e}
	F_e =
	\begin{bmatrix}
		10&
		10&
		5&
		5
	\end{bmatrix}^T
\end{equation}

The $B_e$ is the bound on b matrix defined by:

\begin{equation}\label{B_e}
	B_e=
	\begin{bmatrix}
		0.5& 0 & 0 & 0\\
		0 &0.5 & 0 & 0\\
		0 & 0 & 0.5 & 0\\
		0 & 0 & 0 & 0.5
	\end{bmatrix}
\end{equation}

The error of the controlled states from the desired value is defined by the $\tilde{Y}$:

\begin{equation}
	\tilde{Y}=Y-Y_d
\end{equation}

And the derivative of the $\tilde{Y}$ is as follows:

\begin{equation}
	\dot{\tilde{Y}}=\dot{Y}-\dot{Y}_d
\end{equation}

The surface function in the sliding mode controller is formulated as:

\begin{equation}
	s_r=\dot{Y}_d-\lambda\tilde{Y}
\end{equation}

So the first order derivative of the $s_r$ can be determined by:

\begin{equation}
	\dot{s}_r=\ddot{Y}_d-\lambda \dot{\tilde{Y}}
\end{equation}

E stands for the identity matrix:

\begin{equation}
	E=
	\begin{bmatrix}
		1& 0 & 0 & 0\\
		0 &1 & 0 & 0\\
		0 & 0 & 1 & 0\\
		0 & 0 & 0 & 1
	\end{bmatrix}
\end{equation}

K is the sliding mode control gain matrix given as:

\begin{equation}
	K = (E- B_e)^{-1} (F_e + B_e \  |-f + \dot{s}_r| + \eta);
\end{equation}

The boundary layer thickness $b_s$ is implemented to remove the chattering problem of the sliding mode controller.

\begin{equation}
	b_s=\begin{bmatrix}
		0.8&
		0.8&
		1&
		1
	\end{bmatrix}^T
\end{equation}

So $\bar{K}$ would be the the sliding mode control gain matrix term without the issue of chattering:

\begin{equation}
	\bar{K}=
	\left\{
	\begin{array}{ll}
		\bar{K}_i = K_i s_i/b_{s_i} & \mbox{if } \ |s_i|<b_{s_i}\  for\  i=1,2,3,4 \\
		\bar{K}_i = K_i sign(s_i) & \mbox{if } |s_i| \geqslant b_{s_i} \  for\  i= 1,2,3,4\\
	\end{array}
	\right.
\end{equation}

As a result, the control input of the helicopter can be derived using the following equation:

\begin{equation}
	U=b^{-1} (-f+\dot{s}_r-\bar{K})
\end{equation}

\section{Environment setup}
%\begin{figure}
%	\centering
%	\begin{forest}
%		for tree={
%			circle,
%			draw,
%			minimum width=2.5em,
%			l sep+=1.5em,
%			s sep+=1em,
%			anchor=center,
%			edge path={
%				\noexpand\path[\forestoption{edge}](!u.parent anchor)--(.child anchor)[my edge labels]\forestoption{edge label};
%			},
%		},
%		[$F$
%		[$F_x$
%		[]
%		]
%		[$F_y$
%		]
%		[$F_z$
%		]
%		]
%	\end{forest}
%	\caption{SAC controller schematic}
%	\label{forest_F}
%\end{figure}

\tikzstyle{decision} = [diamond, draw,fill=gray!20,
text width=4.5em, text badly centered, node distance=3cm, inner sep=0pt]
\tikzstyle{block} = [rectangle, draw, fill=gray!20,
text width=5em, text centered, rounded corners, minimum height=4em]
\tikzstyle{line} = [draw, -latex']
\tikzstyle{cloud} = [draw, ellipse,fill=gray!20, node distance=3cm,
minimum height=2em]

\begin{figure}
	\centering
	\begin{tikzpicture}[node distance = 3cm, auto]
		% Place nodes
		\node [block] (init) {initialize model};
%		\node [cloud, left of=init] (expert) {expert};
%		\node [cloud, right of=init] (system) {system};
		\node [block, below of=init] (reset) {reset the environment};
		\node [block, below of=reset] (step) {step the environment};
		\node [input, left of=reset, node distance=4cm]  (terminala) {agent};
		\node [block, below of=step, node distance=4cm]  (agent1) {agent};
		\node [decision, below of=agent1, node distance=4cm] (decide) {is it a terminal state (d=True)?};
		\node [input, right of=step, node distance=4cm] (action) {agent};
		% Draw edges
		\path [line] (init) -- (reset);
		\path [line] (reset) -- (step);
		\path [line, dashed] (step) --  node [near end] {d, R \& S} (agent1);
		\path [line] (decide) -| node [near start] {yes} (terminala);
		\path [line] (terminala) |- (reset);
		\path [line] (action) --  node [near end] {A}(step);
		\draw [line] (decide)-| node [near start] {no} (action);
		\draw [line, dashed] (agent1)-- node [near end] {A} (decide);
%		\path [line,dashed] (expert) -- (init);
%		\path [line,dashed] (system) -- (init);
%		\path [line,dashed] (system) |- (step);
	\end{tikzpicture}

	\caption{Environment Flowchart}
	\label{environment_flowchart}
\end{figure}

%
%\begin{tikzpicture}[node distance = 3cm, auto]
%	% Place nodes
%	\node [block] (init) {initialize model};
%	%		\node [cloud, left of=init] (expert) {expert};
%	%		\node [cloud, right of=init] (system) {system};
%	\node [block, below of=init] (reset) {reset the environment};
%	\node [block, below of=reset] (evaluate) {step the environment};
%	\node [block, left of=evaluate, node distance=4cm]  (terminala) {agent};
%	\node [decision, below of=evaluate] (decide) {is it a terminal state};
%	\node [block, right of=evaluate, node distance=4cm] (action) {agent};
%	% Draw edges
%	\path [line] (init) -- (reset);
%	\path [line] (reset) -- (evaluate);
%	\path [line] (evaluate) -- (decide);
%	\path [line] (decide) -| node [near start] {yes} node [near end] {d=True, R \& S} (terminala);
%	\path [line] (terminala) |- (reset);
%	\path [line] (decide) -| node [near start] {no} node [near end] {d=False, R \& S }(action);
%	\path [line] (action) --  node [near end] {A}(evaluate);
%	%		\path [line,dashed] (expert) -- (init);
%	%		\path [line,dashed] (system) -- (init);
%	%		\path [line,dashed] (system) |- (evaluate);
%\end{tikzpicture}

Now that we have discussed the dynamic of the helicopter, it is possible to set up the environment suitable for an RL process, which is developed in OpenAI Gym \cite{brockman2016openai}. OpenAI gym is a software development kit for creating and comparing reinforcement learning algorithms. figure \ref{environment_flowchart} shows the flowchart of the environment and the dashed line means that the agent is a system outside of the environment. While trying to implement an RL algorithm in a Gym environment, for each episode, first a reset function is called, then the step function is called until a terminal state is reached. In the following sections, the critical points in each part of this environment are discussed. \\

\subsection{Environment Reset} \label{reset}

Each time the environment is restarted, the helicopter is randomly placed in a position where $x$, $y$, and z are uniformly distributed in $[-1,0,1]$ so there would be 27 initial states. Other states are kept constant in this phase at hover state.

\subsection{Step}
In each step of the episode, first, the control input is generated from the actions, then the RK45 method is used for solving the set of ODEs. In addition, the reward and the condition of reaching a final state are considered. They are elaborated in the upcoming sections.

\subsubsection{Actions}
Instead of having the 4 actions as output of the agent, 16 actions are generated by the agent in each step and the control input of the helicopter is find through the following set of equations:

\begin{equation}
	\delta_{col} = a_1 z + a_2 w
\end{equation}

\begin{equation}
	\delta_{lat} = a_3 y + a_4 v + a_5 p + a_6 \phi
\end{equation}

\begin{equation}
	\delta_{lon} = a_7 x + a_{8} v + a_{9} q + a_{10} \theta
\end{equation}

\begin{equation}
	\delta_{ped} = a_{11} r + a_{12} \psi
\end{equation}

This strategy would help the gradient ascent of the agent to find suitable actions for each step more easily.

\subsection{Observation}

The velocity, angular velocity, location, euler angles vectors, and control input of the helicopter are all considered observations in this research. control input is considered an observation since it is not directly generated by the agent.

\subsection{Reward}

The reward function is the most important part of the environment as it provides the goal of the RL algorithm. In this research it consists of 4 terms given as follows:

\begin{equation}
	r_t(s) = r_{f} + r_{p} + r_{\psi}  + r_u
\end{equation}

In the above equation, $r_f$ is the flying reward term, $r_p$ represents the position reward term, $r_\psi$ denotes the yaw angle reward term and the $r_u$ stands for the control input reward term.

\subsubsection{Flying term}

Flying reward $r_{f}$ is just a constant (18.8 in this case), assures that the algorithm is rewarded for longer episodes. The absence of this term will lead to a local minimum of reward in which the agent tries to end the episode to stop receiving negative rewards by crashing the helicopter. It also helps to stabilize the UAV in the long term.

\subsubsection{Position term}

The position error $r_{p}$ punishes the agent for the distance between the current position of the UAV and the origin:

\begin{equation}
	r_{p}(t) = -10 \| X(t) \|_2
\end{equation}

\subsubsection{Yaw angle term}

This term also similarly punishes the agent for the error of $\psi$:

\begin{equation}
	r_{\psi}(t) = -0.25 | \psi(t) |
\end{equation}

\subsubsection{Control input terms}

The control input terms consist of a derivative and a norm term to reduce chattering and increase energy consumption of the UAV:

\begin{equation} \label{control_reward}
	r_u = - 0.015 \|U\| - 0.08 \|U'\|
\end{equation}

\subsection{Checking for a terminal state}

Unless the helicopter crashes or 8 seconds have passed, it is not a terminal state. Crashing in this research is when the states are outside of the $[-100,100]$, except for the Euler angles which the bounds are $ \phi \in [-\pi,\pi]$, $ \theta \in [-\pi/2,\pi/2]$ and $ \psi \in [-2\pi,2\pi]$.
\subsection{Summary}

In this section, the dynamics for 6-DOF nonlinear dynamics of a small-scale
UAV is provided. It included the effect of the fuselage, main rotor, tail rotor, etc. The setup of the environment is explained, and the code is given in Appendix A. The procedure to implement the actions and rewards in this research is also explained in detail. The implementation specifics of the SAC algorithm in this context are elaborated on in the next chapter, and the results are analyzed.


\chapter{Densità}
\begin{equation}
{\color{red}densità}=\dfrac{\rosso{massa}}{volume}
\end{equation}
This is {\color{red} highlighted} \rosso{casa}, and this is not.


\vsssub
\subsubsection{~Sea-state dependent $\tau$: Donelan et al. 2012} \label{sec:FLD2}
\vsssub

\opthead{FLD2}{UMWM}{B. Reichl}

\paragraph{Wind stress according to Donelan et al., 2012}
In \citet{art:Don12} the growth rate parameter in Eq. (\ref{formdrag}) is expressed as:
\begin{equation}
\beta_g(k,\theta)=A_1 \sigma \frac{\left[u_{\lambda/2} \cos(\theta-\theta_w)-c\right]\left| u_{\lambda/2}cos(\theta-\theta_w)-c \right|}{c^2}\frac{\rho_a}{\rho_w},
\end{equation}
\begin{equation}
A_{1}=\left\{
\begin{array}{lll}
0.11, & :  u_{\lambda/2}\cos\theta>c,  &\mbox{for wind forced sea}\\
0.01 & :   0<u_{\lambda/2}\cos\theta<c, &\mbox{for swell faster than the wind}\\
0.1 & :  \cos\theta<0,  &\mbox{for swell opposing the wind}
\end{array}
\right.
\end{equation}
where $A_1$ is the proportionality coefficient determined empirically (so that modeled wave spectra agree with field observations), $u_{\lambda/2}$ is the wind speed at the height of half the wavelength (up to 20 m), $\theta_w$ is the wind direction, and $c$ is the wave phase speed.
The wind speed is calculated using the law of the wall for rough surfaces:
\begin{equation}
u(z)=\frac{u_\star}{\kappa}\ln\left(\frac{z}{z_0}\right),
\end{equation}
where $\kappa$ is the von K\'arm\'an coefficient (default 0.4).
The viscous stress is calculated from the law of the wall for smooth surfaces.
The viscous drag coefficient, $Cd_\nu$ is adjusted to account for sheltering:
\begin{equation}
Cd'_\nu=\frac{Cd_\nu}{3}\left(1+\frac{2 Cd_\nu}{Cd_\nu+Cd_f}\right),
\end{equation}
where $Cd_f$ is the wave form drag coefficient.
The viscous stress can then be solved for as:
\begin{equation}
\vec{\tau}_\nu=\rho_a Cd'_\nu \left|{\bf u_{z}}\right|{\bf {u}}_{z}.
\end{equation}

Note that when using the FLD2 switch, internal variables and output values of the viscous stress, friction velocity, surface roughness length and Charnock parameter are recalculated and overwritten.