\subsection{Concept Questions} % (fold)
\label{sub:concept_questions_flow}

Read over the concepts in this chapter and answer the following questions:
\begin{enumerate}
  \item What values can a Boolean expression have?
  \item What are the values of the following expressions?

  \begin{table}[h]
    \centering
    \begin{tabular}{|c|l|l|}
      \hline
      \textbf{Question} & \textbf{Expression} & \textbf{Given} \\
      \hline
      (a) & \texttt{1 > 5} & \\
      \hline
      (b) & \texttt{1 < 5} & \\
      \hline
      (c) & \texttt{a > b} & \texttt{a} is 1 and \texttt{b} is 2 \\
      \hline
      (d) & \texttt{(a > b) or (b > a)} & \texttt{a} is 1 and \texttt{b} is 2 \\
      \hline
      (e) & \texttt{(a > b) and (b > a)} & \texttt{a} is 2 and \texttt{b} is 2 \\
      \hline
      (f) & \texttt{a or b or c} & \texttt{a} is False, \texttt{b} is False, and \texttt{c} is True\\
      \hline
      (g) & \texttt{(a or b) and (c or d)} & \texttt{a} is False, \texttt{b} is True, \texttt{c} is True, and \texttt{d} is False \\
      \hline
      (h) & \texttt{(a or b) and (c or d)} & \texttt{a} is False, \texttt{b} is True, \texttt{c} is False, and \texttt{d} is False \\
      \hline
      (i) & \texttt{(a and b) or (c and d)} & \texttt{a} is False, \texttt{b} is True, \texttt{c} is True, and \texttt{d} is True \\
      \hline
      (j) & \texttt{a xor b} & \texttt{a} is True and \texttt{b} is True\\
      \hline
      (k) & \texttt{(a or b) and (not (a and b))} & \texttt{a} is True and \texttt{b} is True\\
      \hline
      (l) & \texttt{not True} & \\
      \hline
      (m) & \texttt{a and (not b)} & \texttt{a} is True and \texttt{b} is False\\
      \hline
    \end{tabular}
  \end{table}

  \csection{
    The following table shows the C syntax for these boolean expressions.\newline\newline
    \begin{tabular}{|c|l|}
      \hline
      \textbf{Question} & \textbf{Expression} \\
      \hline
      (a) & \texttt{1 > 5} \\
      \hline
      (b) & \texttt{1 < 5} \\
      \hline
      (c) & \texttt{a > b} \\
      \hline
      (d) & \texttt{(a > b) || (b > a)} \\
      \hline
      (e) & \texttt{(a > b) \&\& (b > a)} \\
      \hline
      (f) & \texttt{a || b || c} \\
      \hline
      (g) & \texttt{(a || b) \&\& (c || d)} \\
      \hline
      (h) & \texttt{(a || b) \&\& (c || d)} \\
      \hline
      (i) & \texttt{(a \&\& b) || (c \&\& d)} \\
      \hline
      (j) & \texttt{a \^{} b} \\
      \hline
      (k) & \texttt{(a || b) \&\& (!(a \&\& b))} \\
      \hline
      (l) & \texttt{!false} \\
      \hline
      (m) & \texttt{a \&\& (!b)}\\
      \hline
    \end{tabular}
  }

  \item What are the two kinds of branching statements?
  \item What are the differences between these statements?
  \item When would you use each of these kinds of branching statements?
  \item What are the two different kinds of looping statements?
  \item What are the differences between these statements?
  \item When would you use each of these kinds of looping statements?
  \item How do Boolean expressions relate to branching and looping statements?
  \item What are the four jumping statements?
  \item What are the differences between these statements?
  \item Why do you need compound statements? Where would these be used?
  \item In structured programming, what are the three different kinds of blocks?
  \item How many entry/exits are there from these blocks?
  \item What are the principles of structured programming?
  \item How does this influence the way you design programs?
  \item Open a new SwinGame project and examine the startup code. How does this program keep the window open until the user closes it?
\end{enumerate}
% subsection concept_questions (end)
\clearpage

\subsection{Code Reading Questions} % (fold)
\label{sub:code_reading_questions_flow}
Use what you have learnt to read and understand the following code samples, and answer the associated questions.
\begin{enumerate}
  \item Read the C code in \lref{clst:rect-move}, or the Pascal code in \lref{plst:rect-move}, and then answer the following questions:
  \begin{enumerate}
    \item What are \texttt{X\_SPEED}, \texttt{width}, \texttt{height}, \texttt{x}, and \texttt{y}? How are they similar/different? What purpose does each of these play? What values are they assigned?
    \item What does the loop in this code do?
    \item Briefly explain what this code does, and suggest a name for the \texttt{????} procedure.
    \item Provide an example of how you could call this procedure.
  \end{enumerate}

  \begin{figure}[h]
    \csection{\ccode{clst:rect-move}{What does this C code do?}{topics/control-flow/exercises/rect_move.c}}
  \end{figure}
  \begin{figure}[h]
    \passection{\pascode{plst:rect-move}{What does this Pascal code do?}{topics/control-flow/exercises/RectMove.pas}}
  \end{figure}

  \item Read the C code in \lref{clst:color-pick}, or the Pascal code in \lref{plst:color-pick}, and then answer the following questions:
  \begin{enumerate}
    \item What will be drawn to the screen when the user is not holding down any key?
    \item When the user is holding down the G key what will be drawn?
    \item When the user is holding down the R and B keys what will be drawn?
    \item What condition causes the loop to end? How can a user end this loop?
    \item Draw a flow chart to illustrate the possible paths through this code.
  \end{enumerate}
  \begin{figure}[h]
    \csection{\ccode{clst:color-pick}{What does this C code do?}{topics/control-flow/exercises/color_pick.c}}
  \end{figure}
  \begin{figure}[h]
    \passection{\pascode{plst:color-pick}{What does this Pascal code do?}{topics/control-flow/exercises/ColorPick.pas}}
  \end{figure}
  \clearpage

  \item Read the C code in \lref{clst:bar_pattern}, or the Pascal code in \lref{plst:bar_pattern}, and then answer the following questions:
  \begin{enumerate}
    \item What do the local variables and parameter keep track of?
    \item How many times will the loop execute?
    \item What is the purpose of the if statement within the loop?
    \item What will be drawn to the screen when this procedure is called?
  \end{enumerate}
  \begin{figure}[h]
    \csection{\ccode{clst:bar_pattern}{What does this C code do?}{topics/control-flow/exercises/draw_bar_pattern.c}}
  \end{figure}
  \begin{figure}[h]
    \passection{\pascode{plst:bar_pattern}{What does this Pascal code do?}{topics/control-flow/exercises/DrawBarPattern.pas}}
  \end{figure}
  \clearpage

  \item Read the C code in \lref{clst:color-pick1}, or the Pascal code in \lref{plst:color-pick1}, and then answer the following questions:
  \begin{enumerate}
    \item What will be drawn to the screen when the user is not holding down any key?
    \item When the user is holding down the G key what will be drawn?
    \item When the user is holding down the R and B keys what will be drawn?
    \item What condition causes the loop to end? How can a user end this loop?
    \item Draw a flow chart to illustrate the possible paths through this code.
  \end{enumerate}
  \begin{figure}[h]
    \csection{\ccode{clst:color-pick1}{What does this C code do?}{topics/control-flow/exercises/color_pick1.c}}
  \end{figure}
  \begin{figure}[h]
    \passection{\pascode{plst:color-pick1}{What does this Pascal code do?}{topics/control-flow/exercises/ColorPick1.pas}}
  \end{figure}
  \clearpage

  \item Read the C code in \lref{clst:cursor}, or the Pascal code in \lref{plst:cursor}, and then answer the following questions:
  \begin{enumerate}
    \item What value is stored in the \texttt{dot x} variable each time through the loop?
    \item Explain what will be drawn on the screen when this code is executed. How does this code respond to user actions?
    \item Do the circles appear on top of, or below the frame rate? Explain.
    \item How frequently are the actions in the loop performed?
    \item How would putting a call to \texttt{delay} inside this loop effect the running of this program?
  \end{enumerate}
  \begin{figure}[h]
    \csection{\ccode{clst:cursor}{What does this C code do?}{topics/control-flow/exercises/mouse_pointer.c}}
  \end{figure}
  \begin{figure}[h]
    \passection{\pascode{plst:cursor}{What does this Pascal code do?}{topics/control-flow/exercises/MousePointer.pas}}
  \end{figure}
  \clearpage

  \item Read the C code in \lref{clst:click_game}, or the Pascal code in \lref{plst:click_game}, and then answer the following questions:
  \begin{enumerate}
    \item What is the purpose of the if statement in this code? What is it testing?
    \item When will the color of the rectangle change? What are the conditions that must be met?
    \item If the user clicks the left button on their mouse will the color of the rectangle change? discuss
    \item What would need to be added to keep a score of the number of times the user changed the color? explain
  \end{enumerate}

  \bigskip

  \begin{figure}[h]
     \centering
     \includegraphics[width=0.8\textwidth]{./topics/control-flow/exercises/ClickGame.png}
     \caption{The game in play}
     \label{fig:click_game}
  \end{figure}

  \begin{figure}[p]
    \csection{\ccode{clst:click_game}{What does this C code do?}{topics/control-flow/exercises/play_game.c}}
  \end{figure}
  \begin{figure}[p]
    \passection{\pascode{plst:click_game}{What does this Pascal code do?}{topics/control-flow/exercises/PlayGame.pas}}
  \end{figure}
  \clearpage

  \item Read the C code in \lref{clst:grid}, or the Pascal code in \lref{plst:grid}, and then answer the following questions:
  \begin{enumerate}
    \item What would be better names for the \texttt{i} and \texttt{j} variables?
    \item How many rectangles are drawn to the screen when this procedure is called?
    \item What does this code draw to the screen when it is called?
    \item How would you need to change the code to ensure that the rectangles were always square?
  \end{enumerate}
  \begin{figure}[h]
    \csection{\ccode{clst:grid}{What does this C code do?}{topics/control-flow/exercises/grid.c}}
  \end{figure}
  \begin{figure}[p]
    \passection{\pascode{plst:grid}{What does this Pascal code do?}{topics/control-flow/exercises/Grid.pas}}
  \end{figure}
  \clearpage

\end{enumerate}
% subsection code_reading_questions (end)

\clearpage

\subsection{Code Writing Questions: Applying what you have learnt} % (fold)
\label{sub:code_writing_questions_applying_what_you_have_learnt_flow}

Apply what you have learnt to the following tasks:
\begin{enumerate}
  \item Write a small program that will print the message `Hello World' to the Terminal `1 MILLION TIMES'... mmmmwwwahahahah\footnote{Dr Evil, Austin Powers, 1997}.
  \item Revisit your Circle Dimensions program from \cref{cha:storing_and_using_data} and adjust its implementation to make use of looping statements. The new output should display the radius, circle area, diameter, and circumference of circles with radius from 1.0cm to 1.0m at 0.25cm increments.
  \item Take your Times Table program from \cref{cha:storing_and_using_data}, and change it so that you can print a variable number of times. For example, have it print the 5 times table from 1 x 5 to 10 x 5, the 73 times table from 1 x 73 to 73 * 73, and the 42 times table from 1 x 42 to 7 * 42.
  \item Implement the Guess that Number program, and test that your implementation works successfully.
  \item Take the adjusted Face Shape program from \cref{cha:storing_and_using_data}, and re-implement it so that it draws a new randomly places face each time through the loop in \texttt{Main}. SwinGame include a \texttt{rnd} function that can be used to generate a random number between 0 and 1, you can then multiply this by the Screen Width or Screen Height to get a random position.
  \item Try implementing some of the code reading questions. See if you can combine a few of them together to create a small game.

  \item Write a function called \texttt{Read Integer} that will read a number from the user. If the user does not enter a number the function will output the message `Please enter a number.', and will repeatedly ask the user to enter a number until they do so. Write a small program that tests if your function works correctly.

  \begin{figure}[h]
    \csection{You can use \texttt{scanf(" \%d", \&result) != 1} to read in the value entered into a \texttt{result} variable, and to check if it was a number\footnote{The \texttt{scanf} function returns the number of values successfully read. See \nameref{sub:c_terminal_input}.}, and you can use \texttt{scanf("\%*[\textasciicircum \textbackslash n]");} to flush the input when you get something other than a number.}
  \end{figure}

  \item Use your \texttt{Read Integer} function to read two values from the user, and then output the larger of the two values.

  \item Use your \texttt{Read Integer} function to read in a number, and then print out a custom message for different values. For example, the value \texttt{42} could have the message `The meaning of life...', the value \texttt{73} could have the message `The best number, according to Dr. Sheldon Cooper', etc. Have at least five custom messages, and a default message when the user does not enter one of the selected values.

  \item Update your \emph{custom message} program, and have it ask the user if they want to quit at the end, allowing them to print multiple message values.
\end{enumerate}
% subsection code_writing_questions_applying_what_you_have_learnt (end)
\clearpage

\subsection{Extension Questions} % (fold)
\label{sub:extension_questions_flow}

If you want to further your knowledge in this area you can try to answer the following questions. The answers to these questions will require you to think harder, and possibly look at other sources of information.

