% Lecture for ph2a Caltech 2017: Vibrations and Waves
\documentclass[pdf,hideothersubsections]{beamer}
\usepackage{beamerthemeshadow}
\mode<presentation>
  {
    \usefonttheme{structuresmallcapsserif}
    \usetheme{CambridgeUS}
    \usecolortheme{seahorse}
    %\useinnertheme{circles}
%    \useoutertheme{tree}
  }

\usepackage{svg}
\usepackage{xmpmulti}

\usepackage{hyperref}
\hypersetup{
    pdffitwindow=true,     % window fit to page when opened
    colorlinks=true,       % false: boxed links; true: colored links
    linkcolor=orange,          % color of internal links (change box color with linkbordercolor)
    citecolor=green,        % color of links to bibliography
    filecolor=magenta,      % color of file links
    urlcolor=blue,           % color of external links
   pdfstartview={Fit}
}

% Fonts/encoding
\renewcommand{\UrlFont}{\tiny}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
%\usepackage[sc,medium,raggedright]{titlesec}
\usepackage{newtxmath}
%\usepackage{libertine}
\usepackage[osf]{ebgaramond}

\graphicspath{{Figures/}}

\begin{document}
\title{Coupled Oscillators}
\author{Caltech: ph2a}
\date{10 - Oct - 2017}
%\logo{\includegraphics[height=0.5cm]{../caltech_logo.png}}

\frame{\titlepage}

\frame{\frametitle{Table of contents}\tableofcontents}

\setbeamerfont{footnote}{size=\tiny}

\section{Previous Summary}
\begin{frame}
\frametitle{Recently}
\begin{itemize}
\item Complex Solutions to harmonic oscillator
\item Driven Oscillators (Wine Glass demo...)
\item Quality Factor
\item Ombuds Meeting
\end{itemize}
\end{frame}

\section{Harmony and a Wine Glass}
\begin{frame}
\frametitle{Resonant Ampification}
How much amplification do we get from a resonant system?
\pause
\begin{enumerate}
\item For a usual free mass, driven sinusoidally, $F = m a \implies x = - (F_0/m) / \omega^2$
\pause
\item For a resonant mass-spring, we have instead: $A =
  \frac{F_0/m}{\omega_0^2 + i \omega \frac{b}{m} - \omega^2}$
\pause
\item At the resonant frequency, $\omega = \omega_0$, so $A =
  \frac{F_0/m}{i \omega \frac{b}{m}}$ or, substituting our expression
  for Q,...
\pause
\item we see the amplification factor is just $Q$:  $A = -i Q \frac{F_0/m}{w_0^2}$

\end{enumerate}

\end{frame}

\begin{frame}
\frametitle{The Quality Factor: II}

\begin{columns}
\begin{column}{0.4\textwidth}
\pause
\begin{block}{Definition II:}
\centering
$Q \equiv \frac{\omega_R}{\Delta \omega}$
\end{block}
\pause
where $\Delta \omega$ is defined as the width of the peak at the
points at which: \\
\centering
$|A(\omega)|^2 = \frac{1}{2} |A_{max}|^2$

\end{column}

\pause
\begin{column}{0.6\textwidth}
\centering
\includegraphics[width=0.8\textwidth]{DrivenSHO.pdf}

\end{column}
\end{columns}
\pause
DEMO: Wine glass with a loudspeaker, strobe light, and \emph{laser}.
\end{frame}

\section{Coupled Oscillator}
\begin{frame}
\frametitle{Coupled Oscillators: Overview}
\pause
\begin{enumerate}
\item For free vibrations, $m \ddot{x} + b \dot{x} + k x = 0$.
\pause
\item In a coupled systems, there can be several springs and several
  masses with many inter-connections.
\pause
\item Also applies in the case of complicated circuits.
\pause
\item Matrix methods are very handy in solving systems of coupled
  differential equations.
\end{enumerate}
\end{frame}

\subsection{Examples}
\begin{frame}
\frametitle{Examples}

\begin{itemize}
\item 2 masses, 3 springs (Airtrack Demo)
\pause
\item 2 Pendula coupled by a weak spring

\end{itemize}

\end{frame}

% \begin{frame}
% \frametitle{More Examples}

% \begin{table}
% \centering
%     \resizebox{\columnwidth}{!}{%
%     \begin{tabular}{|l|c|r|}
%       \hline
%     Item Name                     & Frequency (Hz)  & Quality Factor \\ \hline
%     Strings                       & 10       & 0.5      \\
%     Big Bang                & 330      & 1500     \\
%    The Sun                   &         &          \\
%   Lasers                         &         &          \\
%    Mass on Track (w/ magnetic damping)   &         &          \\
%   Suspension Bridges (e.g. Golden Gate)  &         &           \\
%   Ionosphere (Schumann resonances)        & 8-30   & 3-5       \\
%     Stop Sign (hit with a rubber mallet) & 200      & 1000     \\
%     ${}_0S_0$ Dilational Mode of the Earth     & 0.0008   & 3000     \\
%     75 mm silicon wafer                  & 800      & 50,000   \\
%     Quartz Crystal Resonator             & 10$^6$   & $10^4$\,--\,$10^6$ \\
%     Large, High Purity Glass             & 3000     & 10$^8$   \\ \hline
%     \end{tabular}}
% \end{table}

% \end{frame}

\subsection{Equations of Motion}
\begin{frame}
\frametitle{Two Masses + Three Springs}
\begin{columns}

  \begin{column}{0.5\textwidth}
\pause
    \includegraphics[width=0.85\textwidth]{2masses3springs-cdf.jpg}
