\input{../../utils/header.tex}

\begin{document}

\title{Machine Learning (41204-01)\\HW \#7}
\author{Will Clark and Matthew DeLio \\
\textsf{\{will.clark,mdelio\}@chicagobooth.edu} \\
University of Chicago Booth School of Business}
\date{\today}
\maketitle

\section{Zachary's Karate Club}

Zachary's Karate Club is a network describing a university karate club from the early 1970's. The network has 34 vertices and 78 edges as depicted in \cref{fig:karate_network}. The network was observed and described by Wayne W. Zachary during and after it split into two factions (led by John A and Mr. Hi, denoted with blue circles and red squares, respectively). Because we know the features of the network and the factions into which it split, it is an ideal data set on which to test out various community detection algorithms.

\begin{figure}[!htb]
\centering
\caption{Zachary's Karate Club and Factions}
\includegraphics[scale=.5,trim={0.75in 0.75in 0.75in 0.75in}, clip=True]{karate_network.pdf}
\label{fig:karate_network}
\end{figure}

We used the algorithms listed below to try and determine the underlying community structure in Zachary's karate club. The results are visualized in \cref{fig:edge_betweenness}-\cref{fig:walktrap}. The hierarchical algorithm can be cut into two groups and compared directly to the ground truth of the observed factions, but algorithms with more groups are tougher to evaluate. If an algorithm produces more than two groups, as long as the groups are contained entirely within one faction or the other, we consider there to be no mis-classifications. In the associated figures, members of Mr. H's faction are in squares and members of John A's faction are in circles; the colors are set according to algorithmically-determined groups.
\begin{itemize}
\item \textbf{Edge Betweenness}: A hierarchical algorithm that we cut to obtain two groups. The default setting is for the algorithm to consider the network edge weights, but in doing so the algorithm misclassifies two vertices (Actors 3 and 14; see \cref{fig:edge_betweenness}). By ignoring the edge weights, the algorithm only mis-classifies one vertex (Actor 3).
\item \textbf{Greedy Modularity Optimization (Fast Greedy)}: A hierarchical algorithm that we cut to obtain two groups. It correctly predicts the faction for all vertices (see \cref{fig:fast_greedy}).
\item \textbf{Infomap}: A non-hierarchical algorithm that splits the data into 3 groups. Two groups are made entirely of members from Mr. Hi's faction (see \cref{fig:infomap}).
\item \textbf{Propagating Labels}: A non-hierarchical algorithm that splits the data into 3 groups. Two groups are made entirely of members from Mr. Hi's faction. It classifies the networks into the same groups as the Infomap algorithm (see \cref{fig:label_prop}).
\item \textbf{Leading Eigenvector}: This is another hierarchical algorithm, although the basic \textsf{cutat} function produces an error when we attempt to break the network into two communities.\footnote{\textsf{Warning message: In cutat(cl, 2) : Cannot have that few communities}} As an alternative, we visualize the network as a dendrogram and use the two largest branches as our estimate of the factions. The restuls are not good; five actors in each group are mis-categorized. The results are in \cref{fig:leading_eigen}. This is far and away the worst algorithm for this data set, although it may be because of the implementation in \textsf{igraph}.
\item \textbf{Multi-level Modularity Optimization (Louvain)}: A non-hierarchical algorithm that splits the data into 4 groups. Two groups are made entirely of members from Mr. Hi's faction and two groups are made entirely of members from John A's faction. This algorithm breaks Mr. Hi's faction into the same groups that the Infomap and Propagating Labels algorithms do (see \cref{fig:louvain}).
\item \textbf{Optimal Structure}: A non-hierarchical algorithm that splits the data into 4 groups. The four groups are identical to those identified by the Multi-Level Modularity Optimization algorithm above (see \cref{fig:optimal}).
\item \textbf{Statistical Mechanics (Spinglass)}: A non-hierarchical algorithm that splits the data into 4 groups. The groups are nearly the same as those identified in the Optimal Structure and Multi-Level Modularity Optimation algorithms, except Actor 24 has switched groups within John A's faction (see \cref{fig:spinglass}).
\item \textbf{Short Random Walks (Walktrap)}: A hierarchical algorithm that we cut to obtain two groups. It correctly predicts the faction for all vertices (see \cref{fig:walktrap}).
\end{itemize}
Ultimately, all algorithms besides the Edge Betweenness and Leading Eigenvector algorithm correctly break the network into the correct groups, either the observed factions or subsets of the observed factions.

%%%%%%%%%%%%%%%%%%%% KARATE COMMUNITY GRAPHS %%%%%%%%%%%%%%%%%%%%
\begin{figure}
\centering
\begin{subfigure}[b]{0.32\textwidth}
\caption{Edge Betweenness}
\includegraphics[width=\textwidth,trim={0.75in 0.75in 0.75in 0.75in}, clip=True]{edge_betweenness.pdf}
\label{fig:edge_betweenness}
\end{subfigure}
\hfill
\begin{subfigure}[b]{0.32\textwidth}
\caption{Greedy Optimization}
\includegraphics[width=\textwidth,trim={0.75in 0.75in 0.75in 0.75in}, clip=True]{fast_greedy.pdf}
\label{fig:fast_greedy}
\end{subfigure}
\hfill
\begin{subfigure}[b]{0.32\textwidth}
\caption{Infomap}
\includegraphics[width=\textwidth,trim={0.75in 0.75in 0.75in 0.75in}, clip=True]{infomap.pdf}
\label{fig:infomap}
\end{subfigure}

\begin{subfigure}[b]{0.32\textwidth}
\caption{Propagating Labels}
\includegraphics[width=\textwidth,trim={0.75in 0.75in 0.75in 0.75in}, clip=True]{label_prop.pdf}
\label{fig:label_prop}
\end{subfigure}
\hfill
\begin{subfigure}[b]{0.32\textwidth}
\caption{Leading Eigenvector}
\includegraphics[width=\textwidth,trim={0.75in 0.75in 0.75in 0.75in}, clip=True]{leading_eigen.pdf}
\label{fig:leading_eigen}
\end{subfigure}
\hfill
\begin{subfigure}[b]{0.32\textwidth}
\caption{Multi-level Optimization}
\includegraphics[width=\textwidth,trim={0.75in 0.75in 0.75in 0.75in}, clip=True]{louvain.pdf}
\label{fig:louvain}
\end{subfigure}

\begin{subfigure}[b]{0.32\textwidth}
\caption{Optimal Structure}
\includegraphics[width=\textwidth,trim={0.75in 0.75in 0.75in 0.75in}, clip=True]{optimal.pdf}
\label{fig:optimal}
\end{subfigure}
\hfill
\begin{subfigure}[b]{0.32\textwidth}
\caption{Statistical Mechanics}
\includegraphics[width=\textwidth,trim={0.75in 0.75in 0.75in 0.75in}, clip=True]{spinglass.pdf}
\label{fig:spinglass}
\end{subfigure}
\hfill
\begin{subfigure}[b]{0.32\textwidth}
\caption{Short Random Walks}
\includegraphics[width=\textwidth,trim={0.75in 0.75in 0.75in 0.75in}, clip=True]{walktrap.pdf}
\label{fig:walktrap}
\end{subfigure}
\caption{Community Detection Algorithms for Zachary's Karate Club}
\end{figure}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Wikipedia}
\subsection{Clustering}
Due to the size of and the time required to cluster our data-set, we apply only one of the better performing algorithms from the previous section.  Of two that correctly partitioned the Karate data in the previous section (\textbf{Greedy} and \textbf{Short Random Walk}) we choose to apply \textbf{Short Random Walk} here.

Before looking at the results of the clustering algorithm, we first look at node connectedness to see if we can learn anything we can learn from these data.  \Cref{fig:wiki_conn} shows that many articles have a few connections and few nodes have many connections with a, roughly, linear decline between these two extremes in log-log space.  Because of the large number of poorly connected articles, we'd expect the clustering algorithm to generate a large number of small clusters as it struggles to find common groups for these articles.  Likewise we expect that the well-connected clusters should be easily identifiable and clustered together.  \Cref{fig:wiki_size} shows a histogram of the cluster-size; as we predict we do see a large number of small clusters.

\begin{figure}[!htb]
  \centering
  \begin{subfigure}[b]{0.49\textwidth}
    \caption{Vertex Connectedness}
    \includegraphics[width=\textwidth]{wiki_edge_hist.pdf}
    \label{fig:wiki_conn}
  \end{subfigure}
  \hfill
  \begin{subfigure}[b]{0.49\textwidth}
    \caption{Cluster-Size}
    \includegraphics[width=\textwidth]{wiki_cl_hist.pdf}
    \label{fig:wiki_size}
  \end{subfigure}
  \caption{Cluster and Node Histograms}
\end{figure}

We examine the largest 4 clusters to see how well a sample of the topics (see \cref{tab:wiki_topics}).  Very roughly, it appears that the topics are clusters as follows:
\begin{itemize}
\item \textbf{\#5} Mathematics (Number Theory)
\item \textbf{\#11} Physics / Politics - This cluster appears to be fairly poorly formed
\item \textbf{\#19} Chemistry
\item \textbf{\#20} Telecommunications
\end{itemize}

While not perfect, it appears as though the random walk clustering algorithm does a decent job finding relationships between articles within the graph network.  While difficult to visualize, \cref{fig:wiki_clust} attempts to show the largest 8 clusters along with a couple of the most connected-topics selected from each.\footnote{This was done in an attempt to separate, spatially, the article labels}

\begin{figure}[!htb]
  \centering
  \caption{Visual Representation of the Largest 8 Clusters}
  \includegraphics[width=\textwidth]{wiki_clust.pdf}
  \label{fig:wiki_clust}
\end{figure}

\input{wiki_topics.tex}

\begin{appendices}

\end{appendices}

\end{document}

% \input{.tex}

% \begin{figure}[!htb]
%   \centering
%   \begin{subfigure}[b]{0.49\textwidth}
%     \caption{}
%     \includegraphics[width=\textwidth]{.pdf}
%     \label{fig:}
%   \end{subfigure}
%   \hfill
%   \begin{subfigure}[b]{0.49\textwidth}
%     \caption{}
%     \includegraphics[width=\textwidth]{.pdf}
%     \label{fig:}
%   \end{subfigure}
%   \caption{}
% \end{figure}

% \begin{figure}[!htb]
%   \centering
%   \caption{}
%   \includegraphics[scale=.5]{.pdf}
%   \label{fig:}
% \end{figure}


\newpage{}

\hypertarget{quality-assurance}{%
\section{Quality Assurance}\label{quality-assurance}}

Now if we create algorithms we need to say how good they are. For that
we are using the following methods.

\hypertarget{the-graphical-approach}{%
\subsection{The graphical approach}\label{the-graphical-approach}}

One easy way would be to draw a graph which shows the predictions and
the real values for time spent, while each task is understood as a
category of its own.

We sort that graph by actual duration so we should see the distribution
of durations and around that a hopping range of dots that describes what
the algorithm tells us.

A better algorithm should be closer to the real data. Any algorithm
should never match perfectly, as then we would have a 1:1 mapping. And
that is an over-fit for sure.

\hypertarget{mean-squared-error}{%
\subsection{Mean squared error}\label{mean-squared-error}}

The mean squared error is a common approach to calculate a value for the
quality of an algorithmi. It gets bigger with every estimate we did
wrong.

The formula can be described as:

For every value you predict:

\begin{itemize}
\tightlist
\item
  Calculate the difference between the predicted value and the real
  value
\item
  Sum the squares of each difference
\item
  Divide all the sum by the count of the data entries you check
\end{itemize}

Now, since we want to prevent overfitting we need to prevent
underfitting as well. Since we are talking about seconds and most of the
recorded tasks have a duration in the range of up to 50000 seconds that
means that most tasks are completed in about 13,89 hours. So what about
an error margin of about 5 hours. Which means just as something to think
of, we want the squared error to not exceed squared(5x60x60) =
324.000.000 .

\hypertarget{above-and-below}{%
\subsection{Above and below}\label{above-and-below}}

As a third criteria we have the idea that estimations might even each
other out. In a prefect scenario this would mean that 50\% of the
estimations are too high while the other 50\% are too low. To find out
how good we match we add 1 to a variable for every estimation we find
above the real value and then divide it by the number of tasks. The
result should be .5 when hitting the target.

\hypertarget{the-data}{%
\subsection{The Data}\label{the-data}}

For training and estimating the quality of algorithms I use herein a
dataset that consists of all the tasks that we at the software
development shop at my employer recorded during the time between july
and december 2020 and january 2021 to august 2021. That means there are
two datasets available.

Unfortunately - since this is confidential information - I cannot
publish it alongside this material. But the errors and images can be
shared at it can advance the algorithms - and it is everything that I
have right now. So that will do.

I'll call them: swe2020 and swe2021.

Now let us get a glance at the data as well the first algorithm.


\documentclass[]{article}
\usepackage{amsmath}
\usepackage{xcolor}
\newtheorem{Def}{Definition}
%opening
\title{MTH 343 Numerical Analysis Lecture 2: Review of Computer Arithmetic}
\author{Sheikh Abdul Raheem Ali}

\begin{document}

\maketitle

\section*{Remarks}

\begin{enumerate}
	\item Numerical Analysis requires such tedious \& repetitive operations that only a computer can perform quickly \& without and mistakes.
	\item Computers are dumb and must be given complete instructions of every step. Programs can be written in any language you like.
	\item Writing code is not very important because extensive commercial software packages are available.
		\begin{enumerate}
			\item IMSL: International Mathematics \& Statistics Library
			\item NAG: Numerical Algorithm Group
			\item LAPACK: Linear Algebra package
		\end{enumerate}
		Alternatives: Computer Algebra Systems
		\begin{enumerate}
			\item Mathematica
			\item Maple
			\item MATLAB
		\end{enumerate}

\end{enumerate}

\section*{Floating-Point Arithmetic}

In computers, numbers are stored as floating point quantities in the general form:
\[ \pm \cdot (d_1 d_2 d_3 \ldots d_p) \cdot \beta^e, \] where
$ p $ = precision, the number of significant bits (digits), $ e $ = an integer exponent ranging from $ E_{min} $ to $ E_{max} $, $ \beta $ = the number base, normally 2, 10, 16,
$ d_i: $ ranges from 0 to $ \beta - 1 $, and $ d_1 d_2 d_3 \ldots d_p $ is called the fractional part (mantissa).

Sometimes numbers are normalized: $ 0.023 -> 0.23 \cdot 10^{-1} $

Let us examine the case $ \beta = 10 $ (Decimal)

\begin{eqnarray*}
	3216 &=& 3\cdot10^3 + 2\cdot10^2 + 1\cdot10^1 + 6\cdot10^0\\
	&=& 10^4(3\cdot10^{-1} + 2\cdot10^{-2} + 1\cdot10^{-3} + 6\cdot10^{-4}) \\
	&=& (.3216)\cdot10^4
\end{eqnarray*}

Now let us examine the case $ \beta = 2 $ (Binary)

\begin{eqnarray*}
65 &=& 2^6 + 2^0 \\
      &=& 2^7(2^{-1}) + 2^{-7} \\
 &=&(.1000001)_2 \cdot 2^7
\end{eqnarray*}

\begin{eqnarray*}
	23 &=& 2^4 + 2^3 + 2^2 \\
	&=& 2^5(2^{-1} + 2^{-2} + 2^{-3}) \\
	&=& (.111)_2 \cdot 2^5
\end{eqnarray*}

\begin{eqnarray*}
	  85 &=& 2^6 + 2^4 + 2^2 + 2^0 \\
	&=& 2^7(2^{-1} + 2^{-3} + 2^{-6} + 2^{-7}) \\
	&=& (.1010011)_2 \cdot 2^7
\end{eqnarray*}

\begin{eqnarray*}
	5.75 &=& 2^2 + 2^0 + 2^{-1} + 2^{-2} \\
	&=& 2^3(2^{-1} + 2^{-3} + 2^{-4} + 2^{5}) \\
	&=& (.10111)_2 \cdot 2^3
\end{eqnarray*}

\[ 0.6 = (.1001100110011001\ldots)_2 \]

This last example shows us a conversion error: the decimal is recurring, but since the computer only has a finite number of bits, the value is truncated at some point.

\begin{Def}[Round off error:]
The error that is produced when a computer is used to perform real-number calculations is called round-off error.
\end{Def}

There are two ways of truncating the mantissa:

\begin{enumerate}
	\item Chopping
	\item Rounding
\end{enumerate}

Ex. $ 13.76573 = .1376 {\bf{\color{red} |}} 573 \cdot 10^2 $

4 digits chopping: $ .1376 \cdot 10^2 $
4 digits rounding: $ .1377 \cdot 10^2 $

Numbers are \textbf{rounded} when stored in the floating point format.

\end{document}


%% LyX 1.5.6 created this file.  For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\documentclass[english,preprint]{revtex4}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{graphicx}
\usepackage{babel}

\begin{document}

\title{MATLAB version of the APBS}

\date{\today}

\begin{abstract}
This is the first version. This solver uses the biconjugate gradient
stabilized method and the inexact LU decomposition to numerically
solve the linearized PB equation on the finest (target) 3D-grid. This
version requires the shifted dielectric and the ion accessibility
coefficient (kappa function) maps as generated by the APBS code as
well as the corresponding pqr file generated by the pdb2pqr code.
It uses standard three-linear splines (spl0) to spread the charge
density along the nearest grid points if needed. The resulting electrostatic
potential and charge maps are saved in dx format. For visualization
purpose, this code also generates two files (.fig and .tiff) corresponding
to the graphical representation of the electrostatic potential surface.
\end{abstract}
\maketitle

\subsection*{Description}

This code is based on Michel Holst's thesis and Nathan Baker's APBS
approach. The box-method is used to discretize the following (linearized)
PB equation

\begin{equation}
-\nabla.\left(\epsilon\left(\mathbf{r}\right)\nabla u\left(\mathbf{r}\right)\right)+\bar{\kappa}\left(\mathbf{r}\right)u\left(\mathbf{r}\right)=magic\sum_{i=1}^{N}z_{i}\delta\left(\mathbf{r}-\mathbf{r}_{i}\right)\label{eq:one}\end{equation}

where $u\left(\mathbf{r}\right)=e_{c}\Phi\left(\mathbf{r}\right)/K_{B}T$
and $magic=4\pi e_{c}^{2}/K_{B}T.$ For a diagonal dielectric tensor,
the resulting discretized linear PB equations at the nodes $u_{ijk}=u\left(x_{i},y_{j},z_{k}\right)$
for $1\leq i\leq N_{x}$, $1\leq j\leq N_{y}$ and $1\leq k\leq N_{z}$
reads

\[
\left[\epsilon_{i-1/2,j,k}^{x}\frac{\left(h_{j-1}+h_{j}\right)\left(h_{k-1}+h_{k}\right)}{4h_{i-1}}+\epsilon_{i+1/2,j,k}^{x}\frac{\left(h_{j-1}+h_{j}\right)\left(h_{k-1}+h_{k}\right)}{4h_{i}}+\right.\]

\[
\epsilon_{i,j-1/2,k}^{y}\frac{\left(h_{i-1}+h_{i}\right)\left(h_{k-1}+h_{k}\right)}{4h_{j-1}}+\epsilon_{i,j+1/2,k}^{y}\frac{\left(h_{i-1}+h_{i}\right)\left(h_{k-1}+h_{k}\right)}{4h_{j}}+\]

\[
\epsilon_{i,j,k-1/2}^{k}\frac{\left(h_{i-1}+h_{i}\right)\left(h_{j-1}+h_{j}\right)}{4h_{k-1}}+\epsilon_{i,j,k+1/2}^{k}\frac{\left(h_{i-1}+h_{i}\right)\left(h_{j-1}+h_{j}\right)}{4h_{k}}+\]

\[
\left.\kappa_{ijk}\frac{\left(h_{i-1}+h_{i}\right)\left(h_{j-1}+h_{j}\right)\left(h_{k-1}+h_{k}\right)}{8}\right]u_{ijk}+\]

\[
\left[-\epsilon_{i-1/2,j,k}^{x}\frac{\left(h_{j-1}+h_{j}\right)\left(h_{k-1}+h_{k}\right)}{4h_{i-1}}\right]u_{i-1jk}+\left[-\epsilon_{i+1/2,j,k}^{x}\frac{\left(h_{j-1}+h_{j}\right)\left(h_{k-1}+h_{k}\right)}{4h_{i}}\right]u_{i+1jk}+\]

\[
\left[-\epsilon_{i,j-1/2,k}^{y}\frac{\left(h_{i-1}+h_{i}\right)\left(h_{k-1}+h_{k}\right)}{4h_{j-1}}\right]u_{ij-1k}+\left[-\epsilon_{i,j+1/2,k}^{y}\frac{\left(h_{i-1}+h_{i}\right)\left(h_{k-1}+h_{k}\right)}{4h_{j}}\right]u_{ij+1k}+\]

\[
\left[-\epsilon_{i,j,k-1/2}^{k}\frac{\left(h_{i-1}+h_{i}\right)\left(h_{j-1}+h_{j}\right)}{4h_{k-1}}\right]u_{ijk-1}+\left[-\epsilon_{i,j,k+1/2}^{k}\frac{\left(h_{i-1}+h_{i}\right)\left(h_{j-1}+h_{j}\right)}{4h_{k}}\right]u_{ijk+1}=\]

\begin{equation}
magic\frac{\left(h_{i-1}+h_{i}\right)\left(h_{j-1}+h_{j}\right)\left(h_{k-1}+h_{k}\right)}{8}f_{ijk}\label{eq:two}\end{equation}

in which

\[
h_{i}=x_{i+1}-x_{i},\: h_{j}=y_{j+1}-y_{j}\: h_{k}=z_{k+1}-z_{k}\]

The delta functions appearing in the right hand side of the starting
equations are approximated with linear B-splines (spl0) which spread
the point like charge along the nearest neighborhood. The resulting
$f_{ijk}$ represent the smearing of the point charges along the grid
points.

For more details, including used unit system, please refer to the
Michel Holst's thesis and the APBS user guide online. To visualize
more clearly the problem, let's explicitly write the first equations
for a cubic grid of 5x5x5 containing general coefficients

\[
a_{222}u_{222}+a_{122}u_{122}+a_{322}u_{322}+a_{212}u_{212}+a_{232}u_{232}+a_{221}u_{221}+a_{223}u_{223}=f_{222}\]

\[
a_{322}u_{322}+a_{222}u_{222}+a_{422}u_{422}+a_{312}u_{312}+a_{332}u_{332}+a_{321}u_{321}+a_{323}u_{323}=f_{322}\]

\[
a_{422}u_{422}+a_{122}u_{122}+a_{322}u_{322}+a_{212}u_{212}+a_{232}u_{232}+a_{221}u_{221}+a_{223}u_{223}=f_{422}\]

\[
a_{232}u_{232}+a_{132}u_{132}+a_{332}u_{332}+a_{222}u_{222}+a_{242}u_{242}+a_{231}u_{231}+a_{233}u_{233}=f_{232}\]

\[
\ldots\]

in which the nodes are arranged using the natural ordering

\[
U=[u_{111},u_{211},..,u_{N_{x}11,}u_{121},..,u_{221},u_{321},..,u_{N_{x}21}...,u_{N_{x}N_{y}N_{z}}]^{T}\]

Note that the prescribed values of nodes $u_{1jk},u_{N_{x},j,k},u_{i,1,k},u_{i,N_{y},k},u_{ij1}$
and $u_{ijN_{z}}$ along the faces of the box coming from the Dirichlet
boundary conditions will have their corresponding elements removed
in such a way that only equations for the interior nodes remain. In
other words, we will only consider the following set of unknown nodes

\[
U=[u_{222},u_{322},..,u_{N_{x}-1,22,}u_{232},..,u_{332},u_{432},..,u_{N_{x}-2,32}...,u_{N_{x}-1,N_{y}-1,N_{z}-1}]^{T}\]

in such a way that the previous equations become

\[
a_{222}u_{222}+a_{322}u_{322}+a_{232}u_{232}+a_{223}u_{223}=f_{222}-a_{122}u_{122}-a_{212}u_{212}-a_{221}u_{221}\equiv b_{222}\]

\[
a_{322}u_{322}+a_{222}u_{222}+a_{422}u_{422}+a_{332}u_{332}+a_{323}u_{323}=f_{322}-a_{312}u_{312}-a_{321}u_{321}\equiv b_{322}\]

\[
a_{422}u_{422}+a_{322}u_{322}+a_{232}u_{232}+a_{223}u_{223}=f_{422}-a_{122}u_{122}-a_{212}u_{212}-a_{221}u_{221}\equiv b_{422}\]

\[
a_{232}u_{232}+a_{332}u_{332}+a_{222}u_{222}+a_{242}u_{242}+a_{233}u_{233}=f_{232}-a_{132}u_{132}-a_{231}u_{231}\equiv b_{232}\]

in which the boundary $u's$ are conveniently brought to the right-hand-side
of the equations. The resulting left-hand side equations can be written
in compact form in term of matrix vector product as follows

\[
Au=b\]

in which

\[
u\left(p\right)=u_{ijk},\qquad b\left(p\right)=b_{ijk},\qquad p=(k-2)(N_{x}-2)(N_{y}-2)+(j-2)(N_{x}-2)+i-1\]

\[
i=2,..,N_{x}-2,\quad j=2,..,N_{y}-2,\quad k=2,..,N_{z}-2\]

%
\begin{figure}
\includegraphics[scale=0.75]{Amatrix}

\caption{A Matrix representation}

\end{figure}

and $A$ is a (seven banded block tri-diagonal form) $(N_{x}-2)(N_{y}-2)(N_{z}-2)$
by $(N_{x}-2)(N_{y}-2)(N_{z}-2)$ squared symmetric positive definite
matrix containing the following nonzero elements (see figure 1):

\begin{itemize}
\item The main diagonal elements
\end{itemize}
\[
d_{0}(p)=\left[\epsilon_{i-1/2,j,k}^{x}\frac{\left(h_{j-1}+h_{j}\right)\left(h_{k-1}+h_{k}\right)}{4h_{i-1}}+\epsilon_{i+1/2,j,k}^{x}\frac{\left(h_{j-1}+h_{j}\right)\left(h_{k-1}+h_{k}\right)}{4h_{i}}+\right.\]

\[
\epsilon_{i,j-1/2,k}^{y}\frac{\left(h_{i-1}+h_{i}\right)\left(h_{k-1}+h_{k}\right)}{4h_{j-1}}+\epsilon_{i,j+1/2,k}^{y}\frac{\left(h_{i-1}+h_{i}\right)\left(h_{k-1}+h_{k}\right)}{4h_{j}}+\]

\[
\epsilon_{i,j,k-1/2}^{k}\frac{\left(h_{i-1}+h_{i}\right)\left(h_{j-1}+h_{j}\right)}{4h_{k-1}}+\epsilon_{i,j,k+1/2}^{k}\frac{\left(h_{i-1}+h_{i}\right)\left(h_{j-1}+h_{j}\right)}{4h_{k}}+\]

\begin{equation}
\left.\kappa_{ijk}\frac{\left(h_{i-1}+h_{i}\right)\left(h_{j-1}+h_{j}\right)\left(h_{k-1}+h_{k}\right)}{8}\right]\label{eq:three}\end{equation}

\begin{itemize}
\item The Next upper band diagonal, which is shifted in one column to the
left from the first column, contains the following elements
\end{itemize}
\begin{equation}
\left[d_{1}(p)=-\epsilon_{i+1/2,j,k}^{x}\frac{\left(h_{j-1}+h_{j}\right)\left(h_{k-1}+h_{k}\right)}{4h_{i}}\right]\label{eq:four}\end{equation}

\begin{itemize}
\item The second upper band diagonal which is shifted $N_{x}-2$ columns
from the first column
\end{itemize}
\begin{equation}
d_{2}(p)=\left[-\epsilon_{i,j+1/2,k}^{y}\frac{\left(h_{i-1}+h_{i}\right)\left(h_{k-1}+h_{k}\right)}{4h_{j}}\right]\label{eq:five}\end{equation}

\begin{itemize}
\item The third upper band diagonal which is shifted $(N_{x}-2)(N_{y}-2)$
columns from the first column
\end{itemize}
\begin{equation}
d_{3}(p)=\left[-\epsilon_{i,j,k+1/2}^{k}\frac{\left(h_{i-1}+h_{i}\right)\left(h_{j-1}+h_{j}\right)}{4h_{k}}\right]\label{eq:six}\end{equation}

The remaining elements of the upper triangular squared matrix A are
set equal to zero. By symmetry we obtain the lower triagonal elements
of the matrix A. Because the matrix A is sparse and large, we can
implement efficient methods that optimally solve the linear system
for U. Specifically, we use the biconjugate gradient stabilized method
combined with the inexact LU decomposition of the matrix A. Having
the numerical values for the nodes in the interior of the box, we
finally add the previously removed prescribed values along the six
faces to get the solution over the complete set of grid points.

\subsection*{Computational algorithm}

\begin{enumerate}
\item We read the input file .inm to get the APBS input files (shifted dielectric
coefficients, kappa function and pqr data file) as well as the number
of grid points, the box Lengths, and temperature.
\item By using linear B-splines, we discretize the charge density to get
$f_{ijk}$ for $i=1,..,N_{x},\quad j=1,..,N_{y},\quad k=1,..,N_{z}$.
We also calculate the Dirichlet boundary condition along the six faces
of the box $u_{1jk},u_{N_{x},j,k},u_{i,1,k},u_{i,N_{y},k},u_{ij1}$
and $u_{ijN_{z}}$ using the temperature, the value of the bulk dielectric
coefficient (usually water) and ionic strength.
\item By using the expressions (\ref{eq:three}), (\ref{eq:four}),(\ref{eq:five}),
and (\ref{eq:six}), we evaluate the nonzero components of the matrix
$A$, e.g., the diagonal elements $d_{0}(p),d_{1}(p),d_{2}(p),$ and
$d_{3}(p),$ for $p=(k-2)(N_{x}-2)(N_{y}-2)+(j-2)(N_{x}-2)+i-1$ and
$i=2,..,N_{x}-1,\quad j=2,..,N_{y}-1,\quad k=2,..,N_{z}-1$. The values
for the shifted dielectric coefficients and kappa function elements
are obtained from the APBS input files. The values of the mesh size
$h_{i},h_{j}$ and $h_{k}$ are obtained from the number of grid points
and the Length of the box. Next, we built the sparse upper triangular
matrix A by filling with zeros the remaining elements of the matrix
A. Next, we obtain the lower triangular elements of the matrix A by
using the following symmetry property $A_{pq}=A_{qp}$ for $q=1,..,(N_{x}-2)(N_{y}-2)(N_{z}-2)$
and $p=q,..,(N_{x}-2)(N_{y}-2)(N_{z}-2)$.
\item By using the values obtained for the discretized charge density $f_{ijk}$
and the values of the Dirichlet boundary elements multiplied by the
appropriate shifted dielectric coefficient values, we evaluate the
elements of $b_{ijk}$. We use the natural ordering $p=(k-2)(N_{x}-2)(N_{y}-2)+(j-2)(N_{x}-2)+i-1$
and $i=2,..,N_{x}-1,\quad j=2,..,N_{y}-1,\quad k=2,..,N_{z}-1$ to
construct the corresponding vector $b(p)$ (one index) from the data
array structure (three indices) $b_{ijk}$.
\item We use the inexact $LU$ decomposition of the matrix $A$. The default
tolerance value is set equal to 0.25 which provides a fast evaluation
of the matrices $L$ and $U$.
\item The resulting $L$ and $U$ matrices, the matrix $A$ and the vector
$b$ are used to approximately solve $Au=b$ for the vector $u$ using
the biconjugate gradient stabilized method. The default accuracy is
set equal to 10\textasciicircum{}-9 and the maximum number of iteration
equal to 800.
\item We use the natural ordering relationship to convert the resulting
vector $u(p)$ to data array structure to get the numerical solution
for $u_{ijk}$ for $i=2,..,N_{x}-1,\quad j=2,..,N_{y}-1,\quad k=2,..,N_{z}-1$.
\item Finally we add the previously removed values of the nodes at the faces
of the box to obtain the solution for the nodes $u_{ijk}$ over the
complete set of grid points, namely for $i=1,..,N_{x},\quad j=1,..,N_{y},\quad k=1,..,N_{z}$.
\item The electrostatic potential $u_{ijk}$ and the charge $f_{ijk}$ maps
are saved in dx format files.
\item The electrostatic potential surface $u_{ij(N_{z}+1)/2}$ is saved
in tiff and fig format files for visualization purpose.
\end{enumerate}

\end{document}


\documentclass{article}
\usepackage{amsmath,amssymb}
\usepackage{breqn}
\usepackage{graphicx}
\usepackage[left=1in,right=1in,top=1in,bottom=1in]{geometry}
\usepackage{cleveref}

\makeatletter
\newcommand*{\declarecommand}{%
  \@star@or@long\declare@command
}
\newcommand*{\declare@command}[1]{%
  \provide@command{#1}{}%
  \renew@command{#1}%
}
\makeatother

\declarecommand{\x}{{\mathbf{x}}}
\declarecommand{\p}{{\mathbf{p}}}

\declarecommand{\u}{{\mathbf{u}}}
\declarecommand{\U}{{\mathbf{U}}}
\declarecommand{\grad}{\nabla}
\declarecommand{\Wi}{\textnormal{Wi}}
\declarecommand{\Id}{\mathbb{I}}
\declarecommand{\f}{{\mathbf{f}}}
\declarecommand{\F}{{\mathbf{F}}}
\declarecommand{\n}{{\mathbf{n}}}
\declarecommand{\X}{{\mathbf{X}}}
\declarecommand{\b}{{\mathbf{b}}}
\declarecommand{\a}{{\mathbf{a}}}
\declarecommand{\bmu}{{\boldsymbol{\mu}}}
\declarecommand{\tS}{{\tilde S}}

\begin{document}

\section{The Bingham closure}

The Bingham closure is given by:
\begin{equation}
    S(\x) = \int\psi_B(\x,\p)\p\p\p\p\,d\p,
\end{equation}
where the Bingham distribution $\psi_B(\x,\p)$ is defined by the constraints that:
\begin{subequations}
    \begin{align}
        \int\psi_B(\x,\p)\,d\p      &= \phi(x),  \\
        \int\psi_B(\x,\p)\p\p\,d\p  &= D(\x),
    \end{align}
\end{subequations}
where $\phi$ and $D$ are the zeroth and second moments with respect to the true distribution function $\psi$. We assume that $\psi_B(\x,\p)$ takes the form:
\begin{align}
    \psi_B(\x,\p) = Ae^{B:\p\p}.
\end{align}
Given $\phi(\x)$ and $D(\x)$, our goals is to find the coefficients $A$ and $B$. Because $B$ only appears contracted against a symmetric matrix, it is sufficient to assume that $B$ is symmetric. In fact, we will see that our goal is even simpler than this: we will only be interested in computing $S:E$ and $S:D$, where $E=\grad\u+\grad\u^\intercal$. The purpose of this package is to provide optimized routines for computing these contractions. This documentation describes how the package works.

\section{The Bingham Closure in 2D}

\subsection{A simple formula for the closure}

From Chaubal and Leal, $B$ and $D$ are diagonalized in the same frame. We assume that we are in this frame; and compute the closure here. We will clean up details afterwards. In this frame, we have that:
\begin{subequations}
    \begin{align}
        1 &= \int_0^{2\pi}Ae^{\lambda_0\cos^2\theta + \lambda_1\sin^2\theta}\,d\theta,   \\
        \mu_0 &= \int_0^{2\pi}Ae^{\lambda_0\cos^2\theta + \lambda_1\sin^2\theta}\cos^2\theta\,d\theta,   \\
        \mu_1 &= \int_0^{2\pi}Ae^{\lambda_0\cos^2\theta + \lambda_1\sin^2\theta}\sin^2\theta\,d\theta,
    \end{align}
\end{subequations}
Note we have assumed here that $\phi=1$. $\mu$ and $\lambda$ are the eigenvalues of $D$ and $B$ respectively, with $\mu_0>\mu_1$. We note that $\lambda$ can only be fixed up to an additive constant: to see this, let $\lambda_0$ and $\lambda_1$ solve the above equations.  Then letting $\tilde\lambda_i=\lambda_i+C$ for $i=0,1$, we have:
\begin{equation}
    \int_0^{2\pi}Ae^{\tilde\lambda_0\cos^2\theta + \tilde\lambda_0\sin^2\theta}f(\theta)\,d\theta = \int_0^{2\pi}Ae^{C}e^{\lambda_0\cos^2\theta + \lambda_1\sin^2\theta}f(\theta)\,d\theta,
\end{equation}
which simply changes the definition of $A$. We can choose a convenient choice of $C$ then; it is convenient to choose $C$ so that $\lambda_0 + \lambda_1 = 0$. Then we have that:
\begin{subequations}
    \begin{align}
        1 &= \int_0^{2\pi}Ae^{\lambda_0(\cos^2\theta - \sin^2\theta)}\,d\theta,   \\
        \mu_0 &= \int_0^{2\pi}Ae^{\lambda_0(\cos^2\theta - \sin^2\theta)}\cos^2\theta\,d\theta.
    \end{align}
\end{subequations}
Exploiting the trig identity that $\cos^2\theta-\sin^2\theta=\cos(2\theta)$, and because:
\begin{equation}
    \int_0^{2\pi}e^{\lambda_0\cos(2\theta)}\,d\theta = 2\pi I_0(\lambda_0),
\end{equation}
we find that
\begin{equation}
    A = \frac{1}{2\pi I_0(\lambda_0)},
\end{equation}
where $I_v$ is the modified Bessel function of the first kind. Thus we find:
Now we're down to the single equation:
\begin{equation}
    \mu_0 = \frac{1}{2\pi I_0(\lambda_0)}\int_0^{2\pi}e^{\lambda_0\cos(2\theta)}\cos^2\theta\,d\theta.
\end{equation}
Evaluating this final integral and simplifying gives:
\begin{equation}
    2\mu_0 = 1 + \zeta(\lambda_0),
\end{equation}
where we have defined $\zeta(x)=I_1(x)/I_0(x)$. Thus to find $\lambda_0$ given $\mu_0$, we simply need to solve this nonlinear equation for $\lambda_0$.

\subsection{Solution of the closure equation and numerical issues}

In this section we consider the issues with solving the nonlinear equation:
\begin{equation}
    2\mu = 1 + \zeta(\lambda)
    \label{eqn:bingham_nonlinear}
\end{equation}
for $\lambda$, given $\mu\in[0.5,1.0]$. Note that $\mu_0$, the largest eigenvalue, must live in this range becuase it is the largest eigenvalue and the eigenvalues sum to $1$. The fundamental problem with simply throwing a naive Newton solver at this equation is that as $\mu\to1$, $\lambda\to\infty$. While not a problem in and of itself, $\zeta(\lambda)$ is the ratio of $I_1(\lambda)$ and $I_0(\lambda)$. Both of these functions diverge exponentially fast as $\lambda$ gets large, and so naive evaluation of the ratio fails. Nevertheless, their ratio converges to $1$: our primary challenge is to find a way to evaluate $\zeta$ stably for large arguments. Fortunately, we are in luck! As it turns out, we can write:
\begin{equation}
    I_\nu(z) = \frac{e^z}{\sqrt{2\pi z}}\mathcal{P}_\nu(z),
\end{equation}
where $\mathcal{P}_\nu(z)$ is a power series in $z^{-1}$ that converges for sufficiently large $z$. Our strategy is clear, then. For small arguments, we can evaluate $\zeta$ directly. For larger arguments, we compute $\zeta$ by:
\begin{equation}
    \zeta(\lambda) = \mathcal{P}_1(\lambda)/\mathcal{P}_0(\lambda).
\end{equation}
In my numerical experiments, the power series $\mathcal{P}_0$ and $\mathcal{P}_1$ converge rapidly when the argument is at least $20$, and direct evaluation has no issues for this size argument. We thus evaluate $\zeta$ directly for $|\lambda|\leq20$, and indirectly via the power series representations for $|\lambda|>20$.

In order to find $\lambda$, we compute the solution to the equation $1/2 + \zeta(\lambda)/2 - \mu = 0$. The Jacobian is given by:
\begin{equation}
    2\mathcal{J}(\lambda) = 1 - \zeta(\lambda)/\lambda - \zeta(\lambda)^2.
\end{equation}
Fortunately, the singularity in $\zeta(\lambda)/\lambda$ is removable. We construct a function to evaluate this quantity using the function\_generator package, on an approximation interval of $[ -1.001, 1.0]$. These bounds are chosen so that evaluation points for the Chebyshev interpolants used by the function\_generator do not live at the singularity. When $|\lambda|>1$, we evaluate this quantity direclty.

\subsection{Fast evaluation of the closure equation}

This provides a stable way to compute $\lambda(\mu)$ for every value of $\mu\in[0.5, 1.0]$, but it requires a Newton iteration for every value of $\mu$ given. Instead, we might consider constructing an interpolant for this function. Unfortunately as mentioned before, as $\mu\to1$, $\lambda\to\infty$. Because the function\_generator package allows for adaptive, brute force interpolation, it could probably handle this. But we can be smarter. Note that what we actually want to calculate is:
\begin{equation}
    S(\x) = \psi_B(\x,\p)\p\p\p\p\,d\p.
\end{equation}
By exploiting identities, as we will show momentarily, we can reduce computing all of this to computing:
\begin{equation}
    S_{0000}(\x) = \int_0^{2\pi}Ae^{\lambda(\x)\cos(2\theta)}\cos^4\theta\,d\theta.
\end{equation}
Again, this function can be computed analytically, and dropping the $\x$, the result is:
\begin{equation}
    S_{0000} = \frac{1}{2} - \frac{\zeta(\lambda)}{4\lambda} + \frac{\zeta(\lambda)}{2}
    \label{eqn:bingham_integral}
\end{equation}
Now we're getting somewhere: we just define the function $S_{0000}(\mu)$ as:
\begin{itemize}
    \item Given, $\mu$, solve \Cref{eqn:bingham_nonlinear} for $\lambda$ using a Newton iteration,
    \item Evaluate \Cref{eqn:bingham_integral} with the argument $\lambda$ from the first step.
\end{itemize}
Fortunately, $S_{0000}(\mu)$ is a bounded and relatively smooth function of $\mu$. Thus we can use function\_generator to construct a nearly machine-precision and very accurate approximation of $S_{0000}(\mu)$.

\section{The full algorithm}

We now assume that we are given $D(\x)$ and outline a method for computing $(S:E)(\x)$ and $(S:D)(\x)$. Because these computations are done pointwise, we will omit $\x$ for the remainder. We first compute the eigendecomposition of $D$:
\begin{equation}
    D = \Omega\Lambda\Omega^\intercal.
\end{equation}
Since $D$ is symmetric positive-definite, we may apply the routine np.linalg.eigh (which is faster than np.linalg.eig) in order to find the ordered eigenvalues. We call $\mu_0$ the larger eigenvalue, and $\mu_1$ the smaller eigenvalue. Using the methodology from above, we compute $\tilde S_{0000}(\mu_0)$. Note that here it is denoted explicitly as $\tilde S_{0000}$ - this is because this is not actually $S_{0000}$, but that value in the diagonalized frame. Via identites, we may compute some of the other components as:
\begin{align}
    \tilde S_{0011} &= \mu_0 - S_{0000},    \\
    \tilde S_{1111} &= \mu_1 - S_{0011},    \\
    \tilde S_{0001} &= 0, \\
    \tilde S_{0111} &= 0,
\end{align}
We now simply have to perform a rotation:
\begin{equation}
    S_{ijkl} = \Omega_{im}\Omega_{jn}\Omega_{kq}\Omega_{lp}\tilde S_{mnqp},
\end{equation}
to get $S$ back. Calculuating this sum is somewhat nasty. Luckily we can exploit symmetries/identites to speed things up:
\begin{align}
    S_{0000} &= \Omega_{00}^4\tilde S_{0000} + 4\Omega_{00}^3\Omega_{01}\tilde S_{0001} + 6\Omega_{00}^2\Omega_{01}^2\tilde S_{0011} + 4\Omega_{00}\Omega_{01}^3\tilde S_{0111} + \Omega_{01}^4 S_{1111},  \\
    S_{0001} &= \Omega_{00}^3\Omega_{10}\tilde S_{0000} + (3\Omega^2\Omega_{01}\Omega_{10} + \Omega_{00}^3\Omega_{11})\tilde S_{0001} + 3(\Omega_{00}\Omega_{01}^2\Omega_{10} + \Omega_{00}^2\Omega_{01}\Omega_{11})\tilde S_{0011} +   \\
        &\qquad(3\Omega_{00}\Omega_{01}^2+\Omega_{11}+\Omega_{01}^3\Omega_{10})\tilde S_{0111} + \Omega_{01}^3\Omega_{11}\tilde S_{1111}.
\end{align}
Note that $\tilde S_{0001}=\tilde S_{0111}=0$, and so these terms can be left out of the sums in implementation, reducing these rotations to:
\begin{align}
    S_{0000} &= \Omega_{00}^4\tilde S_{0000} + 6\Omega_{00}^2\Omega_{01}^2\tilde S_{0011} + \Omega_{01}^4 S_{1111},  \\
    S_{0001} &= \Omega_{00}^3\Omega_{10}\tilde S_{0000} + 3(\Omega_{00}\Omega_{01}^2\Omega_{10} + \Omega_{00}^2\Omega_{01}\Omega_{11})\tilde S_{0011} + \Omega_{01}^3\Omega_{11}\tilde S_{1111}.
\end{align}
Knowing these, we can exploit more identites to find the rest of the components:
\begin{subequations}
    \begin{align}
        S_{0000} + S_{0011} &= D_{00},  \\
        S_{0100} + S_{0111} &= D_{01},  \\
        S_{1100} + S_{1111} &= D_{11}.
    \end{align}
\end{subequations}
Finally now that we have these components of $S$, we just need to compute $S:E$ and $S:D$. For a general symmetric tensor $T$, we have that:
\begin{subequations}
    \begin{align}
        (S:T)_{00} &= S_{0000}T_{00} + S_{0011}T_{11} + 2S_{0001}T_{01},    \\
        (S:T)_{01} &= S_{0001}T_{00} + S_{0111}T_{11} + 2S_{0011}T_{01},    \\
        (S:T)_{11} &= S_{0011}T_{00} + S_{1111}T_{11} + 2S_{0111}T_{01}.
    \end{align}
\end{subequations}
Since $S:T$ is symmetric, $(S:T)_{10}=(S:T)_{01}$.

\end{document}


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{hyperref}
\usepackage{float}

\newcommand{\comment}[1]{}

\title{Final Project Team 6}
\author{Berenice, Gabriela, Juan, Héctor, Damián}
\date{May 2021}

\begin{document}
    \maketitle
    \tableofcontents
    \newpage

    \begin{abstract}
        The hypothesis of this theorem is that we have a function $F$ that is continuous in a closed interval $[a, b]$, differentiable in the open interval $(a, b)$ and whose values in its extremes $F(a)$ and $F(b)$ match.
        The thesis of the theorem is that, in this case, the derived function vanishes at some point in the interval $(a, b)$
        Notice that since the interval is closed, it makes sense to talk about both $F(a)$ and $F(b)$. We will see that, intuitively, this statement is very simple.
        Rolle's theorem guarantees us that, under these conditions, there must be at least a certain value $x$ of the interval $(a, b)$ for which $F'(x) = 0$. But it only assures us that there has to be that value, not tells us nothing about its how to find it.

        This will be necessary for the problem, because it has a great complexity and will make us develop different mathematical skills to understand how the application of integrals and derivatives works.
    \end{abstract}

    \section{Problem}
    Let $f$ be a three times differentiable function (defined on $\mathbb{R}$
    and real-valued) such that $f$ has at least five distinct real zeros.
    Prove that $f + 6f' + 12f'' + 8f'''$ has at least two distinct real zeros.

    \subsection{Polynomial Function}
    A polynomial is generally represented as $P(x)$. The highest power of the variable of $P(x)$ is known as its degree. Degree of a polynomial function is very important as it tells us about the behaviour of the function $P(x)$ when x becomes very large, and also helps us to know the number of roots that we can have in a function. The domain of a polynomial function is entire real numbers $\mathbb{R} $.

    \comment{
    \section*{Rolle's Thorem}
    Let $f$ be a continuous function on $[a, b]$ and differentiable on $]a, b[$ such that $f(a) = f(b)$. Then there exists $c \in ]a, b[ $ such that $f'(c) = 0$.

    This theorem will be admitted because its proof requires results which are not seen in this course. If $f$ is the constant function, the result is obvious. Otherwise, since $f$ is continuous over $[a, b]$, $f$ is bounded over $[a, b]$
    and reaches its bounds. This means that there exists $m \in  [a, b] $ and $M \in  [a, b] $ such that $\forall x \in  [a, b]$, $f(m) \leqslant  f(x) \leqslant f(M) (f(m) \neq  f(M)$ because $f$ is not a constant function$)$. As $f(a) = f(b)$, then we have the following cases:

    \begin{enumerate}
        \item if $m = a$ or $m = b$, then $M \in  ]a, b[$, hence we have $c = M$;
        \item if $M = a$ or $M = b$, then $m \in  ]a, b[$, hence we have $c = m$;
        \item $m \in  ]a, b[$ and $M \in  ]a, b[$, hence we have $c = m$ or $c = M$.
    \end{enumerate}

    So in all cases $f$ admits a local extremum at a point $c$ of $]a, b[$ and is differentiable on $]a, b[$, hence, according to the proposition 22, $f'(c) = 0$.
    \section*{Hint}
    Use $g : x \rightarrow  e^ {\alpha x} $
    }

    \subsection{Rolle's Theorem}
    Suppose $f(x)$ is a function that satisfies all of the following.
    $f(x)$ is continuous on the closed interval $[a,b]$.
    $f(x)$ is differentiable on the open interval $(a,b)$.
    $$f(a) = f(b)$$
    Then there is a number c such that $a<c<b$ and $f'(c)=0$. Or, in other words $f(x)$ has a critical point in $(a,b)$.

    \begin{figure}[H]
        \centering
        \includegraphics[width=340px]{img/rollestheorem.jpg}
        \caption{Graphic of Rolle's Theorem}
    \end{figure}

    In other words, if a continuous curve passes through the same y-value (such as the x-axis) twice and has a unique tangent line (derivative) at every point of the interval, then somewhere between the endpoints it has a tangent parallel to the x-axis. The theorem was proved in 1691 by the French mathematician Michel Rolle, though it was stated without a modern formal proof in the 12th century by the Indian mathematician Bhaskara II. Other than being useful in proving the mean-value theorem, Rolle’s theorem is seldom used, since it establishes only the existence of a solution and not its value.

    This theorem helps us to know how many roots (distinct zeros) our function will have once we have derived it three times.

    \subsection{Exponential Function}
    An exponential function is a Mathematical function in form $f(x) = a^x$, where $x$ is a variable and $a$ is a constant which is called the base of the function and it should be greater than $0$. The most commonly used exponential function base is the transcendental number $e$, which is approximately equal to $2.71828$.

    An exponential function is defined by the formula $f(x) = a^x$, where the input variable x occurs as an exponent. The exponential curve depends on the exponential function and it depends on the value of the x.

    The exponential function is an important mathematical function which is of the form

    $$f(x) = a^x$$

    Where $a > 0$ and is not equal to $1$.

    $x$ is any real number.

    If the variable is negative, the function is undefined for $-1 < x < 1$.

    $$$$

    Here,
    $x$ is a variable
    $a$ is a constant, which is the base of the function.

    An exponential curve grows, or decay depends on the exponential function. Any quantity that grows or decays by a fixed per cent at regular intervals should possess either exponential growth or exponential decay.

    \pagebreak
    \section{Solution}
    Applying Rolle's Theorem:

    $f(x) \rightarrow 5$ distinct real zeros

    $f'(x) \rightarrow 4$ distinct real zeros

    $f''(x) \rightarrow 3$ distinct real zeros

    $f'''(x) \rightarrow 2$ distinct real zeros

    \subsection{Hint}
    We should use $e^{\alpha x}$

    \begin{itemize}
        \item Can be differentiable as much as we want
        \item Can have all zeros the function needs
        \item The differentiable function would express the no zeros
    \end{itemize}

    \subsection{Set up:}
    $$g(x) = e^{\alpha x} f(x)$$
    We infer the derivatives from the product rule:
    $$(f(x) g(x))' = f'(x) g(x) + f(x) g'(x)$$
    So, we have the consecuent derivatives:
    \begin{equation}
        \begin{split}
        &g'(x) = \alpha e^{\alpha x} f(x) + e^{\alpha x} f'(x)\\
        &g''(x) = \alpha ^2 e^{\alpha x} f(x) + 2\alpha ^2 e^{\alpha x} f'(x) + \alpha e^{\alpha x} f''(x)\\
        &g'''(x) = \alpha ^3 e^{\alpha x} f(x) + 3\alpha ^2 e^{\alpha x} f'(x) + 3\alpha e^{\alpha x} f''(x) + e^{\alpha x} f'''(x)\\
        \end{split}
    \end{equation}
    Simplifying from the algebraic properties:
    $$g'''(x) = e^{\alpha x} (\alpha ^3  f(x) + 3\alpha ^2f'(x) + 3\alpha f''(x) +  f'''(x))$$

    \subsection{Substitution}
    In the problem we have: $f + 6f' + 12f'' + 8f'''$

    \begin{equation}
        \begin{split}
            &8g'''(x) = 8e^{\alpha x} (\alpha ^3 f(x) + 3\alpha ^2 f'(x) + 3\alpha f''(x) + f'''(x))\\
            &8g'''(x) = e^{\alpha x} (8\alpha ^3 f(x) + 24\alpha ^2 f'(x) + 24\alpha f''(x) + 8f'''(x))\\
        \end{split}
    \end{equation}
    Using $\alpha$ as $\frac{1}{2}$ we got:
    \begin{equation}
        \begin{split}
            8g'''(x) =
            e^{\frac{x}{2}}
            \left(8\left(\frac{1}{2}\right)^3 f(x) +
            24\left(\frac{1}{2}\right)^2 f'(x) +
            24\left(\frac{1}{2}\right) f''(x) +
            8f'''(x)\right)
        \end{split}
    \end{equation}

    So with that we get:

    \begin{equation}
        \begin{split}
            8g'''(x) =
            \frac{e^{\frac{x}{2}}}{8}
            \left(f(x) +
            6 f'(x) +
            12 f''(x) +
            8f'''(x)\right)
        \end{split}
    \end{equation}

    \subsection{Answer:}
    $$g(x) = 8e^{\frac{x}{2}} f(x)$$

    In this case, $g'''(x)$ has at least 2 zeros

    \section{Appendix}
    Before we found the final solution, we made many attempts on how
    we could resolve this:

    \begin{figure}[H]
        \centering
        \includegraphics[width=340px]{img/firstattempt.png}
        \caption{First Attempt}
    \end{figure}

    \begin{figure}[H]
        \includegraphics[width=340px]{img/secondattempt.png}
        \caption{Second Attempt}
    \end{figure}

    \begin{figure}[H]
        \includegraphics[width=340px]{img/thirdattempt.png}
        \caption{Third Attempt}
    \end{figure}

    \pagebreak
    \section{Bibliography}
    \begin{itemize}
        \item Hosch, W. L. (2021, April 15). Rolle’s theorem | Definition, Equation, \& Facts. Encyclopedia Britannica. \\
        \href{https://www.britannica.com/science/Rolles-theorem}{https://www.britannica.com/science/Rolles-theorem}

        \item Dawkins, P. (2019, February 21). Calculus I - The Mean Value Theorem. \\
        \href{https://tutorial.math.lamar.edu/classes/calci/MeanValueTheorem.aspx}{https://tutorial.math.lamar.edu/classes/calci/MeanValueTheorem.aspx}

        \item A. (2021, March 22). General Data Protection Regulation(GDPR) Guidelines BYJU’S. BYJUS. \\
        \href{https://byjus.com/maths/exponential-functions/}{https://byjus.com/maths/exponential-functions/}

        \item A. (2021, March 22). General Data Protection Regulation(GDPR) Guidelines BYJU’S. BYJUS.\\
        \href{https://byjus.com/maths/polynomial-functions/}{https://byjus.com/maths/polynomial-functions/}
    \end{itemize}

\end{document}


\documentclass{beamer}
\usepackage{hyperref}
\usepackage{subfig}  %% To include subfigures
\usepackage{media9} %
\usepackage{ragged2e}  % Allow justification
\usepackage{url}
\usepackage[margin=20pt,font=small,labelfont=bf,labelsep=period]{caption}

\hypersetup{pdfstartview={Fit}, bookmarks=True, pdftitle={Wave Propagation Lectures},
            pdfauthor={Nicolas Guarin-Zapata}, pdfsubject={Lectures},
            pdfkeywords={Waves, Elasticity, Numerical Methods}}  % Configure hyperref

%--- New commands ----%
\newcommand{\footref}[1]{\textsuperscript{\ref{#1}}}
\newcommand{\pardiff}[2]{\frac{\partial #1}{\partial #2}}
\newcommand{\pardiffd}[2]{\frac{\partial^2 #1}{\partial #2^2}}
%---------------------%

%\usefonttheme[onlymath]{serif}  % Make equations to be in serif fonts
\usefonttheme{serif}  % Make equations to be in serif fonts

\begin{document}

%title
\title[Wave propagation in solids] % (optional, only for long titles)
{Wave propagation:}
\subtitle{Numerical tools}
\author[Guarin-Zapata, Nicolas] % (optional, for multiple authors)
{Nicol\'as Guar\'in Zapata\\ \texttt{\small nguarin@purdue.edu}\\
{\tiny Slides available at: \url{https://github.com/nicoguaro/CE597-slides}}}
\institute{Civil Engineering Department\\
  Purdue University}
\date{\today}
\subject{Wave propagation}

% Title page
\frame{\titlepage}

% Outline
\begin{frame}
	\frametitle{Outline}
	\tableofcontents
\end{frame}
%

%  Relations between elastic constants
\section{Relations between elastic constants}
\begin{frame}[shrink=50]
\frametitle{Relations between elastic constants}
\centering

\vspace{3cm}
\begin{table}[h]
\centering %
\begin{tabular}{|c|c|c|c|c|c|c|c|c|c|c|}
\hline
 & $(K,E)$  & $(K,\lambda)$  & $(K,G)$  & $(K,\nu)$  & $(E,G)$  & $(E,\nu)$  & $(\nu,G)$  & $(\nu,\lambda)$  & $(G,\lambda)$  & $(G,M)$ \\
\hline
$K=$  & $K$  & $K$  & $K$  & $K$  & $\frac{EG}{3(3G-E)}$  & $\frac{E}{3(1-2\nu)}$  & $\lambda+\frac{2G}{3}$  & $\frac{\lambda(1+\nu)}{3(1-2\nu)}$  & $\frac{2G(1+\nu)}{3(1-2\nu)}$  & $M-\frac{4G}{3}$ \\
\hline
$E=$  & $E$  & $\frac{9K(K-\lambda)}{3K-\lambda}$  & $\frac{9KG}{2K+G}$  & $3K(1-2\nu)$  & $E$  & $E$  & $\frac{G(3\lambda+2G)}{\lambda+G}$  & $\frac{\lambda(1+\nu)(1-2\nu)}{\nu}$  & $2G(1+\nu)$  & $\frac{G(3-M-4G)}{M-2G}$ \\
\hline
$\lambda=$  & $\frac{3K(3KE)}{9K-E}$  & $\lambda$  & $K-\frac{2G}{3}$  & $\frac{3K\nu}{1+\nu}$  & $\frac{G(E-2G)}{EG-E}$  & $\frac{E\nu}{(1+\nu)(1-2\nu)}$  & $\lambda$  & $\lambda$  & $\frac{2G\nu}{1-2\nu}$  & $M-2G$ \\
\hline
$G=$  & $\frac{3KE}{9K-E}$  & $\frac{3(K-\lambda)}{2}$  & $G$  & $\frac{3K(1-2\nu)}{2(1+\nu)}$  & $G$  & $\frac{E}{2(1+\nu)}$  & $G$  & $\frac{\lambda(1-2\nu)}{2\nu}$  & $G$  & $G$ \\
\hline
$\nu=$  & $\frac{3K-E}{6K}$  & $\frac{\lambda}{3K-\lambda}$  & $\frac{3K-2G}{2(3K+G)}$  & $\nu$  & $\frac{E}{2G}-1$  & $\nu$  & $\frac{\lambda}{2(\lambda+G)}$  & $\nu$  & $\nu$  & $\frac{M-2G}{2(M-G)}$ \\
\hline
$M=$  & $\frac{3K(3K+E)}{9K-E}$  & $3K-2\lambda$  & $K+\frac{4G}{3}$  & $\frac{3K(1-\nu)}{1+\nu}$  & $\frac{G(4G-E)}{3G-E}$  & $\frac{E(1-\nu)}{(1+\nu)(1-2\nu)}$  & $\lambda+2G$  & $\frac{\lambda(1-\nu)}{\nu}$  & $\frac{2G(1-\nu)}{1-2\nu}$  & $M$ \\
\hline
\end{tabular}
\end{table}

{\large $K$: Bulk modulus, $\lambda$: Lam\'e's first parameter, $E$: Young's
modulus, $G$: Shear modulus, $\nu$: Poisson's ratio, $M$: P-wave
modulus.}
\end{frame}

% Elastodynamics equations
\section{Elastodynamic wave equations}
\subsection{Navier-Cauchy Equations}
\begin{frame}
\frametitle{Navier-Cauchy Equations}
The Navier-Cauchy equations read
\begin{equation}
\rho{ \pardiffd{\bold{u}}{t}} = \bold{f} + ( \lambda + 2G)\nabla(\nabla \cdot \bold{u}) - G\nabla \times (\nabla \times \bold{u}) \enspace ,
\label{eq:navierVec}
\end{equation}
or, rearranging the constants
\begin{equation}
 \pardiffd{\bold{u}}{t} = \bold{f} + \alpha^2\nabla(\nabla \cdot \bold{u}) - \beta^2\nabla \times (\nabla \times \bold{u}) \enspace ,
\label{eq:navierVec2}
\end{equation}
being $\alpha$ the speed of the P-wave and $\beta$ the speed of the S-wave. According to this, our problem just depends on two material properties, the two wave speeds.

\end{frame}

% Relations for elastic wave speeds
\subsection{Relations for elastic wave speeds}
\begin{frame}[allowframebreaks]
\frametitle{Relations for elastic wave speeds}
The P-wave is a dilatational wave with speed $\alpha$ given by
\begin{align*}
 & \alpha^{2}=\frac{\lambda+2G}{\rho},\qquad\alpha^{2}=\frac{G(1-\nu)}{\rho},\\
 & \alpha^{2}=\frac{M}{\rho},\qquad\alpha^{2}=\frac{E(1-\nu)}{(1+\nu)(1-2\nu)\rho},\\
 & \alpha^{2}=\frac{2\beta^{2}(1-\nu)}{1-2\nu}.
\end{align*}
 The S-wave is a distorsional wave with speed $\beta$ given by
\begin{align*}
 & \beta^{2}=\frac{G}{\rho}\\
 & \beta^{2}=\frac{E}{2(1+\nu)\rho},\\
 & \beta^{2}=\frac{\alpha^{2}(1-2\nu)}{2(1-\nu)}.
\end{align*}
 Some particular values for the ratio
\[
\frac{\alpha^{2}}{\beta^{2}}=\frac{2(1-\nu)}{1-2\nu}
\]
 are
\begin{align*}
 & \frac{\alpha^{2}}{\beta^{2}}=\frac{4}{3}\quad\mbox{for }\nu=-1\enspace,\\
 & \frac{\alpha^{2}}{\beta^{2}}=2\quad\mbox{for }\nu=0\enspace,\\
 & \frac{\alpha^{2}}{\beta^{2}}=4\quad\mbox{for }\nu=\frac{1}{3}\enspace,\\
 & \frac{\alpha^{2}}{\beta^{2}}\rightarrow\infty\quad\mbox{when }\nu\rightarrow\frac{1}{2}\enspace.
\end{align*}
\end{frame}

\subsection{Ashby chart: $E$ vs. $\rho$}
\begin{frame}{Ashby chart: $E$ vs. $\rho$}
\begin{figure}
\includegraphics[height=6cm]{img/E_vs_density-vector.pdf}
\caption{Ashby chart for Young Modulus vs density. The lines show the sound speed, that is the speed for a wave in a rod made of this
material.  This value is between the longitudinal and shear wave speeds for Poisson ratios in (-0.5,0.5). \cite{ashby2005}}
\end{figure}
\end{frame}

% Solution
\section{Solution}
\subsection{Time domain vs. Frequency domain}
\begin{frame}[allowframebreaks]{Time domain vs. Frequency domain}
If we are interested in waves, we are interested in dynamic behavior. To find the solutions to the (linear) equations  we can use one of two approaches:
\begin{itemize}
\item \textbf{Time domain,} in this case the equations are solved directly.
\item \textbf{Frequency domain,} the solution is expressed as the superposition (sum) of individual waves with different frequencies. This means that the solutions to the equation (and forces) are of the form
\[\bold{u} = \bold{U} \exp(-i\omega t), \text{and } \bold{f} = \bold{F} \exp(-i\omega t)\]
substituting in the original equation we obtain
\begin{equation}
 \alpha^2\nabla(\nabla \cdot \bold{U}) - \beta^2\nabla \times (\nabla \times \bold{U})  + \bold{F} = -\omega^2\bold{U} \enspace ,
\label{eq:navier_freq}
\end{equation}

\pagebreak
Then, we transformed the problem of solving a dynamic equation into solving a set of steady-state problems. For every waveform there is an equivalent function in the frequency domain, i.e., its spectrum.\footnote{This can be formally defined using the Fourier transform, and computed numerically (efficiently) using the FFT algorithm.}
\end{itemize}

\begin{figure}[h]
\centering
\subfloat[Ricker pulse.]{\includegraphics[width=0.4\textwidth]{img/ricker_pulse.pdf}}\qquad
\subfloat[Ricker pulse spectrum.]{\includegraphics[width=0.4\textwidth]{img/ricker_area=2.pdf}}
\caption{Ricker pulse and its spectrum.}

\end{figure}
\end{frame}

\subsection{Numerical methods for waves in solids}
\begin{frame}
\frametitle{Numerical methods for waves in solids}
Exact solutions for the Navier-Cauchy equations are more an exception than the norm. So, we need methods to approximate the solutions (that's the role of numerical methods in general). A rough classification of the methods used in elastodynamics is:
\begin{itemize}
\item Spectral/Pseudo-spectral methods
\item Domain Discretization methods
\begin{itemize}
\item Finite Difference Methods (FDM)
\item Finite Volume Methods (FVM)
\item Finite Element Methods (FEM)
\item Boundary Element Methods (BEM)
\end{itemize}
\end{itemize}
\end{frame}

\subsubsection{Spectral/Pseudo-spectral methods}
\begin{frame}{Spectral/Pseudo-spectral method}
In this class of methods we expand the functions of interest in terms of a (orthogonal) basis \cite{wiki:pseudo_spectral}, i.e.
\[\bold{U}(\bold{x}) = \sum\limits_{n=1}^N c_n h_n(\bold{x}) \enspace ,\]
e.g., we can use a combination of sine functions
\[\bold{U}(\bold{x}) = \sum\limits_{n=1}^N c_n \sin(k_n\bold{x}) \enspace .\]
This methods produce very accurate solutions and present good convergence rates, but are difficult to apply for complex geometries.
\end{frame}

\subsubsection{Domain discretization methods}
\begin{frame}{Domain dizcretization methods}
In this set of methods the domain of interest is subdivided (discretized) to obtain a system of linear/algebraic equations. After applying the discretization process we end up with a system like
\[[K]\lbrace \bold{u}\rbrace + [M]\lbrace \bold{a}\rbrace = \lbrace \bold{f}\rbrace \enspace ,\]
in the time domain, and
\[[K]\lbrace \bold{U}\rbrace - \omega^2 [M]\lbrace \bold{U}\rbrace = \lbrace \bold{F}\rbrace \enspace ,\]
in the frequency domain. In both equations $[K]$,  and $[M]$ are termed stiffness and mass matrices.

\end{frame}

\subsubsection{Finite Difference Methods}
\begin{frame}{Finite Difference Methods}
In FDM the domain is (commonly) decomposed in rectangular regions where the function is constant \cite{wiki:FDM}. The differential equation is approximated with difference equations, e.g.
\[\pardiff{u}{x} \approx \frac{u(x + \Delta x) - u(x)}{h}\]
\begin{figure}
\includegraphics[height=4cm]{img/FDM.pdf}
\caption{Schematic domain discretized in rectangular cells.}
\end{figure}
\end{frame}

\subsubsection{Finite Volume Methods}
\begin{frame}{Finite Volume Methods}
In FVM the geometry is decomposed into regions, denoted as \emph{Finite volumes}. Some quantities are evaluated (as fluxes) at the interface between neighboring \emph{finite volumes}, these methods are conservative. This method is more used in CFD, but it is also popular for wave propagation.\footnote{See for example Clawpack: \url{http://depts.washington.edu/clawpack/}.}
\begin{figure}
\includegraphics[height=4cm]{img/FVM.pdf}
\caption{A finite volume and its surfaces.}
\end{figure}

\end{frame}

\subsubsection{Finite Element Methods}
\begin{frame}{Finite Element Methods}
The FEM is a method that is based on variational principles. It approximates a function in a finite set of points over the domain. To populate the matrices, the domain is split in several subregion called \emph{elements}.

\textcolor{blue}{But I'm sure you are going to talk more about this in the rest of the course}
\begin{figure}
\includegraphics[height=4cm]{img/Piecewise_linear_function2D.pdf}
\caption{A piecewise function represented via {finite elements}. From: \url{https://commons.wikimedia.org/wiki/File:Piecewise_linear_function2D.svg}}
\end{figure}

\end{frame}

\subsubsection{Boundary Element Methods}
\begin{frame}{Boundary Element Methods}
The BEM is similar in formulation to the FEM. The main difference lies in the dimensionality of the mesh, since it only requires the discretization of the contour. This method is really popular in fracture mechanics and wave propagation, the latter due to the capability of represent infinite domains.
\begin{figure}
\includegraphics[height=4cm]{img/BEM.pdf}
\caption{2D domain and the discretization of its contour using Boundary Elements.}
\end{figure}

\end{frame}

\subsection{CFL Condition}
\begin{frame}[allowframebreaks]
\frametitle{CFL Condition}
\justifying
The Courant-Friedrichs-Lewy condition (CFL condition) is a necessary
condition for convergence while solving PDEs by the method of finite
differences \cite{CFL}. It arises when explicit time-marching schemes are used.

The criterion could be stated as
\begin{align*}
C=v_{x}\frac{\Delta t}{\Delta x}\leq C_{max}\qquad\mbox{in 1D}\enspace;\\
C=v_{x}\frac{\Delta t}{\Delta x}+v_{y}\frac{\Delta t}{\Delta y}\leq C_{max}\qquad\mbox{in 2D}\enspace;\\
C=v_{x}\frac{\Delta t}{\Delta x}+v_{y}\frac{\Delta t}{\Delta y}+v_{z}\frac{\Delta t}{\Delta z}\leq C_{max}\qquad\mbox{in 3D}\enspace;
\end{align*}
 Where $v_{x_{i}}$ is the wave speed in the
$x_{i}$ direction, $\Delta x_{i}$ is the minimum spatial discretization
in $x_{i}$ direction, $\Delta t$ is the time step and $C_{max}$
is the maximum allowable value for $C$, which depends on the time
discretization scheme but should be less than 1.

A graphical representation of the criterion is given in Figure \ref{fig:CFL}. Intuitively, we can think about the CFL condition as a limit in the speed for transferring information from one node to its neighbors; this \emph{speed} should be less than the speed for propagation of phenomena in the wave.
\begin{figure}
\centering
\includegraphics[height=4cm]{img/CFLcondition.png}
\caption{Graphic representation of the CFL condition in 1D. \textit{L.A. Barba et al. Practical Numerical Methods with Python, 2014.}}\label{fig:CFL}
\end{figure}

In elastodynamics, and using the the FEM, the criterion could be re-stated as
\begin{align*}
C\leq\alpha\frac{\Delta t}{h}\leq C_{max}\qquad\mbox{in 1D}\enspace;\\
C\leq2\alpha\frac{\Delta t}{h}\leq C_{max}\qquad\mbox{in 2D}\enspace;\\
C\leq3\alpha\frac{\Delta t}{h}\leq C_{max}\qquad\mbox{in 3D}\enspace;
\end{align*}
 where $\alpha$ is the speed for the P-wave and $h$ is the
minimum distance between consecutive nodes. This give us the maximum
allowable timestep as
\begin{align}
\Delta t\leq C_{max}\frac{h}{\alpha}\qquad\mbox{in 1D}\enspace;\\
\Delta t\leq\frac{C_{max}}{2}\frac{h}{\alpha}\qquad\mbox{in 2D}\enspace;\\
\Delta t\leq\frac{C_{max}}{3}\frac{h}{\alpha}\qquad\mbox{in 3D}\enspace.
\end{align}
\end{frame}

\subsection{Nyquist-Shannon sampling criterion}
\begin{frame}[allowframebreaks]
\frametitle{Nyquist-Shannon sampling criterion}
\justifying
The Nyquist--Shannon sampling theorem is a fundamental
result in the field of information theory, in particular telecommunications and signal processing. Sampling is the process of converting a signal (for example, a function of continuous time or space) into a numeric sequence (a function of discrete time or space).
\begin{figure}
\centering
\includegraphics[height=2cm]{img/{CPT-sound-nyquist-thereom-1.5percycle}.pdf}
\caption{The samples of several different sine waves can be identical, when at least one of them is at a frequency above half the sample rate. From: \url{https://commons.wikimedia.org/wiki/File:CPT-sound-nyquist-thereom-1.5percycle.svg}}
\end{figure}

Shannon's version of the theorem states \cite{Shannon}:

\begin{quote}
If a function $x(t)$ contains no frequencies higher than $B$ hertz,
it is completely determined by giving its ordinates at a series of
points spaced $1/(2B)$ seconds apart.
\end{quote}
This theorem implies for us in the numerical simulation of wave propagation
that
\[
h\leq\frac{\lambda}{2}\enspace,
\]
 where $h$ is the maximum distance between consecutive nodes and
$\lambda$ is the shortest wavelength that want to be sampled. So,
the selection of $h$ is commonly
\[
h=\frac{\lambda}{k}\enspace,
\]
 where $k>2$ is a factor that depends on the numerical method. For
finite element methods $k$ is commonly 10.
\end{frame}

\begin{frame}[allowframebreaks]{References}
\def\newblock{}
\bibliographystyle{plain}
\begin{thebibliography}{1}

\bibitem{book:arfken} George B. Arfken \& Hans. J. Weber. Mathematical Methods for Physicists. Elsevier Academic Press, 6th Edition, San Diego, 2005.

\bibitem{ashby2005} Ashby, Michael F. ``Materials
selection in mechanical design." MRS BULLETIN 30 (2005): 995.

\bibitem{CFL} Courant, R.; Friedrichs, K.; Lewy, H. (1928), \emph{\"Uber
die partiellen Differenzengleichungen der mathematischen Physik} (in
German), Mathematische Annalen 100 (1): 32--74.

\bibitem{Shannon} C. E. Shannon, \emph{Communication in the presence
of noise}, Proc. Institute of Radio Engineers, vol. 37, no. 1, pp.
10--21, Jan. 1949. Reprint as classic paper in: Proc. IEEE, vol. 86,
no. 2, (Feb. 1998).

\bibitem{book:waves-rays} Michael A. Slawinski. Waves and rays in Elastic Continua. Second Edition, 2007.

\bibitem{wiki:FDM} Finite difference method. (2014, April 29). In Wikipedia, The Free Encyclopedia. Retrieved 00:11, September 25, 2014, from \url{http://en.wikipedia.org/w/index.php?title=Finite_difference_method&oldid=606345831}

\bibitem{wiki:FVM} Finite volume method. (2014, April 22). In Wikipedia, The Free Encyclopedia. Retrieved 00:44, September 25, 2014, from \url{http://en.wikipedia.org/w/index.php?title=Finite_volume_method&oldid=605282055}

\bibitem{wiki:ondas} Wikipedia community. Onda (f\'isica) [on line]. Wikipedia, La enciclopedia libre, 2010 ; 22 November 2010.

\bibitem{wiki:pseudo_spectral} Pseudo-spectral method. (2014, March 14). In Wikipedia, The Free Encyclopedia. Retrieved 23:12, September 24, 2014, from \url{http://en.wikipedia.org/w/index.php?title=Pseudo-spectral_method&oldid=599542680}

\end{thebibliography}

\end{frame}

\end{document}


\documentclass[serif,xcolor=pdftex,dvipsnames,table,hyperref={bookmarks=false,breaklinks}]{beamer}

\input{../config.tex}

\settitlecard{8}{Numerical Linear Algebra 2}

\begin{document}

\maketitlepage

% \section{Announcements}
% \subsection{Foo}
%
%
% \begin{frame}[t]{Announcements}
% 	\begin{itemize}
% 		\item Assignment 3 is due tonight at 11:55pm.
% 		\item Quiz 4 will go out tonight.
% 		\item Assignment 4 will go out in the next couple of days.
% 		\item Guest lecturer on Tuesday.
% 	\end{itemize}
% \end{frame}

\section{Numerical Linear Algebra 2}
\subsection{Foo}

\begin{frame}[t]{Matrix Inversion}
	% Review
	% Where it is used
	Review: An $n\times n$ square matrix $A$ is said to be \textbf{invertible} if there exists an $n \times n$ matrix $B$ such that:

	$$ AB = BA = I$$

	where $I$ is the identity matrix. If $B$ exists it is called the \textbf{inverse} and is denoted $A^{-1}$. Matrix inversion is the process of finding $A^{-1}$ for a given matrix $A$.
\end{frame}

\begin{frame}[t]{Matrix Inversion: Applications}
	\begin{itemize}[<+->]
		\item Matrix inverses appear in statistics frequently. Part of the reason for this is because of the appearance of a matrix inverse in the PDF of the multivariate normal distribution.

		$$ \mathcal{N}(x;\mu,\Sigma) \propto \exp\left(-\frac{1}{2}(x-\mu)^T \Sigma^{-1}(x - \mu)\right) $$
		\item The analytical solution for least-squares linear regression involves a matrix inverse.
		\item Matrix inversion plays a fundamental role in many computer graphics routines.
		\item Matrix inversion is a subroutine for many more complex linear algebra computations.
	\end{itemize}
\end{frame}

\begin{frame}[t]{Gauss-Jordan Elimination}
	% Description
	\begin{itemize}[<+->]
		\item Many algorithms exist for inverting a matrix.
		\item We will analyze one of the fundamental algorithms called \textbf{Gauss-Jordan Elimination}.
		\item \textbf{Gaussian Elimination} is a method for solving equations of the form $Ax = b$ where $A$ is a matrix, $b$ is a vector, and we are solving for the vector $b$.
		\item Gaussian elimination can be thought of as a systematic application of simple substitution rules.
		\item Gauss-Jordan elimination is the application of this idea to the equation $AX = I$ where now $X$ is a matrix rather than a vector.
	\end{itemize}
\end{frame}

\begin{frame}[t]{Gauss-Jordan Elimination}
	% 2x2 example
	We'll start with a simple example: Let $A$ be the following $2 \times 2$ matrix.

	\pause
	$$A = \begin{bmatrix}[rr]
    	1 & 3 \\
		2 & 5
	\end{bmatrix}$$

	\pause
	Our goal is to find the inverse.
	\pause
	$$A^{-1} = \begin{bmatrix}[rr]
    	-5 & 3 \\
		2 & -1
	\end{bmatrix}$$

\end{frame}

\begin{frame}[t]{Gauss-Jordan Elimination}
	% 2x2 example
	We begin by writing $A$ in the following augmented form:

	\pause
	$$A = \begin{bmatrix}[rr|rr]
    	1 & 3 & 1 & 0\\
		2 & 5 & 0 & 1
	\end{bmatrix}$$

	\pause
	We then apply elementary row operations until the left side is equal to the identity matrix. Elementary row operations include:
	\pause
	\begin{itemize}[<+->]
		\item Scaling a row by a non-zero constant.
		\item Adding a scaled row to another row.
		\item Swapping two rows (we will not use this).
	\end{itemize}

	\pause
	If we are able to do this without getting a row of all zeros on the left, then the right side will be $A^{-1}$. If at any point we get a row with all zeros, then the matrix has no inverse.

\end{frame}

\begin{frame}[t]{Gauss-Jordan Elimination}
	% 2x2 example
	$$\begin{bmatrix}[rr|rr]
    	1 & 3 & 1 & 0\\
		2 & 5 & 0 & 1
	\end{bmatrix}$$

	\pause
	$$R_2 \leftarrow R_2 - 2R_1$$

	\pause
	$$\begin{bmatrix}[rr|rr]
    	1 & 3 & 1 & 0\\
		0 & -1 & -2 & 1
	\end{bmatrix}$$

\end{frame}

\begin{frame}[t]{Gauss-Jordan Elimination}
	% 2x2 example
	$$\begin{bmatrix}[rr|rr]
    	1 & 3 & 1 & 0\\
		0 & -1 & -2 & 1
	\end{bmatrix}$$

	\pause
	$$R_2 \leftarrow -R_2$$

	\pause
	$$\begin{bmatrix}[rr|rr]
    	1 & 3 & 1 & 0\\
		0 & 1 & 2 & -1
	\end{bmatrix}$$

\end{frame}

\begin{frame}[t]{Gauss-Jordan Elimination}
	% 2x2 example
	$$\begin{bmatrix}[rr|rr]
    	1 & 3 & 1 & 0\\
		0 & 1 & 2 & -1
	\end{bmatrix}$$

	\pause
	$$R_1 \leftarrow R_1 - 3R_2$$

	\pause
	$$\begin{bmatrix}[rr|rr]
    	1 & 0 & -5 & 3\\
		0 & 1 & 2 & -1
	\end{bmatrix}$$

	\pause
	\centering
	\Huge{Done!}
\end{frame}

% \begin{frame}[t]{Gauss-Jordan Elimination}
% 	% 2x2 example
% 	$$\begin{bmatrix}[rr|rr]
%     	1 & 0 & -5 & 3\\
% 		0 & 1 & 2 & -1
% 	\end{bmatrix}$$
%
% 	\pause
% 	\centering
% 	\Huge{Done!}
%
% \end{frame}

\begin{frame}[t]{Gauss-Jordan Elimination}
	% 2x2 example
	With such a small matrix it is hard to get a sense for what the steps are:
	\pause
	\begin{itemize}[<+->]
		\item For each row $i$ from top to bottom:
		\begin{itemize}[<+->]
			\item Scale the row so that the diagonal entry equals 1.
			\item Subtract a scaled version of row $i$ from each row below $i$ so that the $i$th column in each of these rows is $0$.
			\item This eliminates all entries below the diagonal and sets the diagonal to ones.
		\end{itemize}
		\item Repeat this process from the bottom up, this time eliminating entries above the diagonal.
	\end{itemize}
\end{frame}

\begin{frame}[t]{Gauss-Jordan Elimination}
	% 2x2 example
	$$A = \begin{bmatrix}[rrr]
    	2 & 3 & 0\\
		1 & -2 & -1\\
		2 & 0 & -1
	\end{bmatrix}$$

\end{frame}

\begin{frame}[t]{Gauss-Jordan Elimination}
	% 2x2 example
	$$\begin{bmatrix}[rrr|rrr]
    	2 & 3 & 0 & 1 & 0 & 0\\
		1 & -2 & -1 & 0 & 1 & 0\\
		2 & 0 & -1 & 0 & 0 & 1
	\end{bmatrix}$$

	\pause
	\begin{align*}
		R_1 &\leftarrow \frac{1}{2}R_1\\
	\end{align*}

	\pause
	$$\begin{bmatrix}[rrr|rrr]
    	1 & \frac{3}{2} & 0 & \frac{1}{2} & 0 & 0\\
		1 & -2 & -1 & 0 & 1 & 0\\
		2 & 0 & -1 & 0 & 0 & 1
	\end{bmatrix}$$

\end{frame}

\begin{frame}[t]{Gauss-Jordan Elimination}
	% 2x2 example
	$$\begin{bmatrix}[rrr|rrr]
    	1 & \frac{3}{2} & 0 & \frac{1}{2} & 0 & 0\\
		1 & -2 & -1 & 0 & 1 & 0\\
		2 & 0 & -1 & 0 & 0 & 1
	\end{bmatrix}$$

	\pause
	\begin{align*}
		R_2 &\leftarrow R_2 - R_1\\
		R_3 &\leftarrow R_3 - 2R_1\\
	\end{align*}

	\pause
	$$\begin{bmatrix}[rrr|rrr]
    	1 & \frac{3}{2} & 0 & \frac{1}{2} & 0 & 0\\
		0 & -\frac{7}{2} & -1 & -\frac{1}{2} & 1 & 0\\
		0 & -\frac{6}{2} & -1 & -1 & 0 & 1
	\end{bmatrix}$$

\end{frame}

\begin{frame}[t]{Gauss-Jordan Elimination}
	% 2x2 example
	$$\begin{bmatrix}[rrr|rrr]
    	1 & \frac{3}{2} & 0 & \frac{1}{2} & 0 & 0\\
		0 & -\frac{7}{2} & -1 & -\frac{1}{2} & 1 & 0\\
		0 & -\frac{6}{2} & -1 & -1 & 0 & 1
	\end{bmatrix}$$

	\pause
	\begin{align*}
		R_2 &\leftarrow -\frac{7}{2}R_2\\
	\end{align*}

	\pause
	$$\begin{bmatrix}[rrr|rrr]
    	1 & \frac{3}{2} & 0 & \frac{1}{2} & 0 & 0\\
		0 & 1 & \frac{2}{7} & \frac{1}{7} & -\frac{2}{7} & 0\\
		0 & -\frac{6}{2} & -1 & -1 & 0 & 1
	\end{bmatrix}$$

\end{frame}

\begin{frame}[t]{Gauss-Jordan Elimination}
	% 2x2 example
	$$\begin{bmatrix}[rrr|rrr]
    	1 & \frac{3}{2} & 0 & \frac{1}{2} & 0 & 0\\
		0 & 1 & \frac{2}{7} & \frac{1}{7} & -\frac{2}{7} & 0\\
		0 & -\frac{6}{2} & -1 & -1 & 0 & 1
	\end{bmatrix}$$

	\pause
	\begin{align*}
		R_3 &\leftarrow R_3 + \frac{6}{2}R_2\\
		R_3 &\leftarrow -7R_3
	\end{align*}

	\pause
	$$\begin{bmatrix}[rrr|rrr]
    	1 & \frac{3}{2} & 0 & \frac{1}{2} & 0 & 0\\
		0 & 1 & \frac{2}{7} & \frac{1}{7} & -\frac{2}{7} & 0\\
		0 & 0 & 1 & -4 & -6 & -7
	\end{bmatrix}$$

\end{frame}

\begin{frame}[t]{Gauss-Jordan Elimination}
	% 2x2 example
	$$\begin{bmatrix}[rrr|rrr]
    	1 & \frac{3}{2} & 0 & \frac{1}{2} & 0 & 0\\
		0 & 1 & \frac{2}{7} & \frac{1}{7} & -\frac{2}{7} & 0\\
		0 & 0 & 1 & -4 & -6 & -7
	\end{bmatrix}$$

	\pause
	\begin{align*}
		R_2 &\leftarrow R_2 - \frac{2}{7}R_3\\
		R_1 &\leftarrow R_1 + 0R_3\\
	\end{align*}

	\pause
	$$\begin{bmatrix}[rrr|rrr]
    	1 & \frac{3}{2} & 0 & \frac{1}{2} & 0 & 0\\
		0 & 1 & 0 & -1 & -2 & 2\\
		0 & 0 & 1 & -4 & -6 & -7
	\end{bmatrix}$$

\end{frame}

\begin{frame}[t]{Gauss-Jordan Elimination}
	% 2x2 example
	$$\begin{bmatrix}[rrr|rrr]
    	1 & \frac{3}{2} & 0 & \frac{1}{2} & 0 & 0\\
		0 & 1 & 0 & -1 & -2 & 2\\
		0 & 0 & 1 & -4 & -6 & -7
	\end{bmatrix}$$

	\pause
	\begin{align*}
		R_1 &\leftarrow R_1 - \frac{3}{2}R_2\\
	\end{align*}

	\pause
	$$\begin{bmatrix}[rrr|rrr]
    	1 & 0 & 0 & 2 & 3 & -3\\
		0 & 1 & 0 & -1 & -2 & 2\\
		0 & 0 & 1 & -4 & -6 & -7
	\end{bmatrix}$$

	\pause
	\centering
	\Huge{Done!}

\end{frame}

\begin{frame}[t]{Gauss-Jordan Complexity}
	% Complexity
	% n multiplications
	% n multiplications
	% (n - i)*n additions
	\pause
	\begin{itemize}[<+->]
		\item When you scale a row so that its diagonal is one, how many multiplications do we perform? How many times do we do this?
		\begin{itemize}[<+->]
			\item $n$ multiplications. One per item in the row.
			\item $n$ times. Once per row.
		\end{itemize}
		\item When performing a row reduction (adding one scaled row to another), how many multiplications and additions do we perform? How many many rows to we add row $i$ to?
		\begin{itemize}
			\item $n$ multiplications. (technically $n-i$)
			\item $n$ additions. (technically $n-i$)
			\item We add row $i$ to all rows below row $i$, so $n-i$ times.
		\end{itemize}
	\end{itemize}
\end{frame}

\begin{frame}[t]{Gauss-Jordan Complexity}
	\begin{align*}
		\onslide<1->{\text{No. Operations } &= 2\sum_{i=1}^{n}\left(n + 2n(n-i)\right)\\}
		\onslide<2->{&= 2\sum_{i=1}^{n}\left(n + 2n^2 - 2ni\right)\\}
		\onslide<3->{&= 2\left[\sum_{i=1}^{n}n + 2\sum_{i=1}^{n}n^2 - 2\sum_{i=1}^{n}ni\right]\\}
		\onslide<4->{&= 2\left[n^2 + 2n^3 - 2n\frac{n(n+1)}{2}\right]\\}
		\onslide<5->{&= 2\left[n^2 + 2n^3 - n^3 + n^2\right]\\}
		\onslide<6->{&= 4n^2 + 2n^3} \onslide<7>{= \mathcal{O}(n^3)}
	\end{align*}

\end{frame}

\begin{frame}[t]{Advanced Matrix Inverse Algorithms}
	% Description
	As with matrix multiplication, more sophisticated algorithms exist the have complexity between $n^2$ and $n^3$.
\end{frame}

\begin{frame}[t]{Estimating the complexity of NumPy Matrix Inverse}
	% Fit a line
	The algorithm used by NumPy is not well documented. How could we estimate its complexity? \pause(Hint: We know the complexity is approximately a monomial (e.g. $\mathcal{O}(n^3)$)).

	\pause
	\begin{align*}
		\text{Run time } &\approx Cn^b\\
		\log(\text{Run time}) &\approx \log(C) + b\log(n)
	\end{align*}

	\pause
	Solution:
	\begin{enumerate}
		\item Run a bunch of tests for different $n$ and record the run times.
		\item Fit a line to the $\log$ run times. The slope will be the degree of the polynomial and the intercept will be the logged constant.
	\end{enumerate}

\end{frame}

\begin{frame}[t]{Estimating the complexity of NumPy Matrix Inverse}
	% Fit a line
	\centering
	\Huge{Demo}
\end{frame}

\begin{frame}[t]{Eigenreview}
	% Description
	% Eigen Review
	Let $A$ be a square $n\times n$ matrix, then the $\mathbf{v}$ is an \textbf{eigenvector} of $A$ if

	$$A\mathbf{v} = \lambda\mathbf{v}$$

	for some constant $\lambda$ known as an \textbf{eigenvalue}. \textbf{Eigen decomposition} is the process of finding the eigenvalue/eigenvector pairs of a matrix.
\end{frame}

\begin{frame}[t]{Eigen Decomposition: Applications}
	% Description
	% Eigen Review
	\begin{itemize}[<+->]
		\item Eigen decompositions are used in many practical applications:
		\begin{itemize}[<+->]
			\item Google's PageRank computes the largest eigenvalue/vector pair.
			\item Principal Components Analysis (PCA) is used in many data analysis settings to reduce the dimensionality of a dataset and reduce collinearity.
			\item Spectral clustering is used in machine learning and computer vision for clustering data points and parts of images. Spectral clustering requires calculating the Eigen decomposition of a similarity matrix.
		\end{itemize}
	\end{itemize}
\end{frame}

\begin{frame}[t]{Eigen Decomposition: Eigenfaces}
	% Description
	% Eigen Review
	\begin{itemize}[<+->]
		\item One of the seminal early pieces of work in computer vision worked by compressing images into eigenvectors.
		\item The basic idea was to calculate the number of images by number of images image covariance matrix and computing the eigenvectors of this matrix.
		\item The result is one eigenvector for each image.
		\item The most well known application was facial recognition where each image was of a face, hence eigenfaces.
	\end{itemize}

	\pause
	\centering
	\includegraphics[height=1in]{{../Figures/Eigenfaces}.png}
\end{frame}

\begin{frame}[t]{Eigen Decomposition: The Power Method}
	% Description
	The Power method is a method for calculating the eigenvalue corresponding to the greatest eigenvalue. Many other methods for calculating the full set of eigenvalues are generalizations of this method.

	\pause
	\begin{block}{The Power Method}
		\begin{enumerate}[<+->]
			\item Given an $n\times n$ matrix $A$, choose a random initial vector $b_0$.
			\item Then, under some mild assumptions, the following sequence will converge to the dominant eigenvector:

			$$\frac{Ab_0}{\norm{Ab_0}},\frac{A^2b_0}{\norm{A^2b_0}},\frac{A^3b_0}{\norm{A^3b_0}},...$$
		\end{enumerate}
	\end{block}

\end{frame}

\begin{frame}[t]{Eigen Decomposition: Complexity}
	% Description
	If we were to calculate matrix matrix powers $A^k$ directly using matrix multiplication, what is the complexity of calculating $A^k$?

	\pause
	\begin{itemize}
		\item Answer: We would need to perform $k$ matrix multiplications, thus using Strassen's this would take $\approx\mathcal{O}(kn^{2.807})$.
	\end{itemize}

	\pause
	Fortunately, there is a better way. We can use the following interative algorithm:

	$$b_{k} = \frac{A^kb_0}{\norm{A^kb_0}} = \frac{A(A^{k-1}b_0)}{\norm{A(A^{k-1}b_0)}} = \frac{Ab_{k-1}}{\norm{Ab_{k-1}}}$$

	\pause
	What is the complexity of computing $Ab_{k-1}$?

	\pause
	\begin{itemize}
		\item The power method has complexity $\mathcal{O}(n^2)$ \textbf{per iteration}.
	\end{itemize}
\end{frame}

\begin{frame}[t]{Inversion and Decomposition in Action: Linear Regression}
	% Linear Regression Derivation
	Let $X \in \mathbb{R}^{n\times m}$ be a $n \times m$ matrix of data cases (i.e. design matrix) and let $y \in \mathbb{R}^n$ be a length $n$ vector of real values. Then in ordinary least squares linear regression, we have the following model:

	$$y = \beta X + \epsilon$$

	where $\epsilon$ is a normally distributed vector of noise. \pause Then using Maximum Likelihood Estimation, we estimate $\hat{\beta}$ as

	$$\hat{\beta} = \argmin_\beta (\beta X - y)^T(\beta X - y)$$

\end{frame}

\begin{frame}[t]{Inversion and Decomposition in Action: Linear Regression}
	% Linear Regression Derivation
	The solution to this minimization problem can be found by taking the gradient with respect to $\beta$, setting it to zero, and solving. The results is:

	$$\hat{\beta} = (X^T X)^{-1}X^Ty$$

	\pause
	\begin{itemize}[<+->]
		\item We can solve this using just matrix inversion and matrix multiplication.
		\item The advanced linear algebraist may have noticed that $(X^T X)^{-1} X^T$ is called the \textbf{Moore-Penrose pseudoinverse}.
		\item There are specialized algorithms for computing the Moore-Penrose pseudoinverse.
		\item Part of Assignment 4 will be implementing and comparing linear regression using straight inversion vs. pseudoinversion.
	\end{itemize}

\end{frame}

% \begin{frame}[t]{Inversion and Decomposition in Action: Linear Regression}
% 	% Demo
% 	\centering
% 	\Huge{Demo}
% \end{frame}

\begin{frame}[t]{Linear Algebra in NumPy}
	NumPy has a sub-module called \textbf{numpy.linalg} which implements the following groups of methods:
	\pause
	\begin{itemize}[<+->]
		\item Products: inner, outer, matrix, etc.
		\item Decompositions: Cholesky, QR, SVD, Eigen
		\item Special numbers: rank, norm, determinant, etc.
		\item Solvers: Inversion, solve $Ax = b$, least squares, pseudoinversion, etc.
		\item Plus a few more...
	\end{itemize}
\end{frame}

\begin{frame}[t]{Numerical Linear Algebra: Major Takeaways}
	\begin{itemize}[<+->]
		\item If you continue to do numerical computing, you will likely find yourself using some of these linear algebra computations.
		\item Keep the approximate complexities for the major methods in mind so that you know what is feasible in your programs.
		\begin{itemize}[<+->]
			\item For example: Directly solving linear regression with 1,000 instances is feasible, but 1,000,000 might not be. In this case you should consider a different method.
		\end{itemize}
		\item As a rule of thumb, assume $\mathcal{O}(n^3)$ runtime.
		\item Approximations for many of these computations exist that are good enough in many cases.
	\end{itemize}
\end{frame}

% \begin{frame}[t]{Eigen Decomposition: QR Algorithm}
% 	% Description
% \end{frame}
%
% \begin{frame}[t]{Eigen Decomposition: QR Algorithm}
% 	% Description
% \end{frame}

% \section{Sparse Matrices}
% \subsection{Foo}

\end{document}


%%% Template originaly created by Karol Kozioł (mail@karol-koziol.net) and modified for ShareLaTeX use

\documentclass[a4paper,11pt]{article}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{xcolor}

\renewcommand\familydefault{\sfdefault}
\usepackage{tgheros}
\usepackage[defaultmono]{droidmono}

\usepackage{amsmath,amssymb,amsthm,textcomp}
\usepackage{enumerate}
\usepackage{multicol}
\usepackage{tikz}

\usepackage{geometry}
\geometry{left=25mm,right=25mm,%
bindingoffset=0mm, top=20mm,bottom=20mm}

\linespread{1.3}

\newcommand{\linia}{\rule{\linewidth}{0.5pt}}

% custom theorems if needed
\newtheoremstyle{mytheor}
    {1ex}{1ex}{\normalfont}{0pt}{\scshape}{.}{1ex}
    {{\thmname{#1 }}{\thmnumber{#2}}{\thmnote{ (#3)}}}

\theoremstyle{mytheor}
\newtheorem{defi}{Definition}

% my own titles
\makeatletter
\renewcommand{\maketitle}{
\begin{center}
\vspace{2ex}
{\huge \textsc{\@title}}
\vspace{1ex}
\\
\linia\\
\@author \hfill \@date
\vspace{4ex}
\end{center}
}
\makeatother
%%%

% custom footers and headers
\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{}
\chead{}
\rhead{}
\lfoot{Object Tracking}
\cfoot{}
\rfoot{Page \thepage}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}
%

% code listing settings
\usepackage{listings}
\lstset{
    language=Matlab,
    basicstyle=\ttfamily\small,
    aboveskip={1.0\baselineskip},
    belowskip={1.0\baselineskip},
    columns=fixed,
    extendedchars=true,
    breaklines=true,
    tabsize=4,
    prebreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\hookleftarrow}},
    frame=lines,
    showtabs=false,
    showspaces=false,
    showstringspaces=false,
    keywordstyle=\color[rgb]{0.627,0.126,0.941},
    commentstyle=\color[rgb]{0.133,0.545,0.133},
    stringstyle=\color[rgb]{01,0,0},
    % numbers=left,
    % numberstyle=\small,
    % stepnumber=1,
    % numbersep=10pt,
    % captionpos=t,
    escapeinside={\%*}{*)}
}

%%%----------%%%----------%%%----------%%%----------%%%

\begin{document}

\title{The Extended Kalman Filter}

\author{Mehdi Raza Khorasani, Habib University}

\date{26/6/2021}

\maketitle

\section*{Introduction}
The extended Kalman Filter is an extension to the Kalman Filter Algorithm. The KF algorithm is defined for Discrete time Linear time invariant systems (DT LTI), which are of the form:
\begin{equation}
    x_k = Fx_{k-1} + V_k
\end{equation}
\begin{equation}
    y_k = Hx_{k} + W_k
\end{equation}
The algorithm fails when the system cannot be represented as $(1)$ and $(2)$. The extended KF approach suggests that we linearize the system and obtain a linear approximation, and then develop a KF algorithm for it. The following lines describe how a non linear system can be linearized and a KF algorithm can be applied to it.
\subsection*{The discrete time non-linear system}
Consider a non-linear system, defined by the following equations:
\begin{equation}
    x_k = f(x_{k-1}) + V_k
\end{equation}
\begin{equation}
    y_k = h(x_{k}) + W_k
\end{equation}
where, $V_k$ and $W_k$ is white uncorrelated Gaussian noise defined as follows:

\begin{equation*}
    V_k \sim (0, Q_k)
\end{equation*}
\begin{equation*}
    W_k \sim (0, R_k)
\end{equation*}

\section*{The EKF Algorithm}
The algorithm of the EKF consists of linearlizing the plant about the optimal value and consecutively applying the baysian estimation steps. Similarly, the observations are also linearized about the optimal value. The following lines summarize the filtering algorithm:

\begin{enumerate}
    \item
    Linearization: Compute the Jacobian of $f$ at $\hat{x}_{k-1|k-1}$ i.e. about the last optimal estimate of $x_k$:
    \begin{equation*}
        F_k = \nabla_{X^T} f(x) \mid_{x = \hat{x}_{k-1|k-1}}
    \end{equation*}

    \item
    State Prediction: Compute Predicted mean and co-variance matrix:
    \begin{equation*}
        \hat{x}_{k|k-1} = f(\hat{x}_{k-1|k-1})
    \end{equation*}
    \begin{equation*}
        P_{k|k-1} = F_k P_{k-1|k-1} F_k^T + Q_k
    \end{equation*}

    \item
    Linearization: Compute the jacobian of $h$ at $\hat{x}_{k|k-1}$ i.e. about the estimate of $x$ given the last estimate:
    \begin{equation*}
        H_k = \nabla_{X^T} h(x) \mid_{x = \hat{x}_{k|k-1}}
    \end{equation*}

    \item
    Measurement Prediction: Compute predicted mean, covariance and Kalman gain:
    \begin{equation*}
        \hat{y}_{k|k-1}  = h(\hat{x}_{k|k-1})
    \end{equation*}
    \begin{equation*}
        S_k = H_k P_{k|k-1} H_k^T + R_k
    \end{equation*}
    \begin{equation*}
        K_k = P_{k|k-1} H_k^T S_k^{-1}
    \end{equation*}

    \item
    Estimation: Compute the posterior mean and co-variance as follows:
    \begin{equation*}
        \hat{x}_{k|k} = \hat{x}_{k|k-1} + K_k(y_k - \hat{y}_{k|k-1})
    \end{equation*}
    \begin{equation*}
        P_{k|k} = P_{k|k-1} - K_k H_k P_{k|k-1}
    \end{equation*}
\end{enumerate}

\section*{Simulation}
We simulate the non-linear system observed by a Radar. The system and radar are governed by the following equations:

\begin{equation}
    \boldsymbol{x_k} =
    \begin{bmatrix}
    x_k\\
    y_k\\
    \phi_k
    \end{bmatrix} =
    \begin{bmatrix}
    x_{k-1} + T v_k cos(\phi_{k-1}) \\
    y_{k-1} + T v_k sin(\phi_{k-1}) \\
    \phi_{k-1} + T\omega_k
    \end{bmatrix} +
    \begin{bmatrix}
    V_{1, k}\\
    V_{2, k}\\
    V_{3, k}
    \end{bmatrix}
\end{equation}
\begin{equation}
    \boldsymbol{y_k} =
    \begin{bmatrix}
    \sqrt{x_k^2+y_k^2}\\
    tan^{-1}\frac{y_k}{x_k}
    \end{bmatrix}
\end{equation}
where $v_k$, $\omega_k$ are the linear and angular velocities of the object respectively and $T$ is sampling time. They are taken as constants:
\begin{equation*}
    v_k = 0.1, \\ \omega_k = 0.01, \\ T = 0.05
\end{equation*}
\subsection*{Algorithm}

\subsubsection{Initialization}
\begin{enumerate}
    \item
    The co-variance Matrix of process Noise was taken as follows:
\begin{equation*}
    Q_k =
    \begin{bmatrix}
    1e^{-6} & 0 & 0\\
    0 & 1e^{-6} & 0 \\
    0 & 0 & 1e^{-6}
    \end{bmatrix}
\end{equation*}

    \item The co-variance Matrix of Radar Noise was taken as follows:
\begin{equation*}
    R_k =
    \begin{bmatrix}
    1e^{-4} & 0 & 0\\
    0 & 1e^{-4} & 0 \\
    0 & 0 & 1e^{-4}
    \end{bmatrix}
\end{equation*}

    \item
    $\mathbf{MATLAB}$'s $randn()$ function was used to generate the the noise vectors $V_k$ and $W_k$ of dimensions $3\times 1$ and $2 \times 1$ respectively.

    \item
    Compute the true value of $x_k$ and $y_k$ from the model's equation (5) and (6) to simulate the non-linear object and Radar.

\end{enumerate}

\subsubsection{Linearization}
\begin{enumerate}
    \item
    The pseudo function for this step is as follows:
    \begin{lstlisting}
        F_k = F_jacobian(T, vk, xhat_km1)
    \end{lstlisting}

    \item The jacobian of $f$ is computed as follows:
    \begin{equation*}
         F_k = \begin{bmatrix}
         \frac{\partial f_1}{\partial x_k} & \frac{\partial f_1}{\partial y_k} & \frac{\partial f_1}{\partial \phi_k} \\
         \frac{\partial f_2}{\partial x_k} & \frac{\partial f_2}{\partial y_k} & \frac{\partial f_2}{\partial \phi_k} \\
         \frac{\partial f_3}{\partial x_k} & \frac{\partial f_3}{\partial y_k} & \frac{\partial f_3}{\partial \phi_k}
         \end{bmatrix}_{\mathbf{x} = \hat{x}_{k-1|k-1}}
    \end{equation*}
    where:
    \begin{equation*}
        \begin{bmatrix}
            f_1 \\
            f_2 \\
            f_3
        \end{bmatrix}
        =
        \begin{bmatrix}
            x_{k-1} + Tv_k \cos(\phi_{k-1})\\
             y_{k-1} + Tv_k \sin(\phi_{k-1})\\
             \phi_{k-1} + Tw_k
        \end{bmatrix}_{\mathbf{x} = \hat{x}_{k-1|k-1}}
    \end{equation*}

     substituting values and taking respective partial derivatives, we get:
    \begin{equation*}
         F_k = \begin{bmatrix}
         1 & 0 & -Tv_k \sin{\phi_{k-1}} \\
         0 & 1 & Tv_k \cos{\phi_{k-1}} \\
         0 & 0 & 1
         \end{bmatrix}_{\mathbf{x} = \hat{x}_{k-1|k-1}}
    \end{equation*}
\end{enumerate}

\subsubsection{State Prediction}
\begin{enumerate}
    \item
    Pseudo function for this step is:
     \begin{lstlisting}
        [xhat_predict, P_predict] = state_predict(xhat_km1, P_km1, F_k,  Q_k, vk, wk, T)
    \end{lstlisting}
    \item The mean is predicted by substituting $\hat{x}_{k-1|k-1}$ directly in $f$ of equation (5)
    \item The co-variance is predicted as described earlier.

\end{enumerate}

\subsubsection{Linearization}
\begin{enumerate}
    \item
    The pseudo function for this step is as follows:
    \begin{lstlisting}
        H_k = H_jacobian(xhat_predict)
    \end{lstlisting}

    \item The jacobian of $h$ is computed similar to what was described earlier. It comes out to be:
    \begin{equation*}
         H_k = \begin{bmatrix}
         -\frac{x_k}{\sqrt{x_k^2+y_k^2} } & \frac{x_k}{\sqrt{x_k^2+y_k^2} } & 0 \\
         \frac{-y_k}{x_k^2+y_k^2} & \frac{x_k}{x_k^2+y_k^2} & 0
         \end{bmatrix}_{\mathbf{x} = \hat{x}_{k|k-1}}
    \end{equation*}
\end{enumerate}

\subsubsection{Measurement Prediction}

    The pseudo function for this step is as follows:
    \begin{lstlisting}
        [yhat_last,K_k] = measurement_predict(xhat_predict, H_k, P_predict, R_k)
    \end{lstlisting}

\subsubsection{Estimation}
    The pseudo function for this step is as follows:
    \begin{lstlisting}
        [xhat_optimal,P_optimal] = estimate(y_k, yhat_predict, P_predict, H_k, K_k)
    \end{lstlisting}

\subsection*{Results}
The true trajectory, sensor observations and predicted trajectory are plotted in figure 1. As can be observed, the EKF algorithm is able to rightly and accurately able to localize the non-linear object.
\begin{figure}[h]
    \centering
    \includegraphics[scale = 1.0]{results.eps}
    \caption{Trajectory of Non linear Object}
    \label{fig:my_label}
\end{figure}

\end{document}