\subsection{Homology theory}

Let's remind ourselves of the First Isomorphism Theorem
\begin{thm}
(First Isomorphism Theorem) Let $G$, $H$ be groups, and let $\phi:G\to H$
be a homomorphism, then\end{thm}
\begin{enumerate}
\item $\text{Img}(\phi)\cong G/\ker(\phi)$,
\item $\ker(\psi)$ is a normal subgroup of $G$,
\item $\text{Img}(\psi)$ is a subgroup of $H$,
\end{enumerate}
In particular if $\phi$ is onto (surjective) then $H\cong G/\ker(\psi)$.
\begin{defn}
The standard $p$-simplex is

\begin{align*}
\Delta^{p} & =\{(t_{0},\dots,t_{p})\in\mathbb{R}^{p+1}\mid t_{i}\geqslant0,\sum_{i}t_{i}=1\}.
\end{align*}

\end{defn}

\begin{defn}
Let $X$ be a topological space. A singular $p$-simplex is a continuous
map $\sigma:\Delta^{p}\to X$.
\end{defn}

\begin{defn}
A $p$-chain is denoted $C_{p}(X)=\{\text{singular \ensuremath{p}-simplesx in \ensuremath{X}}\}=\text{free Abelian group generated by all singular \ensuremath{p}-simplexes in \ensuremath{X}.}$
So a $p$-chain is a formal sum

\begin{align*}
C_{p}(X)=\sum_{i}n_{i}\sigma_{i}
\end{align*}

where each $\sigma_{i}$ is a signular $p$-simplex in $X$ (and all
but finitely-many $n_{i}$ are $0$.)
\end{defn}

\begin{defn}
(Boundaries)

The $i$th face map for the standard $p$-simplex is the affine map

\begin{align*}
[e_{0},\dots,\hat{e}_{i},\dots,e_{p}] & :\Delta^{p-1}\to\Delta^{p}
\end{align*}

(where the hat means we \emph{omit} this vertex.) Let $\sigma^{(i)}:\Delta^{p-1}\to X$
be given by

\begin{align*}
\sigma^{(i)} & =\sigma\circ[e_{0},\dots,\hat{e}_{i},\dots,e_{p}]
\end{align*}

then defined the boundary of $\sigma$, $\partial\sigma$ as

\begin{align*}
\partial\sigma & =\sum_{i=0}^{p}(-1)^{i}\sigma^{(i)}
\end{align*}

and we have $\partial\sigma\in C_{p-1}(X)$. So extend by linearity
to get a homeomorphism $\partial=\partial_{p}:C_{p}(X)\to C_{p-1}(X)$.
Say $C_{p}(X)=0$ for $p<0$, and that $\partial\sigma=0$ for an
singular $0$-chain.\end{defn}
\begin{lem}
$\partial\circ\partial=0$. (Proof by linearity, doing the sums, separating
and noting that they cancel.)\end{lem}
\begin{defn}
Cycles and boundaries\end{defn}
\begin{itemize}
\item $\alpha\in C_{p}(X)$ is a $p$-cycle if $\partial\alpha=0$.
\item $\alpha$ is a $p$-boundary if $\alpha=\partial\beta$ for $\beta\in C_{p+1}(X)$
\item Let $Z_{p}=\{\text{\ensuremath{p}-cycles in \ensuremath{X}}\}=\ker(\partial_{p})$
\item $B_{p}(X)=\{\text{\ensuremath{p}-boundaries in \ensuremath{X}}\}=\text{Img}(\partial_{p+1})$.
\end{itemize}
Then $B_{p}(X)\leq Z_{p}(X)$ (subgroup) since $\partial\circ\partial=0$.
\begin{defn}
The $p$th singular homology group of $X$ is

\begin{align*}
H_{P}(X) & =\frac{Z_{p}(X)}{B_{p}(X)}\\
 & =\frac{\ker(\partial_{p})}{\text{Img}(\partial_{p+1})}.
\end{align*}

Geometrically, the elements of $H_{p}(X)$ are equivalence classes
of $p$-cycles where $\alpha\sim\alpha'$ if $\alpha'=\alpha+\partial\beta$,
$\beta\in C_{p+1}(X)$. We write $[\alpha]$ for the homology class
of a $p$-cycle $\alpha\in C_{p}(X)$.\end{defn}
\begin{lem}
If $\{X_{\alpha}\}$ are the path connected components of $X$, then
$C_{p}(X)=\bigoplus_{\alpha}C_{p}(X_{\alpha})$ and $\partial:C_{p}(X_{\alpha})\to C_{p-1}(X_{\alpha})$,
hence $H_{p}(X)=\bigoplus_{\alpha}H_{p}(X_{\alpha})$.\end{lem}
\begin{defn}
The singular chain complex of $X$

\begin{align*}
C_{p}\to C_{p-1}\to\dots\to C_{0}\to0
\end{align*}

is a sequence of Abelian groups and homomorphisms such that $\partial_{p-1}\circ\partial_{p}=0$.\end{defn}
\begin{thm}
(Interpretation of $H_{0}$)

If $X$ is non-empty and path connected, then $H_{0}(X)\cong\mathbb{Z}$.\end{thm}
\begin{cor}
If $X$ has $k$ path components, then $H_{0}(X)\cong\mathbb{Z}^{k}$.\end{cor}
\begin{thm}
(Interpretation of $H_{1}$) If $X$ is path connected, $x_{0}\in H$,
then $H_{1}(X)$ is the Abelianisation of $\pi_{1}(X,x_{0})$.
\end{thm}

\subsubsection{Effects of continuous maps}