\begin{enumerate}
  \item Implement a Number Guesser program that will guess numbers between 1 and 100. See the program description in \tref{tbl:number_guesser}

  \begin{table}[htbp]
  \centering
  \begin{tabular}{l|p{10cm}}
    \hline
    \multicolumn{2}{c}{\textbf{Program Description}} \\
    \hline
    \textbf{Name} & \emph{Number Guesser} \\
    \\
    \textbf{Description} & This program will work to \emph{guess} a number that the player is thinking of between 1 and 100. This will prompt the user to think of a number, and it will then guess using the following algorithm. The first guess will be 50 (the point half way between 1 and 100). The user will then enter a character to tell the computer how that value relates to the target value: `E' for equal, `L' for your guess is less than the target, and `G' for your guess is greater than the target. \newline

    When the guess is equal, a success message is output. \newline

    When the guess is less than the target the computer will take another guess that is half way between the guess and the pervious upper value (e.g. 1-100 = 50, 50-100 = 75, 75-100 = 87, 25-50 = 37, etc). \newline

    When the guess is larger than the target the computer will take another guess that is half way between the guess and the pervious lower value (e.g. 1-100 = 50, 1-50 = 25, 1-25 = 12, 50-75 = 62, etc). \newline

    Once the number is guessed (or the computer determines the player is cheating...) the program asks if the user wants to play again, and the process is repeated or the program quits.\\
    \hline
  \end{tabular}
  \caption{Description of the Number Guesser program.}
  \label{tbl:number_guesser}
  \end{table}

  \item Create a SwinGame project called \emph{Key Test}. See the description in \tref{tbl:key_test}.
  \begin{table}[htbp]
  \centering
  \begin{tabular}{l|p{10cm}}
    \hline
    \multicolumn{2}{c}{\textbf{Program Description}} \\
    \hline
    \textbf{Name} & \emph{Key Test} \\
    \\
    \textbf{Description} & This SwinGame will draw a filled red rectangle in the center of the screen when the user holds down the \textbf{r} key.\\
    \hline
  \end{tabular}
  \caption{Description of the Key Test program.}
  \label{tbl:key_test}
  \end{table}

  \item Extend the \emph{Key Test} program to draw other shapes when different keys are held down. For example, draw a filled circle when the user holds down the \textbf{c} key.

  \csection{You can call \texttt{key\_down(VK\_R)} to test if the `r' key is held down, then only draw the rectangle \emph{if} this key is held down.}

  \clearpage
  \item Color can be represented as consisting of three components: hue, saturation, and brightness. The hue component represents the color, the saturation is how much of this color is added to the white point, and the white point is controlled by the brightness and moves from black through grey to white. An illustration of this is shown in \fref{fig:color_wheel}.

\begin{figure}[htbp]
   \centering
   \includegraphics[width=0.3\textwidth]{./topics/control-flow/exercises/Color.png}
   \caption{Color wheel showing HSB color}
   \label{fig:color_wheel}
\end{figure}

  SwinGame includes a \texttt{Draw Pixel} procedure that allows you to draw a single pixel on the screen. We can use this to draw a color banding across the screen by calculating the color for each pixel using SwinGame's \texttt{HSB Color} function.

  The structure of this program can be broken into three blocks: a \texttt{Color At} function, a \texttt{My Clear Screen} procedure and a \texttt{Main} procedure. This structure is shown in \fref{fig:my_clear_screen}.

  \begin{figure}[htbp]
     \centering
     \includegraphics[width=0.3\textwidth]{./topics/control-flow/exercises/MyClearScreen.pdf}
     \caption{Structure Chart for My Clear Screen}
     \label{fig:my_clear_screen}
  \end{figure}

\clearpage

The \texttt{Color At} function will be used to calculate the color for each pixel on the screen. This will be passed the \texttt{x} and \texttt{y} coordinates of the pixel (Integers) and an \texttt{offset}\footnote{Offset must have a value between 0 and 1 as the value for Hue must be between 0 and 1.} (a Double value). This function will call SwinGame's \texttt{HSB Color} function, passing in a hue calculated using the equation shown below, with saturation and brightness set to 0.9 and 0.8 respectively, and it will return the resulting color.

\begin{equation}
  hue = \frac{\displaystyle (Offset + \frac{x + y}{Screen Width + Screen Height})}{2}
\end{equation}

The equation will cycle the hue value between 0 and 1, covering the full spectrum of color. The \texttt{x + y} component will alter the color of the pixel based on its position on the screen, while the offset allows the program to change the color of the first pixel as time passes.

The \texttt{MyClearScreen} procedure will loop through all of the pixels across the screen (with x going from 0 up to (but not including) ScreenWidth()). This loop will select a single column of pixels on the screen, the pixels at the given x position. As this column has ScreenHeight() pixels you need an inner loop that loops from 0 to ScreenHeight(). Inside the inner loop the \texttt{x,y} values give you the coordinates of an individual pixel. You can now use the \texttt{Draw Pixel} procedure to draw this pixel, using the \texttt{Color At} function you wrote to determine its color.

Lastly the \texttt{Main} procedure will contain the standard game loop provided in the SwinGame project template. Replace the code within the loop so that it calls \texttt{My Clear Screen} then \texttt{Refresh Screen} and lastly \texttt{Process Events}. Within \texttt{Main} you need to maintain an offset value that you increment by a fixed value each loop\footnote{Trial this with different value < 1}, always ensuring the value is between 0 and 1. This offset is then passed to \texttt{My Clear Screen} when it is called.

See if you can use this information, and program design to create a small screensaver program. Experiment with different values and loops to see what effects you can create.
\end{enumerate}

% subsection extension_questions (end)


\documentclass[11pt]{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{bm}
\usepackage[margin=1in]{geometry}
\usepackage{mathtools}

\DeclareMathOperator{\Tr}{Tr}
%%%\DeclareMathOperator{\diag}{diag}
\DeclarePairedDelimiter{\diagfences}{(}{)}
\newcommand{\diag}{\operatorname{diag}\diagfences}
\newcommand{\sd}{\mathcal{D}} % space D
\newcommand{\bmmpp}{\bm{\mathcal{P_+}}} % curve
\newcommand{\bmmp}{\bm{\mathcal{P}}} % curve
\newcommand{\splinepk}{\sum_j{\beta_{jk}(\bm{P}_j+\bm{D}_j)}}
\newcommand{\splinepx}{\sum_j{\beta_j(x)(\bm{P}_j+\bm{D}_j)}}
\newcommand{\splinek}{\sum_j{\beta_{jk}\bm{P}_j}}
\newcommand{\half}{\frac{1}{2}}
\newcommand{\vX}[1]{\bm{X}_{#1}} % vector
\newcommand{\vN}[1]{\bm{N}_{#1}} % vector
\newcommand{\vT}[1]{\bm{T}_{#1}} % vector
\newcommand{\vTp}[1]{\bm{T^{\prime}}_{#1}} % vector
\newcommand{\vP}[1]{\bm{\mathcal{P}}_{#1}} % vector
\newcommand{\sP}[1]{\mathcal{P}_{#1}} % scalar
\newcommand{\bitk}{\beta_i(t_k)}
\newcommand{\bjtk}{\beta_j(t_k)}
\newcommand{\mA}{\mathbf{A}} % matrix
\newcommand{\mx}{\mathbf{x}} % matrix
\newcommand{\mb}{\mathbf{b}} % matrix
\newcommand{\mB}{\mathbf{B}} % matrix
\newcommand{\mz}{\mathbf{0}} % matrix
\newcommand{\mF}{\mathbf{F}} % matrix
\newcommand{\mG}{\mathbf{G}} % matrix
\newcommand{\mNx}{\mathbf{N_x}} % matrix
\newcommand{\mNy}{\mathbf{N_y}} % matrix
\newcommand{\mTpx}{\mathbf{T^{\prime}_x}} % matrix
\newcommand{\mTpy}{\mathbf{T^{\prime}_y}} % matrix
\newcommand{\mDx}{\mathbf{D_x}} % matrix
\newcommand{\mDy}{\mathbf{D_y}} % matrix
\newcommand{\mHx}{\mathbf{H_x}} % matrix
\newcommand{\mHy}{\mathbf{H_y}} % matrix
\newcommand{\rhs}{\mathrm{RHS}}
\newcommand{\lhs}{\mathrm{LHS}}
\renewcommand{\b}[1]{\beta_{#1}}

\begin{document}

\title{Matrix representation of the linear system encountered in the least-squares minimization}
\author{jerviedog}
\maketitle

\section{Introduction}
This is a short summary of the derivation of the linear system encountered in the linear least-square minimization problem. Specifically, we want to adjust the curve $\bmmpp=\splinepx$ to minimize the distance from the point cloud to the curve. The displacements of the control points $\sd=\{\bm{D}\}$ are the optimization variables. The result can also be used to the optimization problem of similar form.

\section{TDM}
The minimization problem is \\
\[
\min_{\sd}{f(\sd)} = \min_{\sd}{\sum_k{e_k(\sd)}},
\]
where
\[
\begin{split}
e_k(\sd) &= e_{TD,k}(\sd) \\
&= \half\big[(\bmmpp(t_k)-\vX{k})^T\vN{k}\big]^2 \\
&= \half\Bigl[(\splinepk-\vX{k})^T\vN{k}\Bigr]^2,
\end{split}
\]
and
\[
\b{jk}=\bjtk.
\]

In two-dimensional cases, the control point $\bm{P}_j = (P_{jx},P_{jy})^T$, and the displacement of the control point $\bm{D}_j = (D_{jx},D_{jy})^T$.

Suppose that there are $n$ sample points in the point cloud, and $m$ control points. Minimizing $f(\sd)$ leads to
\[
\frac{\partial{f}}{\partial{D_{ix}}} = 0
\]
and
\[
\frac{\partial{f}}{\partial{D_{iy}}} = 0.
\]

Because $f(\sd)$ is linear sum of squares, the minimization of $f(\sd)$ would give us a linear system $\mA\mx=\mb$, the solution of which is the solution of the original optimization problem.

Below is the derivation of such linear system.

First, $f(\mathcal{D})$ can be rewritten as:
\[
\begin{split}
f(\sd) &= \sum_{k=1}^n{e_k(\sd)} \\
&= \half\sum_{k=1}^n{\big[(\bmmpp(t_k)-\vX{k})^T\vN{k}\big]^2} \\
&= \half\sum_{k=1}^n{\big[(\bmmpp(t_k)^T\vN{k})^2-2(\bmmpp(t_k)^T\vN{k})(\vX{k}^T\vN{k})+(\vX{k}^T\vN{k})^2\big]}.
%%% &= \half\sum_{k=1}^n{ \Biggl\{\Bigl[(\splinepk)^T\vN{k}\Bigr]^2-2\Bigl[(\splinepk)^T\vN{k}\Bigr](\vX{k}^T\vN{k})+(\vX{k}^T\vN{k})^2\Biggr\} }
\end{split}
\]

The derivative of $\bmmpp(t_k)$ with respect to $D_{ix}$ and $D_{iy}$ are
\[
\frac{\partial\bmmpp(t_k)}{\partial D_{ix}}=\b{ik} \binom 10,
\]
and
\[
\frac{\partial\bmmpp(t_k)}{\partial D_{ix}}=\b{ik} \binom 01.
\]

The derivative of $\bmmpp(t_k)^T\vN{k}$ with respect to $D_{ix}$ and $D_{iy}$ are
\[
\frac{\partial\bmmpp(t_k)^T\vN{k}}{\partial D_{ix}}=\b{ik} N_{kx},
\]
and
\[
\frac{\partial\bmmpp(t_k)^T\vN{k}}{\partial D_{iy}}=\b{ik} N_{ky}.
\]

Then the derivative of $f$ with respect to $D_{ix}$ and $D_{iy}$ are
\[
\begin{split}
\frac{\partial f}{\partial D_{ix}} &= \sum_k{\Bigl[ \b{ik} N_{kx}\bmmpp(t_k)^T\vN{k} - \b{ik} N_{kx}(\vX{k}^T\vN{k}) \Bigr]} \\
&= \sum_k{\Biggl\{ \b{ik} N_{kx}\Bigl[\splinepk\Bigr]^T\vN{k} - \b{ik} N_{kx}(\vX{k}^T\vN{k}) \Biggr\} },
\end{split}
\]
and
\[
\begin{split}
\frac{\partial f}{\partial D_{iy}} &= \sum_k{\Bigl[ \b{ik} N_{ky}\bmmpp(t_k)^T\vN{k} - \b{ik} N_{ky}(\vX{k}^T\vN{k}) \Bigr]} \\
&= \sum_k{\Biggl\{ \b{ik} N_{ky}\Bigl[\splinepk\Bigr]^T\vN{k} - \b{ik} N_{ky}(\vX{k}^T\vN{k}) \Biggr\} }.
\end{split}
\]

$\partial f/\partial D_{ix}=0$ result in the first $m$ equations, while $\partial f/\partial D_{iy}=0$ lead to the other $m$ equations. We next would like to rewrite these equations in the form of $\mA\mx=\mb$.

For the $i$th equation, that is, $\partial f/\partial D_{ix}=0,$
\[
\begin{split}
\lhs &= \sum_k{ \Bigl\{ \b{ik} N_{kx} \Bigl[ \sum_j(\b{jk}(D_{jx}N_{kx}+D_{jy}N_{ky})) \Bigr] \Bigr\} } \\
&= \sum_j{\sum_k{\b{ik}\b{jk} N_{kx}N_{kx}D_{jx}}} + \sum_j{\sum_k{\b{ik}\b{jk} N_{kx}N_{ky}D_{jy}}} \\
\rhs &= \sum_k{ \Bigl\{ \b{ik} N_{kx} \bigl[ (\vX{k}-\vP{k})^T\vN{k} \bigr] \Bigr\} }.
\end{split}
\]
Note that here $\vP{k}=\splinek$.

For the $i+m$th equation, that is, $\partial f/\partial D_{iy}=0,$
\[
\begin{split}
\lhs &= \sum_k{ \Bigl\{ \b{ik} N_{ky} \Bigl[ \sum_j(\b{jk}(D_{jx}N_{kx}+D_{jy}N_{ky})) \Bigr] \Bigr\} } \\
&= \sum_j{\sum_k{\b{ik}\b{jk} N_{kx}N_{ky}D_{jx}}} + \sum_j{\sum_k{\b{ik}\b{jk} N_{ky}N_{ky}D_{jy}}} \\
\rhs &= \sum_k{ \Bigl\{ \b{ik} N_{ky} \bigl[ (\vX{k}-\vP{k})^T\vN{k} \bigr] \Bigr\} }.
\end{split}
\]

Blah, blah, blah, at last, a matrix form $\mA\mx=\mb$ is obtained:
\[
\begin{split}
\lhs &=
\begin{pmatrix}
\mB & \mz \\
\mz & \mB
\end{pmatrix}^T
\begin{pmatrix}
\mNx & \mz \\
\mz & \mNy
\end{pmatrix}
\begin{pmatrix}
\mNx & \mNy \\
\mNx & \mNy
\end{pmatrix}
\begin{pmatrix}
\mB & \mz \\
\mz & \mB
\end{pmatrix}
\begin{pmatrix}
\mDx \\
\mDy
\end{pmatrix},\\
\rhs &=
\begin{pmatrix}
\mB & \mz \\
\mz & \mB
\end{pmatrix}^T
\begin{pmatrix}
\mNx & \mz \\
\mz & \mNy
\end{pmatrix}
\begin{pmatrix}
\mF \\
\mF
\end{pmatrix},
\end{split}
\]
where
\[
\begin{split}
\mB &=
\begin{pmatrix}
\b{11} & \dots & \b{m1} \\
\vdots & \ddots & \vdots \\
\b{1n} & \dots & \b{mn}
\end{pmatrix}_{n\times m},\\
\mNx &=
\diag{N_{1x},\dots,N_{nx}}_{n\times n},\\
\mNy &=
\diag{N_{1y},\dots,N_{ny}}_{n\times n},\\
\mDx &= (D_{1x},\dots,D_{mx})^T,\\
\mDy &= (D_{1y},\dots,D_{my})^T,\\
\mF &= \bigl((\vX{1}-\vP{1})^T\vN{1},\dots,(\vX{n}-\vP{n})^T\vN{n}\bigr)^T.
\end{split}
\]

\section{SDM}
The error term in SDM is similar to that in TDM. In SDM,
\[
\begin{split}
e_k(\sd) &= e_{SD,k}(\sd) \\
&= \half\frac{d_k}{d_k-\rho_k}\big[(\bmmpp(t_k)-\vX{k})^T\vT{k}\big]^2 + \half\big[(\bmmpp(t_k)-\vX{k})^T\vN{k}\big]^2.
\end{split}
\]
The forms of the two parts are similar. If we define $\vTp{k}=\sqrt{\frac{d_k}{d_k-\rho_k}}\vT{k}$, we can simplify the error term as:
\[
e_k(\sd) = \half\big[(\bmmpp(t_k)-\vX{k})^T\vTp{k}\big]^2 + \half\big[(\bmmpp(t_k)-\vX{k})^T\vN{k}\big]^2.
\]
The minimization of $f(\sd)$ leads to a linear system as below.
\[
\begin{split}
\lhs &=
\begin{pmatrix}
\mB & \mz \\
\mz & \mB
\end{pmatrix}^T
\Biggl[
\begin{pmatrix}
\mNx & \mz \\
\mz & \mNy
\end{pmatrix}
\begin{pmatrix}
\mNx & \mNy \\
\mNx & \mNy
\end{pmatrix}
+
\begin{pmatrix}
\mTpx & \mz \\
\mz & \mTpy
\end{pmatrix}
\begin{pmatrix}
\mTpx & \mTpy \\
\mTpx & \mTpy
\end{pmatrix}
\Biggr]
\begin{pmatrix}
\mB & \mz \\
\mz & \mB
\end{pmatrix}
\begin{pmatrix}
\mDx \\
\mDy
\end{pmatrix},\\
\rhs &=
\begin{pmatrix}
\mB & \mz \\
\mz & \mB
\end{pmatrix}^T
\Biggl[
\begin{pmatrix}
\mNx & \mz \\
\mz & \mNy
\end{pmatrix}
\begin{pmatrix}
\mF \\
\mF
\end{pmatrix}
+
\begin{pmatrix}
\mTpx & \mz \\
\mz & \mTpy
\end{pmatrix}
\begin{pmatrix}
\mG \\
\mG
\end{pmatrix}
\Biggr],
\end{split}
\]
where
\[
\begin{split}
\mTpx &=
\diag{\sqrt{\frac{d_1}{d_1-\rho_1}}T_{1x},\dots,\sqrt{\frac{d_n}{d_n-\rho_n}}T_{nx}}_{n\times n},\\
\mTpy &=
\diag{\sqrt{\frac{d_1}{d_1-\rho_1}}T_{1y},\dots,\sqrt{\frac{d_n}{d_n-\rho_n}}T_{ny}}_{n\times n},\\
\mG &= \bigl((\vX{1}-\vP{1})^T\vTp{1},\dots,(\vX{n}-\vP{n})^T\vTp{n}\bigr)^T.
\end{split}
\]

\section{PDM}
In PDM, the error term is:
\[
\begin{split}
e_k(\sd) &= e_{PD,k}(\sd) \\
&= \half\Bigl\Vert{\bmmpp(t_k)-\vX{k}}\Bigr\Vert_2^2 \\
&= \half(\bmmpp(t_k)-\vX{k})^T(\bmmpp(t_k)-\vX{k}) \\
&= \half\bmmpp(t_k)^T\bmmpp(t_k)-\bmmpp(t_k)^T\vX{k}+\half\vX{k}^T\vX{k}.
\end{split}
\]

The minimization of $f(\sd)$ results in two separate linear systems:
\[
\begin{split}
\mB^T\mB\mDx &= \mB^T\mHx,\\
\mB^T\mB\mDy &= \mB^T\mHy,
\end{split}
\]
where
\[
\begin{split}
\mHx &= (X_{1x}-\sP{1x},\dots,X_{nx}-\sP{nx})^T,\\
\mHy &= (X_{1y}-\sP{1y},\dots,X_{ny}-\sP{ny})^T.
\end{split}
\]

\section{Internal energy terms}
The internal energy terms are the integrals of the first and second derivatives of the curve:
\[
F_1 = \int {\|P^\prime_D(t)\|^2 }dt
\]
and
\[
F_2 = \int{\|P^{\prime\prime}_D(t)\|^2}dt .
\]

Eventually, the evaluation of the integration of the form $\int{\b{ik}\b{jk}}dt$ is needed. Here, I focus on the special case of cardinal spline basis. It seems that there would be no explicit/analytical form of these integrals.

\section{Appendix}
Some useful relations are listed here, in case I forget the derivation process.

\subsection{matrix manipulation}
\begin{align*}
&\mathrel{\phantom{=}}\sum_j^m{\sum_k^n{\b{ik}\b{jk}N_kM_kD_{jx}}} \notag \\
&=
\begin{pmatrix}
\b{i1}N_1M_1 & \dots & \b{in}N_nM_n
\end{pmatrix}
\mB
\begin{pmatrix}
D_{1x} \\
\vdots \\
D_{mx}
\end{pmatrix}
\end{align*}

\begin{align*}
&\mathrel{\phantom{=}}
\begin{pmatrix}
\b{11}N_{1x}N_{1y} & \dots & \b{1n}N_{nx}N_{ny} \\
\vdots & \ddots & \vdots \\
\b{m1}N_{1x}N_{1y} & \dots & \b{mn}N_{nx}N_{ny}
\end{pmatrix} \notag \\
&=\mB^T\cdot\diag{N_{1x},\dots,N_{nx}}\cdot\diag{N_{1y},\dots,N_{ny}}
\end{align*}

\subsection{manipulation of cardinal splines}
For cardinal splines $\$_{j,k,\mathbb{Z}}$, there is only one ``unique'' spline for a given order $k$; all other cardinal splines of order $k$ can be generated by translating the one that spans $\mathopen[0,k+1\mathclose)$. I denotes these ``unique'' cardinal splines as $Q_k$ of order $k$. I list the cardinal splines up to order $4$.

\[
\begin{split}
Q_1 &=
\begin{cases}
1 & 0 \leq x < 1\\
0 & otherwise
\end{cases}, \\
Q_2 &=
\begin{cases}
x & 0 \leq x < 1 \\
-(x-1)+1 & 1 \leq x < 2\\
0 & otherwise
\end{cases}, \\
Q_3 &=
\begin{cases}
\frac{1}{2}x^2 & 0 \leq x < 1 \\
-(x-1)^2+(x-1)+\frac{1}{2} & 1 \leq x < 2\\
\frac{1}{2}(x-2)^2-(x-2)+\frac{1}{2} & 2 \leq x < 3\\
0 & otherwise
\end{cases}, \\
Q_4 &=
\begin{cases}
\frac{1}{6}x^3 & 0 \leq x < 1 \\
-\frac{1}{2}(x-1)^3+\frac{1}{2}(x-1)^2+\frac{1}{2}(x-1)+\frac{1}{6} & 1 \leq x < 2\\
\frac{1}{2}(x-2)^3-(x-2)^2+\frac{2}{3} & 2 \leq x < 3\\
-\frac{1}{6}(x-3)^3+\frac{1}{2}(x-3)^2-\frac{1}{2}(x-3)+\frac{1}{6} & 3 \leq x < 4\\
0 & otherwise
\end{cases}.
\end{split}
\]

The derivative of the $Q_k$ is:
\[
Q_{i,k}^{\prime} = Q_{i,k-1}-Q_{i+1,k-1},
\]
where
\[
Q_{i,k} = Q_k(x-i),
\]
e.g., $Q_{i,k}$ is a translate of $Q_k$.

Suppose that a order $k$ b-spline curve with cardinal basis $Q_j$ is given: $\bmmp=\sum_{j=1}^m\bm{P}_jQ_j$. The first and second derivatives of $\bmmp$ with respect to x are:

\[
\begin{split}
\bmmp^{\prime} &= \sum_{j=1}^{m+1}[Q_{j,k-1}(\bm{P}_j-\bm{P}_{j-1})], \\
\bmmp^{\prime\prime} &= \sum_{j=1}^{m+2}[Q_{j,k-2}(\bm{P}_j-2\bm{P}_{j-1}+\bm{P}_{j-2})]. \\
\end{split}
\]

For open curves,
\[
\bm{P}_j=0, j \notin [1,m],
\]
while for closed curves,
\[
\bm{P}_j = \bm{P}_{j \bmod m}, j \notin [1,m].
\]

The derivatives of $\bmmp_+^{\prime}$ and $\bmmp_+^{\prime\prime}$ with respect to $D_{ix}$ and $D_{iy}$ are:
\[
\begin{split}
\frac{\partial{\bmmp_+^{\prime}}}{\partial{D_{ix}}}
&= [Q_{i,k-1}-Q_{i+1,k-1} , 0]^T \\
\frac{\partial{\bmmp_+^{\prime}}}{\partial{D_{iy}}}
&= [0 , Q_{i,k-1}-Q_{i+1,k-1}]^T \\
\frac{\partial{\bmmp_+^{\prime\prime}}}{\partial{D_{ix}}}
&= [Q_{i,k-2}-2Q_{i+1,k-2}+Q_{i+2,k-2} , 0]^T \\
\frac{\partial{\bmmp_+^{\prime\prime}}}{\partial{D_{iy}}}
&= [0 , Q_{i,k-2}-2Q_{i+1,k-2}+Q_{i+2,k-2}]^T.
\end{split}
\]

For open curves,
\[
Q_j=0, j \notin [1,m],
\]
while for closed curves,
\[
Q_j = Q_{j \bmod m}, j \notin [1,m].
\]

Two regularization terms ($F_1$ and $F_2$) are added in cost function $f$ to control the smoothness of the b-spline curves:
\[
\begin{split}
F_1 &= \half\int_{\bmmp}{\|\bmmp^{\prime}\|^2}dt ,\\
F_2 &= \half\int_{\bmmp}{\|\bmmp^{\prime\prime}\|^2}dt .
\end{split}
\]

Now we calculate the partial derivative of $F_1$ with respect to $D_{ix}$ and $D_{iy}$:
\[
\begin{split}
\frac{\partial{F_1}}{\partial{D_{ix}}}
&= \frac{\partial}{\partial{D_{ix}}}\half\int({{P_{x+}^{\prime}}^2+{P_{y+}^{\prime}}^2})dt \\
&= \half\int\frac{\partial}{\partial{D_{ix}}}({P_{x+}^{\prime}}^2+{P_{y+}^{\prime}}^2)dt \\
&= \int(P^{\prime}_{x+}\cdot \frac{\partial{P^{\prime}_{x+}}}{\partial{D_{ix}}} + P^{\prime}_{y+}\cdot \frac{\partial{P^{\prime}_{y+}}}{\partial{D_{ix}}})dt ,
\end{split}
\]
where
\[
P_{x+} = \sum_{j=1}^m(P_{jx}+D_{jx})Q_{j,k},
\]
\[
P_{y+} = \sum_{j=1}^m(P_{jy}+D_{jy})Q_{j,k},
\]
\[
\frac{\partial{P^{\prime}_{x+}}}{\partial{D_{ix}}} = Q_{i,k-1}-Q_{i+1,k-1}
\]
and
\[
\frac{\partial{P^{\prime}_{y+}}}{\partial{D_{ix}}} = 0.
\]

After simplify the above equation, we have
\[
\begin{split}
\frac{\partial{F_1}}{\partial{D_{ix}}}
&= \int(P^{\prime}_{x+}\cdot \frac{\partial{P^{\prime}_{x+}}}{\partial{D_{ix}}} + P^{\prime}_{y+}\cdot \frac{\partial{P^{\prime}_{y+}}}{\partial{D_{ix}}})dt \\
&= \int(P^{\prime}_{x+}\cdot \frac{\partial{P^{\prime}_{x+}}}{\partial{D_{ix}}})dt \\
&= \sum_{j=1}^{m+1}\Biggl\{\Bigl[(P_{jx}+D_{jx})-(P_{j-1,x}+D_{j-1,x})\Bigr] \cdot \int\Bigl[Q_{j,k-1}(Q_{i,k-1}-Q_{i+1,k-1})\Bigr]dt\Biggr\}.
\end{split}
\]

Similarly, we have
\[
\begin{split}
\frac{\partial{F_1}}{\partial{D_{iy}}}
&= \sum_{j=1}^{m+1}\Biggl\{\Bigl[(P_{jy}+D_{jy})-(P_{j-1,y}+D_{j-1,y})\Bigr] \cdot \int\Bigl[Q_{j,k-1}(Q_{i,k-1}-Q_{i+1,k-1})\Bigr]dt\Biggr\}, \\
\frac{\partial{F_2}}{\partial{D_{ix}}}
&= \sum_{j=1}^{m+2}\Biggl\{\Bigl[(P_{jx}+D_{jx})-2(P_{j-1,x}+D_{j-1,x})+(P_{j-2,x}+D_{j-2,x})\Bigr] \\
&\mathrel{\phantom{=}}\quad\quad\quad\cdot \int\Bigl[Q_{j,k-2}(Q_{i,k-2}-2Q_{i+1,k-2}+Q_{i+2,k-2})\Bigr]dt\Biggr\}, \\
\frac{\partial{F_2}}{\partial{D_{iy}}}
&= \sum_{j=1}^{m+2}\Biggl\{\Bigl[(P_{jy}+D_{jy})-2(P_{j-1,y}+D_{j-1,y})+(P_{j-2,y}+D_{j-2,y})\Bigr] \\
&\mathrel{\phantom{=}}\quad\quad\quad\cdot \int\Bigl[Q_{j,k-2}(Q_{i,k-2}-2Q_{i+1,k-2}+Q_{i+2,k-2})\Bigr]dt\Biggr\}.
\end{split}
\]

\end{document}


\chapter{Gravitational Waves}
\begin{itemize}
    \item vacuum solution of linearized Einstein equations
    \begin{equation}
        \Box \overline{h}_{\mu\nu} = 0 \qquad \text{in de Donder gauge}
    \end{equation}
    \item describes \emph{weak} gravitational waves \emph{only} $\rightarrow$ linearized treatment justified
    \item description breaks down for strong gravitational fields as the theory becomes essentially \emph{non-linear} (e.g.\ two black holes merge)
    \item analysis similar to electrodynamics, but here $h_{\mu\nu}$: spin-2 field, $A_\mu$: spin-1 field
    \item vacuum equations
    \begin{equation}
        \Box \overline{h}_{\mu\nu} = 0 \qquad \overline{h}_{\mu\nu}=h_{\mu\nu} - \frac{1}{2} \eta_{\mu\nu} h \qquad \partial^\mu \overline{h}_{\mu\nu} = 0
    \end{equation}
    \item Gauge freedom not yet completely exhausted by de Donder gauge.
    \begin{equation}
        \partial^\mu \overline{h'}_{\mu\nu} = \underbrace{\partial^\mu \overline{h'}_{\mu\nu}}_{=0} + \Box \xi_\nu = 0
    \end{equation}
    $\implies$ All gauge transformations generated by $\xi_\nu$ that satisfy $\Box \xi_\nu = 0$ do not lead out of the de Donder gauge. \\
    Compare with electromagnetic field:
    \begin{align}
        & A_\mu \to {A'}^\mu = A^\mu + \partial_\mu \lambda(x) \\
        & \partial_\mu A^\mu = 0 \qquad \text{Lorentz gauge} \\
        & \partial_\mu {A'}^\mu = \underbrace{ \partial_\mu A^\mu}_{=0} + \partial_\mu \partial^\mu \lambda(x) = 0 \implies \Box \lambda = 0
    \end{align}
    Exploit this remaining gauge freedom to make perturbations $h_{\mu\nu}$ \emph{transverse} and \emph{traceless}
    \item transversality
    \begin{equation}
        \partial^\mu h'_{\mu\nu} = \partial^\mu h_{\mu\nu} + \Box \xi_\nu + \partial_\nu \partial^\mu \xi_\mu \overset{!}{=} 0
    \end{equation}
    Since only gauge transformations that satisfy $\Box \xi_\nu$ are allowed (do not lead out of de Donder gauge), the equation that shoud
    be solved for $\xi_\nu$ is
    \begin{equation}
        \partial_\nu \partial^\mu \xi_\mu = - \partial^\mu h_{\mu\nu}
    \end{equation}
    \item In order for the perturbations to be traceless, we need to find a solution to
    \begin{equation}
        h' = h + \partial_\mu \xi^\mu = 0 \implies \partial_\mu \xi^\mu =
        -\frac{1}{2}h
    \end{equation}
\end{itemize}
\begin{figure}[hbtp!]
\centering
 \includegraphics{gwave1.pdf}
  \includegraphics{gwave2.pdf}
  \includegraphics{gwave3.pdf}
\caption{Polarisations of gravitational waves. From top to bottom:
$+$,$\times$ and mixed (circular) polarised waves.}
%TODO Caption
\end{figure}

% \begin{figure}
% \centering
% \begin{tikzpicture}[decoration={markings,
%   mark=between positions 0 and 1 step 22.3pt
%   with { \draw [fill] (0,0) circle [radius=2pt];}}]
% \draw[->] (0,1) -- (3,1);
% \draw[->] (0,1) -- (0,4);
% \node[text width=0.25cm] at (3,1.25){$x$};
% \node[text width=0.25cm] at (0.25,4){$y$};
%
% \node[text width=2cm, align=center] (1) at (2,5){$\omega t= 0$};
% \node[text width=2cm, align=center] (1) at (5,5){$\omega t= \frac{\pi}{2}$};
% \node[text width=2cm, align=center] (1) at (8,5){$\omega t= \pi$};
% \node[text width=2cm, align=center] (1) at (11,5){$\omega t= \frac{3\pi}{2}$};
% \node[text width=2cm, align=center] (1) at (14,5){$\omega t= 2\pi$};
% \begin{scope}[]
% \draw[dots along my path]  (2,3) ellipse (1 and 1);
% \end{scope}
% \begin{scope}[shift={(3,-0.5)},transform canvas={yscale=1.2}]
% \draw[dots along my path]  (2,3) ellipse (1 and 1);
% \end{scope}
% \begin{scope}[shift={(6,0)}]
% \draw[dots along my path]  (2,3) ellipse (1 and 1);
% \end{scope}
% \begin{scope}[shift={(7.2,0)},transform canvas={xscale=1.2}]
% \draw[dots along my path]  (2,3) ellipse (1 and 1);
% \end{scope}
% \begin{scope}[shift={(12,0)}]
% \draw[dots along my path]  (2,3) ellipse (1 and 1);
% \end{scope}
% \end{tikzpicture}
% \caption{Gravitational wave.}
% \end{figure}
% \begin{figure}
% \centering
% \begin{tikzpicture}[decoration={markings,
%   mark=between positions 0 and 1 step 22.3pt
%   with { \draw [fill] (0,0) circle [radius=2pt];}}]
% \draw[->] (0,1) -- (3,1);
% \draw[->] (0,1) -- (0,4);
% \node[text width=0.25cm] at (3,1.25){$x$};
% \node[text width=0.25cm] at (0.25,4){$y$};
%
% \node[text width=2cm, align=center] (1) at (2,5){$\omega t= 0$};
% \node[text width=2cm, align=center] (1) at (5,5){$\omega t= \frac{\pi}{2}$};
% \node[text width=2cm, align=center] (1) at (8,5){$\omega t= \pi$};
% \node[text width=2cm, align=center] (1) at (11,5){$\omega t= \frac{3\pi}{2}$};
% \node[text width=2cm, align=center] (1) at (14,5){$\omega t= 2\pi$};
% \begin{scope}[]
% \draw[dots along my path]  (2,3) ellipse (1 and 1);
% \end{scope}
% \begin{scope}[shift={(3,-0.5)},transform canvas={yscale=1.2}]
% \draw[dots along my path]  (2,3) ellipse (1 and 1);
% \end{scope}
% \begin{scope}[shift={(6,0)}]
% \draw[dots along my path]  (2,3) ellipse (1 and 1);
% \end{scope}
% \begin{scope}[shift={(7.2,0)},transform canvas={xscale=1.2},rotate around
% ={45,(9.2,3)}] \draw[dots along my path]  (2,3) ellipse (1 and 1);
% \end{scope}
% \begin{scope}[shift={(12,0)}]
% \draw[dots along my path]  (2,3) ellipse (1 and 1);
% \end{scope}
% \end{tikzpicture}
% \caption{Gravitational wave.}
% \end{figure}

\section{Degrees of Freedom (DoF) and Scalar Vector Tensor (SVT) Decomposition in Space/Time}

Parametrize the line element as
\begin{equation}
    \dif s^2 = - \left(1+2\Phi\right)\dif t^2 + \tensor{v}{_i} \left(\dif t \dif \tensor{x}{^i} + \dif t \dif \tensor{x}{^i}\right) + \left(\tensor{\delta}{_{ij}} + \tensor{h}{_{ij}} \right)\dif \tensor{x}{^i}\dif \tensor{x}{^j}\,.
\end{equation}
Block matrix
\begin{equation}
    \tensor{h}{_{\mu\nu}} =
    \begin{bmatrix}
        \tensor{h}{_{00}} & \tensor{h}{_{0j}} \\
        \tensor{h}{_{i0}} & \tensor{h}{_{ij}}
    \end{bmatrix}
    =
    \begin{bmatrix}
        -2\Phi & \tensor{v}{_{j}} \\
        \tensor{v}{_{i}} & \tensor{h}{_{ij}}
    \end{bmatrix}
    \, , \quad \abs{\Phi},\abs{\tensor{v}{_i}},\abs{\tensor{h}{_{ij}}} \ll 1\,.
\end{equation}
In general algebraic decomposition: symmetric/antisymmetric
\begin{equation}
    \tensor{T}{_{\mu\nu}} = \tensor*{T}{_{\mu\nu}^{\text{s}}} + \tensor*{T}{_{\mu\nu}^{\text{as}}} = \tensor{T}{_{(\mu\nu)}} + \tensor{T}{_{[\mu\nu]}} = \frac{1}{2}\left(\tensor{T}{_{\mu\nu}}+\tensor{T}{_{\nu\mu}}\right) + \frac{1}{2}\left(\tensor{T}{_{\mu\nu}}-\tensor{T}{_{\nu\mu}}\right)\,.
\end{equation}
Can we decompose $\tensor*{T}{_{\mu\nu}^{\text{s}}}$ further?\newline
Wake the trace
\begin{equation}
    \tensor{T}{^{\text{s}}} = \tensor{g}{^{\mu\nu}}\tensor*{T}{_{\mu\nu}^{\text{s}}}\, ,
\end{equation}
so we can write $\tensor*{T}{_{\mu\nu}^{\text{s}}}$ as follows:
\begin{equation}
    \tensor*{T}{_{\mu\nu}^{\text{s}}} = \tensor*{T}{_{\mu\nu}^{\text{tf}}} + \frac{1}{d}\,\tensor{g}{_{\mu\nu}}\tensor{T}{^{\text{s}}}\,,
\end{equation}
where $d$ denotes the dimension of spacetime and $\tensor*{T}{_{\mu\nu}^{\text{tf}}}$ is a tracefree, symmetric tensor.
Can we decompose $\tensor*{T}{_{\mu\nu}^{\text{tf}}}$ any further? Possible for tensor fields $\tensor{T}{_{\mu\nu}}(x)$ as this involves derivatives.
Decompose metric perturbations:
\begin{equation}
    \tensor{h}{_{00}} = -2\Phi
\end{equation}
\begin{equation}
    \tensor{h}{_{0i}} = \tensor{v}{_i}
\end{equation}
\begin{equation}
    \tensor{h}{_{ij}} = 2\tensor{s}{_{ij}} - 2\Psi\tensor{s}{_{ij}}\,\quad
    \begin{cases}
\Psi := -\frac{1}{6}\tensor{\delta}{^{ij}}\tensor{h}{_{ij}}\,\quad
&\text{``trace''} \\
\tensor{s}{_{ij}} := \frac{1}{2} \left(\tensor{h}{_{ij}} - \frac{1}{3}\tensor{\delta}{^{kl}}\tensor{h}{_{kl}}\tensor{\delta}{_{ij}}\right)\,\quad &\text{``strain''}
\end{cases}
\end{equation}
\begin{equation}
    \dif s^2 = -\left(1+2\Phi\right)\dif t^2 + \tensor{v}{_i} \left(\dif t \dif \tensor{x}{^i} + \dif t \dif \tensor{x}{^i}\right) + \left[\left(1-2\Psi\right)\tensor{\delta}{_{ij}} + 2 \tensor{S}{_{ij}} \right]\dif \tensor{x}{^i}\dif \tensor{x}{^j}
\end{equation}
$\tensor{\partial}{_i} = \tensor{k}{_i}$ (momentum space)
Decompose vector $\tensor{\omega}{_i}$ into transverse and longitudinal components.
\begin{align}
    \tensor{v}{_i} = &\tensor{S}{_i} + \tensor{\partial}{_i}B\\
    &3\,\,+\,\,\,\,\,1\,\,\,\,\underbrace{-1}_{\tensor{\partial}{_i}\tensor{S}{^i}=0}\,\quad \text{DoF}
\end{align}
\begin{equation}
    \tensor{S}{_{ij}} = \tensor{\partial}{_{(i}}\tensor{F}{_{j)}} + \left( \tensor{\partial}{_i}\tensor{\partial}{_j} - \frac{1}{3}\,\tensor{\delta}{_{ij}}\Delta\right) E + \tensor*{h}{_{ij}^{\text{\tiny TT}}}
\end{equation}
\begin{center}
    \begin{tabular}{c l}
        $4$ & 4 scalars \\
        $+$ & \\
        $2\,(3-1)$ & 2 transverse vectors \\
        $+$ & \\
        $6-1-3$ & 1 symmetric transverse traceless tensor \\
        \midrule
        $10$ & independent components of $\tensor{h}{_{\mu\nu}}$
    \end{tabular}
\end{center}
valid for (reduce free components):
\begin{equation}
    \tensor{\partial}{_i}\tensor{F}{^i}=0\,,\quad\tensor*{h}{_{ij}^{\text{\tiny TT}}}=\tensor*{h}{_{ji}^{\text{\tiny TT}}}\,,\quad\tensor*{h}{_{ij}^{\text{\tiny TT}}}\tensor{\delta}{^{ij}}=0\,,\quad\tensor{\partial}{^i}\tensor*{h}{_{ij}^{\text{\tiny TT}}}=0
\end{equation}

\begin{figure}[hbtp!]
\centering
 \includegraphics{antisymmetricMatrix.pdf}
\caption{}
%TODO Caption
\end{figure}
\begin{figure}[hbtp!]
\centering
 \includegraphics{Coorddist.pdf}
\caption{}
%TODO Caption
\end{figure}

\begin{figure}[hbtp!]
\centering
 \includegraphics[scale=.75]{ellipse1.pdf}
 \includegraphics[scale=.75]{ellipse2.pdf}
\caption{}
%TODO Caption
\end{figure}

\subsection{Decomposition for tensor \emph{fields}}
Tensor fields depend on the space-time coordinate $x$.
We can build tensors from derivatives of one-rank tensors.
%irgendwas mit $\nabla_i = \partial_i$
Each derivative $\partial_i$ corresponds to $k_i$ in momentum space.
$k_i$ points in some direction.
We can decompose e.g.\ $v_i$ with respect to the direction $k_i$.
\begin{equation}
    v_i = v_{i,\perp} + v_{i,\parallel},
\end{equation}
where $v_{i,\perp}$ is perpendicular and $v_{i,\parallel}$ parallel to $k_i$.
The transverse vector is divergence free:
\begin{equation}
    \tensor{\partial}{_i} \tensor{v}{_\perp^i} = 0 \qquad \left( \implies \vec{k} \perp \vec{v} \right)
\end{equation}
The longitudinal vector $\vec{v}_\parallel$ is curl free.
\begin{equation}
    \tensor{\epsilon}{^i^j^k} \tensor{\partial}{_j} \tensor{v}{_\perp_k} = 0
\end{equation}
$\implies$ conditions for differential equations, makes only sense when applied to a vector field.
The transverse vector can be represented as curl of some other vector:
\begin{equation}
    \tensor{v}{^i_\perp} = \tensor{\epsilon}{^i^j^k} \tensor{\partial}{_i} \tensor{u}{_k} \,.
\end{equation}
The choice of $u_i$ is not ??? unless we impose additional conditions on $u$:
\begin{equation}
    \tensor{\partial}{_i} \tensor{u}{^i} = 0 \qquad \left( \text{``divergence free''} \right)
\end{equation}
The longitudinal vector is the divergence of a scalar function $\lambda(x)$
\begin{equation}
    \tensor{v}{_\partial_j} = \tensor{\partial}{_j} \lambda(x)
\end{equation}
Similarly, we can decompose the strain vector $\tensor{s}{_i_j}$:
\begin{equation}
    \tensor{s}{^i^j} = \tensor{s}{_{\perp}^i^j} + \tensor{s}{_s^i^j} + \tensor{s}{_{\parallel}^i^j}
    \qquad \left( \text{symmetric, trace free}, \tensor{s}{_i_j} = \tensor{s}{_j_i}, \tensor{\delta}{_i_j} \tensor{s}{^i^j} \right)
\end{equation}
with the transverse part $\tensor{s}{_\perp^i^j}$, the solenoidal part $\tensor{s}{_s^i^j}$,
and the parallel part $\tensor{s}{_\parallel^i^j}$.
\begin{itemize}
    \item The transverse part is divergence free $ \tensor{\delta}{_i} \tensor{s}{_\perp^i^j} = 0$.
    \item The divergence of the solenoidal part is a transverse vector,
    \begin{equation}
        \tensor{\partial}{_i} \tensor{\partial}{_j} \tensor{s}{_s^i^j} = 0
    \end{equation}
    \item The divergence of the longitudinal part is a longitudinal vector
    \begin{equation}
        \tensor{\epsilon}{^j^k^l} \tensor{\partial}{_l} \tensor{\partial}{_i} \tensor{s}{_\parallel^i_j} = 0
    \end{equation}
\end{itemize}
The longitudinal part can be constructed from a scalar field $E$:
\begin{equation}
    \tensor{s}{^\parallel_i_j} = \left( \tensor{\partial}{_i} \tensor{\partial}{_j} - \frac{1}{3} \tensor{\partial}{_i_j} \Delta \right) E
\end{equation}
The part in the braces is the only object with two derivatives of a scalar with two free indices.
The coefficients are fixed by $ \tensor{\delta}{^i^j} \tensor{s}{^\parallel_i_j} = 0$ (trace free).
The solenoidal part can be constructed from a traversal vector field $F_i$.


\section{Meromorphic Functions; A Worked Example}
\subsection{Meromorphic Functions}
Recall that the only analytic maps from a compact Riemann surface to $\mathbb C$ are constants since $\mathbb C$ is not compact.
Therefore, it is more useful to consider a bigger and compact range, i.e. the Riemann sphere $\mathbb C_\infty$.
\begin{definition}
    A meromorphic function on a Riemann surface $R$ is an analytic map $f:R\to\mathbb C_\infty$ that is not constantly $\infty$.
\end{definition}
First we do some ground work for consistency.
\begin{proposition}
    Let $D\subset\mathbb C$ be a domain and $A\setminus D$ a discrete subset.
    A function $f:D\to\mathbb C_\infty$ is meromorphic if and only if $f:D\setminus A\to\mathbb C$ is analytic, $f(A)=\{\infty\}$ and $f$ has a pole at each $a\in A$.
\end{proposition}
\begin{proof}
    Quite obvious honestly, but we are gonna go though it.
    For the ``only if'' direction, let $A=f^{-1}(\{\infty\})$, then $f$ obviously restricts analytically to $\mathbb C$ on $D\setminus A$.
    It remains to show that each $a\in A$ is a pole.
    Working in the standard atlas on $\mathbb C_\infty$ and pick that chart about $\infty$.
    We see that $(f(z))^{-1}=(z-a)^mg(z)$ in a neighbourhood of $a$ where $m\ge 1$ and $g$ is analytic with $g(a)\neq 0$.
    Then $f(z)=(z-a)^{-m}/g(z)$ on an even small domain where $g$ does not have any root.
    This shows immediately that $a$ is a pole.\\
    For the ``if'' direction, then again $f$ is obviously analytic on $D\setminus A$.
    At each $a\in A$, we know that $f(z)=(z-a)^{-m}h(z)$ on a neighbourhood of $a$ with $h(0)\neq 0$.
    Restricting to an even smaller domain where $h$ does not have any root, then $1/f(z)=(z-a)^m/h(z)$, so $f$ is analytic at each $a\in A$ as well by checking the standard atlas.
\end{proof}


\chapter{Inverse linear problems}

The main objective of the course is dealing with the following objects:

\begin{equation}
	\int_{\Omega} (input) (system) = output
\end{equation}

In inverse linear problems we usuall


\documentclass[10pt, reqno]{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{amsthm}
%american mathematical society - AMS

\usepackage{hyperref}
\usepackage{float}
\usepackage{enumerate}
\usepackage{graphicx}
\usepackage{wrapfig}
\usepackage{subfig}

\oddsidemargin = 0in
\textwidth=6.5in

\numberwithin{equation}{section}

\numberwithin{figure}{section}

\title{The Trapezoidal rule}
\author{Johnny Minor}
\date{\today}

\begin{document}

\maketitle

\section{Introduction}
Beyond the pages of introductory calculus textbooks mathematicians, engineers, and physicists will frequently encounter functions that are integrable, but do not have antiderivatives. As we know quite well the integral of a function can be thought of as the area underneath that specific curve. This geometric interpretation of the integral can be leveraged when dealing with functions that are not well behaved.
In order to use this geometric interpretation we employ trapezoids, and a computer program to solve for the integral.

\section{Trapezoids}
\label{sec:trapz}

Trapezoids seem like a reasonable shape to use to find the sum of an unknown area. This is because the characteristics of trapezoids lend well to conforming to many different shapes. Often times functions that are not well behaved end up being very strange shapes. In comparison to a well behaved periodic function such as $y = \sin(x) $ or $y = \cos(x)$. Figure \ref{fig:trapz} is an illustration of how many different shapes and sizes that a trapezoid can take on depending on the length of its sides and also the angle in the corners.

\begin{figure}[H] % 'h' means put it here and 'ht' means that put it at top the and here. 'ht!' put it here now!
\centering
\includegraphics[width=0.4\textwidth]{Trapezoid_special_cases.png}
\caption{Special cases of trapezoids\cite{ref:trapz}}
\label{fig:trapz}
\end{figure}

\noindent Figure \ref{fig:trapz} should be taken as clear motivation for why we use trapezoids instead of triangles, or octahedrons.
Furthermore, we choose trapezoids because when we have a function $f(x)$ we can easily calculate two adjacent points. From these two points we can then form a trapezoid. This is essentially what the two point trapezoidal rule is.

\section{The Trapezoid Rule}

To begin with the trapezoid rule we first define the area of integration to be the closed interval $[a,b]$. We then divide this interval into $h$ sections.
\begin{equation}
\label{eq:meshspacing}
h = \frac{b-a}{n}
\end{equation}

\noindent Where $n$ is the number of intervals, $b$ is the end of interval and $a$ is the beginning of the interval, and $h$ is how large each interval will be. $h$ is often referred to as the mesh spacing. We have now determined how large each section in the interval should be in order to be equal. This means we need  $n + 1$ points. We will evaluate the function we are trying to integrate at these points.

A natural question arises about why we need $n+1$ points. This is a common snag in many people's intuitive understanding of dividing an interval into equal sections, and is often referred to as the "Fencepost error" in Applied Mathematics and Computer Science. For example:

\medskip

\noindent \textit{If you want to build a straight fence 30 meters long with posts spaced 3 meters apart, how many posts do you need?}

\medskip

\noindent Well, the intuitive answer for most people is to think: $30/3 = 10$ so we must need 10 posts. Unfortunately that is not correct, and we really need 11 posts! If you don't believe me you can look at this diagram

\begin{figure}[ht!] % 'h' means put it here and 'ht' means that put it at top the and here. 'ht!' put it here now!
\centering
\includegraphics[width=0.4\textwidth]{Fencepost.png}
\caption{An illustration on the fence post problem \cite{ref:fencepost}}
\label{fig:fencepost}
\end{figure}

\noindent This shows without question that we will indeed $n+1$ points in order to create our trapezoids with spacing of $h$.

Now that we have split out interval into intervals we need to form our trapezoids. First, our points which we will evaluate our function at will be defined as

$$
x_0 = a, x_1 = a + \Delta x, x_2 = a + 2\Delta x, \dots x_n = a + n\Delta x = b.
$$

From these $x$ values we need to calculate the corresponding output of our function of interest. This will look like
$$
y_0 = f(x_0), y_1 = f(x_1), y_2 = f(x_2), \dots, y_n = f(x_n)
$$

Now that we have the $x$ values and the $y$ values we can then form $n$ trapezoids throughout or closed interval. To find the area of a trapezoid by breaking our trapezoid into a square of length $y_0$ and then a right triangle with height $y_1 - y_0$. This expression will be

\begin{equation}
\label{eq:area}
A = y_0 h + \frac{1}{2}(y_1 - y_0)h.
\end{equation}

The first term in the sum is the area of the square, and the second term is the area of the left over triangle. In equation \ref{eq:area} $A$ is the area of the trapezoid $y_0$ is $f(x_0)$ analogously $y_1$ is $f(x_1)$, and $h$ is the mesh spacing from equation \ref{eq:meshspacing}(or change in $x$ between adjacent trapezoids).
If we apply this area to our $n$ trapezoids we end up with the expression

\begin{equation}
\int_{a}^{b} f(x)dx \approx \frac{(y_0 + y_1)h}{2} + \frac{(y_1 + y_2)h}{2} + \frac{(y_2 + y_3)h}{2} + \cdots + \frac{(y_n + y_n)h}{2}
\end{equation}

\noindent With some work this will simplify to the trapezoidal rule formula.

\begin{equation}
\int_{a}^{b} f(x) \approx \frac{h}{2}(y_0 + 2y_1 + 2y_2 + \cdots + 2y_{n-1} + y_n)
\end{equation}

\theoremstyle{definition}
\newtheorem{exmp}{Example}[section]

\begin{exmp}
Consider the function $f(x) = \cos(x)$ on the closed interval $[0,\pi/2]$. If we choose $n$ equal to 3 then the function with the trapezoids will look like this

\begin{figure}[H] % 'h' means put it here and 'ht' means that put it at top the and here. 'ht!' put it here now!
\centering
\includegraphics[width=0.4\textwidth]{HW6-plot.png}
\caption{Trapezoidal Rule on 3 Subintervals}
\label{fig:cosine_example}
\end{figure}

It should be noted that when $n$ is chosen to be relatively small that the trapezoidal rule will underestimate a function that is concave down on the interval and alternatively will overestimate a function that is concave up. This can be seen by inspection of the unaccounted white space seen in figure \ref{fig:cosine_example}.
The error equation for the Trapezoidal Rule is\cite{ref:Atkinson}

\begin{equation}
\label{eq:error}
E_{n}^{T}(f) \equiv \int_{a}^{b} f(x) dx - T_n(f) = - \frac{h^2 (b-a)}{12} f''(c_n).
\end{equation}

\noindent Equation\ref{eq:error} shows that the error decreases approximately proportional to to $h^2$. Therefore, if $n$ is doubled and therefor halving $h$. The resulting error would be approximately $2^2$ or 4.\cite{ref:Atkinson}

\begin{table}[H]
\begin{center}
\caption{Trapezoidal Rule for $f(x)$}
\begin{tabular}{|c|l c|}
\hline
\boldmath$2h/\pi$ & \multicolumn{1}{|c}{\textbf{Integral}} & \multicolumn{1}{c|}{\textbf{Error}} \\ \hline
%\rule{0pt}{3ex} this would add more vertical space, but I don't really like it...
$10^{-1}$ & 0.99794299 & $2.057 \times 10^{-3}$ \\
$10^{-2}$ & 0.99997944 & $2.056 \times 10^{-5}$ \\
$10^{-3}$ & 0.9999979 & $2.056 \times 10^{-7}$ \\
$10^{-4}$ & $\sim 1$ & $2.056 \times 10^{-9} $\\
$10^{-5}$ & $\sim 1$ & $2.057 \times 10^{-11}$ \\
\hline
\end{tabular}
\end{center}
\end{table}

\noindent As we increase $n$ it can be seen that precision increases quickly, and with large enough $n$(or small $h$) converge to the analytic solution of 1. Naysayers might protest noting that we could have integrated $f(x) = \cos(x)$ easily and found the analytic solution with considerably less work. However, we have proven that the trapezoidal rule is a very accurate method for integrating periodic functions without anti derivatives.

\end{exmp}

\section{Conclusion}

We have motivated the use of trapezoids rather than other geometric objects. They are able to change shape to match many different features found in functions. We then developed the general expression for the Trapezoid Rule for a general function $f(x)$ on a closed interval $[a,b]$. We then illustrated the usage of the Trapezoidal Rule on the function $f(x) = \cos(x)$ on $[0,\pi/2]$, and showed that using the Trapezoidal Rule the solution can be found to be the same as the analytic solution. Thus proving that the Trapezoidal Rule is indeed an accurate and effective method for calculating the integral of functions without anti derivatives.

\begin{thebibliography}{XX}

\bibitem{ref:trapz} %when we refer to it by name we use this name.
Tomruen(\url{https://commons.wikimedia.org/wiki/File:Trapezoid_special_cases.png})[Public Domain], via Wikimedia Commons

\bibitem{ref:fencepost} %when we refer to it by name we use this name.
Acdx (\url{https://commons.wikimedia.org/wiki/File:Fencepost_error.svg})[Public Domain], via Wikimedia Commons

\bibitem{ref:Cazelais}
Gilles Cazelais, \textbf{The Trapezoid Rule}, Camosun College, Victoria, British Columbia, \today.

\bibitem{ref:Atkinson}
Kendall E Atkinson, \textbf{Trapezoidal Method Error Formula}, University of Iowa, Iowa City, Iowa, \today.
\end{thebibliography}

\end{document}


We set up a simple scenario for assessing the performance of the method for
estimating marginal likelihoods based on approximating the likelihood function
with a general linear model (GLM) fitted to posterior samples collected via
approximate-likelihoood Bayesian computation (ABC) \citep{Leuenberger2010};
hereforth referred to as ABC-GLM.
The scenario is a DNA sequence, 10,000-nucleotides in length, that evolves
along a branch according to a Jukes-Cantor continuous-time Markov chain (CTMC)
model of nucleotide substitution \citep{JC1969}.
Because the Jukes-Cantor model forces the relative rates of change among the
four nucleotides and the equilibrium nucleotide frequencies to be equal, there
is only a single parameter in the model, the length of the branch, and the
direction of evolution along the branch does not matter.

\subsection{Simulating data sets}
We simulated 100 data sets under this model by
\begin{enumerate}
    \item drawing 10,000 nucleotides of the ``ancestral'' sequence from their
        equilibrium frequencies ($\frac{1}{4}$),
    \item drawing a branch length $\sim$ uniform(0.0001, 0.1), and
    \item evolving the sequence along the branch according to the Jukes-Cantor
        CTMC model to get the ``descendant'' sequence.
\end{enumerate}
This was done using the DendroPy phylogenetic API (version 4.3.0 commit
72ce015) \citep{Sukumaran2010}.

\subsection{Calculating ``true'' Bayes factors}
For each data set, we used quadrature approaches to approximate the marginal
likelihood by integrating the posterior density over the branch length prior.
We did this for two models:
\begin{enumerate}
    \item the correct model [branch length $\sim$ uniform(0.0001, 0.1)], and
    \item a model with a branch length prior slightly more than twice as broad
        [branch length $\sim$ uniform(0.0001, 0.2)], which we refer to as the
        ``vague model''.
\end{enumerate}
For both models and for each dataset we used the rectangular and trapezoidal
quadrature rules with 1,000 and 10,000 steps (i.e., four approximations of the
marginal likelihood for each data set under each model).
Across all 100 data sets and both models, all four approximations were
identical to at least five decimal places.
For each data set, we calculated the log Bayes factor comparing the correct
model to the vague model.

\subsection{Approximate-likelihood Bayesian computation}
To collect an approximate posterior sample from the correct model for a data
set, we first calculated the proportion of variable sites (\pvar) between the
two sequences.
Next, we simulated 50,000 datasets under the correct model, calculated \pvar
for each of them, and retained the 1,000 samples with the values of \pvar
closest to that calculated from the data.
Lastly, we used ABCtoolbox version 1.1 \cite{ABCtoolbox} to fit a GLM to the
retained samples and calculate the marginal density of the GLM, using a
bandwidth of 0.002.
We did the same to obtain an ABC-GLM estimate of the marginal density
for the vague model with two differences:
(1) we drew the branch length for each prior sample from the vague prior
[branch length $\sim$ uniform(0.0001, 0.2)], and
(2) to maintain the same expected tolerance under both models, we simulated
100,000 datasets under the vague model (retaining the 1,000 samples closest to
the \pvar of the data).

For each data set, we calculated the log Bayes factor from the GLM marginal
densities of the correct and vague model, and compared the ABC-GLM-estimated
Bayes factor to the ``true'' Bayes factor calculated via quadrature
integration (Figure~\ref{fig:glmPerformance}).

\subsection{Full-likelihood Markov chain Monte Carlo analyses}
One goal of the simplicity of the above model is that the additional
approximation of the ABC approach would be limited.
All numerical Bayesian analyses, based on full or approximate likelihoods,
suffer from Monte Carlo error associated with approximating the posterior with
a finite number of samples.
Approximate-likelihood methods usually suffer from two additional sources of
approximation:
(1) the full data are replaced with insufficient summary statistics, and
(2) samples are retained that do not exactly match the data or summary
statistics (i.e., the ``tolerance'' of ABC).
In our analyses described above, we avoided the former source of error by
using a sufficient statistic.
We hoped to minimize the latter source of error by evaluating many samples from
a one-dimensional model with finite bounds; we also kept this source of error
approximately equal for both models by sampling in proportion to the width of
the model.

To verify that the error introduced by the tolerance of the ABC analyses was
minimal, we compared the branch length estimates to those estimated by
full-likelihood Markov chain Monte Carlo (MCMC).
For each data set, under both models, we ran a chain for 10,000 generations,
sampling every 10 generations.
All chains appeared to reach stationarity by the first sample (10th
generation).
We plotted the branch length estimated via ABC-GLM and MCMC under both
the true and vague models against the true branch lengths.
The results of all four analyses across all 100 data sets are almost
indistinguishable (Figure~A\ref{fig:branchLengthEstimates}), confirming that
the approximation introduced by the tolerance is very minimal.
Our ABC-GLM analyses are essentially equivalent to full-likelihood Bayesian
analyses, creating a ``best-case scenario'' for evaluating the marginal
likelihood estimates of the ABC-GLM method.

\embedAppendixFigure{images/branch-length-estimates.pdf}{
    A comparison of the true branch length separating each pair of simulated
    sequences to the branch length estimated by ABC-GLM and full-likelihood
    MCMC under the correct branch-length model (branch length $\sim$ uniform(0.0001,
    0.1)) and the vague model (branch length $\sim$ uniform(0.0001, 0.1)).
}{fig:branchLengthEstimates}

\subsection{Reproducibility}
All of the code to replicate our results is freely available at
\href{https://github.com/phyletica/abc-glm-marginal-test}{https://github.com/phyletica/abc-glm-marginal-test}.


\section{Discrete Risk Measures}
\label{DiscreteRiskMeasures}
This Post-Processor calculates a series of risk importance measures from a PointSet.
This calculation is performed for a set of input parameters given an output target.

The user is required to provide the following information:
\begin{itemize}
   \item the set of input variables. For each variable the following need to be specified:
     \begin{itemize}
       \item the set of values that imply a reliability value equal to $1$ for the input variable
       \item the set of values that imply a reliability value equal to $0$ for the input variable
     \end{itemize}
   \item the output target variable. For this variable it is needed to specify the values of
      the output target variable that defines the desired outcome.
\end{itemize}

The following variables are first determined for each input variable $i$:
\begin{itemize}
   \item $R_0$ Probability of the outcome of the output target variable (nominal value)
   \item $R^{+}_i$ Probability of the outcome of the output target variable if reliability of the input variable is equal to $0$
   \item $R^{-}_i$ Probability of the outcome of the output target variable if reliability of the input variable is equal to $1$
\end{itemize}

Available measures are:
\begin{itemize}
   \item Risk Achievement Worth (RAW): $RAW = R^{+}_i / R_0 $
   \item Risk Achievement Worth (RRW): $RRW = R_0 / R^{-}_i$
   \item Fussell-Vesely (FV): $FV = (R_0 - R^{-}_i) / R_0$
   \item Birnbaum (B): $B = R^{+}_i - R^{-}_i$
\end{itemize}

\ppType{RiskMeasureDiscrete}{RiskMeasureDiscrete}

In the \xmlNode{PostProcessor} input block, the following XML sub-nodes are required,
independent of the \xmlAttr{subType} specified:

\begin{itemize}
   \item \xmlNode{measures}, \xmlDesc{string, required field}, desired risk importance measures
      that have to be computed (RRW, RAW, FV, B)
   \item \xmlNode{variable}, \xmlDesc{string, required field}, ID of the input variable. This
      node is provided for each input variable. This nodes needs to contain also these attributes:
     \begin{itemize}
       \item \xmlAttr{R0values}, \xmlDesc{float, required field}, interval of values (comma separated values)
          that implies a reliability value equal to $0$ for the input variable
       \item \xmlAttr{R1values}, \xmlDesc{float, required field}, interval of values (comma separated values)
          that implies a reliability value equal to $1$ for the input variable
     \end{itemize}
   \item \xmlNode{target}, \xmlDesc{string, required field}, ID of the output variable. This nodes needs to
      contain also the attribute \xmlAttr{values}, \xmlDesc{string, required field}, interval of
      values of the output target variable that defines the desired outcome
\end{itemize}

\textbf{Example:}
This example shows an example where it is desired to calculate all available risk importance
measures for two input variables (i.e., pumpTime and valveTime)
given an output target variable (i.e., Tmax).
A value of the input variable pumpTime in the interval $[0,240]$ implies a reliability
value of the input variable pumpTime equal to $0$.
A value of the input variable valveTime in the interval $[0,60]$ implies a reliability
value of the input variable valveTime equal to $0$.
A value of the input variables valveTime and pumpTime in the interval $[1441,2880]$ implies a
reliability value of the input variables equal to $1$.
The desired outcome of the output variable Tmax occurs in the interval $[2200,2500]$.
\begin{lstlisting}[style=XML,morekeywords={subType,debug,name,class,type}]
<Simulation>
  ...
  <Models>
    ...
    <PostProcessor name="riskMeasuresDiscrete" subType="RiskMeasuresDiscrete">
      <measures>B,FV,RAW,RRW</measures>
      <variable R0values='0,240' R1values='1441,2880'>pumpTime</variable>
      <variable R0values='0,60'  R1values='1441,2880'>valveTime</variable>
      <target   values='2200,2500'                  >Tmax</target>
    </PostProcessor>
    ...
  </Models>
  ...
</Simulation>
\end{lstlisting}

This Post-Processor allows the user to consider also multiple datasets (a data set for each initiating event)
and calculate the global risk importance measures.
This can be performed by:
\begin{itemize}
  \item Including all datasets in the step
\begin{lstlisting}[style=XML,morekeywords={subType,debug,name,class,type}]
<Simulation>
  ...
  </Steps>
    ...
    <PostProcess name="PP">
      <Input   class="DataObjects"  type="PointSet"        >outRun1</Input>
      <Input   class="DataObjects"  type="PointSet"        >outRun2</Input>
      <Model   class="Models"       type="PostProcessor"   >riskMeasuresDiscrete</Model>
      <Output  class="DataObjects"  type="PointSet"        >outPPS</Output>
      <Output  class="OutStreams"   type="Print"           >PrintPPS_dump</Output>
    </PostProcess>
  </Steps>
  ...
</Simulation>
\end{lstlisting}
  \item Adding in the Post-processor the frequency of the initiating event associated to each dataset
\begin{lstlisting}[style=XML,morekeywords={subType,debug,name,class,type}]
<Simulation>
  ...
  <Models>
    ...
    <PostProcessor name="riskMeasuresDiscrete" subType="SR2ML.RiskMeasuresDiscrete">
      <measures>FV,RAW</measures>
      <variable R1values='-0.1,0.1' R0values='0.9,1.1'>Astatus</variable>
      <variable R1values='-0.1,0.1' R0values='0.9,1.1'>Bstatus</variable>
      <variable R1values='-0.1,0.1' R0values='0.9,1.1'>Cstatus</variable>
      <variable R1values='-0.1,0.1' R0values='0.9,1.1'>Dstatus</variable>
      <target   values='0.9,1.1'>outcome</target>
      <data     freq='0.01'>outRun1</data>
      <data     freq='0.02'>outRun2</data>
    </PostProcessor>
    ...
  </Models>
  ...
</Simulation>
\end{lstlisting}

\end{itemize}

This post-processor can be made time dependent if a single HistorySet is provided among the other data objects.
The HistorySet contains the temporal profiles of a subset of the input variables. This temporal profile can be only
boolean, i.e., 0 (component offline) or 1 (component online).
Note that the provided history set must contains a single History; multiple Histories are not allowed.
When this post-processor is in a dynamic configuration (i.e., time-dependent), the user is required to specify an xml
node \xmlNode{temporalID} that indicates the ID of the temporal variable.
For each time instant, this post-processor determines the temporal profiles of the desired risk importance measures.
Thus, in this case, an HistorySet must be chosen as an output data object.
An example is shown below:
\begin{lstlisting}[style=XML,morekeywords={subType,debug,name,class,type}]
<Simulation>
  ...
  <Models>
    ...
    <PostProcessor name="riskMeasuresDiscrete" subType="SR2ML.RiskMeasuresDiscrete">
      <measures>B,FV,RAW,RRW,R0</measures>
      <variable R1values='-0.1,0.1' R0values='0.9,1.1'>Astatus</variable>
      <variable R1values='-0.1,0.1' R0values='0.9,1.1'>Bstatus</variable>
      <variable R1values='-0.1,0.1' R0values='0.9,1.1'>Cstatus</variable>
      <target   values='0.9,1.1'>outcome</target>
      <data     freq='1.0'>outRun1</data>
      <temporalID>time</temporalID>
    </PostProcessor>
    ...
  </Models>
  ...
  <Steps>
    ...
    <PostProcess name="PP">
      <Input     class="DataObjects"  type="PointSet"        >outRun1</Input>
      <Input     class="DataObjects"  type="HistorySet"      >timeDepProfiles</Input>
      <Model     class="Models"       type="PostProcessor"   >riskMeasuresDiscrete</Model>
      <Output    class="DataObjects"  type="HistorySet"      >outHS</Output>
      <Output    class="OutStreams"   type="Print"           >PrintHS</Output>
    </PostProcess>
    ...
  </Steps>
  ...
</Simulation>
\end{lstlisting}


\input{SingleAssignmentSetup.tex}
\input{../WeekTitles.tex}
\usepackage{bbding} % for Checkmarkbold
\begin{document}

\newcommand{\ub}{\underbrace}

\begin{center}
\subsection*{MNTC P01 - Week \#6 - \WeekTitleSix}
\end{center}

\subsection*{Numerical Integration}

\begin{Question} As part of this assignment, you should be able to reproduce the
  LHR rule calculations in MATLAB using a loop. You should know how to
  adapt it to handle either data from a file, or a function defined by
  a formula, as the case requires.
\end{Question}

\begin{enumerate}[1.]
% ******************************
\item \begin{Question}
{\bf Theory} Consider the problem of estimating the general form
  of the integral $$\int_a^b f(x)~dx$$
\begin{enumerate}[(a)]
\item Assume $f(x)$ is a smooth and continuous function.  For our the
  Left-Hand sum, LEFT($n$), by what factor do we reduce the error if
  we use 10 times the number of intervals?

\item Evaluate the integral $\ds \int_0^6 \cos(x)~dx$ exactly, using
  anti-derivatives and the Fundamental Theorem of Calculus.

\item Confirm your answers to part (a) by finding the change in the
  error for LEFT($n$) for the same integral, $\ds \int_0^6 \cos(x)~dx$
  using $n=20$, $n=200$, and $n = 2000$.  Find the error with each $n$
  value, and then compute the ratio of the errors each time you use 10$\times$ as many intervals.
\end{enumerate}
\end{Question}

\begin{Solution}
\begin{enumerate}[(a)]
\item If $f(x)$ is a smooth function, if we use 10 times the
  intervals, the error for LHR's estimate will drop by a factor of $\frac{1}{10}$
\item We can evaluate the integral based on our earlier study of
  derivatives. The only thing to be careful of is that the input
  values will be in radians (not degrees).
  \begin{align*}
    \int_0^6 \cos(x)~dx = \sin(x) \Big|_0^6 = \sin(6) - \sin(0) = -0.279415498
  \end{align*}

\item  The linked script below computes LEFT($n$) for $n=20, 200$ and 2000.  It also computes
the error for each of those integral estimates.

\href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/W06CosineIntegral.m}{W06CosineIntegral.m}

Here are the results from the script (with the values for \verb@I@
being the integrals, and \verb@Err@ being the error in the estimates.

\lstinputlisting[showstringspaces=false]{MATLAB/W06CosineIntegral_results.txt}

\end{enumerate}

We can see that as we increase the number of intervals by a factor of
10, the error in the LEFT($n$) estimate is scaled by a factor of
(approximately) $\ds \frac{1}{10}$.

\begin{itemize}
\item When going to $n=200$ from $n=20$, the error ratio is $\ds \frac{6.184021769584658e-04}{0.008073223414391} \approx 0.07 \approx 0.1$ or $\ds \frac{1}{10}$.
\item When going to $n=2000$ from $n=200$, the error ratio is $\ds \frac{5.995413167569907e-05}{6.184021769584658e-04}
 \approx 0.097\approx 0.1$ or $\ds \frac{1}{10}$.
\end{itemize}

\end{Solution}

% ******************************
\item \begin{Question}
For each of the following integrals,
\begin{itemize}
\item Evaluate the integral exactly,
\item use MATLAB to compute the LEFT(1000) integral estimate, and
\item comment on the agreement between the results.
\end{itemize}
\begin{enumerate}[(a)]
\item $\displaystyle \int_0^5 x^3 - 5~dx$
\item $\displaystyle \int_1^{10} \log_{10}(x)~dx$
\item $\displaystyle \int_{-1}^{1} x^2 e^{x^3}~dx$
\end{enumerate}
  \end{Question}

\begin{Solution}
 \begin{enumerate}[(a)]
\item Exact value: $\displaystyle \int_0^5 x^3 - 5~dx = \frac{x^4}{4} - 5x \Big|_0^5 =
\left(\frac{5^4}{4} - 5(5)\right) - (0-0) = 131.25$ \\
LEFT(1000) estimate (see link below to the script that computed this):  130.9377

The agreement is pretty good, with the LEFT(1000) within 1\% of the exact value.

\item $\displaystyle \int_1^{10} \log_{10}(x)~dx $.\\ This requires
  integration by parts, and remembering the derivative rule for
  non-base-$e$ logarithms:
  $\ds \frac{d}{dx} \log_{10}(x) = \frac{1}{x \ln(10)}$ Computing the
  integral (without the $x=1$ and $x=10$ for now to keep the notation
  simpler):
    \begin{align*}
\mbox{ We choose }       u & = \log_{10} x&\mbox{ and } dv & = dx \\
\mbox{ so } du & = \frac{1}{x \ln(10)} ~dx& \mbox{ and } v & = x
    \end{align*}
Using the integration by parts formula,
\begin{align*}
  \int \log_{10} x~dx & = x (\log_{10} x)
- \int x \frac{1}{x \ln(10)}~ dx \\
& = x \log_{10} x - \int \frac{1}{\ln(10)} ~dx \\
& = x \log_{10} x - \frac{x}{\ln(10)} + C
\end{align*}
This gives the definite integral value of
\begin{align*}
\int_1^{10} \log_{10}(x)~dx  & = \left(x \log_{10} x - \frac{x}{\ln(10)} \right) \Big|_1^{10} \\
& = \left(10 \log_{10}(10) - \frac{10}{\ln(10)}\right)
- \left(1 \log_{10}(1) - \frac{1}{\ln(10)}\right)  \\
&  = 10 - \frac{10}{\ln(10)} + \frac{1}{\ln(10)} \\
& \approx 6.0914
\end{align*}
LEFT(1000) estimate (see link below to the script that computed this):  6.0868

The agreement is better for this function, with the LEFT(1000) within 0.1\% of the exact value.

\item $\displaystyle \int_{-1}^{1} x^2 e^{x^3}~dx$

This integral can be evaluated exactly using a substitution.

  \begin{align*}
    \mbox{Let }w & = x^3, \mbox{ so } dw=3x^2~dx \mbox{ or } dx = \frac{1}{3x^2}~dw\\
    \int x^2 e^{x^3}~dx & = \int x^2 e^w \left(\frac{1}{3x^2}~dw\right) = \int \frac{1}{3} e^w dw = \frac{1}{3} e^w + C = \frac{1}{3} e^{x^3} + C \\
\end{align*}
This gives the definite integral value of
\begin{align*}
\int_{-1}^{1} x^2 e^{x^3}~dx  & = \frac{1}{3} e^{x^3} \Big|_{-1}^1 \\
& = \frac{1}{3}\left( e^1 - e^{-1}\right) \\
& \approx 0.7834
\end{align*}
LEFT(1000) estimate (see link below to the script that computed this):  0.7811

The agreement is good for this function, with the LEFT(1000) within 1\% of the exact value.
\end{enumerate}
Link to the MATLAB code: \\
\href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/W06LeftExamples.m}{W06LeftExamples.m}
\end{Solution}

% ******************************
\item
  \begin{Question}
    Use the \verb#integral# function to estimate the following
    integrals, and print the estimates with at least 8 digits after
    the decimal.  Use the default accuracy for the \verb#integral#
    function.  Note that the exact values for these integrals were
    computed in the previous question, so we can compare the
    \verb@integral@ estimate to the exact values.
\begin{enumerate}[(a)]
\item $\displaystyle \int_0^5 x^3 - 5~dx$  (exact value: $\displaystyle \frac{5^4}{4} - 25$)
\item $\displaystyle \int_1^{10} 4 \log_{10}(x)~dx$ (exact value:
$\displaystyle \frac{(-36+40 \ln(2)+40 \ln(5))}{\ln(2)+\ln(5)}$
\item $\displaystyle \int_{-1}^{1} x^2 e^{x^3}~dx$
\end{enumerate}
  \end{Question}

\begin{Solution}
Code that does this would be e.g.
\begin{verbatim}
clc
format long
f = @(x) x.^3 - 5;
a = 0;
b = 5;
I = integral(f, a, b);
exact_value = (5^4/4 - 5*5);
[exact_value, I]  % display both the exact value and the integral estimate
\end{verbatim}
A link to the full MATLAB script for all the examples is included below.

The results are
\begin{enumerate}
\item function: \verb#f = @(x) x.^3 - 5;# \\
estimated integral: 131.25000000
\item function: \verb#f = @(x) log10(x);# \\
estimated integral: 6.091349662870734
\item function: \verb#f = @(x) x.^2 .* exp(x.^3);#\\
estimate integral: 0.783467462429201
\end{enumerate}
Note that all these values are the same as the exact values for all or
almost all the digits shown (12 digits of accuracy).  This is a lot
better performance than using the LEFT($n$) approach, and is actually
easier to code because the \verb@integral@ function is built into
MATLAB.

Link to the MATLAB code: \\
\href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/W06IntegralExamples.m}{W06IntegralExamples.m}
\end{Solution}

% ******************************
\item
  \begin{Question}

When a pendulum oscillates, with maximum deviation angle
  $\theta_0$, the period of the pendulum is given by

\begin{align*}
  T = 4 \sqrt{L/g} \int_0^{\pi/2} \frac{dx}{\sqrt{1 - k^2 \sin^2 x}}
\end{align*}

where $k = \sin\left(\frac{1}{2} \theta_0\right)$ and $g$ is the
acceleration due to gravity, $9.8 $ m/s.

Compute and compare the period of a pendulum with
\begin{itemize}
\item $L = 2$, $\theta_0 = 40^o$,
\item $L =2$, $\theta_0 = 20^o$.
\item $L = 2.5$, $\theta_0 = 40^o$,
\item $L =2.5$, $\theta_0 = 20^o$.
\end{itemize}

Describe how significant the effect of maximum swing angle $\theta_0$
is on the period of a pendulum, compared to the effect of the pendulum
length.
  \end{Question}

\begin{Solution}
 Full solution is available in:
\href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/W06PendulumPeriod.m}{W06PendulumPeriod.m}

  The periods computed are show below.  \verb@integral@ was used for the
  integration.

L = 2.0 m, theta0 = 40.0 deg, period = 2.9274 s. \\
L = 2.0 m, theta0 = 20.0 deg, period = 2.8602 s. \\
L = 2.5 m, theta0 = 40.0 deg, period = 3.2729 s. \\
L = 2.5 m, theta0 = 20.0 deg, period = 3.1978 s. \\

From these results, it is clear that the angle has a minimal effect on
the period of the oscillations, compared with the effect of the
length.  This insensitivity of the period to the oscillation angle
explains why pendulum clocks and metronomes do not need a specific
swing angle to be fairly accurate, but {\em do} need a specific
length.
\end{Solution}

%*******************************
\item
\begin{Question}
  When underground mining operations are in progress, one concern the
  monitoring or predicting the subsidence of the rock between the mine
  and the surface, as ``the differential settlement and horizontal
  strain developed during subsidence tend to be critical in terms of
  structural damage''\footnote{SME Mining Engineering Handbook, 3rd
    edition, page 632.}

  If the rock is considered isomorphic in character, then complete
  subsidence $s$ at a point on the surface is given by an integral of the form
$$ s = \int_0^R  p(r)~dr$$
where $R$ is the effective radius of influence, and $p(r)$ is an
empirically derived ``influence function''.

Consider the influence function
$\ds p(r) = \frac{1}{R^2} e^{-\pi r^2/R^2}$
and use an appropriate technique to evaluate the integral
$$ s = \int_0^R  p(r)~dr$$ with $R = 100$.

\end{Question}

\begin{Solution}
The resulting integral is
    $\ds \int_0^{100} \frac{1}{100^2} e^{-\pi r^2/(100^2)}~dr$.

    Since $r$ is the variable, we have an integral of the form
    essentialy $\int e^{-x^2}~dx$, which {\bf cannot} be evaluated by
    hand.  Thus we will use a numerical method in MATLAB.

    Of the two methods we have in MATLAB, LEFT($n$) and the
    \verb@integral@ function, the \verb@integral@ function is both
    easier to use and more accurate.

    The actual script required to just evaluate this integral is
    remarkably short.

\lstinputlisting[showstringspaces=false]{MATLAB/W06Subsidence.m}

and the output gives an integral value of \verb@I = 0.0049@.
\end{Solution}

%*******************************
\item
\begin{Question}
  The $x$ coordinate of the center of mass of a object can be computed using the formula
$$\bar{x} = \frac{\int_a^b x \cdot f(x)~dx}{\int_a^b f(x)~dx}$$
where $f(x)$ is the height (or mass) at the point $x$.

For the following functions, use MATLAB, with the \verb@integral@ function and other tools, to
\begin{itemize}
\item plot the graph of $f(x)$,
\item compute the $x$ center of mass of the object, and
\item draw the center of mass as a vertical line on the same graph.
\end{itemize}
\begin{enumerate}
\item  $f(x) = x^3$ on $x=[1, 4]$.
\item  $f(x) = x e^{-x}$ on $x=[0, 10]$.
\end{enumerate}

\end{Question}

\begin{Solution}
  In this problem, the challenge in coding the MATLAB script is to
  remember that when using the \verb@integral@ function, you need to
  specify separate (MATLAB) function for each integral. In the
  listings below, we did this using the syntax
\begin{verbatim}
f = @(x) x.^3;
% Note: we also need a _new_ function for the integral in the numerator
fn = @(x) x .* f(x);
xcenter = integral(fn, 1, 4) / integral(f, 1, 4)
\end{verbatim}

Full solutions: \\
\href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/W06CenterOfMass.m}{W06CenterOfMass.m}

\begin{enumerate}[(a)]
\item The $x$ center of mass for $f(x) = x^3$ on [1, 4] is
  $\bar{x} = 3.2094$, or closer to the right end of the interval,
  because the function has higher values there.
\item The $x$ center of mass for $f(x) = x e^{-x}$ on [0, 10] is
  $\bar{x} = 1.9955$, or closer to the left end of the interval,
  because the function has a peak there before declining to lower
  values as $x$ approaches 10.
\end{enumerate}
\begin{center}
\includegraphics[width=0.4\linewidth]{graphics/Week06_IntegrationApplications/W06CenterOfMass1}
\includegraphics[width=0.4\linewidth]{graphics/Week06_IntegrationApplications/W06CenterOfMass2}
\end{center}

\end{Solution}

% ******************************
\item
  \begin{Question}
    The width (in feet) of the golf course fairway was measured at
    100-foot intervals as indicated on the figure.  Estimate the
    square footage of of the fairway, using any appropriate means
    taught in the course.

    \begin{center}
      \includegraphics[width=0.5\linewidth]{graphics/Week06_IntegrationApplications/GolfHole}
    \end{center}
  \end{Question}

\begin{Solution}
  {\bf NOTE: } In this problem, because we are only given data and not
  a function, the only technique we have that would work is LEFT($n$).
  We {\bf can't} use the Fundamental Theorem or the \verb@integral@
  function because both of those require a formula for the function
  that we don't have.

The key steps are:
\begin{enumerate}[1.]
\item Create a vector of the widths, {\em including} the zero widths
  at the start and end of the fairway.
\item Note that  $\Delta x$ = 100 feet for each left-to-right distance.
\item Note that there are 10 intervals (11 widths), so we will be performing a LEFT(10) calculation.
\item Approximate the area on each left-to-right interval as
  $\mbox{width at the left} \times \Delta x$ (this is the LEFT rule on
  each interval).
\item Use a loop to accumulate the total area estimated.
\end{enumerate}

Link to the MATLAB code: \\
\href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/W06Golf.m}{W06Golf.m}

The final estimated area is 88,000 square feet.  We don't have a
formula for the widths, so this is simply our best estimate of the
area.

\end{Solution}

\hrulefill

\subsection*{Average Value}

\begin{multicols}{2}
%*****************
\item
  \begin{Question}
    \begin{enumerate}[(a)]
    \item Using the graph shown below, find $\ds \int^6_1 f(x)~dx$.

\includegraphics[width=0.9\linewidth]{graphics/Week06_AverageValue/average_1}
    \item What is the average value of $f$ on [1, 6]?
    \end{enumerate}
  \end{Question}

  \begin{Solution}
    The integral represents the area below the graph of $f(x)$ but above the $x$-axis.
    \begin{enumerate}[(a)]
    \item Since each square has area 1, by counting squares and half-squares we find
      \begin{align*}
\int^6_1 f(x) ~dx = 8.5.
      \end{align*}
(b) The average value is  \begin{align*}
\frac{1}{6 - 1} \int^6_1 f(x) ~dx
& = \frac{8.5}{ 5 } = 1.7
\end{align*}
    \end{enumerate}
  \end{Solution}

%*****************
\item
  \begin{Question}
    \begin{enumerate}[(a)]
    \item Using the graph below, estimate $\ds \int^3_{-3} f(x) ~dx$.
    \item  Which of the following average values of $f(x)$ is larger?  \\
      (i) Between $x = -3$ and $x = 3$, or   \\
      (ii) Between $x = 0$ and $x = 3$?
    \end{enumerate}
\includegraphics[width=0.7\linewidth]{graphics/Week06_AverageValue/average_2}
  \end{Question}

  \begin{Solution}
    \begin{enumerate}[(a)]
    \item The integral is the area above the $x$-axis minus the area
      below the $x$-axis. Thus, we can see that
      $\ds \int^3_{-3}f(x)~dx$ is about $-6 + 2 = -4$, based on \\
      the negative of the area from $t = -3$ to $t = 1$ plus the area
      from $t = 1$ to $t = 3$.  \\Note that your estimate may differ
      from this, depending on how you counted squares under the curved
      shape.
    \item Since the integral in part (a) is negative, the average
      value of $f(x)$ between $x = -3$ and $x = 3$ is negative. From
      the graph, however, it appears that the integral of $f(x)$ from
      $x = 0$ to $x = 3$ is positive overall, meaning that the average
      value will also be positive. Hence (ii) is the larger quantity.
    \end{enumerate}
  \end{Solution}
%*****************
\item
  \begin{Question}
    \begin{enumerate}[(a)]
    \item Using the graphs of $f(x)$ and $g(x)$ shown below, find the
      average value on $0 \le x \le 2$ of \\
      (i) $f(x)$ \hfill (ii) $g(x)$ \hfill (iii) $f( x) \cdot g(x)$ \\ \noindent
\includegraphics[width=0.5\linewidth]{graphics/Week06_AverageValue/average_product_graphs1}
\includegraphics[width=0.5\linewidth]{graphics/Week06_AverageValue/average_product_graphs2}

    \item Is the statement that \begin{center}``Average($f$) $\cdot$ Average($g$) = Average($f \cdot g$)'' \end{center}true or not?  Explain your answer.
    \end{enumerate}
  \end{Question}

  \begin{Solution}
 (i) Since the triangular region under the graph of $f(x)$ has area $\ds \frac{1}{2}$, we have
 \begin{align*}
\mbox{Average}(f) &= \frac{1}{ 2 - 0} \int^2_0 f(x) ~dx
& = \frac{1}{2} \cdot \frac{1}{2}
& = \frac{1}{4}
 \end{align*}
(ii) Similarly,
 \begin{align*}
\mbox{Average}(g) &= \frac{1}{2 - 0} \int^2_0 g(x) ~dx
& = \frac{1}{2} \cdot \frac{1}{2}
& = \frac{1}{4}
 \end{align*}

 (iii) Since $f(x)$ is nonzero only for $0 \le x < 1$ and $g(x)$ is
 nonzero only for $1 < x \le 2$, the product $f(x)g(x)$ = 0 for all
 $x$. (To confirm this, try picking any $x$ value between 0 and 2, and
 subbing it into the expression $f(x) \cdot g(x)$.

 Thus
\begin{align*}
\mbox{Average}(f \cdot g) & = \frac{1}{2 - 0} \int^2_0 f(x)g(x) ~dx
& = \frac{1}{2} \int^2_0 0 ~dx = 0.
\end{align*}
(b) Since the average values of $f(x)$ and $g(x)$ are nonzero, their
product is nonzero. Thus the left side of the statement, Avg($f$)$\cdot$Avg($g$), is
nonzero. However, the average of the product $f(x)g(x)$ is zero. Thus,
the right side of the statement is zero, so the statement is not true.
  \end{Solution}
%*****************
\item
  \begin{Question}
    \begin{enumerate}[(a)]
    \item Without computing any integrals, explain why the average
      value of $f(x) = \sin x$ on $[0, \pi]$ must be between 0.5 and 1.
    \item Compute the exact average of $\sin x$ on $[0, \pi]$.
    \item Use MATLAB to plot the graph of $\sin(x)$ on $[0, \pi]$, and
      draw the average value on the graph as well.
    \end{enumerate}
  \end{Question}

  \begin{Solution}
    \begin{enumerate}[(a)]
    \item Since $f(x) = \sin x$ over $[0, \pi]$ is between 0 and 1,
      the average of $f(x)$ must itself be between 0 and
      1. Furthermore, since the graph of $f(x)$ is concave down on
      this interval, the average value must be greater than the
      average height of the triangle shown in the figure below, namely
      $y = 0.5$.

\includegraphics[width=0.7\linewidth]{graphics/Week06_AverageValue/sin_average_solution}

\item Average =$\ds \frac{1 }{\pi-0}\int^\pi_0 \sin x ~dx \approx 0.64$.

\item \lstinputlisting[showstringspaces=false]{MATLAB/W06SineAverage.m}

\includegraphics[width=0.9\linewidth]{graphics/Week06_AverageValue/W06SineAverage}
    \end{enumerate}
  \end{Solution}
%*****************
\item
  \begin{Question}
    \begin{enumerate}[(a)]
    \item What is the average value of $f(x) = \sqrt{ 1 - x^2}$ over
      the interval $0 \le x \le 1$?  (Hint: we don't have the
      integration tools to evaluate the integral of $f(x)$ from first
      principles, but can use MATLAB to evaluate it.)
    \item Use MATLAB to plot the graph of $f(x)$ on $[0, 1]$, and draw
      the average value on the graph as well.
    \item How can you tell whether this average value is more or less
      than 0.5 without doing any calculations?
    \end{enumerate}
  \end{Question}

  \begin{Solution}
    \begin{enumerate}[(a)]
    \item Solution is computed in the script below, with the average value being 0.7854.

\item Graphing is also included in the script below.

\href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/W06RootAverage.m}{W06RootAverage.m}

\includegraphics[width=0.7\linewidth]{graphics/Week06_AverageValue/W06RootAverage}

\item The area between the graph of $y = 1-x$ and the $x$-axis is
  0.5. Because the graph of $y = \sqrt{1 - x^2}$ is concave down, it
  lies above the straight line $y = 1 - x$, so its average value is
  above 0.5. See the figure below.

\includegraphics[width=0.7\linewidth]{graphics/Week06_AverageValue/circle_average_solution}
    \end{enumerate}
  \end{Solution}
\end{multicols}

\end{enumerate}
\end{document}


\documentclass[11pt,a4paper]{report}
\usepackage{amsmath,amsfonts,amssymb,amsthm,epsfig,epstopdf,titling,url,array}
\usepackage{enumitem}
\usepackage{changepage}
\usepackage{graphicx}
\usepackage{caption}
\theoremstyle{plain}
\newtheorem{thm}{Theorem}[section]
\newtheorem{lem}[thm]{Lemma}
\newtheorem{prop}[thm]{Proposition}
\newtheorem*{cor}{Corollary}
\theoremstyle{definition}
\newtheorem{defn}{Definition}[section]
\newtheorem{conj}{Conjecture}[section]
\newtheorem{exmp}{Example}[section]
\newtheorem{exercise}{Exercise}[section]
\theoremstyle{remark}
\newtheorem*{rem}{Remark}
\newtheorem*{note}{Note}
\def\changemargin#1#2{\list{}{\rightmargin#2\leftmargin#1}\item[]}
\let\endchangemargin=\endlist
\begin{document}

\section*{Problem} How many proper factors does one billion have?  A
\emph{proper factor} of a positive integer $n$ is a positive integer $m$ such
that $n = km$ where $k$ is a positive integer greater than $1$ and less than
$n$.

\section*{Solution} $10^9 = 2^9 \mathord{\cdot} 5^9.$  Every proper factor of
$10^9$ must have the form $2^m \mathord{\cdot} 5^n$ where $m$ and $n$ are
between $0$ and $9$.  The two combinations $m = n = 0$ and $m = n = 9$ must be
excluded, but all others are valid and distinct factors.  They are all distinct
because $2$ and $5$ are primes (so two different products of them to different
exponents cannot be the same, by uniqueness of prime factorization).  Therefore,
there are exactly $10 \mathord{\cdot} 10 - 2 = 98$ proper factors of one
billion.

\end{document}


\section*{Hashing - Peter Christensen}

\begin{enumerate}
	\item Define a hash function
	\item Define $c$-universal hashing
	\item use case: Assigning unique signatures and give an example such as Exercise 2.5
	\item Define multiply--mod--prime hash function and talk about how it works.
	\item Define Strongly universal and talk about coordinated--sampling.
\end{enumerate}