\documentclass[10pt,letterpaper]{article}

\usepackage{amsmath}
\usepackage{tikz}
\usepackage{hyperref}

\newcommand{\volume}{{\ooalign{\hfil$V$\hfil\cr\kern0.08em--\hfil\cr}}}

\author{Thaddeus Hughes \\ hughes.thad@gmail.com \\ thaddeus-maximus.github.io}
\date{\today}
\title{Adiabatic Model of a Pneumatic Cylinder}

\begin{document}
	\maketitle

	\begin{abstract}
		There are some basic models for pneumatic cylinders that result in modeling cylinders as constant-force devices, or nearly so. This is a rough first pass at creating a model that is a little more sophisticated. This model assumes that the air in the cylinder is adiabatic; that is, no heat transfer takes place. This model will be a differential equation intended to be used in conjunction with a numerical DE solver due to the non-linear nature of many conditions.

		Pneumatic pistons can be used for many scenarios- they can act as quick stops, can push or pull heavy loads, or can be configured to quickly launch objects. Depending on the nature of the system, they can behave quite differently, and this model may capture more of the underlying physics that affects these various use cases.
	\end{abstract}

	\section*{System Definition}

	We will analyze the open system that is the air contained within a pneumatic cylinder, plus the air in the hose leading up to it (inclusion of the hose will become of crucial importance and will be evident later on).
	\begin{center}
	\begin{tikzpicture}[x=1.0in,y=1.0in]
		\draw[->] (-0.5,0)--(-0.1,0) node[pos=0, left]{$P_{in}$, $\dot{\volume}_{in}$, $T_{in}$};

		\draw[] (0.3,-0.1)--(0,-0.1)--(0,0.1)--(0.3,0.1)--(0.3,0.5)--(1.2,0.5)--(1.2,-0.5)--(0.3,-0.5)--cycle;
		\node (state) [text width=0.4] at (0.65,0) {$m_s$ \\ $P_s$ \\ $\volume_s$ \\ $T_s$};
		\draw[->] (1.0, 0.6)--(1.4, 0.6) node[pos=0.5, above] {$v$, $x$};
		%\draw[->] (1.9,0)--(1.4,0) node[pos=0, above] {$F = P_{atm} A$};
	\end{tikzpicture}
	\end{center}

	Air at pressure $P_{in}$ and temperature $T_{in}$ enters the cylinder at rate $\dot{\volume}_{in}$.
	It mixes with air in the cylinder ($m_s$, $\volume_s$) and is assumed to become of uniform pressure and temperature $P_s$ and $T_s$.

	The piston ram is moving at velocity $v$ and with position $x$. $x$ is zero when the cylinder is bottomed out (no air in the cylinder).

	\begin{center}
	\begin{tikzpicture}[x=1.0in,y=1.0in]
		\draw[ultra thick] (0,-0.5)--(0,0.5);
		\draw[] (0,-0.15)--(0,0.15)--(1,0.15)--(1,-0.15)--cycle;

		\node (ctr) [] at (0.5,0) {$M$};

		\draw[->] (-0.3,0)--(-0.1,0) node[pos=0,left]{$P_s A$};
		\draw[->] (-0.3,0.2)--(-0.1,0.2); \draw[->] (-0.3,0.4)--(-0.1,0.4); \draw[->] (-0.3,-0.2)--(-0.1,-0.2); \draw[->] (-0.3,-0.4)--(-0.1,-0.4);

		\draw[->] (1.3,0)--(1.1,0) node[pos=0,right]{$F_{load}$};
		\draw[->] (0.3,0.3)--(0.1,0.3) node[pos=0,right]{$P_{atm} A$};
		\draw[->] (0.3,-0.3)--(0.1,-0.3);
	\end{tikzpicture}
	\end{center}

	The cylinder itself is considered to have a mass $M$ and sees the cylinder pressure, a resistive force $F_{load}$, and atmospheric pressure.

	\section*{Conservation Principles In The Cylinder}

	First, let's apply conservation of mass to the air.

	\begin{align}
		\frac{d}{dt} m_{sys} &= \sum_{+ in} \dot{m} \nonumber
	\end{align}\begin{align} \label{eq:ddt_m}
		\frac{d}{dt} m_s &= \dot{m}_{in}
	\end{align}

	We can use the ideal gas law $P \volume = m R T$ to write this in terms of knowns.
	\begin{align}
		\dot{m}_{in} = \frac{P_{in} \dot{\volume}_{in}}{R T_{in}} \nonumber
	\end{align}

	We'll assume that the primary flow restriction comes from the tube. We can use an equation derived from \href{https://www.engineeringtoolbox.com/pressure-drop-compressed-air-pipes-d_852.html}{this empirical pipe flow equation in The Engineering ToolBox}.

	\begin{align}
		\dot{\volume} = 0.0197862 (d_{tube}^5 [P_{in}-P_{atm}] [P_{in}-P_{s}]/L_{tube})^{20/37} \nonumber
	\end{align}
	(Assuming units of $Pa$, $m$, and $m^3/s$)

	\begin{align} \label{eq:sup_mdot}
		\dot{m}_{in} = \frac{0.0197862}{R T_{in}} (d_{tube}^5 [P_{in}-P_{atm}] [P_{in}-P_{s}]/L_{tube})^{20/37}
	\end{align}

	Now, let's apply conservation of energy.

	\begin{align}
		\frac{d}{dt} E_{sys} = \sum \dot{Q}_{in} + \sum \dot{W}_{in} + \sum_{+in} \dot{m} (v^2/2 + gz + h) \nonumber
	\end{align}

	We will assume:
	\begin{itemize}
		\item The energy of the system is entirely thermal; $E_sys$ = $m_s u(T)$
		\item No heat transfer; $\dot{Q}_{in} = 0$
		\item Work is out of the system, defined by the pressure of the system and velocity of the ram; $\dot{W}_{in} = - \vec{F} \cdot \vec{v} = - P_s A v$
		\item Mass transfer into the system has negligible velocity ($v$) and head height ($gz$), leaving only enthalpy $h(T)$.
	\end{itemize}

	\begin{align}
		\frac{d}{dt} [m_s u(T)] = - P_s A v + \dot{m}_{in} (h(T_{in} + (\frac{\dot{\volume}}{\pi / 4 \ d_{tube}})^2) \nonumber
	\end{align}

	The derivative here is quite ugly, since both the mass and temperature of the system are changing. However, it can be broken up with the product rule ($\frac{d}{dt}[xy] = y \frac{dx}{dt} + x \frac{dy}{dt}$).

	\begin{align}
		m_s \frac{d u(T_s)}{dt} + u(T_s) \frac{d m_s}{dt} = - P_s A v + \dot{m}_{in}  (\frac{\dot{\volume}}{\pi / 4 \ d_{tube}})^2 + \dot{m}_{in} h(T_{in}) \nonumber \\
		m_s \frac{d u(T_s)}{dt} = \dot{m}_{in} h(T_{in}) - P_s A v - u(T_s) \frac{d m_s}{dt} + \dot{m}_{in}  (\frac{\dot{\volume}}{\pi / 4 \ d_{tube}})^2\nonumber
	\end{align}

	Recognizing that $u(T)$ and $h(T)$ can be approximated as $u(T) = c_p T$ and $h(T) = c_v T$, we can then solve the equation for $\frac{dT_s}{dt}$.

	\begin{align} \label{eq:ddt_T}
		\frac{d T_s}{dt} = \frac{ \dot{m}_{in}[ c_v T_{in} - c_p T_s + (\frac{\dot{\volume}}{\pi / 4 \ d_{tube}})^2]     - P_s A v}{c_p m_s}
	\end{align}

	All of these are knowns or states aside from $P_s$, which can be determined with the ideal gas law, and the modeling of the system volume as based on cylinder extension, crossectional area, and initial (i.e. hose) dead volume.

	\begin{align}
		P_s = \frac{m_s R T_s}{\volume_s} \nonumber
	\end{align}\begin{align} \label{eq:sup_P}
		P_s = \frac{m_s R T_s}{\volume_{dead} + x A}
	\end{align}

	\section*{Conservation Principles on the Piston}

	We can simply apply conservation of linear momentum to the piston.

	\begin{align}
		\frac{d}{dt} P_{sys}= \sum F + \sum \dot{m} v \nonumber
	\end{align}

	We will assume:
	\begin{itemize}
		\item No mass transfer in this system, so $\frac{d}{dt} P_{sys} = M \frac{dv}{dt}$ and $\dot{m}=0$
		\item The forces acting on the piston are the cylinder pressure, atmospheric pressure, and the external load.
	\end{itemize}

	\begin{align}
		M \frac{dv}{dt} &= P_s A - P_{atm} A - F_{load} \nonumber
	\end{align}\begin{align}  \label{eq:ddt_v}
		\frac{dv}{dt} &= \frac{(P_s - P_{atm}) A - F_{load}}{M}
	\end{align}

	We are also interested in the position of the piston.

	\begin{align} \label{eq:ddt_x}
		\frac{dx}{dt} = v
	\end{align}

	\section*{Limiting the Model}
	In review we have:

	\begin{itemize}
		\item A model for rate of change of $m_s$
		\item A model for rate of change of $T_s$
		\item Supporting equation for cylinder pressure $P_s$
		\item Supporting equation for massflow into cylinder $\dot{m}$
		\item A model for velocity $v$
		\item A model for position $x$
	\end{itemize}

	We simply now need initial conditions, "bumper" conditions, and termination criteria.

	We will assume the initial temperature $T_s$ will be set to that of the input gas $T_{in}$. (This may seem like it would negate all the point of this model, but recall that when gases expand/contract, they change pressure)

	\begin{align} \label{eq:ic_T}
		T_s(t=0) = T_{in}
	\end{align}

	The initial mass of cylinder air can be found with the ideal gas law, assuming we start at atmospheric pressure (wholly unpressurized)

	\begin{align} \label{eq:ic_m}
		m_s(t=0) = \frac{P_{atm} L_{tube} \frac{\pi}{4} d_{tube}^2}{R T_s(t=0)}
	\end{align}

%	There's an interesting edge case that our model doesn't look at, and that's when we overfill the cylinder. Air can't keep coming into our cylinder. An orifice model may be more appropriate, but let's just say that if the cylinder pressure is greater than the input pressure, no more air can be added.

%	\begin{equation} \label{eq:bump_pres}
%		\mbox{if } P_s > P_{in} \mbox{ then } \dot{m}_{in} = 0
%	\end{equation}

	Assume the piston starts from rest.

	\begin{align} \label{eq:ic_x}
		x(t=0) &= 0
	\end{align}\begin{align}\label{eq:ic_v}
		v(t=0) &= 0
	\end{align}

	And that the piston cannot go past its endstops at $x=x_f$ or $x=0$.

	\begin{align} \label{eq:bump_end}
		\mbox{if } x \geq x_f &\mbox{ then } x = x_f, v \leq 0
	\end{align}\begin{align}\label{eq:bump_start}
		\mbox{if } x \leq 0   &\mbox{ then } x = 0, v \geq 0
	\end{align}

	We will terminate our simulation when we reach the endstop and fully pressurize.

	\begin{align} \label{eq:term}
		\mbox{terminate simulation if } x \geq x_f \mbox{ and } P_s \geq P_{in}
	\end{align}

	\section*{The Full Model}

	\begin{align}
		\dot{m}_{in} &= \frac{0.0197862}{R T_{in}} (d_{tube}^5 [P_{in}-P_{atm}] [P_{in}-P_{s}]/L_{tube})^{20/37} \tag{\ref{eq:sup_mdot}} \\
		P_s &= \frac{m_s R T_s}{L_{tube} \frac{\pi}{4} d_{tube}^2 + x A} \tag{\ref{eq:sup_P}} \\
		\mbox{if } x \geq x_f &\mbox{ then } x = x_f, v \leq 0 \tag{\ref{eq:bump_end}} \\
		\mbox{if } x \leq 0   &\mbox{ then } x = 0, v \geq 0 \tag{\ref{eq:bump_start}} \\
		\frac{d}{dt} m_s &= \dot{m}_{in} \tag{\ref{eq:ddt_m}} \\
		\frac{d T_s}{dt} &= \frac{ \dot{m}_{in} c_v T_{in} - P_s A v - c_p T_s \dot{m}_{in} }{c_p m_s} \tag{\ref{eq:ddt_T}} \\
		\frac{dv}{dt} &= \frac{(P_s - P_{atm}) A - F_{load}}{M} \tag{\ref{eq:ddt_v}} \\
		\frac{dx}{dt} &= v \tag{\ref{eq:ddt_x}} \\
		T_s(t=0)& = T_{in} \tag{\ref{eq:ic_T}} \\
		m_s(t=0) &= \frac{P_{atm} L_{tube} \frac{\pi}{4} d_{tube}^2}{R T_s(t=0)} \tag{\ref{eq:ic_m}} \\
		v(t=0) &= 0 \tag{\ref{eq:ic_v}} \\
		x(t=0) &= 0 \tag{\ref{eq:ic_x}} \\
		\mbox{terminate}& \mbox{ if } x \geq x_f \mbox{ and } P_s \geq P_{in} \tag{\ref{eq:term}}
	\end{align}

	There's a lot going on here. But let's do a sanity check and see if there's going to be any issues going forth.

	The big equation of interest is $dT_s/dt$.
	\begin{itemize}
		\item If the heat capacity or system mass is larger, then the rate of temperature change decreases (makes sense)
		\item If the cylinder expands ($v > 0$) then the gas expands, lowering the temperature.
		\item Let's focus on this term
			\begin{align}
				  & \dot{m}_{in} c_v T_{in} - c_p T_s \dot{m}_{in} \nonumber \\
				=\ & \dot{m} [c_v T_{in} - c_p T_s] \nonumber \\
				=\ & \dot{m} [h(T_{in}) - u(T_s)] \nonumber \\
				=\ & \dot{m} [u(T_{in}) + P_{in} / \rho_{in} - u(T_s)] \nonumber \\
				\approx \ & \dot{m} [u(T_{in}-T_s) + P_{in} / \rho_{in}] \nonumber
			\end{align}

			Recognizing that $T_in > T_s$ since the cylinder is expanding, we see that yes, the massflow into the system corresponds to higher thermal energy transfer, and higher pressure energy transfer.
		\item The mass of the system $m_s$ is in the denominator - if this is zero, the derivative would not exist, so it is imperative that we begin our simulation with some mass (hence, the dead mass $L_{tube} \frac{\pi}{4} d_{tube}^2$ term)
	\end{itemize}

	\section*{Implemented Simulation and Examples}

	This has been implemented into my \href{http://thaddeus-maximus.github.io/swissarmyengineer/}{Swiss Army Engineer} suite. Here are some example cases, starting with a base case.

	\includegraphics[width=\textwidth]{pneu_case_1.png}

	This case has mass, and a load much higher than the mass (simulating, say, a piston pushing on a spring). The results aren't too interesting - air inrushes into the cylinder rather quickly, producing the quadratic position profile you'd expect of a constant-force system.

	\includegraphics[width=\textwidth]{pneu_case_2.png}

	This case increases the mass by ten times. The timescale, accordingly, stretches out, taking much longer to reach its end stop.

	\includegraphics[width=\textwidth]{pneu_case_3.png}

	This case removes the load and returns to the lighter system mass, so that this is a purely inertial system. In this case, the piston accelerates like normal, but the cylinder pressure actually begins to decrease as the inertia of the system carries it further forwards. Eventually the hardstop is hit, and at this point the piston can fully pressurize with air.

	What happens if we choke it out even further, by shrinking the line diameter?

	\includegraphics[width=\textwidth]{pneu_case_4.png}

	The line diameter here has been shrunk to 2mm. We see this same decayed acceleration as in the previous example, but to a larger degree.

	\includegraphics[width=\textwidth]{pneu_case_5.png}

	If we add a large load, the position curve becomes linear, as the cylinder has to fight the heavy load and its air supply eventually becomes the limiting factor on speed.

	\includegraphics[width=\textwidth]{pneu_case_6.png}

	If we make the load an expression, we could model a piston pulling on a spring, giving us this very neat mass-spring system.

	\section*{Validation}
		No validation has been performed yet on these models, as I don't have access to the equipment to test them. If anyone wants to perform this testing, it would be very cool!

	\section*{Future Improvements (and when to say no)}
		The next obvious improvement to make would be to model the flow restriction provided by valves. Beyond that, looking at pressure drop in the tank as air is consumed, or flow restriction as a result of the pressure regulator seems to me to be splitting hairs, and chasing high-speed actuation which would require significant testing anyways.

\end{document}


%! Author = tstreule

\section{SPECT \textnormal{-- Single Photon Emission CT}}

$\textrm{SNR} \propto \sqrt{\textrm{total \#detected gamma-rays}}$
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Radioactivity in the body}
%
\highlight{$Q = -\frac{dN}{dt} = \lambda \cdot N \implies N(t) = N_0 \eu^{-\lambda t}$}\\
$[Q] = \unit{Curie} = \unit{Ci} = 3.7 \cdot 10^{10} \unit{Bq}$
\quad where \quad $1\unit{Bq} = 1\unitfrac{disintegration}{s}$

\textbf{Physical} $t\ped{1/2} = \frac{\ln 2}{\lambda}$, \quad \textbf{biological} $t\ped{1/2 bio} = \frac{\ln 2}{\lambda\ped{bio}}$

$N(t) = N_0 \eu^{-(\lambda + \lambda\ped{bio}) t} = N_0\eu^{-\lambda\ped{1/2 eff}t}$
\quad where \quad $t\ped{1/2 eff} = \frac{t\ped{1/2} \cdot t\ped{1/2 bio}}{t\ped{1/2} + t\ped{1/2 bio}}$

\textbf{$\gamma$ photons: Absorption} or \textbf{Scattering}: Change of direction by $\theta$. \highlight{$E_{\gamma}' = \frac{m_e \cdot c^2}{m_e c^2 / E_\gamma + 1 - \cos \theta}$}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Interaction of $\gamma$ photons with matter}
%
\textbf{Absorption} leads to ejection of orbital electron\\
\textbf{Scattering}: Change of direction by $\theta$. \highlight{$\displaystyle E_{\gamma}' = \frac{m_e \cdot c^2}{m_e c^2 / E_\gamma + 1 - \cos \theta}$}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Anger Camera}
\textbf{Collimation} necessary $\implies$ losses of 99.9\%

\includegraphics[width = \linewidth]{SPECT_NuclearCollimator}
Bad isolation of \textbf{septa} (lamellae) $\implies L\ped{eff} = L - \frac{2}{\mu\ped{septa}}$\\
%
\highlight{$\displaystyle R_0(x) = \frac{2 r_{col}}{L_{\text{eff}}}(x + \frac{L_{\text{eff}}}{2})$} \; \highlight{$\displaystyle \textrm{FWHM} = \Delta r_{col}(x) = \frac{2 r_{col}}{L_{\text{eff}}}(x + L_{\text{eff}})$}\\

\textbf{Signal Path}: $\gamma$-rays @140keV $\to$ Scintillation crystal $\to$ PMT $\to$ (Pos. network || pulse height analyzer) $\to$ digitizer
\begin{itemize}
    \item Scintillation crystal efficiency: \highlight{$\displaystyle \epsilon = 1 - \eu^{-\mu d}$}. $\mu$: attenuation coefficient of crystal, $d$: its thickness

    \item Usually many PMTs per crystal. Positioning network calculates where the event in the crystal was.

    \item $U_{PMT} \propto \gamma$ energy. Scattering $\implies$ $E\ped{scattered photons}\downarrow$. Pulse height analyzer has energy window $\Delta E$ around $E_0$.

    \item Poisson distributed $P_n(N)\vert \sigma^2 = \mu \implies \highlight{SNR = \sqrt{\mu}}$

    \item \textbf{Spatial resolution}: $R_{system}^2 = R\ped{gamma}^2 + R_{coll}^2$ \quad $R\ped{gamma}$ = uncertainty of positioning network. $R\ped{coll}$ see above.
\end{itemize}

\textbf{CT Version}: Res.: best at edge, worst in center. $\approx$ 7mm\\

For small objects: Use \textbf{pinhole} (cone-shaped, tungsten) camera: Problem: more dose needed, ``transparency'' around hole
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\subsection{Generation of Technetium}
No cyclotron needed, only generator: Essentially \ce{^{99}_{42}Mo} ($\unit[67]{h} \;\widehat{=}\; \unit[2.9\E{-6}]{s^{-1}}$) decays into \ce{^{99}_{43}Tc} ($\unit[6]{h} \;\widehat{=}\; \unit[3.1\E{-5}]{s^{-1}}$)

\textbf{Kinetic equations}:\\
$\deriv{~}{t}N_{Mo} = -k_{Mo}N_{Mo}$ and $\deriv{~}{t}N_{Tc} = k_{Mo}N_{Mo} - k_{Tc} N_{Tc}$\\
\highlight{$\displaystyle \implies N_{Tc}(t) = N_{Mo}(0) \frac{k_{Mo}}{k_{Tc} - k_{Mo}} (e^{-k_{Mo}t} - e^{-k_{Tc} t})$} {\scriptsize exp. decay}


% This is part of the TFTB Reference Manual.
% Copyright (C) 1996 CNRS (France) and Rice University (US).
% See the file refguide.tex for copying conditions.

\markright{margtfr}
\section*{\hspace*{-1.6cm} margtfr}

\vspace*{-.4cm}
\hspace*{-1.6cm}\rule[0in]{16.5cm}{.02cm}
\vspace*{.2cm}

{\bf \large \sf Purpose}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
Marginals and energy of a time-frequency representation.
\end{minipage}
\vspace*{.2cm}

{\bf \large \sf Synopsis}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
\begin{verbatim}
[margt,margf,E] = margtfr(tfr)
[margt,margf,E] = margtfr(tfr,t)
[margt,margf,E] = margtfr(tfr,t,f)
\end{verbatim}
\end{minipage}
\vspace*{.3cm}

{\bf \large \sf Description}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
        {\ty margtfr} calculates the time and frequency marginals and the
        energy of a time-frequency representation. The definitions used for
        the computation are the following\,:
\begin{eqnarray*}
m_f(t)=\int_{-\infty}^{+\infty} \mbox{tfr}(t,f)\ df && \mbox{\it time
marginal}\\
m_t(f)=\int_{-\infty}^{+\infty} \mbox{tfr}(t,f)\ dt && \mbox{\it frequency
marginal}\\
E= \int_{-\infty}^{+\infty}\int_{-\infty}^{+\infty} \mbox{tfr}(t,f)\ df\ dt && \mbox{\it energy}
\end{eqnarray*}

\hspace*{-.5cm}\begin{tabular*}{14cm}{p{1.5cm} p{8.5cm} c}
Name & Description & Default value\\
\hline
        {\ty tfr} & time-frequency representation {\ty (M,N)}\\
        {\ty t}   & vector containing the time samples in sec.
               & {\ty (1:N)}\\
        {\ty f}   & vector containing the frequency samples in Hz, not
               necessary uniformly sampled & {\ty (1:M)}\\
 \hline {\ty margt} & time marginal\\
        {\ty margf} & frequency marginal\\
        {\ty E}     & energy of {\ty tfr}\\

\hline
\end{tabular*}

\end{minipage}
\vspace*{.5cm}

{\bf \large \sf Example}
\vspace*{-.1cm}
\begin{verbatim}
         S=amgauss(128).*fmlin(128);
         [tfr,t,f]=tfrscalo(S,1:128,8,.05,.45,128,1);
         [margt,margf,E] = margtfr(tfr);
         subplot(211); plot(t,margt);
         subplot(212); plot(f,margf);
\end{verbatim}
\vspace*{.3cm}

{\bf \large \sf See Also}\\
\hspace*{1.5cm}
\begin{minipage}[t]{13.5cm}
\begin{verbatim}
momttfr, momftfr.
\end{verbatim}
\end{minipage}


\chapter[Additional Derivations and Formulae]{Additional Derivations and \\ Formulae}
\label{app:derivations}
\stoptocwriting
This appendix outlines further derivations and formulae used throughout this thesis.

\section{Ellipse Geometry}
\label{app:derivellipse}

Section \ref{s:trphysprop} considers the change in area when distorting a unit circle to an ellipse with the same circumference.
An ellipse can be defined in terms of the major and minor axis radii, denoted $a$ and $b$ respectively.
The distortion can then be described by the eccentricity, $\xi$, given by:
\begin{equation}
	\xi = \left(1-\frac{b^2}{a^2}\right)^{1/2}.
\end{equation}
The circumference of an ellipse of given eccentricity, $C\left(\xi\right)$, can then be calculated from the complete elliptic integral of the second kind,
\begin{equation}
	\label{eq:ellipsec}
	C\left(\xi\right) = 4a\int_0^{\pi/2} \left(1-\xi^2\sin^2\theta\right)^{1/2}\dd\theta,
\end{equation}
whilst the area is given more straightforwardly by
\begin{equation}
	A = \pi ab.
\end{equation}
The relative area between an ellipse and a unit circle for a given eccentricity is then
\begin{equation}
 	A/A^0=ab,
\end{equation}
where $a$,$b$ satisfy $C\left(\xi\right)=2\pi$.

\section{Aboav\--Weaire with aG}
\label{app:derivag}

Section \ref{s:toptmapconfigspace} considers the meaning of the \aw{} parameter for aG systems \ie{} those containing just 5\--, 6\-- and 7\--rings.
For these relatively constrained systems, $\alpha$ can be related specifically to the proportions of specific ring adjacencies.
To derive this relationship, results are used from sections \ref{s:theorynodeprob} and \ref{s:toptmapconfigspace}.

The aG system has the node joint degree distribution
\begin{align}
	\mathbf{e} &= \begin{blockarray}{*{3}{c} l}
	\begin{block}{*{3}{>{$\footnotesize}c<{$}} l}
	5 & 6 & 7 \\
	\end{block}
	\begin{block}{[*{3}{c}]>{$\footnotesize}l<{$}}
	e_{55} & e_{56} & e_{57} \: \bigstrut[t]& 5\\
	e_{65} & e_{66} & e_{67} & 6 \\
	e_{75} & e_{76} & e_{77} & 7\\
	\end{block}
	\end{blockarray} .
\end{align}
Taking the \aw{} law, equation \eqref{eq:aboavweaire}, and noting that $m_5=\sumk ke_{5k}/q_5$, leads to the relationship
\begin{equation}
	\frac{5}{q_5}\left(5e_{55}+6e_{56}+7e_{57}\right) = \ki^2+\mu_2+\ki\left(1-\alpha\right)\left(5-\ki\right),
\end{equation}
to which several simplifications can be made.
These arise from the constraints $\ki=6$ and $\sumk e_{5k}=q_5$, which on substitution and rearrangement yield:
\begin{align}
	\frac{5}{q_5}\left(5e_{55}+6\left(q_5-e_{55}-e_{57}\right)+7e_{57}\right) &= 36+\mu_2-6\left(1-\alpha\right) \nonumber \\
	\frac{5}{q_5}\left(e_{57}-e_{55}\right)&=\mu_2+6\alpha.
\end{align}
This can be further simplified by applying the relationships $q_5=5p_5/6$, $p_5=\left(1-p_6\right)/2$, $\mu_2=1-p_6$ and introducing the parameter $\chi_{75}^{5}=e_{57}-e_{55}$, to obtain:
\begin{equation}
	\alpha=\frac{12\chi_{75}^5-\left(1-p_6\right)^2}{6\left(1-p_6\right)}.
\end{equation}
This final relationship is the same as equation \eqref{eq:agalpha}, which expresses the \aw{} parameter in terms of the difference between the 5\--7 and 5\--5 ring adjacencies.

\clearpage
\section{Relating Aboav\--Weaire to Assortativity}
\label{app:derivalphaassort}

Section \ref{s:assortativity} provides a relationship between the assortativity  and the \aw{} parameter, the derivation for which is detailed here.
The assortativity is defined
\begin{equation}
	r = \frac{\sumjk jk\left(e_{jk}-q_jq_k\right)}{\sumk k^2q_k - \left(\sumk kq_k\right)^2}\,,
\end{equation}
which can be rewritten by noting that $q_k=kp_k/\ki$, in the form
\begin{equation}
	\label{appeq:assort}
	r = \frac{\ki^2\sumjk jke_{jk}-\kii^2}{\ki\kiii-\kii^2}\,.
\end{equation}
The mean node degree about a node of degree $j$ is given by $m_j=\frac{1}{q_j}\sumk e_{jk}$, which leads to the relationship
\begin{equation}
	\sumjk jk e_{jk} = \sumj jq_jm_j = \frac{1}{\ki}\sumj jp_j jm_j\,,
\end{equation}
that contains within it the left hand component of the \aw{} law, equation \eqref{eq:aboavweaire}.
Substituting and simplifying gives:
\begin{align}
	\sumjk jk e_{jk} &= \frac{1}{\ki}\sumj jp_j\left[\ki^2+\mu_2+\ki\left(1-\alpha\right)\left(j-\ki\right)\right] \\
	&= \frac{1}{\ki}\left[ \ki\left(1-\alpha\right)\sumj j^2p_j + \left(\alpha\ki^2+\mu_2\right)\sumj jp_j\right] \nonumber \\
	&= \kii\left(1-\alpha\right)+\alpha\ki^2+\mu_2 \nonumber \\
	&=-\alpha\mu_2+\mu_2+\kii\,.
\end{align}
This allows equation \eqref{appeq:assort} to be written
\begin{align}
	r &= \frac{-\alpha\mu_2+\mu_2\ki^2+\kii\ki^2-\kii^2}{\ki\kiii-\kii^2} \\[0.5em]
	r &= \frac{-\alpha\mu_2-\mu_2^2\ki^2}{\ki\kiii-\kii^2} \nonumber \\[0.5em]
	\alpha &= -\frac{r\left(\ki\kiii-\kii^2\right)}{\mu_2\ki^2}-\frac{\mu_2}{\ki^2}\,,
\end{align}
which is the final form given in equation \eqref{eq:rawlink}.

\clearpage
\section{Assortativity of Crystalline Lattices}
\label{app:crystals}

Crystalline systems can provide a useful comparison point to contrast with random networks, as is demonstrated for procrystalline lattices in chapter \ref{ch:procrystals}.
Figure \ref{appfig:crystals} shows two series of crystalline motifs taken from Altman \etal{} \cite{Malashevich2016}, and their corresponding assortativities.

An example calculation for these crystals is included for the $8 - 6^1 - 5^2$ lattice, figure \ref{appfig:86152}.
The edge joint degree distribution is given by:
\begin{align}
	\mathbf{e} &= \frac{1}{24}\, \begin{blockarray}{*{3}{c} l}
	\begin{block}{*{3}{>{$\footnotesize}c<{$}} l}
	5 & 6 & 8 \\
	\end{block}
	\begin{block}{[*{3}{c}]>{$\footnotesize}l<{$}}
	2 & 4 & 4 \: \bigstrut[t]& 5\\
	2 & 2 & 2 & 6 \\
	4 & 2 & 2 & 8\\
	\end{block}
	\end{blockarray}.
\end{align}
The rows are deduced using two considerations.
Firstly, the relative ring adjacencies for each ring size are accounted for \eg{} each 8\--ring has $4\times5$\--ring neighbours, $2\times6$\--ring neighbours and $2\times8$\--ring neighbours.
Secondly, the relative ring proportions are factored in to the row sums \ie{} $p_5=2p_6=2p_8$.
Once this matrix has been constructed, the assortativity can be calculated using equation \eqref{appeq:assort}.

\begin{figure}[h!]
     \centering

	\begin{subfigure}[b]{0.15\textwidth}
         \centering
         \includegraphics[width=\textwidth]{./appendices/figures/crystal_8_60_52.pdf}
         \caption{$8 - 6^0 - 5^2$, \\$r=-0.350$}
         \label{appfig:86052}
     \end{subfigure}
     \hfill
      \begin{subfigure}[b]{0.15\textwidth}
         \centering
         \includegraphics[width=\textwidth]{./appendices/figures/crystal_8_61_52.pdf}
         \caption{$8 - 6^1 - 5^2$, \\$r=-0.185$}
         \label{appfig:86152}
     \end{subfigure}
     \hfill
      \begin{subfigure}[b]{0.15\textwidth}
         \centering
         \includegraphics[width=\textwidth]{./appendices/figures/crystal_8_62_52.pdf}
         \caption{$8 - 6^2 - 5^2$, \\$r=-0.373$}
         \label{appfig:86252}
     \end{subfigure}
     \hfill
      \begin{subfigure}[b]{0.15\textwidth}
         \centering
         \includegraphics[width=\textwidth]{./appendices/figures/crystal_8_63_52.pdf}
         \caption{$8 - 6^3 - 5^2$, \\$r=-0.220$}
         \label{appfig:86352}
     \end{subfigure}
     \hfill

     \vspace{2mm}
      \begin{subfigure}[b]{0.15\textwidth}
         \centering
         \includegraphics[width=\textwidth]{./appendices/figures/crystal_8_60_4.pdf}
         \caption{$8 - 6^0 - 4$, \\$r=-0.500$}
         \label{appfig:8604}
     \end{subfigure}
     \hfill
      \begin{subfigure}[b]{0.15\textwidth}
         \centering
         \includegraphics[width=\textwidth]{./appendices/figures/crystal_8_61_4.pdf}
         \caption{$8 - 6^1 - 4$, \\$r=-0.260$}
         \label{appfig:8614}
     \end{subfigure}
     \hfill
      \begin{subfigure}[b]{0.15\textwidth}
         \centering
         \includegraphics[width=\textwidth]{./appendices/figures/crystal_8_62_4.pdf}
         \caption{$8 - 6^2 - 4$, \\$r=-0.412$}
         \label{appfig:8624}
     \end{subfigure}
     \hfill
      \begin{subfigure}[b]{0.15\textwidth}
         \centering
         \includegraphics[width=\textwidth]{./appendices/figures/crystal_8_63_4.pdf}
         \caption{$8 - 6^3 - 4$, \\$r=-0.395$}
         \label{appfig:8634}
     \end{subfigure}
     \hfill

     \caption{Repeating units from two series of crystalline motifs and their assortativities.
      Panels (a)\--(d) show the series $8-6^i-5^2$ with half the number of 8\-- as 5\--rings, interspersed with varying numbers of 6\--rings. Panels (e)\--(h) show the series $8-6^i-4$ with equal numbers of 8\-- and 4\--rings, interspersed with varying numbers of 6\--rings.}
     \label{appfig:crystals}
\end{figure}

\resumetocwriting


\subsubsection{TypicalHistoryFromHistorySet}
\label{TypicalHistoryFromHistorySet}

This Post-Processor performs a simplified procedure of \cite{wilcox2008users} to form a ``typical'' time series from multiple time series. The input should be a HistorySet, with each history in the HistorySet synchronized. For HistorySet that is not synchronized, use Post-Processor method \textbf{HistorySetSync}  to synchronize the data before running this method.

Each history in input HistorySet is first converted to multiple histories each has maximum time specified in \xmlNode{outputLen} (see below). Each converted history $H_i$ is divided into a set of subsequences $\{H_i^j\}$, and the division is guided by the \xmlNode{subseqLen} node specified in the input XML. The value of \xmlNode{subseqLen} should be a list of positive numbers that specify the length of each subsequence. If the number of subsequence for each history is more than the number of values given in \xmlNode{subseqLen}, the values in \xmlNode{subseqLen} would be reused.

For each variable $x$, the method first computes the empirical CDF (cumulative density function) by using all the data values of $x$ in the HistorySet. This CDF is termed as long-term CDF for $x$. Then for each subsequence $H_i^j$, the method computes the empirical CDF by using all the data values of $x$ in $H_i^j$. This CDF is termed as subsequential CDF. For the first interval window (i.e., $j=1$), the method computes the Finkelstein-Schafer (FS) statistics \cite{finkelstein1971improved} between the long term CDF and the subsequential CDF of $H_i^1$ for each $i$. The FS statistics is defined as following.
\begin{align*}
FS & = \sum_x FS_x\\
FS_x &= \frac{1}{N}\sum_{n=1}^N\delta_n
\end{align*}
where $N$ is the number of value reading in the empirical CDF and $\delta_n$ is the absolute difference between the long term CDF and the subsequential CDF at value $x_n$. The subsequence $H_i^1$ with minimal FS statistics will be selected as the typical subsequence for the interval window $j=1$. Such process repeats for $j=2,3,\dots$ until all subsequences have been processed. Then all the typical subsequences will be concatenated to form a complete history.

\ppType{TypicalHistoryFromHistorySet}{TypicalHistoryFromHistorySet}

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

\begin{itemize}
   \item \xmlNode{pivotParameter}, \xmlDesc{string, optional field}, ID of the temporal variable
   \default{Time}
   \item \xmlNode{subseqLen}, \xmlDesc{integers, required field}, length of the divided subsequence (see above)
   \item \xmlNode{outputLen}, \xmlDesc{integer, optional field}, maximum value of the temporal variable for the generated typical history
   \default{Maximum value of the variable with name of \xmlNode{pivotParameter}}
\end{itemize}

For example, consider history of data collected over three years in one-second increments,
where the user wants a single \emph{typical year} extracted from the data.
The user wants this data constructed by combining twelve equal \emph{typical month}
segments.  In this case, the parameter \xmlNode{outputLen} should be \texttt{31536000} (the number of seconds
in a year), while the parameter \xmlNode{subseqLen} should be \texttt{2592000} (the number of seconds in a
month).  Using a value for \xmlNode{subseqLen} that is either much, much smaller than \xmlNode{outputLen} or
of equal size to \xmlNode{outputLen} might have unexpected results.  In general, we recommend using a
\xmlNode{subseqLen} that is roughly an order of magnitude smaller than \xmlNode{outputLen}.


\subsubsection*{Unary Operations\hspace*{\fill}\hyperlink{ElementwiseOperations}{(up)}\hypertarget{unaryOperations}{}}\addcontentsline{toc}{subsubsection}{Unary Operations}
Unary operations involve calculations on a single \ttbf{view}. Functions which involve a calculation where the answer is a scalar, such as \ttbf{sumval} generally have a \ttbf{val} as part of the root name.
\begin{table}[H]
\caption{Unary Operations}
\label{tab:unaryOperations}
\begin{center}
\begin{tabular}{|l|l|}\hline
\hlnkFunc{arg} & Argument\\
\hlnkFunc{ceil} & Ceiling\\
\hlnkFunc{conj} & Conjugate\\
\hlnkFunc{cumsum} & Cumulative Sum\\
\hlnkFunc{euler} & Euler\\
\hlnkFunc{floor} & Floor\\
\hlnkFunc{mag} & Magnitude\\
\hlnkFunc{cmagsq} & Complex Magnitude Squared\\
\hlnkFunc{meanval} & Mean Value\\
\hlnkFunc{meansqval} & Mean Square Value\\
\hlnkFunc{modulate} & Modulate\\
\hlnkFunc{neg} & Negate\\
\hlnkFunc{recip} & Reciprocal\\
\hlnkFunc{round} & Round\\
\hlnkFunc{rsqrt} & reciprocal Square Root\\
\hlnkFunc{sq} & Square\\
\hlnkFunc{sumval} & Sum Value\\
\hlnkFunc{sumsqval} & Sum of Squares Value\\
\hline\end{tabular}
\end{center}
%\label{default}
\end{table}%


% Project Specifications

\chapter{Quantization}

%In such networks a large part of the computation is done with an
%operation called the \gls{gemm}. An efficient implementation of \gls{gemm} is crucial to the
%acceleration. However, \glspl{cnn} are normally implemented with floating-point numbers, which are usually less
%efficient to handle in hardware than fixed-point numbers. A class of techniques called quantization can
%be used to carry out the computation in fixed-point numbers and then convert the result back to
%floating-point numbers, realizing high performance in hardware. Chapter 4 provides a detailed discussion
%of the quantization scheme used in this project.

Machine learning programs normally represent numbers as floating-point. Although floating-point numbers
have limited precision and can sometimes lead to numerical instability when the numbers involved get too
small or too large, they are intuitive and easy to work with. But floating-point arithmetic can be rather
expensive to implement in hardware. Modern \glspl{fpga} contain dedicated \gls{dsp} units, some of which
natively
support floating-point, others require special \gls{ip} cores to implement the relevant
circuitry. \gls{fpga}'s processing power mainly shines on fixed-point. In order to run our model in fixed-point
on \gls{fpga}, it is necessary to convert the model into fixed-point representation, through a process known as
quantization. When the result is computed by \gls{fpga}, it is then converted back to floating-point, which is
naturally called dequantization.

Quantization brings additional benefits with the compression of model size. When 32-bit floating-point
weights are quantized to 8-bit, the model size can be reduced up to $75\%$. This cuts down memory bandwidth
requirement and improves memory efficiency. Consequently, the power consumption can be greatly reduced.

One could imagine that quantization would cause a tremendous loss to the precision of a model, for example, when
32-bit floating-point numbers
are converted to
8-bit unsigned integers, the range drops from $(1.175494351 \times 10^{-38}, 3.402823466 \times 10^{38})$ to $(0, 255)$. However, one of the peculiar
properties of neural networks is that they are very resilient to noise. Of course this is also one of the
reasons why they are so successful in real world applications. We could view the internal precision loss of
quantization as a form of internal noise. It turned out that the precision loss incurred is rather small
\cite{vincent2011}.

There exist several different quantization methods. This project adopted the quantization scheme
implemented in Google's \textit{gemmlowp} library \cite{github:gemmlowp}
as well as described in \cite{DBLP:journals/corr/abs-1712-05877}. \textit{gemmlowp} is
a low precision \gls{gemm} implementation for fixed-point numbers.
The original 32-bit single-precision weights represented in \mintinline{c}{float} are mapped to
\mintinline{c}{uint8_t}. \gls{gemm} is then
carried out on the \gls{fpga} board with these 8-bit integers. Intermediate \gls{mac} results are 32-bit
integers to accommodate the accumulation. Eventually the final output of the model is converted back to
\mintinline{c}{float}.

The quantization works by first identifying the range of the data. In neural networks, the values are usually
distributed within a relatively small range. For instance, for 8-bit quantization, if the minimum value of the
weights is $-10.0$ and maximum value is $20.0$, then $-10.0$ would be mapped to $0$ while $20.0$ would be mapped to $255$.
However, there is an additional requirement that can bring great benefits to subsequent computations:
the real value $0$ should be exactly representable. As will be shown, this can be achieved with a small
modification to the mapping equation.

Th quantization scheme can be derived with basic algebra. Define a real number $f$ as the result of an affine
mapping $f = S q + B$, where $q$ is the corresponding quantized integer, $S \in \mathbb{R}$ is a constant
scaling factor, and $B \in \mathbb{R}$ is a constant shift. Next, modify the mapping a bit so that the
real value $0$ is exactly representable:

\begin{equation} \label{eq:dequantization}
  f = S(q - Z),
\end{equation}

where $Z \in \mathbb{N}$ is a constant shift applied to $q$. Since convolution and transposed convolution
both can involve zero-paddings, having an exact representation of $0$ avoids the accumulation of errors
(essentially a form of bias). With this form of affine mapping, $0$ is trivially mapped to $q = Z$. $S$ and
$Z$ are called the quantization parameters of this mapping. From equation \ref{eq:dequantization},
it is clear that the quantization mapping is done with

\begin{equation} \label{eq:quantization}
  q = \frac{f}{S} + Z
\end{equation}

Let $A \in \mathbb{R}^{m \times k}$, $B \in \mathbb{R}^{k \times n}$, $A B = C \in \mathbb{R}^{m \times n}$,
let the corresponding quantization parameters for $A$, $B$ and $C$ be $S_A$ and $Z_A$, $S_B$ and $Z_B$,
$S_C$ and $Z_C$. These parameters can be predetermined, since in most cases the ranges of numbers in these matrices
are known. Let $p$, $q$, $r$ be the entries in the quantized version of $A$, $B$ and $C$.

For an entry $f_C$ in $C$, $f_C = \sum_{i}^{} A_i B_i$, where $i$ is the index of entries in a particular
row in $A$ and corresponding column in $B$,

\begin{equation}
\begin{split}
  f_C & = \sum_{i} A_i B_i \\
      & = \sum_{i} S_A (p_i - Z_A) S_B (q_i - Z_A) \\
      & = S_A S_B \sum_{i} (p_i - Z_A) (q_i - Z_B)
\end{split}
\end{equation}

Clearly the term $\sum_{i} (p_i - Z_A) (q_i - Z_B)$ is the computational core here, let it be
$\mathfrak{K}$. Then,

\begin{equation}
\begin{split}
  r_C & = \frac{f_C}{S_C} + Z_C \\
      & = \frac{S_A S_B}{S_C} \mathfrak{K} + Z_C
\end{split}
\end{equation}

Therefore, once $\mathfrak{K}$ is computed, it can be simply multiplied by a constant multiplier
$M = \frac{S_A S_B}{S_C}$ and added the zero offset $Z_C$ to obtain the quantized value $r_C$. The
multiplication by $M$ can be implemented by multiplying $\mathfrak{K}$ by a constant integer and right shifting the
result by a fixed number of bits. These can be predetermined as well. The computation of $\mathfrak{K}$ might seem problematic at first glance:
it contains a subtraction operation for each entry,
which would bring extra overhead to the \gls{gemm} computation. To solve this, expand the expression $\mathfrak{K}$:

\begin{equation}
\begin{split}
  \mathfrak{K} & = \sum_{i} (p_i - Z_A) (q_i - Z_B) \\
               & = \sum_{i} (p_i q_i - Z_B p_i - Z_A q_i + Z_A Z_B) \\
               & = \sum_{i} p_i q_i - \sum_{i} Z_B p_i - \sum_{i} Z_A q_i + \sum_{i} Z_A Z_B \\
               & = \sum_{i} p_i q_i - Z_B \sum_{i} p_i - Z_A \sum_{i} q_i + k Z_A Z_B
\end{split}
\end{equation}

Note that $\sum_{i} 1$ is k, which is the number of columns of $A$ and the number of rows of $B$. Four terms are
obtained here. The first term $\sum_{i} p_i q_i$ is the main term. The second term, $- Z_B \sum_{i} p_i$
is the product of $- Z_B$ and the sum of the current row, which only needs to be calculated once and added
to each result of the first term. The third term $- Z_A \sum_{i} q_i$ and the fourth term $k Z_A Z_B$ are
similar. The computation can be carried out by computing the first term as the core, and add the rest three
terms one by one.

\section{Handling of Biases}

Biases are added to the 32-bit accumulator result. Recall that $f_C = \sum_{i} A_i B_i = S_A S_B \mathfrak{K}$,
it is clear that biases should be quantized with $S = S_A S_B$ and $Z = 0$. When zero offset is $0$, negative
numbers will remain negative, consequently the type of bias values should be signed 32-bit integers.

\clearpage %force the next chapter to start on a new page. Keep that as the last line of your chapter!


\graphicspath{ {./img/TheFEM/} }
\chapter{Interpolation in the Finite Element Method}

\section*{Preliminary}
In a broad sense the finite element method is nothing else than an
approximation technique for solutions to boundary and initial value problems.
In this section we will discuss some fundamental and basic aspects related to
the approximation of functions through interpolation. In the finite element
method, such approximation takes place for the geometry of the computational
domain and for the field variable of the problem at hand. As it will be seen,
the concept of finite element it self corresponds to a local domain or
sub-domain in which the solution function is approximated via interpolation
techniques. We will cover this subject from a materialistic point of view, as
required on the implementation of a first finite element algorithm, with
mathematical rigor left to the excellent texts on the subject. The set of notes
starts with the problem definition and its solution in terms of the Lagrange
interpolation theorem. From that point the notes discuss practical applications
including its implementation in Python scripts built for finite element
analysis.

At the end of this chapter\footnote{This chapter, together with theoretical and
computational learning activities is complemented by Jupyter Notebooks 1
through 4 available at the course's repository.} the student should be able to:

\begin{itemize}
\item[•] Formulate appropriate interpolation schemes to generate approximate functions out of sets of discrete values representing the behavior of the unknown functions over one-dimensional and two-dimensional domains.
\item[•] Recognize the differences in terms of advantages and limitations  between global and local interpolation schemes. This includes the pathologies associated with each method.
\item[•] Recognize the concept {\bf finite element} like a family of locally defined interpolation schemes valid over canonical domains.
\item[•] Develop efficient and effective  Python implementations of interpolation schemes within the context of the finite element method for one-dimensional and two-dimensional applications.
\end{itemize}

\section{Statement of the problem}
Let $f(x)$ be an unknown function, whose values, however, are known at $n$ discrete points ${x_1, x_2,...,x_n}$. We want to know (interpolate) the value of $f(x)$ at an arbitrary point $x \in \left[ {{x_1},{x_n}} \right]$ and different to one of the $n$ points.

The problem of interpolation is precisely that of finding the unknown value of $f(x)$ using the known values $\left\{ {{f^1},{f^2},...,{f^n}} \right\}$. As schematically described in \cref{fig:interpol1} it involves two steps:

\begin{itemize}
\item[i]  Fitting an approximate function, known as the interpolating polynomial, to the known data points.
\item[ii] Evaluating the function at the point of interest where the function is unknown.
\end{itemize}

We can (i) follow a global approach using all the known $n$-data points and fit an $(n-1)$-th order polynomial to these data points\footnote{As this approach is problem-dependent it is also cumbersome and difficult to code and therefore not amenable to be used in finite elements.}(\cref{fig:interpol1})  or (ii) use a local approach where one splits the domain into sub-intervals and fits lower order polynomials to the data points within each sub-interval(\cref{fig:interpol2}).

Local interpolation uses a finite number of nearest-neighbors and generates interpolated versions of $f(x)$ that do not in general have continuous first or higher derivatives. The advantage of this local approach lies in the fact that independent of the function or the number of data points, the interpolation operation always uses the same polynomial. For instance in \cref{fig:interpol2} only first-order polynomials are being used in each local sub-domain: as a result a computer implementation of this scheme would only need to store the local polynomial once. In the jargon of the finite element method the interpolation functions or its resulting local polynomials are termed a finite element.

\begin{figure}[H]
\centering
\includegraphics[width=10cm]{img/interpol1.pdf}
\caption{The black points represent known values of an otherwise unknown function. The dashed line represents an approximation to the unknown function in terms of a polyonmial of order $n-1$.}
\label{fig:interpol1}
\end{figure}

\begin{figure}[H]
\centering
\includegraphics[width=10cm]{img/interpol2.pdf}
\caption{Interpolation takes place inside each sub-interval producing a piecewise interpolation approximation to the unknown function}
\label{fig:interpol2}
\end{figure}

\section{One-dimensional scalar functions}
\subsection{Lagrange interpolation theorem}
Given a set of n-points $\{ (x^1, y^1),\cdots,(x^n, y^n)\}$ where $y^n \equiv f({x^n})$ then: ``there exists a unique polynomial $p(x)$ of order at most $(n-1)$ such $p(x^I) = f(x^I)$ for $I=1,2,\cdots,n$". The polynomial is given by;

\begin{equation}\label{eq:inter_expl}
p(x) = {L^1}(x)f({x^1}) + {L^2}(x)f({x^2}) +  \ldots  + {L^n}(x)f({x^n})
\end{equation}

and the term ${L^I}(x)$ is computed as;

\begin{equation}\label{eq:coef}
  L^I(x) = \prod_{\substack{J = 1\\ I \ne J}}^n \frac{(x - x^J)}{(x^I - x^J)}.
\end{equation}

The approximate function $p(x)$ of order $n-1$ is termed {\bf the interpolating polynomial} such

\[f(x)\simeq p(x)\]

while each one of the terms ${L^I}(x)$, also of order $n-1$, are termed {\bf the interpolation functions}. In the context of the finite element method these are also called {\bf shape functions}.

The use of index notation, and particularly the summation convention, can be extended to represent the linear superposition of functions given by \cref{eq:inter_expl}. We use capital super-scripts to denote interpolation in such a way that a capital superscript denotes a data point in the interpolation scheme. Accordingly \cref{eq:inter_expl} can be equivalently written like:

\begin{equation}\label{eq:pol}
  p(x^I) = L^I(x) f(x^I)
\end{equation}

where the fact that $I=1,2,...,n$ is implicit in the notation.

Recall that the interpolating polynomial $p(x)$ is just an approximation to the actual function $f(x)$. However, the approximation should at least be such that $p(x^I) = f(x^I)$ at the $n$ nodal points. To satisfy this condition the interpolation polynomials must be such that:

\[L^I(x^J) = \delta^{IJ}\]

and where ${\delta ^{IJ}}$ is the delta function extended to the interpolation polynomials.

\begin{tcolorbox}
The approximate function $p(x)$ resulting from the interpolation process is called the {\bf interpolating polynomial} while the Lagrange polynomials $L^I(x)$ are called interpolation functions. In the language of the finite element methods these are simply called {\bf shape functions}.
\end{tcolorbox}

\begin{tcolorbox}
\paragraph*{A note regarding superscripts in indicial notation:} In this Class Notes superscripts associated to symbols like in the expression $x^4$ are frequently used to describe a variable associated to a nodal point: for instance, in this context the expression $x^4$ represents the $x$-coordinate of nodal point $4$. However, in some other cases this same expression might appear, for example, in the definition of a function like in $f(x) = {x^4} + 4{x^3}$. In both cases the specific meaning should be clear by the context in which it appears.

Python function {\bf LagrangPoly()} in NB-1 generates Lagrange interpolation polynomials of different order and over a varying range.
\end{tcolorbox}

\paragraph*{Example: Interpolation of a function using 3 data points}

Use a Lagrange interpolation scheme to find an interpolating polynomial that approximates the function

\[ f(x) = {x^3} + 4{x^2} - 10 \]

at the sampling (or also nodal) points ${x^1} =  - 1.0$, ${x^2} =  + 1.0$ and ${x^3} = 0.0$ over the interval $[-1,1]$ together with its first order derivative at $x = 0.7$.  Assume that the values of the first derivative at the nodal points are unknown.

\Cref{ejemplo2} contains the exact values for the function

\begin{center}
\begin{tabular}{ll}
  \hline
  $x$ & $f(x)$ \\
  \hline
  $-1.0$  & $-7.000$  \\
  $ 0.00$  & $-10.00$  \\
  $ 1.00$  & $-5.000$  \\
  \hline
\end{tabular}
\captionof{table}{Known values of the function $f(x) = {x^3} + 4{x^2} - 10$ over the interval $[-1,1]$}
\label{ejemplo2}
\end{center}

The formulation of the interpolation scheme consists in finding the interpolation polynomials $L^I(x)$ and the interpolating function $p(x)$. From \cref{eq:coef} the interpolation functions, shown in \cref{fig:pols}, are:
\begin{align*}
L^1(x) = \frac{(x - x^2)(x - x^3)}{(x^1 - x^2)(x^1-x^3)} & \equiv  - \frac{1}{2}(1 -  x)x\\
L^2(x) = \frac{(x - x^1)(x - x^3)}{(x^2 - x^1)(x^2 - x^3)} & \equiv +   \frac{1}{2}(1 + x)x\\
L^3(x) = \frac{(x - x^1)(x - x^2)}{(x^3 - x^1)(x^3 - x^2)} & \equiv + (1 - x^2).
\end{align*}

\begin{figure}[H]
  \centering
  \includegraphics[width=10cm]{func1.pdf}
  \caption{(top) Interpolation polynomials $L^1(x)$, $L^2(x)$ and $L^3(x)$ as per \cref{eq:coef} computed for a second order scheme with 3 nodal points.}
  \label{fig:pols}
\end{figure}

The interpolating polynomial approximating the function is obtained using \cref{eq:pol} which in this case is given by

\[p(x) = 10{x^2} + \frac{7}{2}(1 - x)x - \frac{5}{2}(1 + x)x - 10.\]

The approximate and actual function are compared in \cref{fig:functions}. Clearly $p(x)$, being a second order function differs with $f(x)$, which is a third order function. However, as stated in the interpolation theorem both functions coincide at the nodal points where the known values of the function are available.

\begin{figure}[H]
  \centering
  \includegraphics[width=10cm]{func2.pdf}
  \caption{Resulting interpolating function $p(x)$ as per \cref{eq:pol} compared with the exact function $f(x) = {x^3} + 4{x^2} - 10$. The Python code used for the interpolation is available in NB-1}
  \label{fig:functions}
\end{figure}

In finding an approximations to the first derivative recall that at the nodal points the values of these first derivatives are unknown. Thus the only available choice is to operate directly on $p(x)$ and use it to approximate also the first derivative like:

\begin{equation}\label{eq:der}
  \frac{ dp(x)}{dx} = \frac{dL^1(x)}{dx} f^1 + \frac{dL^2(x)}{dx}(x) f^2 + \frac{dL^3(x)}{dx} f^3
\end{equation}

It is evident that the approximation takes the form of a lineal superposition of products between interpolation functions (which in this case are derivatives of the $L^I$) and known values of the function. The first derivative obtained in this form differs from the actual derivative of $f(x)$ since this approach uses the values of $f$ and not those of $\frac{{df(x)}}{{dx}}$ and $\frac{dL^I(x)}{dx}$ instead of $L^I$ and these functions do not satisfy the condition $\frac{dL^I(x^J)}{dx} = \delta ^{IJ}$.

\begin{figure}[H]
  \centering
  \includegraphics[width=10cm]{deriv.pdf}
  \caption{Comparison between the first order derivative of $p(x)$ with the closed-form result of $\frac{{df(x)}}{{dx}}$}
  \label{fig:deriv}
\end{figure}

\Cref{fig:first der 1} shows the approximation to $f'(x)$ when $p(x)$ is computed using 4th-order polynomials.

\begin{figure}[H]
  \centering
  \includegraphics[width=10cm]{firstder.pdf}
  \caption{Comparison between the first order derivative of $p(x)$ computed using 4th-order polynomials with the closed-form result of $\frac{{df(x)}}{{dx}}$}
  \label{fig:first der 1}
\end{figure}

To identify the variation in the solution with different interpolation schemes \cref{fig:several interpol} compares the exact and interpolated solution for polynomials of order 1, 2 and 4 respectively. The left column displays the interpolation polynomials ${{L^I}(x)}$.

\begin{figure}[H]
\centering
	\begin{subfigure}[b]{0.450\textwidth}\qquad
		\includegraphics[width=\textwidth]{lineal.pdf}
		\caption{First order. }
	\end{subfigure}\,
%
	\begin{subfigure}[b]{0.450\textwidth}\qquad
		\includegraphics[width=\textwidth]{interlin.pdf}
		\caption{Actual and interpolated function.}
	\end{subfigure}\\
%
\centering
	\begin{subfigure}[b]{0.450\textwidth}\qquad
		\includegraphics[width=\textwidth]{quadra.pdf}
		\caption{Second order.}
	\end{subfigure}\,
%
	\begin{subfigure}[b]{0.450\textwidth}\qquad
		\includegraphics[width=\textwidth]{interqua.pdf}
		\caption{Actual and interpolated function.}
	\end{subfigure}\\
%
\centering
	\begin{subfigure}[b]{0.450\textwidth}\qquad
		\includegraphics[width=\textwidth]{third.pdf}
		\caption{Fourth order.}
	\end{subfigure}\,
%
	\begin{subfigure}[b]{0.450\textwidth}\qquad
		\includegraphics[width=\textwidth]{intertri.pdf}
		\caption{Actual and interpolated function.}
	\end{subfigure}
\caption{Interpolation of the function $f(x) = {x^3} + 4{x^2} - 10$ using Lagrange polynomials of increasing order.}
\label{fig:several interpol}
\end{figure}

\subsection{Local interpolation using a piece-wise continuous function}
An alternative to the non-uniform nodal distribution approach used in the previous section to improve the interpolation scheme is based on splitting the solution interval $[x_1, x_n]$ in smaller sub-domains where the interpolation is performed locally. For instance \cref{subdo} shows such a partition for the interval $[-1.0, 1.0]$ where each sub-domain is conformed by a pair of consecutive nodes. The table shows values of the function $f(x) = {x^3} + 4{x^2} - 10$ at the edges of the sub-domains. In this particular case, considering each sub-domain to be conformed by a pair of points, the lineal interpolation scheme reduces to finding the straight line (first order polynomial) that passes along the pair of nodes. Higher order local schemes are possible if additional points are added to the sub-domains.

\begin{center}
\begin{tabular}{ccc}
  \hline
  Subdomain & Range & Values for $f(x)$ \\
  \hline
   1  & $[-1.0, -0.5]$ & $[-7.000, -9.125]$  \\
   2  & $[-0.5,  0.00]$ & $[-9.125, -10.00]$  \\
   3  & $[+0.0,  +0.5]$ & $[-10.00, -8.875]$   \\
   4  & $[+0.5, +1.0]$ & $[-8.875, -5.000]$   \\
  \hline
\end{tabular}
\captionof{table}{Partition of the interval $[-1.0, 1.0]$ into subdomains}
\label{subdo}
\end{center}

\begin{tcolorbox}

Using piecewise interpolation over constant size intervals implies also the use of constant polynomials. In the finite element method each sub-domain is mapped into a constant size interval facilitating systematization of the interpolation process.
Notebook 2 in the REPO shows the Python implementation of piecewise interpolation.

\end{tcolorbox}

\begin{figure}[H]
  \centering
  \includegraphics[width=10cm]{localone.pdf}
  \caption{Local interpolating polynomials.}
  \label{fig:loc-pols}
\end{figure}

This approach results in an interpolating polynomial as shown in \cref{fig:fully local 1} and where the approximated function is piece-wise continuous. As a result the local based technique the first derivative of the function is now discontinuous at the boundaries of the subdomains. However this local schema is advantageous since the local polynomials are unique and the scheme can be used for an arbitrary number of nodal points facilitating computer implementation.

\begin{figure}[H]
\centering
	\begin{subfigure}[b]{0.45\textwidth}\qquad
		\includegraphics[width=\textwidth]{localfun.pdf}
		\caption{Approximation to the function using first order interpolation polynomials.}
	\end{subfigure}\,
%
	\begin{subfigure}[b]{0.45\textwidth}\qquad
		\includegraphics[width=\textwidth]{localfirst.pdf}
		\caption{Variation of the first order derivative.}
	\end{subfigure}\\

\caption{Locally based interpolation scheme for the function $f(x) = {x^3} + 4{x^2} - 10$.}
\label{fig:fully local 1}
\end{figure}

\subsection{Distribution of the sampling (or nodal) points}
The simple problems considered so far have used a small number of sample points and a constant separation distance. This approach worked nicely considering the smooth functions involved. However if the function to be interpolated exhibits strong gradients, as might be the case in problems of wave propagation, the approximation with a small number of data points is very likely insufficient. The natural solution seems to be the addition of data points and thus an increase in the order of the interpolating polynomial. Unfortunately, as we will show here this approach does not always works in the desired direction.

Consider the function:

\[f(x) = \frac{1}{{1 + 25{x^2}}}\]

shown in \cref{fig:rungelag}.

\begin{figure}[H]
\centering
\includegraphics[width=8cm]{runge.pdf}
\caption{Runge function $f(x) = \frac{1}{1 + 25{x^2}}$.}
\label{fig:rungelag}
\end{figure}

This function shows a strong spatial variation requiring an interpolating polynomial of larger order and as a result a larger number of nodal points. The 11 black dots shown in the figure represent nodal points equally spaced at $\Delta x = 0.2$ and where the function is assumed to be known. We wish to approximate this function using these 11 points and an order 10 interpolating Lagrange polynomial.

\Cref{fig:rungeequi} shows the 11 order-10 Lagrange interpolation polynomials for the equidistant nodal distribution and the resulting interpolating polynomial $p(x)$. Clearly the approximation is highly inaccurate, specially near the edges of the interval where it exhibits strong oscillations. This spurious result along the edges is introduced by the equidistant separation of the sampling points. The interpolation scheme can be improved using a non-uniform nodal spacing. The resulting alternative scheme is shown in  \cref{fig:rungechevy} where there is a large concentration of nodal points along the edges.

\begin{figure}[H]
\centering
	\begin{subfigure}[b]{0.50\textwidth}\qquad
		\includegraphics[width=\textwidth]{lag11.pdf}
		\caption{}
	\end{subfigure}\,
%
	\begin{subfigure}[b]{0.45\textwidth}\qquad
		\includegraphics[width=\textwidth]{rungaprlag.pdf}
		\caption{}
	\end{subfigure}\\

\caption{(a) Lagrange interpolation polynomials of order 10 associated to the 11 sampling points for \cref{fig:rungelag}. (b) Interpolating polynomial to approximate the Runge function with the order-10 polynomials from part (a).}
\label{fig:rungeequi}
\end{figure}

\begin{figure}[H]
\centering
	\begin{subfigure}[b]{0.50\textwidth}\qquad
		\includegraphics[width=\textwidth]{lagchv.pdf}
		\caption{}
	\end{subfigure}\,
%
	\begin{subfigure}[b]{0.45\textwidth}\qquad
		\includegraphics[width=\textwidth]{rungaprchv.pdf}
		\caption{}
	\end{subfigure}\\

\caption{(a) Order 10 Lagrange interpolation polynomials associated to non-equidistant sampling points. (b) Interpolating polynomial to approximate a Runge function built with the polynomials derived in part (a).}
\label{fig:rungechevy}
\end{figure}

To understand this numerical pathology, related with the distribution of the nodal points, consider the interpolation polynomials corresponding to the central point and to the edge point of the equidistant distribution (\cref{fig:rungeequi}) as shown in part(a) of \cref{fig:compara}. The green line corresponds to the polynomial associated to the central node, while the blue line is that of the edge nodal point. Clearly, the central-point polynomial introduces a strong variation along the edges of the sampling interval, while the edge-point polynomial exhibits a rather smooth variation. Similarly, part(b) in the same figure shows once again the central-point and the edge-point polynomials associated to non-uniform nodal distribution. It can be observed how in this last case both polynomials exhibit a smooth variation over the interval eliminating the strong oscillation towards the edges.

\begin{figure}[H]
\centering
	\begin{subfigure}[b]{0.45\textwidth}\qquad
		\includegraphics[width=\textwidth]{central.pdf}
		\caption{}
	\end{subfigure}\,
%
	\begin{subfigure}[b]{0.45\textwidth}\qquad
		\includegraphics[width=\textwidth]{extremo.pdf}
		\caption{}
	\end{subfigure}\\

\caption{(a) Equidistant nodal points (b) Non-uniform nodal points.}
\label{fig:compara}
\end{figure}

\section{Extension to two-dimensional domains}
Assume we are now interested in conducting interpolation of a function over a spatial 2-dimensional domain where every point is specified by a position vector of the form $\overrightarrow x = x \hat{\imath} + y\hat{\jmath}$. We want to know, via interpolation, the value of a function $f(\overrightarrow x)$ at an arbitrary point $\overrightarrow x$ provided we know the set of n-pairs of the form $\{(\overrightarrow x ^1, f^1),\cdots,(\overrightarrow x ^n, f^n)\}$. The domain and the visualization of the function are shown in \cref{fig:element}.

\begin{figure}[H]
\centering
	\begin{subfigure}[b]{0.45\textwidth}\qquad
		\includegraphics[width=\textwidth]{element.pdf}
		\caption{Square two-dimensional domain}
	\end{subfigure}\,
%
	\begin{subfigure}[b]{0.45\textwidth}\qquad
		\includegraphics[width=\textwidth]{surface.pdf}
		\caption{Interpolated function $f(x,y)$.}
	\end{subfigure}\\

\caption{Function $f(x,y)$ over a square two-dimensional domain with nodal points labeled 1 , 2 , 3 , 4.}
\label{fig:element}
\end{figure}

Since now the function $f$ depends on the 2D space coordinates $(x , y)$ it is natural to expect that interpolation functions depend also on $(x , y)$. Using this condition on the function approximation we have:

\[f(x,y) = N^1(x,y)f^1 + N^2(x,y)f^2 + N^3(x,y)f^3 + N^4(x,y)f^4\]

where now $N^Q(x,y)$ is the 2D interpolation (or shape) function associated to the sampling point $Q$. Using indicial summation convention, we can write the interpolated function as:

\[f(x,y) = N^Q(x,y)f^Q\]

where now $Q = 1,...,N$.

The method to find the required 2D shape functions $N^Q(x,y)$ consists in the recursive (or iterated) application of the Lagrange one-dimensional scheme discussed previously in terms of interpolating polynomials $L^Q(\eta)$ and where now $\eta$ is a dummy variable that can assume the role of $x$ or $y$.

In the domain shown in  \cref{fig:element} assume that we wish to interpolate the value of the function along the 1-4 direction. Note that along this line x is constant and then the function depends only on $y$. Fixing $x = x^A$ it is possible to conduct 1-dimensional interpolation along the $y$ direction as shown in \cref{fig:onedimn}. In this case $\eta$ assumes the role of $y$ and we have:

\[f(x^A,y) = L^1(y)f^1 + L^4(y)f^4.\]

Since the interpolation scheme is taking place along the 1-4 direction in terms of the 2 nodal values $f^1$ and $f^4$, the functions $L^1$ and $L^4$ in this case are the first order Lagrange polynomials associated to the points 1 and 4 respectively, and obtained with the already known product formula given in \cref{eq:coef}.

Clearly, the above scheme provides the value of the function for an arbitrary point $A$ along the 1-4 line. Proceeding similarly along the 2-3 direction, that is setting $x = x^B$ and interpolating once again along the $y$ direction we have:
\[f(x^B,y) = L^2(y)f^2 + L^3(y)f^3.\]

\begin{figure}[H]
\centering
\includegraphics[width=10cm]{inter1D.pdf}
\caption{Interpolation along the $y$-direction}
\label{fig:onedimn}
\end{figure}

So far, we have captured only the dependence on $y$ since $x$ was assumed constant as indicated by $f(x^A,y)$ and $f(x^B,y)$. The dependence on $x$ is now captured proceeding similarly along the arbitrary line $A-B$ using the functions $f(x^A,y)$ and $f(x^B,y)$ respectively as follows;

\[f(x,y) = L^A(x) f(x^A,y) + L^B(x)f(x^B,y)\]

which after substituting with the found expressions for $f(x^A,y)$ and $f(x^B,y)$ becomes

\begin{align*}
  &f(x,y) = L^A(x)\{L^1(y)f^1 + L^4(y)f^4\} + L^B(x)\{L^2(y)f^2 + L^3(y)f^3\}\\
  &f(x,y) = L^A(x)L^1(y)f^1 + L^A(x)L^4(y)f^4 + L^B(x)L^2(y)f^2 + L^B(x)L^3(y)f^3 \enspace .
\end{align*}

Note that strictly speaking there are only 2 interpolation functions of the form $L^Q(\eta)$ since one-dimensional interpolation is taking place. Thus the interpolating polynomials satisfy the following equivalences:

where
\begin{align*}
L^A(x) & \equiv L^1(x)\\
L^B(x) & \equiv L^2(x)\\
L^1(y) & \equiv L^1(y)\\
L^2(y) & \equiv L^1(y)\\
L^3(y) & \equiv L^2(y)\\
L^4(y) & \equiv L^2(x) \enspace .
\end{align*}

The resulting two-variable shape functions $N^Q(x,y)$ follow from the product of one-dimensional interpolation functions like:

\begin{align*}
N^1(x,y) & = L^1(x)L^1(y)\\
N^2(x,y) & = L^2(x)L^1(y)\\
N^3(x,y) & = L^2(x)L^2(y)\\
N^4(x,y) & = L^1(x)L^2(y) \enspace .
\end{align*}

In the actual computer implementation of the discussed interpolation scheme it is desirable to have the actual functions embedded into the code instead of having the computer finding the corresponding Lagrange polynomials each time the size of the square domain changes. In practice, the functions $N^Q(x,y)$ are coded for a canonic square of general side $h$. This resulting canonic domain can be referred as a finite element.

\paragraph*{A 2D finite element}
From the geometric point of view a {\bf finite element} is a canonical interpolation domain together with a set of shape functions and its derivatives. \Cref{fig:four-nodes-interp} shows the shape functions for a so-called bi-linear element of side $h = 1.0$. The element is called bi-linear as linear (or first order) interpolation is used along the $x$ and $y$ directions. Elements of higher order result after adding nodal points and the required corrections to the 2D-shape functions $N^Q(x,y)$.

%The shape functions for a 9-noded element are displayed in \cref{fig:nine-nodes-interp}.

\begin{figure}[H]
\centering
	\begin{subfigure}[b]{0.45\textwidth}\qquad
		\includegraphics[width=\textwidth]{shape_func-4-nodes-1.pdf}
		\caption{Shape function ${N^1(x,y)=\frac{1}{4}(1-x)(1-y)}$. }
	\end{subfigure}\,
%
	\begin{subfigure}[b]{0.45\textwidth}\qquad
		\includegraphics[width=\textwidth]{shape_func-4-nodes-2.pdf}
		\caption{Shape function ${N^2(x,y)=\frac{1}{4}(1+x)(1-y)}$.}
	\end{subfigure}\\
%
	\begin{subfigure}[b]{0.45\textwidth}\qquad
		\includegraphics[width=\textwidth]{shape_func-4-nodes-3.pdf}
		\caption{Shape function ${N^3(x,y)=\frac{1}{4}(1+x)(1+y)}$.}
	\end{subfigure}\,
%
	\begin{subfigure}[b]{0.45\textwidth}\qquad
		\includegraphics[width=\textwidth]{shape_func-4-nodes-4.pdf}
		\caption{Shape function ${N^4(x,y)=\frac{1}{4}(1-x)(1+y)}$.}
	\end{subfigure}
\caption{Shape functions for a 4-nodes element.}
\label{fig:four-nodes-interp}
\end{figure}

\begin{tcolorbox}

See Notebooks 3 and 4 for an easy to follow Python implementation of interpolation resembling the finite element method.

\end{tcolorbox}

%\begin{figure}[H]
%\centering
%	\begin{subfigure}[b]{0.45\textwidth}\qquad
%		\includegraphics[width=\textwidth]{shape_func-9-nodes-1.pdf}
%		\caption{Shape function $N^1(x,y)$. }
%	\end{subfigure}\,
%%
%	\begin{subfigure}[b]{0.45\textwidth}\qquad
%		\includegraphics[width=\textwidth]{shape_func-9-nodes-2.pdf}
%		\caption{Shape function $N^2(x,y)$.}
%	\end{subfigure}\\
%%
%	\begin{subfigure}[b]{0.45\textwidth}\qquad
%		\includegraphics[width=\textwidth]{shape_func-9-nodes-3.pdf}
%		\caption{Shape function $N^3(x,y)$.}
%	\end{subfigure}\,
%%
%	\begin{subfigure}[b]{0.45\textwidth}\qquad
%		\includegraphics[width=\textwidth]{shape_func-9-nodes-4.pdf}
%		\caption{Shape function $N^4(x,y)$.}
%	\end{subfigure}\\
%	%
%	\begin{subfigure}[b]{0.45\textwidth}\qquad
%		\includegraphics[width=\textwidth]{shape_func-9-nodes-5.pdf}
%		\caption{Shape function $N^5(x,y)$.}
%	\end{subfigure}\,
%%
%	\begin{subfigure}[b]{0.45\textwidth}\qquad
%		\includegraphics[width=\textwidth]{shape_func-9-nodes-6.pdf}
%		\caption{Shape function $N^6(x,y)$.}
%	\end{subfigure}
%	\caption{Shape functions for a 9-nodes element.}
%	\label{fig:nine-nodes-interp}
%\end{figure}
%%
%\begin{figure} [H]
%	\ContinuedFloat
%	\centering
%	\begin{subfigure}[b]{0.45\textwidth}\qquad
%		\includegraphics[width=\textwidth]{shape_func-9-nodes-7.pdf}
%		\caption{Shape function $N^7(x,y)$.}
%	\end{subfigure}\,
%%
%	\begin{subfigure}[b]{0.45\textwidth}\qquad
%		\includegraphics[width=\textwidth]{shape_func-9-nodes-8.pdf}
%		\caption{Shape function $N^8(x,y)$.}
%	\end{subfigure}\\
%	%
%	\begin{subfigure}[b]{0.45\textwidth}\qquad
%		\includegraphics[width=\textwidth]{shape_func-9-nodes-9.pdf}
%		\caption{Shape function $N^9(x,y)$.}
%	\end{subfigure}
%\caption{Shape functions for a 9-nodes element. (Continued)}
%\end{figure}

\begin{tcolorbox}
\paragraph*{Finite element mesh}
Spatial-discretization of the computational domain is in the core of finite element analysis. This corresponds to the partition of the whole domain into {\bf finite elements}. The complete set of finite elements, and its defining attributes, corresponding to a particular domain is termed a {\bf mesh}. If the geometry is irregular the mesh would contain mostly distorted elements with respect to the canonical shape. In the finite element method this is nicely solved using space transformations between the distorted and the canonical shape.

\end{tcolorbox}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section{Interpolation over distorted domains}
So far all the 2D interpolation operations have taken place over perfectly squared domains of side $h$ where the interpolation functions are known at least in terms of the side parameter $h$. In many cases and particularly in finite element methods it is common to find distorted interpolation domains (see \cref{fig:physical}) which difficult the interpolation operation as the interpolation polynomials would be element-dependent and the problem would become impossible to code in a systematic way.

\begin{figure}[h]
\centering
\includegraphics[width=6cm]{rombo}
\caption{Distorted quadrilateral interpolation domain.}
\label{fig:physical}
\end{figure}

In these cases the solution approach is based upon the continuous mapping of the distorted domain and the functions defined in this space into a constant or canonical element where the interpolation functions are always the same. Moreover, the mapping between both spaces is conducted also using interpolation theory. This idea is explained next with reference to \cref{fig:natural domain}.

\begin{figure}[h]
\centering
\includegraphics[width=8cm]{figure1.pdf}
\caption{Schematic representation of the one-to-one mapping between points in the physical space (left) and the natural or canonical space (right).}
\label{fig:natural domain}
\end{figure}

In the figure the space of the distorted domain, and with position vectors $\overrightarrow x$, is termed the physical space as this corresponds to the space of interest in a particular problem. The figure also shows a perfectly squared domain where we denote space coordinates by a position vector $\overrightarrow r$: we refer to this perfectly squared element as the canonical element and to its mathematical space as the natural space. For reasons that will be explained later, it is convenient to  have canonical elements of side $h = 2.0$. Note also that since the canonical element is a constant square of side $h$ the corresponding Lagrange interpolation functions are always the same. This implies that if the interpolation process were to be conducted over the natural space it could be easily coded as it would amount to coding the interpolation functions per se.

In mathematical language the connection between both spaces is written in terms of general functional relations as:

\begin{equation}
\begin{aligned}
x_i &=x_i(\overrightarrow r)\\
r_I &=r_I(\overrightarrow x).
\end{aligned}
\label{eq:trans}
\end{equation}

The first of these functional relationships maps every point $\overrightarrow r$ from the canonical space into a point $\overrightarrow x$ of the physical space. In particular the relationship \ref{eq:trans}(a) can be written using:

\begin{equation}
x_i=N^Q(\overrightarrow r)x^Q.
\label{eq:trans_iso}
\end{equation}

where the physical space has been represented as an interpolated approximation using as the exact functions the coordinates of the nodal points of the physical space. In particular, the first expression provides the position vector $\overrightarrow x$ in the physical space for a point that in the canonical space occupies the position vector $\overrightarrow r$. Similarly, the inverse relation gives the position vector $\overrightarrow r$ in the canonical space for a point that occupies the position vector $\overrightarrow x$ in the physical space. Note that since in \ref{eq:trans}(a) we have used the interpolation functions formulated for the perfectly squared canonical space to approximate the geometry or actual physical space this same transformation can be used to transform functions as explained next.

Assume that $f=f(\overrightarrow x)$ is a function that describes the space variation of a quantity of interest over a given domain.  Using  the mapping \ref{eq:trans} it is also possible to obtain the variation of the quantity in the canonical space after writing:

\[ f=f(\overrightarrow x)\equiv f\lbrack x_i(\overrightarrow r)\rbrack\equiv F(\overrightarrow r). \]

In the above expression $F = F(\overrightarrow r)$ represents the same physical variable but expressed in terms of the position vector in the canonical space. Being able to represent functions in the physical and the canonical space is an important result since it is now possible to conduct interpolation of functions over constant domains as:

\begin{equation}
F(\overrightarrow r)=N^Q(\overrightarrow r)f^Q.
\label{eq:trans_inter}
\end{equation}

Note that as a result of the space transformation \ref{eq:trans}, finding the physical function at a point $r_I$ is equivalent to finding the function at an associated point $x_i$ in the physical domain.

\begin{tcolorbox}

Notebooks 3 and 4 in the REPO make use of Pyhton functions to perform interpolation over two-dimensional domains. In particular, NB4 applies interpolation over arbitrary domains to visualize closed-form solutions.

\end{tcolorbox}

%
\paragraph*{Proposed problems}
\begin{enumerate}

\item \label{punto01} For the computational domain $x\in\lbrack-1.0\;,\;1.0\rbrack$ and 3 nodal points corresponding to $x^1 = -1.0$, $x^2 = +1.0$ and $x^3 = 0.0$ find the Lagrange polynomials $L^1(x)$, $L^2(x)$ and $L^3(x)$.

\item \label{punto02} Verify that the polynomials $L^1(x)$, $L^2(x)$ and $L^3(x)$ satisfy the property $L^I(x^J)=\delta^{IJ}$.

\item \label{punto03} Implement a Python script that uses the vector of known values of a function $[f^1 , f^2 , f^3 ]$ and the polynomials from problem 1 and compute the interpolating polynomial $p(x)$.

\item \label{punto04} Using $p(x)$ from problem 3 compute and plot the first order derivative of $f(x)$ in the interval $[-1 , 1]$.

\item \label{punto 05} For the function  $f(x) = {x^3} + 4{x^2} - 10$ for $x$ in the range $[-1.0, 1.0]$ find values at nodal points that result from splitting the complete interval into 4 sub-domains each one with 3 nodal points. Using these values implement a local interpolation scheme using 2-nd order local interpolation polynomials. Plot the interpolation polynomial in each sub-domain and the corresponding interpolating function $p(x)$. In the same plot compare $p(x)$ and $f(x)$. Additionally, plot the first derivative of the function obtained from $p(x)$ and $f(x)$.

\item \label{punto 06} For the Runge function defined by:

\[f(x) = \frac{1}{{1 + 25{x^2}}}\]

implement an interpolation scheme using local 1st-order Lagrange polynomials. Use (i) sub-domains of constant size $\Delta x = 0.2$ and (ii) sub-domains whose size decreases towards the edges of the interval.

\item \label{punto 07} Using an independent script (or a notebook) implement a local interpolation scheme using a canonical element of size 2.0 and use it to approximate the Runge function.

\item \label{punto 08} Assume that at the 4 nodal points of a 2D square domain of side $2h$ we know the vector field

\[\overrightarrow u=u(x,y)\widehat i+v(x,y)\widehat j \]

and where $u(x,y)$ and $v(x,y)$ are the scalar rectangular components along the $x$ and $y$ direction of a cartesian coordinate system.

\begin{itemize}
\item[•] Implement an interpolation scheme to compute the vector field $\overrightarrow u\;=\;\overrightarrow u(x,y)$ at an arbitrary point $(x,y)$.
\item[•] Use the interpolation scheme to compute $\varepsilon_{xx}=\frac{\partial u}{\partial x}$ and $\varepsilon_{yy}=\frac{\partial v}{\partial y}$
\item[•] Implement a Python script to visualize the vector field and the scalars $\varepsilon_{xx}$ and $\varepsilon_{yy}$.
\end{itemize}

\item \label{punto 09}
Implement a Python script to visualize analytic (or numerical) solutions available at a set of nodes. Use the following steps;

\begin{itemize}
\item[•] Use external software\footnote{Gmsh is an open source software for pre and post processing of complex 1D, 2D and 3D domains. Meshio is a set of Python scripts to read and write Gmsh readable files using dictionaries.} to define and mesh and arbitrary solution domain.
\item[•] Evaluate the solution at the nodal points of the mesh and store the results into arrays.
\item[•] Use Python triangularization objects together with matplotlib routines to visualize the solution.
\end{itemize}

\end{enumerate}


\documentclass[12pt, letterpaper, oneside, notitlepage, onecolumn]{article}
\author{Buck Baskin}
\title{Vector-Rotation to Euler Angles}
\pagestyle{plain}

\usepackage{parskip}

\usepackage{textcomp}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{listings}
\usepackage{color}
\usepackage{verbatim}
\usepackage{soul}
\usepackage[margin=0.69in]{geometry}

% math
\usepackage{amsmath, amssymb, amsthm, gensymb}

\usepackage{graphicx}
\graphicspath{ {images/tc1/} }
% \includegraphics[height=6.75in,angle=270]{HW25}

\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}

\lstset{frame=tb,
  language=Matlab,
  aboveskip=3mm,
  belowskip=3mm,
  showstringspaces=false,
  columns=flexible,
  basicstyle={\small\ttfamily},
  numbers=none,
  numberstyle=\tiny\color{gray},
  keywordstyle=\color{blue},
  commentstyle=\color{dkgreen},
  stringstyle=\color{mauve},
  breaklines=true,
  breakatwhitespace=true,
  tabsize=3
}

\DeclareMathOperator*{\argmax}{arg\,max}

\DeclareMathOperator*{\argmin}{arg\,min}

\newcommand{\subsubsubsection}{\paragraph}
\newcommand{\bbs}[1]{\section{#1}}
\newcommand{\bbss}[1]{\subsection{#1}}
\newcommand{\bbsss}[1]{\subsubsection{#1}}
\newcommand{\bbssss}[1]{\subsubsubsection{#1}}

\newcommand{\norm}[1]{\left\lVert#1\right\rVert}

\begin{document}
\maketitle

Some calculations were verified using Python.

\section{Answer}

$R_{BA} = R_{y}(\alpha) R_{x}(\beta) R_{y}(\gamma)$

$R_{y} (\alpha) =
\left[\begin{matrix}\cos{\left (\alpha \right )} & 0 & \sin{\left (\alpha \right )}\\0 & 1 & 0\\- \sin{\left (\alpha \right )} & 0 & \cos{\left (\alpha \right )}\end{matrix}\right]$

$R_{x} (\beta) =
\left[\begin{matrix}1 & 0 & 0\\0 & \cos{\left (\beta \right )} & - \sin{\left (\beta \right )}\\0 & \sin{\left (\beta \right )} & \cos{\left (\beta \right )}\end{matrix}\right]$

$R_{y} (\gamma) =
\left[\begin{matrix}\cos{\left (\gamma \right )} & 0 & \sin{\left (\gamma \right )}\\0 & 1 & 0\\- \sin{\left (\gamma \right )} & 0 & \cos{\left (\gamma \right )}\end{matrix}\right]$

$R_{BA} =
\left[\begin{matrix}\cos{\left (\alpha \right )} & 0 & \sin{\left (\alpha \right )}\\0 & 1 & 0\\- \sin{\left (\alpha \right )} & 0 & \cos{\left (\alpha \right )}\end{matrix}\right]
\left[\begin{matrix}1 & 0 & 0\\0 & \cos{\left (\beta \right )} & - \sin{\left (\beta \right )}\\0 & \sin{\left (\beta \right )} & \cos{\left (\beta \right )}\end{matrix}\right]
\left[\begin{matrix}\cos{\left (\gamma \right )} & 0 & \sin{\left (\gamma \right )}\\0 & 1 & 0\\- \sin{\left (\gamma \right )} & 0 & \cos{\left (\gamma \right )}\end{matrix}\right]$

$R_{BA} =
\left[\begin{matrix}- \sin{\left (\alpha \right )} \sin{\left (\gamma \right )} \cos{\left (\beta \right )} + \cos{\left (\alpha \right )} \cos{\left (\gamma \right )} & \sin{\left (\alpha \right )} \sin{\left (\beta \right )} & \sin{\left (\alpha \right )} \cos{\left (\beta \right )} \cos{\left (\gamma \right )} + \sin{\left (\gamma \right )} \cos{\left (\alpha \right )}\\\sin{\left (\beta \right )} \sin{\left (\gamma \right )} & \cos{\left (\beta \right )} & - \sin{\left (\beta \right )} \cos{\left (\gamma \right )}\\- \sin{\left (\alpha \right )} \cos{\left (\gamma \right )} - \sin{\left (\gamma \right )} \cos{\left (\alpha \right )} \cos{\left (\beta \right )} & \sin{\left (\beta \right )} \cos{\left (\alpha \right )} & - \sin{\left (\alpha \right )} \sin{\left (\gamma \right )} + \cos{\left (\alpha \right )} \cos{\left (\beta \right )} \cos{\left (\gamma \right )}\end{matrix}\right]$

The equivalent rotation to $R(\begin{bmatrix}1/\sqrt{3} \\ 1/\sqrt{3} \\ 1/\sqrt{3}\end{bmatrix}, -90 \degree)$ using Rodriguez Formula:

$e^{\hat{\omega} \theta} = I + \hat{\omega} sin(\theta) + (1 - cos(\theta) )\hat{\omega}^{2}$

For $\theta = -90$:

$e^{\hat{\omega} \theta} = I - \hat{\omega} + \hat{\omega}^{2}$

% ---- %
$\hat{\omega} =
\left[\begin{matrix}0 & - \frac{\sqrt{3}}{3} & \frac{\sqrt{3}}{3}\\\frac{\sqrt{3}}{3} & 0 & - \frac{\sqrt{3}}{3}\\- \frac{\sqrt{3}}{3} & \frac{\sqrt{3}}{3} & 0\end{matrix}\right]$

$\hat{\omega}^{2} =
\left[\begin{matrix}- \frac{2}{3} & \frac{1}{3} & \frac{1}{3}\\\frac{1}{3} & - \frac{2}{3} & \frac{1}{3}\\\frac{1}{3} & \frac{1}{3} & - \frac{2}{3}\end{matrix}\right]$

$e^{\hat{\omega} \theta} = I -
\left[\begin{matrix}0 & - \frac{\sqrt{3}}{3} & \frac{\sqrt{3}}{3}\\\frac{\sqrt{3}}{3} & 0 & - \frac{\sqrt{3}}{3}\\- \frac{\sqrt{3}}{3} & \frac{\sqrt{3}}{3} & 0\end{matrix}\right]
 +
\left[\begin{matrix}- \frac{2}{3} & \frac{1}{3} & \frac{1}{3}\\\frac{1}{3} & - \frac{2}{3} & \frac{1}{3}\\\frac{1}{3} & \frac{1}{3} & - \frac{2}{3}\end{matrix}\right]$

$e^{\hat{\omega} \theta} = \left[\begin{matrix}\frac{1}{3} & \frac{1}{3} + \frac{\sqrt{3}}{3} & - \frac{\sqrt{3}}{3} + \frac{1}{3}\\- \frac{\sqrt{3}}{3} + \frac{1}{3} & \frac{1}{3} & \frac{1}{3} + \frac{\sqrt{3}}{3}\\\frac{1}{3} + \frac{\sqrt{3}}{3} & - \frac{\sqrt{3}}{3} + \frac{1}{3} & \frac{1}{3}\end{matrix}\right] = R_{BA} =$
$\begin{bmatrix}
r_{11} & r_{12} & r_{13} \\
r_{21} & r_{22} & r_{23} \\
r_{31} & r_{32} & r_{33} \\
\end{bmatrix}$

$\left[\begin{smallmatrix}- \sin{\left (\alpha \right )} \sin{\left (\gamma \right )} \cos{\left (\beta \right )} + \cos{\left (\alpha \right )} \cos{\left (\gamma \right )} & \sin{\left (\alpha \right )} \sin{\left (\beta \right )} & \sin{\left (\alpha \right )} \cos{\left (\beta \right )} \cos{\left (\gamma \right )} + \sin{\left (\gamma \right )} \cos{\left (\alpha \right )}\\\sin{\left (\beta \right )} \sin{\left (\gamma \right )} & \cos{\left (\beta \right )} & - \sin{\left (\beta \right )} \cos{\left (\gamma \right )}\\- \sin{\left (\alpha \right )} \cos{\left (\gamma \right )} - \sin{\left (\gamma \right )} \cos{\left (\alpha \right )} \cos{\left (\beta \right )} & \sin{\left (\beta \right )} \cos{\left (\alpha \right )} & - \sin{\left (\alpha \right )} \sin{\left (\gamma \right )} + \cos{\left (\alpha \right )} \cos{\left (\beta \right )} \cos{\left (\gamma \right )}\end{smallmatrix}\right] = $
$\begin{bmatrix}
r_{11} & r_{12} & r_{13} \\
r_{21} & r_{22} & r_{23} \\
r_{31} & r_{32} & r_{33} \\
\end{bmatrix}$

\subsection{Solving for $\alpha, \beta, \gamma$}

\subsubsection{$\beta$}

Using the atan method, one can solve for $\beta$ using the middle column of the matrix. The middle value, $r_{22} = cos(\beta)$. Using $r_{12}^{2} + r_{32}^{2} = sin^{2}(\beta)(sin^{2}(\gamma) + cos^{2}(\gamma)) = sin^{2}(\beta)$, $sin(\beta) = \sqrt{r_{12}^{2} + r_{32}^{2}}$. Therefore, $\beta = atan2(\sqrt{r_{12}^{2} + r_{32}^{2}}, r_{22})$.

\subsubsection{$\gamma$}

Looking at two elements in the middle row of the matrix, one can solve for $\gamma$ using the atan method. $r_{21} = sin(\beta) sin(\gamma)$, so $sin(\gamma) = r_{21} / sin(\beta)$. $r_{23} = -sin(\beta) cos(\gamma)$, so $cos(\gamma) = -r_{23} / sin(\beta)$. Therefore, $\gamma = atan2(r_{21} / sin(\beta), -r_{23} / sin(\beta))$.

\subsubsection{$\alpha$}

$r_{12} = sin(\alpha) sin(\beta)$, $sin(\alpha) = r_{12} / sin(\beta)$

$r_{32} = cos(\alpha) sin(\beta)$, $cos(\alpha) = r_{32} / sin(\beta)$

$\alpha = atan2(r_{12} / sin(\beta), r_{32} / sin(\beta))$

\subsection{Conclusion}

Substituting the correct values from $R_{BA}$ calculated by Rodriguez Formula into the spaces allocated by $r_{ij}$ that were solved into an $atan2$ formula using $R_{BA}$ calculated by the rotation matrices gives the solution for the equivalent rotations in radians (rounded to 3 decimal places).

$\alpha = 1.833, \beta = 1.231, \gamma = -2.880$

\end{document}


\chapter{Yao's Principle}
Using game theory we may develop a method for proving lower bounds on complexity
of randomized algorithms. To illustrate this method we will study the problem we
duscussed in \Cref{chapter:expectation,chapter:structural-induciton}: Alice
choose a number from $\range{1000}$, Bob can flip a coin and ask yes/no
questions; Bob would like to guess it using, on average, as few questions as
possible. We claim that Bob needs at least $9$ questions.
\begin{theorem}
\label{theorem:randomized-dts-lower-bound}
  Let $\Omega$ be a set of $B$-decision trees and $\Distribution{D}$ be a
  probability distribution on $\Omega$. If $\DTValue{T}{x} = x$ for all
  $x \in \range{1000}$ and $T \in \Omega$, then
  $\expectation[\Distribution{D}]{\DTValue{T}{x}} \ge 9$ for some $x \in
  \range{1000}$.
\end{theorem}

To prove this statement we would need to give formal notions of randmoized
$B$-decision tree complexity and heuristic $B$-decision tree cmplexity.
\begin{definition}
  Let $f : [1000] \to \Z$ be a function. We say that $\bar{T} =
  (\Omega, \Distribution{T})$ is a \emph{randomized}\footnote{%
    In fact, there are many notions of randomized computation; the algorithms
    discussed in this chapter are usually called zero-error randomized
    algorithms.
  }
  \emph{$B$-decision tree for $f$ of cost $C$}
  if $\Omega$ is a set of $B$-decision trees, $\Distribution{T}$ is a
  probability distribution on $\Omega$, $\DTValue{T}{x} = f(x)$ for all $x \in
  \range{1000}$, and
  $\expectation[T \gets \Distribution{T}]{\DTHeight[x]{T}} = C$.

  We also say that $T$ is a \emph{heuristic $B$-decision tree for $f$ of
  expected cost $C$ with respect to a distribution $\Distribution{X}$} if
  $\DTValue{T}{x} = f(x)$ for all $x \in \range{1000}$ and
  $\expectation[x \gets \Distribution{X}]{\DTHeight[x]{T}} = C$.

  The \emph{randmoized $B$-decision tree complexity $\RandCBDT{f}$ of $f$}
  is equal to the minimal $C$ such that there is a randomized $B$-decision tree
  for $f$ of cost $C$.

  The \emph{heuristic $B$-decision tree complexity
  $\HeurCBDT[\Distribution{X}]{f}$ of $f$ with respect to $\Distribution{X}$} is
  equal to the minimal $C$ such that there is a $B$-decision tree for $f$ of
  expected cost $C$ with respect to $\Distribution{X}$. Finally, \emph{the
  heuristic $B$-decision tree complexity $\HeurCBDT{f}$ of $f$} is equal to the
  maximum of $\HeurCBDT[\Distribution{X}]{f}$ over all distributions
  $\Distribution{X}$.
\end{definition}

Nonetheless that we have not had the notion of randomized complexity before, we
proved upper bounds in \Cref{chapter:expectation}. Unfortunately, proving lower
bounds directly for randomized complexity is not easy, but it is usually way
easier to prove lower bounds on heuristic complexity. Yao's principle (also
called Yao's minimax principle or Yao's lemma) gives a connection between these
measures.
\begin{theorem}[Yao's principle]
\label{theorem:yaos-principle}
  Let $f : [1000] \to \Z$ be a function. Then $\RandCBDT{f} =
  \HeurCBDT{f}$.
\end{theorem}

Using this principle is easy to prove the lower bound.
\begin{proof}[Proof of \Cref{theorem:randomized-dts-lower-bound}]
  Note that the theorem says that $\RandCBDT{\identity{\range{1000}}} \ge 9$;
  hence, it is enough to prove that $\HeurCBDT{\identity{\range{1000}}} \ge 9$.
  Let us assume the opposite; i.e., that for any distribution $\Distribution{X}$
  there is a $B$-decision tree $T$ such that $\DTValue{T}{x} = x$ for all $x \in
  \range{1000}$ and $\expectation[x \gets \Distribution{X}]{\DTHeight[x]{T}} <
  9$.
  This implies that there is a $B$-decision tree $T$ such that
  $\frac{1}{1000} \sum_{x \in \range{1000}} \DTHeight[x]{T} < 9$; we are going to prove that
  this is impossible. Let us fix such a $T$.

  We prove, using induction by $|S|$ that $\sum_{x \in S} \DTHeight[x]{T} \ge
  |S| \log_2 |S|$. The base case for $|S| = 1$ is clear since
  $\DTHeight[x]{T} \ge 1$ for all $x \in \range{1000}$.

  Let us prove the induction step. Let $T = \DTIf{f}{T_0}{T_1}$, and let
  $S_0 = \set[f(x) = 0]{x \in S}$ and $S_1 = \set[f(x) = 1]{x \in S}$.
  It is clear that $\sum_{x \in S} \DTHeight[x]{T} = |S| +
  \sum_{x \in S_0} \DTHeight[x]{T} + \sum_{x \in S_1} \DTHeight[x]{T}$. By the
  induction hypothesis, this implies that $\sum_{x \in S} \DTHeight[x]{T} = |S|
  + |S_0| \log_2 |S_0| + |S_1| \log_2 |S_1| \ge |S| + |S| \log_2 (|S| / 2) =
  |S| \log_2 |S|$.

  Therefore, $\frac{1}{1000} \sum_{x \in \range{1000}} \DTHeight[x]{T} \ge
  \log_2 1000 > 9$, which contradicts the assumption.
\end{proof}

Now we are ready to prove Yao's principle.
\begin{proof}[Proof of \Cref{theorem:yaos-principle}]
  We start from proving that $\RandCBDT{f} \ge \HeurCBDT{f}$. Assume the
  opposite; i.e., that $\RandCBDT{f} < \HeurCBDT{f}$.

  Let $\bar{T} = (\Omega, \Distribution{T})$ be a randomized $B$-decision tree
  for $f$ of cost $C < \HeurCBDT{f}$. Note that
  $\expectation[T \gets \Distribution{T}]{\DTHeight[x]{T}} \le C$ for all $x \in
  \range{1000}$. Hence,
  $\expectation[x \gets \Distribution{X}]{
    \expectation[T \gets \Distribution{T}]{\DTHeight[x]{T}}
  } \le C$. However,
  \begin{multline*}
    C \ge
    \expectation[x \gets \Distribution{X}]{
      \expectation[T \gets \Distribution{T}]{\DTHeight[x]{T}}
    } = \\
    \expectation[T \gets \Distribution{T}]{
      \expectation[x \gets \Distribution{X}]{\DTHeight[x]{T}}
    } \ge
    \expectation[T \gets \Distribution{T}]{
      \HeurCBDT[\Distribution{X}]{f}
    } = \HeurCBDT[\Distribution{X}]{f}.
  \end{multline*}
  As a result, $C \ge \HeurCBDT{f}$, which is a contradiction to the assumption.

  Let us prove that $\RandCBDT{f} \le \HeurCBDT{f}$. To prove this inequality we
  are going to use von Neumann's minimax theorem (\Cref{theorem:von-neumanns-minimax}).
  Let $X = \range{1000}$, $Y$ be a set of all $B$-decision trees $T$ such that
  $\DTHeight{T} \le 1000$ and $\DTValue{T}{x} = f(x)$ for all $x \in
  \range{1000}$, and $A(x, T) = - B(x, T) = \DTHeight[x]{T}$.

  \Cref{theorem:von-neumanns-minimax} implies that there are distributions
  $\Distribution{X}$ and $\Distribution{T}$ over $X$ and $Y$ respectively such
  that
  \[
    \expectation[x \gets \Distribution{X}]{\DTHeight[x]{T'}} \ge
    \expectation[x \gets \Distribution{X}]{
      \expectation[T \gets \Distribution{T}]{\DTHeight[x]{T}}
    } \ge
    \expectation[T \gets \Distribution{T}]{\DTHeight[x']{T}}
  \]
  for all $x' \in X$ and $T' \in Y$. Which implies that
  \[
    \HeurCBDT[\Distribution{X}]{f} \ge
    \expectation[x \gets \Distribution{X}]{
      \expectation[T \gets \Distribution{T}]{\DTHeight[x]{T}}
    } \ge \RandCBDT{f}.
  \]
\end{proof}


\RequirePackage[l2tabu, orthodox]{nag}
\documentclass{article}

\usepackage[letterpaper]{geometry}
\usepackage{booktabs}
\usepackage{mathtools}
\usepackage[binary-units=true]{siunitx}
\usepackage{tikz}

\title{ECE 487 Assignment 6}
\author{Michael Kwok}
\begin{document}

\maketitle
\subsection*{CSMA/CD minimum frame size}
Propagation Time: \(T_p = \frac{1200}{3 \times 10^8} = 4\times 10 ^{-6}\) \\
Frame time: \(T_{fr} \geq 8 \times 10^{-6} = \SI{8}{\micro\second}\) \\
Minimum frame size: \( \SI{10}{\mega\bit\per\second} \cdot \SI{8}{\micro\second} = 80 \text{ bits}\)

\subsection*{Reservation Access Method}
Reservation phrase: \(\SI{10}{\micro\second} \cdot 5 = \SI{50}{\micro\second}\) \\
Data transmission time: \( \frac{5 \cdot 1000}{10 \times 10^6} = \SI{0.00055}{\second} \) \\
Throughput: \(\frac{0.001}{0.00055} = \SI{1.818}{\mega\bit\per\second}\)

\subsection*{WLAN}
\begin{enumerate}
    \item First transmission: \(\text{DIFS} + \SI{200}{\micro\second} = \SI{250}{\micro\second}\)
    \item \(\text{RTS}+ \text{SIFS} + \text{CTS} + \text{SIFS} + \text{DATA} + \text{SIFS} + \text{ACK} + (\text{Station 2 timer} - \text{Station 1 timer}) = \SI{1580}{\micro\second}\)
\end{enumerate}

\end{document}


\documentclass[axodraw]{amsart}
\usepackage{amsmath}
\usepackage{feyn}
\usepackage{feynmp}
\begin{document}

\section{feyn}

\begin{align*}
    \feyn{a} \\
    \feyn{b} \\
    \feyn{c} \\
    \feyn{d} \\
    \feyn{e} \\
    \feyn{f} \\
    \feyn{g} \\
    \feyn{h} \\
    \feyn{i} \\
    \feyn{j} \\
    \feyn{k} \\
    \feyn{l} \\
    \feyn{m} \\
    \feyn{n} \\
    \feyn{o} \\
    \feyn{p} \\
    \feyn{q} \\
    \feyn{r} \\
    \feyn{s} \\
    \feyn{t} \\
    \feyn{u} \\
    \feyn{v} \\
    \feyn{w} \\
    \feyn{x} \\
    \feyn{y} \\
    \feyn{z} \\
\end{align*}
\[
\Diagram{\vertexlabel^a \\
  fd \\
& g\vertexlabel_{\mu,c} \\
\vertexlabel_b fu \\
}
=
\Diagram{\vertexlabel^a \\
  fdA \\
& gA\vertexlabel_{\mu,c} \\
\vertexlabel_b fuV \\
}
=
\Diagram{\momentum{fdA}a\\
   & \vertexlabel_{\gamma_\mu} \momentum{gA}{\mu,c} \\
\momentum{fuV}b
}
=
ig\gamma_\mu (T^c)_{ab}
\]

Two-loop diagram:
\[
\feyn{ff gl ff}
\to
\feyn{ff\bigbosonloopA\smallbosonloopV ff}
=
\feyn{!{fA}p f!{glBV}k !{glSA}l a\vertexlabel_{p+k-l} ff}
\]

Bremsstrahlung:
\[
  \Diagram{        & x \\
                   & ![ulft]{gvV}{q=p'+k-p}   & !{guA}k \\
           ![bot]{fA}p & ![bot]{fA}{p'+k} & ![lrt]{fA}{p'} \\
        }
        +
  \Diagram{    & !{guA}k \\
           !{fA}p & fs ![bot]{fA}{p-k} & !{fA}{p'} \\
               &         & ![lrt]{gvA}{q=p'+k-p} \\
               &         & x\\
        }
\]

OPE:
\def\bracket#1{\langle #1 \rangle}
\begin{eqnarray*}
-i\Sigma_{\mathrm{ope}} &=&
     \left[\feyn{faf + fsfglffs
                 + \cdots} \right] 1 \\
     && \quad{}+
           \left[ \feyn{ fxfs0glfs0xf
                  + \cdots} \right]
         \bracket{\overline\psi M \psi} \\
     && \quad{}+ \left[
         \Diagram{   &x  &x  \\
                     &gv &gv \\
                  fs &fs &fs \\} + \cdots
         \right]
         \bracket{G_{\mu\nu}^a G_{\mu\nu}^a}\\
\end{eqnarray*}

Complete vertex:
\begin{eqnarray*}
\feyn{fcf} &=& \feyn{faf + fpf + fpfpf + \cdots} \\
    &=& \sum_{n=0}^\infty \feyn{fA ( pfA)}^n \\
    &=& \feyn{\frac{fA}{1-(pfA)}}.
\end{eqnarray*}

\section{feynmp}
\begin{fmffile}{simple} \begin{fmfgraph}(40,25)
% Note that the size is given in normal parentheses % instead of curly brackets.
% Define external vertices from bottom to top
\fmfleft{i1,i2}
\fmfright{o1,o2}
\fmf{fermion}{i1,v1,o1}
\fmf{fermion}{i2,v2,o2}
\fmf{photon}{v1,v2}
\end{fmfgraph}
\end{fmffile}

\begin{fmffile}{simple_tree} \begin{fmfgraph}(40,25)
\fmfleft{i} \fmfright{o1,o2,o3} \fmf{fermion}{i,v1,o1} \fmf{photon}{v1,v2} \fmf{fermion}{o2,v2,o3}
\end{fmfgraph} \end{fmffile}

\begin{fmffile}{simple_box} \begin{fmfgraph}(40,25)
\fmfleft{i1,i2} \fmfright{o1,o2} \fmf{fermion}{i1,v1,v2,o1} \fmf{fermion}{o2,v4,v3,i2} \fmf{photon}{v1,v3} \fmf{photon}{v2,v4}
\end{fmfgraph} \end{fmffile}

\begin{fmffile}{simple_labels} \begin{fmfgraph*}(40,25)
\fmfleft{i1,i2} \fmfright{o1,o2}
\fmflabel{$e^-$}{i1} \fmflabel{$e^+$}{i2} \fmflabel{$e^+,\mu^+$}{o1} \fmflabel{$e^-,\mu^-$}{o2}
\fmflabel{$i\sqrt{\alpha}$}{v1} \fmflabel{$i\sqrt{\alpha}$}{v2}
\fmf{fermion}{i1,v1,i2} \fmf{fermion}{o1,v2,o2}
\fmf{photon,label=$\gamma,,Z^0$}{v1,v2} \end{fmfgraph*}
\end{fmffile}

\begin{fmffile}{tree} \begin{fmfgraph}(40,30)
\fmfbottom{i1,d1,o1} \fmfright{o0,o2,o3} \fmf{fermion}{i1,v1,o1} \fmffreeze \fmf{fermion}{o2,v2,o3} \fmf{photon,tension=1.5}{v1,v2}
\end{fmfgraph} \end{fmffile}

\begin{fmffile}{simple_cross} % This is a bad sample. \begin{fmfgraph}(40,25)
\fmfleft{i1,i2} \fmfright{o1,o2} \fmf{fermion}{i1,v1,o2} \fmf{fermion}{i2,v2,o1} \fmf{photon}{v1,v2}
\end{fmfgraph} \end{fmffile}

\begin{fmffile}{penguin_both} \begin{fmfgraph}(40,25)
\fmfleft{i1,i2} \fmfright{o1,o2} \fmf{fermion}{i1,v1,o1} \fmf{fermion}{i2,v2,v3,v4,o2} \fmf{photon}{v1,v3} \fmf{photon,left=0.5,tension=0.2}{v2,v4} % W line
\end{fmfgraph} \end{fmffile}

\begin{fmffile}{penguin_oneside} \begin{fmfgraph*}(40,30)
\fmfstraight \fmfleft{g1,g2,i3} % g1 and g2 will be used later \fmfright{o1,o2,o3} \fmf{fermion}{i3,v2,v3,v4,o3} \fmf{fermion}{o1,v1,o2} \fmf{photon,label=$Z^0$}{v1,v3} \fmf{photon,left=0.5,tension=0.2}{v2,v4}
\fmf{phantom}{g1,v1,g2} % pull Z0 from the left \end{fmfgraph*}
\end{fmffile}

\begin{fmffile}{sunset} \begin{fmfgraph}(40,40)
\fmfleft{i} \fmfright{o} \fmf{plain,tension=5}{i,v1} \fmf{plain,tension=5}{v2,o} \fmf{plain,left,tension=0.4}{v1,v2,v1} \fmf{plain}{v1,v2} \fmfdot{v1,v2}
\end{fmfgraph} \end{fmffile}

\begin{fmffile}{ears} \begin{fmfgraph}(50,30)
\fmftop{t0,t1,t2,t3} \fmfbottom{b0,b1,b2,b3} \fmf{phantom}{t1,v1,b1} \fmf{phantom}{t2,v2,b2} \fmffreeze \fmf{plain,right}{v1,v2,v1} \fmf{plain,tension=0.8,right=270}{v1,v1} \fmf{plain,tension=0.8,right}{v2,v2} \fmfdot{v1,v2}
\end{fmfgraph} \end{fmffile}

\begin{fmffile}{eyeglasses} \begin{fmfgraph}(50,30)
\fmfleft{i} \fmfright{o} \fmf{phantom,tension=10}{i,i1} \fmf{phantom,tension=10}{o,o1} \fmf{fermion,left,tension=0.4}{i1,v1,i1} \fmf{fermion,right,tension=0.4}{o1,v2,o1} \fmf{dashes}{v1,v2}
\end{fmfgraph} \end{fmffile}

\end{document}