If $f:X\to Y$ is continuous and $\sigma:\Delta^{p}\to X$ is a singular
$p$-simplex in $X$ then $f\circ\sigma:\Delta^{p}\to Y$ is a singular
$p$-simplex in $Y$. Then there exists an induced homomorphism $f_{\#}:C_{p}(X)\to C_{p}(Y)$
with $f_{\#}\left(\sum n_{i}\sigma_{i}\right)=\sum n_{i}(f\circ\sigma_{i})$.
\begin{lem}
$\partial\circ f_{\#}=f_{\#}\circ\partial$.\end{lem}
\begin{thm}
There are induced homomorphisms $f_{*}=H_{p}(f):H_{p}(X)\to H_{p}(Y)$,
where $f_{*}([c])=[f_{\#}c]$.\end{thm}
\begin{cor}
If $f:X\to Y$ is a homeomorphism, then $f_{*}:H_{p}(X)\to H_{p}(Y)$
is an isomorphism. Therefore $H_{p}$ are topological invariants.
\end{cor}

\subsubsection{Homotopy invariance of homology}
\begin{thm}
If $f,g$ are two homotopic maps $f,g:X\to Y$, then $f_{*}=g_{*}:H_{p}(X)\to H_{p}(Y),$
$p\geqslant0$.\end{thm}
\begin{cor}
If $f:X\to Y$ is a homotopy equivalence, then $f_{*}:H_{p}(X)\to H_{p}(Y)$
is an isomorphism.\end{cor}
\begin{defn}
(Simplicial homology)

A $\Delta$-complex is a topological space $X$ obtained from the
disjoint uninion of simplies, with ordered vertices glued together
by affine homeomorphisms.
\end{defn}
Define $C_{n}=C_{n}^{\Delta}(X)=\left\{ \sum n_{i}\sigma_{i}\mid n_{i}\in\mathbb{Z},\,\sigma_{i}\,\text{geometric \ensuremath{n}-simplices in \ensuremath{X}}\right\} $.
Then define $\partial:C_{n}\to C_{n-1}$ as before. Then we define
simplicial homology

\begin{align*}
H_{n}^{\Delta}(X) & =\frac{\ker(\partial_{n})}{\text{Img}(\partial_{n+1})}
\end{align*}

\begin{thm}
If $X$ is a $\Delta$-complex then $H_{n}^{\Delta}(X)\cong H_{n}(X)$.
(I.e. ``simplicial homology'' = ``singular homology'').\end{thm}
\begin{defn}
(Reduced homology) If $X=\emptyset$ then we can define an augmented
singular chain complex

\begin{align*}
\cdots\to C_{n}\to^{\partial_{n}}\partial_{n-1}\to^{\partial_{n-1}}\cdots\partial_{1}\to^{\partial_{1}}C_{0}\to^{\epsilon}\mathbb{Z}\to0
\end{align*}

where we let $\epsilon\left(\sum n_{x}x\right)=\sum n_{x}$. Now $\tilde{H}_{n}(X)=0$
if $X$ is contractible.
\end{defn}

\subsubsection{Exact sequences}
\begin{defn}
A sequence (finite or infinite) of Abelian groups and homomorphisms

\begin{align*}
\xymatrix{\cdots\ar[r] & A_{n}\ar[r]^{\alpha_{n}} & A_{n-1}\ar[r]^{\alpha_{n-1}} & \cdots}
\end{align*}

is called \emph{exact} if the image of each homomorphism is the kernal
of the next. I.e. $\text{Img}(\alpha_{n})=\ker(\alpha_{n-1})$ $\forall n$.
\end{defn}

\paragraph{Examples}
\begin{enumerate}
\item $0\to A\to^{f}B$ exact $\Leftrightarrow\ker(f)=\{0\}\Leftrightarrow f$
is 1-1 (injective)
\item $A\to^{f}B\to^{0}0$ exact $\Leftrightarrow\text{Img}(f)=\ker(B)=0$
$\Leftrightarrow f$ is onto (surjective).
\item $0\to^{0}A\to^{f}B\to^{0}0$ exact $\Leftrightarrow$ $f$ is an isomorphism.
\item $0\to^{0}A\to^{f}B\to^{g}C\to^{0}0$ exact $\Leftrightarrow$ $f$
is 1-1, $g$ is onto, $C=\text{Img}(g)\cong B/\ker(g)=B/\text{Img}(f)$\end{enumerate}
\begin{defn}
A short exact sequence $0\to A\to^{f}B\to^{g}C\to0$ splits if there
exists a homomorphism such that $g\circ h=1_{C}$ ($h:C\to B$). Then
$B=f(A)\oplus h(C)\cong A\oplus B$. The sequence splits if, for example,
$C$ is a free Abelian group.
\end{defn}

\paragraph{Mayer-Vietoris sequence}

An analogue to SvK. Let $X=X_{1}\cup X_{2}$ be a topological space,
where $X_{1},X_{2},X_{0}=X_{1}\cap X_{2}$ are open sets in $X$,
then we have inclusion maps

\begin{align*}
\xymatrix{ & X_{1}\ar[dr]_{j_{1}}\\
X_{0}\ar[ur]^{i_{1}}\ar[dr]_{i_{2}} &  & X\\
 & X_{2}\ar[ur]_{j_{2}}
}
\end{align*}

then we have

\begin{align*}
\xymatrix{ & H_{p}(X_{1})\ar[dr]_{j_{1*}}\\
H_{P}(X_{0})\ar[ur]^{i_{1*}}\ar[dr]_{i_{2*}} &  & H_{p}(X)\\
 & H_{p}(X_{2})\ar[ur]_{j_{2*}}
}
\end{align*}

\begin{thm}
(Mayer-Vietoris theorem) There is a long exact sequence in Homology

\begin{align*}
\xymatrix{\cdots\ar[r] & H_{p}(X_{0})\ar[r]^{i_{1*}\oplus i_{2*}} & H_{p}(X_{1})\oplus H_{p}(X_{2})\ar[r]^{j_{1*}-j_{2*}} & H_{p}(X)\ar[r]^{\partial_{*}} & H_{p-1}(X_{0})}
\end{align*}

\end{thm}

From this we can calculate the homotopy of the $n$-sphere as

\begin{align*}
\tilde{H}_{p}(S^{n}) & =\begin{cases}
\mathbb{Z} & p=n\\
0 & \text{otherwise}
\end{cases}
\end{align*}

\begin{thm}
(Topological invariance of dimension) If $\mathbb{R}^{n}\approx\mathbb{R}^{m}$,
then $n=m$. \end{thm}
\begin{proof}
If $f:\mathbb{R}^{n}\to\mathbb{R}^{m}$ is a homeomorphism then we
also have an induced homeomorphism $\mathbb{R}^{n}-\{pt\}\to\mathbb{R}^{m}-\{pt\}$
so we have a homotopy $S^{n-1}\simeq S^{m-1}$ so $\tilde{H}_{*}(S^{n-1})\cong\tilde{H}_{*}(S^{m-1})$
so $n=m$.\end{proof}
\begin{defn}
A continous map $f:S^{n}\to S^{n}$ has degree $d$ if $f_{*}=H_{n}(f):\tilde{H}_{n}(S^{n})\to\tilde{H}_{n}(S^{n})$
is multiplication be the integer $d$.\end{defn}
\begin{rem}
$f\simeq g\implies\deg(f)=\deg(g)$. $\deg(f\circ g)=\deg(f)\times\deg(g)$.\end{rem}
\begin{thm}
(Subdivision theorem) Let $U=\{U_{j}\}$ be an open cover of $X$
(or a collection of subsets of $X$ whose inteiors cover $X$.). Then
let $C_{p}^{U}(X)$ be the subgroup of $C_{p}(X)$ consisting of singular
$p$-chains $\sum n_{i}\sigma_{i}$ such that the image $\sigma_{i}(\Delta^{p})$
of each $\sigma_{i}$ lies in one of the sets in $U$.

Then $\partial:C_{p}^{U}(X)\to C_{p-1}^{U}(X)$, so we get a chain
complex $C_{*}^{U}(X)$ with homology $H_{*}^{U}(X)$.

The inclusion $C_{*}^{U}(X)\to C_{*}(U)$ induces an isomorphism $H_{p}^{U}(X)\to H_{p}(X)$
for all $p$.\end{thm}
\begin{defn}
(Relative homotopy) Let $(X,A)$ be a pair of topological spaces with
$A\leq X$ ($A$ is a subspace.) The inclusion $A\hookrightarrow X$
induces the inclusions $C_{p}(A)\to C_{p}(X)$ for all $p$ and we
can define

\begin{align*}
C_{p}(X,A) & =\frac{C_{p}(X)}{C_{p}(A)}.
\end{align*}

\end{defn}
Then $\partial:C_{p}(X)\to C_{p-1}(X)$ takes $C_{p}(A)\to C_{p-1}(A)$
so induces a homomorphism $\bar{\partial}:C_{p}(X,A)\to C_{p-1}(X,A)$
and $\bar{\partial}\circ\bar{\partial}=0$. This gives a chain complex
with homology $H_{p}(X,A)=\frac{\ker(\bar{\partial}_{p})}{\text{Img}(\bar{\partial}_{p+1})}$
called the ``relative homology of $X$ mod $A$.''


\section{Multigrid methods for numerical PDEs}\label{sec:mg}

With the restriction $R_{\ell}^{\ell+1}$ and prolongation $P_{\ell+1}^\ell$
obtained in Lemma \ref{ris:plon},
%as defined in \eqref{mg-prolong} and restriction $R_{\ell}^{\ell+1} = (P_{\ell+1}^{\ell})^T$.
we have the following relationship to define coarse operation
\begin{equation}\label{eq:def_coarse}
A^{\ell+1}=R_{\ell}^{\ell+1} A^{\ell}P_{\ell+1}^{\ell} \quad (\ell = 1:J-1),
\end{equation}
with $A^1 = A$.
Similarly, with the smoother obtained by \eqref{eq:convS}, we can define
$S^{\ell}: \mathbb{R}^{m_\ell \times n_\ell} \mapsto \mathbb{R}^{m_\ell \times n_\ell}$.

Now using the smoother $S^\ell$, prolongation $P^{\ell}_{\ell+1}$, restriction $R_{\ell}^{\ell+1}$ and mapping
$A^\ell$ as given in \eqref{eq:def_coarse}, we can formulate the following algorithm
 as a major component of a multigrid algorithm.
\begin{breakablealgorithm}%[!htb]
	\caption{$(u^{\ell,\nu_\ell}: ~\ell = 1:J) = {\text{MG0}}(f; J,\nu_1, \cdots, \nu_J)$}
	\label{alg:L-Slash0}
	\begin{algorithmic}
		\State Set up
		$$
		f^1 = f, \quad u^{1,0}=0.
		$$
		\State Smoothing and restriction from fine to coarse level (nested)
		\For{$\ell = 1:J$}
		\State Pre-smoothing:
		\For{$i = 1:\nu_\ell$}
		\State
		\begin{equation}\label{eq:smoothing}
		u^{\ell,i} = u^{\ell,i-1} + K_S \ast (f^\ell - K_A \ast u^{\ell,i-1}).
		\end{equation}
		\EndFor
		\State Form restricted residual and set initial guess:
		$$
		u^{\ell+1,0} = 0, \quad f^{\ell+1} = K_R \ast_2 (f^\ell -  K_A \ast u^{\ell,\nu_\ell}).
		$$
		\EndFor
	\end{algorithmic}
\end{breakablealgorithm}
Here $K_S$ can be chosen as $K_{S_0}$ or $K_{S_1}$ as definition in \eqref{eq:kernel-S} and \eqref{eq:kernel-S2}.

Using the above algorithm, there are different multigrid algorithms such as: $\backslash$-cycle, V-cycle and W-cycle.
Let us now only give one special form of multigrid algorithm for solving \eqref{laplace} or \eqref{laplace-h} as follows.
\begin{breakablealgorithm}%[!htb]
	\caption{$u = {\backslash\text{-MG}}(f; J,\nu_1, \cdots, \nu_J)$}
	\label{alg:L-Slash1}
	\begin{algorithmic}
		\State Call Algorithm \ref{alg:L-Slash0},
		$$
		(u^{\ell,\nu_\ell}: ~\ell = 1:J) = {\text{MG0}}(f; J,\nu_1, \cdots, \nu_J).
		$$
		\State Prolongation and restriction from coarse to fine level
		\For{$\ell = J-1:1$}
		\State
		$$
		u^{\ell,\nu_\ell} \leftarrow u^{\ell,\nu_\ell} + K_R  \ast_2^{\top} u^{\ell+1, \nu_{\ell+1}}.
		$$
%		%		\IF{V-cycle}
%		\For{$i = 1:\nu_\ell$}
%		\State
%		$$
%		u^{\ell,i} \leftarrow u^{\ell,i-1} + [B^{\ell,i}]^T (f^\ell - A^{\ell} u^{\ell,i-1})
%		$$
%		\EndFor
%		%		\ENDIF
		\EndFor
		\State Output
		$$
		u = u^{1,\nu_\ell}.
		$$
	\end{algorithmic}
\end{breakablealgorithm}


%&LaTeX

\section{Signals in the Computer}

In this lab, you will use JDSP to explore how a physical signal can
be considered to be composed of a sum of sinusoids --- its
\emph{Fourier series}. You will then investigate how capturing this
signal for computer use --- sampling and quantization --- modifies the
signal. Finally, you will see how the choices you make in the
parameters for sampling and quantization affect the quality of the
digitized, computer signal.

\subsection{Fourier series representation of a physical signal}
	In this section, you will use the  \block{Cont Sig} block in J-DSP to simulate a analog signal
	generator. The \block{Cont
	Sig} block is capable of simulating analog signals of varying frequency content.

	% Block diagram, step 1
	\begin{figure}[b]
	  \begin{center}
	    \includegraphics[height=1.5in]{lab3/block_diagram_step1}
	  \end{center}
	\caption{ A J-DSP diagram for summing and plotting analog sine waves (left) and a single
	triangle wave (right).
	\label{fg:step1}}
	\end{figure}

\paragraph{Step 1.1} Create a J-DSP diagram that sums together four continuous time sinusoids
	and plots their sum. Use the ``Analog Blocks'' function set and the \block{Adder} block only (See
	Figure \ref{fg:step1}). Also create a separate set of blocks that plots a continuous time triangle
	wave at a \option{frequency} of 200 Hz and \option{amplitude} of 1. In the next section you will
	simulate this triangle wave using the sum of sinusoids.

\paragraph{Step 1.2} Recall that any periodic signal can be represented as a sum of harmonic
	sinusoids. The amplitude of each harmonic is known as the Fourier Series. It may at first seem
	like sums of sinusoids would be poor approximations of real periodic signals, but this is not
	the case. We
 	can illustrate this using a triangle wave. The formula for synthesis of a triangle wave with
	frequency $\omega_0$ is a sum of harmonically related sine waves (its Fourier series):
	\[
	x(t) = \sum_{k=0}^{\infty}
	\left(
	\underbrace{ \frac{8}{\pi^2} \frac{(-1)^k}{(2k+ 1)^2} }_{ \text{amplitude} }
	\underbrace{ \sin((2k+1)\omega_0 t) }_{ (2k+1)^{th}\text{ harmonic} }
	\right)
	\]
	In this case, in the analog domain, we are dealing with frequencies in
	Hz, and so $\omega_0 = 2\pi f_0$. Notice that the Fourier Series of the triangle wave only uses
	odd harmonics (i.e., the only non-zero frequencies are $(2k+1)\omega_0=\omega_0, 3\omega_0,
	5\omega_0 \cdots$). Also notice that resulting wave will be zero mean because there is no ``DC''
	term (i.e., $2k+1 \neq 0$ for any integer k).
	Use your diagram from Step 1.1 to generate and sum the
	first 7 harmonics of the Fourier Series of a triangle wave and plot the resultant signal (i.e., use
	$f_0$, $2f_0$, $3f_0$, $\cdots 7f_0$, where $f_0=$200 Hz). How does this signal compare to
	the triangle wave computed directly in \block{Cont. Sig}?

\paragraph{Step 1.3} Another way to view a signal is in the \emph{frequency domain}. For a
	signal expressed in terms of its Fourier Series, the \emph{frequency domain} representation
	is merely the coefficients of the harmonics. Use your favorite spreadsheet or plotting tool to
	compute and plot
	the spectrum of a triangle wave.  Note that you are \emph{not} being asked to plot the triangle
	wave as a function of time; you should plot the amplitudes of the Fourier Series as a
	function of the harmonics' frequencies (like the vertical lines in textbook figure~1.12).

\subsection{Sampling}

The first step in digitization is \emph{sample and hold}, in which the
continuous analog signal is converted to a \textit{discrete-time}
analog signal (an analog signal that only changes its value at
particular points in time). You will use the \block{Sample-Hold} block
in J-DSP to simulate this.

% Block diagram, step 2
\begin{figure}[h]
  \begin{center}
    \includegraphics[height=1.5in]{lab3/block_diagram_step2}
  \end{center}
  \caption{ A J-DSP diagram for investigating sampling rate. \label{fg:step2}}
\end{figure}

\paragraph{Step 2.1} Create an \block{Cont Sig} sine waveform ranging
from -5 to 5V with a frequency of 200Hz (Figure \ref{fg:step2},
leftmost block).

\paragraph{Step 2.2} Use four \block{Sample-Hold} blocks to sample
this signal at 300Hz, 500Hz, 1000Hz and 2000Hz. Plot the original and
all four sampled signals separately (Figure \ref{fg:step2}). Clearly,
the results are not the same, and none look identical to the original
sine wave. What are the two essential pieces of information about a
sine wave that need to be preserved when sampling it?  Does it appear
that all sampled versions are equally useful in achieving this? Why or
why not (in other words, your answer to this question should not be
just ``yes'' or ``no'')?

\subsection{Analog to Digital Conversion}

	The last step of digitization is called ``analog to digital conversion,'' or \emph{quantization}. In
	this step, the sampled analog signal is converted to a discrete signal, with values represented
	by $b$ bit integers. We will use the \block{Quantizer} block under the ``Statistical DSP'' function
	set to perform this conversion.

	% Block diagram, step 3
	\begin{figure}[h]
	  \begin{center}
	    \includegraphics[width=5.0in]{lab3/block_diagram_step3}
	  \end{center}
	\caption{ A J-DSP diagram for investigating quantization error and calculating SNR.
	\label{fg:step3}}
	\end{figure}

\paragraph{Step 3.1} Next we will be using the \block{SNR} block under the ``Basic Blocks''
	function set to compute signal-to-noise ratio (SNR) as a result of quantization. Place the
	\block{SNR} block and open the dialog associated with it. You will see the equations used to
	calculate the numerator and denominator. In your own words, what quantity is calculated in the
	numerator and what quantity is calculated in the denominator of this ratio (Hint: can you
	describe the values in terms of \emph{root mean square} (RMS) values)? Note that this SNR is
	different than what we did in the textbook, because we are now doing the computation for a
	\textit{specific} signal, not just figuring SNR for a possible \textit{range} of signal values.

\paragraph{Step 3.2} Use the J-DSP diagram in Figure \ref{fg:step3} to compute the SNR for a
	quantized sinusoid. Use the \block{SigGen} block with a ``amplitude'' of 5, ``frequency'' of 0.05$
	\pi$, and ``Pulsewidth'' of 256. Use 2, 4, 8, 12, and 16 bits quantization. Use your favorite
	spreadsheet or plotting software to plot SNR versus number of quantization bits (i.e., a scatter
	plot). Use the output of \block{SigGen} as reference for the \block{SNR} block.

\paragraph{Step 3.3} Plot the quantization error by using the \block{Adder} block (bottom right
	blocks of Figure \ref{fg:step3}). Set the \block{Adder} block to ``subtract'' by opening the dialog
	and switching the ``$+$'' sign to ``$-$.'' You only need to include the quantization error plot for
	``4 bits'' in your report (but be sure to view the error plots for all quantization levels).

\paragraph{Step 3.4} Repeat Step 3.2 using a triangle waveform with ``Gain'' of 5 and
	``Pulsewidth'' of 256.

\paragraph{Step 3.5} As you double the number of bits used in quantization, how does the SNR
	change? Refer to specific features of your plots from Steps~3.2--3.4 to justify your answer.

% LocalWords:  WebQ MATLAB


 \documentclass{article}
\usepackage[margin=1in]{geometry}
\setlength{\parindent}{0in}

\usepackage[utf8]{inputenc}

\usepackage{latexsym,amsfonts,amssymb,amsthm,amsmath}
\usepackage {tikz}
\usetikzlibrary {positioning}
\usetikzlibrary{quantikz}

\usepackage{braket}

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

\title{Quantum Computing - Assignment 1}
\author{Kishlaya Jaiswal}

\begin{document}

\maketitle

\subsection*{Exercise 1}
\begin{proof}
Observe that
$$\norm{ \ket{\psi} }^2 = \braket{\psi|\psi} = \big(\ket{\psi}\big)^{\dag} \big(\ket{\psi}\big)$$
$U$ is unitary that is $U^{\dag} U = I$, and hence
$$\norm{ U\ket{\psi} }^2 = \big(U\ket{\psi}\big)^{\dag} \big(U\ket{\psi}\big) = \bra{\psi} U^\dag U \ket{\psi} = \bra{\psi} I \ket{\psi} = \braket{\psi|\psi} = \norm{ \ket{\psi} }^2$$

Since $\norm{.} \geq 0 \implies \norm{ U\ket{\psi} } = \norm{ \ket{\psi} }$
\end{proof}

\subsection*{Exercise 2}
\begin{proof}
\begin{align*}
    [X,Z] \ket{0} &= (XZ-ZX) \ket{0} = X\ket{0} - Z\ket{1} = \ket{1} + \ket{1} = 2 \ket{1} \\
    [X,Z] \ket{1} &= (XZ-ZX) \ket{1} = -X\ket{1} - Z\ket{0} = -\ket{0} - \ket{0} = -2 \ket{0}
\end{align*}

Hence $[X,Z] = \begin{pmatrix} 0 & -2 \\ 2 & 0\end{pmatrix}$
\end{proof}

\subsection*{Exercise 3}
\begin{proof}
$$X = \begin{pmatrix}0 & 1 \\ 1 & 0\end{pmatrix} \implies X^\dag = \begin{pmatrix}0 & 1 \\ 1 & 0\end{pmatrix} = X \text{ and } X^\dag X = \begin{pmatrix}0 & 1 \\ 1 & 0\end{pmatrix}\begin{pmatrix}0 & 1 \\ 1 & 0\end{pmatrix} = \begin{pmatrix}1 & 0 \\ 0 & 1\end{pmatrix} = I$$
$$Y = \begin{pmatrix}0 & -i \\ i & 0\end{pmatrix} \implies Y^\dag = \begin{pmatrix}0 & -i \\ i & 0\end{pmatrix} = Y \text{ and } Y^\dag Y = \begin{pmatrix}0 & -i \\ i & 0\end{pmatrix}\begin{pmatrix}0 & -i \\ i & 0\end{pmatrix} = \begin{pmatrix}1 & 0 \\ 0 & 1\end{pmatrix} = I$$
$$Z = \begin{pmatrix}1 & 0 \\ 0 & -1\end{pmatrix} \implies Z^\dag = \begin{pmatrix}1 & 0 \\ 0 & -1\end{pmatrix} = Z \text{ and } Z^\dag Z = \begin{pmatrix}1 & 0 \\ 0 & -1\end{pmatrix}\begin{pmatrix}1 & 0 \\ 0 & -1\end{pmatrix} = \begin{pmatrix}1 & 0 \\ 0 & 1\end{pmatrix} = I$$

Thus Pauli matrices are Hermitian and Unitary. And,

% Suppose $A$ is any Hermitian matrix, then for any non-zero eigenvector $v$ with eigenvalue $\lambda$:
% $$\bar\lambda \braket{v, v} = \braket{\lambda v, v} = \braket{Av, v} = \braket{v, A^\dag v} = \braket{v, Av} = \braket{v, \lambda v} = \lambda \braket{v, v}$$
% So, eigenvalues of Hermitian matrices are real.

% Suppose $A$ is any Unitary matrix, then for any non-zero eigenvector $v$ with eigenvalue $\lambda$:
% $$\bar \lambda \braket{v, Av} = \braket{\lambda v, Av} = \braket{Av, Av} = \braket{v, A^\dag A v} = \braket{v, v}$$

% But $\bar \lambda \braket{v, Av} = \lambda\bar \lambda \braket{v, v}$ and hence $|\lambda|^2 = 1$. So eigenvalues of Unitary matrices lie on the unit circle.

% Since the only real numbers satisfying $|\lambda|^2 = 1$ are $\pm 1$, so only possible eigenvalues of Pauli matrices are $\pm 1$ and in particular

$$X\begin{pmatrix}1/\sqrt{2} \\ 1/\sqrt{2}\end{pmatrix} = \begin{pmatrix}1/\sqrt{2} \\ 1/\sqrt{2}\end{pmatrix}, X\begin{pmatrix}1/\sqrt{2} \\ -1/\sqrt{2}\end{pmatrix} = - \begin{pmatrix}1/\sqrt{2} \\ -1/\sqrt{2}\end{pmatrix}$$

$$Y\begin{pmatrix}1/\sqrt{2} \\ i/\sqrt{2}\end{pmatrix} = \begin{pmatrix}1/\sqrt{2} \\ i/\sqrt{2}\end{pmatrix}, Y\begin{pmatrix}1/\sqrt{2} \\ -i/\sqrt{2}\end{pmatrix} = -\begin{pmatrix}1/\sqrt{2} \\ -i/\sqrt{2}\end{pmatrix}$$

$$Z\begin{pmatrix}1 \\ 0\end{pmatrix} = \begin{pmatrix}1 \\ 0\end{pmatrix}, Z\begin{pmatrix}0 \\ 1\end{pmatrix} = -\begin{pmatrix}0 \\ 1\end{pmatrix}$$
\end{proof}

Thus the eigenvalues are of Pauli matrices are $\pm 1$.

\subsection*{Exercise 4}
\begin{proof}
\begin{align*}
  HXH \ket{0} &= HX \ket{+} = H \ket{+} = \ket{0}  \\
  HXH \ket{1} &= HX \ket{-} = H (-\ket{-}) = -\ket{1}
\end{align*}
And hence $HXH = Z$

\begin{align*}
  HZH \ket{0} &= HZ \ket{+} = H \ket{-} = \ket{1} \\
  HZH \ket{1} &= HZ \ket{-} = H \ket{+} = \ket{0}
\end{align*}
And hence $HZH = X$
\end{proof}

\subsection*{Exercise 5}
\begin{proof}
From the above exercise $3$, we know that $\ket{+}$ is an eigenvector of $X$ with eigenvalue $1$ and $\ket{-}$ is an eigenvector of $X$ with eigenvalue $-1$, that is $X = \ket{+}\bra{+} - \ket{-}\bra{-}$

Hence $\{\ket+ \bra+, \ket- \bra-\}$ is an eigenbasis for $X$ and so $\ket+ \bra+$ and $\ket- \bra-$ are the measurement operators corresponding to a measurement of $X$ observable.
\end{proof}

\subsection*{Exercise 6}
\begin{proof}
First we check that $\frac{1}{\sqrt{2}}\big(\ket{00} + \ket{11}\big) = \frac{1}{\sqrt{2}}\big(\ket{++} + \ket{--}\big)$ indeed.

\begin{align*}
    \ket{++} + \ket{--} &= \frac{1}{2}(\ket0 + \ket1)(\ket0 + \ket 1) + \frac{1}{2}(\ket0 - \ket1)(\ket0 - \ket 1) \\
    &= \frac12 (\ket{00} + \ket{01} + \ket{10} + \ket{11} + \ket{00} - \ket{01} - \ket{10} + \ket{11}) = \ket{00} + \ket{11}
\end{align*}

So it suffices to show that $\frac{1}{\sqrt{2}}\big( \ket{00} + \ket{11}\big)$ is an entangled state. \\

Suppose not and so it can be written as $\ket{\psi} \otimes \ket{\phi}$ where $\ket{\psi} = \alpha \ket0 + \beta \ket1$ and $\ket\phi = \gamma \ket0 + \delta \ket1$.

Then $\ket{\psi} \otimes \ket{\phi} = \alpha \gamma \ket{00} + \alpha \delta \ket{01} + \beta \gamma \ket{10} + \beta \delta \ket{11} = \frac{1}{\sqrt{2}}\big(\ket{00} + \ket{11}\big)$ \\

Since $\{\ket{00}, \ket{01}, \ket{10}, \ket{11}\}$ is a linearly independent set in $\mathbb{C}^4$, we get $\alpha \delta = \beta \gamma = 0$ and $\alpha \gamma = \beta \delta \neq 0$ whose solution doesn't exist. Hence $\frac{1}{\sqrt{2}}\big(\ket{00} + \ket{11}\big)$ is an entangled state.
\end{proof}

\subsection*{Exercise 7}
\begin{center}
\begin{quantikz}
\lstick{$\ket{0}$} & \gate{H} & \ctrl{1} & \qw \rstick[wires=2]{$\ket\phi$} \\
\lstick{$\ket{0}$} & \qw & \targ{} & \qw
\end{quantikz}
\end{center}

\begin{proof}
We start with $\ket\psi = \ket{00}$ state.\\

Applying $H \otimes I$ to $\ket\psi$, we get $\frac{1}{\sqrt2}\big(\ket0 + \ket1\big)\ket0 = \frac{1}{\sqrt2}\big(\ket{00} + \ket{10}\big)$\\

Applying controlled-NOT gate (where first qubit is the control and second qubit is target) to this, we finally get $\ket\phi = \frac{1}{\sqrt2}\big(\ket{00} + \ket{11}\big)$ as required.
\end{proof}

\end{document}


\section{Dual Averaging}
In this section, we introduce Nesterov's method of dual averaging. In order to best explain where this method comes from,
we first start with a different perspective on subgradient descent. We note that the subgradient descent iteration
(\ref{subgradient_descent_variable}) (with $x_1 = 0$) can also be written as
\begin{equation}\label{minimizing_subgradient_descent}
 x_{n+1} = \arg\min_x \left(\displaystyle\sum_{i = 1}^n \langle s_ig_i, x\rangle + \frac{1}{2}\|x\|_2^2\right)
\end{equation}

This can easily be proven by induction on $n$ and we leave this as an exercise. The above minimization problem
can be rewritten as
\begin{equation}
 x_{n+1} = \arg\min_x \left(\displaystyle\sum_{i = 1}^ns_i(f(x_i) + \langle g_i, x - x_i\rangle) + \frac{1}{2}\|x\|_2^2\right)
\end{equation}
and we note that the terms $f(x_i) + \langle g_i, x - x_i\rangle$ are lower bounds on the objective $f$. Thus, the next iterate
can be obtained by optimizing an average of these lower bounds (with weights $s_i$) plus a regularization term which is a multiple
of $\|x\|_2^2$.

In the previous section we set $s_i = \frac{1}{\sqrt{i}}$. This means that we are giving more weight to the bounds coming from
earlier iterates, which is odd. The starting point for Nesterov's dual averaging method is to ask whether we can change the
weights $s_i$. It turns out that we can, but in order to do this we must weight the regularization term differently as well.

We thus modify (\ref{minimizing_subgradient_descent}) and consider methods of the form
\begin{equation}\label{generalized_dual_averaging}
 x_{n+1} = \arg\min_x \left(\displaystyle\sum_{i = 1}^n \langle s_ig_i, x\rangle + \frac{\alpha_{n+1}}{2}\|x\|_2^2\right)
\end{equation}
where the sequences $s_i,\alpha_i > 0$ are parameters (and $g_i\in \partial f(x_i)$ as before).

We obtain the following convergence result for the dual averaging method as a consequence of the definition (\ref{generalized_dual_averaging}).
\begin{theorem}\label{dual_averaging_theorem}
 Assume that $f$ is convex and Lipschitz with constant $M$, i.e. $\|g\|_2\leq M$ for $g\in \partial f(x)$. Let
 $x^*\in \arg\min_x f(x)$.

 Then if the sequence $x_i$ is given by (\ref{generalized_dual_averaging}) (note in particular that this means that
 $x_1 = 0$) with $\alpha_i \leq \alpha_{i+1}$, we have
 \begin{equation}
  f(\bar{x}_n) - f(x^*) \leq \frac{1}{2}\left(\displaystyle\sum_{i = 1}^n s_i\right)^{-1} \left(\alpha_n\|x^*\|_2^2 + M\displaystyle\sum_{i = 1}^n \frac{s_i^2}{\alpha_i}\right)
 \end{equation}
 where $\bar{x}_n = (\sum_{i = 1}^n s_i)^{-1}\sum_{i = 1}^n s_ix_i$ is a weighted average of the iterates with weights $s_i$.
 We also have
 \begin{equation}
  \min_{i = 1,...,n} f(x_i) - f(x^*) \leq \frac{1}{2}\left(\displaystyle\sum_{i = 1}^n s_i\right)^{-1} \left(\alpha_n\|x^*\|_2^2 + M\displaystyle\sum_{i = 1}^n \frac{s_i^2}{\alpha_i}\right)
 \end{equation}

\end{theorem}
\begin{proof}
 We begin by noting that by convexity we have
 \begin{equation}
  f(\bar{x}_n) - f(x^*) \leq \left(\displaystyle\sum_{i = 1}^n s_i\right)^{-1}\displaystyle\sum_{i = 1}^n s_i(f(x_i) - f(x^*))
 \end{equation}
 and since the minimum is smaller than the average we have
 \begin{equation}
  \min_{i = 1,...,n} f(x_i) - f(x^*) \leq \left(\displaystyle\sum_{i = 1}^n s_i\right)^{-1}\displaystyle\sum_{i = 1}^n s_i(f(x_i) - f(x^*))
 \end{equation}
 Thus it suffices to prove that
 \begin{equation}
  \displaystyle\sum_{i = 1}^n s_i(f(x_i) - f(x^*)) \leq \frac{1}{2}\left(\alpha_n\|x^*\|_2^2 + M\displaystyle\sum_{i = 1}^n \frac{s_i^2}{\alpha_i}\right)
 \end{equation}
 For this we first use the subgradient property to get
 \begin{equation}\label{eq_dual_averaging_59}
  \displaystyle\sum_{i = 1}^n s_i(f(x_i) - f(x^*)) \leq \displaystyle\sum_{i = 1}^n \langle s_ig_i, x_i - x^*\rangle
 \end{equation}
 and rewrite this sum as follows
 \begin{equation}\label{eq_dual_averaging_63}
  \displaystyle\sum_{i = 1}^n \langle s_ig_i, x_i - x^*\rangle = \displaystyle\sum_{i = 1}^n \langle s_ig_i, x_n - x^*\rangle
  + \displaystyle\sum_{i = 1}^{n-1} \displaystyle\sum_{j = 1}^i\langle s_jg_j, x_i - x_{i+1}\rangle
 \end{equation}
 We proceed by bounding the term
 \begin{equation}
  \sum_{j = 1}^i\langle s_jg_j, x_i - z\rangle
 \end{equation}
 Recall from (\ref{generalized_dual_averaging}) that $x_i = \arg\min_x f_i(x)$ where
 \begin{equation}
  f_i(x) = \displaystyle\sum_{j = 1}^{i-1} \langle s_jg_j, x\rangle + \frac{\alpha_{i}}{2}\|x\|_2^2
 \end{equation}
 This means that $0\in\partial f_i(x_i)$ and thus, defining
 $$f_i^\prime(x) = f_i(x) + \langle s_ig_i, x\rangle$$
 we see that $s_ig_i\in \partial f_i^\prime(x_i)$. Moreover, $f_i^\prime$ is strongly convex with convexity parameter
 $\alpha_i$, which implies that
 $$f_i^\prime(x_i) \leq f_i^\prime(z) + \frac{1}{2\alpha_i}\|s_ig_i\|_2^2
 $$
 Using the definition of $f_i^\prime$ and rearranging, this gives
 \begin{equation}
  \sum_{j = 1}^i\langle s_jg_j, x_i - z\rangle \leq \frac{\alpha_i}{2}(\|z\|_2^2 - \|x_i\|_2^2) + \frac{s_i^2}{2\alpha_i}\|g_i\|_2^2
 \end{equation}
 Pluggin this into (\ref{eq_dual_averaging_63}) with $z = x_{i+1}$ and $z = x^*$, we obtain (recalling that $x_1 = 0$ and
 $\|g_i\|_2 \leq M$)
 \begin{equation}
  \displaystyle\sum_{i = 1}^n \langle s_ig_i, x_i - x^*\rangle \leq \frac{\alpha_n}{2}\|x^*\|_2^2
  + \frac{1}{2}\displaystyle\sum_{i = 2}^n (\alpha_{i-1} - \alpha_i)\|x_i\|_2^2 + \frac{M}{2}\displaystyle\sum_{i = 2}^n \frac{s_i^2}{\alpha_i}
 \end{equation}
 Now we use the assumption that $\alpha_i$ is an non-decreasing sequence, i.e. $$\alpha_{i-1} - \alpha_i \leq 0$$ to see that
 the middle sum above is negative. Combining this with (\ref{eq_dual_averaging_59}) we see that
 \begin{equation}
  \displaystyle\sum_{i = 1}^n s_i(f(x_i) - f(x^*)) \leq \frac{1}{2}\left(\alpha_n\|x^*\|_2^2 + M\displaystyle\sum_{i = 1}^n \frac{s_i^2}{\alpha_i}\right)
 \end{equation}
 which completes the proof.

\end{proof}

We conclude this section by noting several consequences of this theorem. First, the choice $s_i = \frac{1}{\sqrt{i}}$
and $\alpha_i = 1$ recovers subgradient descent. For this choice of $s_i$ and $\alpha_i$
$$\displaystyle\sum_{i = 1}^n s_i = O(\sqrt{n})
$$
and
$$\displaystyle\sum_{i = 1}^n \frac{s_i^2}{\alpha_i} = O(\log(n))
$$
and we obtain the bound from Theorem \ref{variable_subgradient_descent_thm}.

We can remove the logarithmic factor by
shifting the weights $s_i$ to the more recent iterates. For example, if we set $s_i = 1$ and $\alpha_i = \sqrt{i}$,
we recover the dual averaging method originally proposed by Nesterov, for which we have
$$\displaystyle\sum_{i = 1}^n s_i = n
$$
and
$$\displaystyle\sum_{i = 1}^n \frac{s_i^2}{\alpha_i} = O(\sqrt{n})
$$
which removes the logarithmic factor we had for subgradient descent.

Other choices are also reasonable, for example we could
set $s_n = n^c$ and $\alpha_n = n^{c + (1/2)}$ for any $c \geq -(1/2)$. As long as $c > -(1/2)$ we obtain the same convergence
rate up to a constant, if $c = (1/2)$, then we recover subgradient descent which introduces a logarithmic factor.

Next, we address the issue of scaling. If we scale the sequences $s_i$ and $\alpha_i$ by the same (positive) factor,
then both the bound in Theorem \ref{dual_averaging_theorem} and the method (\ref{generalized_dual_averaging}) don't change.
However, scaling one sequence and not the other has the effect of changing the `step size' of the method, and
changes the relative importance of the initial distance to the optimum $\|x^*\|_2^2$ and the Lipschitz constant $M$ in the
our bound.

Finally, we address the issue of setting the initial iterate to $0$. This is done solely out of notational convenience. In particular,
if we change the regularization term $\|x\|_2^2$ to $\|x - x_1\|_2^2$, our analysis is unchanged. In fact, we can replace the
$2$-norm regularizer by any strongly convex function, as the reader can easily verify. This is an important point which we
will use in a later section to extend these methods to the Banach space setting.


\input{SingleAssignmentSetup.tex}
\input{../WeekTitles.tex}
\begin{document}

\begin{center}
\subsection*{MNTC P01 - Week \#8 - \WeekTitleEight}
\end{center}

\newcommand{\Fext}{  F_{\mbox{ext}} }
\newcommand{\Fspring}{  F_{\mbox{spring}} }
\newcommand{\Fdamping}{  F_{\mbox{damp}} }

\begin{enumerate}

% ******************************
\item
  \begin{Question}
    Use \verb@ode45@ to generate a graph of the solution to the
    following DEs, over the specified interval, given the initial
    condition.

\begin{enumerate}
\item $\ds \frac{dy}{dt} = t^2 + y^2$, $y(0) = 0$, and $0 \le t \le 1$.
\item $\ds \frac{dy}{dt} = \sin(t) + \cos(y)$, $y(0) = 0$, and $0 \le t \le 10$.
\item $\ds \frac{dy}{dt} = (1-y^2) + 0.2 \sin(t)$, $y(0) = 0$, and $0 \le t \le 20$.
\end{enumerate}
\end{Question}

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

Here are the graphs of the solutions.

\includegraphics[width = 0.3\linewidth]{graphics/Week08_DESolutions/W08DE01_a}
\includegraphics[width = 0.3\linewidth]{graphics/Week08_DESolutions/W08DE01_b}
\includegraphics[width = 0.3\linewidth]{graphics/Week08_DESolutions/W08DE01_c}

\end{Solution}

% ******************** Newton's Cooling  ********************************
\item
\begin{Question}
Newton's law of heating and cooling states that an object with
  temperature $T$ in an environment at temperature $T_{ext}$ will heat
  up or cool down according to the differential equation

 $$\frac{dT}{dt} = -k (T - T_{ext})$$

 Consider a garage used as a workshop.  Its insulation and surface
 area give $k$ a value of 0.1, if time $t$ is measured in hours and
 the temperatures, $T$ and $T_{ext}$, are in degrees Celsius.

The temperature outside changes during the day, as described by
the formula
$$T_{ext} =  10 + 7 \cos\left(\frac{\pi}{12} t\right)$$

We now imagine that the power goes out, with the garage at 23$^o$ C at
$t=0$.

\begin{enumerate}
\item Use ode45 and the DE to generate a numerical prediction of the
  garage's temperature $T$ over time.  Graph the solution over a time
  interval that shows both the initial and long-term behaviour of the
  temperature.

  In your script, try to use the functions \verb#title#,
  \verb#xlabel#, \verb#ylabel#, and \verb#legend# to annotate the
  graph to make it easier for a reader to understand.

  For the following questions, just use the graph or the numerical
  prediction of the temperature. You are {\em not} expected to solve
  the DE analytically.

\item How many days does it take for the garage to get into a
  consistent temperature cycle?  (You will need to estimate this by
  eye.)

\item How many degrees does the temperature in the building fluctuate
  by, once the temperature gets into a steady cycle?

\item Suppose the building were better insulated, so that the rate of
  heat loss were cut in half.  Should $k$ be half as large, or twice
  as large?

\item Generate a numerical prediction for the temperature over time in
  the better-insulated scenario, and produce a graph of the
  temperature vs time for both scenarios on the same axes.

\item How large are the temperature fluctuations in the building, now
  that the extra insulation has been added?  Does halving the net heat
  flow also halve the net temperature fluctuations?
\end{enumerate}

\end{Question}

\begin{Solution}
\begin{enumerate}
\item  A graph of the temperature over time is shown below:

  \includegraphics[width=3in]{graphics/Week08_Spring/W08GarageTemp_1}

The file
  \href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/W08GarageTemp.m}{W08GarageTemp.m}
has the MATLAB code that generated the graph above.

\item From the graph, it takes the building roughly 2 days (48 hours)
  to get into a repeating cycle of temperature variation.

\item Careful zooming of the graph (or a look at the $y$ values in the
  ode45 output) give a highest temperature of 12.5 (high) and 7.5
  (low), for a net fluctuation of approximately 2.6 degrees per day.

\item $k$ represents the coefficient of heat flow between the building
  and the environment. The bigger $k$ is, the {\em larger} the
  headflow between the two.  Since we're adding insulation, this
  should {\em reduce} the heat flow, and so {\em lower} the value of
  $k$.

\item A graph of the heat change over time, given better insulation,
  is shown below.

  \includegraphics[width=3in]{graphics/Week08_Spring/W08GarageTemp_2}

\item Zooming in on the peaks of the {\bf red line} graph (new
  insulation model), the temperature now fluctuates between
  approximately 11.3 and 8.8 degrees Celsius, for a range of 2.5
  degrees.  This {\em is} roughly half the magnitude of the
  fluctuations we saw earlier.

\end{enumerate}
\end{Solution}

\hrulefill

\subsection*{Modelling Spring Systems}
% ******************************
\item  \label{SpringNoDampingNoFext}
  \begin{Question}
    \begin{minipage}[t]{0.6\linewidth}
\vspace{0pt}
Consider the single spring/mass system shown to the right, with no damper.

Newton's second law gives us the relationship:
\begin{align*}
ma & = \sum F = \Fspring \\
m x'' & = -k x
\end{align*}
where $k$ is the spring constant.
    \end{minipage}
    \begin{minipage}[t]{0.3\linewidth}
\vspace{0pt}
\begin{center}
\includegraphics[width=1.0in]{graphics/Week08_Spring/SpringNoDamping}
\end{center}
    \end{minipage}

\begin{enumerate}
\item By hand, write this second order DE as a system of 1st order
  DEs, using the new variables $w_1 = x$ and $w_2 = x'$

\item Write a MATLAB function file called \verb#springDE1.m# starting
  with the first line \\
  \verb#function dw_dt = springDE1(t, w, m, k)# \\
  that implements the system of differential equations from part (a).
\item Write a MATLAB script that simulates the motion of the mass
  using $m = 0.5$ kg and $k = 10$ N/m.  Choose the time interval for
  the simulation so that 4-5 cycles of oscillation are shown.
\end{enumerate}
\end{Question}

\begin{Solution}
 \begin{enumerate}
 \item  The first-order system would be:
 \begin{align*}
 \frac{d}{dt} w_1 & = x' = w_2 \\
 \frac{d}{dt} w_2 & = x'' = \frac{1}{m} \left(-kx \right) = \frac{1}{m} \left(-k w_1  \right)
 \end{align*}

\item The function file
  \href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/springDE1.m}{springDE1.m}
  implements the differential equation system, with the $\Fext $ term
  left out.

\item The main script
\href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/W08SpringSimulation01.m}{W08SpringSimulation01.m}
has the code that will run this simulation.

In the resulting plot, we see a very nice example of simple harmonic
motion.

\begin{center}
\includegraphics[width=3in]{graphics/Week08_Spring/W08SpringSimulation01}
\end{center}

\end{enumerate}
\end{Solution}

% ******************************
\item  \label{SpringNoDampingWithFext}
  \begin{Question}
    \begin{minipage}[t]{0.6\linewidth}
\vspace{0pt}
Consider the single spring/mass system shown to the right, with no damper.

This is the same as in Question \ref{SpringNoDampingNoFext}, except
with the addition of the $\Fext$ shown as an external applied force.

Newton's second law gives us the relationship:
\begin{align*}
ma & = \sum F = \Fspring + \Fext \\
m x'' & = -k x + \Fext
\end{align*}
where $k$ is the spring constant.
    \end{minipage}
    \begin{minipage}[t]{0.3\linewidth}
\vspace{0pt}
\begin{center}
\includegraphics[width=1.0in]{graphics/Week08_Spring/SpringNoDampingWithFext}
\end{center}
    \end{minipage}

\begin{enumerate}
\item By hand, write this second order DE as a system of 1st order
  DEs, using the new variables $w_1 = x$ and $w_2 = x'$

\item We will now incorporate an external force of the form
  $F_{\mbox{ext}} = a \sin(b t)$. Write a MATLAB function file called
  \verb#springDE2.m# starting with the first line \\
  \verb#function dw_dt = springDE2(t, w, m, k, a, b)# \\
  that implements the system of differential equations from part (a).

\item Create a new MATLAB script.  In the script, set $m =0.5$ kg,
  $k = 10$ N/m, and use $a = 5$ and $b = 1$ in
  $F_{\mbox{ext}} = a \sin(bt)$.  Use \verb#ode45# to simulate the
  motion of the spring for 30 seconds (\verb#tspan = [0, 30]#), given
  an initial displacement of $x(0) = 0.2$ m, and initial velocity of
  zero: $x'(0) = 0$. \label{forced}

\item Explain why the motion looks so disorganized.

\item Repeat Question (\ref{forced}), but with an external force of
  $\Fext = \sin(4 t)$. Explain why the motion in this case has cyclic
  waves in its amplitude.

\end{enumerate}
  \end{Question}

\begin{Solution}
 \begin{enumerate}
 \item  The first-order system would be:
 \begin{align*}
 \frac{d}{dt} w_1 & = x' = w_2 \\
 \frac{d}{dt} w_2 & = x'' = \frac{1}{m} \left(-kx + \Fext\right) = \frac{1}{m} \left(-k w_1  + \Fext \right)
 \end{align*}

\item The file
  \href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/springDE2.m}{springDE2.m}
  implements the differential equation system, with new external force
  $\Fext = a \sin(bt)$.
\item The file
\href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/W08SpringSimulation02.m}{W08SpringSimulation02.m}
has the code that will run this simulation.

In the resulting plot, see some wildly varying and irregular
oscillations.

\begin{center}
\includegraphics[width=3in]{graphics/Week08_Spring/W08SpringSimulation02}
\end{center}

\item The motion of the mass looks very disorganized because the
  natural frequency (the frequency at which the mass would oscillate
  if you just let swing on its own) is different from the frequency
  that we are pushing and pulling on it with through $\Fext$.

  Recall: the natural frequency of a spring/mass system is given by
  $\omega = \sqrt{k/m}$, which for this scenario gives $\omega = \sqrt{ \frac{25}{4}}$

\item
The file
  \href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/W08SpringSimulation02.m}{W08SpringSimulation02.m}
  has the code that will run this simulation.  Here is a graph of the
  resulting mass motion.

\begin{center}
\includegraphics[width=3in]{graphics/Week08_Spring/W08SpringSimulation03}
\end{center}

In the plot, we see
  that the natural frequency and the regular stimulation by the
  outside force are close to each other: the natural frequency is
  $\omega = \sqrt{\frac{10}{0.5}} \approx 4.5$, rad/s, and the
  stimulating frequency is at $\omega = 4$ rad/s.  This close match of
  the frequencies leads to the phenonenon called {\em beats}, or {\em
    near resonance}.
\end{enumerate}
\end{Solution}

% ******************************
\item
  \begin{Question}
    \begin{minipage}[t]{0.6\linewidth}
      \vspace{0pt} We return to the same single spring/mass system
      from Question \ref{SpringNoDampingWithFext}, shown to the right,
      with no damper.

The $\Fext$ shown is an external applied force.
    \end{minipage}
    \begin{minipage}[t]{0.3\linewidth}
\vspace{0pt}
\begin{center}
\includegraphics[width=1.0in]{graphics/Week08_Spring/SpringNoDampingWithFext}
\end{center}
    \end{minipage}

\begin{enumerate}
\item For a mass of $m = 5$ kg , and a spring constant of $k = 2$ N/m,
  what is the natural frequency of the system?
\item Define an external force of the form $\Fext = \sin(b t)$ that will
produce {\bf resonance} in the system.
\item Use MATLAB to simulate the motion of the spring, with your
  selected external force, using an initial condition where the mass
  starts at its equilibrium and at rest.
\item The system will break if the oscillations become too large,
  specifically if $x(t)$ exceeds 2 m (in the positive or negative
  directions).  Does the system break, and if so, how long does it
  take for the system to break?
\end{enumerate}
\end{Question}

\begin{Solution}
  \begin{enumerate}[(a)]
  \item The natural frequency of a spring/mass system is given by
    $\omega = \sqrt{\frac{k}{m}} = \sqrt{\frac{2}{5}} \approx 0.6325$
    rad/s.
  \item If we want to produce resonance in the oscillations, our
    applied force's frequency must be exactly at the same frequency as
    the natural frequency. That way, the natural oscillations and the
    applied force are perfectly synchronized, leading to a build-up of
    the energy in the system.

    This means we should select $\Fext = sin( 0.6325 t)$.

  \item For this problem, we can recycle the differential equation
    code in
    \href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/springDE2.m}{springDE2.m}
    from Question \ref{SpringNoDampingWithFext}, because the forces
    (spring and $\Fext$) are in the same form as in that problem.

    In our new main script,
    \href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/W08Resonance1.m}{W08Resonance1.m}
    we will set up all the constants needed for the simulation:
    \begin{itemize}
    \item $m = 5$, $k = 2$,
    \item $a = 1$ and $b= 0.6325$ to define
      $\Fext = a \sin(bt) = 1 \cdot \sin(0.6325 t)$.
    \end{itemize}

    Below is a graph of the simulated motion of the mass over 30
    seconds:
\begin{center}
\includegraphics[width=0.4\linewidth]{graphics/Week08_Spring/W08SpringResonance1}
\end{center}

\item From the graph, we see that the amplitude of the oscillations
  are growing with every cycle, as expected when we induce resonance.  This means that the system {\em will } break at some point.

  The specific limit we were given was that breakage will occur if
  $x(t)$ exceeds 2 m. Zooming in, we can see that the system will
  reach $x(t) \approx 2$ m around $t = 14$ seconds.

\begin{center}
\includegraphics[width=0.4\linewidth]{graphics/Week08_Spring/W08Resonance1_BreakPoint}
\end{center}

  \end{enumerate}
\end{Solution}

% ******************************
\item
  \begin{Question}
    \begin{minipage}[t]{0.6\linewidth}
      \vspace{0pt} Consider the damped system shown at right.

      The damping force exerted by the dashpot/damper is proportional
      to the velocity of the mass.

Newton's second law gives us the relationship:
\begin{align*}
ma & = \sum F = \Fspring + \Fdamping \\
m x'' & = -k x  - c x'
\end{align*}
where $k$ is the spring constant in N/m, and $c$ is the damping
coefficient in N/(m/s).

    \end{minipage}
    \begin{minipage}[t]{0.3\linewidth}
\vspace{0pt}
\begin{center}
\includegraphics[width=0.9\linewidth]{graphics/Week08_Spring/W08DampedSpringNoFext}
\end{center}
    \end{minipage}

\begin{enumerate}[(a)]
\item By hand, write this second order DE as a system of 1st order
  DEs, using the new variables $w_1 = x$ and $w_2 = x'$

\item We define a system with a mass of $m = 10$ kg, spring constant
  $k = 2$ N/m, and a damping coefficient of $c = 0.4$ N/(m/s).  If the
  system is displaced by 0.5 m and then let go with zero initial
  velocity, use MATLAB to find out how long (in both seconds and
  cycles) it takes for the oscillations to reach approximately 10\% of
  their original amplitude.  Note: you will need to write both a
  MATLAB function for the differential equation, and a main script to
  run the simulation.
\item What damping coefficient would be needed for the oscillations to
  be reduced to 10\% of their original amplitude within 3 cycles?  You
  will need to estimate your answer based on guessing and checking
  against the graph.  Hint: add horizontal lines to the solution plot
  at $x = 0.05$ and $x = -0.05$ to see easily whether the oscillations
  are reduced to that level.
\end{enumerate}
\end{Question}

\begin{Solution}
  \begin{enumerate}[(a)]
 \item  The first-order system would be:
 \begin{align*}
 \frac{d}{dt} w_1 & = x' = w_2 \\
 \frac{d}{dt} w_2 & = x'' = \frac{1}{m} \left(-kx -c x'\right) = \frac{1}{m} \left(-k w_1  - c w_2\right)
 \end{align*}

\item To simulate the motion of the spring/mass system, we need a
  function file with the differential equation, which we called
  \href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/springDEDamped.m}{springDEDamped.m},
  and a main script, which we called
  \href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/W08DampedSpringSystem.m}{W08DampedSpringSystem.m}.

  Here is the graph of the resulting motion, showing a nice
  oscillatory pattern, but the amplitude diminishing over time due to
  the damping.

\begin{center}
\includegraphics[width=3in]{graphics/Week08_Spring/W08DampedSpring1A}
\end{center}

seconds, or between 8 and 9 cycles.

\begin{center}
\includegraphics[width=3in]{graphics/Week08_Spring/W08DampedSpring1B}
\end{center}

\item Using $c = 1.1$ N/(m/s) gives the required damping, putting the amplitude
of the oscillations below 10\% of their original magnitude after 3 cycles.

\begin{center}
\includegraphics[width=3in]{graphics/Week08_Spring/W08DampedSpring1C}
\end{center}

  \end{enumerate}
\end{Solution}

% %****************
\item
\begin{Question}
  For a spring/mass system with $m=1$ kg, $c=0.5$ N/(m/s), and $k=45$,
  approximately what frequency of external forcing would produce the
  largest amplitude steady-state vibration?  Give your answer to the
  nearest 0.5 rad/s.

  You will need to estimate the answer based on guessing and checking
  against the graph.
 \end{Question}

 \begin{Solution}
   The natural frequency is going to be near
   $\omega = \sqrt{k/m} \approx 6.7$ rad/s.

   To run the simulation in MATLAB, we know we need to build the
   differential equation based on the sum of the forces in MATLAB.  $ma = \sum F$ gives us
$$ m x'' = -kx - c x' + \sin(bt)$$
where $b$ is the frequency we can experiment with to maximize the
steady-state oscillations.

The first-order system version of this is:
 \begin{align*}
 \frac{d}{dt} w_1 & = x' = w_2 \\
 \frac{d}{dt} w_2 & = x'' =  \frac{1}{m} \left(-kx -c x'+ \sin(bt) \right) \\
& = \frac{1}{m} \left(-k w_1  - c w_2 + \sin(bt) \right)
 \end{align*}

 We built this system of differential equations into the file
 \href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/springDEDampedAndForced.m}{springDEDampedAndForced.m}
and the main script
 \href{http://www.mast.queensu.ca/~apsc171/MNTCP01/PracticeProblems/MATLAB/W08DampedSpringSystem3.m}{W08DampedSpringSystem3.m}

 If we are only interested in the steady-state behaviour, then the
 initial conditions won't matter (their influence fades in the long
 run). We make the easiest choice, which is the mass starting at
 equilibrium at rest, $x(0) = 0$ and $x'(0) = 0$.

 Below is a graph showing the response of the system to
 $b = 5, 5.5, 6, 6.5$ and 7 rad/s, and then the graph for $b = 6.5$
 rad/s only.

\begin{center}
\includegraphics[height=2.6in]{graphics/Week08_Spring/W08DampedAndForced1}
\includegraphics[height=2.6in]{graphics/Week08_Spring/W08DampedAndForced2}
\end{center}

Looking for the steady-state oscillations means looking at the
amplitudes once the graph steadies into a regular repeating pattern.
The graph corresponding to $\Fext = \sin(6.5~t)$ is the graph with the
highest amplitude once that steady-state oscillation pattern is
reached.

 \end{Solution}

\end{enumerate}

\end{document}