\documentclass[t,usenames,dvipsnames]{beamer}
\usetheme{Copenhagen}
\setbeamertemplate{headline}{} % remove toc from headers
\beamertemplatenavigationsymbolsempty

\usepackage{amsmath, tikz, xcolor}
\usetikzlibrary{arrows.meta, calc}

\title{Polar Coordinates}
\author{}
\date{}

\AtBeginSection[]
{
  \begin{frame}
    \frametitle{Objectives}
    \tableofcontents[currentsection]
  \end{frame}
}

\begin{document}

\begin{frame}
    \titlepage
\end{frame}

\section{Plot polar coordinates.}

\begin{frame}{Polar Coordinates}
    \begin{tabular}{p{0.4\textwidth}p{0.4\textwidth}}
\begin{tikzpicture}
    \coordinate (O) at (0,0);
    \draw [->, >=stealth, color=blue, line width = 1.25] (O) -- (2,0) node [above, midway, black] {Polar axis};
    \draw [fill=blue, color=blue] (O) circle (2pt) node [below left, black] {Pole};
\end{tikzpicture}
&
\begin{tikzpicture}
    \coordinate (O) at (0,0);
    \draw [->, >=stealth, color=blue, line width = 1.25] (O) -- (2,0) node [below, midway, black] {Polar axis};
    \draw [fill=blue, color=blue] (O) circle (2pt) node [below left, black] {Pole};
    \draw [-, >=stealth, color=blue, line width = 1.25] (O) -- (30:2) node [right, black] {$P=(r,\theta)$};
    \draw [color=red, fill=red] (30:2) circle (2pt);
    \draw [->,>=stealth] (0:1) arc (0:30:1) node [midway, right] {$\theta$};
    \node at (30:1) [above left] {$r$};
\end{tikzpicture}
\end{tabular}
\\[10pt]
\pause
For polar coordinates:  \newline\\
\begin{itemize}
    \item Start at the origin (pole)    \pause
    \item Go out $r$ units right ($r > 0$) or left ($r < 0$)    \pause
    \item Rotate by the amount given (\textbf{**direction**})   \pause
\end{itemize}
\vspace{10pt}

The polar coordinates of a point are $(r, \theta)$.
\end{frame}

\begin{frame}{Example 1. Plot each of the following}
\begin{minipage}{0.56\textwidth}
\begin{tikzpicture}[scale = 0.52, every node/.style={scale=0.6}]
    % Origin Point
    \coordinate (O) (0,0);
    % Circles
    \foreach \r in {1.5,3,...,6}
    {
        \draw[thick] (O) circle (\r cm);
    }

    % \foreach \r in {1.5,3,...7.5}
    % {
    %     \node at (\r, 0) [anchor=north] {\r/};
    % }

    \foreach \r in {0.75, 2.25,...,5.25}
    {
        \draw[dotted] (O) circle (\r cm);
    }

    % \node at (1.5, 0) [anchor = north west] {1};
    % \node at (3, 0) [anchor = north west] {2};
    % \node at (4.5, 0) [anchor = north west] {3};
    % \node at (6, 0) [anchor = north west] {4};
    % \node at (7.5,0) [anchor = north west] {5};
    % \foreach \r in {1.5,3,...,9}
    % {
    %     \node at (\r, 0) {\r/1.5};
    % }
    % Radial Lines
    \foreach \l in {0,15,...,360}
    {
        \draw (O) -- ++(\l:6cm);
    }
    % Thicker Radial Lines at 90 degree increments
    \foreach \l in {0,90,...,360}
    {
        \draw[very thick] (O) -- ++(\l:6cm);
    }
    % Minor Tick marks on outer circle
    \foreach \t in {0,5,...,360}
    {
        \draw (O) ++ (\t:5.85cm) -- ++(\t:0.3cm);
    }
    % Now for the fun part, the text
    % Degrees
    \node[draw=none, rotate= -90 ] at ( 0 :6.45cm) {$ 0 ^\circ$};
    \node[draw=none, rotate= -75 ] at ( 15 :6.45cm) {$ 15 ^\circ$};
    \node[draw=none, rotate= -60 ] at ( 30 :6.45cm) {$ 30 ^\circ$};
    \node[draw=none, rotate= -45 ] at ( 45 :6.45cm) {$ 45 ^\circ$};
    \node[draw=none, rotate= -30 ] at ( 60 :6.45cm) {$ 60 ^\circ$};
    \node[draw=none, rotate= -15 ] at ( 75 :6.45cm) {$ 75 ^\circ$};
    \node[draw=none, rotate= 0 ] at ( 90 :6.45cm) {$ 90 ^\circ$};
    \node[draw=none, rotate= 15 ] at ( 105 :6.45cm) {$ 105 ^\circ$};
    \node[draw=none, rotate= 30 ] at ( 120 :6.45cm) {$ 120 ^\circ$};
    \node[draw=none, rotate= 45 ] at ( 135 :6.45cm) {$ 135 ^\circ$};
    \node[draw=none, rotate= 60 ] at ( 150 :6.45cm) {$ 150 ^\circ$};
    \node[draw=none, rotate= 75 ] at ( 165 :6.45cm) {$ 165 ^\circ$};
    \node[draw=none, rotate= 90 ] at ( 180 :6.45cm) {$ 180 ^\circ$};
    \node[draw=none, rotate= 285 ] at ( 195 :6.45cm) {$ 195 ^\circ$};
    \node[draw=none, rotate= 300 ] at ( 210 :6.45cm) {$ 210 ^\circ$};
    \node[draw=none, rotate= 315 ] at ( 225 :6.45cm) {$ 225 ^\circ$};
    \node[draw=none, rotate= 330 ] at ( 240 :6.45cm) {$ 240 ^\circ$};
    \node[draw=none, rotate= 345 ] at ( 255 :6.45cm) {$ 255 ^\circ$};
    \node[draw=none, rotate= 360 ] at ( 270 :6.45cm) {$ 270 ^\circ$};
    \node[draw=none, rotate= 375 ] at ( 285 :6.45cm) {$ 285 ^\circ$};
    \node[draw=none, rotate= 390 ] at ( 300 :6.45cm) {$ 300 ^\circ$};
    \node[draw=none, rotate= 405 ] at ( 315 :6.45cm) {$ 315 ^\circ$};
    \node[draw=none, rotate= 420 ] at ( 330 :6.45cm) {$ 330 ^\circ$};
    \node[draw=none, rotate= 435 ] at ( 345 :6.45cm) {$ 345 ^\circ$};
    % Radians

    \node[draw=none, rotate= -90 ] at ( 0 :7cm) {$  $};
    \node[draw=none, rotate= -75 ] at ( 15 :7cm) {\scriptsize$ \pi/12 $};
    \node[draw=none, rotate= -60 ] at ( 30 :7cm) {\scriptsize$ \pi/6 $};
    \node[draw=none, rotate= -45 ] at ( 45 :7cm) {\scriptsize$ \pi/4 $};
    \node[draw=none, rotate= -30 ] at ( 60 :7cm) {\scriptsize$ \pi/3 $};
    \node[draw=none, rotate= -15 ] at ( 75 :7cm) {\scriptsize$ 5\pi/12 $};
    \node[draw=none, rotate= 0 ] at ( 90 :7cm) {\scriptsize$ \pi/2 $};
    \node[draw=none, rotate= 15 ] at ( 105 :7cm) {\scriptsize$ 7\pi/12 $};
    \node[draw=none, rotate= 30 ] at ( 120 :7cm) {\scriptsize$ 2\pi/3 $};
    \node[draw=none, rotate= 45 ] at ( 135 :7cm) {\scriptsize$ 3\pi/4 $};
    \node[draw=none, rotate= 60 ] at ( 150 :7cm) {\scriptsize$ 5\pi/6 $};
    \node[draw=none, rotate= 75 ] at ( 165 :7cm) {\scriptsize$ 11\pi/12 $};
    \node[draw=none, rotate= 90 ] at ( 180 :7cm) {\scriptsize$ \pi $};
    \node[draw=none, rotate= 285 ] at ( 195 :7cm) {\scriptsize$ 13\pi/2 $};
    \node[draw=none, rotate= 300 ] at ( 210 :7cm) {\scriptsize$ 7\pi/6 $};
    \node[draw=none, rotate= 315 ] at ( 225 :7cm) {\scriptsize$ 5\pi/4 $};
    \node[draw=none, rotate= 330 ] at ( 240 :7cm) {\scriptsize$ 4\pi/3 $};
    \node[draw=none, rotate= 345 ] at ( 255 :7cm) {\scriptsize$ 17\pi/12 $};
    \node[draw=none, rotate= 360 ] at ( 270 :7cm) {\scriptsize$ 3\pi/2 $};
    \node[draw=none, rotate= 375 ] at ( 285 :7cm) {\scriptsize$ 19\pi/12 $};
    \node[draw=none, rotate= 390 ] at ( 300 :7cm) {\scriptsize$ 5\pi/3 $};
    \node[draw=none, rotate= 405 ] at ( 315 :7cm) {\scriptsize$ 7\pi/4 $};
    \node[draw=none, rotate= 420 ] at ( 330 :7cm) {\scriptsize$ 11\pi/6 $};
    \node[draw=none, rotate= 435 ] at ( 345 :7cm) {\scriptsize$ 23\pi/12 $};

    \onslide<2->{\draw [color=red,fill=red] (240:3) circle (4pt) node [above] {$A$};}
    \onslide<4->{\draw [color=red,fill=red] (30:6) circle (4pt) node [left, yshift=0.2cm] {$B$};}
    \onslide<6->{\draw[color=red,fill=red] (270:3.75) circle (4pt) node [above right] {$C$};}
    \onslide<8->{\draw[color=red,fill=red] (135:4.5) circle (4pt) node [right] {$D$};}
\end{tikzpicture}
\end{minipage}
\hspace{1.17cm}
\begin{minipage}{0.12\textwidth}
% (a) $A\left(2, 240^\circ\right)$ \\[15pt]
% \onslide<3->{(b) $B\left(-4,\dfrac{7\pi}{6}\right)$} \\[15pt]
% \onslide<5->{(c) $C\left(2.5,-\dfrac{5\pi}{2}\right)$} \\[15pt]
% \onslide<7->{(d) $D\left(-3, -\dfrac{\pi}{4}\right)$} \\
\begin{align*}
    &(a)\quad A\left(2, 240^\circ\right) \\[15pt]
    \onslide<3->{&(b)\quad B\left(-4,\dfrac{7\pi}{6}\right)} \\[15pt]
    \onslide<5->{&(c)\quad C\left(2.5,-\dfrac{5\pi}{2}\right)}    \\[15pt]
    \onslide<7->{&(d)\quad D\left(-3, -\dfrac{\pi}{4}\right)}
\end{align*}
\end{minipage}
\end{frame}

\section{Convert from polar to rectangular coordinates.}

\begin{frame}{Polar to Rectangular Coordinates}

\begin{center}
\begin{tikzpicture}
    \draw [->, >=stealth] (-0.5,0) -- (3.5,0) node [right] {$x$};
    \draw [->, >=stealth] (0,-0.5) -- (0,2.5) node [left] {$y$};
    \draw (2.5,0) rectangle +(-0.25,0.25);
    \draw [color=blue, line width = 1.5] (0,0) -- (2.5,0) node [midway, below, black] {$x$};
    \draw [color=blue, line width=1.5] (2.5,0) -- (2.5,2) node [midway, right, black] {$y$};
    \draw [color=red, line width=1.5] (0,0) -- (2.5,2) node [above right] {$P(r,\theta)$};
    \draw [color=red, fill=red] (2.5,2) circle (2pt);
    \node at (2.5,2) [below right, blue] {$P(x, y)$};
    \node at (1.25, 1) [above left] {$r$};
    \draw [->, >=stealth] (0:0.75) arc (0:38.5:0.75) node [midway, right] {$\theta$};
\end{tikzpicture}
\end{center}
\pause

\begin{align*}
    \onslide<2->{\cos\theta = \frac{x}{r} \qquad & \qquad \sin\theta = \frac{y}{r}} \\[11pt]
    \onslide<3->{x = r\cos \theta \qquad & \qquad y = r\sin \theta}   \\
\end{align*}
\end{frame}

\begin{frame}{Example 2}
Convert each to rectangular coordinates.    \newline\\
(a) \quad $\left(2, 240^\circ\right)$
\begin{align*}
    \onslide<2->{x=2\cos240^\circ \quad & \quad y = 2\sin240^\circ} \\[10pt]
    \onslide<3->{x=2\left(-\frac{1}{2}\right) \quad & \quad y = 2\left(-\frac{\sqrt{3}}{2}\right)} \\[10pt]
    \onslide<4->{x=-1 \quad & y = -\sqrt{3}}
\end{align*}
\onslide<5->{\[(-1,\, -\sqrt{3})\]}
\end{frame}