\url{http://demonstrations.wolfram.com/MotionOfTwoMassesConnectedBySprings/}
  \end{column}

  \begin{column}{0.5\textwidth}
    \pause
    Forces on Mass 1:
    \begin{equation}
      F_1 = m \ddot{x_1} = -k x_1 + k (x_2 - x_1)
    \end{equation}
    \pause
    Forces on Mass 2:
    \begin{equation}
      F_2 = m \ddot{x_2} = -k x_2 - k (x_2 - x_1)
    \end{equation}
    \pause
    2 Eqs \& 2 Unknowns:
    \begin{align}
      m \ddot{x_1} + 2 k x_1 - k x_2 &= 0 \\
      m \ddot{x_2} + 2 k x_2 - k x_1 &= 0
    \end{align}
  \end{column}

\end{columns}
\end{frame}

\begin{frame}
\frametitle{Two Masses + Three Springs}
    \pause
\begin{enumerate}
\item For SHO, we guessed $\tilde{x} = A e^{i (\omega t + \phi_0)}$
\pause
\item For CHO, we use $\tilde{x_1} = A_1 e^{i \omega t}$ and
  $\tilde{x_2} = A_2 e^{i \omega t}$; assuming $A_1$ and $A_2$ are complex.
\pause
\item Plugging into Eqs. 3 \& 4, we get: \\
\begin{align*}
\Big[ -\omega^2 A_1 + 2 \frac{k}{m}A_1 - \frac{k}{m}A_2 \Big] e^{i
  \omega t} &= 0 \\
\Big[ -\omega^2 A_2 + 2 \frac{k}{m}A_2 - \frac{k}{m}A_1 \Big] e^{i
  \omega t} &= 0
\end{align*}
\pause
\item Divide both sides by $e^{i \omega t}$ and using $\omega_0^2 =
  k/m$, we get:
\pause
\begin{align*}
\Big[ -\omega^2 A_1 + 2 \omega_0^2 A_1 - \omega_0^2 A_2 \Big] &= 0 \\
\Big[ -\omega^2 A_2 + 2 \omega_0^2 A_2 - \omega_0^2 A_1 \Big] &= 0
\end{align*}

\end{enumerate}
\end{frame}

\section{Eigenfrequencies}
\begin{frame}
\frametitle{Matrix Methods: Eigenvalues}
\pause
Rewrite in matrix form to allow using linear algebra: \\
\[
\begin{bmatrix}
-\omega^2 + 2 \omega_0^2 & -\omega_0^2 \\
-\omega_0^2 & -\omega^2 + 2 \omega_0^2
\end{bmatrix}
\begin{bmatrix}
A_1 \\
A_2
\end{bmatrix}
=
\begin{bmatrix}
0 \\
0
\end{bmatrix}
\]
\pause
Equivalent to setting the determinant equal to zero:
\[
\begin{vmatrix}
-\omega^2 + 2 \omega_0^2 & -\omega_0^2 \\
-\omega_0^2 & -\omega^2 + 2 \omega_0^2
\end{vmatrix}
= 0
\]
\pause
which gives us the following quadratic equation:
\pause
$(-\omega^2 + 2 \omega_0^2)^2 -\omega_0^4 = 0$ \\
\pause
and solving for $\omega$, we have
\centering
\pause
$\omega^2 = 2 \omega_0^2 \pm \omega_0^2$ \\
\pause
so $\omega = (\omega_0, \sqrt{3} \omega_0)$

\end{frame}

\section{Eigenmodes}
\begin{frame}
\frametitle{Eigen (Normal) Modes}
\pause
Plugging the eigenfrequencies back into our equations, we get:
\pause
\begin{align}
\omega = \omega_0 :  -\omega_0^2 + 2 \omega_0^2 A_1 - \omega_0^2 A_2 &=
0 \implies A_1 = A_2\\
\omega = \sqrt{3} \omega_0 :  -3 \omega_0^2 + 2 \omega_0^2 A_1 - \omega_0^2 A_2 &=
0 \implies A_1 = - A_2
\end{align}
\pause
These are the \emph{symmetric} and \emph{anti-symmetric} solutions, respectively.

\end{frame}

% \section{Beats}
% \begin{frame}
% \frametitle{Beats: Examples}
% \end{frame}

\section{Summary}
\begin{frame}
\frametitle{Properties of Eigenmodes}
\begin{enumerate}
\pause
\item $\omega_{1,i}=\omega_{2,i}=\omega_i$~(for the i$^{th}$ mode)
\pause
\item $A_{1,i}, A_{2,i} \ne f(t)$; i.e. amplitudes are constant
\pause
\item A's are constant, so the relative phases of the modes are
  constant
\pause
\item \underline{Stable}: w/o friction the oscillations go on forever
\pause
\item \textbf{Complete}: Any motion can be decomposed into a sum of
  normal modes.

\end{enumerate}
\end{frame}

\end{document}


\documentclass[aos]{imsart}

%% Packages
\RequirePackage{amsthm,amsmath,amsfonts,amssymb}
\RequirePackage[numbers]{natbib}
%\RequirePackage[authoryear]{natbib}%% uncomment this for author-year citations
\RequirePackage[colorlinks,citecolor=blue,urlcolor=blue]{hyperref}
\RequirePackage{graphicx}

\startlocaldefs
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%                                          %%
%% Uncomment next line to change            %%
%% the type of equation numbering           %%
%%                                          %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\numberwithin{equation}{section}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%                                          %%
%% For Axiom, Claim, Corollary, Hypothesis, %%
%% Lemma, Theorem, Proposition              %%
%% use \theoremstyle{plain}                 %%
%%                                          %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\theoremstyle{plain}
\newtheorem{axiom}{Axiom}
\newtheorem{claim}[axiom]{Claim}
\newtheorem{theorem}{Theorem}[section]
\newtheorem{lemma}[theorem]{Lemma}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%                                          %%
%% For Assumption, Definition, Example,     %%
%% Notation, Property, Remark, Fact         %%
%% use \theoremstyle{remark}                %%
%%                                          %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\theoremstyle{remark}
\newtheorem{definition}[theorem]{Definition}
\newtheorem*{example}{Example}
\newtheorem*{fact}{Fact}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Please put your definitions here:        %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\endlocaldefs

\begin{document}

\begin{frontmatter}
\title{A sample article title}
%\title{A sample article title with some additional note\thanksref{t1}}
\runtitle{A sample running head title}
%\thankstext{T1}{A sample additional note to the title.}

\begin{aug}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Only one address is permitted per author. %%
%% Only division, organization and e-mail is %%
%% included in the address.                  %%
%% Additional information can be included in %%
%% the Acknowledgments section if necessary. %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\author[A]{\fnms{First} \snm{Author}\ead[label=e1]{first@somewhere.com}},
\author[B]{\fnms{Second} \snm{Author}\ead[label=e2,mark]{second@somewhere.com}}
\and
\author[B]{\fnms{Third} \snm{Author}\ead[label=e3,mark]{third@somewhere.com}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Addresses                                %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\address[A]{Department,
University or Company Name,
\printead{e1}}

\address[B]{Department,
University or Company Name,
\printead{e2,e3}}
\end{aug}

\begin{abstract}
The abstract should summarize the contents of the paper.
It should be clear, descriptive, self-explanatory and not longer
than 200 words. It should also be suitable for publication in
abstracting services. Formulas should be used as sparingly as
possible within the abstract. The abstract should not make
reference to results, bibliography or formulas in the body
of the paper---it should be self-contained.

This is a sample input file.  Comparing it with the output it
generates can show you how to produce a simple document of
your own.
\end{abstract}

\begin{keyword}[class=MSC]
\kwd[Primary ]{00X00}
\kwd{00X00}
\kwd[; secondary ]{00X00}
\end{keyword}

\begin{keyword}
\kwd{First keyword}
\kwd{second keyword}
\end{keyword}

\end{frontmatter}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Please use \tableofcontents for articles %%
%% with 50 pages and more                   %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\tableofcontents

\section{Introduction}

This template helps you to create a properly formatted \LaTeXe\ manuscript.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% `\ ' is used here because TeX ignores    %%
%% spaces after text commands.              %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Prepare your paper in the same style as used in this sample .pdf file.
Try to avoid excessive use of italics and bold face.
Please do not use any \LaTeXe\ or \TeX\ commands that affect the layout
or formatting of your document (i.e., commands like \verb|\textheight|,
\verb|\textwidth|, etc.).

\section{Section headings}
Here are some sub-sections:
\subsection{A sub-section}
Regular text.
\subsubsection{A sub-sub-section}
Regular text.

\section{Text}
\subsection{Lists}

The following is an example of an \emph{itemized} list,
two levels deep.
\begin{itemize}
\item
This is the first item of an itemized list.  Each item
in the list is marked with a ``tick.''  The document
style determines what kind of tick mark is used.
\item
This is the second item of the list.  It contains another
list nested inside it.
\begin{itemize}
\item This is the first item of an itemized list that
is nested within the itemized list.
\item This is the second item of the inner list.  \LaTeX\
allows you to nest lists deeper than you really should.
\end{itemize}
This is the rest of the second item of the outer list.
\item
This is the third item of the list.
\end{itemize}

The following is an example of an \emph{enumerated} list of one level.

\begin{longlist}
\item This is the first item of an enumerated list.
\item This is the second item of an enumerated list.
\end{longlist}

The following is an example of an \emph{enumerated} list, two levels deep.
\begin{longlist}
\item[1.]
This is the first item of an enumerated list.  Each item
in the list is marked with a ``tick.''  The document
style determines what kind of tick mark is used.
\item[2.]
This is the second item of the list.  It contains another
list nested inside of it.
\begin{longlist}
\item
This is the first item of an enumerated list that
is nested within.
\item
This is the second item of the inner list.  \LaTeX\
allows you to nest lists deeper than you really should.
\end{longlist}
This is the rest of the second item of the outer list.
\item[3.]
This is the third item of the list.
\end{longlist}

\subsection{Punctuation}
Dashes come in three sizes: a hyphen, an intra-word dash like ``$U$-statistics'' or ``the time-homogeneous model'';
a medium dash (also called an ``en-dash'') for number ranges or between two equal entities like ``1--2'' or ``Cauchy--Schwarz inequality'';
and a punctuation dash (also called an ``em-dash'') in place of a comma, semicolon,
colon or parentheses---like this.

Generating an ellipsis \ldots\ with the right spacing
around the periods requires a special command.

\section{Fonts}
Please use text fonts in text mode, e.g.:
\begin{itemize}
\item[]\textrm{Roman}
\item[]\textit{Italic}
\item[]\textbf{Bold}
\item[]\textsc{Small Caps}
\item[]\textsf{Sans serif}
\item[]\texttt{Typewriter}
\end{itemize}
Please use mathematical fonts in mathematical mode, e.g.:
\begin{itemize}
\item[] $\mathrm{ABCabc123}$
\item[] $\mathit{ABCabc123}$
\item[] $\mathbf{ABCabc123}$
\item[] $\boldsymbol{ABCabc123\alpha\beta\gamma}$
\item[] $\mathcal{ABC}$
\item[] $\mathbb{ABC}$
\item[] $\mathsf{ABCabc123}$
\item[] $\mathtt{ABCabc123}$
\item[] $\mathfrak{ABCabc123}$
\end{itemize}
Note that \verb|\mathcal, \mathbb| belongs to capital letters-only font typefaces.

\section{Notes}
Footnotes\footnote{This is an example of a footnote.}
pose no problem.\footnote{Note that footnote number is after punctuation.}

\section{Quotations}

Text is displayed by indenting it from the left margin. There are short quotations
\begin{quote}
This is a short quotation.  It consists of a
single paragraph of text.  There is no paragraph
indentation.
\end{quote}
and longer ones.
\begin{quotation}
This is a longer quotation.  It consists of two paragraphs
of text.  The beginning of each paragraph is indicated
by an extra indentation.

This is the second paragraph of the quotation.  It is just
as dull as the first paragraph.
\end{quotation}

\section{Environments}

\subsection{Examples for \emph{\texttt{plain}}-style environments}
\begin{axiom}\label{ax1}
This is the body of Axiom \ref{ax1}.
\end{axiom}

\begin{proof}
This is the body of the proof of the axiom above.
\end{proof}

\begin{claim}\label{cl1}
This is the body of Claim \ref{cl1}. Claim \ref{cl1} is numbered after
Axiom \ref{ax1} because we used \verb|[axiom]| in \verb|\newtheorem|.
\end{claim}

\begin{theorem}\label{th1}
This is the body of Theorem \ref{th1}. Theorem \ref{th1} numbering is
dependent on section because we used \verb|[section]| after \verb|\newtheorem|.
\end{theorem}

\begin{theorem}[Title of the theorem]\label{th2}
This is the body of Theorem \ref{th2}. Theorem \ref{th2} has additional title.
\end{theorem}

\begin{lemma}\label{le1}
This is the body of Lemma \ref{le1}. Lemma \ref{le1} is numbered after
Theorem \ref{th2} because we used \verb|[theorem]| in \verb|\newtheorem|.
\end{lemma}

\begin{proof}[Proof of Lemma \ref{le1}]
This is the body of the proof of Lemma \ref{le1}.
\end{proof}

\subsection{Examples for \emph{\texttt{remark}}-style environments}
\begin{definition}\label{de1}
This is the body of Definition \ref{de1}. Definition \ref{de1} is numbered after
Lemma \ref{le1} because we used \verb|[theorem]| in \verb|\newtheorem|.
\end{definition}

\begin{example}
This is the body of the example. Example is unnumbered because we used \verb|\newtheorem*|
instead of \verb|\newtheorem|.
\end{example}

\begin{fact}
This is the body of the fact. Fact is unnumbered because we used \verb|\newtheorem*|
instead of \verb|\newtheorem|.
\end{fact}

\section{Tables and figures}
Cross-references to labeled tables: As you can see in Table~\ref{sphericcase}
and also in Table~\ref{parset}.

\begin{table*}
\caption{The spherical case ($I_1=0$, $I_2=0$)}
\label{sphericcase}
\begin{tabular}{@{}lrrrrc@{}}
\hline
Equil. \\
points & \multicolumn{1}{c}{$x$}
& \multicolumn{1}{c}{$y$} & \multicolumn{1}{c}{$z$}
& \multicolumn{1}{c}{$C$} & S \\
\hline
$L_1$    & $-$2.485252241 & 0.000000000  & 0.017100631  & 8.230711648  & U \\
$L_2$    & 0.000000000  & 0.000000000  & 3.068883732  & 0.000000000  & S \\
$L_3$    & 0.009869059  & 0.000000000  & 4.756386544  & $-$0.000057922 & U \\
$L_4$    & 0.210589855  & 0.000000000  & $-$0.007021459 & 9.440510897  & U \\
$L_5$    & 0.455926604  & 0.000000000  & $-$0.212446624 & 7.586126667  & U \\
$L_6$    & 0.667031314  & 0.000000000  & 0.529879957  & 3.497660052  & U \\
$L_7$    & 2.164386674  & 0.000000000  & $-$0.169308438 & 6.866562449  & U \\
$L_8$    & 0.560414471  & 0.421735658  & $-$0.093667445 & 9.241525367  & U \\
$L_9$    & 0.560414471  & $-$0.421735658 & $-$0.093667445 & 9.241525367  & U \\
$L_{10}$ & 1.472523232  & 1.393484549  & $-$0.083801333 & 6.733436505  & U \\
$L_{11}$ & 1.472523232  & $-$1.393484549 & $-$0.083801333 & 6.733436505  & U \\
\hline
\end{tabular}
\end{table*}

\begin{table}
\caption{Sample posterior estimates for each model}
\label{parset}
%
\begin{tabular}{@{}lcrcrrr@{}}
\hline
&& & &\multicolumn{3}{c}{Quantile} \\
\cline{5-7}
Model &Parameter &
\multicolumn{1}{c}{Mean} &
Std. dev.&
\multicolumn{1}{c}{2.5\%} &
\multicolumn{1}{c}{50\%}&
\multicolumn{1}{c@{}}{97.5\%} \\
\hline
{Model 0} & $\beta_0$ & $-$12.29 & 2.29 & $-$18.04 & $-$11.99 & $-$8.56 \\
          & $\beta_1$  & 0.10   & 0.07 & $-$0.05  & 0.10   & 0.26  \\
          & $\beta_2$   & 0.01   & 0.09 & $-$0.22  & 0.02   & 0.16  \\[6pt]
{Model 1} & $\beta_0$   & $-$4.58  & 3.04 & $-$11.00 & $-$4.44  & 1.06  \\
          & $\beta_1$   & 0.79   & 0.21 & 0.38   & 0.78   & 1.20  \\
          & $\beta_2$   & $-$0.28  & 0.10 & $-$0.48  & $-$0.28  & $-$0.07 \\[6pt]
{Model 2} & $\beta_0$   & $-$11.85 & 2.24 & $-$17.34 & $-$11.60 & $-$7.85 \\
          & $\beta_1$   & 0.73   & 0.21 & 0.32   & 0.73   & 1.16  \\
          & $\beta_2$   & $-$0.60  & 0.14 & $-$0.88  & $-$0.60  & $-$0.34 \\
          & $\beta_3$   & 0.22   & 0.17 & $-$0.10  & 0.22   & 0.55  \\
\hline
\end{tabular}
%
\end{table}

\begin{figure}
\includegraphics{figure1}
\caption{Pathway of the penicillin G biosynthesis.}
\label{penG}
\end{figure}

Sample of cross-reference to figure.
Figure~\ref{penG} shows that it is not easy to get something on paper.

\section{Equations and the like}

Two equations:
\begin{equation}
    C_{s}  =  K_{M} \frac{\mu/\mu_{x}}{1-\mu/\mu_{x}} \label{ccs}
\end{equation}
and
\begin{equation}
    G = \frac{P_{\mathrm{opt}} - P_{\mathrm{ref}}}{P_{\mathrm{ref}}}  100(\%).
\end{equation}

Equation arrays:
\begin{eqnarray}
  \frac{dS}{dt} & = & - \sigma X + s_{F} F,\\
  \frac{dX}{dt} & = &   \mu    X,\\
  \frac{dP}{dt} & = &   \pi    X - k_{h} P,\\
  \frac{dV}{dt} & = &   F.
\end{eqnarray}
One long equation:
\begin{eqnarray}
 \mu_{\text{normal}} & = & \mu_{x} \frac{C_{s}}{K_{x}C_{x}+C_{s}}  \nonumber\\
                     & = & \mu_{\text{normal}} - Y_{x/s}\bigl(1-H(C_{s})\bigr)(m_{s}+\pi /Y_{p/s})\\
                     & = & \mu_{\text{normal}}/Y_{x/s}+ H(C_{s}) (m_{s}+ \pi /Y_{p/s}).\nonumber
\end{eqnarray}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Example with single Appendix:            %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{appendix}
\section*{Title}\label{appn} %% if no title is needed, leave empty \section*{}.
Appendices should be provided in \verb|{appendix}| environment,
before Acknowledgements.

If there is only one appendix,
then please refer to it in text as \ldots\ in the \hyperref[appn]{Appendix}.
\end{appendix}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Example with multiple Appendixes:        %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{appendix}
\section{Title of the first appendix}\label{appA}
If there are more than one appendix, then please refer to it
as \ldots\ in Appendix \ref{appA}, Appendix \ref{appB}, etc.

\section{Title of the second appendix}\label{appB}
\subsection{First subsection of Appendix \protect\ref{appB}}

Use the standard \LaTeX\ commands for headings in \verb|{appendix}|.
Headings and other objects will be numbered automatically.
\begin{equation}
\mathcal{P}=(j_{k,1},j_{k,2},\dots,j_{k,m(k)}). \label{path}
\end{equation}

Sample of cross-reference to the formula (\ref{path}) in Appendix \ref{appB}.
\end{appendix}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Support information, if any,             %%
%% should be provided in the                %%
%% Acknowledgements section.                %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{acks}[Acknowledgments]
The authors would like to thank the anonymous referees, an Associate
Editor and the Editor for their constructive comments that improved the
quality of this paper.
\end{acks}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Funding information, if any,             %%
%% should be provided in the                %%
%% funding section.                         %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{funding}
The first author was supported by NSF Grant DMS-??-??????.

The second author was supported in part by NIH Grant ???????????.
\end{funding}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Supplementary Material, including data   %%
%% sets and code, should be provided in     %%
%% {supplement} environment with title      %%
%% and short description. It cannot be      %%
%% available exclusively as external link.  %%
%% All Supplementary Material must be       %%
%% available to the reader on Project       %%
%% Euclid with the published article.       %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{supplement}
\stitle{Title of Supplement A}
\sdescription{Short description of Supplement A.}
\end{supplement}
\begin{supplement}
\stitle{Title of Supplement B}
\sdescription{Short description of Supplement B.}
\end{supplement}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%                  The Bibliography                       %%
%%                                                         %%
%%  imsart-???.bst  will be used to                        %%
%%  create a .BBL file for submission.                     %%
%%                                                         %%
%%  Note that the displayed Bibliography will not          %%
%%  necessarily be rendered by Latex exactly as specified  %%
%%  in the online Instructions for Authors.                %%
%%                                                         %%
%%  MR numbers will be added by VTeX.                      %%
%%                                                         %%
%%  Use \cite{...} to cite references in text.             %%
%%                                                         %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%% if your bibliography is in bibtex format, uncomment commands:
%\bibliographystyle{imsart-number} % Style BST file (imsart-number.bst or imsart-nameyear.bst)
%\bibliography{bibliography}       % Bibliography file (usually '*.bib')

%% or include bibliography directly:
\begin{thebibliography}{4}
%%
\bibitem{r1}
\textsc{Billingsley, P.} (1999). \textit{Convergence of
Probability Measures}, 2nd ed.
Wiley, New York.

\bibitem{r2}
\textsc{Bourbaki, N.}  (1966). \textit{General Topology}  \textbf{1}.
Addison--Wesley, Reading, MA.

\bibitem{r3}
\textsc{Ethier, S. N.} and \textsc{Kurtz, T. G.} (1985).
\textit{Markov Processes: Characterization and Convergence}.
Wiley, New York.

\bibitem{r4}
\textsc{Prokhorov, Yu.} (1956).
Convergence of random processes and limit theorems in probability
theory. \textit{Theory  Probab.  Appl.}
\textbf{1} 157--214.
\end{thebibliography}

\end{document}


\documentclass{article}
\usepackage{fullpage}

\begin{document}

\title{Haskell port of ``Handbook of Practical Logic and Automated Reasoning''}
\author{Sean McLaughlin}
\maketitle \pagestyle{plain} % page numbers

\section{Introduction}

This document describes a port of the Objective Caml code supporting
John Harrison's logic textbook ``Handbook of Practical Logic and
Automated Reasoning'' to Haskell.

The code is written in literate Haskell style. We kept the file names
of the original mostly intact, though use Haskell naming conventions.
As an experiment, in some places we inlined the book text. We think
this is a nice way to present the code, but didn't have the patience
to do it systematically. Perhaps we will do this when the text is
finalized.

One minor difference is in the moderate use of type classes which
makes some of the functionality much cleaner, esp. the manipulation
of polynomials, which can be achieved through Haskell binary operators
instead of explicit term manipulation.

To interact with the port, we wrote our own interpreter for reasons
given in the next section.

\subsection{Interpreter}
While Haskell and OCaml are quite
similar, the port is complicated by the mode of interaction used in
Harrison's code. Harrison uses the OCaml top level interpreter
as a basis for experimentation. Additionally, his syntax for
formulas includes

\begin{verbatim}
A \/ B /\ C
\end{verbatim}

\noindent for ``$A$ or $B$ and $C$''. The backslash characters pose a
difficulty. For example:

\begin{verbatim}
$ ocaml
        Objective Caml version 3.09.3

# let s = "A \/ B";;
Warning X: illegal backslash escape in string.
val s : string = "A \\/ B"
\end{verbatim}

\noindent As you see, in Ocaml strings can only contain backslashes when they
are ``escaped'' with yet another backslash. Indeed, as in most other
languages (including Haskell), backslash is used to type unprintable
or extended characters which are translated by the OCaml parser to the
relevant ASCII or Unicode symbols.
Note that OCaml only issues a warning when you type such
a backslash, but this is still a problem:

\begin{verbatim}
$ ocaml
        Objective Caml version 3.09.3

# "A /\ B";;
- : string = "A / B"
\end{verbatim}

\noindent An escaped space is certainly not what you meant here!
When we try this experiment using GHCi (the interpreter of GHC, a
Haskell compiler) we get an error.

\begin{verbatim}
$ ghci
GHCi, version 6.8.2: http://www.haskell.org/ghc/  :? for help
Loading package base ... linking ... done.
Prelude> "A \/ B";
<interactive>:1:5: lexical error in string/character literal at character ' '
\end{verbatim}

\noindent Harrison avoids the difficulty by using the Camlp4 preprocessing tool.
Using Camlp4, new types of quotations can be invented, and parsed
however you like. In particular, he can treat backslash as a simple
backslash and not as an escape character. The default Camlp4 syntax
for quotations is \texttt{<<string>>}, which Harrison uses in his examples:

\begin{verbatim}
# <<A /\ B>>;;
- : fol formula = <<A /\ B>>
# <<A \/ B>>;;
- : fol formula = <<A \/ B>>
\end{verbatim}

\noindent Having no such tool for GHC, how do we proceed?  There are a number
of options.

\begin{enumerate}
\item Change the syntax of the examples to something the top level
  can parse, for instance
\begin{verbatim}
"A & B | C"
\end{verbatim}
\item Escape every backslash
\begin{verbatim}
A /\\ B \\/ C
\end{verbatim}
\item Write your own top level.  Then you can parse a string
any way you like, as Camlp4 does.
\end{enumerate}

\noindent We decided upon the third option so as to keep the syntax close
to the original.  This means, however, that we can not do
experiments in the same way Harrison does.  He simply types
commands at the OCaml top level

\begin{verbatim}
# resolution <<(exists y. forall x. P(x, y)) ==> forall x. exists y. P(x, y)>>;;
1 used; 1 unused.
- : bool list = [true]
\end{verbatim}

\noindent We write

\begin{verbatim}
$ atp
> resolution -f <<(exists y. forall x. P(x, y)) ==> forall x. exists y. P(x, y)>>
<<(exists y. forall x. P(x, y)) ==>
  (forall x. exists y. P(x, y))>>
0 used; 2 unused.
1 used; 1 unused.
Solution found!
Computation time: 0.000 sec
\end{verbatim}

\noindent The \texttt{-f} means "parse what follows as a formula".  We also have
a large file of stored formulas that can be referenced by name.  For
example, Pelletier's problems can be used as

\begin{verbatim}
> resolution p1
<<p ==> q <=> ~q ==> ~p>>
Solution found!
Computation time: 0.000 sec
\end{verbatim}

\noindent As a result, we can't experiment quite as freely in Haskell as in
OCaml.  For instance, trying a different normalization function
in a decision procedure involves killing the top level, recompiling,
and starting the top level again.  However, we do think writing your
own top level has advantages of its own.

\section{The ATP Interpreter}

Experimenting with the code is done through the \emph{ATP interpreter}.
After building the interpreter:

\begin{verbatim}
$ make
ghc  -fwarn-deprecations -fwarn-dodgy-imports -fwarn-hi-shadowing -fwarn-implicit-prelude -fwarn-incomplete-patterns -fwarn-missing-fields -fwarn-missing-methods -fwarn-missing-signatures -fwarn-orphans -fwarn-overlapping-patterns -fwarn-monomorphism-restriction -fwarn-tabs -fwarn-type-defaults -fwarn-unused-binds -fwarn-unused-imports -fwarn-unused-matches -fno-warn-incomplete-record-updates -fno-warn-simple-patterns   -o atp --make Main.lhs
[ 1 of 33] Compiling UnionFind        ( UnionFind.lhs, UnionFind.o )
[ 2 of 33] Compiling Lib              ( Lib.lhs, Lib.o )
...
Linking atp ...
\end{verbatim}

you can start it by typing

\begin{verbatim}
$ atp
>
\end{verbatim}

Now you are ready to issue commands.  If you just type \texttt{<return>}
or \texttt{help} you will get a list of all available interpreter commands.

\begin{verbatim}
> help
Possible commands:
   === Util ===

      help                : this message
      echo                : echo the inputs
      bug                 : bug of the moment

   === Parsing ===

      parseE              : parse an arithmetic expression and print it
      parseP              : parse a propositional formula and print it
      parseT              : parse a first order term and print it
      parseF              : parse a first order formula and print it

   === Propositional formulas ===

      nnf                 : negation normal form
      cnf                 : conjunctive normal form
      dnf                 : disjunctive normal form
      defcnf              : definitional cnf
      truthtable          : show propositional truth table

   === Propositional decision procedures ===

      tautology           : Tautology checker via truth tables
      dp                  : Davis-Putnam procedure (propositional)
      dpll                : Davis-Putnam-Loveland-Logemann procedure (propositional)

   === First order formulas ===

      showFol             : show first order test formula
      pnf                 : prenex normal form
      skolemize           : skolem normal form

   === Basic Herbrand methods ===

      gilmore             : Gilmore procedure
      davisputnam         : Davis-Putname procedure

   === Unification ===

      unify               : unify two terms

   === Tableaux ===

      prawitz             : Prawitz procedure
      tab                 : Analytic tableaux procedure
      splittab            : Analytic tableaux procedure

   === Resolution ===

      basicResolution     : Basic resolution procedure
      resolution          : Resolution with subsumption
      positiveResolution  : Postive resolution
      sosResolution       : Set-of-support resolution

   === Prolog ===

      hornprove           : Basic horn clause prover using backchaining
      prolog              : Prolog interpreter

   === MESON ===

      basicMeson          : Basic Meson procedure
      meson               : Optimized Meson procedure

   === Equality ===

      ccvalid             : Congruence closure validity
      rewrite             : Rewriting
      bmeson              : Meson with equality elimination
      paramodulation      : Paramodulation

   === Decidable problems ===

      aedecide            : Decide AE problems
      dloQelim            : Dense Linear Orders
      integerQelim        : Presburger arithmetic
      nelopInt            : Nelson Oppen

>
\end{verbatim}

When a new algorithm is implemented, the top level is extended by adding
a clause to \texttt{Main.lhs}.  For example, when resolution was finished,
we added

\begin{verbatim}
> import qualified Resolution
\end{verbatim}

to the preamble and

\begin{verbatim}
> resolution :: Command
> resolution = ("resolution",
>                "Resolution with subsumption",
>                runfol Resolution.resolution)
\end{verbatim}

to the list of first order logic algorithms.  If one wishes to extend the
Haskell atp code with their own algorithms, it is fairly easy to see how
to add it to the interpreter.

\end{document}

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


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

\makeatletter

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
\def\labnb{0}
\usepackage{hyperref}
\usepackage{ulem}
\usepackage{latexsym}
\usepackage{amssymb}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{graphics}
\usepackage{lastpage}
\usepackage{titling}
\setlength{\droptitle}{-0.4in}

\linespread{1.1}

%%% HEADER
\usepackage[head=32pt,top=1in,bottom=1in,left=1in,right=1in]{geometry}
\usepackage{fancyhdr}
\setlength{\headheight}{24pt}
\pagestyle{fancy}
\lhead{CE 191: CEE Systems Analysis \\ University of California, Berkeley}
\rhead{Fall 2014 \\ Professor Scott Moura}
\cfoot{Page \thepage\ of \pageref{LastPage}}

\title{\Large Lab 2: Energy Portfolio Optimization}
\author{Due: Friday 10/3 at 2:00pm}
\date{}

\renewcommand{\headrulewidth}{0.5pt}
%\newcommand{\answer}[1]{\par\begin{center}\framebox{\parbox{5in}{{\bfseries \footnotesize Answer: }\textit{#1}}}\end{center}}
\newcommand{\answer}[1]{}
\newcommand{\inset}[2]{{\in\{{#1},\cdots,{#2}\} }}

\newcounter{question}
\setcounter{question}{1}
\def\newquestion{\textbf{Question \arabic{section}.\arabic{question}\ \ }\stepcounter{question}}

%\topmargin = -27 pt
%\leftmargin = -0.5 in
%\rightmargin= 1 in
%\oddsidemargin =  -0.10 in \textheight = 8.5 in \textwidth = 6.5in

\setlength{\parindent}{0mm}

\addtolength{\parskip}{0.5\baselineskip}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\maketitle
\thispagestyle{fancy}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Lab Overview}
In this lab, you will learn to formulate a quadratic program to find a mix of energy supplies
which minimizes the variability of the price of energy. Section \ref{sec:problem} provides a description of
the full problem and the constraints. In Section \ref{sec:implementation}, you are asked to form the quadratic
program, and implement it in MATLAB. Finally, in Section \ref{sec:addanalysis}, you will modify the quadratic
program to analyze various scenarios. \textbf{Please remember to submit your MATLAB
code (.m files in one ZIP file), and explain in the report how to run the code.}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Energy Portfolio Investment Problem}\label{sec:problem}

The California Public Utilities Commission (CPUC) is charged with strategically planning its energy generation over
the next several years to meet its growing energy demand. Currently, California generates
199 \textit{megawatt hours} (MWh) from a combination of eight sources. This collection, or mix, of
energy sources is known as an \textit{energy portfolio}. The contributions of eight sources to
California's current portfolio is listed in Table \ref{tbl:energymix}.

\begin{table}[h]
\caption{Current sources of energy to California, and percent contribution to the current energy mix. Source: \url{http://energyalmanac.ca.gov/electricity/total_system_power.html}}
\begin{center}
\begin{tabular}{|c || c|c|c|c|c|c|c|c|}
\hline
{\bfseries Source} & Coal & Hydro & Natural Gas & Nuclear & Biomass & Geo & Solar & Wind \\ \hline
{\bfseries 2012 energy mix (\%)} & 7.5 & 8.3 & 43.4 & 9.0 & 2.3 & 4.4 & 0.9 & 6.3  \\ \hline
\end{tabular}
\end{center}
\label{tbl:energymix}
\end{table}

By the year 2020, California must generate a peak of 225 MWh to meet its growing demand for energy. The expected price in 2020 in dollars per megawatt hour (USD/MWh) is given in Table 2.

\begin{table}[h]
\caption{Expected price of energy in California in the year 2020. Adopted from: Adopted from \url{http://www.eia.gov/forecasts/aeo/pdf/electricity_generation.pdf}}
\begin{center}
\begin{tabular}{|c || c|c|c|c|c|c|c|c|}
\hline
{\bfseries Source} & Coal & Hydro & Natural Gas & Nuclear & Biomass & Geo & Solar & Wind \\ \hline
{\bfseries Expected Price (\$/MWh)} & 100 & 90 & 130 & 108 & 111 & 90 & 144 & 87  \\ \hline
\end{tabular}
\end{center}
\label{tbl:energyprice}
\end{table}

Through technology improvements, California can expand its resources as required to meet future demands. However, the price of each energy source contains uncertainty. Namely, variations in fuel costs, technology development, and future environmental regulations in 2020 impose uncertainty on the expected prices listed in Table \ref{tbl:energyprice}. Suppose the standard deviation $\sigma$ of the prices for each source is given in Table \ref{tbl:std}\footnote{For simplicity, we are ignoring correlations in prices between sources.}.

\begin{table}[h]
\caption{Standard deviation of energy source prices in 2020.}
\begin{center}
\begin{tabular}{|c || c|c|c|c|c|c|c|c|}
\hline
{\bfseries Source} & Coal & Hydro & Natural Gas & Nuclear & Biomass & Geo & Solar & Wind \\ \hline
{\bfseries $\sigma$ (\$/MWh)} & 22 & 30 & 15 & 20 & 30 & 36 & 32 & 40  \\ \hline
\end{tabular}
\end{center}
\label{tbl:std}
\end{table}

The variance in price of an uncertain good (for example 1 MWh of electricity) can be used as a measure of the \textit{risk} of that good. Recall that variance is
\begin{equation*}
	\textrm{var} = \sigma^{2}.
\end{equation*}
Portfolio theory assumes that for a given level of risk, planners prefer lower costs to higher ones. Conversely, for a given expected cost, planners prefer less risk to more risk. By combining various goods in a portfolio, it is possible to create a portfolio with lower risk than any of the goods individually. This is known as \textit{diversification}. The same concepts hold for stock market portfolios.

To ensure competitive electricity rates, California must keep the expected cost of its energy portfolio under 100 USD/MWh. Therefore, California would like to determine an optimal energy portfolio. That is, determine the portfolio with the least risk for the given maximum expected cost.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Implementation}\label{sec:implementation}

\begin{enumerate}
\item Formulate a quadratic program (QP) that California can use to minimize the risk of obtaining its energy, while satisfying the maximum expected energy cost constraint described above.
\begin{enumerate}
\item Define your mathematical notation in a table. Be precise and organized.
\item Using this notation, formulate (i) the objective function and (ii) all the constraints.
\item Encode this formulation in matrices \texttt{Q, R, A, b}, where the QP is formulated as $\min \frac{1}{2} x^{T} Q x + R^{T} x$ subject to $A x \leq b$. Write down matrices \texttt{Q, R, A, b} in your report.
\item Is the Hessian $Q$ positive definite, negative definite, positive semi-definite, negative semi-definite, or indefinite? Note: the answer would not be trivial if we assumed correlations exist between the standard deviations of energy prices.
\end{enumerate}

\item Solve the QP that you have formulated using MATLAB's \texttt{quadprog} command (read the documentation). In your report, provide
\begin{itemize}
\item the value of the objective function, i.e. the risk,
\item the value of the decision variables at the optimum,
\item a qualitative description the optimal solution, including the active constraints.
\end{itemize}

\end{enumerate}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section{Additional Analysis}\label{sec:addanalysis}

The following questions study modifications to the original problem. Each question is independent from the other ones, i.e. the changes are not cumulative. Where applicable, provide (i) the objective function value, and (ii) the optimal decision variable values.

\begin{enumerate}
\setcounter{enumi}{2}
\item A concerned and conservative member of the CPUC (yup, they went to Leland StanfUrd JR. University), who is not familiar with portfolio optimization suggests the safest plan is to apply the current energy portfolio mix (Table \ref{tbl:energymix}) to the year 2020. \textbf{Note:} The mix in Table 1 does not add to 100\%. In practice, California imports the remaining energy supply from out-of-state. Apply the same percentages to 2020 as 2012. The total energy supply will not add to 225 MWh. What is the expected risk and expected cost of this portfolio? Note this is a lower bound, because the remaining energy must be imported. Is there a safer (in the sense of less variance) portfolio with at least as small expected cost? Explain.

\item The President of CPUC would like to know the minimal risk of each energy profile ranging in price 50 to 250 USD/MWh. Compute the minimal risk for each energy profile in this price range.

\begin{enumerate}
\item Summarize your results in a plot with max price [USD/MWh] on the $x-$axis and risk on the $y-$axis. Describe qualitatively the results.
\item Describe qualitatively the mix of the lowest risk energy portfolio in this price range. How much does it cost?
\end{enumerate}

\textbf{Remark:} Ideally, one wishes to both minimize expected cost and minimize risk. This is a multi-objective optimization problem. However, you'll find there exists a tradeoff between these two objectives. To assess multiple objectives we often create the plot above. This is known as \textit{Pareto optimization}\footnote{\url{http://en.wikipedia.org/wiki/Multi-objective_optimization}}.

\item By the year 2020, California must generate 225 MWh to meet its growing demand for energy. New information suggests that, due to resource limitations and future governmental regulations, the maximum energy supply of non-renewables sources in 2020 is constrained. The energy supply limits are given in Table \ref{tbl:limits}. Moreover, California has mandated a 33\% \textit{renewable portfolio standard} by 2020\footnote{\url{http://www.cpuc.ca.gov/PUC/energy/Renewables/index.htm}}, which includes wind, solar, biomass, and geothermal. Modify the original QP to include these additional constraints.

\begin{enumerate}
\item Formulate the appropriate constraints imposed by these limits, using the notation of Question 1. Describe any additional notation introduced.
\item Solve the original QP with these additional constraints. How does the new solution compare with your results from Question 1? Which constraints are active?
\end{enumerate}

\begin{table}[h]
\caption{Energy supply limits in 2020.}
\begin{center}
\begin{tabular}{|c || c|c|c|c|c|c|c|c|}
\hline
{\bfseries Source} & Coal & Hydro & Natural Gas & Nuclear & Biomass & Geo & Solar & Wind \\ \hline
{\bfseries Limit (MWh)} & 40 & 50 & 150 & 35 & 10 & 15 & 200 & 50  \\ \hline
\end{tabular}
\end{center}
\label{tbl:limits}
\end{table}

\end{enumerate}

%%%%%%%%%%%%%
\section*{Deliverables}
Submit the following on bSpace. Zip your code. Be sure that the function files are named exactly as specified (including spelling and case), and make sure the function declaration is exactly as specified.

\texttt{LASTNAME{\textunderscore}FIRSTNAME{\textunderscore}LAB2.PDF}\\
\texttt{LASTNAME{\textunderscore}FIRSTNAME{\textunderscore}LAB2.ZIP} which contains your respective Matlab files.

\end{document}


\section*{Introduction}
\begin{quote}
  Quantum weirdness is a resource, not a nuisance. --- TC Burt
\end{quote}

(Caution: Brief descriptions of quantum mechanics, even with mathematics,
carry the easily-realized potential to be wrong. Don't carry the qualitative
narrative too deeply.)

\subsection*{Quantum Resources}
\begin{description}
\item[Measurement changes things]
\item[Superposition increases things]
\item[Entanglement intertwingles thingles] \bcsmbh Wonderful, \bcquestion mysterious, and \bcdanger treacherous.
\end{description}

\subsection*{Quantum Information Processing Triad}
\begin{description}
\item[Sensing] refined measurement, novel imaging
\item[Communication] provable (or at least enhanced) security, protected
  information exchange, detectable adversaries, key generation and distribution
\item[Computing] exponentially faster calculations, tractable simulation of
  physical systems
\end{description}


\documentclass{homework}
\course{Math 5522H}
\author{Jim Fowler}
\input{preamble}

\begin{document}
\maketitle

\begin{inspiration}
It is singularity which often makes the worst part of our suffering.\\
\byline{Jane Austen}, not talking about this problem set.
\end{inspiration}

\section{Terminology}

This week, there is a lot of new terminology to classify the various
sorts of singularities we might encounter.  For full credit, be sure
to give careful precise definitions.

\begin{problem}
  What is an \textbf{isolated singularity}?
\end{problem}

\begin{problem}
  What is meant by the \textbf{order} (or \textbf{multiplicity}) of a zero?  Of a pole?
\end{problem}

\begin{problem}
  What is a \textbf{removable singularity}?
\end{problem}

\begin{problem}
  What does it mean to say that a function $f : \C \to \C$ has a zero of order $n$ at infinity?  Has a pole of order $n$ at infinity?
\end{problem}

\begin{problem}
What is a \textbf{meromorphic function}?
\end{problem}

\section{Numericals}

\begin{problem}
  Let $f(z) = e^{z \sin^2 z} - 1$.  What is the order of the zero at $z = 0$?
\end{problem}

\begin{problem}
  Let $f(z) = \left( \cos z \right) - 1 - z^2/2$, and compute
  \[
    \int_\gamma \frac{f'(z)}{f(z)} \, dz
  \]
  for $gamma : [0,2\pi]$ given by $\gamma(\theta) = e^{i\theta}$.
\end{problem}

\section{Exploration}

\begin{problem}
  Explain the topology on $\mathbb{C} \cup \{ \infty \}$, the \textbf{Riemann sphere}.
\end{problem}

\begin{problem}\label{jordans-lemma}
  Prove \textbf{Jordan's lemma}; next week, this lemma will help us estimate integrals over the contour $\gamma : [0,\pi] \to \C$ given by $\gamma(\theta) = re^{i\theta}$.
  Specifically, show that if $a > 0$, then
  \[
    \abs{\int_\gamma e^{iaz} \, g(z) \, dz } \leq \frac{\pi}{a} \sup_{\theta \in [0,\pi]} \abs{g(\gamma(\theta))}.
    \]
\end{problem}

\begin{problem}\label{riemann-removable-singularity}
  For an open set $U \ni z_0$, suppose
  $f : U \setminus \{z_0\} \to \C$ is holomorphic.  Show that
  $\lim_{z\to z_0} (z-z_0)f(z)=0$ if and only $f$ extends to a
  holomorphic function $F : U \to \C$.
\end{problem}

\begin{problem}\label{idempotent-entire}Describe holomorphic functions
  $f : \C \to \C$ with the property that $f(f(z)) = f(z)$ for all
  $z \in \C$.
\end{problem}

\begin{problem}
  Suppose $U$ is a disk and $f : U \to \C$ is holomorphic with
  finitely many zeros, and repeatedly invoke \ref{factor-theorem} to
  explain why you can find $z_1,z_2,\ldots,z_n \in \C$ and write
  \[
    f(z) = (z-z_1)(z-z_2) \cdots (z-z_n) \, g(z)
  \]
  for a nowhere-vanishing analytic function $g : U \to \C$.
\end{problem}

\begin{problem}\label{argument-principle-zeros}
  Continuing as above, compute $f'(z)/f(z)$ in terms of $z_1,z_2,\ldots,z_n \in \C$ and $g'(z)/g(z)$, and evaluate
  \[
    \frac{1}{2\pi i} \int_\gamma \frac{f'(z)}{f(z)} \, dz
  \]
  in terms of the winding numbers $n(\gamma,z_j)$.
\end{problem}

\begin{problem}
  Let's justify the terminology that a ``zero of multiplicity $n$''
  really means there are $n$ solutions to a certain equation.  Suppose
  the holomorphic function $f : B_1(0) \to \C$ has a zero of order $n$
  at zero, i.e., suppose $f(z) = z^n g(z)$ for a holomorphic
  $g : B_1(0) \to \C$ with $g(0) \neq 0$.  For all sufficiently small
  $\epsilon > 0$, find $\delta > 0$ so that for all
  $w \in B_\epsilon(0) - \{0\}$, the set
  $f^{-1}(\{w\}) \cap B_\delta(0)$ consists of $n$ elements.
\end{problem}

\section{Prove or Disprove and Salvage if Possible}

\begin{problem}\label{factor-theorem}
  Suppose $U \subset \C$ is open, and $f : U \to \C$ is analytic, and for some $z_0 \in U$, we have $f(z_0) = 0$.  Then there is a positive $m \in \Z$ so that
  \[
    g(z) := \frac{f(z)}{(z-z_0)^m}
  \]
  yields an analytic function $g : U \to \C$ which does not vanish at $z_0$.
\end{problem}

\begin{problem}\label{entire-dominate-entire}Suppose $f, g : \C \to \C$ are holomorphic and for all $z \in \C$ we have $\abs{f(z)} \leq \abs{g(z)}$.  In this case, we say that $g$ dominates $f$.  Then $f(z) = \lambda \cdot g(z)$ for some $\lambda \in \C$.  (Compare \ref{identity-dominate-entire}.)
\end{problem}

\begin{problem}
  If $f : \C \to \C$ has a pole of order $n$ at infinity, then $f$ is a polynomial of degree at most $n$.
\end{problem}

\begin{problem}\label{casorati-weierstrass}
  Suppose $f : U \setminus \{ z_0 \} \to \C$ is holomorphic with an essential singularity at $z_0 \in U$.  If $V \subset U$ is a neighborhood of $z_0$, then $f(V \setminus \{ z_0 \})$ is dense in $\C$.
\end{problem}

\begin{problem}
  There exists a holomorphic function $f : \C \to \C$ so that both $f$
  and $z \mapsto e^{f(z)}$ have poles at zero.
\end{problem}

\begin{problem}
  There exists a nowhere-vanishing holomorphic function
  $f : \C \to \C$ such that $\lim_{z \to \infty} f(z) = \infty$.
\end{problem}

\end{document}


\section{A Brief Primer on TSTL}

\begin{figure}
{\scriptsize
\begin{code}
$_{01}$ @import avl
$_{02}$ @import math
\vspace{0.1in}
$_{03}$ <@
$_{04}$ def it(s):
$_{05}$     l = []
$_{06}$     for i in s:
$_{07}$        l.append(i)
$_{08}$     return sorted(l)
$_{09}$ @>
\vspace{0.1in}
$_{10}$ source: avl.py
\vspace{0.1in}
$_{11}$ pool: <int> 4 CONST
$_{12}$ pool: <avl> 2 REF
$_{13}$ pool: <list> 2
\vspace{0.1in}
$_{14}$ log: 1 <avl>.inorder()
\vspace{0.1in}
$_{15}$ property: <avl>.check\_balanced()
\vspace{0.1in}
$_{16}$ <list>:=[]
$_{17}$ ~<list>.append(<int>)
$_{18}$ <int>:= <[1..20]>
\vspace{0.1in}
$_{19}$ <avl>:=avl.AVLTree()
$_{20}$ <avl>:=avl.AVLTree(<list>)
\vspace{0.1in}

$_{21}$ ~<avl>.insert(<int>) => (len(<avl,1>.inorder()) == pre<(len(<avl,1>.inorder()))>+1)
  or pre<(<avl,1>.find(<int,1>))>
$_{22}$ ~<avl>.delete(<int>) => (len(<avl,1>.inorder()) == pre<(len(<avl,1>.inorder()))>-1)
  or not pre<((<avl,1>.find(<int,1>)))>
$_{23}$ ~<avl>.find(<int>)
$_{24}$ <avl>.inorder()
$_{25}$ len(<avl,1>.inorder()) > 5 -> <avl>.display()
\vspace{0.1in}
$_{26}$ reference: avl.AVLTree ==> set
$_{27}$ reference: insert ==> add
$_{28}$ reference: delete ==> discard
$_{29}$ reference: find ==> \_\_contains\_\_
$_{30}$ reference: METHOD(inorder) ==> CALL(it)
$_{31}$ reference: METHOD(display) ==> CALL(print)
\vspace{0.1in}
$_{32}$ compare: find
$_{33}$ compare: inorder
\end{code}
}
\caption{Part of a TSTL definition of AVL tree tests.}
\label{fig:example}
\end{figure}

\begin{figure}
{\scriptsize
\begin{code}
avl1 = avl.AVLTree()
int3 = 10
int1 = 11
avl1.insert(int1)
int1 = 1
avl1.insert(int3)
avl1.insert(int1)
int3 = 9
avl1.insert(int3)
int2 = 11
avl1.delete(int2)
\end{code}
}
\caption{An example TSTL-produced test.}
\label{fig:avlrun}
\end{figure}

TSTL, the Template Scripting Testing Language, \cite{NFM15,ISSTA15,tstl} is a language for defining the
structure of test cases (usually API-call sequences, but also
grammar based tests using string construction), and a set of tools for
use in generating, manipulating, and understanding those test cases.
Figure \ref{fig:example} shows a TSTL definition of tests (known as a
harness definition, or \emph{harness} for short) for a Python class
implementing AVL trees\footnote{AVL trees, named after Georgy
  Adelson-Velsky and Evgenii Landis \cite{AVL} are balanced tree
  structures similar to red-black trees; they are also frequently used as
  examples in software testing \cite{FASE,ISSRE}.} \cite{avltree}, in the latest syntax for TSTL (modified in the
course of the work described in this paper).  Given a harness like the
one in Figure \ref{fig:example}, TSTL compiles it into a Python file
defining a class that gives an interface for testing.  The class
interface provides features such as
querying the set of available testing actions, restarting a test,
replaying a test, collecting data about which lines and branches in
the source code of the SUT have been executed by each test, and other commonly
needed testing features.  The TSTL release \cite{tstl} provides
testing tools that use this interface for test generation and
debugging.

A TSTL test harness defines a set of \emph{pools} that hold values
produced and used during testing.   Using pools \cite{AndrewsTR} is a common
approach to defining API-testing sequences.  The harness also defines a set of
actions that are possible during testing, typically API calls and
assignments to pool values.  Code marked off with the {\tt @}
symbol (lines 1-9) is raw Python code.  The ability to call
arbitrary code in Python makes TSTL extremely expressive.  Other
elements of the ``preamble'' (before action definitions) are the
indication of the source files to be tested (line 10), the information
on how to produce logs of test activity for debugging (line 14), and a
correctness property to check after every test action (line 15).

In this example, there are three pools,
{\tt int}, {\tt list}, and {\tt avl}.  There are four instances of the
{\tt int} pool, which means that a test in progress can store up to 4
{\tt int}s at one time (in variables named {\tt int0}, {\tt int1},
{\tt int2}, and {\tt int3}), and two instances of the {\tt avl} and
{\tt list}
pools.  The actions defined here include setting the value of an {\tt int}
pool to any integer in the range 1-20 inclusive (line 18), setting the value of
an {\tt avl} pool to a newly constructed AVL tree (line 19), and calling an AVL
tree's {\tt insert}, {\tt delete}, {\tt find} and {\tt inorder}
methods (lines 21-24).  One line of TSTL typically defines more than one action. For
example, the line of TSTL code {\tt <avl>.find(<int>)} defines 8 actions, one
for each choice of {\tt avl} and {\tt int} pool instance:  the action set
includes {\tt avl0.find(int0)}, {\tt avl1.find(int0)}, {\tt
  avl0.find(int1)}, and so forth.

Figure \ref{fig:avlrun} shows a valid test case produced by
running a random test generator on the TSTL-compiled interface
produced by this definition.  TSTL automatically enforces the requirement that
tests are well-formed: for example, no pool instance (such as {\tt
  avl1}) can appear in an action until it has been assigned a value,
and no pool instance that has been assigned a value can be assigned a
different value until it has been used in an action, to avoid
degenerate sequences such as {\tt int3 = 10} followed by {\tt int3 =
  4}.  Each action in a test case is called a ``step'' --- the first
step of the first test case in Figure \ref{fig:avlrun} is storing a
new AVL tree in {\tt avl1}.  Prefacing a ``use'' of a pool with a
tilde {\tt ~} indicates that use does not allow the pool to be
re-initialized; in this example, an AVLtree object cannot be replaced
with a new tree until it has been displayed or traversed.

Figure \ref{fig:example} shows some additional features
of TSTL.  The syntax {\tt guard -> action} allows {\tt action} to take
place only if {\tt guard} is an expression that evaluates to true (see
line 25).  In the example, only AVL trees of size 5 or larger are
displayed.  One feature that is not shown is the syntax for allowing
an action to throw an exception without stopping testing. This
construct, {\tt \{exception [, exception2, ...]\} action}, is used
many times in the TSTL harness, because operations in ArcPy tend to
signal failure by throwing an exception.

Because thus far our testing
has been limited to checking for crash failures and comparing outputs
between versions, we have not needed the TSTL feature for checking an
assertion after an action ({\tt action => pred}, shown on lines 21 and
22), but we expect to use it in future testing, as discussed in Section
\ref{futureoracle}.  The construct {\tt <avl,1>} is a pool use that
always expands to the same pool variable as the indexed occurrence of
the pool in the current action.  The final feature of interest here is the full
support for reference testing.  Marking a pool as {\tt REF} (line 12) creates a
second copy of each pool variable, and transforms the operations
performed on the copy according to a reference mapping, as given in
lines 26-31 of the example code.  In the example, AVL trees are
checked for behavioral equivalence to Python sets.  Lines 32 and 33
also indicate the return values from {\tt find} and {\tt inorder}
should be compared to their reference equivalents.  Section
\ref{sec:reftest} discusses the possibility of using
differential/reference testing \cite{Differential,ICSEDiff} in ArcPy
within a single version.


\input ../6001mac

\newcommand{\Code}[1]{\mbox{\tt #1}}

\begin{document}

\psetheader{Sample Problem Set}{Evaluators}
\begin{center}\large
{\bf Scheme Evaluators}
\end{center}

\bigskip

You will be working with evaluators as described in Chapter 4 of the
notes.  If you don't have a good understanding of how the evaluators are
structured, it is very easy to become confused between the programs that
the evaluator is interpreting, the procedures that implement the evaluator
itself, and Scheme procedures called by the evaluator.  You will need to
have carefully studied Chapter 4 through subsection 4.2.2 in order to do
this assignment.

To help you focus on some of the important ideas in the Notes, we ask you
to prepare:

\section{Using the Evaluators}

The code for this problem set includes these files:
\begin{itemize}

\item {\tt meval.scm} is the metacircular evaluator described in
section 4.1.1 of the notes.  It has been extended to allow declarations of
lazy procedure parameters, as described in section 4.2 and exercise 4.25.
In order to avoid confusing the {\tt eval} and {\tt apply} of this
evaluator with the {\tt eval} and {\tt apply} of the underlying Scheme
system, we have renamed these procedures {\tt meval} and {\tt m-apply}.

\item {\tt analyze.scm} is the {\tt analyze} evaluator of section 4.1.6,
extended to allow declarations of lazy procedure parameters, as described
in section 4.2 and exercise 4.25.

\item {\tt evdata.scm} contains the procedures that define the evaluator's
data structures, as in section 4.1.3.  In order to interface the evaluator
to the underlying Scheme system, we have made the modification outlined in
exercise 4.14.  Namely, any variable that is not found in the evaluator's
global environment will be looked up in the underlying Scheme; Scheme
procedures found in this way will treated by the evaluator as primitive
procedures.  Programs being executed by the metacircular evaluator can
therefore make use of any procedure (primitive or compound) which may be
defined in your Scheme environment.

\item {\tt syntax.scm} contains the procedures that define the syntax of
expressions, as described in section 4.1.2.

\end{itemize}

You'll be switching back and forth between both of these evaluators.  The
two evaluators use the same implementation of environment data structures.
This arrangement requires that you reinitialize the evaluator's environment
when you switch from one to the other.  Here's how to manage this:
\begin{itemize}

\item
To start up, load the files {\tt syntax.scm}, {\tt evdata.scm},
{\tt meval.scm}, and {\tt analyze.scm}.  Then evaluate
\Code{(start-meval)} in Scheme; this starts the read-eval-print loop
for the meta-circular evaluator with a freshly initialized global
environment.

To evaluate an expression, you may type the expression into the
\Code{*scheme*} buffer followed by {\tt ctrl-x ctrl-e}.  (Don't use
{\tt M-z} to evaluate the expression in the \Code{*scheme*}
buffer---the presence of the evaluator prompts confuses the {\tt M-z}
mechanism.  But {\tt M-z} works fine for sending expressions to the
\Code{*scheme*} buffer from other Scheme-mode buffers.)

We've arranged that each read-eval-print loop identifies itself by its
input and output prompts.  For example,

\beginlisp
MEVAL=> (+ 3 4)
;;M-value: 7
\endlisp
shows an interaction with the {\tt meval} evaluator.

\item
You should keep in a separate file any procedure definitions you want to
install in an evaluator.  If your Edwin Scheme buffer is running the
read-eval-print loop of an evaluator, you can then visit this definitions
file and type {\tt M-o} to enter the definitions into the evaluator.  To
get you started, we have included an alternative implementation of lazy
lists in the buffer {\tt ps8defs.scm}.

\item You can interrupt an evaluator by typing {\tt ctrl-c ctrl-c}.
To restart it with the recent definitions still intact, evaluate {\tt
(eval-loop)}.

\item To start a read-eval-print loop for the analyzing evaluator in a
fresh global environment, return to Scheme and evaluate {\tt
(start-analyze)}.

\item The evaluators you are working with do not include error
systems.  If you hit an error you will bounce back into ordinary
Scheme.  You can restart the current evaluator, with its global
environment still intact, by evaluating {\tt (eval-loop)}.

\item The variable {\tt current-evaluator} is defined as an alias for
the current evaluator in each of the files {\tt meval.scm} and {\tt
analyze.scm}.  This should ensure that {\tt current-evaluator} will be
bound to the most recently started evaluator at all times during your
lab session.

\item It can be instructive to trace {\tt current-evaluator}, {\tt
m-apply}, and/or {\tt exapply} during evaluator executions.  (You will
also probably need to do this while debugging your code for this
assignment.)

\item  Since environments are generally complex, circular list
structures, we have set Scheme's printer so that it will not go into
an infinite loop when asked to print a circular list.  This was done by
\beginlisp
(set! *unparser-list-depth-limit* 7)
(set! *unparser-list-breadth-limit* 10)
\endlisp
at the end of the file {\tt evdata.scm}.  You may want to alter the
values of these limits to vary how much list structure will be printed as
output.

\end{itemize}

\section{Exercises}

\paragraph{Lab exercise 1A:}
Start the \Code{meval} evaluator and evaluate a few simple expressions
and definitions.  It's a good idea to make an intentional error and
practice restarting the read-eval-print loop.  Notice that we have
already installed {\tt let} in the evaluator, so you can use this in
your programs.

\paragraph{Lab exercise 1B:}
Now start the analyzing evaluator.  You can tell that you are typing
at the analyzing evaluator because the prompt will be {\tt AEVAL=>}
rather than {\tt MEVAL=>}.  Now once again evaluate a few simple
expressions and definitions.

\paragraph{Lab exercise 1C:}
Start the meta-circular evaluator again.  Now trace evaluation of the
application of some simple procedure to some argument.

\medskip

Ben Bitdiddle thinks it is silly to reinitialize the environment every
time he switches from one evaluator to the other.  So, after having
run the meta-circular evaluator, he revises the \Code{start-analyze}
procedure so it does not reinitialize the global environment:

\beginlisp
(define (start-analyze)
  (set! current-evaluator (lambda (exp env) ((analyze exp) env)))
  (set! current-prompt "AEVAL=> ")
  (set! current-value-label ";;A-value: ")
;  (init-env)              ;Ben comments-out this line
  (eval-loop))
\endlisp

He reasons that now, if he types \Code{(start-analyze)}, the
definitions he already evaluated in the meta-circular evaluator will
be available to the analyzing evaluator.  Eva Lu Ator agrees that the
definitions of the meta-circular evaluator will indeed be available to
the analyzing evaluator, but that this will usually crash the system
rather then being helpful.

\paragraph{Lab exercise 2A:}
Try Ben's suggestion and observe what Eva Lu Ator meant.

\paragraph{Post-Lab exercise 2B:}
Briefly, but clearly, explain what goes wrong with Ben's suggestion.
Are there any definitions it would be safe to preserve when switching
between evaluators?

\subsection{Comparing evaluation speeds}

This next problem asks you to demonstrate the improved efficiency of
the analyzing evaluator over the meta-circular one.

To time things, you can use the procedure {\tt show-time}, which you used
in Problem Set 2.  Thus, for example, you can find out how long it takes
the evaluator to evaluate {\tt (fib 10)} by quitting out of the evaluator
and evaluating

\beginlisp
(show-time (lambda() (current-evaluator '(fib 10) the-global-environment)))
\endlisp
in Scheme.  Be careful to define the procedure you are timing, e.g., {\tt
fib}, in the evaluator, not in ordinary Scheme!  Otherwise, you'll end up
timing the underlying Scheme interpreter.  This is because of the way
we've linked the evaluator into Scheme: if you define {\tt fib} in Scheme,
and not in the evaluator's global environment, {\tt lookup-variable-value}
will find the Scheme procedure {\tt fib} and {\tt m-apply} will treat this
as a primitive.

\paragraph{Lab exercise 3A:} Design and carry out an experiment to
compare the speeds of the {\tt meval} and {\tt analyze} evaluators on some
procedures.  Use tests that run for a reasonable amount of time (say 10 or
20 seconds).  It may be helpful to use test procedures for which small
changes in the input cause large changes in running time, so you can
rapidly increase the time by increasing the input.

Summarize what you observe about the relative speeds of the two evaluators
on your test programs.

\paragraph{Lab exercise 3B (Optional---Extra Credit):}
See if you can find an example where the interpreters run for at least
five seconds and the {\tt analyze} evaluator runs {\em no more} than 1.5
times the speed of {\tt meval}.

\subsection{Adding Language Constructs}

\paragraph{Pre-Lab exercise 4A:}

Lazy parameter declarations are already supported by both the \Code{meval}
and \Code{analyze} evaluators supplied to you.  Suppose we are given some
{\em fixed} $n \geq 0$.  With lazy parameters, it is possible to define
within an evaluator an $n$-argument {\em procedure}, {\tt and-proc}, that
yields the same results as the Scheme special form \Code{and} when it is
used with $n$ subforms.  That is, \Code{(and $E_1$ \ldots $E_n$)} in
Scheme, and
\Code{(and-proc $E_1$ \ldots $E_n$)} in an evaluator, should produce exactly the
same results (including side-effects) for any expressions $E_1 \ldots
E_n$.  Outline how to define the $n$-argument \Code{and-proc} within our
evaluators.

\paragraph{Lab exercise 4B:}
Evaluate the definition of a three-argument {\tt and-proc} in both the
\Code{meval} and \Code{analyze} evaluators.

\paragraph{Lab exercise 5:}
Since our evaluators do not yet have the ability to define procedures with
varying numbers of arguments, we can't add the general {\tt and} construct
with the approach of Exercise 4.  So instead, add {\tt and} as a special
form to one of the evaluators (you may choose either one, but see exercise
7A below before you make the choice).

\medskip

We can also extend the evaluators to handle Scheme's convention
for defining procedures with varying numbers of arguments.  For
example, an abstraction of the form \Code{(lambda (x y . z) \ldots)}
defines a procedure of two or more arguments, in which the value of
the first argument would be bound to \Code{x}, the value of the second
to \Code{y}, and a list consisting of the values of any further
arguments would be bound to \Code{z}.  If, instead of \Code{z}, we had
\Code{(w lazy)}, then a list consisting of the {\em delayed} values of
any further arguments would be bound to \Code{w}.  Similarly, if \Code{(w
lazy-memo)} appears instead of z, this indicates that a list consisting of
the memoized delayed values of any further arguments would be bound to
\Code{w}.

We accomplish the extension by modifying the procedure
\Code{process-arg-procs} in the file {\tt evdata.scm}\footnote{The
procedure \Code{parameter-names} in {\tt syntax.scm} must also be written
to handle the dotted-pair form of parameters, but we have done this
already.}.  As written, \Code{(process-arg-procs params aprocs env)}
expects \Code{params} and \Code{aprocs} to be {\em lists} of equal length,
and it returns a list of parameter values of the same length.  If
\Code{params} is not a list of parameter declarations, but instead ends
with a dotted pair of parameters, then we want \Code{process-arg-procs} to
return a list of values in which the last value is itself a list of all
the "extra" values, possibly all delayed or all memoized depending on
whether the final parameter is declared to be lazy or memoized.  For
example,

\beginlisp
((lambda (x y . (w lazy)) (list x y (car w)))
 1  2  (if \#t 3 4)  (print "this should not print"))
\endlisp
will return \Code{(1 2 3)}, without printing anything.  This is because in
the application, the variable \Code{x} will be bound to 1, variable
\Code{y} will be bound to 2, and variable \Code{w} will be bound to the
list consisting of the delayed values of
\beginlisp(if \#t 3 4)\endlisp
and
\beginlisp(print "this should not print").\endlisp
The first delayed value gets forced when the primitive procedure
\Code{list} is applied to it.  The second delayed value is not ever
forced, so no printing occurs.

So we revise \Code{process-arg-procs} to reformat its arguments if
necessary, after which it proceeds as before:

\beginlisp
(define (process-arg-procs params aprocs env)
  (let ((params (undot params))
        (aprocs (matchup-args params aprocs)))
    (map (lambda (param aproc)
           (cond ((variable? param) (aproc env))
                 ((lazy? param) (delay-it aproc env))
                 ((memo? param) (delay-it-memo aproc env))
                 (else (error "Unknown declaration" param))))
         params
         aprocs)))
\endlisp

The procedure \Code{matchup-args} has the task of taking the list of
arguments and returning the list with all the "extra" arguments put
into a single list at the end.  But at this point each argument in the
input list is actually packaged as an "argument procedure."  The
argument procedure will yield the argument value when it is applied to
the environment.  So \Code{matchup-args} actually has to create a
similar package which yields the list of extra argument values when it
is applied to the environment.

\beginlisp
(define (matchup-args params aprocs)
  (cond
   ((null? params)
    (if (null? aprocs) '() (error "matchup-args: too many args" aprocs)))
   ((variable? params)
    (list
     (lambda (env)
       (map (lambda (aproc) (aproc env)) aprocs))))
   ((lazy? params)
    (list
     (lambda (env)
       (map (lambda (aproc) (delay-it aproc env)) aprocs))))
   ((memo? params)
    (list
     (lambda (env)
       (map (lambda (aproc) (delay-it-memo aproc env)) aprocs))))
   ((null? aprocs) (error "matchup-args: too few args" params))
   (else (cons
          (car aprocs)
          (matchup-args (cdr params) (cdr aprocs))))))
\endlisp

\medskip
\beginlisp
(define (undot params)
  <blob6a>)
\endlisp

\paragraph{Lab exercise 6A:}
Complete the definition of \Code{<blob6a>}, insert these definitions in
the file {\tt evdata.scm} and reload the file into Scheme.  You will find
a copy of the above definitions in buffer \Code{ps8-ans.scm}.

\paragraph{Lab exercise 6B:}
Now we can add, as a procedure, a genuine {\tt and} construct to the
evaluators.  Namely,

\beginlisp
(define and (lambda (l lazy) (and-lproc l)))
\endlisp

where {\tt and-lproc} is a procedure that takes as its argument a single
list\footnote{Both \Code{lazy} and \Code{lazy-memo} are now specified to
be keywords.  This prevents misreading \Code{(lambda (l lazy) \ldots)} as
a procedure with two formals, \Code{l} and \Code{lazy}.  Rather, it is a
procedure which may be applied to zero or more arguments---all of which
will be delayed.} of delayed values. Give the definition of {\tt
and-lproc}.

\medskip

Even with lazy and varying numbers of parameters, not all language
facilities can be defined as Scheme procedures.  An example is the
special form {\tt (for-list <var> <l> <body>)} where \Code{<var>} is a
variable and the value of {\tt <l>} is a list.  Evaluation of this
expression causes \Code{<body>} to be repeatedly evaluated as {\tt
<var>} is successively assigned to be the elements of the value of
{\tt <l>}.  More precisely, {\tt (for-list <var> <l> <body>)} is
syntactic sugar for\footnote{We're assuming that the local variables
\Code{iter} and \Code{rest-l} are not \Code{eq?} to \Code{<var>} or
any variable free in \Code{<body>}.}

\beginlisp
(let (rest-l <l>))
  (define (iter)
    (if (null? rest-l)
        the-unspecified-value
        (begin
          (set! <var> (car rest-l))
          <body>
          (set! rest-l (cdr rest-l))
          (iter))))
  (iter))
\endlisp

\paragraph{Lab exercise 7A:}
Add the {\tt for-list} construct as a special form ({\em not} as syntactic
sugar like {\tt let} or {\tt cond}) to whichever evaluator you did {\em not}
choose in exercise 4.

\paragraph{Lab exercise 7B:}
Exhibit a \Code{<body>} for which {\tt (for-list x '(1 2) <body>)}
does not have the same behavior as {\tt (map (lambda (x) >body>) '(1
2))}.

\paragraph{Lab and Post-Lab exercise 8 (Optional):}
The evaluators are not quite powerful enough to evaluate themselves.  That
is, it doesn't quite work to evaluate all the \Code{meval} definition
files within the \Code{MEVAL} read-eval-print loop, and then to apply
\Code{meval} as a procedure defined {\em within} the \Code{MEVAL}
evaluator.  You may want to try this to see what happens.

Discuss what further features or definitions would be required in order to
successfully run the evaluators within themselves and/or within each other.

\iffalse
\paragraph{Lab and Post-Lab exercise 9 (Optional):}
Ben Bitdiddle's idea from exercise 2 of having both evaluators share
the same environment---so that definitions evaluated in either evaluator
would be available and run correctly in the other---can be made to work.
It requires only a slight modification in the way that the \Code{meval}
evaluator represents procedure objects.  The modification is already
suggested in the \Code{value-procs} procedure in {\tt meval.scm}, which
enabled the two evaluators to use the same parameter declaration and
environment structures.  Explain more fully how to make Ben's idea work.
Do it if you have time.
\fi

\end{document}


\documentclass{beamer}

\usepackage{default}
\usepackage{amssymb}
\usepackage[utf8]{inputenc}

\DeclareGraphicsExtensions{{.pdf},{.png},{.jpg}}
\graphicspath{ {./img/} }

\hypersetup{colorlinks,urlcolor=blue}
\AtBeginSection[]
{
	\begin{frame}
		\begin{NoHyper}
		\tableofcontents[currentsection]
		\end{NoHyper}
	\end{frame}
}
\begin{document}
\addtobeamertemplate{footline}{\insertframenumber/\inserttotalframenumber}

\title{Introduction to MCMC}
\author{Alberto Lumbreras}
\date{February 4, 2016}
\maketitle

\begin{frame}\frametitle{Overview}
\begin{NoHyper}
\tableofcontents
\end{NoHyper}
\end{frame}

\section{Introduction to Bayesian inference}
\begin{frame}{Intro to Bayesian Inference (``inverse probability")}{A controversial formula}

	\begin{quote}
	{\color{red}``The theory of inverse probability is founded upon an error, and must be wholly rejected!"}  \text{\normalfont Fisher, 1925}
	\end{quote}
	\begin{quote}
	{\color{red}``Reduces all probability to a subjective judgement."} \text{\normalfont Fisher, 1956}
\end{quote}
	\begin{quote}
	{\color{red}``Fallacious rubbish!"} \text{\normalfont Fisher about Laplace, 1958}
	\end{quote}

	\begin{quote}
	``The more I consider it, the more clearly it would appear that I have been doing almost exactly what Bayes had done in the 18th century."
	\text{\normalfont Fisher, 1959}
	\end{quote}
\end{frame}

\begin{frame}{Intro to Bayesian Inference (``inverse probability" )}{Thomas Bayes' formula (one more time)}

	\begin{equation}
	\underbrace{p(\theta | y)}_{posterior}
	=
	\frac{\overbrace{p(y, \theta)}^{\text{joint probability}}}{\int_\theta p(y, \theta)}
	=
	\frac{\overbrace{p(y | \theta)}^{likelihood} \overbrace{p(\theta)}^{prior}}{\int_\theta p(y | \theta) p(\theta)}
	\propto
	\overbrace{p(y | \theta)}^{likelihood} \overbrace{p(\theta)}^{prior}
	\end{equation}
	\begin{itemize}
		\item Imagine we want to use Bayes in a non-trivial problem...
		\item ...where the denominator is not tractable (\textbf{usually})
		\item ...but we can \textbf{always} evaluate the likelihood and the prior.
		\item How can I get access to the posterior?
	\end{itemize}
\end{frame}

\begin{frame}{Intro to Bayesian Inference }{Computers to the rescue}
		\begin{equation}
		\underbrace{p(\theta | y)}_{posterior}
		\propto
		\overbrace{p(y | \theta)}^{likelihood} \overbrace{p(\theta)}^{prior}
		\end{equation}
		\begin{itemize}
			\item The denominator was just a normalizing factor.
			\item Therefore drawing samples from $\overbrace{p(y | \theta)}^{likelihood} \overbrace{p(\theta)}^{prior}$ is like drawing samples directly from the posterior.
			\item ``\textit{I wish I had an Intel Core i7} :\_(" \\Thomas Bayes, circa 1750.
		\end{itemize}
\end{frame}

\section{Basic Monte Carlo methods}
\begin{frame}{Inverse method}{Exploiting the Cumulative Distribution Function}

	\begin{enumerate}
		\item Get the CDF: $\mathbb{R} \rightarrow [0,1]$
		\item Draw samples from uniform distribution.
		\item Map uniform samples to final samples through $CDF^{-1}: [0,1] \rightarrow \mathbb{R}$
	\end{enumerate}
	\begin{figure}
		\centering
		\includegraphics[width=\textwidth]{inverse.png}
	\end{figure}
\end{frame}

\begin{frame}{Inverse method}{Pros and cons}
	Pros:
		\begin{itemize}
			\item Faster possible method.
		\end{itemize}
	Cons:
	\begin{itemize}
		\item We don' have the CDF of most distributions.
		\item Only for unidimensional distributions.
	\end{itemize}

\end{frame}

\begin{frame}{Accept-Reject sampling}{Sample from a comfort zone}
	\begin{enumerate}
		\item Look for some pretty density $q$ that you know how to can sample from.
		\item Make it bigger than your ugly density $p$. ($Kq$)
		\item Draw sample $x_i$ from the pretty one.
		\item Accept samples with probability $p(x_i)/(Kq(x_i))$
	\end{enumerate}
		\begin{figure}
			\centering
			\includegraphics[width=\textwidth]{rejection.png}
		\end{figure}
\end{frame}

\begin{frame}{Accept-Reject sampling}{Pros and cons}
	Pros:
	\begin{itemize}
		\item We can usually find a pretty envelope $Kq$.
	\end{itemize}
	Cons:
	\begin{itemize}
		\item If $Kq$ is too wide or too big, lots of wasted (rejected) samples.
		\item Not always easy to find a good $Kq$.	\end{itemize}
\end{frame}

\section{Markov Chain Monte Carlo methods}
\begin{frame}{Metropolis-Hastings}{Jumping from last sample}
	\begin{enumerate}
		\item Start at some initial sample $x_0$
		\item Random jump $x_{i+1} \sim x_i + \mathcal{N}(0,step)$
		\item Accept with probability $p(x_{i+1})/p(x_i)$. If rejected, $x_{i+1} = x_i$
		\item Repeat 2 and 3 $N$ times ($N$ is the length of your Markov Chain)
	\end{enumerate}

	\begin{figure}
		\centering
		\includegraphics[width=\textwidth]{metropolis_unidimensional.png}
	\end{figure}
\end{frame}

\begin{frame}{Metropolis-Hastings}{About the Markov Chain}
	\begin{itemize}
		\item First samples depend on $x_0$. We drop them (burn-in).
		\item After burn-in, we are sampling from the true distribution.
		\item Statistical convergence checks (see \texttt{coda} package in R).
	\end{itemize}
	\begin{figure}
		\centering
		\includegraphics[width=\textwidth]{metropolis_traces.png}
	\end{figure}
\end{frame}

\begin{frame}{Metropolis-Hastings}{Pros and cons}
	Pros:
	\begin{itemize}
		\item Always works! (see ugly density below)
		\item Step size needs to be carefully tuned (for efficiency).
	\end{itemize}
	Cons:
	\begin{itemize}
		\item Rejects $\sim 70-80$\% of samples (recommended).
	\end{itemize}
	\begin{figure}
		\centering
		\includegraphics[width=\textwidth]{metropolis_bidimensional.png}
	\end{figure}
\end{frame}

\begin{frame}{Gibbs sampling}{Sample from pretty individual conditional probabilities.}
	Ugly $p(x1, x2, x3)$
	\begin{enumerate}
		\item Choose a initial $x_1, x_2, x_3$
		\item Draw $x_1 \sim p(x_1 | x_2, x_3)$ (easy!)
		\item Draw $x_2 \sim p(x_2 | x_1, x_3)$ (easy!)
		\item Draw $x_3 \sim p(x_2 | x_2, x_1)$ (easy!)
		\item Repeat 1,2 and 3 $N$ times.
	\end{enumerate}
		\begin{figure}
			\centering
			\includegraphics[width=\textwidth]{gibbs.png}
		\end{figure}
\end{frame}

\begin{frame}{Gibbs sampling}{Prons and cons}
	Pros:
	\begin{itemize}
		\item Just a smart Metropolis-Hastings where all samples are accepted.
	\end{itemize}
	Cons:
	\begin{itemize}
		\item Only possible if conditional probabilities are pretty.
		\item (... and for that we need to use \textit{conjugate priors})
	\end{itemize}
\end{frame}

\begin{frame}{Wrap-up}
	\begin{itemize}
	\item We sample because we can't do the maths analytically.
	\item Gibbs and Metropolis are a must in a Bayesian toolbox.
	\end{itemize}
	Caveat:
	\begin{itemize}
		\item burn-in can take toooo much.
	\end{itemize}
	Alternatives:
	\begin{itemize}
		\item Laplace approximations.
		\item\textbf{ Variational Inference (approximation)} (very popular, very scalable)
		\item Expectation Propagation (approximation).
		\item ...
	\end{itemize}
\end{frame}
\begin{frame}{Readings}
	Historical debate:
	\begin{itemize}
			\item \href{http://projecteuclid.org/download/pdf_1/euclid.ba/1340370565}{Aldrich, \textit{R. A. Fisher on Bayes and Bayes' theorem }}
			\item  \href{http://www.phil.vt.edu/dmayo/PhilStatistics/b\%20Fisher\%20design\%20of\%20experiments.pdf}{Fisher. \textit{The Design of Experiments} (page 6) }
	\end{itemize}
	MC Books and tutorials
	\begin{itemize}
		\item \href{http://www.indiana.edu/~kruschke/DoingBayesianDataAnalysis/}{Kruschke. \textit{Doing Bayesian Data Analysis.}}
		\item Bishop. \textit{Pattern Recognition and Machine Learning} (ch.11)
		\item Robert et Casella. \textit{Méthodes de Monte Carlo avec R}.
		\item \href{http://www.stat.columbia.edu/~gelman/book/}{Gellman. \textit{Bayesian Data Analysis}.}
		\item \href{http://camdavidsonpilon.github.io/Probabilistic-Programming-and-Bayesian-Methods-for-Hackers/}{Cam Davidson-Pilon. \textit{Bayesian Methods for Hackers}.}
	\end{itemize}
	Divulgation:
	\begin{itemize}
		\item \href{https://en.wikipedia.org/wiki/The_Lady_Tasting_Tea}{Salsburg, D.\textit{The lady tasting tea.}}
		\item \href{https://en.wikipedia.org/wiki/The_Signal_and_the_Noise}{Silver, N. \textit{The Signal and the Noise: Why So Many Predictions Fail --but Some Don't.}}
	\end{itemize}
\end{frame}
\end{document}


\documentclass[a4paper,12pt,twocolumn]{article}
\usepackage[latin1]{inputenc}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{mathtools}

% Set top and bottom to 1in too.
\usepackage[margin=1in]{geometry}

\usepackage{titling}
\usepackage{nomencl}
\usepackage{siunitx}
\usepackage[style=ieee,backend=bibtex]{biblatex}
\usepackage[font={small}]{caption}

\usepackage{graphicx}
\usepackage{color}
\usepackage[table]{xcolor}

\usepackage{booktabs}
\usepackage{threeparttable}
\usepackage{multirow}
\usepackage{fancyhdr}
\usepackage{float}

\usepackage{varioref}
\usepackage{xspace}
\usepackage{textcomp}
\usepackage[activate={true,nocompatibility},final,tracking=true,kerning=true,spacing=nonfrench,factor=1100,stretch=10,shrink=10]{microtype}
% activate={true,nocompatibility} - activate protrusion and expansion final -
% enable microtype; use "draft" to disable tracking=true, kerning=true,
% spacing=true - activate these techniques factor=1100 - add 10% to the
% protrusion amount (default is 1000) stretch=10, shrink=10 - reduce
% stretchability/shrinkability (default is 20/20) Reduce tracking around small
% caps to 40%
\SetTracking{encoding={*}, shape=sc}{40}

% Document info.
\author{Z0966990}
\title{Numerical Methods in \textsc{Matlab}\textsuperscript{\textregistered}}
\date{\today}

% Path to images.
\graphicspath{{img/}}

% Setup bibiliography.
\renewcommand{\bibfont}{\footnotesize}
\addbibresource{numerical_methods.bib}

% Setup bibiliography.

% Header and footer.
\pagestyle{fancy}
\fancyhf{}
\lhead{\thetitle}
\rhead{\theauthor}
\cfoot{\thepage}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}

\begin{document}

% Title page.
\begin{titlepage}
    \centering
    \vspace*{\fill}
    \includegraphics[width=0.5\textwidth]{Durham.png}\\
    \vspace*{\fill}
    \LARGE\thetitle\\
    \large\theauthor\\
    \large L2 Engineering Mathematics\\
    \large\thedate\\
    \vspace*{\fill}
\end{titlepage}

% Main matter.

\section{Taylor Series Method}

The $p$\textsuperscript{th} order Taylor's series method for \textsc{ode}s finds
numerical approximations to equations of the form $y'(t) = f(t,y)$. In $n$ steps
it approximates the solution $y_n$ at some time $T$ given an initial condition:
{\footnotesize
\begin{equation} \label{eq:taylor-method}
    \begin{aligned}
        t_0 &= 0 &
        y_0 &= y(0) \\
        t_{i+1} &= t_i + h &
        y_{i+1} &= y_i + \sum_{k=1}^{p}\frac{h^k}{k!}f^{(k-1)}(t_i,y_i) \\
        t_n &= T &
        y_n &\approx y(T)
    \end{aligned}
\end{equation}
}where $h = T/n$ and is the step-size.

The use of successive approximations results in an error composed of the
accumulation error and truncation error at each step. At time $T$, this is
the final global error:
{\footnotesize
\begin{equation} \label{eq:error}
        \epsilon_n = |y_n - y(T)| = O(h^p) = O(n^{-p})
\end{equation}

}A \textsc{Matlab} implementation of \eqref{eq:taylor-method} with order 4 was
applied to three \textsc{ode}s of the form $y'(t) = f(t,y)$. These were defined
as follows:
{\footnotesize
\begin{align}
    \tag{a}\label{ode:a}
    y'(t) &= \sin(t) & y(0) &= 0 & 0 &\leq t \leq \pi  \\
    \tag{b}\label{ode:b}
    y'(t) &= 5y(t)   & y(0) &= 1 & 0 &\leq t \leq 0.1 \\
    \tag{c}\label{ode:c}
    y'(t) &= 2       & y(0) &= 0 & 0 &\leq t \leq 1
\end{align}

}\subsection{Results}

The results of the fourth order Taylor series method were compared to the
results of Taylor's second and Euler's method---which is a first order
Taylor's series method. Table~\ref{table:ode} lists $y_n$ for different numbers
of iterations $n$.

\begin{table}[h]
    \centering
    \footnotesize
    \caption{\textsc{Ode} Verification Simulations}
    \label{table:ode}
    \begin{threeparttable}
        \begin{tabular}{
            @{}
            l
            S[table-format=2]
            S[table-format=1.2]
            S[table-format=1.2]
            S[table-format=1.2]
            @{}
        }
            \toprule
            & $n$
            & {\bf Euler\tnote{$\dagger$}}
            & {\bf Taylor 2\textsuperscript{nd}\tnote{$\dagger$}}
            & {\bf Taylor 4\textsuperscript{th}\tnote{$\dagger$}} \\
            \midrule
            \multirow{4}{*}{\rotatebox[origin=c]{90}{
                \shortstack{\textsc{ode}~\eqref{ode:a}\\at $t=\mathrm{\pi}$}}}
            & 10 & 1.99 & 2.03 & 2.00 \\
            & 20 & 2.00 & 2.01 & 2.00 \\
            & 40 & 2.00 & 2.00 & 2.00 \\
            & 80 & 2.00 & 2.00 & 2.00 \\
            \midrule
            \multirow{4}{*}{\rotatebox[origin=c]{90}{
                \shortstack{\textsc{ode}~\eqref{ode:b}\\at $t=0.1$}}}
            & 10 & 1.63 & 1.65 & 1.65 \\
            & 20 & 1.64 & 1.65 & 1.65 \\
            & 40 & 1.64 & 1.65 & 1.65 \\
            & 80 & 1.65 & 1.65 & 1.65 \\
            \midrule
            \multirow{4}{*}{\rotatebox[origin=c]{90}{
                \shortstack{\textsc{ode}~\eqref{ode:c}\\at $t=1$}}}
            & 10 & 2.00 & 2.00 & 2.00 \\
            & 20 & 2.00 & 2.00 & 2.00 \\
            & 40 & 2.00 & 2.00 & 2.00 \\
            & 80 & 2.00 & 2.00 & 2.00 \\
            \bottomrule
        \end{tabular}
        \begin{tablenotes}
            \item[$\dagger$] Accurate to 3 significant figures.
        \end{tablenotes}
    \end{threeparttable}
\end{table}

\textsc{Ode}~\eqref{ode:c} had no non-zero derivatives in $f(t,y)$, so Euler's
method contained sufficiently many terms to find $y_n$ when $T=1$. In fact,
\textsc{ode}~\eqref{ode:c} converged superlinearly for all methods. For
\textsc{ode}s \eqref{ode:b} and \eqref{ode:c}, many more iterations were
required for the lower order methods to converge on a value for $y_n$ as the
higher order coefficients were larger.

The exact errors of the results in Table~\ref{table:ode} are detailed in
Table~\ref{table:err}, in which \textsc{ode}s \eqref{ode:a} and \eqref{ode:b}
demonstrate how increasing the number of iterations reduces the final global
error, as suggested in \eqref{eq:error}. Equation~\eqref{eq:error} also
suggests increasing the order of the Taylor's series method reduces the final
global error. Particularly for \textsc{ode}~\eqref{ode:b}, doubling the order
of the method halved the magnitude of the error.

\begin{table}[h]
    \centering
    \footnotesize
    \caption{\textsc{Ode} Errors}
    \label{table:err}
    \begin{threeparttable}
        \begin{tabular}{
            @{}
            l
            S[table-format=2]
            S[table-format=1.2e+2]
            S[table-format=1.2e+2]
            S[table-format=1.2e+2]
            @{}
        }
            \toprule
            & {$n$}
            & {\bf\shortstack{Euler\\Error\tnote{$\dagger$}}}
            & {\bf\shortstack{Taylor 2\textsuperscript{nd}\\
                Error\tnote{$\dagger$}}}
            & {\bf\shortstack{Taylor 4\textsuperscript{th}\\
                Error\tnote{$\dagger$}}}\\
            \midrule
            \multirow{4}{*}{\rotatebox[origin=c]{90}{
                \shortstack{\textsc{Ode}~\eqref{ode:a}\\at $t=\mathrm{\pi}$}}}
            & 10 & 1.65e-02 & 3.29e-02 & 1.62e-04 \\
            & 20 & 4.11e-03 & 8.22e-03 & 1.01e-05 \\
            & 40 & 1.03e-03 & 2.06e-03 & 6.34e-07 \\
            & 80 & 2.57e-04 & 5.14e-04 & 3.96e-08 \\
            \midrule
            \multirow{4}{*}{\rotatebox[origin=c]{90}{
                \shortstack{\textsc{Ode}~\eqref{ode:b}\\at $t=0.1$}}}
            & 10 & 1.98e-02 & 3.31e-04 & 4.12e-08 \\
            & 20 & 1.01e-02 & 8.43e-05 & 2.63e-09 \\
            & 40 & 5.10e-03 & 2.13e-05 & 1.66e-10 \\
            & 80 & 2.56e-03 & 5.34e-06 & 1.04e-11 \\
            \midrule
            \multirow{4}{*}{\rotatebox[origin=c]{90}{
                \shortstack{\textsc{Ode}~\eqref{ode:c}\\at $t=1$}}}
            & 10 & 2.22e-16 & 2.22e-16 & 2.22e-16 \\
            & 20 & 4.44e-16 & 4.44e-16 & 4.44e-16 \\
            & 40 & 8.88e-16 & 8.88e-16 & 8.88e-16 \\
            & 80 & 3.11e-15 & 3.11e-15 & 3.11e-15 \\
            \bottomrule
        \end{tabular}
        \begin{tablenotes}
            \item[$\dagger$] Accurate to 3 significant figures.
        \end{tablenotes}
    \end{threeparttable}
\end{table}

For \textsc{ode}~\eqref{ode:c}, higher order methods behave as Euler's method,
so the order of the method has no effect on accuracy. Contrary to
\eqref{eq:error} the error increased with the number of iterations. This can be
explained by the round off error in $h$. When $n=10$, binary64 format cannot
encode every bit of the recurring binary fraction
\mbox{$h=0.0\dot{0}01\dot{1}_2$}. As the number of iterations $n$ increase,
more round off error accumulates.

\section{Finite Difference\\Method}

The convection--diffusion--reaction equation can be used to describe how mass
concentration $u$ varies in space $\mathbf{x}$ with time $t$. The parabolic
equation includes convection $\mathbf{v}\cdot\nabla u$, wherein the mass has a
net velocity $\mathbf{v}$; diffusion $c^2\nabla^2u$, where random motion
disperses mass over time; and reaction $du$, a sink or source of mass---present
when chemical reactions are taking place.

The equation can also be applied to heat transfer, where $u$ represents heat.

In \textsc{1d}, the convection--diffusion--reaction equation is defined as
follows:
{\footnotesize
    \begin{multline} \label{eq:pde}
            \forall x,t \in (a, b) \times (0, T] \\
            u_t(x,t) = c^2u_{xx}(x,t) + vu_x(x,t) + du(x,t)
    \end{multline}
}with homogenous Dirichlet boundary conditions and initial condition
\mbox{$u(x,0)=f(x)$} applied.

A \textsc{fdm} schema was devised to numerically solve \eqref{eq:pde}, second order in
space with interval $h$, first order in time with time-step $k$, and
implemented in \textsc{Matlab} as follows:
{\footnotesize
    \begin{multline} \label{eq:schema}
        u^i_{j+1} = u^i_j + dku^i_j + \frac{vk}{h}(u^{i+1}_j - u^i_j) \\
            + \frac{c^2k}{h^2}(u^{i+1}_j - 2u^i_j + u^{i-1}_j)
    \end{multline}

}Errors in \textsc{fdm} schema can grow or be damped out at each time-step,
leading to a stable and accurate, or unstable solutions. Von Neumann stability
analysis assumes these errors are amplified by a constant
factor~\cite{charney1950numerical}. An expression for amplification factor is
determined by expressing the errors as a Fourier series in space. Stability
necessitates the amplification factor is not much greater than one for all  frequencies resolvable in the grid.

Applying von Neumann stability analysis to \eqref{eq:schema} yielded the
following stability criterion, and expression for the amplification factor:
{\footnotesize
    \begin{gather}
        \label{eq:stability}
        \begin{split}
            G = \operatorname{max}\left|
            \begin{array}{r}
                1 + R + (2S + C)(\cos\theta - 1)\\
                +\,\operatorname{i}C\sin\theta
            \end{array}
            \right| \leq 1 + \epsilon k
        \end{split} \\
        \nonumber
        \begin{aligned}
            R &= dk & C &= \frac{vk}{h} & S &= \frac{c^2k}{h^2}
        \end{aligned}
    \end{gather}
}where $R$, $C$ and $S$ are dimensionless quantities: the reaction, Courant and
diffusion numbers. $\epsilon$ is a constant assumed to be smaller than
\num{800e-3}.

The model was tested on the following:
{\footnotesize
    \begin{align*}
        c &= 1    & a &= 0   & m,n  =&\;\{10,100,200,400,800\} \\
        v &= 0.5  & b &= 1   &       &\;\times \{4,16,32\} \\
        d &= 0.02 & T &= 0.2 & f(x) =&\;4x - 4x^2
    \end{align*}

}With these coefficients fixed, the amplification factor in \eqref{eq:stability}
varies as follows:
{\footnotesize
    \begin{equation} \label{eq:gain}
        G = O(k/h^2) = O(n^2/m)
    \end{equation}

}\subsection{Results}

Table~\ref{table:pde} lists the value of \eqref{eq:pde} at the midpoint of the
interval at time $T$, for each $n$ and $m$.

\begin{table}[h]
    \centering
    \footnotesize
    \caption{\textsc{Pde} Verification Simulations}
    \label{table:pde}
    \begin{threeparttable}
        \begin{tabular}{
            @{\hspace{1em}}
            S[table-format=2]
            S[table-format=3]
            S[table-format=+2.2e2]
            S[table-format=2.2]
            @{\hspace{1em}}
        }
            \toprule
            {$n$} & {$m$} & {$u(0.5,0.2)$\tnote{$\dagger$}} &
                {$G$\tnote{$\dagger$}}\\
            \cmidrule(rl){1-3}\cmidrule(rl){4-4}
             4 &  10 &  0.111   &  1.00 \\\rowcolor{red!20}
            16 &  10 & -1.32e7  & 19.8  \\\rowcolor{red!20}
            32 &  10 & 22.7     & 81.6  \\
             4 & 100 &  0.137   &  1.00 \\\rowcolor{red!20}
            16 & 100 &  0.102   &  1.08 \\\rowcolor{red!20}
            32 & 100 & -3.71e80 &  7.26 \\
             4 & 200 &  0.139   &  1.00 \\
            16 & 200 &  0.138   &  1.00 \\\rowcolor{red!20}
            32 & 200 & -3.65e93 &  3.13 \\
             4 & 400 &  0.139   &  1.00 \\
            16 & 400 &  0.138   &  1.00 \\\rowcolor{red!20}
            32 & 400 & -5.65e4  &  1.06 \\
             4 & 800 &  0.140   &  1.00 \\
            16 & 800 &  0.139   &  1.00 \\
            32 & 800 &  0.140   &  1.00 \\
            \bottomrule
        \end{tabular}
        \begin{tablenotes}
            \item\colorbox{red!20}{Unstable: unable to satisfy
                \eqref{eq:stability}.}
            \item[$\dagger$] Accurate to 3 significant figures.
        \end{tablenotes}
    \end{threeparttable}
\end{table}

The highlighted simulations with a gain greater than unity did not appear to
converge on similar values to those determined stable by \eqref{eq:stability}.
This occurred when there were too many intervals in space for a given number of
time-steps, as predicted by \eqref{eq:gain}.

% References.
\printbibliography

\end{document}