\documentclass[12pt,letterpaper]{article}

\usepackage{assignments}
% \usepackage{minted}
\usepackage{graphicx}
\usepackage{bm}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{subcaption}

\begin{document}

\title{\vspace{-4ex}ECE521: Inference Algorithms and Machine Learning \\
University of Toronto\\ \  \\
Solution to Assignment 3: \\Unsupervised Learning and Probabilistic Models}
\author{Renjie Liao}
% \date{\vspace{-8ex}TA: Use Piazza for Q\&A \\ Due date: Mar. 24 11:59 pm, 2017 \\ Electronic submission to: \href{mailto:ece521ta@gmail.com}{ece521ta@gmail.com} }

\maketitle

\begin{mycomments}
\section{Part 1}
\end{mycomments}
\section{K-means}

\subsection{Learning K-means [8 pt.]}

1, [3 pt.] The loss function $\mathcal{L}(\bm{\mu})$ is non-convex in $\bm{\mu}$. We use contradiction to prove the statement. Assuming $\mathcal{L}(\bm{\mu})$ is convex in $\bm{\mu}$.
We consider a special case where $B = 1$, $K = 2$, $D = 2$, $\mathbf{x} = \left[x_1, x_2\right]$, $\mathbf{\bm{\mu}} = \left[\bm{\mu}_1, \bm{\mu}_2\right]$ and $\mathbf{x} \neq \mathbf{0}$.
We can rewrite the loss function as $\mathcal{L}(\bm{\mu}) = \min(\Vert \mathbf{x} - \bm{\mu}_{1} \Vert_{2}^{2}, \Vert \mathbf{x} - \bm{\mu}_{2} \Vert_{2}^{2})$.
Now we will show that for two specific constructions $\mathbf{\bm{\mu}} = \left[\bm{\mu}_1, \bm{\mu}_2\right]$ and $\mathbf{\bm{\mu}^{\prime}} = \left[\bm{\mu}_{1}^{\prime}, \bm{\mu}_{2}^{\prime}\right]$ and any $\theta \in (0, 1)$, we have $\mathcal{L}(\theta \bm{\mu} + (1 - \theta) \bm{\mu}^{\prime}) > \theta \mathcal{L}(\bm{\mu}) + (1 - \theta) \mathcal{L}(\bm{\mu}^{\prime})$.
In particular, let $\bm{\mu}_{1} = \left[ x_{1}, x_{2} \right]$, $\bm{\mu}_{2} = \left[ 0, 0 \right]$, $\bm{\mu}_{1}^{\prime} = \left[ 0, 0 \right]$ and $\bm{\mu}_{2}^{\prime} = \left[ x_{1}, x_{2} \right]$.
We have that $\mathcal{L}(\bm{\mu}) = 0$ and $\mathcal{L}(\bm{\mu}^{\prime}) = 0$.
Moreover, we have,
\begin{align}
\mathcal{L}(\theta \bm{\mu} + (1 - \theta) \bm{\mu}^{\prime}) & = \min(\Vert \mathbf{x} - \theta \bm{\mu}_{1} - (1 - \theta) \bm{\mu}_{1}^{\prime} \Vert_{2}^{2}, \Vert \mathbf{x} - \theta \bm{\mu}_{2} - (1 - \theta) \bm{\mu}_{2}^{\prime} \Vert_{2}^{2}) \nonumber \\
& = \min( (1 - \theta)^{2} \Vert \mathbf{x} \Vert^{2}, \theta^{2} \Vert \mathbf{x} \Vert^{2} ).
\label{kmeans_ineq}
\end{align}
It is thus clear that $\mathcal{L}(\theta \bm{\mu} + (1 - \theta) \bm{\mu}^{\prime}) > \theta \mathcal{L}(\bm{\mu}) + (1 - \theta) \mathcal{L}(\bm{\mu}^{\prime}) = 0$ for any $\theta \in (0, 1)$. Hence, we have the contradiction.

\begin{figure}
    \centering
    \begin{subfigure}[b]{0.45\textwidth}
        \includegraphics[width=\textwidth]{imgs/kmeans_train_loss.png}
        \caption{Kmeans, training loss}
        \label{kmeans_loss}
    \end{subfigure}
    \begin{subfigure}[b]{0.45\textwidth}
        \includegraphics[width=\textwidth]{imgs/kmeans_K_1.png}
        \caption{Kmeans, K = 1}
        \label{kmeans_1}
    \end{subfigure}

    \begin{subfigure}[b]{0.45\textwidth}
        \includegraphics[width=\textwidth]{imgs/kmeans_K_2.png}
        \caption{Kmeans, K = 2}
        \label{kmeans_2}
    \end{subfigure}
    \begin{subfigure}[b]{0.45\textwidth}
        \includegraphics[width=\textwidth]{imgs/kmeans_K_3.png}
        \caption{Kmeans, K = 3}
        \label{kmeans_3}
    \end{subfigure}

    %add desired spacing between images, e. g. ~, \quad, \qquad, \hfill etc.
    %(or a blank line to force the subfigure onto a new line)
    \begin{subfigure}[b]{0.45\textwidth}
        \includegraphics[width=\textwidth]{imgs/kmeans_K_4.png}
        \caption{Kmeans, K = 4}
        \label{kmeans_4}
    \end{subfigure}
    \begin{subfigure}[b]{0.45\textwidth}
        \includegraphics[width=\textwidth]{imgs/kmeans_K_5.png}
        \caption{Kmeans, K = 5}
        \label{kmeans_5}
    \end{subfigure}
\end{figure}

2, [2 pt.] Referring to Fig. \ref{kmeans_loss}.

3, [3 pt.] Referring to Fig. \ref{kmeans_1} - \ref{kmeans_5}. K = 5 might be the best as it gives the lowest loss function value.

4, [2 pt.] The validation loss values are $12856.2089844$, $2982.43554688$, $1659.61975098$, $1095.4954834$, $921.618164062$ when $K = 1, 2, 3, 4, 5$ respectively. Therefore, $K = 5$ is the best.

\section{Mixtures of Gaussians}

\subsection{The Gaussian cluster model}

1, [3 pt.]
\begin{align}
P(z | \mathbf{x}) = \frac{ \pi^{k} \left( \sigma^{k} \right)^{-D} \exp \left\{ -\frac{1}{2 \left( \sigma^{k} \right)^{2}} (\mathbf{x} - \bm{\mu}^{k})^{\top} (\mathbf{x} - \bm{\mu}^{k}) \right\} }{ \sum_{j=1}^{K} \pi^{j} \left( \sigma^{j} \right)^{-D} \exp \left\{ -\frac{1}{2 \left( \sigma^{j} \right)^{2}} (\mathbf{x} - \bm{\mu}^{j})^{\top} (\mathbf{x} - \bm{\mu}^{j}) \right\} }
\label{GMM_posterior}
\end{align}

2, [2 pt.] Referring to Fig. \ref{dist_func}.
\begin{figure}
\centering
    \begin{subfigure}[b]{0.45\textwidth}
        \includegraphics[width=\textwidth]{imgs/GMM_2_1_2.png}
        \caption{Distance function.}
        \label{dist_func}
    \end{subfigure}
    \begin{subfigure}[b]{0.45\textwidth}
        \includegraphics[width=\textwidth]{imgs/GMM_2_1_3.png}
        \caption{Log probability.}
        \label{log_prob}
    \end{subfigure}
\end{figure}

3, [3 pt.] Referring to Fig. \ref{log_prob}. Using log-sum-exp is numerically more accurate and stable and will help avoid issues like underflow or overflow.

\subsection{Learning the MoG}

1, [2 pt.]
\begin{align}
\nabla_{\bm{\mu}} \log P(\mathbf{x}) & = \nabla_{\bm{\mu}} \log \left( \sum_{k=1}^{K} P(\mathbf{x}, z=k) \right) \nonumber \\
& = \frac{1}{\sum_{k=1}^{K} P(\mathbf{x}, z=k)} \sum_{k=1}^{K} \nabla_{\bm{\mu}} P(\mathbf{x}, z=k) \nonumber \\
& = \frac{1}{\sum_{k=1}^{K} P(\mathbf{x}, z=k)} \sum_{k=1}^{K} P(\mathbf{x}, z=k) \nabla_{\bm{\mu}} \log P(\mathbf{x}, z=k) \nonumber \\
& = \sum_{k=1}^{K} P(z=k | \mathbf{x}) \nabla_{\bm{\mu}} \log P(\mathbf{x}, z=k),
\label{GMM_posterior}
\end{align}
where we use the fact $f(x) \nabla_x \log (f(x)) = \nabla_x f(x)$.

2, [6 pt.] Referring to Fig. \ref{GMM_loss}. The best model parameters are: $\mu_1 = [-1.09925008 -3.30307055]$, $\mu_2 = [1.29940867  0.30630079]$, $\mu_1 = [0.09809401 -1.5353744]$, $\sigma_1 = 0.19823763$, $\sigma_2 = 0.19667165$, $\sigma_3 = 0.99854809$, $\pi_1 = 0.32968238$, $\pi_2 = 0.33281365$ and $\pi_3 = 0.33750394$.

\begin{figure}
    \centering
    \begin{subfigure}[b]{0.45\textwidth}
        \includegraphics[width=\textwidth]{imgs/gmm_train_loss.png}
        \caption{GMM training loss}
        \label{GMM_loss}
    \end{subfigure}
    \begin{subfigure}[b]{0.45\textwidth}
        \includegraphics[width=\textwidth]{imgs/GMM_K_1.png}
        \caption{GMM, K = 1}
        \label{GMM_1}
    \end{subfigure}

    \begin{subfigure}[b]{0.45\textwidth}
        \includegraphics[width=\textwidth]{imgs/GMM_K_2.png}
        \caption{GMM, K = 2}
        \label{GMM_2}
    \end{subfigure}
    \begin{subfigure}[b]{0.45\textwidth}
        \includegraphics[width=\textwidth]{imgs/GMM_K_3.png}
        \caption{GMM, K = 3}
        \label{GMM_3}
    \end{subfigure}

    %add desired spacing between images, e. g. ~, \quad, \qquad, \hfill etc.
    %(or a blank line to force the subfigure onto a new line)
    \begin{subfigure}[b]{0.45\textwidth}
        \includegraphics[width=\textwidth]{imgs/GMM_K_4.png}
        \caption{GMM, K = 4}
        \label{GMM_4}
    \end{subfigure}
    \begin{subfigure}[b]{0.45\textwidth}
        \includegraphics[width=\textwidth]{imgs/GMM_K_5.png}
        \caption{GMM, K = 5}
        \label{GMM_5}
    \end{subfigure}
\end{figure}

3, [2 pt.] Referring to Fig. \ref{GMM_1} - \ref{GMM_5}. $K = 3$ is the best as the validation loss is the minimum.

4, [2 pt.] The loss function values of Kmeans and MoG with different K are listed in the table \ref{loss_table}. From the current trials, it seems that the number of clusters should be around 20. We visualize the cluster assignments and cluster centers in terms of the first 2 dimensions of the 100D dataset in Fig. \ref{kmeans_100D} and Fig. \ref{GMM_100D}. It is hard to draw any conclusion from the learned results. It seems that some clusters of GMM are collapsed while the ones of Kmeans are not.

\begin{table}
\centering
\begin{tabular}{ l | c c }
& GMM & Kmeans \\ \hline
k=3 & 651434.875 & 1418950.625 \\ \hline
k=5 & 352855.46875 & 1091327.0 \\ \hline
k=10 & 211011.671875 & 1304746.25 \\ \hline
k=15 & 209351.46875 & 486612.15625 \\ \hline
k=20 & 207494.59375 & 484491.625 \\ \hline
k=30 & 204230.015625 & 485255.78125
\end{tabular}
\caption{Loss values on 100D dataset.}
\label{loss_table}
\end{table}

\begin{figure}
    \centering
    \begin{subfigure}[b]{0.45\textwidth}
        \includegraphics[width=\textwidth]{imgs/kmeans_100D.png}
        \caption{K = 5, 100D, Kmeans}
        \label{kmeans_100D}
    \end{subfigure}
    \begin{subfigure}[b]{0.45\textwidth}
        \includegraphics[width=\textwidth]{imgs/GMM_100D.png}
        \caption{K = 5, 100D, GMM}
        \label{GMM_100D}
    \end{subfigure}
\end{figure}

\section{Discover Latent Dimensions}

\subsection{Factor Analysis}

1, [2 pt.]
First, we have,
\begin{align}
P(\mathbf{x}) & = \int_{\mathbf{z}}  P(\mathbf{x} | \mathbf{z}) P(\mathbf{z}) d\mathbf{z}
\end{align}
From the multivariate results appended in the assignment, we have,
\begin{align}
P(\mathbf{z}) & = \mathcal{N}(\mathbf{z} | 0, I) \nonumber \\
P(\mathbf{x} | \mathbf{z}) & = \mathcal{N}(z | W \mathbf{z} + \bm{\mu}, \Psi) \nonumber \\
P(\mathbf{x}) & = \mathcal{N}(\mathbf{x} | \bm{\mu}, \Psi + W W^{\top} )
\end{align}

2, [3 pt.] The training, validation and testing marginal likelihoods are $8399.542$, $1238.54980469$ and $4644.74414062$ respectively. The visualization of weights are in Fig. \ref{FA_weight_1} - \ref{FA_weight_4}. You can see that the weight capture the some parts of digits. For example, Fig. \ref{FA_weight_1} and \ref{FA_weight_3} captures the shape of ``5" and Fig. \ref{FA_weight_2} and \ref{FA_weight_4} captures ``3".