\begin{frame}{Example 2}
    \begin{center}
    \begin{tikzpicture}[scale = 0.52, every node/.style={scale=0.6}]
    % Origin Point
    \coordinate (O) (0,0);
    % Circles
    \foreach \r in {1.5,3,...,6}
    {
        \draw[thick] (O) circle (\r cm);
    }

    % \foreach \r in {1.5,3,...7.5}
    % {
    %     \node at (\r, 0) [anchor=north] {\r/};
    % }

    \foreach \r in {0.75, 2.25,...,5.25}
    {
        \draw[dotted] (O) circle (\r cm);
    }

    % \node at (1.5, 0) [anchor = north west] {1};
    % \node at (3, 0) [anchor = north west] {2};
    % \node at (4.5, 0) [anchor = north west] {3};
    % \node at (6, 0) [anchor = north west] {4};
    % \node at (7.5,0) [anchor = north west] {5};
    % \foreach \r in {1.5,3,...,9}
    % {
    %     \node at (\r, 0) {\r/1.5};
    % }
    % Radial Lines
    \foreach \l in {0,15,...,360}
    {
        \draw (O) -- ++(\l:6cm);
    }
    % Thicker Radial Lines at 90 degree increments
    \foreach \l in {0,90,...,360}
    {
        \draw[very thick] (O) -- ++(\l:6cm);
    }
    % Minor Tick marks on outer circle
    \foreach \t in {0,5,...,360}
    {
        \draw (O) ++ (\t:5.85cm) -- ++(\t:0.3cm);
    }
    % Now for the fun part, the text
    % Degrees
    \node[draw=none, rotate= -90 ] at ( 0 :6.45cm) {$ 0 ^\circ$};
    \node[draw=none, rotate= -75 ] at ( 15 :6.45cm) {$ 15 ^\circ$};
    \node[draw=none, rotate= -60 ] at ( 30 :6.45cm) {$ 30 ^\circ$};
    \node[draw=none, rotate= -45 ] at ( 45 :6.45cm) {$ 45 ^\circ$};
    \node[draw=none, rotate= -30 ] at ( 60 :6.45cm) {$ 60 ^\circ$};
    \node[draw=none, rotate= -15 ] at ( 75 :6.45cm) {$ 75 ^\circ$};
    \node[draw=none, rotate= 0 ] at ( 90 :6.45cm) {$ 90 ^\circ$};
    \node[draw=none, rotate= 15 ] at ( 105 :6.45cm) {$ 105 ^\circ$};
    \node[draw=none, rotate= 30 ] at ( 120 :6.45cm) {$ 120 ^\circ$};
    \node[draw=none, rotate= 45 ] at ( 135 :6.45cm) {$ 135 ^\circ$};
    \node[draw=none, rotate= 60 ] at ( 150 :6.45cm) {$ 150 ^\circ$};
    \node[draw=none, rotate= 75 ] at ( 165 :6.45cm) {$ 165 ^\circ$};
    \node[draw=none, rotate= 90 ] at ( 180 :6.45cm) {$ 180 ^\circ$};
    \node[draw=none, rotate= 285 ] at ( 195 :6.45cm) {$ 195 ^\circ$};
    \node[draw=none, rotate= 300 ] at ( 210 :6.45cm) {$ 210 ^\circ$};
    \node[draw=none, rotate= 315 ] at ( 225 :6.45cm) {$ 225 ^\circ$};
    \node[draw=none, rotate= 330 ] at ( 240 :6.45cm) {$ 240 ^\circ$};
    \node[draw=none, rotate= 345 ] at ( 255 :6.45cm) {$ 255 ^\circ$};
    \node[draw=none, rotate= 360 ] at ( 270 :6.45cm) {$ 270 ^\circ$};
    \node[draw=none, rotate= 375 ] at ( 285 :6.45cm) {$ 285 ^\circ$};
    \node[draw=none, rotate= 390 ] at ( 300 :6.45cm) {$ 300 ^\circ$};
    \node[draw=none, rotate= 405 ] at ( 315 :6.45cm) {$ 315 ^\circ$};
    \node[draw=none, rotate= 420 ] at ( 330 :6.45cm) {$ 330 ^\circ$};
    \node[draw=none, rotate= 435 ] at ( 345 :6.45cm) {$ 345 ^\circ$};
    % Radians

    \node[draw=none, rotate= -90 ] at ( 0 :7cm) {$  $};
    \node[draw=none, rotate= -75 ] at ( 15 :7cm) {\scriptsize$ \pi/12 $};
    \node[draw=none, rotate= -60 ] at ( 30 :7cm) {\scriptsize$ \pi/6 $};
    \node[draw=none, rotate= -45 ] at ( 45 :7cm) {\scriptsize$ \pi/4 $};
    \node[draw=none, rotate= -30 ] at ( 60 :7cm) {\scriptsize$ \pi/3 $};
    \node[draw=none, rotate= -15 ] at ( 75 :7cm) {\scriptsize$ 5\pi/12 $};
    \node[draw=none, rotate= 0 ] at ( 90 :7cm) {\scriptsize$ \pi/2 $};
    \node[draw=none, rotate= 15 ] at ( 105 :7cm) {\scriptsize$ 7\pi/12 $};
    \node[draw=none, rotate= 30 ] at ( 120 :7cm) {\scriptsize$ 2\pi/3 $};
    \node[draw=none, rotate= 45 ] at ( 135 :7cm) {\scriptsize$ 3\pi/4 $};
    \node[draw=none, rotate= 60 ] at ( 150 :7cm) {\scriptsize$ 5\pi/6 $};
    \node[draw=none, rotate= 75 ] at ( 165 :7cm) {\scriptsize$ 11\pi/12 $};
    \node[draw=none, rotate= 90 ] at ( 180 :7cm) {\scriptsize$ \pi $};
    \node[draw=none, rotate= 285 ] at ( 195 :7cm) {\scriptsize$ 13\pi/2 $};
    \node[draw=none, rotate= 300 ] at ( 210 :7cm) {\scriptsize$ 7\pi/6 $};
    \node[draw=none, rotate= 315 ] at ( 225 :7cm) {\scriptsize$ 5\pi/4 $};
    \node[draw=none, rotate= 330 ] at ( 240 :7cm) {\scriptsize$ 4\pi/3 $};
    \node[draw=none, rotate= 345 ] at ( 255 :7cm) {\scriptsize$ 17\pi/12 $};
    \node[draw=none, rotate= 360 ] at ( 270 :7cm) {\scriptsize$ 3\pi/2 $};
    \node[draw=none, rotate= 375 ] at ( 285 :7cm) {\scriptsize$ 19\pi/12 $};
    \node[draw=none, rotate= 390 ] at ( 300 :7cm) {\scriptsize$ 5\pi/3 $};
    \node[draw=none, rotate= 405 ] at ( 315 :7cm) {\scriptsize$ 7\pi/4 $};
    \node[draw=none, rotate= 420 ] at ( 330 :7cm) {\scriptsize$ 11\pi/6 $};
    \node[draw=none, rotate= 435 ] at ( 345 :7cm) {\scriptsize$ 23\pi/12 $};
    \draw [color=red,fill=red] (240:3) circle (4pt);
    \end{tikzpicture}
    \end{center}
\end{frame}

\begin{frame}{Example 2}
\begin{center}
\begin{tikzpicture}[scale=0.52]
    \draw [<->,>=stealth,very thick] (-6,0) -- (6,0) node [right] {$x$};
    \draw [<->,>=stealth,very thick] (0,-6) -- (0,6) node [right] {$y$};
    \foreach \x in {-4.5,-3,...,4.5}
    \draw [thick] (\x,0.2) -- (\x,-0.2);
    \foreach \y in {-4.5,-3,...,4.5}
    \draw [thick] (0.2,\y) -- (-0.2,\y);
    \draw [color=red,fill=red] (240:3) circle (4pt);
\end{tikzpicture}
\end{center}
\end{frame}

\begin{frame}{Example 2}
(b) \quad $\left(-4, \dfrac{7\pi}{6}\right)$
\begin{align*}
    \onslide<2->{x = -4\cos\left(\frac{7\pi}{6}\right) \quad & \quad y = -4\sin\left(\frac{7\pi}{6}\right)} \\[10pt]
    \onslide<3->{x = -4\left(-\frac{\sqrt{3}}{2}\right) \quad & \quad y = -4\left(-\frac{1}{2}\right)} \\[10pt]
    \onslide<4->{x = 2\sqrt{3} \quad & \quad y = 2}
\end{align*}
\onslide<5->{\[\left(2\sqrt{3}, \, 2\right) \]}
\end{frame}

\begin{frame}{Example 2}
(c) \quad $\left(2.5, -\dfrac{5\pi}{2}\right)$
\begin{align*}
    \onslide<2->{x = 2.5\cos\left(-\frac{5\pi}{2}\right) \quad & \quad y = 2.5\sin\left(-\frac{5\pi}{2}\right)} \\[10pt]
    \onslide<3->{x = 2.5\left(0\right) \quad & \quad y = 2.5\left(-1\right)} \\[10pt]
    \onslide<4->{x = 0 \quad & \quad y = -2.5}
\end{align*}
\onslide<5->{\[\left(0, \, -2.5\right) \]}
\end{frame}

\begin{frame}{Example 2}
(d) \quad $\left(-3, -\dfrac{\pi}{4}\right)$
\begin{align*}
    \onslide<2->{x = -3\cos\left(-\frac{\pi}{4}\right) \quad & \quad y = -3\sin\left(-\frac{\pi}{4}\right)} \\[10pt]
    \onslide<3->{x = -3\left(\frac{\sqrt{2}}{2}\right) \quad & \quad y = -3\left(-\frac{\sqrt{2}}{2}\right)} \\[10pt]
    \onslide<4->{x = -\frac{3\sqrt{2}}{2} \quad & \quad y = \frac{3\sqrt{2}}{2}}
\end{align*}
\onslide<5->{\[\left(-\frac{3\sqrt{2}}{2}, \, \frac{3\sqrt{2}}{2}\right) \]}
\end{frame}

\section{Convert from rectangular to polar coordinates.}

\begin{frame}{Rectangular to Polar}
\begin{center}
\begin{tikzpicture}
    \draw [->, >=stealth] (-0.5,0) -- (3.5,0) node [right] {$x$};
    \draw [->, >=stealth] (0,-0.5) -- (0,2.5) node [left] {$y$};
    \draw (2.5,0) rectangle +(-0.25,0.25);
    \draw [color=blue, line width = 1.5] (0,0) -- (2.5,0) node [midway, below, black] {$x$};
    \draw [color=blue, line width=1.5] (2.5,0) -- (2.5,2) node [midway, right, black] {$y$};
    \draw [color=red, line width=1.5] (0,0) -- (2.5,2) node [above right] {$P(r,\theta)$};
    \draw [color=red, fill=red] (2.5,2) circle (2pt);
    \node at (2.5,2) [below right, blue] {$P(x, y)$};
    \node at (1.25, 1) [above left] {$r$};
    \draw [->, >=stealth] (0:0.75) arc (0:38.5:0.75) node [midway, right] {$\theta$};
\end{tikzpicture}
\end{center}
\pause
\[
r = \sqrt{x^2+y^2} \qquad \onslide<3->{\theta' = \tan^{-1}\left| \frac{y}{x} \right|}
\]
\vspace{15pt}
\onslide<4->{where $\theta '$ is the \underline{reference angle} used to find the total angle rotated, $\theta$.}
\end{frame}

\begin{frame}{Example 3}
Convert each of the following to polar coordinates. \newline\\
(a) \quad $\left(2, -2\sqrt{3}\right)$   \newline\\
\begin{minipage}{0.5\textwidth}
\begin{tikzpicture}[scale=0.9]
\draw[<->,>=stealth] (-2.5,0) -- (2.5,0) node [right] {$x$};
\draw[<->,>=stealth] (0,-2.5) -- (0,2.5) node [above] {$y$};
\coordinate (A) at (1,-1.7);
\draw[fill=black] (A) circle (2pt);
\onslide<2->{\draw (0,0) -- (A) -- node [right] {\scriptsize $-2\sqrt{3}$} (1,0) -- node [above] {\scriptsize $2$} cycle;}
\onslide<4->{\node at (0.3,-0.15) {\scriptsize $\theta'$};}
\onslide<4->{\node at (300:1) [left] {\scriptsize 4};}
\onslide<6->{\node at (0.3,-0.15) [color=white,fill=white] {};}
\onslide<6->{\node at (0.35,-0.15) {\scriptsize $60^\circ$};}
\onslide<7->{\draw[->,>=stealth,color=red] (0.75,0) arc (0:300:0.75) node [xshift=-1cm, yshift=1.25cm] {\scriptsize $300^\circ$};}
\end{tikzpicture}
\end{minipage}
\hspace{-0.5cm}
\begin{minipage}{0.5\textwidth}
\begin{align*}
    \onslide<3->{r &= \sqrt{2^2 + (2\sqrt{3})^2} = \sqrt{16} = 4}  \\[8pt]
    \onslide<5->{\theta' &= \tan^{-1}\left|\frac{-2\sqrt{3}}{2}\right| = 60^\circ}    \\[8pt]
    \onslide<7->{\theta &= 300^\circ} \\[8pt]
    & \onslide<8->{\left(4, \frac{5\pi}{3}\right)} \\
\end{align*}
\end{minipage}
\end{frame}

\begin{frame}{Example 3}
(b) \quad $(-3, -3)$ \newline\\
\begin{minipage}{0.4\textwidth}
\begin{tikzpicture}[scale=0.8]
\draw[<->,>=stealth] (-2.5,0) -- (2.5,0) node [right] {$x$};
\draw[<->,>=stealth] (0,-2.5) -- (0,2.5) node [above] {$y$};
\coordinate (A) at (-1.75,-1.75);
\draw [fill=black] (A) circle (2pt);
\onslide<2->{\draw (0,0) -- (A) -- node [left] {\scriptsize $-3$} (-1.75,0) -- node [above] {\scriptsize $-3$} cycle;}
\onslide<4->{\node at (-0.5,-0.2) {\scriptsize $\theta'$};}
\onslide<4->{\node at (225:1) [below, yshift=-0.1cm] {\scriptsize $3\sqrt{2}$};}
\onslide<6->{\node at (-0.5,-0.2) [color=white,fill=white] {};}
\onslide<6->{\node at (-0.6,-0.2) {\scriptsize $45^\circ$};}
\onslide<7->{\draw[->,>=stealth,color=red] (1.25,0) arc (0:225:1.25) node [midway, above left] {\scriptsize $225^\circ$};}
\end{tikzpicture}
\end{minipage}
\hspace{0.5cm}
\begin{minipage}{0.5\textwidth}
\begin{align*}
    \onslide<3->{r &= \sqrt{3^2 + 3^2} = 3\sqrt{2}}  \\[8pt]
    \onslide<5->{\theta' &= \tan^{-1}\left|\frac{-3}{-3}\right| = 45^\circ}    \\[8pt]
    \onslide<7->{\theta &= 225^\circ} \\[8pt]
    & \onslide<8->{\left(3\sqrt{2}, \frac{5\pi}{4}\right)} \\
\end{align*}
\end{minipage}
\end{frame}

\begin{frame}{Example 3}
(c) \quad $(0, -3)$ \newline\\
\begin{minipage}{0.4\textwidth}
\begin{tikzpicture}[scale=0.8]
\draw[<->,>=stealth] (-2.5,0) -- (2.5,0) node [right] {$x$};
\draw[<->,>=stealth] (0,-2.5) -- (0,2.5) node [above] {$y$};
\draw (-0.15,-1.75) -- (0.15,-1.75) node [right] {\scriptsize $-3$};
\coordinate (A) at (0,-1.75);
\draw [fill=black] (A) circle (2pt);
\onslide<2->{\node at (0,-0.75) [right, red] {\scriptsize $r = 3$};}
\onslide<3->{\draw[->,>=stealth,color=red] (1,0) arc (0:270:1) node [midway, above left] {\scriptsize $270^\circ$};}
\end{tikzpicture}
\end{minipage}
\hspace{0.5cm}
\begin{minipage}{0.5\textwidth}
\begin{align*}
    \onslide<2->{r &= 3}  \\[12pt]
    \onslide<4->{\theta &= \frac{3\pi}{2}} \\[12pt]
    \onslide<5->{&\left(3, \frac{3\pi}{2}\right)}
\end{align*}
\end{minipage}
\end{frame}

\begin{frame}{Example 3}
(d) \quad $(-3, 4)$ \newline\\
\begin{minipage}{0.4\textwidth}
\begin{tikzpicture}[scale=0.8]
\draw[<->,>=stealth] (-2.5,0) -- (2.5,0) node [right] {$x$};
\draw[<->,>=stealth] (0,-2.5) -- (0,2.5) node [above] {$y$};
\coordinate (A) at (-1.75,2);
\draw [fill=black] (A) circle (2pt);
\onslide<2->{\draw (0,0) -- (A) -- node [left] {\scriptsize $4$} (-1.75,0) -- node [below] {\scriptsize $-3$} cycle;}
\onslide<4->{\node at (-0.5,0.2) {\scriptsize $\theta'$};}
\onslide<4->{\node at (127:1.5) [right] {\scriptsize $5$};}
\onslide<6->{\node at (-0.5,0.2) [color=white,fill=white] {};}
\onslide<6->{\node at (-0.7,0.2) {\scriptsize $53.13^\circ$};}
\onslide<7->{\draw[->,>=stealth,color=red] (1,0) arc (0:127:1) node [midway, above right] {\scriptsize $126.87^\circ$};}
\end{tikzpicture}
\end{minipage}
\hspace{0.5cm}
\begin{minipage}{0.5\textwidth}
\begin{align*}
    \onslide<3->{r &= \sqrt{3^2 + 4^2} = 5}  \\[8pt]
    \onslide<5->{\theta' &= \tan^{-1}\left|\frac{4}{-3}\right| \approx 53.13^\circ}    \\[8pt]
    \onslide<7->{\theta &\approx 126.87^\circ} \\[8pt]
    \onslide<8->{&\left(5, \pi - \tan^{-1}\left(\frac{4}{3}\right)\right)} \\
\end{align*}
\end{minipage}
\end{frame}

\section{Convert rectangular equations to polar equations.}

\begin{frame}{Rectangular and Polar Equations}
We can use the relationship between rectangular and polar coordinates to convert equations of one form to the other.  \newline\\  \pause

\begin{tabular}{p{0.4\textwidth}p{0.4\textwidth}}
    \onslide<2->{$x = r\cos \theta$}  & \onslide<3->{$x^2 + y^2 = r^2$}  \\
    \onslide<2->{$y = r\sin\theta$}   & \onslide<3->{$\tan\theta = \dfrac{y}{x}$}  \\
\end{tabular}
\end{frame}

\begin{frame}{Example 4}
    Convert each to polar equations.    \newline\\
(a) \quad $y=-x$
\begin{align*}
    \onslide<2->{{\color{red}y}&=-{\color{blue}x}} \\[8pt]
    \onslide<3->{{\color{red}r\sin\theta} &= - {\color{blue}r\cos\theta}} \\[8pt]
    \onslide<4->{r\cos\theta\ + r\sin\theta &= 0} \\[8pt]
    \onslide<5->{r(\cos\theta + \sin\theta) &= 0}
\end{align*}
\end{frame}

\begin{frame}{Example 4}
\[r(\cos\theta + \sin\theta) = 0\]
\begin{align*}
    \onslide<2->{r &= 0 & \cos\theta + \sin\theta &= 0} \\[8pt]
    \onslide<3->{&& \cos\theta &= -\sin\theta} \\[8pt]
    \onslide<4->{&&{\color{red}\theta} &= {\color{red}-\frac{\pi}{4}}}
\end{align*}
\end{frame}

\begin{frame}{Example 4}
(b) \quad $y=x^2$
\begin{align*}
    \onslide<2->{{\color{red}r\sin\theta} &= \left({\color{blue}r\cos\theta}\right)^2} \\[8pt]
    \onslide<3->{r\sin\theta &= r^2\cos^2\theta} \\[8pt]
    \onslide<4->{r\sin\theta - r^2\cos^2\theta &= 0} \\[8pt]
    \onslide<5->{r\left(\sin\theta - r\cos^2\theta\right) &= 0}
\end{align*}
\begin{align*}
    \onslide<6->{r &= 0 & \sin\theta - r\cos^2\theta &= 0}
\end{align*}
\end{frame}

\begin{frame}{Example 4}
\[ \sin\theta - r\cos^2\theta = 0\]
\begin{align*}
    \onslide<2->{\sin\theta &= r\cos^2\theta} \\[8pt]
    \onslide<3->{r &= \frac{\sin\theta}{\cos^2\theta}} \\[10pt]
    \onslide<4->{r &= \left(\frac{\sin\theta}{\cos\theta}\right)\left(\frac{1}{\cos\theta}\right)}    \\[10pt]
    \onslide<5->{r &= \tan\theta \cdot \sec\theta}
\end{align*}
\end{frame}

\begin{frame}{Example 4}
(c) \quad $(x-3)^2 + y^2 = 9$
\begin{align*}
    \onslide<2->{(r\cos\theta - 3)^2 + (r\sin\theta)^2 &= 9} \\[6pt]
    \onslide<3->{r^2\cos^2\theta - 6r\cos\theta + 9 + r^2\sin^2\theta &= 9} \\[6pt]
    \onslide<4->{r^2\cos^2\theta + r^2\sin^2\theta - 6r\cos\theta &= 0} \\[6pt]
    \onslide<5->{r^2\left(\cos^2\theta + \sin^2\theta\right) - 6r\cos\theta &= 0} \\[6pt]
    \onslide<6->{r^2 - 6r\cos\theta &= 0} \\[6pt]
    \onslide<7->{r(r-6\cos\theta) &= 0}
\end{align*}
\begin{align*}
\onslide<8->{r &= 0 & r-6\cos\theta &= 0}
\end{align*}
\end{frame}

\begin{frame}{Example 4}
    \[r-6\cos\theta = 0\]
\onslide<2->{\[r = 6\cos\theta \]}
\end{frame}

\section{Convert polar equations to rectangular equations}

\begin{frame}{Example 5}
Convert each of the following to rectangular equations. \newline\\
(a) \quad $r = -3$
\begin{align*}
    \onslide<2->{r &= -3} \\[8pt]
    \onslide<3->{r^2 &= 9} \\[8pt]
    \onslide<4->{x^2+y^2 &= 9}
\end{align*}
\end{frame}

\begin{frame}{Example 5}
(b) \quad $\theta = \dfrac{4\pi}{3}$
\begin{align*}
    \onslide<2->{\theta &= \frac{4\pi}{3}} \\[8pt]
    \onslide<3->{\tan \theta &= \tan\left(\frac{4\pi}{3}\right)} \\[8pt]
    \onslide<4->{\frac{y}{x} &= \sqrt{3}} \\[8pt]
    \onslide<5->{y &= x\sqrt{3}} \\
\end{align*}
\end{frame}

\begin{frame}{Example 5}
(c) \quad $r = 1 - \cos\theta$
\begin{align*}
    \onslide<2->{r &= 1 - \cos\theta} \\[6pt]
    \onslide<3->{r \cdot {\color{red}r} &= {\color{red}r}(1-\cos\theta)} \\[6pt]
    \onslide<4->{r^2 &= r-r\cos\theta} \\[6pt]
    \onslide<5->{x^2 + y^2 &= r - x} \\[6pt]
    \onslide<6->{x^2 + y^2 + x &= r} \\[6pt]
    \onslide<7->{\left(x^2 + y^2 + x\right)^2 &= x^2 + y^2}
\end{align*}
\end{frame}

\end{document}


\documentclass[11pt]{scrartcl} % Font size
\input{../structure.tex} % Include the file specifying the document structure and custom commands

%----------------------------------------------------------------------------------------
%	TITLE SECTION
%----------------------------------------------------------------------------------------

\title{
	\normalfont\normalsize
	\textsc{Harvard Privacy Tools Project}\\ % Your university, school and/or department name(s)
	\vspace{25pt} % Whitespace
	\rule{\linewidth}{0.5pt}\\ % Thin top horizontal rule
	\vspace{20pt} % Whitespace
	{\huge Covariance Sensitivity Proofs}\\ % The assignment title
	\vspace{12pt} % Whitespace
	\rule{\linewidth}{2pt}\\ % Thick bottom horizontal rule
	\vspace{12pt} % Whitespace
}

% \author{\LARGE} % Your name

\date{\normalsize\today} % Today's date (\today) or a custom date

\begin{document}

\maketitle

\section{Preliminaries}

\begin{definition}
Let $X$ be a matrix of values and $X_i$ indicate the $i^{\text{th}}$ column of the matrix. Denote the sample mean of column $X_i$ as $\bar{X}_i$, and let $n$ be the size of $X_i$. Then the covariance matrix of $X$ has $ij^{\text{th}}$ element
$$ \frac{1}{n-1} \sum_{k=1}^n (x_{ki} - \bar{X}_i)(x_{kj} - \bar{X}_j).$$
\end{definition}
\begin{lemma}
\label{lemma:cancel}
Let $X$ be a matrix of values and $X_i$ indicate the $i^{\text{th}}$ column of the matrix. Denote the sample mean of column $X_i$ as $\bar{X}_i$, and let $n$ be the size of $X_i$. Then,
$\forall i,$
$$ \sum_{j=1}^n (x_{ji} - \bar{X}_i) = 0.$$
\end{lemma}

\begin{proof}
\begin{align*}
\sum_{j=1}^n (x_{ji} - \bar{X}_i) &= \sum_{j=1}^n x_{ji} - n \bar{X}_i,\\
	&= \sum_{j=1}^n x_{ji} - n\left( \frac{1}{n}\sum_{j=1}^n x_{ji}\right), \\
	&= 0.
\end{align*}
\end{proof}

\begin{lemma}
\label{cov:rewrite}
Let $X$ be a matrix and let
$$ f_{ij}(X) = \sum_{k=1}^n (x_{ki} - \bar{X}_i)(x_{kj} - \bar{X}_j).$$
Note that this is equivalent to the $ij^{\text{th}}$ element of the sample covariance matrix for $X$, without the normalization by $n-1$. Consider the matrix $X'$ equal to $X$ with a single row $Y$ added, so that $X_i' = X_i \cup \{y_i\}$. Say $X_i$ has size $n$. Let  $\bar{X}_i$, $\bar{X}_j$, $\bar{X}_i'$, and $\bar{X}_j'$ be the sample means of $X_i, X_j, X_i'$ and $X_j'$ respectively. Then,
$$ f_{ij}(X') = f_{ij}(X) + n(\bar{X}_i - \bar{X}_i')(\bar{X}_j - \bar{X}_j') + (y_i - \bar{X}_i)(y_j - \bar{X}_j).$$
\end{lemma}

\begin{proof}
Note that
\begin{align*}
f_{ij}(X') &= \sum_{k=1}^{n+1} (x_{ki}' - \bar{X}_i')(x_{kj}' - \bar{X}_j'),\\
	&= \sum_{k=1}^{n} (x_{ki} - \bar{X}_i')(x_{kj} - \bar{X}_j') + (y_i - \bar{X}_i')(y_j - \bar{X}_j'),\\
	&= \sum_{k=1}^{n} \left( (x_{ki} - \bar{X}_i )+ (\bar{X}_i - \bar{X}_i' )\right) \left( (x_{kj} - \bar{X}_j )+ (\bar{X}_j - \bar{X}_j') \right) + (y_i - \bar{X}_i')(y_j - \bar{X}_j'),\\
	&= \sum_{k=1}^{n} (x_{ki} - \bar{X}_i)(x_{kj} - \bar{X}_j) + (\bar{X}_j - \bar{X}_j')\sum_{k=1}^{n} (x_{ki}-\bar{X}_i) + (\bar{X}_i - \bar{X}_i') \sum_{k=1}^{n} (x_{kj} - \bar{X}_j),\\
	& \hspace{1cm} + \sum_{k=1}^{n} (\bar{X}_i - \bar{X}_i')(\bar{X}_j - \bar{X}_j') + (y_i - \bar{X}_i')(y_j - \bar{X}_j'),\\
	&= f_{ij}(X) + n(\bar{X}_i - \bar{X}_i')(\bar{X}_j - \bar{X}_j') + (y_i - \bar{X}_i')(y_j - \bar{X}_j'),
\end{align*}
where the cancellation of the second and third terms in the second-to-last line is due to Lemma \ref{lemma:cancel}.
\end{proof}

\begin{lemma}
\label{lemma:term1bound}
Let $X$ be a matrix of values and $X_i$ indicate the $i^{\text{th}}$ column of the matrix. Let  $X_i$ have size $n$ and consider the matrix $X'$ equal to $X$ with a single row $Y$ added, so that $X_i' = X_i \cup \{y_i\}$. Say that the space of datapoints $\mathcal{X}_i$ that the elements of $X_i'$ are drawn from is bounded above by $M_i$ and bounded below by $m_i$. Let  $\bar{X}_i$, $\bar{X}_j$, $\bar{X}_i'$, and $\bar{X}_j'$ be the sample means of $X_i, X_j, X_i'$ and $X_j'$ respectively. Then,
$$ n \left\vert (\bar{X}_i - \bar{X}_i')(\bar{X}_j - \bar{X}_j') \right\vert \le \frac{n}{(n+1)^2}(M_i - m_i)(M_j - m_j).$$
\end{lemma}

\begin{proof}
Note that
\begin{align*}
 n \left\vert (\bar{X}_i - \bar{X}_i')(\bar{X}_j - \bar{X}_j') \right\vert &= n\left\vert \left( \frac{1}{n} \sum_{k=1}^n x_{ki} - \frac{1}{n+1} \sum_{k=1}^{n+1} x_{ki}' \right) \left( \frac{1}{n} \sum_{k=1}^n x_{kj} - \frac{1}{n+1} \sum_{k=1}^{n+1} x_{kj}' \right)\right\vert, \\
 	&= n \left\vert \left( \left(\frac{1}{n} - \frac{1}{n+1}\right) \sum_{k=1}^n x_{ki} - \frac{y_i}{n+1} \right)\left( \left(\frac{1}{n} - \frac{1}{n+1}\right) \sum_{k=1}^n x_{kj} - \frac{y_j}{n+1} \right) \right\vert, \\
	&= n  \left\vert \left(\frac{1}{n(n+1)} \sum_{k=1}^n x_{ki} - \frac{y_i}{n+1} \right)\left( \frac{1}{n(n+1)} \sum_{k=1}^n x_{kj} - \frac{y_j}{n+1} \right) \right\vert, \\
	&= \frac{n}{(n+1)^2} \left\vert \left( \frac{1}{n} \sum_{k=1}^n x_{ki} - \frac{y_i}{n+1} \right)\left( \frac{1}{n} \sum_{k=1}^n x_{kj} - \frac{y_j}{n+1} \right) \right\vert,\\
	&\le \frac{n}{(n+1)^2} (M_i - m_i)(M_j - m_j).
 \end{align*}
\end{proof}

\begin{lemma}
\label{lemma:term2bound}
Let $X$ be a matrix of values and $X_i$ indicate the $i^{\text{th}}$ column of the matrix. Let  $X_i$ have size $n$ and consider the matrix $X'$ equal to $X$ with a single row $Y$ added, so that $X_i' = X_i \cup \{y_i\}$. Say that the space of datapoints $\mathcal{X}_i$ that the elements of $X_i'$ are drawn from is bounded above by $M_i$ and bounded below by $m_i$. Let  $\bar{X}_i$, $\bar{X}_j$, $\bar{X}_i'$, and $\bar{X}_j'$ be the sample means of $X_i, X_j, X_i'$ and $X_j'$ respectively. Then,
$$ \left\vert (y_i - \bar{X}_i')(y_j - \bar{X}_j') \right\vert \le \frac{n^2}{(n+1)^2}(M_i - m_i)(M_j-m_j).$$
\end{lemma}

\begin{proof}
Note that
\begin{align*}
 \left\vert (y_i - \bar{X}_i')(y_j - \bar{X}_j') \right\vert &= \left\vert \left( y_i - \frac{y_i + n \bar{X}_i}{n+1}\right) \left( y_j - \frac{y_j + n \bar{X}_j}{n+1}\right) \right\vert, \\
 	&= \frac{1}{(n+1)^2} \left\vert \left((n+1)y_i - y_i - n\bar{X}_i \right)\left((n+1)y_j - y_j - n\bar{X}_j \right) \right\vert, \\
	&= \frac{n^2}{(n+1)^2} \left\vert (y_i - \bar{X}_i)(y_j - \bar{X}_j)\right\vert, \\
	&\le \frac{n^2}{(n+1)^2} (M_i - m_i)(M_j - m_j).
\end{align*}
\end{proof}

\section{Neighboring Definition: Add/Drop One}
\subsection{$\ell_1$-sensitivity}

\begin{theorem}
Let $X$ be a matrix of values and let $X_i$ indicate the $i^{\text{th}}$ column of the matrix. Let
$$ f_{ij} (X)= \sum_{k=1}^n (x_{ki} - \bar{X}_i)(x_{kj} - \bar{X}_j).$$
Say that the space of datapoints $\mathcal{X}_i$ that $X_i$ is drawn from is bounded above by $M_i$ and bounded below by $m_i$. Then the $\ell_1$-sensitivity in the add/drop-one model of $f(\cdot)$ is bounded above by
 $$ \frac{n}{(n+1)}  (M_i - m_i)(M_j - m_j).$$
\end{theorem}

\begin{proof}

We must consider both adding and removing a row from $X$.

Adding a row:\\
Let $X'_i = X_i \cup \{y_i\}$. Then, from Lemma \ref{cov:rewrite},
\begin{align}
\label{eq:addone}
\left\vert f_{ij}(X') - f_{ij}(X) \right\vert &= \left\vert n(\bar{X}_i - \bar{X}_i')(\bar{X}_j - \bar{X}_j') + (y_i - \bar{X}_i)(y_j - \bar{X}_j) \right\vert \hspace{1cm} \nonumber\\
	&\le  n \left\vert (\bar{X}_i - \bar{X}_i')(\bar{X}_j - \bar{X}_j') \right\vert + \left\vert (y_i - \bar{X}_i)(y_j - \bar{X}_j) \right\vert \nonumber\\
	&\le \frac{n}{(n+1)^2} (M_i - m_i)(M_j - m_j) + \frac{n^2}{(n+1)^2} (M_i - m_i)(M_j - m_j) \nonumber	\\
	& \hspace{8cm} \text{(By Lemmas \ref{lemma:term1bound} and \ref{lemma:term2bound})} \nonumber\\
	&= \frac{n}{n+1}  (M_i - m_i)(M_j - m_j).
\end{align}

Removing a row:\\
Let $Y$ be the last row of $X$, and let $X_i'  = X_i \setminus \{y_i\}$. Note that Lemma \ref{cov:rewrite} can be rewritten in this setting by parametrizing $n$ as $n-1$ and swapping X and X' in its expression:

$$ f_{ij}(X) = f_{ij}(X') + (n-1)(\bar{X}_i' - \bar{X}_i)(\bar{X}_j' - \bar{X}_j) + (y_i - \bar{X}_i')(y_j - \bar{X}_j'). $$

Lemmas \ref{lemma:term1bound} and \ref{lemma:term2bound} may be rewritten with the same reparametrization:
$$ (n-1)\left\vert (\bar{X}_i' - \bar{X}_i)(\bar{X}_j' - \bar{X}_j) \right\vert \le \frac{n-1}{n^2} (M_i - m_i)(M_j - m_j),$$
and
$$ \left\vert (y_i - \bar{X}_i')(y_j - \bar{X}_j') \right\vert \le \frac{(n-1)^2}{n^2}(M_i - m_i)(M_j - m_j).$$
Then,
\begin{align}
\label{eq:subone}
\left\vert f_{ij}(X) - f_{ij}(X') \right\vert &= \left\vert (n-1)(\bar{X}_i' - \bar{X}_i)(\bar{X}_j' - \bar{X}_j) + (y_i - \bar{X}_i')(y_j - \bar{X}_j') \right\vert, \nonumber\\
	&\le  \left\vert (n-1)(\bar{X}_i' - \bar{X}_i)(\bar{X}_j' - \bar{X}_j) \right\vert + \left\vert(y_i - \bar{X}_i')(y_j - \bar{X}_j') \right\vert, \nonumber\\
	&\le \frac{n-1}{n^2} (M_i - m_i)(M_j - m_j) + \frac{(n-1)^2}{n^2}(M_i - m_i)(M_j - m_j), \nonumber\\
	&= \frac{n-1}{n} (M_i - m_i)(M_j - m_j).
\end{align}

Note that for any $n \ge 1$,
	\begin{equation}
	\label{ineq}
	 \frac{n}{n + 1} > \frac{n-1}{n}.
	\end{equation}

So, the worst-case bound always occurs in the ``add-one'' case, and in general the  $\ell_1$ sensitivity of $f(\cdot)$ is bounded by
 $$ \frac{n}{n+1}  (M_i - m_i)(M_j - m_j).$$
\end{proof}

\begin{corollary}
\label{cor:renorm}
Let $X \leftarrow \mathcal{X}$ where $\mathcal{X}_i$ is bounded above by $M_i$ and bounded below by $m_i$. Then the $\ell_1$-sensitivity in the add/drop-one model of the $ij^{\text{th}}$ element of the covariance matrix for $X$ is bounded above by
$$ \frac{1}{n+1}(M_i - m_i)(M_j - m_j).$$
\end{corollary}

\begin{proof}
Note that the $ij^{\text{th}}$ element of the covariance matrix for $X$ is equal to $f(x)/n$.
\end{proof}

\begin{corollary}
\label{cor:renorm}
Let $X \leftarrow \mathcal{X}$ where $\mathcal{X}_i$ is bounded above by $M_i$ and bounded below by $m_i$. Then the $\ell_1$-sensitivity in the add/drop-one model of the $ij^{\text{th}}$ element of a sample covariance matrix for $X$ is bounded above by
$$ \frac{n}{n^2-1}(M_i - m_i)(M_j - m_j).$$
\end{corollary}

\begin{proof}
Note that the $ij^{\text{th}}$ element of the sample covariance of $X$ is equal to $f(x)/(n-1)$, and that $(n-1)(n+1) = n^2 -1$.
\end{proof}

\subsection{$\ell_2$-sensitivity}

\begin{theorem}
Let $X \leftarrow \mathcal{X}$ where $\mathcal{X}_i$ is bounded above by $M_i$ and bounded below by $m_i$. Then the $\ell_2$-sensitivity in the add/drop-one model of the $ij^{\text{th}}$ element of the covariance matrix for $X$ is bounded above by
$$ \frac{1}{n+1}(M_i - m_i)(M_j - m_j).$$
\end{theorem}

\begin{proof}
% note you can also do this directly by squaring the bounds and bounding the central term. I can add that in but it's just a lot of cluttered arithmetic.
This follows from the bounds in Equations \ref{eq:addone} and \ref{eq:subone} and the inequality in Equation \ref{ineq}, and a renormalization by $n$ from the definition of covariance.
\end{proof}

\begin{corollary}
\label{thm:l2addsub}
Let $X \leftarrow \mathcal{X}$ where $\mathcal{X}_i$ is bounded above by $M_i$ and bounded below by $m_i$. Then the $\ell_2$-sensitivity in the add/drop-one model of the $ij^{\text{th}}$ element of a sample covariance matrix for $X$ is bounded above by
$$\frac{n}{n^2-1}(M_i - m_i)(M_j - m_j).$$
\end{corollary}

\begin{proof}
The logic here is identical to the proof of Theorem \ref{thm:l2addsub}, with a renormalization by $n$ rather than by $n-1$.
\end{proof}

\section{Neighboring Definition: Change One}
\subsection{$\ell_1$-sensitivity}

\begin{theorem}
\label{thm:l1change1}
Let $X$ be a matrix of values and let $X_i$ indicate the $i^{\text{th}}$ column of the matrix. Let
$$ f_{ij} (X)= \sum_{k=1}^n (x_{ki} - \bar{X}_i)(x_{kj} - \bar{X}_j).$$
Say that the space of datapoints $\mathcal{X}_i$ that $X_i$ is drawn from is bounded above by $M_i$ and bounded below by $m_i$. Then the $\ell_1$-sensitivity in the change-one model of $f(\cdot)$ is bounded above by
$$ \frac{2(n-1)}{n}  (M_i - m_i)(M_j - m_j).$$
\end{theorem}

\begin{proof}
Recall from Equation \ref{eq:addone} that
$$ \left\vert f_{ij}(X) - f_{ij}(X') \right\vert \le \frac{n}{(n+1)}  (M_i - m_i)(M_j - m_j).$$
and
$$ \left\vert f_{ij}(X) - f_{ij}(X'') \right\vert \le \frac{n}{(n+1)}  (M_i - m_i)(M_j - m_j).$$
Reparametrizing these equations so that $n$ is the size of $X'$ and $X''$ gives that
$$ \left\vert f_{ij}(X) - f_{ij}(X') \right\vert \le \frac{n-1}{n}  (M_i - m_i)(M_j - m_j).$$
and
$$ \left\vert f_{ij}(X) - f_{ij}(X'') \right\vert \le \frac{n-1}{n}  (M_i - m_i)(M_j - m_j).$$
It then follows from the triangle inequality that
$$ \left\vert f_{ij}(X') - f_{ij}(X'') \right\vert \le \frac{2(n-1)}{n}  (M_i - m_i)(M_j - m_j).$$
\end{proof}

\begin{corollary}
The $\ell_1$-sensitivity in the change-one model of covariance is bounded above by
$$\frac{2(n-1)}{n^2}  (M_i - m_i)(M_j - m_j).$$
\end{corollary}

\begin{proof}
Note that the $ij^{\text{th}}$ element of the covariance of $X$ is equal to $f(x)/n$.
\end{proof}

\begin{corollary}
\label{cor:renorm2}
The $\ell_1$-sensitivity in the change-one model of sample covariance is bounded above by
$$\frac{2}{n}  (M_i - m_i)(M_j - m_j).$$
\end{corollary}

\begin{proof}
Note that the $ij^{\text{th}}$ element of the sample covariance of $X$ is equal to $f(x)/(n-1)$.
\end{proof}

\subsection{$\ell_2$-sensitivity}

\begin{theorem}
\label{thm:l2change}
The $\ell_2$-sensitivity in the change-one model of covariance is bounded above by
$$\frac{2(n-1)}{n^2}  (M_i - m_i)(M_j - m_j).$$
\end{theorem}

\begin{proof}
This follows from the bounds in the proof of Theorem \ref{thm:l1change1} and a renormalization by $n$.
\end{proof}

\begin{corollary}
The $\ell_2$-sensitivity in the change-one model of sample covariance is bounded above by
$$\frac{2}{n}  (M_i - m_i)(M_j - m_j).$$
\end{corollary}

\begin{proof}
The logic here is identical to the proof of Theorem \ref{thm:l2change}, with a renormalization by $n-1$ rather than by $n$.
\end{proof}

\bibliographystyle{alpha}
\bibliography{mean}

\end{document}


%!TEX root = ../paper.tex

\subsection{Clustering}
\label{sec:clustering}

After extracting all features and vectorising them for each blog post in our data set, we group them into clusters according to the similarity of their feature vectors.
Thus, the blog posts are grouped by their writing style into clusters.
One cluster represents one writing style, so that one cluster contains only blog posts with similar writing styles.

To this end, we use a \textit{k-means} algorithm, which is a method from vector quantization.
\textit{K-means} is one of the most popular and simplest clustering algorithms.
Using a \textit{k-means} algorithm the number of resulting clusters can be configured easily.

To run a \textit{k-means} algorithm, the value $k$ has to be set beforehand.
$K$ represents the number of clusters and thus the number of similar writing styles, which should be determined.
The value of $k$ can differ depending on the use case.
If $k$ is set to the number of authors, one cluster, i.e., writing style group, should correspond to one author.
So one cluster would contain only blog posts of one author.
Choosing a value much lower than the actual number of authors for $k$, would allow us to find similar authors.
In this case, one cluster would contain blog posts from multiple authors having a similar writing style.

\subsection{Cluster Labelling}
\label{sec:cluster_labeling}

Labelling the resulting clusters helps the user to distinguish between them.
The labels indicate what kind of blog posts are in those clusters, so that the user gets an idea what to expect from blog posts in this cluster.

A lot of current research in the field of cluster labelling focuses on topic-dependent labels.
Because we want to cluster by writing style rather than topic, we were unable to use those techniques.
However, trying to find universally accepted writing styles leads to labels, such as ``informative'' or ``opinion piece'', which merely represent the text's intention, not the author's personal style~\cite{lee2001genres}.
Thus, these types of labels were also not applicable for our use case.

Therefore, we implemented our own cluster labelling methods, which consider the most important features of a cluster.
They will be explained using the following example:
The current feature is \textit{blog post length} and we have three clusters with the following values for the feature:
\begin{center}
\begin{tabular}{c|c|c}
  Cluster 1 & Cluster 2 & Cluster 3 \\ \hline
  0.7 & 1 & 0.1 \\
 \end{tabular}
\end{center}

\paragraph{Method 1}
For each feature and cluster, the highest and lowest value is determined.
Those clusters are then given the appropriate labels.

In the example cluster 2 has the highest value and obtains the label ``longest blog posts''.
The label ``shortest blog posts'' is assigned to cluster 3, because this cluster has the lowest value.

Using this method only the most significant labels are set.
But a cluster might not obtain any label if it does not have any features with extreme values.

\paragraph{Method 2}
First, the average vector of all blog posts is calculated.
In a second step, the distance for each feature and cluster to that average vector is determined.
If the distance is over or below a certain threshold, the cluster is labelled.

The example is shown in Figure~\ref{fig:cluster_labeling_2}.
Cluster 3 is not over or below the threshold, thus the length of the blog post is not significant for the cluster, so no label is assigned to that cluster.
Cluster 1 and 2 are both over the threshold and obtain the label ``long blog posts''.

This method assigns more labels to clusters than method 1, but can also lead to clusters without any labels if all features of a cluster have average values.

\begin{figure}[ht!]
	\centering
	\includegraphics[width=0.6\textwidth]{images/cluster_labeling_2.pdf}
	\caption{Cluster labelling method 2: The distance of each cluster to the overall average blog posts is calculated. If the distance is over or below a certain threshold (shaded in grey), the cluster obtains a label. In this example a label is assigned to cluster 1 and 2.}
	\label{fig:cluster_labeling_2}
\end{figure}

\paragraph{Method 3}
Similarly to method 2, we look at each feature and discern the average for that feature.
We then calculate the difference between this average and the average value of every cluster.
However, we also normalize it to the total range this feature has for our data.
Then, we pick the $n$ most significant of these differences for each cluster and use them to label it.
Significance, in this case, is mostly determined by how great the deviation from the average is: The larger the deviation the higher the significance.
The only exception to this is that being close to the average is considered slightly more significant than being fairly close to it.

In our example, clusters 2 and 3 would most likely obtain the label ``longest'' or ``shortest blog posts'' respectively.
Cluster 1 on the other hand would be eligible for the low significance label ``average length blog posts'' but might not receive it if it has $n$ more significant labels.

With this method, every cluster is guaranteed to obtain a set number of labels, even if it has no features that deviate from the average.


\documentclass[twocolumn]{article}

\usepackage{graphicx}
\usepackage{subfigure}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage[usenames,dvipsnames]{color} % good support for colors
\usepackage{pstricks}

\def\registered{\textsuperscript{\textregistered}}
\def\copyright{\textsuperscript{\textcopyright}}
\def\trademark{\textsuperscript{\texttrademark}}

\newrgbcolor{kennycolor}{.1 .1 .7}
\newcommand{\kenny}[1]{  { \bf \kennycolor Kenny:  #1}}

\renewcommand{\vec}[1]{ \ensuremath{\mathbf{#1} } }
\newcommand{\abs}[1]{\left| {#1} \right|}
\newcommand{\quat}[1]{ #1 }
\newcommand{\mat}[1]{\ensuremath{\mathbf{#1} }}
\newcommand{\norm}[1]{\parallel {#1} \parallel}
\renewcommand{\Re}{ \mathbb{R} }
\newcommand{\set}[1]{\mathcal{#1}}
\newcommand{\identity}{ \ensuremath{ \mat I }}
\newcommand{\bigO}{ \ensuremath{ \mathcal{O} }}
\newcommand{\atan }{ \ensuremath{ \phantom{\cdot}\text{atan}_2 } }
\newcommand{\acos }{ \ensuremath{ \cos^{-1} } }
\newcommand{\asin }{ \ensuremath{ \sin^{-1} } }
\newcommand{\logand }{ \ensuremath{ \wedge } }
\newcommand{\logor }{ \ensuremath{ \vee } }
\newcommand{\degree}{\,^{\circ}}
\renewcommand{\th}{ \ensuremath{ ^{\text{th}}} }
\newcommand{\sgn }[1]{ \ensuremath{ \text{sgn}\left( #1 \right) } }
\newcommand{\union}{\ensuremath{ \cup }}
\newcommand{\intersection}{\ensuremath{ \cap }}
\newcommand{\hull }[1]{ \ensuremath{ \text{convex hull}\left( #1 \right) } }
\newcommand{\proj }[1]{ \ensuremath{ \text{proj}\left( #1 \right) } }
\newcommand{\trace }[1]{ \ensuremath{ \text{tr}\left( #1 \right) } }
\newcommand{\inertia}{\ensuremath{\mathcal{I} }}
\newcommand{\mass}{\ensuremath{\mathcal{M} }}

\title{Hints for using the MASS Library}
\author{Kenny Erleben}
\date{December 2010}

\begin{document}

\maketitle

\begin{abstract}
  The MASS library was created to provide a functional toolbox for handling mass
  properties of rigid bodies. This includes functions for computing the total
  mass of an object $\mass$ the center of mass position $\vec r$ and the inertia
  tensor $\inertia$. These values depend on the chosen frame of reference. To
  achieve a correct physical simulation it is important to apply the correct
  mass values. In this short paper we will address the issues in computing the
  values of mass properties of rigid bodies and compounds hereof. Our
  contribution includes a description of how to deal with initialization and the
  visualization synchronizations of rigid bodies and the construction of
  compound bodies.
\end{abstract}

\section*{Creation of a Rigid Body}
When creating content in rigid body simulators several different coordinate
frames are used. We will work with three different coordinate frames
\begin{description}
\item[The world frame] is the world coordinate frame wherein everything can be
  absolutely placed.
\item[The body frame ] refers to the coordinate frame with its origin at the
  center of mass of a given rigid body and the orientation of the frame chosen
  such that the inertia tensor of the given body is a constant and diagonal
  tensor.
\item[The model frame] is some convenient frame used to describe the geometry
  in. The frame is fixed in the sense that it always will follow the same motion
  as the geometry.
\end{description}
The model frame needs not be physically founded, but can in principle be defined
anywhere where it make sense from an artistic or modelling viewpoint. From a
modelling viewpoint one would define the geometry in some model reference frame
and then place the model frames in the world frame. However, when connecting to
a rigid body simulator one must account find the body frame as this is the one
the simualtor works with. This leads to the following steps of actions
\begin{enumerate}
\item Compute model frame values $\mass$, $\vec r_M$, and $\inertia_M$
\item Transform from model frame values into the body frame values $\inertia_B$.
\item Given model frame placement in the world $\vec r$ and $\mat R$ compute the
  body frame placement in the world $\vec r_B$ and $\mat R_B$.
\end{enumerate}
The MASS library provides routines for the first step. For the second step one
would have to first transform the reference point to the center of mass position
using the parallel-axis theorem ($\textbf{p-a-t}$)\footnote{See
  $mass::translate_inertia$ function},
\begin{equation}
  \inertia^\prime = \textbf{p-a-t}(\inertia_M, - \vec   r_M) .
\end{equation}
Next we can apply eigen-value-decomposition to find the body frame
values\footnote{See $mass::compute_orientation$ function},
\begin{equation}
  \inertia_M  = \mat R_M \inertia_B \mat R_M^T.
\end{equation}
Now given the model frame placement in the world frame we compute,
\begin{subequations}
  \begin{align}
    \vec r_B &= \vec r - \mat R \vec r_M, \\
    \mat R_B &= \mat R \mat R_M.
  \end{align}
\end{subequations}
There is one more subtlety in the creation process. The rigid body simulator
must know where its geometry is relative to its body frame. Thus, one should
apply the transformation from model frame to body frame onto the geometry. That
is first translate by $- \vec r_M$ and then rotation by $\mat
R_M^{-1}$\footnote{Some simultors allow one to specify these transformations
  directly. Other simulators assume geometry is living in body space in which
  case one actually has to transform the geometry.}.

\section*{Visualization Update of a Rigid Body}\label{sec:visualization-update}
During simulation a rigid body simulator will compute new values of $\vec r_B$
and $\mat R_B$. However, for the visualization one needs to find the new
placement of the model frame in the world. Thus one must solve
\begin{subequations}
  \begin{align}
    \vec r  &= \vec r_B + \mat R \vec r_M, \\
    \mat R &= \mat R_B \mat R_M^{-1}.
  \end{align}
\end{subequations}
Thus, one must always store the body frame to model frame transformation given
by $\vec r_M$ and $\mat R_M$.

\section*{The Curious Parallel Axis Theorem}\label{sec:curi-parall-axis}
One has to be carefull when applying the parallel axis theorem. By its
definition it transforms a body frame inertia tensors $\inertia$ into a modified
tensor $\inertia^\prime$
having the same orientation but a different reference point given by the
translation $\vec d$
\begin{subequations}
  \begin{align}
    \inertia_{\alpha \alpha}^\prime
    &=
    \inertia_{\alpha \alpha} + \mass (\vec d_{\beta}^2  + \vec d_{\gamma}^2)\\
    \inertia_{\alpha \beta}^\prime &=  \inertia_{\alpha \beta} - \mass (\vec
    d_{\alpha} \vec d_{\beta})
  \end{align}
\end{subequations}
Here the primed quantities would be the modified inertia tensor that no longer
lives in the body space frame. The translation given here is the vector from the
body space frame origin to the new origin of the new model frame.

However, here comes the tricky part say one which to do another translation to
get $\inertia^{\prime\prime}$ then one can not just apply the above formula to
$\inertia^\prime$. Instead one must first transform $\inertia^\prime$ back to the
body space frame using
\begin{subequations}
  \begin{align}
    \inertia_{\alpha \alpha}
    &=
    \inertia_{\alpha \alpha}^\prime - \mass (\vec d_{\beta}^2  + \vec d_{\gamma}^2)
    \\
    \inertia_{\alpha \beta}
    &=
    \inertia_{\alpha \beta}^\prime + \mass (\vec d_{\alpha} \vec d_{\beta})
  \end{align}
\end{subequations}
and first then may one apply the transform taking one from the body space
inertia tensor into the $\inertia^{\prime\prime}$ tensor.

\section*{Creating Compounds}\label{sec:creating-compounds}
As mass properties are defined by volume integrals it is easy to see that all we
need to do is to make sure all properties are given with respect to the same
reference frame. When this is the case we can simply sum up all the mass
properties.

Here we will give an example of two rigid bodies their body frame inertia
tensors are given by the constant diagonal tensors $\inertia_A$ and
$\inertia_B$. The center of mass positions are given by $\vec r_A$ and $\vec
r_B$ and the orientatio of the body frames wrt. the world frame is given by
$\mat R_A$ and $\mat R_B$.

In our first step we will transform inertia tensors into the world frame
\begin{subequations}
\begin{align}
  \inertia^{\prime}_A &\leftarrow \mat R_A \inertia_A \mat R_A^T,\\
  \inertia^{\prime}_B &\leftarrow \mat R_B \inertia_B \mat R_B^T,\\
  \inertia^{\prime^\prime}_A &\leftarrow \textbf{p-a-t}(\inertia_A^\prime,  \vec   r_A) ,\\
  \inertia^{\prime^\prime}_B &\leftarrow \textbf{p-a-t}(\inertia_B^\prime, \vec
  r_B) .
\end{align}
\end{subequations}
Next we may find the compund inertia tensor with reference to the world frame
\begin{equation}
  \inertia_C^\prime = \inertia^{\prime^\prime}_A + \inertia^{\prime^\prime}_B
\end{equation}
The total mass is simply
\begin{equation}
  \mass_C = \mass_A + \mass_B
\end{equation}
and the center of mass position is
\begin{equation}
  \vec r_C =  \frac{\mass_A \vec r_A + \mass_B \vec r_B}{ \mass_C}
\end{equation}
What remains is to find the body frame inertia tensor
\begin{subequations}
  \begin{align}
    \inertia_C^{\prime\prime} &\leftarrow \textbf{p-a-t}(\inertia_C^\prime, - \vec   r_C) .\\
    \mat R_C \inertia_C \mat R_C^T  &\leftarrow \inertia_C^{\prime\prime}
  \end{align}
\end{subequations}
The recipe can be incremental extended straigthforwardly. Observe that there is
one more snag as with the creation of rigid bodies one must ensure that
geometries in the rigid body simulator is given writh respect to the new
compound bodies body frame.

\section*{Handling a Deformed Box}\label{sec:handl-deform-box}
Imagine we are given a deformed box shape. We assume that the deformation can be
specified by some linear coordinate transformation and write thie mathematically
as
\begin{equation*}
  \begin{bmatrix}
    x\\
    y\\
    z
  \end{bmatrix}
  =
  \mat \Phi
  \begin{bmatrix}
    X\\
    Y\\
    Z
  \end{bmatrix}
\end{equation*}
where the linear transformation is given by the matrix $\mat A$ that maps
coordinates the $(X, Y, Z)$ from a regular unit box with its center placed at
the origin into the deformed coordinates $(x, y, z)$. Here we assume that the
mapping is bijective and thus $\mat  \Phi$ must be invertible implying we
can find the inverse mapping given by $\mat \Phi^{-1}$

We now wish to find closed form solutions for the mass properties of the
deformed box. That is we wish to solve the volume integrals
\begin{subequations}
  \begin{align*}
    \mass &= \int_v  \rho   dv \\
    \vec r_x &= \frac{1}{\mass}\int_v  \rho \vec x  dv \\
    \vec r_y &= \frac{1}{\mass}\int_v  \rho \vec y  dv \\
    \vec r_z &= \frac{1}{\mass}\int_v  \rho \vec z  dv \\
    \inertia_{xx} &= \int_v  \rho \left( y^2 + z^2 \right)  dv \\
    \inertia_{yy} &= \int_v  \rho \left( x^2 + y^2 \right)  dv \\
    \inertia_{zz} &= \int_v  \rho \left( x^2 + y^2 \right)  dv \\
    \inertia_{xy} &= - \int_v  \rho \left( x y \right)  dv \\
    \inertia_{xz} &= - \int_v  \rho \left( x z\right)  dv \\
    \inertia_{yz} &= - \int_v  \rho \left( y z \right)  dv
  \end{align*}
\end{subequations}
Our approach to finding the closed form solutions we seek is to rewrite the
volume integrals such that we are integrating over the undeformed volume. The
machinery is pretty much the same for all equations so we will here just treat
one term in detail and leave the remaining terms for the reader.

First we will make a change of variables using the formula $dv = j dV$ where $j
= \det(\mat  \Phi)$
\begin{subequations}
  \begin{align*}
    \inertia_{xx} &= \int_v  \rho \left( y^2 + z^2 \right)  dv \\
    \inertia_{xx} &= j \rho \int_V   \left( y^2 + z^2 \right)  dV
  \end{align*}
\end{subequations}
Secondly we observe that the deformed coordinates are a linear mapping of
undeformed coordinates that means
\begin{subequations}
  \begin{align*}
    x  &=  \Phi_{11} X + \Phi_{12} Y + \Phi_{13} Z\\
    y  &=  \Phi_{21} X + \Phi_{22} Y + \Phi_{23} Z\\
    z  &=  \Phi_{31} X + \Phi_{32} Y + \Phi_{33} Z
  \end{align*}
\end{subequations}
using this we have that
\begin{equation*}
  \begin{split}
    y^2 + z^2
    =
    \left(\Phi_{21} X + \Phi_{22} Y + \Phi_{23} Z\right)^2
    \quad \quad \quad \quad \quad
    \\
    +
    \left(\Phi_{31} X + \Phi_{32} Y    + \Phi_{33} Z\right)^2
    =
    P_2(X,Y,Z)
  \end{split}
\end{equation*}
where $P_2$ denotes a general second order polynomial in $X$, $Y$ and $Z$. So
now the volume integral reads
\begin{subequations}
  \begin{align*}
    \inertia_{xx}
    &=
    j \rho \int_V   P_2(X,Y,Z)  dV\\
    &=
    j \rho
    \int_{-\frac{1}{2}}^{\frac{1}{2}}
    \int_{-\frac{1}{2}}^{\frac{1}{2}}
    \int_{-\frac{1}{2}}^{\frac{1}{2}}
    P_2(X,Y,Z)
    dX
    dY
    dZ
  \end{align*}
\end{subequations}
Which is straightforward to solve for a closed form solution. All that are
missing in order to do this is formulas for the coefficients of $P_2$ in the
given integral these are
\begin{subequations}
  \begin{align*}
    P_2(X,Y,Z)
    &= \underbrace{ \left (\Phi_{21}^2 + \Phi_{31}^2\right) }_{a_{XX}} X^2 \\
    &+ \underbrace{ \left (\Phi_{22}^2 + \Phi_{32}^2\right) }_{a_{YY}}  Y^2 \\
    &+ \underbrace{ \left (\Phi_{23}^2 + \Phi_{33}^2\right) }_{a_{ZZ}}  Z^2\\
    &+ \underbrace{ 2 \left ( \Phi_{21} \Phi_{22} + \Phi_{31} \Phi_{32} \right)
    }_{a_{XY}}  X Y \\
    &+ \underbrace{ 2 \left ( \Phi_{21} \Phi_{23} + \Phi_{31} \Phi_{33} \right)
    }_{a_{XZ}}  X Z \\
    &+ \underbrace{ 2 \left(\Phi_{22} \Phi_{23} + \Phi_{32} \Phi_{33}\right) }_{a_{YZ}} Y Z
  \end{align*}
\end{subequations}
From this we find
\begin{subequations}
  \begin{align*}
    \inertia_{xx}
    =
    j \rho
    \begin{bmatrix}
      \begin{matrix}
        \frac{a_{XX}}{3} X^3 Y Z +\frac{a_{yy}}{3} X Y^3 Z +\\
        \quad \frac{a_{zz}}{3} X Y Z^3 +  \frac{a_{XY}}{4} X^2 Y^2  Z +\\
        \quad \quad         \frac{a_{XZ}}{4} X^2 Y  Z^2 + \frac{a_{YZ}}{4} X Y^2  Z^2
      \end{matrix}
    \end{bmatrix}_{ (-\frac{1}{2}, -\frac{1}{2}, -\frac{1}{2} ) } ^{ (
      \frac{1}{2}, \frac{1}{2}, \frac{1}{2})  }
  \end{align*}
\end{subequations}
From this we can generalize the recipe for deriving a closed form solution for
all the volume intergrals. The other principal moments of inertia will be
permutations of the above derivation. The products of moments are all similar
and will also result in integrals of second order polynomials. The center of
mass integrals all results in integrals of linear polynomials and the mass
volume integral is straightforward.

\bibliographystyle{plain}
\bibliography{references}

\end{document}

%%% Local Variables:
%%% mode: pdflatex
%%% TeX-master: t
%%% End:


\chapter{Lip Reed}\label{ch:lipreed}
The dynamics of wind instruments can be modelled by acoustic tubes as presented in Chapter 5. Excitation of these instruments happens either by blowing a jet of air across an opening, such as in a flute, or by the buzzing of a \textit{reed}. In \cite{Fletcher1998}, the authors state that all wind-instrument reeds fall into one of three categories: the single reed, (clarinet, saxophone), the double reed (oboe, bassoon) and the lip reed (trumpet, trombone). The latter will be the focus of this chapter.

% Brass instruments are excited by the lips of the player. The opening and closing of the lips causes a vibration
Sections \ref{sec:webstersExcitation} and \ref{sec:pulseTrain} presented a physically inspired pulse train that attempts to model the opening and closing of the lips using a clipped sinusoidal signal. A more physical approach, which is bidirectional, is to model the lips as a mass-spring-damper system that interacts with the left boundary of the tube. The literature describes lip reed models with varying degrees of freedom (DoF) (see \cite{Fletcher1998,Harrison2018} for an overview). Recent work includes vortex-induced vibration into the lip reed model that allows for the buzzing of the lips without the need of an acoustic tube [\hyperref[ch:listOfPublications]{S1}].

As the contribution of this project to a brass instrument model was mainly focused on the resonator, the simple `outward striking door model' was chosen, which is a simple single one-DoF mass-spring-damper system. The model is as presented in \cite{Bilbao2009Reed} excluding the collision. An alternative collision model was added in paper \citeP[H] and will be elaborated on in Chapter \ref{ch:trombone}.\todo{check}

This chapter starts by introducing the mass-spring-damper system, after which the lip reed model will be given in continuous and discrete time. The lip reed will be coupled to the first-order system of equations presented in \ref{sec:firstOrderSystem}. Unless denoted otherwise, this chapter follows \cite{Harrison2018}.

\section{Mass-spring systems revisited: Damping}\label{sec:massSpringDamping}
Before moving on to the lip reed system, the mass-spring system given in Section \ref{sec:massSpringSystem} will be extended to contain damping.

Recall the mass spring system presented in Eq. \eqref{eq:massSpringPDE}, where $u=u(t)$ is the displacement of the mass from its equilibrium position (in m). Damping can be easily be added to yield a mass-spring-damper system as follows:
\begin{equation}\label{eq:massSpringDampingPDE}
    M\ddot u = -Ku - R\dot u,
\end{equation}
with mass $M$ (in kg), spring constant $K$ (N/m) and damping coefficient $R$ (in kg/s). Figure \ref{fig:massSpringDamper} shows the behaviour of the system for different values of $R$.

\def\figWidth{0.32}
\begin{figure}[b]
    \centering
    \subfloat[$R=0$.\label{fig:massSpringDamper1}]{\includegraphics[width=\figWidth\textwidth]{figures/exciters/lipreed/massSpringDamper1.eps}}\hfill
    \subfloat[$R=50$.\label{fig:massSpringDamper2}]{\includegraphics[width=\figWidth\textwidth]{figures/exciters/lipreed/massSpringDamper2.eps}}\hfill
    \subfloat[$R=200$.\label{fig:massSpringDamper3}]{\includegraphics[width=\figWidth\textwidth]{figures/exciters/lipreed/massSpringDamper3.eps}}
    \caption{The mass-spring-damper system in Eq. \eqref{eq:massSpringDampingPDE} with $f_0=440$ Hz for different values of $R$. \label{fig:massSpringDamper}}
\end{figure}

Equation \eqref{eq:massSpringDampingPDE} can then be discretised to the following FD scheme:
\begin{equation}\label{eq:massSpringDampingFDS}
    M\dtt \un = -K\un - R\dtd \un.
\end{equation}
Expanding and solving for $u^{n+1}$ yields the following update equation (before division with the term multiplied onto $u^{n+1}$):
\begin{equation}\label{eq:massSpringDampingUpdate}
    \left(1+\frac{Rk}{2M}\right)u^{n+1} = 2 \un - u^{n-1} - \frac{Kk^2}{M}\un + \frac{Rk}{2M}u^{n-1}.
\end{equation}

\subsection{Energy analysis}
Following Section \ref{sec:energyAnalysis} (without explicitly following the steps for brevity), one can obtain the energy of Eq. \eqref{eq:massSpringDampingFDS} through a multiplication of the scheme by $(\dtd \un)$ to get
\begin{equation}\label{eq:massSpringDampingPreEnergyBalance}
    M(\dtt \un)(\dtd \un) = -K(\dtd \un)\un - R(\dtd \un)^2.
\end{equation}
As there is damping present in the system, the energy balance will be of the form
\begin{equation*}
    \dtp \h = -\q.
\end{equation*}
Using identities \eqref{eq:prodIdentity1} and \eqref{eq:prodIdentity2}, $\h$ and $\q$ can be obtained from Eq. \eqref{eq:massSpringDampingPreEnergyBalance}
\begin{equation}\label{eq:energyBalanceMassSpringDamper}
    \h = \t + \v, \qwiq
    \t = \frac{M}{2}(\dtm\un)^2, \qaq \v = \frac{K}{2}\un e_{t-}\un.
\end{equation}
and
\begin{equation}\label{eq:massDampingEnergy}
    \q = R(\dtd \un)^2.
\end{equation}

Figure \ref{fig:massSpringDamperEnergy} shows the energy output of the mass spring damper system with $R = 50$ and $f_0 = 2\pi\sqrt{K/M} = 440$ Hz. One can observe that the damping term causes the system to lose energy when the mass is in motion (high kinetic energy).
\begin{figure}[h]
    \centering
    \begin{tikzpicture}[->,node distance=3cm,
        thick,main node/.style={circle,draw}]

        \node[] (image) at (0,0) {
        \includegraphics[width=\textwidth]{figures/exciters/lipreed/massSpringDamperEnergy.eps}
        };

        \node[] (he) at (0.2,0.5) {\small $\mathfrak{h}_\text{e}$};

        \node[] (h) at (-5.8, 1) {\small $\mathfrak{h}$};
        \node[] (v) at (-5.8, 0.5) {\small $\color{red}\mathfrak{v}$};
        \node[] (t) at (-5.8, 0) {\small $\color{blue}\mathfrak{t}$};
      \end{tikzpicture}
      \caption{The potential (red), kinetic (blue), and total (black) energy of the mass-spring-damper system. The right panel shows the normalised energy (according to Eq. \eqref{eq:normalisedEnergyDamping}) and shows that the deviation of the energy is within machine precision. \label{fig:massSpringDamperEnergy}}
\end{figure}

\section{Continuous time}\label{sec:lipreedContinuous}
As mentioned at the beginning of this chapter, the lip reed will be modelled as a mass-spring-damper system as in Eq. \eqref{eq:massSpringDampingPDE}. The system will be coupled to an acoustic tube described by the first-order system of PDEs described in Section \ref{sec:firstOrderSystem}, Eq. \eqref{eq:firstOrderSystem}.
% :
% \begin{subequations}\label{eq:firstOrderSystemLipReedCh}
%     \begin{align}
%         \frac{S}{\rho_0 c^2}\partial_t p &= -\partial_x(Sv),\label{eq:contPressureLipReedCh}\\
%         \rho_0\partial_tv &= -\partial_xp\label{eq:discVelocityLipReedCh},
%     \end{align}
% \end{subequations} %An additional term  due to the pressure difference between the mouth and the tube is added to the equation and t

Using dots to denote derivatives with respect to time $t$, the PDE of the lip reed connected to an acoustic tube is defined as
\begin{equation}\label{eq:lipReedDimensional}
    M\ddot y = -K y - R \dot y + S_\text{r}\Delta p.
\end{equation}
with displacement of the lip reed from equilibrium $y = y(t)$ (in m), mass of the lip reed $M > 0$ (in kg), lip stiffness $K\geq 0$ (in N/m), damping coefficient $R\geq 0$ (in kg/s), and effective surface area of the lip $S_\text{r}\geq 0$ (in m$^2$). Furthermore,
\begin{equation}\label{eq:deltaP}
    \Delta p = \Delta p(t) = P_\text{m} - p(0,t)
\end{equation}
is the difference between the pressure in the mouth $P_\text{m} = P_\text{m}(t)$ and the pressure at the left boundary of the acoustic tube $p(0,t)$ (all in Pa). The acoustic tube can be described by the first-order system presented in Section \ref{sec:firstOrderSystem}. See Figure \ref{fig:lipSystem} for a schematic representation of the lip reed.
\input{exciters/lipReedSystemFig.tex}

The pressure difference in Eq. \eqref{eq:deltaP} causes a volume flow velocity (in  m$^3$/s) and follows the Bernoulli equation
\begin{equation}
    U_\text{B} = U_\text{B}(t) = w[y + H_0]_+\text{sgn}(\Delta p) \sqrt{\frac{2|\Delta p|}{\rho_0}},
\end{equation}
with effective lip-reed width $w$ (in m), density of air $\rho_0$ (in kg/m$^3$), static equilibrium separation $H_0$ (in m). Moreover, $[\cdot]_+$ describes the `positive part of' (see Chapter \ref{ch:collisions}). The negative equilibrium separation $-H_0$ can be seen as the location of the lower lip, and when $y + H_0 \leq 0$, the lips are closed and $U_\text{B}$ is 0. Another volume flow (in m$^3$/s) is generated by the lip reed itself according to
\begin{equation}
    U_\text{r} = U_\text{r}(t) = S_\text{r} \dot y,
\end{equation}
and assuming that the volume flow velocity is conserved, the total air volume entering the acoustic tube at the left boundary is defined as
\begin{equation}
    S(0)v(0,t) = U_\text{B}(t) + U_\text{r}(t).
\end{equation}

\subsubsection{Compact PDE}
To reduce the number of variables in later derivations in this chapter, one can divide all terms in Eq. \eqref{eq:lipReedDimensional} by $M$ to obtain
\begin{equation}
    \ddot y = -\omega_0^2 y - \sigma_\text{r} \dot y + \frac{S_\text{r}}{M}\Delta p,
\end{equation}
with angular frequency of the lip reed $\omega_0 = \sqrt{K/M}$ (rad/s) and loss parameter $\sigma_\text{r} = R / M$ (s$^{-1}$).

\section{Discrete time}\label{sec:discreteLipReed}
\def\nph{}
\def\nphSys{n+1/2}

Following \cite{Harrison2018}, $y$, $\Delta p$, and thereby $U_\text{B}$ and $U_\text{r}$ are placed on the interleaved temporal grid\footnote{The variables are placed on the non-interleaved spatial grid, as the lip reed interacts with the boundary of the tube ($x=0$).}, and the equations presented above can be discretised to the following system:
\begin{subequations}\label{eq:discreteLipSystem}
    \begin{align}
        \delta_{tt}y^{\nphSys} &= -\omega_0^2\mu_{t\cdot}y^{\nphSys}-\sigma_\text{r}\delta_{t\cdot}y^{\nphSys} + \frac{S_\text{r}}{M}\Delta p^{\nphSys},\label{eq:discReed}\\
        \Delta p^{\nphSys} &= P_\text{m} - \mu_{t+}p_0^n,\label{eq:pDiff}\\
        U_\text{B}^{\nphSys} &= w[y^{\nphSys}+H_0]_+\text{sgn}(\Delta p^{\nphSys})\sqrt{\frac{2|\Delta p^{\nphSys}|}{\rho_0}},\label{eq:bernoulli}\\
        U_\text{r}^{\nphSys} &= S_\text{r}\delta_{t\cdot}y^{\nphSys},\label{eq:Ur}\\
        \mu_{x-}(S_{1/2}v_{1/2}^{\nphSys}) &= U_\text{B}^{\nphSys} + U_\text{r}^{\nphSys}.\label{eq:UbUr}
    \end{align}
\end{subequations}
Here, $p_0^n$ and $S_{1/2}v_{1/2}^{\nphSys}$ are discrete values at the left boundary of an acoustic tube described by system \eqref{eq:firstOrderFDS}. Expanding the operators in Eq. \eqref{eq:discReed} and solving for $y^{n+3/2}$ yields
\begin{equation}\label{eq:updateEqLipreed}
    % \left(1 + \frac{\omega_0^2 k^2}{2} + \frac{\sigma_\text{r} k}{2}\right)y^{n+3/2} &= 2 y^{n+1/2} - \left(1 + \frac{\omega_0^2 k^2}{2} - \frac{\sigma_\text{r} k}{2}\right) y^{n-1/2} + \frac{S_\text{r} k^2}{M} \Delta p^{n+1/2}\nonumber\\
    \alpha_\text{r}y^{n+3/2} = 4y^{n+1/2} + \beta_\text{r}y^{n-1/2} + \xi_\text{r}\Delta p^{n+1/2}
\end{equation}
where (after a multiplication by 2 to reduce fractions)
\begin{equation}\label{eq:lipreedUpdateTerms}
    \alpha_\text{r} = 2 + \omega_0^2k^2 + \sigma_\text{r} k\ , \quad \beta_\text{r} =  \sigma_\text{r} k - 2 - \omega_0^2 k^2\ , \quad \text{and} \quad \xi_\text{r} = \frac{2 S_\text{r}k^2}{M}.
\end{equation}
Although Eq. \eqref{eq:updateEqLipreed} seems to be implicitly dependent to the pressure difference $\Delta p^{n+1/2}$ it is possible to explicitly solve it. A derivation is shown below.

\subsection{Obtaining $\Delta p$}\label{sec:obtainingDeltaP}
In the following, the superscript $n+1/2$ will be suppressed for $y$, $\Delta p$, $U_\text{B}$, $U_\text{r}$, $S_{1/2}$ and $v_{1/2}$ for brevity.

\subsubsection{Rewrite Eq. \eqref{eq:discReed}}
Using identities \eqref{eq:identity1} and \eqref{eq:identity4}, Eq. \eqref{eq:discReed} can be rewritten to
\begin{equation*}
    \frac{2}{k} (\delta_{t\cdot} - \delta_{t-})y^{\nph} = -\omega_0^2(k\delta_{t\cdot} + e_{t-})y^{\nph} - \sigma_\text{r}\delta_{t\cdot} y^{\nph} + \frac{S_\text{r}}{M}\Delta p^{\nph},
\end{equation*}
and, after grouping the terms,
\begin{equation}
    a_1\delta_{t\cdot}y^{\nph} - a_2\Delta p^{\nph} - a_3^n = 0,\label{eq:preAEquation}
\end{equation}
where
\begin{equation}\label{eq:aCoeffs}
    a_1 = \frac{2}{k} + \omega_0^2k + \sigma_\text{r} \geq 0, \quad a_2 = \frac{S_\text{r}}{M} \geq 0\ , \quad \text{and} \quad a_3^n = \left(\frac{2}{k} \delta_{t-} - \omega_0^2e_{t-}\right)y^{\nph}\ .
\end{equation}
Note that non-negativity property can be applied to $a_1$ and $a_2$ as these are calculated solely from non-negative parameters. %The same will be done for other coefficients below.
Equation \eqref{eq:Ur} can then be substituted into Eq. \eqref{eq:preAEquation}
\begin{equation*}
    \frac{a_1}{S_\text{r}}U_\text{r}^{\nph} - a_2 \Delta p^{\nph} - a_3^n = 0,
\end{equation*}
and consequently Eq. \eqref{eq:UbUr} to get
\begin{equation}\label{eq:aEquation}
    \frac{a_1}{S_\text{r}}\left(\mu_{x-}(S_{1/2}v_{1/2}^{\nph}) - U_\text{B}^{\nph}\right) - a_2 \Delta p^{\nph} - a_3^n = 0.
\end{equation}
%
\subsubsection{Obtaining $\mu_{x-}(S_{1/2}v_{1/2}^{\nph})$}
To obtain a definition for $\mu_{x-}(S_{1/2}v_{1/2}^{\nph})$, one can use the FD scheme for the pressure of the first-order system in \eqref{eq:discPressure} and evaluate this at $l = 0$
\begin{equation}
    \frac{\bar S_0}{\rho_0 c^2}\delta_{t+}p_0^n = -\delta_{x-}(S_{1/2}v_{1/2}^{\nph}).
\end{equation}
Using identity \eqref{eq:identityLip} for $\dxm$ and $\delta_{t+}$, this can be rewritten to
% \begin{equation}
%     \frac{\bar S_0}{\rho_0 c^2}\delta_{t+}p_0^n = \frac{2}{h} \left(\mu_{x-}(S_{1/2}v_{1/2}^{\nph})-S_{1/2}v_{1/2}^{\nph}\right),
% \end{equation}
% and, using the same identity for $\delta_{t+}$, yields
\begin{equation}
    \frac{2\bar S_0}{\rho_0 c^2k}(\mu_{t+}p_0^n-p_0^n) = \frac{2}{h} \left(\mu_{x-}(S_{1/2}v_{1/2}^{\nph})-S_{1/2}v_{1/2}^{\nph}\right).
\end{equation}
and substituting Eq. \eqref{eq:pDiff} yields
\begin{align}
    \frac{2\bar S_0}{\rho_0 c^2k}(P_\text{m} - \Delta p^{\nph}-p_0^n) &= \frac{2}{h} \left(\mu_{x-}(S_{1/2}v_{1/2}^{\nph})-S_{1/2}v_{1/2}^{\nph}\right).\nonumber\\
    \mu_{x-}(S_{1/2}v_{1/2}^{\nph}) &= b_1^n - b_2\Delta p^{\nph}\label{eq:bEquation}
\end{align}
where
\begin{equation}\label{eq:bCoeffs}
    b_1^n = S_{1/2}v_{1/2}^{\nph} + \frac{\bar S_0h}{\rho_0 c^2k} (P_\text{m} - p_0^n), \quad \text{and} \quad b_2 = \frac{\bar S_0h}{\rho_0 c^2k} \geq 0\ .
\end{equation}
\subsubsection{Final steps}
Equations \eqref{eq:bEquation} and \eqref{eq:bernoulli} can be substituted into Eq. \eqref{eq:aEquation} to get
\begin{gather}
    \frac{a_1}{S_\text{r}}\left(b_1^n - b_2\Delta p^{\nph} - w[y^{\nph}+H_0]_+\text{sgn}(\Delta p^{\nph})\sqrt{\frac{2|\Delta p^{\nph}|}{\rho_0}}\right) - a_2 \Delta p^{\nph} - a_3^n = 0,\nonumber\\
    - w[y^{\nph}+H_0]_+\text{sgn}(\Delta p^{\nph})\sqrt{\frac{2|\Delta p^{\nph}|}{\rho_0}} - b_2\Delta p^{\nph} - \frac{a_2S_\text{r}}{a_1} \Delta p^{\nph} + b_1^n - \frac{a_3^nS_\text{r}}{a_1} = 0,\nonumber\\
    -c_1^n\text{sgn}(\Delta p^{\nph})\sqrt{|\Delta p^{\nph}|} - c_2\Delta p^{\nph} + c_3^n = 0\label{eq:cEquation}
\end{gather}
where
\begin{equation}\label{eq:cCoeffs}
    c_1^n = w[y^{\nph} + H_0]_+\sqrt{\frac{2}{\rho_0}} \geq 0, \quad c_2 = b_2 + \frac{a_2S_\text{r}}{a_1} \geq 0, \quad \text{and}\quad c_3^n = b_1^n - \frac{a_3^nS_\text{r}}{a_1}\ .
\end{equation}
Equation \eqref{eq:cEquation} can be divided by $-\text{sgn}(\Delta p^{\nph})$ to get a quadratic equation in $\sqrt{|\Delta p^{\nph}|}$
\begin{equation}
    c_2|\Delta p^{\nph}| + c_1^n\sqrt{|\Delta p^{\nph}|} - \frac{c_3^n}{\text{sgn}(\Delta p^{\nph})} = 0.
\end{equation}
As $c_1^n, c_2 \geq 0$, the following must be true for any real solutions to exist
\begin{equation}\label{eq:sgnEquality}
    \text{sgn}(c_3^n) = \text{sgn}(\Delta p^{\nph}) \quad \Longrightarrow \quad \frac{c_3^n}{\text{sgn}(\Delta p^{\nph})} = |c_3^n|.
\end{equation}
and one can solve for $\sqrt{|\Delta p^{\nph}|}$:
\begin{equation}
    \sqrt{|\Delta p^{\nph}|} = \frac{-c_1^n \pm \sqrt{(c^n_1)^2+4c_2|c_3^n|}}{2c_2}\ .
\end{equation}
Finally, because $\sqrt{(c_1^n)^2 + 4c_2|c_3^n|} \geq c_1^n$, one can only guarantee a positive solution if square root term is added. Using Eq. \eqref{eq:sgnEquality}, the definition for the pressure difference can be found:
\begin{equation}\label{eq:pressureDiff}
    \Delta p^{\nph} = \text{sgn}(c_3^n)\left(\frac{-c_1^n + \sqrt{(c^n_1)^2+4c_2|c_3^n|}}{2c_2}\right)^2.
\end{equation}
which can be used in the update of the lip reed in Eq. \eqref{eq:discReed}.

\subsection{Coupling to the tube}\label{sec:lipreedTube}
The coupling of the lip reed to the acoustic tube is easily done by rewriting Eq. \eqref{eq:pressureUpdate} evaluated at $l=0$ to
\begin{equation}\label{eq:tubeCoupling}
    p^{n+1}_0 = p_0^n - \frac{\rho_0c\lambda}{\bar S_0}\left(-2\mu_{x-}(S_{1/2}v_{1/2}^{\nph}) + 2 S_{1/2}v_{1/2}^{\nph}\right).
\end{equation}
Equation \eqref{eq:UbUr} can then be substituted to get
\begin{equation}\label{eq:pressureCoupled}
    p^{n+1}_0 = p_0^n - \frac{\rho_0c\lambda}{\bar S_0}\left(-2(U_\text{B}^{\nph} + U_\text{r}^{\nph}) + 2 S_{1/2}v_{1/2}^{\nph}\right).
\end{equation}
Figure \ref{fig:lipreedTube} shows an implementation of the lip reed connected to an acoustic tube. The lip reed is shown on the left and the left boundary of the tube is on the right side of the lip reed. The frequency of the lips is set to $f_0 = 600$ Hz ($\omega_0 = 1200\pi$ rad/s), the input pressure $P_\mtxt = 2000$ Pa and the other parameters are as listed in paper \citeP[H]. The initial conditions of the lip have been set to $y^{1/2} = y^{3/2} = -H_0$ such that the lips are closed at the start of the simulation and the tube is cylindrical with a circular cross-section of $S(x) = 5\cdot 10^{-5}$. The figure shows that the lips oscillate, and that when the lips are closed, i.e. when $y \leq H_0$, no energy enters the acoustic tube.\footnote{This is similar behaviour to what the pulse train in Section \ref{sec:pulseTrain} attempts to model.}

% \subsubsection{Discussion}
% The oscillation shown in Figure \ref{fig:lipreedTube} is similar to the pulse train shown in Section \ref{eq:pulseTrain}, and shows that this type of excitation signal is sufficient as a test case for a lip reed excitation.

\begin{figure}[h]
    \centering
    \includegraphics[width=\textwidth]{figures/exciters/lipreed/lipreedImplementation.eps}
    \caption{A lip reed (shown at the left side of the plots) exciting a cylindrical acoustic tube. The y-axis refers to the displacement of the lips and the pressure in the tube $p_l^n$ is shown in red and highlighted with a dashed line (not related to the y-axis).\label{fig:lipreedTube}}
\end{figure}
\section{Energy analysis}
This section performs an energy analysis on the lip reed system coupled to an acoustic tube using the steps described Section \ref{sec:energyAnalysis}. As all physical parameters need to be written out to obtain the correct units, Eq. \eqref{eq:lipReedDimensional} is discretised to get
\begin{equation}\label{eq:discLipreedDimensional}
    M\delta_{tt}y^{\nphSys} = -K\mu_{t\cdot}y^{\nphSys}-R\delta_{t\cdot}y^{\nphSys} + S_\text{r}\Delta p^{\nphSys},
\end{equation}
and will be used in this analysis. Again, the superscript $n+1/2$ will be suppressed for $y$, $\Delta p$, $U_\text{B}$, $U_\text{r}$, $S_{1/2}$ and $v_{1/2}$ for brevity.

\subsubsection{Step 1: Obtain $\dtp \h$}
Multiplying Eq. \eqref{eq:discLipreedDimensional} by $(\delta_{t\cdot}y)$, and moving all terms to the left-hand side, yields the rate of change of the energy in the lip reed $\h_\rtxt$
\begin{equation*}
    \dtp \h_\rtxt = M(\delta_{t\cdot}y^{\nph})(\delta_{tt}y^{\nph}) + K(\delta_{t\cdot}y^{\nph})(\mu_{t\cdot}y^{\nph}) + R(\delta_{t\cdot}y^{\nph})^2 - S_\text{r}(\delta_{t\cdot}y^{\nph})\Delta p^{\nph} = 0.
\end{equation*}
One can substitute Eqs \eqref{eq:Ur}, and \eqref{eq:UbUr} thereafter, to get
\begin{align*}
    \dtp \h_\rtxt = M(\delta_{t\cdot}y^{\nph})(\delta_{tt}y^{\nph}) &+ K(\delta_{t\cdot}y^{\nph})(\mu_{t\cdot}y^{\nph}) + R(\delta_{t\cdot}y^{\nph})^2 \\
    &\quad- \left(\mu_{x-}(S_{1/2}v_{1/2})-U_\text{B}\right)\Delta p^{\nph} = 0.
\end{align*}
Finally, substituting Eq. \eqref{eq:pDiff}, yields
\begin{align*}
    \dtp \h_\rtxt = M(\delta_{t\cdot}y^{\nph})(\delta_{tt}y^{\nph}) &+ K(\delta_{t\cdot}y^{\nph})(\mu_{t\cdot}y^{\nph}) + R(\delta_{t\cdot}y^{\nph})^2 \\
    &\quad+ U_\text{B}\Delta p^{\nph} - \mu_{x-}(S_{1/2}v_{1/2})(P_\mtxt-\mtp p_0^n) = 0.
\end{align*}
One can then include the tube by recalling that $\delta_{t+}\mathfrak{h}_\text{t} = -\mathfrak{b}_\text{r} + \mathfrak{b}_\text{l}$, and that the left boundary term is defined as (Eq. \eqref{eq:firstOrderLeftBoundary})
\begin{equation*}
    \mathfrak{b}_\text{l} = (\mu_{t+}p_0)\mu_{x-}(S_{1/2}v_{1/2}),
\end{equation*}
and substituting this (ignoring the right boundary term, i.e., $\b_\rtxt = 0$) to get
\begin{align*}
    \dtp (\h_\rtxt +\h_\ttxt) = M(\delta_{t\cdot}y^{\nph})(\delta_{tt}y^{\nph}) &+ K(\delta_{t\cdot}y^{\nph})(\mu_{t\cdot}y^{\nph}) + R(\delta_{t\cdot}y^{\nph})^2 \\
    &\quad+ U_\text{B}\Delta p^{\nph} - \mu_{x-}(S_{1/2}v_{1/2})P_\mtxt = 0.
\end{align*}
\subsubsection{Step 2: Identify energy types and isolate $\dtp$}
Using identities \eqref{eq:prodIdentity1} and \eqref{eq:prodIdentity4}, the energy balance can be shown to be
\begin{equation}
    \delta_{t+}\left(\mathfrak{h}_\text{t}+\mathfrak{h}_\text{r}\right) = - \q_\text{r} - \mathfrak{p}_\text{r},
\end{equation}
where the energy of the tube $\h_\ttxt$ is as defined in Eq. \eqref{eq:energyBalanceFirstOrder} and the energy of the mass is
\begin{equation}
    \h_\rtxt = \t_\rtxt + \v_\rtxt, \qwiq \t_\rtxt = \frac{M}{2}(\delta_{t-}y)^2, \qaq \v = \frac{K}{2}\mu_{t-}(y^2).
\end{equation}
Furthermore, the damping term is defined as
\begin{equation}
    \mathfrak{q}_\text{r} = R(\dtd y)^2 + U_\text{B}\Delta p^{\nph},
\end{equation}
and the input power as
\begin{equation}
    \mathfrak{p}_\text{r} = -(U_\text{B} + U_\text{r})P_\text{m}.
\end{equation}
It is interesting to note that due to the choice of discretisation of the lip reed, $\t_\rtxt$, $\v_\rtxt$ and $\q_\rtxt$ are non-negative, making the lip reed strictly dissipative and thus inherently stable.

\subsubsection{Step 3: Check units}
The kinetic and potential energy of the lip reed can be written in their units as
\begin{align*}
    \t_\rtxt = \frac{M}{2}(\delta_{t-}y)^2 &\ \overset{\text{in units}}{\xrightarrow{\hspace*{1cm}}} \quad\text{kg}\cdot(\text{s}^{-1}\cdot \text{m})^{2}= \text{kg}\cdot\text{m}^2\cdot\text{s}^{-2},\\
    \v_\rtxt = \frac{K}{2}\mu_{t-}(y^2)&\ \overset{\text{in units}}{\xrightarrow{\hspace*{1cm}}} \quad \text{N} \cdot \text{m}^{-1} \cdot \text{m}^2 = \text{kg}\cdot\text{m}^2\cdot\text{s}^{-2},
\end{align*}
and have the correct units. Recalling that the damping and input power terms need to have units of kg$\cdot$ m$^2 \cdot $s$^{-3}$, writing the individual components of these terms in their respective units yields
\begin{align*}
    R(\dtd y)^2 &\ \overset{\text{in units}}{\xrightarrow{\hspace*{1cm}}} \quad\text{kg}\cdot\text{s}^{-1}\cdot(\text{s}^{-1}\cdot \text{m})^{2} = \text{kg} \cdot \text{m}^2 \cdot \text{s}^{-3} ,\\
    U_\text{B}\Delta p^{\nph}&\ \overset{\text{in units}}{\xrightarrow{\hspace*{1cm}}} \quad \text{m}^3 \cdot \text{s}^{-1}\cdot \text{kg}\cdot \text{m}^{-1}\cdot \text{s}^{-2}= \text{kg} \cdot \text{m}^2 \cdot \text{s}^{-3},\\
    -(U_\Btxt + U_\rtxt)P_\mtxt&\ \overset{\text{in units}}{\xrightarrow{\hspace*{1cm}}} \quad \text{m}^3 \cdot \text{s}^{-1}\cdot \text{kg}\cdot \text{m}^{-1}\cdot \text{s}^{-2}= \text{kg} \cdot \text{m}^2 \cdot \text{s}^{-3},
\end{align*}
and shows that the units are indeed correct.
\subsubsection{Step 4: Implementation}
Figure \ref{fig:lipReedEnergy} shows the energetic output of the lip reed exciting coupled to an acoustic tube corresponding to the behaviour shown in Figure \ref{fig:lipreedTube}. The total energy of the system increases due to the input pressure and is mainly transferred to the tube. The oscillations of the lip reed can be observed from the oscillations in its kinetic and potential energy. The normalised energy (using Eq. \eqref{eq:normalisedEnergyDamping}) does not include the first time index as one full iteration of the coupling is necessary to yield a correct energy calculation. Instead, one starts at $n=1$ and uses $\h^1$ instead of $\h_0$ in Eq. \eqref{eq:normalisedEnergyDamping}. \SWcomment[Check with stefan]

\begin{figure}[h]
    \centering
    \begin{tikzpicture}[->,node distance=3cm,
        thick,main node/.style={circle,draw}]

        \node[] (image) at (0,0) {
        \includegraphics[width=\textwidth]{figures/exciters/lipreed/lipreedEnergy.eps}
        };

        \node[] (he) at (0.2,0.5) {\small $\mathfrak{h}_\text{e}$};

        \node[] (h) at (-5.8, 1) {\small $\mathfrak{h}$};
        \node[] (v) at (-5.8, 0.5) {\small $\color[HTML]{00DB00}\mathfrak{h}_\ttxt$};
        \node[] (t) at (-5.8, 0) {\small $\color{red}\mathfrak{v}_\rtxt$};
        \node[] (c) at (-5.8, -0.5) {\small $\color{blue}\mathfrak{t}_\rtxt$};

      \end{tikzpicture}
      \caption{The energy of the acoustic tube (green), the potential energy (red) and the kinetic energy of the lip reed (blue), and the total energy (black) of the system corresponding to Figure \ref{fig:lipreedTube}. The right panel shows the normalised energy (according to Eq. \eqref{eq:normalisedEnergyDamping} starting at $n=1$) shows that the deviation of the energy is within machine precision. \label{fig:lipReedEnergy}}
\end{figure}