\begin{figure}
    \centering
    \begin{subfigure}[b]{0.24\textwidth}
        \includegraphics[width=\textwidth]{imgs/FA_weight_1.png}
        \caption{Weight 1}
        \label{FA_weight_1}
    \end{subfigure}
    \begin{subfigure}[b]{0.24\textwidth}
        \includegraphics[width=\textwidth]{imgs/FA_weight_2.png}
        \caption{Weight 2}
        \label{FA_weight_2}
    \end{subfigure}
    \begin{subfigure}[b]{0.24\textwidth}
        \includegraphics[width=\textwidth]{imgs/FA_weight_3.png}
        \caption{Weight 3}
        \label{FA_weight_3}
    \end{subfigure}
    \begin{subfigure}[b]{0.24\textwidth}
        \includegraphics[width=\textwidth]{imgs/FA_weight_4.png}
        \caption{Weight 4}
        \label{FA_weight_4}
    \end{subfigure}
\end{figure}

3, [3 pt.]

The first component of PCA is $[-2.72359396e^{-04}, -2.61620560e^{-04}, 9.99999929e^{-01}]$ which approximately corresponds to the maximum variance direction, i.e., $x_3$.

The conditional distribution of $\mathbf{s}$ (posterior) of a new data point $\mathbf{x}^*$ in a FA is:
 $p(\mathbf{s} | \mathbf{x}^*) = \mathcal{N}\big(\mathbb{s}; \Sigma W^T\Psi^{-1}(\mathbf{x}^* - \boldsymbol{\mu}), \Sigma\big)$ (Eq(4) of Multivariate Gaussian Results) where $\Sigma = (I + W^T\Psi^{-1}W)^{-1}$. Now let us definite $W_{proj} \triangleq \Sigma W^T\Psi^{-1} = (I + W^T\Psi^{-1}W)^{-1}W^T\Psi^{-1}$. Therefore, $W_{proj}$ is the ``principle component'' matrix that transforms the observation data to the latent space of FA instead of the $W$ matrix.

The first component of $W_{proj}$ is $[3.48705414e-01,   5.76612226e-01,  4.26386575e-08]$ which is the maximum correlation direction, i.e. $x_1+x_2$

\end{document}


\chapter{Review of Prior Work}
\label{chap:chapter2}

Existing algorithms for Herbrand equivalence analysis are either
exponential or imprecise. The precise algorithms are based on an
early algorithm by Kildall \cite{Kildall}, which discovers
equivalences by performing an abstract interpretation over the
lattice of Herbrand equivalences. Kildall's algorithm is precise in
the sense it finds all the Herbrand equivalences but is exponential
in time. The partition refinement algorithm of Alpern, Wegman and
Zadek (AWZ) \cite{AWZ} is efficient but much imprecise compared to
Kildall's. AWZ algorithm represent the values of variables after a
join using a fresh selection function $\phi_i$, similar to functions
in the static single assignment form and treats $\phi_i$ as
uninterpreted functions. It is incomplete in the sense it treats all
$\phi_i$ as uninterpreted. In an attempt to remedy this problem,
Ruthing, Knoop and Steffen proposed a polynomial-time algorithm (RKS)
\cite{RKS} that alternately applies the AWZ algorithm and some
rewrite rules for normalization of terms involving $\phi$ functions,
until the congruence classes reach a fixed point. Their algorithm
discovers more equivalences than the AWZ algorithm, but remains
incomplete.

\section{Algorithm by Gulwani and Necula}
\label{sec:AlgorithmByGulwaniAndNecula}
Gulwani and Necula \cite{Gulwani} that there is a family of acyclic
programs for which the set of all Herbrand equivalences requires an
exponential sized (with respect to the size of the program) \textbf{value
graph} representation - the data structure used by Kildall in his
algorithm. This explains the reason for exponential complexity of
Kildall's algorithm which cannot be improved to polynomial and imprecise
nature of existing polynomial time algorithms.

They showed that Herbrand equivalences among program sub expressions
can always be represented using linear sized value graph.
So contrasting to Kildall's algorithm, which finds \textbf{all the
Herbrand equivalent classes} corresponding to constants, variables
and operators occurring in the program, their algorithm discovers
\textbf{equivalences among program subexpressions} (expressions that
can occur syntactically in a program), in linear time with respect to
parameter $s$, the maximum size of an expression in terms of number
of operators used. For global value numbering, $s$ can be safely
taken to be $N$, the size of the program and hence the algorithm is
linear in the program size.

They also proved that the lattice of sets of Herbrand equivalences
has finite height $k$, which is the number of program variables. So,
an abstract interpretation over the lattice of Herbrand equivalences
will terminate in at most $k$ iterations even for cyclic programs.

\subsection{Overview of the Algorithm}
\label{subsec:OverviewOfTheAlgorithmGulwani}
The program expressions can be represented as
$$e\ ::=\ x\ \mid\ c\ \mid\ F(e_1, e_2)$$
where $c$ and $x$ are constants and variables occurring in the program
respectively and $F$ stands for an operator. Any expression of length
greater than two (in terms of number of operands) can be converted into
two length expression by introduction of extra variables.

The data structure used is called \textbf{Strong Equivalence DAG
(SED)}. Each node of SED is of the form $<V,t>$ where $V$ is a set of
program variables and $t$ is either $\perp$ or $c$ for leaf nodes and
$F(n_1, n_2)$ where $n_1$ and $n_2$ are SED nodes for non leaf nodes
(also indicating that the node has two ordered successors). $\perp$
means that the variables in the node have undefined values.

There is a SED associated with each program point and the algorithm
starts with the following initial SED
$$G_0\ =\ \{<x,\perp>\ \mid\ x \text{ is a program variable}\}$$
Two functions \texttt{Join($G_1$, $G_2$, $s'$)} and \texttt{Assignment($G_1$, $x := e$)}
are used to compute SEDs for other points in the flow graph node
corresponding to the program, as shown in \autoref{fig:GulwaniAlgorithm}.
$s'$ in the argument of \texttt{Join} is a positive integer, and it
returns equivalences between expressions of size atmost $s'$.

\begin{figure}[H]
    \centering {
        \setlength{\fboxsep}{8pt}
        \fbox{\includegraphics[scale=0.75]{GulwaniAlgorithm.png}}
    }
    \caption{Computation of SED for flowgraph nodes of a program}
    \label{fig:GulwaniAlgorithm}
\end{figure}

For detailed implementation of \texttt{Join} and \texttt{Assignment}
functions, as well as a correctness proof of the algorithm, see
\cite{Gulwani}.

\subsection{Complexity of the Algorithm}
\label{subsec:ComplexityOfTheAlgorithm}
The complexity of the algorithm is $\mathcal{O}(k^{3} * N * j)$, where
$k$ is the total number of program variables, $N$ is the size of the
program and $j$ is the number of join operations in the program. $k$ and
$j$ are bounded by $N$, making the whole algorithm polynomial in $N$.

\section{Algorithm by Saleena and Paleri}
\label{sec:AlgorithmBySaleenAndPaleri}
Saleena and Paleri \cite{Saleena} gave an algorithm for \textbf{global
value numbering (GVN)}. GVN works by assigning a \textbf{value number}
to variables and expressions. The same value number is assigned to those
variables and expressions which are provably equivalent. A notable
difference between Herbrand equivalence and GVN is that in Herbrand
equivalence one talks about equivalences at a particular program point
but GVN is concerned with equivalence between expressions at two
different program points.

The data structure used in the algorithm is called \textbf{value
expression} - an expression with value numbers as operands. Two
expressions are equivalent if they have same value expression.
So, a value expression can be used to represent a set of equivalent
program expressions.

\subsection{Notations}
\label{subsec:NotationsSaleena}
Input to the algorithm is a \textbf{flow graph} with atmost one assignment
statement in each node which has one of the following forms
\begin{itemize} \tightlist
    \item $x\; ::=\; e$
    \item $e\ ::=\ x\ \mid\ c\ \mid\ x_1\ op\ x_2$
\end{itemize}
The flow graph also has two additional empty \texttt{ENTRY} and \texttt{EXIT}
nodes. For a node $n$, \texttt{IN$_n$} and \texttt{OUT$_n$} denotes the input
and output program points of the node.

\textbf{Expression pool} at a program point, is a partition of expressions
at that point, in which equivalent expression belongs to the same partition.
Each class will have a \textbf{value number} which will be considered as its
first element. For a node $n$, \texttt{EIN$_n$} and \texttt{EOUT$_n$} denotes
the expression pools at input and output program points of the node.

\subsection{Value Expression}
\label{subsec:ValueExpression}
The \textbf{value expression} corresponding to an expression is obtained
by replacing the actual operands with their corresponding value numbers.
Example - For the expression pool $\{\{v_1, a, x\}$, $\{v_2, b, y \}\}$
and statement $z ::= x + y$ , the value expression for $x + y$ will be
$v_1 + v_2$. Instead of $x + y$, its value expression is included in
the expression pool, with a new value number. So the new expression pool
would be $\{\{v_1, a, x\},\{v_2, b, y\}, \{v_3, v_1 + v_2, z\}\}$.

The value expression $v_1 + v_2$ represents not just $x + y$ but the
set of equivalent expressions $\{a + b, x + b, a + y, x + y\}$. Its
presence indicates that an expression from this set is already
computed and this information is enough for detection of redundant
computations. Also, a single binary value expression can represent
equivalence among any numbre of expressions of any length. Example -
$v_1 + v_3$ represents, $a + z$, $x + z$, $a + (a + b)$,
$a + (x + b)$ and so on.

\subsection{Overview of the Algorithm}
\label{subsec:OverviewOfTheAlgorithmSaleena}
Similar to Gulwani's algorithm, the algorithm consists of two main
functions - a \textbf{transfer} function for changes in expression pool
across assignment statements and a \textbf{confluence function} to find
the expression pool at points were two branches meet. The algorithm
starts with \texttt{EOUT$_{\texttt{ENTRY}}$ = $\phi$}, and uses transfer
and confluence functions to calculate expression pools at other points.
This process is repeated till there is any change in the equivalence
information. For detailed algorithm refer to \cite{Saleena}.

\section{Algorithm by Babu, Krishnan and Paleri}
\label{sec:AlgorithmByBabuKrishnanAndPaleri}
One problem is that most of these alogrithms are based on fix point
computations but the classical definition of Herbrand equivalence is
not a fix point based definition making it difficult to prove their
precision or completeness. Babu, Krishnan and Paleri \cite{Babu}
developed a lattice theoretic fix-point formulation of Herbrand
equivalence on the lattice defined over the set of all terms constructible
from variables, constants and operators of a program and showed that
this definition is equivalent to the classical meet over all path
characterization over the set of all possible expressions. They also
proposed an algorithm which is able to detect all the equivalences as
by that of Saleena and Paleri. This is discussed in detail in
\autoref{chap:chapter3}.

\section{Conclusion}
\label{sec:Conclusion}
So to sum up, Kildall’s algorithm finds all the equivalent classes but
is exponential. The algorithms by Saleena and Paleri; Babu, Krishnan
and Paleri are polynomial and efficient among other imprecise algorithms.
They are able to find all equivalence classes restricted to program
expressions (all expressions with length atmost 2), which is precisely
what is practically useful.


\documentclass[main.tex]{subfiles}
\begin{document}

\marginpar{Friday\\ 2021-12-17}

We were dealing with the interaction of radiation with matter, and
we saw the Bethe-Bloch formula.

We have found a way to determine the radiation length
%
\begin{align}
x_0 = 180 \frac{A}{Z^2} \SI{}{g/cm^2}
\,,
\end{align}
%
as well as seeing that the maximum penetration length is \(x \propto \log (E_0 / E_i)\).

The final angle of the particle after interaction with the medium will be
distributed according to a Gaussian with:
%
\begin{align}
\sigma _\theta \approx \frac{\SI{21}{MeV}}{p \beta } \sqrt{ \frac{x}{x_0 }} \left(1 + \num{2e-3} \log (\dots)\right)
\,,
\end{align}
%
where the logarithmic correction is small, of the scale of its prefactor.

The Molière radius is defined as
%
\begin{align}
R _{\text{Molière}} = \frac{\SI{21}{MeV}}{E_c} x_0
\,,
\end{align}
%
and it characterizes the transverse scale of the beam.

What happens if a proton is incoming?
The length before it interacts will typically be
%
\begin{align}
\Lambda \approx \frac{1}{n \sigma }
\,,
\end{align}
%
and we have already seen what the plot for \(\sigma_{pp}\) looks like.
The oscillations mostly happen at low energies, ad high energies there is an increase,
while in a wide region we have \(\sigma_{pp} \approx \SI{40}{mb}\).

The hadronic cross-section is quite universal: the length scale for
the interaction of a pion is similar to that for a proton.

The longitudinal shower development is always peaked at few interaction lengths, like \(5 \Lambda \),
always proportional to \(\log E\).

The distribution of the angle of the products after an hadronic interaction
will look like \(\theta \approx p_T / p_ \parallel\), with \(e^{- p_T / p_0}\).

Let us assume that \(p_0 \) is close to \SI{300}{MeV};
the scale of the transverse momentum can be estimated with \(\Delta x \Delta p \sim \hbar\),
where \(\Delta x\) is the length scale of the interaction cross-section.

Typically, in a material \(\Lambda > x_0 \) as long as \(Z > 10\) in the medium.
Therefore, the hadronic shower typically is deeper than the electromagnetic one.

Suppose we are in LNGS and we have cosmic rays in the atmosphere.
What is the minimum energy the muons must have to reach the detector?

Gran Sasso rock has a density of roughly \SI{2.3}{g/cm^3} and a
depth of roughly \SI{1500}{m}.
This is equivalent to about \SI{3}{km} of water.

So, we want to require that \(R_\mu (E) \geq \SI{3200}{mwe}\).
This is defined as
%
\begin{align}
R_\mu (E) = \int_{m_\mu c^2}^{E} \frac{ \dd{E}}{\abs{ \dv*{E}{x}}}
\,.
\end{align}

How does the energy loss scale as a function of energy?
We will have several contributions, so
%
\begin{align}
\dv{E}{x} = f_{\text{bethe-bloch}} (E) + f _{\text{bremsstrahlung}}(E)
\,,
\end{align}
%
but the bremsstrahlung for muons will be suppressed by \((m_\mu / m_e)^2\) compared to that of electrons: the threshold for electrons is roughly \SI{100}{MeV}, so that for muons is \SI{4}{TeV}.

So, unless we deal with very high-energy muons the contribution can be neglected; we will be able to write the radiative energy loss by bremsstrahlung as \(bE\) if needed (above a few hundred \SI{}{GeV}).

The Bethe-Bloch term is roughly constant for a very large energy range, so the integral is rather easy.
In general, if
%
\begin{align}
\abs{\dv{E}{x}} = a + bE
\,
\end{align}
%
then we will have
%
\begin{align}
R(E) = \int_{mc^2}^{E} \frac{ \dd{E}}{a + bE} \approx \int_{mc^2}^{E_c} \frac{\dd{E}}{a} + \int_{E_c}^{E} \frac{ \dd{E}}{a+ bE}
\,,
\end{align}
%
where the \(bE\) term is dominant in the high-energy range.
This means that when \(E < E_c\) we simply have \(R(E) = (E-mc^2) / a\), while for \(E > E_c\) we get
%
\begin{align}
R(E) \approx \frac{E_c - mc^2}{a} + \frac{1}{b} \log \frac{E}{E_c}
\approx  \left( 2.5 + 1.2 \log \frac{1 + 4 E / \SI{}{TeV}  }{3} \right)
\,.
\end{align}

The value of \(b\) when \(E\) is expressed in \SI{}{MeV} is approximately \(b \approx \SI{8e-6}{MeV g^{-1} cm^2}\).
This means that the minimum energy for muons is about \SI{1.4}{TeV}.
Not very many muons have such an energy.
Suppose we have a pion with energy \SI{10}{GeV}.

The  minimum energy loss rate is \(\SI{2}{MeV g^{-1} cm^2}\), this means that the range is
%
\begin{align}
R = \frac{E}{\Delta E / \Delta x} \approx \SI{5e3}{g / cm^2}
\,,
\end{align}
%
which translates to \SI{50}{m} in water, \SI{7}{m} in iron, \SI{50}{km} in air.

These are generally quite long; what about the hadronic interaction length \(\Lambda \)?
For the cross-section we can use \(\sigma \sim \SI{40}{mb}\), for the number density we have
%
\begin{align}
n = N_A \frac{\text{atoms}}{\text{mol}} \frac{A \text{nucleons / atom}}{A \text{grams}/ \text{mol}} \rho \approx N_A \frac{\rho}{\SI{}{g/cm^3}} \text{targets / } \SI{}{cm^3}
\,.
\end{align}

This comes out to
%
\begin{align}
\Lambda \approx \frac{\SI{0.4}{m}}{\rho / (\SI{}{g/cm^3})}
\,.
\end{align}

When looking up the proper numbers, we have that interaction dominates: in iron, it happens after only \SI{1.6}{cm}, in water after \SI{8.6}{cm}, in air after about \SI{700}{m}.

We also need to look at decay! The decay time is \SI{26}{ns} for a pion, meaning that the decay length is \(c \tau \gamma \beta \approx \gamma \SI{7.8}{m} \approx \SI{540}{m}\).

This scales linearly with the energy!

\section{Detectors}

One of the simplest detectors one can make is a Geiger counter:
a chamber with gas and a wire.

A particle comes through and generates ionization;
since there is an electric field the electrons and ions are separated from each other.

The velocity of the drift is typically plotted as a function of \(E / P\); it saturates to a certain plateau value, typically of the order of a few \SI{}{cm/\micro s} for electrons, while ions are a factor 1000 slower.

The variation in the voltage is related to a variation in the charge by \(\delta V = \delta q / C\).
The rise time of the voltage is roughly \(v _{\text{drift}} R\), where \(R\) is the length scale of the detector.

The HV of the detector is quite large compared to \(\delta V\); so we need a high-pass filter, such that \(RC \lesssim v _{\text{drift}} R\).

Typically one uses \(RC = (\SI{50}{\ohm}) (\dots)\).
Also, one puts a strong resistor before the detector, to reduce the current.

The value of \(\delta q\) will be \(N _{\text{pairs}} e\); the number of pairs can be computed as
%
\begin{align}
N _{\text{pairs}} = \frac{\Delta E}{W}
\,,
\end{align}
%
where \(W\) is the energy needed to release one pair, while \(\Delta E\) is the total energy lost in the detector.
Typically, in gas detectors one has \(W \approx \SI{10}{eV}\).

What is \(\Delta E\)? Since \(\dv*{E}{x} \approx \SI{2}{MeV g^{-1} cm^2}\) we have \(\Delta E = \SI{2}{keV}\) in a \SI{}{cm} of material.
This means that we release about 200 pairs, or \SI{3e-17}{C}.

What is the capacitance of a cylinder with a given length and a radius of \SI{1}{cm}?

\end{document}


\chapter{Formal syntax and semantics}
\label{formalchapter}

This chapter provides formal descriptions of what has already been
described informally in previous chapters of this report.

\todo{Allow grammar to say that else clause needn't be last?}

\section{Formal syntax}
\label{BNF}

This section provides a formal syntax for Scheme written in an extended
BNF.  The syntax for the entire language, including features which are
not essential, is given here.

All spaces in the grammar are for legibility.  Case is insignificant;
for example, {\cf \#x1A} and {\cf \#X1a} are equivalent.  \meta{empty}
stands for the empty string.

The following extensions to BNF are used to make the description more
concise:  \arbno{\meta{thing}} means zero or more occurrences of
\meta{thing}; and \atleastone{\meta{thing}} means at least one
\meta{thing}.

\subsection{Lexical structure}

This section describes how individual tokens\index{token} (identifiers,
numbers, etc.) are formed from sequences of characters.  The following
sections describe how expressions and programs are formed from sequences
of tokens.

\meta{Intertoken space} may occur on either side of any token, but not
within a token.

\vest Tokens which require implicit termination (identifiers, numbers,
characters, and dot) may be terminated by any \meta{delimiter}, but not
necessarily by anything else.

\begin{grammar}%
\meta{token} \: \meta{identifier} \| \meta{boolean} \| \meta{number}\index{identifier}
\>  \| \meta{character} \| \meta{string}
\>  \| ( \| ) \| \sharpsign( \| \singlequote{} \| \backquote{} \| , \| ,@ \| {\bf.}
\meta{delimiter} \: \meta{whitespace} \| ( \| ) \| " \| ;
\meta{whitespace} \: \meta{space or newline}
\meta{comment} \: ; \= $\langle$\rm all subsequent characters up to a
		    \>\ \rm line break$\rangle$\index{comment}
\meta{atmosphere} \: \meta{whitespace} \| \meta{comment}
\meta{intertoken space} \: \arbno{\meta{atmosphere}}%
\end{grammar}

\label{extendedalphas}
\label{identifiersyntax}

% This is a kludge, but \multicolumn doesn't work in tabbing environments.
\setbox0\hbox{\cf\meta{variable} \goesto{} $\langle$}

\begin{grammar}%
\meta{identifier} \: \meta{initial} \arbno{\meta{subsequent}}
 \>  \| \meta{peculiar identifier}
\meta{initial} \: \meta{letter} \| \meta{special initial}
\meta{letter} \: a \| b \| c \| ... \| z
\meta{special initial} \: ! \| \$ \| \% \| \verb"&" \| * \| / \| : \| < \| =
 \>  \| > \| ? \| \verb"~" \| \verb"_" \| \verb"^"
\meta{subsequent} \: \meta{initial} \| \meta{digit}
 \>  \| \meta{special subsequent}
\meta{digit} \: 0 \| 1 \| 2 \| 3 \| 4 \| 5 \| 6 \| 7 \| 8 \| 9
\meta{special subsequent} \: .\ \| + \| -
\meta{peculiar identifier} \: + \| -
%\| 1+ \| -1+
\meta{syntactic keyword} \: \meta{expression keyword}\index{keyword}\index{syntactic keyword}
 \>  \| else \| => \| define
 \>  \| unquote \| unquote-splicing
\meta{expression keyword} \: quote \| lambda \| if
 \>  \| set! \| begin \| cond \| and \| or \| case
 \>  \| let \| let* \| letrec \| do \| delay
 \>  \| quasiquote

\copy0\rm any \meta{identifier} which isn't\index{variable}
\hbox to 1\wd0{\hfill}\ \rm also a \meta{syntactic keyword}$\rangle$

\meta{boolean} \: \schtrue{} \| \schfalse{}
\meta{character} \: \#\backwhack{} \meta{any character}
 \>  \| \#\backwhack{} \meta{character name}
\meta{character name} \: space \| newline
\todo{Explain what happens in the ambiguous case.}
\meta{string} \: " \arbno{\meta{string element}} "
\meta{string element} \: \meta{any character other than \doublequote{} or \backwhack}
 \>  \| \backwhack\doublequote{} \| \backwhack\backwhack %
\end{grammar}

\label{numbersyntax}

\begin{grammar}%
\meta{number} \: \meta{real} %
	      \| \meta{real} + \meta{ureal} i
	   \> \| \meta{real} - \meta{ureal} i %
              \| \meta{real} @ \meta{real}
\meta{real} \: \meta{sign} \meta{ureal}
\meta{ureal} \: \meta{ureal 2} \| \meta{ureal 8} \| \meta{ureal 10} \| \meta{ureal 16}%
\end{grammar}

The following rules for \meta{ureal $R$}, \meta{uinteger $R$}, and
\meta{prefix $R$} should be replicated for \hbox{$R = 2, 8, 10,$}
and $16$:  \todo{Mark Meyer and David Bartley want to fix this.}

\begin{grammar}%
\meta{ureal $R$} \: %
      \meta{prefix $R$} \meta{uinteger $R$} \meta{suffix}
\> \| \meta{prefix $R$} \meta{uinteger $R$} / \meta{uinteger $R$} \meta{suffix}
\> \| \meta{prefix $R$} .\ \atleastone{\meta{digit $R$}} \arbno{\#} \meta{suffix}
\> \| \meta{prefix $R$} \atleastone{\meta{digit $R$}} .\ \arbno{\meta{digit $R$}} %
                        \arbno{\#} \meta{suffix}
\> \| \meta{prefix $R$} \atleastone{\meta{digit $R$}} \arbno{\#} .\ \arbno{\#} \meta{suffix}
\meta{prefix $R$} \: \meta{radix $R$} \meta{exactness} \meta{precision}
	   \> \| \meta{radix $R$} \meta{precision} \meta{exactness}
	   \> \| \meta{exactness} \meta{radix $R$} \meta{precision}
           \> \| \meta{exactness} \meta{precision} \meta{radix $R$}
           \> \| \meta{precision} \meta{radix $R$} \meta{exactness}
           \> \| \meta{precision} \meta{exactness} \meta{radix $R$}
\meta{uinteger $R$} \: \atleastone{\meta{digit $R$}} \arbno{\#}

\meta{sign} \: \meta{empty}  \| + \|  -
\meta{suffix} \: \meta{empty} \| e \meta{sign} \atleastone{\meta{digit}}
\meta{exactness} \: \meta{empty} \| \#i\sharpindex{i} \| \#e\sharpindex{e}
\meta{precision} \: \meta{empty} \| \#s\sharpindex{s} \| \#l\sharpindex{l}
\meta{radix 2} \: \#b\sharpindex{b}
\meta{radix 8} \: \#o\sharpindex{o}
\meta{radix 10} \: \meta{empty} \| \#d
\meta{radix 16} \: \#x\sharpindex{x}
\meta{digit 2} \: 0 \| 1
\meta{digit 8} \: 0 \| 1 \| 2 \| 3 \| 4 \| 5 \| 6 \| 7
\meta{digit 10} \: \meta{digit}
\meta{digit 16} \: \meta{digit} \| a \| b \| c \| d \| e \| f %
\end{grammar}

\todo{Mark Meyer of TI sez, shouldn't we allow {\tt 1e3/2}?}

\subsection{External representations}

\meta{Datum} is what the \ide{read} procedure (section~\ref{read})
successfully parses.  Note that any string which parses as an
\meta{ex\-pres\-sion} will also parse as a \meta{datum}.  \label{datum}

\begin{grammar}%
\meta{datum} \: \meta{simple datum} \| \meta{compound datum}
\meta{simple datum} \: \meta{boolean} \| \meta{number}
\>  \| \meta{character} \| \meta{string} \|  \meta{symbol}
\meta{symbol} \: \meta{identifier}
\meta{compound datum} \: \meta{list} \| \meta{vector}
\meta{list} \: (\arbno{\meta{datum}}) \| (\atleastone{\meta{datum}} .\ \meta{datum})
\>    \| \meta{abbreviation}
\meta{abbreviation} \: \meta{abbrev prefix} \meta{datum}
\meta{abbrev prefix} \: ' \| ` \| , \| ,@
\meta{vector} \: \#(\arbno{\meta{datum}}) %
\end{grammar}

\subsection{Expressions}

\begin{grammar}%
\meta{expression} \: \meta{variable}
\>  \| \meta{literal}
\>  \| \meta{procedure call}
\>  \| \meta{lambda expression}
\>  \| \meta{conditional}
\>  \| \meta{assignment}
\>  \| \meta{derived expression}

\meta{literal} \: \meta{quotation} \| \meta{self-evaluating}
\meta{self-evaluating} \: \meta{boolean} \| \meta{number}
\>  \| \meta{character} \| \meta{string}
\meta{quotation} \: '\meta{datum} \| (quote \meta{datum})
\meta{procedure call} \: (\meta{operator} \arbno{\meta{operand}})
\meta{operator} \: \meta{expression}
\meta{operand} \: \meta{expression}

\meta{lambda expression} \: (lambda \meta{formals} \meta{body})
\meta{formals} \: (\arbno{\meta{variable}}) \| \meta{variable}
\>  \| (\atleastone{\meta{variable}} .\ \meta{variable})
\meta{body} \: \arbno{\meta{definition}} \meta{sequence}
\meta{sequence} \: \arbno{\meta{command}} \meta{expression}
\meta{command} \: \meta{expression}

\meta{conditional} \: (if \meta{test} \meta{consequent} \meta{alternate})
\meta{test} \: \meta{expression}
\meta{consequent} \: \meta{expression}
\meta{alternate} \: \meta{expression} \| \meta{empty}

\meta{assignment} \: (set! \meta{variable} \meta{expression})

\meta{derived expression} \:
\>  \> (cond \atleastone{\meta{cond clause}})
\>  \| (cond \arbno{\meta{cond clause}} (else \meta{sequence}))
\>  \| (c\=ase \meta{expression}
\>       \>\atleastone{\meta{case clause}})
\>  \| (c\=ase \meta{expression}
\>       \>\arbno{\meta{case clause}}
\>       \>(else \meta{sequence}))
\>  \| (and \arbno{\meta{test}})
\>  \| (or \arbno{\meta{test}})
\>  \| (let (\arbno{\meta{binding spec}}) \meta{body})
\>  \| (let \meta{variable} (\arbno{\meta{binding spec}}) \meta{body})
\>  \| (let* (\arbno{\meta{binding spec}}) \meta{body})
\>  \| (letrec (\arbno{\meta{binding spec}}) \meta{body})
\>  \| (begin \meta{sequence})
\>  \| (d\=o \=(\arbno{\meta{iteration spec}})
\>       \>  \>(\meta{test} \meta{sequence})
\>       \>\arbno{\meta{command}})
\>  \| (delay \meta{expression})
\>  \| \meta{quasiquotation}

\meta{cond clause} \: (\meta{test} \meta{sequence})
\>   \| (else \meta{sequence})
\>   \| (\meta{test})
\>   \| (\meta{test} => \meta{recipient})
\meta{recipient} \: \meta{expression}
\meta{case clause} \: ((\arbno{\meta{datum}}) \meta{sequence})
\>   \| (else \meta{sequence})

\meta{binding spec} \: (\meta{variable} \meta{expression})
\meta{iteration spec} \: (\meta{variable} \meta{init} \meta{step})
\> \| (\meta{variable} \meta{init})
\meta{init} \: \meta{expression}
\meta{step} \: \meta{expression} %
\end{grammar}

\subsection{Quasiquotations}

The following grammar for quasiquote expressions is not context-free.
It is presented as a recipe for generating an infinite number of
production rules.  Imagine a copy of the following rules for $D = 1, 2,
3, \ldots$.  $D$ keeps track of the nesting depth.

\begin{grammar}%
\meta{quasiquotation} \: \meta{quasiquotation 1}
\meta{template 0} \: \meta{expression}
\meta{quasiquotation $D$} \: `\meta{template $D$}
\>    \| (quasiquote \meta{template $D$})
\meta{template $D$} \: \meta{simple datum}
\>    \| \meta{list template $D$}
\>    \| \meta{vector template $D$}
\>    \| \meta{unquotation $D$}
\meta{list template $D$} \: (\arbno{\meta{template or splice $D$}})
\>    \| (\atleastone{\meta{template or splice $D$}} .\ \meta{template $D$})
\>    \| '\meta{quasiquotation $D$}
\>    \| \meta{quasiquotation $D+1$}
\meta{vector template $D$} \: \#(\arbno{\meta{template or splice $D$}})
\meta{unquotation $D$} \: ,\meta{template $D-1$}
\>    \| (unquote \meta{template $D-1$})
\meta{template or splice $D$} \: \meta{template $D$}
\>    \| \meta{splicing unquotation $D$}
\meta{splicing unquotation $D$} \: ,@\meta{template $D-1$}
\>    \| (unquote-splicing \meta{template $D-1$}) %
\end{grammar}

In \meta{quasiquotation}s, a \meta{list template $D$} can sometimes
be confused with an \meta{un\-quota\-tion $D$} or \meta{splicing
un\-quo\-ta\-tion $D$}.  The interpretation as an
\meta{un\-quo\-ta\-tion} or \meta{splicing
un\-quo\-ta\-tion $D$} takes precedence.

\subsection{Programs and definitions}

\begin{grammar}%
\meta{program} \: \arbno{\meta{command or definition}}
\meta{command or definition} \: \meta{command} \| \meta{definition}
\meta{definition} \: (define \meta{variable} \meta{expression})
\>   \| (define (\meta{variable} \meta{def formals}) \meta{body})
\meta{def formals} \: \arbno{\meta{variable}}
\>   \| \arbno{\meta{variable}} .\ \meta{variable} %
\end{grammar}


\documentclass[10pt]{article}

\input{../header.tex}

\begin{document}

%\section*{Resonances}

Jacopo Tissino, 1152348, 22 June 2019. The images used come from the slide presentation by Donatella Lucchesi.

\textbf{Describe the concept of \emph{resonance} in particle physics, the Breit-Wigner distribution and its use in data analysis.}

Many particles we would like to study are not stable, but are instead in shallow
minima of their potential: they can tunnel or be excited easily through the potential barriers, and decay before they can be observed moving through space.

We can study them in the brief period of time between when they are formed and when
they decay: suppose we have a process \(\ce{a} + \ce{b} \rightarrow O \rightarrow \ce{c} + \ce{d} \), where \(O\) is the metastable state we want to study.

We expect the cross section to be higher if a metastable resonant state is formed: the probability of going from \(\ce{a}  + \ce{b}  \) to \(\ce{c} + \ce{d} \) directly will be less than the probability of the process happening through \(O\).

This qualitative statement can be formalized:

\begin{equation} \label{eq:breit-wigner}
    \sigma(E)  =
    \frac{(2J+1)}{(2s_a + 1) (2s_b + 1)} \frac{4 \pi }{E^2}
    \frac{\Gamma_i \Gamma_f}{(E-M_R)^2 + (\Gamma/2)^2}
\end{equation}

where

\begin{itemize}
    \item \(\sigma\) is the scattering integral cross section, which is calculated as \((\#\text{events} - \#\text{background events})/ L\), where \(L\) is the time-integrated luminosity;
    \item \(E = \sqrt{s} \) is the center of mass energy;
    \item \(J\) is the spin quantum number of the resonant state;
    \item \(s_{a, b}\) are the spin quantum numbers of the reacting particles: these spin terms are included to account in order to average over all possible polarizations of the incoming particles;
    \item \(\Gamma_{i,f}\) are the initial and final partial widths of the reaction, respectively the width for \(\ce{a} + \ce{b} \rightarrow O\) and \(O \rightarrow \ce{c} + \ce{b} \),
    \item \(\Gamma\) is the resonance width: the square modulus of the wavefunction of the resonance contains the term \(\exp(- \Gamma t) \),
    \item \(M_R\) is the mass of the resonant state.
\end{itemize}

Note that the Breit-Wigner formula comes from a first-order expansion, so it is only valid near the peak: when \(\abs{E-M_R} \gg \Gamma \) we cannot use it.

An example of application of this formula was the discovery of the \(\Delta\) baryon: when colliding \(\pi^{\pm}\) on a fixed proton target, we see a resonance at an energy of \SI{1236}{MeV}: if we plot the scattering cross section \(\sigma\) against the center of mass energy \(E = \sqrt{s} \) we see a peak, which corresponds to \(E = M_R\).

\begin{figure}[H]
    \centering
    \includegraphics[width=0.5\textwidth]{breit-wigner.png}
    \caption{Resonance peak corresponding to the \(\Delta\) baryon.}
    \label{fig:delta-baryon}
\end{figure}

Another way to find resonances in reactions of the form \(\ce{a} + \ce{b} \rightarrow \ce{R} + \ce{e} \rightarrow \ce{c} + \ce{d} + \ce{e} \), that is, some of the times the reaction occurs it passes through the metastable state \(\ce{R} \) which decays into \(\ce{c} + \ce{d}\).

This reaction path will be favoured over others, so we can histogram the number of events we see at each value of the invariant mass of the particle pair \(\ce{c} + \ce{d}\).

\begin{figure}
    \center
    \includegraphics[width=0.34\textwidth]{eta.png}
    \caption{Resonances in Bevatron bubble chamber. Number of events is plotted against the invariant mass of the \((\pi^+ + \pi^+ + \pi^0)\) system. The dotted line represents the phase-space volume corresponding to the invariant mass, normalized in order for it to have integral \(N=233\).}
    \label{fig:bevatron}
\end{figure}

This was used, for example, in the study of a beam of \(\SI{1.23}{GeV/c} \) \(\pi^+\) going into a bubble chamber of liquid deuterium. The reaction is

\begin{equation} \label{eq:process}
    \pi ^+ + \ce{d} \rightarrow
    x + \ce{p}  + \ce{p}
    \rightarrow \pi^+ + \pi^- + \pi^0 + \ce{p} + \ce{p}
\end{equation}

and we assume the new particle \(x\) decays into \(\pi^+ + \pi^- + \pi^0 \) so we look at the histogram with respect to the invariant mass of this combination.
We would expect to get the curve of the Lorentz-invariant phase-space volume corresponding to the differential mass \(\dd{m} \):

\begin{equation}
    f(m) \propto
    %\frac{1}{(2 \pi)^9}
    \int \frac{\dd[3]{p_{\pi^+}}\dd[3]{p_{\pi^-}}\dd[3]{p_{\pi^0}}}
    {8E_{\pi^+}E_{\pi^-}E_{\pi^0}}
\end{equation}

where the integral extends over all of the combinations of phase-space coordinates such that
\(m \leq \sqrt{(\sum E_i)^2 - (\sum p_i)^2} \leq m + \dd{m}\) (\(i\) labels the three \(\pi\) particles).

This will have some smooth shape, and we can calculate its bounds knowing the momentum of the \(\pi^+\) is \(\SI{1230}{MeV}\), the masses of the \(\pi^{\pm}\) are \SI{139.570}{MeV}, the mass of the \(\pi^0\) is \SI{134.977}{MeV}, the mass of the deuton is \SI{1875.612}{MeV}, the mass of the proton is \SI{938.783}{MeV}.

Then, then energy of the incoming beam will be \(E_{\pi^+} = \SI{1238}{MeV}\) and we will have \(\sqrt{s} = \sqrt{
(E_{\pi^+} + m_d)^2 - p_{\pi^+}^2
} \approx \SI{2860}{MeV} \).

Surely the invariant mass \(m = m(\pi^+ + \pi^- + \pi^0)\) will not be less than the sum of the \(\pi\) masses, which equals \(m(\pi^+) + m(\pi^-) + m(\pi^0)=\SI{414.12}{MeV} \leq m\).

Also, we must have \(m + m_d \leq \sqrt{s} \). This means \(m \leq \SI{984.6}{MeV} \).

Instead of a smooth curve between these extremes, we see two definite peaks: the results are shown in figure \ref{fig:bevatron}.

We fit each peak with a Lorentzian, whose FWHM is \(\Gamma\), and whose center is \(E_R\). These correspond to two possible \(x\) particles in the process \eqref{eq:process}: the fit results are shown in table \ref{fig:decay-pi}, labelled by the names of the two particles.

\begin{table}[H]
    \centering
    \begin{tabular}{c|c|c}
            & \(E_R\) &  \(\Gamma\) \\
            \hline
        \(\omega\) &  \SI{782}{MeV}  &   \SI{8}{MeV}    \\
        \hline
        \(\eta\) &  \SI{548}{MeV}    &   \SI{1.3}{MeV}    \\
    \end{tabular}
    \caption{Fit parameters for the particles which decay into \(\pi^+ + \pi^- + \pi^0 \).}
    \label{fig:decay-pi}
\end{table}

Thus, through a Breit-Wigner fit we can very precisely measure the properties (mass and average lifespan) of a resonant state which only exists for an incredibly short time: for example, the \(\eta\) has \(\Gamma=\SI{1.3}{MeV} \), so its lifespan is of the order \(\tau = \hbar/\Gamma \approx \SI{5e-22}{s}\).

%
% We derive the formula \eqref{eq:breit-wigner} in the case \(\ce{a}, \ce{b} = \ce{c}, \ce{d}\).
% Recall the fact that, if the incoming wavefunction is a planar wave and the outgoing function is of the form \(\psi =  f(\theta, \varphi) \exp(pr/i \hbar) / r \), then the differential scattering cross section is
%
% \begin{equation}
%     \dv{\sigma}{\Omega} = \abs{f(\theta, \varphi)}^2
% \end{equation}
%
% In our case the widths are all the same, \(\Gamma = \Gamma_i = \Gamma_f\), since there is only one transition type. It can be shown %by Fourier transforming the potential?
% that to first order
%
% \begin{equation}
%     f = \frac{E_R - E}{\Gamma/2}
% \end{equation}
%
% The factor \(4 \pi (2J+1) / k^2\) comes from an optical analysis.
%
% The factor \(1/(2s_a+1) (2s_b+1)\) comes from summing over the spin states.

\end{document}


\pdfoutput=1

% !TEX TS?program = pdflatexmk
\documentclass[12pt, a4paper]{article}
% Use the option doublespacing or reviewcopy to obtain double line spacing
\usepackage[utf8]{inputenc}

\usepackage[style= bwl-FU, backend = biber, maxbibnames=99, maxcitenames=2,uniquelist=minyear, firstinits=true]{biblatex}

% \AtEveryBibitem{\clearfield{number}}
% \AtEveryBibitem{\clearfield{doi}}
\AtEveryBibitem{\clearfield{note}}
\AtEveryBibitem{\clearfield{urldate}}
\AtEveryBibitem{\clearfield{issn}}
\AtEveryBibitem{\clearfield{isbn}}
%
\addbibresource{Library.bib}

\usepackage{authblk}
\usepackage{color,graphicx,tikz}
\usetikzlibrary{positioning,arrows}
% The amssymb package provides various useful mathematical symbols
\usepackage{mathtools,amssymb,amsmath,mathdots,amsfonts}
\usepackage[mathscr]{eucal} %just for the font \mathscr
\usepackage{enumerate}
%\usepackage{amsthm}
%\usepackage{mathaccents}
\usepackage{setspace}
\newtheorem{remark}{Remark} [section]
\usepackage{hyperref}

\input{mst-macros}
\input{macros}

% \graphicspath{{../images/}}
%\graphicspath{{Media/}}

\doublespacing
% \setlength{\topmargin}{0cm} \addtolength{\textheight}{2cm}
\evensidemargin=0cm \oddsidemargin=0cm \setlength{\textwidth}{16cm}

\begin{document}

\title{Notes on effective waves in a multi-species material}

% \author{
% Artur L. Gower$^{a}$, Michael J. A. Smith$^{a}$, \\ William Parnell$^{a}$ and David Abrahams$^{a,b}$ \\[12pt]
% \footnotesize{$^{a}$ School of Mathematics, University of Manchester, Oxford Road, Manchester, M13 9PL, UK}\\
% \footnotesize{$^{b}$ Isaac Newton Institute for Mathematical Sciences, 20 Clarkson Rd, Cambridge CB3 0EH, UK}
% }

\author[$\dagger$]{Artur L.\ Gower}

\affil[$\dagger$]{School of Mathematics, University of Manchester, Oxford Road, Manchester M13 9PL, UK}

\date{\today}
\maketitle

\begin{abstract}
This Supplementary Material is a self-contained document providing further detail on the  calculation of effective wavenumbers for uniformly distributed multi-species inclusions. The formulae for multi-species cylinders and spheres are given here, in addition to expressions describing reflection from a halfspace filled with cylinders. Code to implement the formulas is given in \url{github.com/arturgower/EffectiveWaves.jl}.  For detailed derivations see \href{https://arxiv.org/abs/1712.05427}{our paper}~\parencite{gower_reflection_2017}, which shows how to introduce a pair-correlation between the species.
\end{abstract}

\noindent
{\textit{Keywords:} polydisperse, multiple scattering, multi-species, effective waves, quasicrystalline approximation, statistical methods }

\section{Effective waves for uniformly distributed species}
\label{sec:results}

We consider a halfspace $x>0$ filled with $S$ types of inclusions (species) that are uniformly distributed. The fields are governed by the scalar wave equation:
\begin{align}
  &\nabla^2 u + k^2 u = 0, \quad \text{(in the background material)} \\
  &\nabla^2 u + k^2_j u = 0, \quad \text{(inside the $j$-th scatterer)},
\end{align}
 The background and species material properties are summarised in Table~\ref{tab:properties}.
The goal is to find an effective homogeneous medium with wavenumber $k_*$, where waves propagate, in an
\href{https://en.wikipedia.org/wiki/Ensemble_average_(statistical_mechanics)}{ensemble average sense}, with the same speed and attenuation as they would in a material filled with scatterers. See~\cite{gower_effectivewaves.jl:_2017} for the code that implements the formulas below.

Below we present the effective wavenumber, for any incident wavenumber and moderate number fraction, when the species are either all cylinders or spheres\footnote{In principal these formulas can be extended to include different shaped scatterers by using Waterman's T-matrix \cite{waterman_symmetry_1971,varadan_multiple_1978,mishchenko_t-matrix_1996}}.
  For cylindrical inclusions we also present the reflection of a plane wave from this multi-species material.

\input{table_properties.tex}

% The scalar wave equation appears in electromagnetism when the magnetic field is polarised in the direction along the cylinders by replacing $c \mapsto c_0 / n$ and $\rho \mapsto \mu_\mathrm{r}$, where $c_0$ is the speed of light in vacuum, $n$ is the refractive index of the material,  and $\mu_\mathrm{r}$ is the linear magnetic susceptibility. And likewise, when the electric field is polarised along the cylinders, with the replacements $c \mapsto c_0 / n$   and $\rho \mapsto \varepsilon_{r}$, where $\varepsilon_\mathrm{r}$ is the linear electric susceptibility.

\section{Cylindrical species}

We consider an incident wave
\begin{gather}
  u_\inc =  \ee^{\ii \mathbf k \cdot \mathbf x} \quad \text{with} \quad \mathbf k \cdot \mathbf x = k x \cos \theta_\inc  + k y \sin \theta_\inc,
%   u_\inc(x,y) = \ee^{\ii \mathbf k \cdot \mathbf x}, \quad \text{with} \quad \mathbf k \cdot \mathbf x = \alpha  x  + \beta y,
% \notag \\
%   \text{where} \qquad  \alpha = k \cos \theta_\inc, \quad \beta = k \sin \theta_\inc,
  \label{eqns:incident}
\end{gather}
and angle of incidence $\theta_\inc$ from the $x$-axis, exciting a material occupying the halfspace $x>0$. Then, assuming low number density $\nfrac {}$ (or low volume fraction $\sum_\ell \pi a_\ell^2 \nfrac \ell$), the effective transmitted wavenumber $k_\eff$ becomes
\begin{align}
   k_\eff^2 = k^2 - 4 \ii \nfrac {} \ensem{f_\circ}(0) -  4 \ii \nfrac {}^2 \ensem{f_{\circ\circ}}(0)
   + \mathcal O(\nfrac {}^3),
  \label{eqn:SmallNfracDiscrete}
\end{align}
with $\ensem{f_\circ}$ and $\ensem{f_{\circ\circ}}$ given by \eqref{eqn:fcircs}. The above reduces to \cite{linton_multiple_2005} equation (81) for a single species in the low frequency limit; This equation (81) has been confirmed by several independent methods~\cite{martin_estimating_2010,martin_multiple_2008,chekroun_time-domain_2012,kim_models_2010}.

The ensemble-average reflected wave measured at $x <0$ is given by
\begin{equation}
   \langle u_\mathrm{ref}\rangle =
     \frac{\nfrac {}}{\alpha^2}\left [
 R_1 + \nfrac {} R_2
  \right]\ee^{-\ii \alpha x + \ii \beta y} + \mathcal O (\nfrac {}^3),
  \label{eqn:reflection_results}
\end{equation}
where
\begin{align}
  &R_1 = \ii \ensem{f_\circ}(\theta_\reflect), \quad \theta_\reflect = \pi - 2 \theta_\inc,
  \\
  &R_2 = \frac{2 \ensem{f_\circ}(0)}{k^2 \cos^2 \theta_\inc}\left [\sin \theta_\inc \cos \theta_\inc \ensem{f_\circ}'(\theta_\reflect)  - \ensem{f_\circ}(\theta_\reflect) \right] + \ii \ensem{f_{\circ\circ}}(\theta_\reflect),
\end{align}
which reduces to \cite{martin_multiple_2011} equations (40-41) for a single species, which they show agrees with other known results for small $k$.

The ensemble-average far-field pattern and multiple-scattering pattern are\footnote{Note we introduced the terminology ``multiple-scattering pattern''.}
\begin{align}
  &\ensem{f_\circ}(\theta) = - \sum_{\ell=1}^S  \sum_{n=-\infty}^\infty  \frac{\nfrac \ell}{\nfrac {}} \scatZ_\ell^n \ee^{\ii n \theta},
\notag \\
  &\ensem{f_{\circ\circ}}(\theta) = - \pi  \sum_{\ell,j=1}^S \sum_{m,n=-\infty}^\infty  a_{\ell j}^2 d_{n-m}(k a_{\ell j})  \frac{\nfrac \ell \nfrac j}{\nfrac {}^2}\scatZ_\ell^n \scatZ_j^m \ee^{\ii n \theta},
  \label{eqn:fcircs}
\end{align}
where $d_m(x) = J_m'(x) H_m'(x) + (1 - (m/x)^2)J_m(x) H_m(x)$, the $J_m$ are Bessel functions, the $H_m$ are Hankel functions of the first kind and $a_{\ell j} > a_\ell + a_j$ is some fixed distance. The  $\scatZ^m_j$ describe the type of scatterer:
\be
\scatZ^m_j = \frac{q_j J_m' (k a_j) J_m ( k_j a_j) - J_m (k a_j) J_m' (k_j a_j) }{q_j H_m '(k a_j) J_m(k_j a_j) - H_m(k a_j) J_m '(k_j a_j)} = \scatZ^{-m}_j,
\label{eqn:Zm}
\en
with $q = (\rho_j c_j)/(\rho c)$. For instance, taking the limits $q \to 0$ or $q \to \infty$, recovers Dirichlet or Neumann boundary conditions, respectively.

\subsection{Any volume fraction}

The series expansions for low number density (or volume fraction) do not work when the particles are strong scatterers. In these cases we need to use formulas valid for any volume fraction.

Borrowing equations (45 - 47) from~\cite{gower_reflection_2017} we have
\begin{align}
    &k_\eff \sin \theta_\eff = k \sin \theta_\inc  \quad \text{with} \quad \mathbf k_\eff =(\alpha_\eff, \beta) := k_\eff(\cos\theta_\eff, \sin\theta_\eff),
    \label{eqn:Snells}
  \\
    &  \sum_\ell \sum_{n=-\infty}^\infty ( 2 \pi\nfrac \ell
  \mathcal Q^{n-m}_{j \ell}(k_\eff) \scatZ^n_\ell + \delta_{m n} \delta_{j \ell}  ) \A n_\ell
   = 0,
 \label{eqn:AmT}
\\
  &
   2\sum_{n=-\infty}^\infty \ee^{\ii n (\theta_\inc - \theta_\eff)} \sum_\ell
   \nfrac \ell \scatZ^n_\ell \A n_\ell = (\alpha_\eff-\alpha) \ii \alpha,
 \label{eqn:AmInc}
 \end{align}
in terms of the unknown parameters $\A n_\ell$ and $k_\eff$, where
\begin{align}
  &\mathcal Q^n_{j \ell}(k_\eff) =  \frac{\mathcal N_{n}(ka_{j\ell},k_\eff a_{j\ell})}{k^2 - k_\eff^2}  +  \mathcal X_{n}(\s_j,\s_\ell),
   \\
  & \mathcal N_n(x,y) = x H_n'(x) J_n(y) - y H_n(x) J_n'(y),
\label{eqn:Nn}
\end{align}
and $\mathcal X_{n} = 0$ for {\it hole correction}, or for a more general pair distribution
\begin{equation}
   \mathcal X_{n}(\s_j,\s_\ell) =  \int_{a_{j \ell}< R < \bar a_{j \ell}} H_{n}(k R) J_{n}(k_\eff R) \chi(R | \s_j, \s_\ell)  R \, d R,
   \label{eqn:X_eff}
\end{equation}
where we assume that when the distance between two cylinders $R_{j \ell} > \bar a_{j \ell}$, then the pair correlation is the same as hole correction.

In the notation given in~\cite{gower_reflection_2017} we replaced $\A m_\eff(\s_2) \to \A m_\ell$, $\p(\s_2) \to \delta(\s_2 - \s_\ell) \frac{\nfrac \ell}{\nfrac {}}$,  $\nfrac {} = \sum_\ell \nfrac j$, $\nfrac {} = \sum_\ell \nfrac j$,
$\scatZ^n(\s_2) \to \scatZ^n_j$, $\mathcal X_{\eff} \to \mathcal X_{n-m}(\s_j,\s_\ell)$ and here we assumed no boundary-layer $\bar x = 0$.
%
% Rewriting in terms of infinite dimensional tensors
% \begin{align}
%    & (\vec A_\ell)_n = \A n_\ell, \quad  (\vec z_\ell)_n =  \sqrt{\scatZ^n_\ell},
%     % \;\; n=-N,\ldots, N,
%    \\
%    & (\vec { Q}_{j \ell})_{mn} =  \mathcal Q^{n-m}_{j \ell}(k_\eff),
%  \end{align}
%
% \begin{align}
%     &  \sum_\ell \sum_{n=-\infty}^\infty ( 2 \pi\nfrac \ell
%   \vec {Q}_{j \ell}(k_\eff) \scatZ^n_\ell + \delta_{m n} \delta_{j \ell}  ) \A n_\ell
%    = 0,
% \\
%   &
%    2\sum_{n=-\infty}^\infty \ee^{\ii n (\theta_\inc - \theta_\eff)} \sum_\ell
%    \nfrac \ell \scatZ^n_\ell \A n_\ell = (\alpha_\eff-\alpha) \ii \alpha,
%  \end{align}

Now we approximate (\ref{eqn:AmInc},\ref{eqn:AmT}) by summing $n$ from $-N$ to $N$ and then rewriting these equations as
\begin{align}
    &  \sum_\ell \vec M_{j \ell} \vec A_\ell
   = 0, \implies \det (\vec M_{j \ell}) = 0,
   \label{eqn:secular}
% \\
%   &
%     \sum_\ell \vec Z_\ell \cdot \vec A_\ell = (\alpha_\eff-\alpha) \ii \alpha,
%    \label{eqn:effective_amplitude}
 \end{align}
where
\begin{align}
   & (\vec A_\ell)_n = \A n_\ell, \quad
  %  (\vec Z_\ell)_n =  \scatZ^n_\ell,
    % \;\; n=-N,\ldots, N,
   (\vec M_{j \ell})_{mn} =  2 \pi\nfrac \ell \scatZ^n_\ell \mathcal Q^{n-m}_{j \ell}(k_\eff) + \delta_{m n} \delta_{j \ell},
 \end{align}
and $n, m = -N, -N +1, \ldots N$.

The strategy to solve these equations is to: find $k_\eff$ such that the determnant in~\eqref{eqn:secular} is zero and then find the eigenvector $\mathbb A$ of $(\vec M_{j \ell})$ with the smallest eignvalue; use Snell's law~\eqref{eqn:Snells} to find $\theta_\eff$; finally use~\eqref{eqn:AmInc} to determine the magnitude of $\mathbb A$.

One concern, is that the solutions $k_\eff$ to \eqref{eqn:secular} are not unique.

\subsubsection{Reflection coefficient}

Borrowing equation (88) from~\cite{gower_reflection_2017}, the average reflection from a halfspace is
\begin{align}
  &\ensem{u_\mathrm{ref}(x,y)} = \ee^{\ii k( - x \cos \theta_\inc + y \sin \theta_\inc)} R,
  \\
  & R =  \frac{2 \ii }{\alpha (\alpha + \alpha_\eff)} \sum_{\ell = 1}^S \sum_{n=-\infty}^\infty \ee^{\ii n \theta_\mathrm{ref}} \nfrac \ell \A n_\ell \scatZ^n_\ell,
\end{align}
with $\theta_\mathrm{ref} = \pi - \theta_\inc - \theta_\eff$.

\section{Spherical species}
\label{sec:Effective3D}

The results here are derived by applying the theory in our paper to the results in \cite{linton_multiple_2006}. We omit the details as the result follows by direct analogy.

For spherical inclusions the transmitted wavenumber becomes,
\begin{align}
   k_\eff^2 = k^2 -\nfrac {} \frac{4 \pi \ii}{k}  \ensem{F_\circ}(0) + \nfrac {}^2 \frac{(4 \pi)^2 }{k^4} \ensem{F_{\circ\circ}}
   + \mathcal O(\nfrac {}^3),
  \label{eqn:SmallNfracSpheres}
\end{align}
where for spheres we define the ensemble-average far-field pattern and multiple-scattering pattern,
\begin{align}
  &\ensem{F_\circ}(\theta) = - \sum_{n=0}^\infty P_n (\cos \theta ) \sum_{j=1}^S (2n+1)  \scatZs^n_j \frac{\nfrac j}{\nfrac{}},
\\
  &\ensem{F_{\circ\circ}} = \frac{\ii (4 \pi)^2}{2} \sum_{n,p=0}^\infty \sum_{j,\ell=1}^S \sum_q  \frac{\sqrt{(2n+1)(2p+1)}}{(4 \pi)^{3/2}}  \sqrt{2 q +1} \mathcal G(n,p,q) k a_{j\ell} D_q(k a_{j\ell}) \scatZs^n_j \scatZs^p_\ell \frac{\nfrac j \nfrac \ell}{\nfrac{}^2},
\notag
\end{align}
where
\[
D_m(x) = x j_m'(x)( x h_m'(x) + h_m(x)) + (x^2 - m(m+1))j_m(x) j_m(x),
\]
$P_n$ are   Legendre polynomials, $j_m$ are   spherical Bessel functions, $h_m$ are spherical Hankel functions of the first kind  and
\be
\scatZs^m_j = \frac{q_j j_m' (k a_j) j_m ( k_j a_j) - j_m (k a_j) j_m' (k_j a_j) }{q_j h_m'(k a_j) j_m(k_j a_j) - h_m(k a_j) j_m '(k_j a_j)} = \scatZs^{-m}_j,
\label{eqn:Zm}
\en
with $q = (\rho_j c_j)/(\rho c)$, where the $\mathcal G$ is a Gaunt coefficient and is equal to
\[
\mathcal G(n,p,q) = \frac{\sqrt{(2n+1)(2p+1)(2q+1)}}{2 \sqrt{4 \pi}}   \int_0^\pi  P_n(\cos \theta) P_p(\cos \theta) P_q(\cos \theta) \sin \theta d \theta.
\]
See \cite{caleap_effective_2012} for details on reflection from a single species, although, to our knowledge, a formula for reflection from a single species valid for moderate number fraction and any wavenumber has not yet been deduced.

\printbibliography

% \bibliographystyle{RS}
% \bibliography{../Library2}

\end{document}


\documentclass[../mit-general-chemistry.tex]{subfiles}
\begin{document}

\chapter{Chemical equilibrium}

Chemical reactions reach a state of chemical equilibrium in which the
rate of forward and reverse reactions are equal and there is no net
change in composition.

\section{Nature of chemical equilibrium}

Consider the Haber process

\ceeq{N2(g) + 3H2(g) <=> 2NH3(g)\label{rxn:amonia}}

Say we start with a mixture of nitrogen gas and hydrogen gas. In a
diagram of the concentrations of reactants and product over time we
would see the concentrations of the reactants decrease as the reaction
progres and then level off.

Initially, we have pure reactants and the reaction is spontaneous in
the forward direction. We can tell that $\gibbs < 0$ as this is a
forward reaction.

\begin{center}
  \begin{tikzpicture}
    \begin{axis}[
        width=6cm,
        height=6cm,
        xlabel = time,
        ylabel = concentration,
        axis lines = left,
        xmin=0,
        xmax=10,
        ymin=0,
        ymax=10,
        xtick = {},
        ytick = {},
        xticklabels = \empty,
        yticklabels = \empty,
        clip = false,
      ]
      \draw[blue] (0,9) to[curve through={(1,8) .. (2, 7.5) .. (3, 7)}] (10, 5.8);
      \draw[blue] (0,8) to[curve through={(1,7) .. (2, 6.5) .. (3, 6)}] (10, 5);
      \draw[red!50!black] (0,0) to[curve through={(1,1) .. (2, 1.5)}] (10, 2.8);
    \end{axis}
  \end{tikzpicture}
\end{center}

If you would have pure products, then the reaction would be
spontaneous in the reverse direction and $\gibbs > 0$.

We interpreted Gibb's free energy in terms of stable or unstable
products relative to reactants. When $\gibbs < 0$, the product is more
stable than the reactants and vice versa.

\begin{center}
  \begin{tikzpicture}
    \begin{axis}[
        width=8cm,
        height=6cm,
        xlabel = progress of reaction,
        ylabel = free energy,
        axis lines = left,
        xmin=0,
        xmax=5,
        ymin=0,
        ymax=5,
        xtick = {},
        ytick = {},
        xticklabels = \empty,
        yticklabels = \empty,
        clip = false,
      ]
      \addplot[domain=0:4]{(x - 2)^2};
      \draw[dashed] (2, -0.3) -- (2, 1)
      node[at end,above]{$\gibbs = 0$};
      \node[text width=1.5cm] at (axis cs:.7, .6) {\scriptsize pure reactants, $\gibbs < 0$};
      \node[text width=1.5cm] at (axis cs:5, .6) {\scriptsize pure products, $\gibbs > 0$};
    \end{axis}
  \end{tikzpicture}
\end{center}

If we have pure reactants and $\gibbs < 0$. Reaction~\ref{rxn:amonia} will
progress, the reactants will spontaneously form products and the free
energy will decrease.

On the other hand, if we have a state of pure reactants, \gibbs will
be greater than zero. Now, products will spontaneously dissolve into
the reactants.

This will happen until the system reaches equilibrium. So \gibbs is
closely related to the notion of chemical equilibrium.

The reaction free energy ($\gibbs_r$) chamges as the proportions of
reactants and products change.

This is reflected in the equation
\begin{equation}\label{eq:free-energy}
  \gibbs = \gibbs^0 + RT \ln Q
\end{equation}

Where

\begin{center}
  \begin{tabularx}{.67\textwidth}{lX}
    \gibbs & Reaction free energy at any definite fixed composition of
    the reaction mixture. \\
    $\gibbs^0$ & The difference in molar free energy of the products
    on one hand, and reactants on the other, in their standard
    states. \\
    $Q$ & Reaction quotient. \\
    $R$ & Universal gas constant. \\
    $T$ & Temperature (K). \\
  \end{tabularx}
\end{center}

The reaction quotient, $Q$, os calculated in different way depending
on whether it occurs in a gaseous state or in solution.

For some reaction

\ceeqstar{aA + bB <=> cC + dD}

When reactants and prodicts are in a gasous state the reaction
quotient is defined as
\begin{equation}
  Q =
  \left[
    \frac{
      \left( \frac{P_C}{P_{\text{ref}}} \right)^c
      \left( \frac{P_D}{P_{\text{ref}}} \right)^d
    }{
      \left( \frac{P_A}{P_{\text{ref}}} \right)^a
      \left( \frac{P_B}{P_{\text{ref}}} \right)^b
    }
    \right]
\end{equation}
where $P_{\text{ref}}$ is a reference partial pressure. Under standard
conditions $P_{\text{ref}} = \SI{1}{\bar}$. Under those conditions
\begin{equation}
  Q =
  \frac{
    (P_C)^c
    (P_D)^d
  }{
    (P_A)^a
    (P_B)^b
  }
\end{equation}

In solution the reaction quotient is defined by
\begin{equation}
  Q = \frac{
    \left( \frac{[C]}{C_{\text{ref}}} \right)^c
    \left( \frac{[D]}{C_{\text{ref}}} \right)^d
  }{
    \left( \frac{[A]}{C_{\text{ref}}} \right)^a
    \left( \frac{[B]}{C_{\text{ref}}} \right)^b
  }
\end{equation}
for the concentrations of the reactants and products.

For a refernce concentration, $C_{\text{ref}}$, of \SI{1}{\mol} the
equations is simplified
\begin{equation}
  Q = \frac{ [C]^c [D]^d }{ [A]^a [B]^b }
\end{equation}

\section{Meaning of $K$}

At equilibrium $\gibbs = 0$ and $Q = K$, the equilibrium constant. For
$\gibbs = 0$ we can solve Equation~\ref{eq:free-energy} for $\gibbs^0$
\begin{align*}
  \gibbs &= \gibbs^0 + RT \ln Q \\
  0 &= \gibbs^0 + RT \ln K & \text{for } \gibbs = 0 \\
  \gibbs^0 &= -RT \ln K \\
\end{align*}

$K$ is the equilibrium constant. $K = Q$ at equilibrium as in the
equations above. It has the same form as $Q$ but only uses the amounts
of reactants and products at equilibrium.

\section{Relationship between equilibrium expressions}

Now, we can rewrite Equation~\ref{eq:free-energy} one more time
\begin{equation}
  \gibbs = \gibbs^0 + RT \ln Q = -RT \ln K + RT \ln Q = RT \ln\left(\frac{Q}{K}\right)
\end{equation}

Lets think about the factor $\ln\left(\frac{Q}{K}\right)$ for a
minute. If $Q < K$ then $\gibbs < 0$ and the forward reaction will
occur. If $Q > K$ then $\gibbs > 0$ and the reverse reaction will
occur.

\begin{example}
  If $K = \num{1.9d-4}$ at \SI{400}{\kelvin} and
  \begin{center}
    \begin{tabular}{ll}
      $P_{\ce{NH3}}$ & \SI{1.1}{\bar} \\
      $P_{\ce{N2}}$ & \SI{5.5}{\bar} \\
      $P_{\ce{H2}}$ & \SI{2.2}{\bar} \\
    \end{tabular}
  \end{center}
  also at \SI{400}{\kelvin}.

  Now consider
  \ceeqstar{N2(g) + 3H2(g) <=> 2NH3(g)\tag{a}}

  Which direction will the reaction go?

  Lets calculate
  \begin{align*}
    Q &= \frac{(P_{\ce{NH3}})^2}{(P_{\ce{N2}})(P_{\ce{H2}})^3} =
    \frac{(1.1)^2}{(5.5)(2.2)^3} =
    \num{2.067d-2} > K
  \end{align*}

  Now, since $Q > K$ under these conditions
  \begin{equation*}
    \ln\left(\frac{Q}{K}\right) > 0
  \end{equation*}
  and the reaction will go in the
  reverse direction, forming nitrogen gas and hydrogen gas from ammonia
  gas. Ammonia will dissociate until equilibrium is reached.
\end{example}

What does $K$ tell us? It tells us the mixture of reactants and
products at equilibrium, wether we can expect high or low
concentration of products at equilibrium.

\begin{equation*}
  K = \left[ \frac{\text{products}}{\text{reactants}} \right]_{\text{equilibrium}}
\end{equation*}

When $K > 1$ we will find a high concentration of products at
equilibrium and conversively when $K < 1$ we will find a low (-er)
concentration of products at equilibrium. This comes, naturally, from

For $K > 1$
\ceeqstar{2NO2 <=> N2O4}

This reaction has $\gibbs^0 = \SI{-4.76}{\kilo\joule\per\mol}$ and $K
= 6.84$ at \SI{298}{\kelvin}.

Initially, we start with a pressure at \SI{1}{\bar} of \ce{NO2}
(reactants) and no \ce{N2O4} (product).

\begin{center}
  \begin{tikzpicture}
    \begin{axis}[
        width=8cm,
        height=6cm,
        xlabel = progress of reaction (time),
        ylabel = partial pressure (\si{\bar}),
        axis lines = left,
        xmin=0,
        %xmax=2,
        ymin=0,
        ymax=4,
        xtick = {},
        ytick = {2.5},
        xticklabels = \empty,
        yticklabels = 1,
        clip = false,
      ]
      \addplot[domain=0.001:10]{1/(x + .5) + .5}
      node[pos=.9, above]{\ce{NO2} (reactant)};
      \addplot[domain=0.001:10]{2 - (1/(x + .5))}
      node[pos=.9, above]{\ce{N2O4} (product)};
    \end{axis}
  \end{tikzpicture}
\end{center}

Early on, we have no products in the reaction mix. This means that $Q
< K$ which in turn yields $\gibbs < 0$. This makes the reaction
spontaneous in the forward direction, which means the reactants start
to form products.

So, what will be the concentrations at equilibrium? We can go ahead
and calculate this.
\begin{center}
  \begin{tabular}{llll}
    & \ce{2NO2} & \ce{<=>} & \ce{N2O4} \\
    initial pressure & 1 & & 0 \\
    change & $-2x$ & & $+x$ \\
    equilibrium & $1 - 2x$ && $x$ \\
  \end{tabular}
\end{center}

Now, we know $K = 6.84$, so let us plug this in
\begin{equation*}
  K = 6.84 = \frac{P_{\ce{N2O4}}}{(P_{\ce{NO2}})^2}
  = \frac{x}{(1 - 2x)^2}
\end{equation*}

Solving this for $x$ we get $x = \SI{0.381}{\bar}$. From our
calculations we can plug this in and find the concentrations at
equilibrium.
\begin{align*}
  P_{\ce{NO2}} &= 1 - 2x = 1 - 2(0.381) = \SI{0.238}{\bar} \\
  P_{\ce{N2O4}} &= x = \SI{0.381}{\bar} \\
\end{align*}

We see that, at equilibrium, we have more product than reactants which
is consistent with $K > 1$.

It is easyly understood from  Equation~\ref{eq:free-energy} that
\begin{equation}
  \gibbs^0 = -RT \ln K \quad\Leftrightarrow\quad K = e^{ - \frac{\gibbs^0}{RT}}
\end{equation}
and that this suggests that $K$ is large if $\gibbs^0$ is large and
negative. If $K$ is a large, positive number, then there is going to
be a lot of products and not so much reactants and vice versa.

If the overall reaction can be written as a {\em sum} of two or more
chemical equations, the equilibrium constant for the overall reaction
is the {\em product} of the equilibrium constants for the component
reactions.

\begin{align*}
  &\ce{2P(g) + 3Cl2(g) <=> 2PCl3(g)} &K_1 \tag{a}\\
  &\ce{PCl3(g) + Cl2(g) <=> PCl5(g)} &K_2 \tag{b}\\
  &\ce{2P(g) + 5Cl2(g) <=> 2PCl5(g)} &K_3 \tag{c}\\
\end{align*}

We see that we can write an formula of the first two equations to
obtain the third as $a + b + b = a + 2b = c$. If $K_1$ and $K_2$ are
known, we can calculate $K_3 = K_1 \cdot K_2^2$. Since we add $b$
twice, we need to take $K_2$ to the second power when we're
multiplying the equilibrium constants.

\section{External effects on the relationship of $Q$ and $K$}

\begin{definition}[Le Châtelier's principle]
  A system in equilibrium that is subjected to stress will react in a
  way that tends to minimize the effect of that stress.
\end{definition}

{\em Stress of a system} is a change of the circumstances surrounding
the system that disturbs the equilibrium of that system.

La Châtelier's principle predict a way to, qualitatively, predict the
direction of change of a system under an external influence or
perturbation.

\subsection{Adding and removing reactants and products of the system}

One way to add stress to a system is to add to or remove from it. This
disturbs the equilibrium.

Consider, again,
\ceeqstar{N2(g) + 3H2(g) <=> 2NH3(g)}

If we add some more hydrogen gas to this system after it has reached
equilibrium, two things will happen. The first thing is that the two
reactants will for more product. Since this requires both reactants,
thís will consume both hydrogen gas and nitrogen gas. We will thus see
a decrease in nitrogen gas as ammonia is formed. All this occurs in a
way that equilibrium is reached again, $Q = K$ again.

In the same way, if we would add some of the product to the
system. More ammonia would dissociate into reactants until the
proportions of the reactants and the product ($Q$) again mounts up to
the equilibrium constant ($K$) so that $Q = K$.

If you are in equilibrium, adding more hydrogen gas, the system will
tend to minimize the increase. Reaction shift to right ``toward
product''.

If the system is at equilibrium $Q = K$. When we add to one of the
reactants $Q$ is decreased momentarily, $Q < K$. Then \gibbs is
decreased. We remember that $\gibbs < 0$ makes a reaction spontaneous
to the right so the system start to form more product.

Conversely, if we add more ammonia to the system, $Q =
\frac{[\text{products}]}{[\text{reactants}]}$ will increase and this
makes $\gibbs > 0$. So the reaction will dissociate product into
reactants as a consequence.

\subsection{Changing the volume}

A decrease of the volume of a system increases the total pressure of
that system.

The ideal gas law jumps to mind
\begin{equation}
  PV = nRT
\end{equation}

Le Châtelier's principle predicts that the system will respond to an
increase in pressure in a way as to reduce the total pressure.

As an example, consider the reaction \ceeqstar{2P2(g) <=> P4(g)}

We could take a mixture of the gasses in a sealed container. We
assume the mix has reached equilibrium. We can then decrease the
volume, and thus increase the pressure. This would cause the
reaction to tend towards product, since there are two moles of
reactant for every mole of product and this releases pressure.

In the terms of $Q$ and $K$, suppose the volume of the container is
decreased by half (a factor of two). This will increase the partial
pressure of both gasses by a factor of two, initially. Before the
stress is applied to the system, $Q = K$. When we apply the pressure
and the partial pressures of the gasses are doubled. Let $p_r$ be the
partial pressure of the reactant at equilibrium and $p_p$ be the
partial pressure of the product at equilibrium. At equilibrium
\begin{equation*}
  Q = K = \frac{p_p}{(p_r)^2}
\end{equation*}
When we apply the stress to the system and decrease the volume of the
container $Q$ changes accordingly
\begin{equation*}
  Q = \frac{2p_p}{(2p_r)^2} = \frac{p_p}{2p_r} < \frac{p_p}{p_r} = K
\end{equation*}

So the denominator will increase more from the stress than the
nominator will. As $Q < K$ which yields $\gibbs < 0$ the reaction will
tend toward product until $Q = K$ again.

If we instead increase the volume and thus decrease the pressure
inside the container we will have the opposite effect. Let's say we
decrease the pressure inside the container by half, a factor of
two. At equilibrium
\begin{equation*}
  Q = K = \frac{p_p}{(p_r)^2}
\end{equation*}

When we decrease the total pressure of the container, we half the
partial pressures of the gasses, so
\begin{equation*}
  Q = \frac{\frac{1}{2}p_p}{(\frac{1}{2}p_r)^2}
  = \frac{p_p}{\frac{1}{2}p_r}
  = 2\frac{p_p}{p_r}
  > \frac{p_p}{p_r} = K
\end{equation*}

And similarly to last example, as $Q > K$ the reaction tends toward
reactants and dissociating product until $Q = K$ again.

We should be aware of the fact that $Q$ depends of the quotient of the
partial pressures of the reactants and the products of a reaction. If
we insert an inert gas into the container, this affects the partial
pressures equally and does not affect $Q$ and will, thus, not disturb
the equilibrium.

\begin{hfigure}
  \begin{remark}
    \textbf{Review: partial pressure}

    The partial pressure of a gas, possibly in a mixture of gasses, in a
    container is the pressure that gas would exert on the container if
    it was alone in the container.

    Let's say we have a volume of \ce{O2(g)} with $P_{\ce{O2}} =
    \SI{1}{\bar}$ and we have an equal volume of \ce{N2(g)} with
    $P_{\ce{N2}} = \SI{1}{\bar}$. If those two amounts of the gasses
    were put inside the same volume, the total pressure would be
    \SI{2}{\bar}. The partial pressure of each gas is \SI{1}{\bar}
    whether the other gas is present or not.

    We have the equation
    \begin{equation}
      P_A = n_A \frac{RT}{V}
    \end{equation}
    the partial pressure for the gas \ce{A}. $n_A$ being the number of
    moles of gas; $R, T$ and $V$ as expected.

    The total pressure of a gas mixture on it's container is the sum of
    the partial pressures of the gasses on that container.
    \begin{equation}
      P_{\text{tot}} = \sum_i \left(n_i\frac{RT}{V}\right)
      = \frac{RT}{V}\cdot\sum_i n_i
      = \frac{RT}{V} n_{\text{tot}}
    \end{equation}
  \end{remark}
  \caption{Review of the concept of partial pressure.}
\end{hfigure}

\subsection{Changing the temperature}

Raising the temperature of a mixture in equilibrium by adding heat
causes the reaction to shift in such a way that some of the heat is
absorbed. This is consistent with Le Châteliers principle. Here the
enthalpy change (\enthalpy) of a reaction is the predictive tool when
thinking about the direction of change when adding heat to a system.

Let's consider the effect of raising the temperature of an {\em
  exothermic} reaction ($\enthalpy < 0$), a reactions that releases
heat to the surroundings. Such a reaction tends to the reactants to
absorb the extra heat, it favors the forming of reactants.

For an exothermic reaction
\ceeqstar{{\text{reactants}} <=>[{heat produced}][{heat absorbed}] {\text{products}}}

If we add heat, the system will go in a direction that will absorb
that heat.

For an {\em endothermic} reaction ($\enthalpy > 0$) that absorbs heat
from the surroundings to proceed to the right, the effect of
adding heat is the opposite from the example above
\ceeqstar{{\text{reactants}} <=>[{heat absorbed}][{heat produced}] {\text{products}}}
so increasing the temperature of the system favors formation of
products.

We know from the definition of the notion of endothermic reactions
that an endothermic reaction is a reaction that {\em requires energy
  to occur} (going forward, to the right). Here we actually add that
heat.

\begin{example}
  Consider
  \ceeqstar{2SO2(g) + O2 <=> 2SO3(g)}

  Enthalpy for this reaction is $\enthalpy^0 =
  \SI{-197.78}{\kilo\joule\per\mol}$ \ce{O2}.

  If we add heat to this reaction when it has reached equilibrium,
  what direction will the reaction go?

  As $\enthalpy^0 < 0$ this is a exothermic reaction and adding heat
  will make it tend toward the reactants.
\end{example}

\subsection{The dependence of $K$ on temperature}

So far we have looked at the equilibrium constant at constant
temperatures. But $K$ actually depends on the temperature and changes
with it. Reaction rates also changes with temperature.

So what is the relation between the equilibrium constant and the
temperature? We have already seen the two relations
\begin{align*}
  \gibbs^0 &= -RT \ln K \\
  \gibbs^0 &= \enthalpy^0 - \dS T \\
\end{align*}
The two left hand sides is obviously equal so we conclude that the two
right hand sides are equal and
\begin{equation}\label{eq:lnk}
  \ln K = - \frac{\enthalpy^0}{RT} + \frac{\dS^0}{R}
\end{equation}

It is assumed that $\enthalpy^0$ and $\dS^0$ is relatively independent
of the temperature, as this changes within ``temperatures of interest
to us'', so we're safe to say that $K$ changes with a change in $T$.

Consider now a reaction carried out at two temperatures $T_1$ and
$T_2$. Plugging this in into Equation~\ref{eq:lnk}
\begin{align*}
  \ln K_1 &= -\frac{\enthalpy^0}{RT_1} + \frac{\dS}{R}\quad\text{and}\\
  \ln K_2 &= -\frac{\enthalpy^0}{RT_2} + \frac{\dS}{R}\\
\end{align*}

Subtracting $K_1$ from $K_2$ give us a measure ($\Delta K$) of the
impact a temperature change from $T_1$ to $T_2$ have on the
equilibrium constant
\begin{equation}\label{eq:van't hoff}\tag{Van't Hoff equation}
  \Delta K = \ln K_2 - \ln K_1 = \ln\left(\frac{K_2}{K_1}\right) = - \frac{\enthalpy^0}{R}~\left[\frac{1}{T_2} - \frac{1}{T_1}\right]
\end{equation}

In this section we will notice that the sign of $\Delta K$ tells us
whether $K_2 > K_1$ (less reactants, more product) or $K_2 < K_1$
(more reactants, less product) after the change in temperature.

Consider a case of an exothermic reaction, $\enthalpyO < 0$. That
means that the factor $- \frac{\enthalpy^0}{R} > 0$.

We begin with looking at the case where we are going to increase the
temperature, $T_2 > T_1$.

We have
\begin{equation*}
  \left[\frac{1}{T_2} - \frac{1}{T_1}\right] < 0
\end{equation*}
since $T_2 > T_1$ and thus
\begin{equation*}
  \ln\left(\frac{K_2}{K_1}\right) < 0
  \implies \frac{K_2}{K_1} < 1
  \implies K_2 < K_1
\end{equation*}
and this means that the quotient of product to reactants have
decreased so the reaction tends toward reactants.

In the same manner, if $\enthalpyO < 0$ (exothermic reaction) and $T_2
< T_1$ (decreased temperature), we get $K_2 > K_1$ which means more
products, The reaction tends toward products.

When we look at endothermic reactions, that just means another switch
of signs as $\enthalpy^0 > 0$ (reaction requires energy to occur). A
system of reactants that absorbs heat from the surroundings in an
endothermic reaction has a positive \enthalpy, because the enthalpy of
the products is higher than the enthalpy of the reactants of the
system. If we only look at the equation and the sign of the factors, a
positive \enthalpy means the first factor ($-\frac{\enthalpy^0}{RT}$)
is negative. Now the second factor (within brackets) is positive when
$T_2 < T_1$, a decrease in temperature and negative when $T_2 > T_1$,
a increase in temperature. We can conclude that for endothermic reactions
$\Delta K > 0$ (more products) if we decrease the temperature and
$\Delta K < 0$ (more reactants) if we increase the temperature. We can
conclude this simply by looking at the equation.

The table below
\begin{htable}
  \begin{center}
    \begin{tabularx}{.8\textwidth}{XXX}
      \toprule
      Reaction is & \specialcell{$T_2 < T_1$\\ (decrease)} & \specialcell{$T_2 > T_1$\\ (increase)} \\
      \midrule
      \specialcell{Exothermic\\ $\enthalpy^0 < 0$} &
      \specialcell{$K_2 < K_1$\\ more reactants} &
      \specialcell{$K_2 > K_1$\\ more products} \\
      \\
      \specialcell{Endothermic\\ $\enthalpy^0 > 0$} &
      \specialcell{$K_2 > K_1$\\ more products} &
      \specialcell{$K_2 < K_1$\\ more reactants} \\
      \bottomrule
    \end{tabularx}
  \end{center}
  \caption{The relationship between temperature; thermodynamics and
    the effect of temperature change.}
\end{htable}

\section{Examples of chemical equilibrium}

\subsection{Maximizing the yield of a reaction}

If we were to design an industrial process we could use our new
knowledge from the last two chapters to maximize the outcome from
certain reactions.

Consider
\ceeqstar{N2(g) + 3H2(g) <=> 2NH3(g)}

This is an exothermic reaction ($\enthalpyO < 0$), which means that
low temperature favors products, but also slows reaction rate down (as
we shall see in chapter on kinetics) so we would need to compromise
and so people do. Ammonia tends to be produces at \SI{500}{\celsius}.

In the equation of the reaction, on the stoichiometric coefficients,
that there are four moles of gas of reactants and two moles of gas of
the product. The forward reaction would be favored if we increased the
pressure within the container harboring the process.

Removing ammonia during the process would also favor forming of more
ammonia.

People are also working on designing enzymatic processes to replace
the Haber-Bosch process of high temperature and high pressure.

The enzyme {\em nitrogenase} is able to split \ce{N2} molecules into
two nitrogen atoms using transition metals. This is a hot research
topic in chemistry today.

\subsection{Oxygen and hemoglobin}

The combination of oxygen with hemoglobin (Hb) which carries oxygen
through the blood can be represented by
\ceeqstar{Hb(aq) + O2(aq) <=> HbO2(aq)}
where \ce{HbO2} is oxyhemoglobin which is oxygen bound to hemoglobin.

If we travel to higher altitudes, the partial pressure of oxygen in
the atmosphere will decrease, At an altitude of \SI{3}{\kilo\meter}
the partial pressure of oxygen is \SI{0.14}{\atm} compared to
\SI{0.2}{\atm} at sea level.

At a lower partial pressure of oxygen the reaction above will tend
towards reactants, so less hemoglobin will be bound to hemoglobin in
the body.

The body will, however, react to this new situation and start
producing more hemoglobin as this will result in more product being
formed.

\end{document}


%----------------------------------------------------------------------------------------
%	PACKAGES AND OTHER DOCUMENT CONFIGURATIONS
%----------------------------------------------------------------------------------------

\documentclass[paper=a4, fontsize=11pt]{scrartcl} % A4 paper and 11pt font size

\usepackage[T1]{fontenc} % Use 8-bit encoding that has 256 glyphs
\usepackage{fourier} % Use the Adobe Utopia font for the document - comment this line to return to the LaTeX default
\usepackage[english]{babel} % English language/hyphenation
\usepackage{amsmath,amsfonts,amsthm,amssymb} % Math packages
\usepackage{mathrsfs}

\usepackage{algorithm, algorithmic}
\renewcommand{\algorithmicrequire}{\textbf{Input:}} %Use Input in the format of Algorithm
\renewcommand{\algorithmicensure}{\textbf{Output:}} %UseOutput in the format of Algorithm

\usepackage{listings}
\lstset{language=Matlab}

\usepackage{lipsum} % Used for inserting dummy 'Lorem ipsum' text into the template

\usepackage{sectsty} % Allows customizing section commands
\allsectionsfont{\centering \normalfont\scshape} % Make all sections centered, the default font and small caps

\usepackage{fancyhdr} % Custom headers and footers
\pagestyle{fancyplain} % Makes all pages in the document conform to the custom headers and footers
\fancyhead{} % No page header - if you want one, create it in the same way as the footers below
\fancyfoot[L]{} % Empty left footer
\fancyfoot[C]{} % Empty center footer
\fancyfoot[R]{\thepage} % Page numbering for right footer
\renewcommand{\headrulewidth}{0pt} % Remove header underlines
\renewcommand{\footrulewidth}{0pt} % Remove footer underlines
\setlength{\headheight}{13.6pt} % Customize the height of the header

\numberwithin{equation}{section} % Number equations within sections (i.e. 1.1, 1.2, 2.1, 2.2 instead of 1, 2, 3, 4)
\numberwithin{figure}{section} % Number figures within sections (i.e. 1.1, 1.2, 2.1, 2.2 instead of 1, 2, 3, 4)
\numberwithin{table}{section} % Number tables within sections (i.e. 1.1, 1.2, 2.1, 2.2 instead of 1, 2, 3, 4)

\setlength\parindent{0pt} % Removes all indentation from paragraphs - comment this line for an assignment with lots of text

%----------------------------------------------------------------------------------------
%	TITLE SECTION
%----------------------------------------------------------------------------------------
\newcommand{\horrule}[1]{\rule{\linewidth}{#1}} % Create horizontal rule command with 1 argument of height

\title{
\normalfont \normalsize
\textsc{Shanghai Jiao Tong University, UM-SJTU JOINT INSTITUTE} \\ [25pt] % Your university, school and/or department name(s)
\horrule{0.5pt} \\[0.4cm] % Thin top horizontal rule
\huge Introduction to Numerical Analysis \\ HW6 \\ % The assignment title
\horrule{2pt} \\[0.5cm] % Thick bottom horizontal rule
}

\author{Yu Cang \\ 018370210001} % Your name

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

\begin{document}

\maketitle % Print the title

%----------------------------------------------------------------------------------------
%	PROBLEM 1
%----------------------------------------------------------------------------------------
\section{Richardson Extrapolation}
\begin{enumerate}
	\item
		\begin{proof}
			When $k = 0$, it's clear that $A_0(t) = a_0 + O(t)$.\\
			Assume that the statement is still valid when $k=n-1$, which means $A_{n-1} = a_0 + O(t^n)$\\
			Thus, when $k = n$, apply the induction hypothesis
			\begin{equation}
				\begin{aligned}
					A_n(t) & = \frac{r^n A_{n-1}(t) - A_{n-1}(rt)}{r^n-1}\\
					          & = \frac{r^n (a_0 + O(t^n)) - (a_0 + r^n O(t^n))}{r^n-1}\\
					          & = a_0 + \frac{r^n}{r^n-1} (O(t^n) - O(t^n))\\
					          & = a_0 + O(t^{n+1})
				\end{aligned}
			\end{equation}
			Hence, the statement is valid.
		\end{proof}
	\item
		\begin{enumerate}
			\item
				\begin{proof}
					When $t_m = \frac{t_0}{r_0^m}$, and $r_0 > 1$
					\begin{equation}
						\lim\limits_{m\rightarrow\infty} A_n(t_m) = a_0 + O((\frac{t_0}{r_0^m})^{n+1}) = a_0 + \frac{O(t_0 ^ {n+1})}{r_0^{m(n+1)}} = 0
					\end{equation}
					The equation above is valid as $O(t_0 ^{n+1})$ is finite.
			\end{proof}

			\item
				\begin{proof}
					As $O(t_0 ^{n+1})$ is finite, it's clear that
					\begin{equation}
						 A_n(t_m) = a_0 + O((\frac{t_0}{r_0^m})^{n+1}) = a_0 + O(r_0^{-m(n+1)})
					\end{equation}
				\end{proof}

		\end{enumerate}
	\item
		Algorithm used to do extrapolation is given as below.

		\begin{algorithm}
		\caption{Calculation of the extrapolation matrix and the improved quadrature}
			\begin{algorithmic}[1]
				\REQUIRE $A(t)$, $n$, $m$, $a$, $b$, $r_0$
				\ENSURE $M$, $a_0$
				\STATE $h \gets b-a$
				\STATE $i \gets 0$
				\WHILE {$i \neq m$}
					\STATE $M[i][0] \gets A(\frac{h}{2^i})$
					\STATE $i \gets i + 1$
				\ENDWHILE
				\STATE $j \gets 1$
				\WHILE {$j \neq n$}
					\STATE $i \gets j$
					\WHILE {$i \neq m$}
						\STATE $M[i][j] \gets \frac{r_0^i}{r_0^i - 1} M[i][j-1] - \frac{1}{r_0^i -1} M[i-1][j-1]$
						\STATE $i \gets i+1$
					\ENDWHILE
					\STATE $j \gets j+1$
				\ENDWHILE
				\STATE $a_0 \gets M[m-1][n-1]$
				\RETURN $M$, $a_0$
			\end{algorithmic}
		\end{algorithm}

	\item
		\begin{enumerate}
			\item
				With proper combination, lower-order terms inside original quadratures can be eliminated when generating the new quadrature.
			\item
				Consider the Runge's Function
				\begin{equation}
					f(x) = \frac{1}{1+25x^2}
				\end{equation}
				(to be added...)
		\end{enumerate}

\end{enumerate}

%----------------------------------------------------------------------------------------
%	PROBLEM 2
%----------------------------------------------------------------------------------------
\section{Integration}
\begin{enumerate}
	\item
		The number of nodes is too small s.t. the order of error is too large.
	\item
		Denote
		\begin{equation}
			E(f) = \int_{a}^{b} f(x)dx - (b-a)f(\frac{a+b}{2})
		\end{equation}
		Thus
		\begin{equation}
			E(1) = (b-a) - (b-a) = 0
		\end{equation}
		\begin{equation}
			E(x) = \frac{b^2-a^2}{2} - (b-a)\frac{b+a}{2} = 0
		\end{equation}
		and
		\begin{equation}
			E(x^2) = \frac{b^3-a^3}{3} - (b-a)(\frac{a+b}{2})^2 \neq 0
		\end{equation}
		Hence, the degree of this quadrature formula is $1$.\\
		The kernel is determined as
		\begin{equation}
			\begin{aligned}
				K_N(t) & = E(x\in [a, b] \rightarrow [(x-t)_+])\\
					   & = \int_{a}^{b} f(x)dx - (b-a)f(\frac{a+b}{2})\\
					   & = \int_{a}^{b} (x-t)_+dx - (b-a)(\frac{a+b}{2}-t)_+\\
					   & = \frac{(b-t)_+^2 + (a-t)_+^2}{2} - (b-a)(\frac{a+b}{2}-t)_+
			\end{aligned}
		\end{equation}
		Divide the whole range into 4 sub-gap as $(-\infty, a]$,$(a, \frac{a+b}{2}]$,$(\frac{a+b}{2}, b]$,$(b, +\infty)$.
		The non-negative property is seen when examine each gap.
	\item
		\begin{proof}
			As has been proved above, $k_N(t)$ is non-negative over $[a, b]$, so the first mean value theorem can be applied
			\begin{equation}
				\begin{aligned}
					E(f) & = \frac{1}{N!} \int_{a}^{b}k_N(t)f''(t)dt\\
					     & = f''(\xi) \int_{a}^{b}k_N(t)dt\\
					     & = \frac{(b-a)^3}{24} f''(\xi)
				\end{aligned}
			\end{equation}
		\end{proof}
\end{enumerate}

%----------------------------------------------------------------------------------------
%	PROBLEM 3
%----------------------------------------------------------------------------------------
\section{Gauss's method}
\begin{enumerate}
	\item
		\begin{enumerate}
			\item
				\begin{proof}
					It's clear that $w(x)$ is positive.\\
					And
					\begin{equation}
						\int_{-1}^{1} w(x) dx = \int_{-1}^{1} \sqrt{1-x^2}dx = \int_{-\pi/2}^{\pi/2} cos^2\theta d\theta = \frac{\pi}{2}
					\end{equation}
					Thus, $w(x)$ is a weight function.
				\end{proof}
			\item
				\begin{proof}
					Since
					\begin{equation}
						\begin{aligned}
							\int_{-1}^{1}w(x)q_m(x)q_n(x)dx
							& = \int_{\pi}^{0} \sqrt{1-cos^2\theta} \frac{sin(m+1)\theta}{sin\theta} \frac{sin(n+1)\theta}{sin\theta}(-sin\theta)d\theta\\
							& = \int_{0}^{\pi} sin(m+1)\theta sin(n+1)\theta d\theta
						\end{aligned}
					\end{equation}
					It's clear that when $n=m$, the value of the equation above is $\frac{\pi}{2}$, and is $0$ when $n \neq m$.
					Thus, $q_k\  (k\in \mathbb{N})$ defines a sequence of orthogonal polynomials for weight function $w(x)$.
				\end{proof}
			\item
				\begin{equation}
					p_k(x) = \sqrt{\frac{2}{\pi}} q_k(x)
				\end{equation}
		\end{enumerate}
	\item
		\begin{enumerate}
			\item
				$x_k$ are roots of orthogonal polynomial over $[a, b]$, whose order is $n+1$ and weight is $w(x)$.
			\item
				With the Lagrange interpolation function $f(x) = \sum_{i=0}^{n} l_i(x)f(x_i)$, $A_k$ can be determined as
				\begin{equation}
					A_k = \int_{-1}^{1} l_k(x)w(x)dx
				\end{equation}
				As $x_k = cos(\frac{k\pi}{n+2})$, it can be further resolved that $A_k = \frac{\pi}{n+2}sin^2\frac{(k+1)\pi}{n+2}$.
			\item
				\begin{proof}
					Apply Hermitan interpolation polynomials $H_{2n+1}(x)$ onto $f(x_k)$ s.t. for $k = 0, 1, ... , n$
					\begin{equation}
						H_{2n+1}(x_k) = f(x_k)
					\end{equation}
					and
					\begin{equation}
						H'_{2n+1}(x_k) = f'(x_k)
					\end{equation}
					Thus
					\begin{equation}
						f(x) = H_{2n+1}(x) + \frac{f^{(2n+2)}(\xi)}{(2n+2)!}\omega^2(x)
					\end{equation}
					Then integrate over $[-1, 1]$ with weight $w(x)$
					\begin{equation}
						I = \int_{-1}^{1} f(x) w(x) dx = \int_{-1}^{1} H_{2n+1}(x) w(x)dx + R_n[f]
					\end{equation}
					Thus
					\begin{equation}
						R_n[f] = I- \sum_{k=0}^{n}A_k f(x_k) = \int_{-1}^{1}\frac{f^{(2n+2)}(\xi)}{(2n+2)!}\omega^2(x)w(x)dx
					\end{equation}
					Since $\omega^2(x)w(x) \geq 0$, it can be concluded from the first intermediate value theorem that
					\begin{equation}
						R_n[f] = \frac{f^{(2n+2)}(\eta)}{(2n+2)!}\int_{-1}^{1}\omega^2(x)w(x)dx \triangleq c  \frac{f^{(2n+2)}(\eta)}{(2n+2)!}
					\end{equation}

				\end{proof}

		\end{enumerate}
\end{enumerate}

\end{document}