\chapter{hydrogen atom solutions}
\begin{abox}
	Practice set 1 solutions
	\end{abox}
\begin{enumerate}
\begin{minipage}{\textwidth}
	\item The energy levels of the non-relativistic electron in a hydrogen atom (i.e. in a Coulomb potential $V(r) \propto-1 / r$ ) are given by $E_{n l m} \propto-1 / n^{2}$, where $n$ is the principal quantum number, and the corresponding wave functions are given by $\psi_{n l m}$, where $l$ is the orbital angular momentum quantum number and $m$ is the magnetic quantum number. The spin of the electron is not considered. Which of the following is a correct statement?
	\exyear{NET JUNE 2011}
\end{minipage}
\begin{tasks}(1)
	\task[\textbf{A.}] There are exactly $(2 l+1)$ different wave functions $\psi_{n l m}$, for each $E_{n l m}$.
	\task[\textbf{B.}]There are $l(l+1)$ different wave functions $\psi_{n l m}$, for each $E_{n l m}$.
	\task[\textbf{C.}] $E_{n l m}$ does not depend on $l$ and $m$ for the Coulomb potential.
	\task[\textbf{D.}]There is a unique wave function $\psi_{n l m}$ for each $E_{n l m}$.
\end{tasks}
\begin{answer}
	The correct option is \textbf{(c)}
\end{answer}
\begin{minipage}{\textwidth}
	\item Let $\psi_{n l m_{l}}$ denote the eigenfunctions of a Hamiltonian for a spherically symmetric potential $V(r)$. The wavefunction $\psi=\frac{1}{4}\left[\psi_{210}+\sqrt{5} \psi_{21-1}+\sqrt{10} \psi_{211}\right]$ is an eigenfunction only of
	\exyear{NET JUNE 2012}
\end{minipage}
\begin{tasks}(2)
	\task[\textbf{A.}] $H, L^{2}$ and $L_{2}$
	\task[\textbf{B.}]$H$ and $L_{z}$
	\task[\textbf{C.}]$H$ and $L^{2}$
	\task[\textbf{D.}]$L^{2}$ and $L_{z}$
\end{tasks}
\begin{answer}
	$H \psi=E_{n} \psi$\\
	$L^{2} \psi=l(l+1) \hbar^{2} \psi$ and $L_{2} \psi \neq m \hbar \psi$.\\
	The correct option is \textbf{c}
\end{answer}
\begin{minipage}{\textwidth}
	\item The wave function of a state of the Hydrogen atom is given by,
	$$
	\psi=\psi_{200}+2 \psi_{211}+3 \psi_{210}+\sqrt{2} \psi_{21-1}
	$$
	where $\psi_{n l m}$ is the normalized eigen function of the state with quantum numbers $n, l, m$ in the usual notation. The expectation value of $L_{z}$ in the state $\psi$ is
	\exyear{NET DEC 2012}
\end{minipage}
\begin{tasks}(2)
	\task[\textbf{A.}] $\frac{15 \hbar}{6}$
	\task[\textbf{B.}]$\frac{11 \hbar}{6}$
	\task[\textbf{C.}]$\frac{3 \hbar}{8}$
	\task[\textbf{D.}]$\frac{\hbar}{8}$
\end{tasks}
\begin{answer}
	Firstly normalize $\psi, \psi=\frac{1}{\sqrt{16}} \psi_{200}+\frac{2}{\sqrt{16}} \psi_{211}+\frac{3}{\sqrt{16}} \psi_{210}+\frac{\sqrt{2}}{\sqrt{16}} \psi_{21-1}$ \\
	$P(0 \hbar)=\frac{1}{16}+\frac{9}{16}=\frac{10}{16} .$\\
	Probability of getting $(1 \hbar)$ i.e. $P(\hbar)=\frac{4}{16}$ and $P(-\hbar)=\frac{2}{16}$.\\
	Now, $\left\langle L_{z}\right\rangle=\frac{\left\langle\psi\left|L_{z}\right| \psi\right\rangle}{\langle\psi \mid \psi\rangle}=0 \hbar \times \frac{10}{16}+1 \hbar \times \frac{4}{16}+(-1 \hbar) \times \frac{2}{16}=\frac{4}{16} \hbar-\frac{2}{16} \hbar=\frac{2}{16} \hbar=\frac{\hbar}{8}$\\
	The correct option is \textbf{(d)}
\end{answer}
\begin{minipage}{\textwidth}
	\item Let $\psi_{n l m}$ denote the eigenfunctions of a Hamiltonian for a spherically symmetric potential $V(r)$. The expectation value of $L_{z}$ in the state
	$$\psi=\frac{1}{6}\left[\psi_{200}+\sqrt{5} \psi_{210}+\sqrt{10} \psi_{21-1}+\sqrt{20} \psi_{211}\right] \text { is }$$
	\exyear{NET DEC 2013}
\end{minipage}
\begin{tasks}(2)
	\task[\textbf{A.}] $-\frac{5}{18} \hbar$
	\task[\textbf{B.}]$\frac{5}{6} \hbar$
	\task[\textbf{C.}]$\hbar$
	\task[\textbf{D.}] $\frac{5}{18} \hbar$
\end{tasks}
\begin{answer}
	$$\left\langle L_{z}\right\rangle=\left\langle\psi\left|L_{z}\right| \psi\right\rangle=\frac{1}{36} \times 0 \hbar+\frac{5}{36} \times 0 \hbar+\frac{10}{36} \times(-1 \hbar)+\frac{20}{36}(1 \hbar)=\frac{10}{36} \hbar=\frac{5}{18} \hbar \quad \because\langle\psi \mid \psi\rangle=1$$\\
	The correct option \textbf{(d)}
\end{answer}
\begin{minipage}{\textwidth}
	\item An electron is in the ground state of a hydrogen atom. The probability that it is within the Bohr radius is approximately equal to
	\exyear{NET JUNE 2014}
\end{minipage}
\begin{tasks}(2)
	\task[\textbf{A.}] $0.60$
	\task[\textbf{B.}] $0.90$
	\task[\textbf{C.}]$0.16$
	\task[\textbf{D.}]$0.32$
\end{tasks}
\begin{answer}
	$$\int_{0}^{a_{0}}\left|\frac{1}{\sqrt{\pi a_{0}^{3}}} e^{-r / a_{0}}\right|^{2} 4 \pi r^{2} d r=\frac{4 \pi}{\pi a_{0}^{3}} \int_{0}^{a_{0}} r^{2} e^{-2 r / a_{0}} d r$$
	$\begin{aligned}
	&=\frac{4}{a_{0}^{3}}\left\{\left[r^{2} e^{-2 r / a_{0}}\left(-\frac{a_{0}}{2}\right)\right]_{0}^{a_{0}}-\left[2 r\left(e^{-2 r / a_{0}}\right)\left(-\frac{a_{0}}{2}\right)\left(-\frac{a_{0}}{2}\right)\right]_{0}^{a_{0}}+\left[2 e^{-2 r / a_{0}}\left(-\frac{a_{0}}{2}\right)\left(-\frac{a_{0}}{2}\right)\left(-\frac{a_{0}}{2}\right)\right]_{0}^{a_{0}}\right\} \\
	&=\frac{4}{a_{0}^{3}}\left[a_{0}^{2} e^{\frac{2 a_{0}}{a_{0}}}\left(-\frac{a_{0}}{2}\right)-2 a_{0}\left(\frac{a_{0}^{2}}{4}\right) e^{-2 a_{0} / a_{0}}-\frac{a_{0}^{3}}{4} e^{-2 a_{0} / a_{0}}+2 e^{-0}\left(\frac{a_{0}^{3}}{8}\right)\right] \\
	&=\frac{4}{a_{0}^{3}}\left[-\frac{a_{0}^{3}}{2} \frac{1}{e^{2}}-\frac{a_{0}^{3}}{2} \frac{1}{e^{2}}-\frac{a_{0}^{3}}{4 e^{2}}+\frac{a_{0}^{3}}{4}\right]=4\left[-\frac{5}{4 e^{2}}+\frac{1}{4}\right]=\left[-5 \times \frac{1}{e^{2}}+1\right] \\
	&=[-5 \times 0.137+1]=[-0.685+1]=0.32
	\end{aligned}$
	The correct option is \textbf{(d)}
\end{answer}
\begin{minipage}{\textwidth}
	\item Let $\psi_{\text {nlm }}$ denote the eigenstates of a hydrogen atom in the usual notation. The state
	$$
	\frac{1}{5}\left[2 \psi_{200}-3 \psi_{211}+\sqrt{7} \psi_{210}-\sqrt{5} \psi_{21-1}\right]
	$$
	is an eigenstate of
	\exyear{NET DEC 2015}
\end{minipage}
\begin{tasks}(2)
	\task[\textbf{A.}] $L^{2}$, but not of the Hamiltonian or $L_{z}$
	\task[\textbf{B.}]the Hamiltonian, but not of $L^{2}$ or $L_{z}$
	\task[\textbf{C.}]the Hamiltonian, $L^{2}$ and $L_{z}$
	\task[\textbf{D.}]$L^{2}$ and $L_{z}$, but not of the Hamiltonian
\end{tasks}
\begin{answer}
	$|\psi\rangle=\frac{1}{5}\left[2 \psi_{200}-3 \psi_{211}+\sqrt{7} \psi_{210}-\sqrt{5} \psi_{21-1}\right]$\\ $H|\psi\rangle=-\frac{13.6}{4}|\psi\rangle$\\
	So $|\psi\rangle$ is eigen state of $H$\\
	But $L^{2}|\psi\rangle \neq \alpha|\psi\rangle$ and $L_{z}|\psi\rangle \neq \beta|\psi\rangle$\\
	So $|\psi\rangle$ is not eigen state of $L^{2}$ and $L_{z}$\\
	The correct option is \textbf{(b)}
\end{answer}
\begin{minipage}{\textwidth}
	\item If the position of the electron in the ground state of a Hydrogen atom is measured, the probability that it will be found at a distance $r \geq a_{0}$ ( $a_{0}$ being Bohr radius) is nearest to
	\exyear{NET DEC 2018}
\end{minipage}
\begin{tasks}(2)
	\task[\textbf{A.}] $0.91$
	\task[\textbf{B.}] $0.66$
	\task[\textbf{C.}] $0.32$
	\task[\textbf{D.}]$0.13$
\end{tasks}
\begin{answer}
	\begin{minipage}{0.5\textwidth}
		$P\left(a_{0} \leq r<\infty\right)=\int_{a_{0}}^{\infty} r^{2}\left|R_{10}\right|^{2} d r$
		$$
		R_{10}=\frac{2}{a_{0}^{3 / 2}}
		$$
		$P\left(a_{0} \leq r<\infty\right)=\frac{4}{a_{0}^{3}} \int_{a_{0}}^{\infty} r^{2} e^{-\frac{2 r}{a_{0}}} d r=0.66$\\
	\end{minipage}
	\begin{minipage}{0.5\textwidth}
		\begin{figure}[H]
			\centering
			\includegraphics[height=3cm,width=5cm]{jamshi 7-crop}
		\end{figure}
	\end{minipage}
	The correct option is \textbf{(b)}
\end{answer}
\end{enumerate}

\newpage
\begin{abox}
	Practice set 2 solutions
	\end{abox}
\begin{enumerate}
	\begin{minipage}{\textwidth}
		\item The normalized ground state wavefunciton of a hydrogen atom is given by $\psi(r)=\frac{1}{\sqrt{4 \pi}} \frac{2}{a^{3 / 2}} e^{-r / a}$, where $a$ is the Bohr radius and $r$ is the distance of the electron from the nucleus, located at the origin. The expectation value $\left\langle\frac{1}{r^{2}}\right\rangle$ is
		\exyear{GATE 2011}
	\end{minipage}
	\begin{tasks}(2)
		\task[\textbf{A.}] $\frac{8 \pi}{a^{2}}$
		\task[\textbf{B.}]$\frac{4 \pi}{a^{2}}$
		\task[\textbf{C.}]$\frac{4}{a^{2}}$
		\task[\textbf{D.}]$\frac{2}{a^{2}}$
	\end{tasks}
	\begin{answer}
		$\left\langle\frac{1}{r^{2}}\right\rangle=\frac{4}{4 \pi a^{3}} \int_{0}^{\infty} \frac{1}{r^{2}} r^{2} e^{-\frac{2 r}{a}} d r \int_{0}^{\pi} \int_{0}^{2 \pi} \sin \theta d \theta d \phi=\frac{2}{a^{2}}$\\
		THe correct option is \textbf{(d)}
	\end{answer}
	\begin{minipage}{\textwidth}
		\item The ground state wavefunction for the hydrogen atom is given by $\psi_{100}=\frac{1}{\sqrt{4 \pi}}\left(\frac{1}{a_{0}}\right)^{3 / 2} e^{-r / a_{0}}$, where $a_{0}$ is the Bohr radius. The plot of the radial probability density, $P(r)$ for the hydrogen atom in the ground state is
		\exyear{GATE 2012}
	\end{minipage}
	\begin{tasks}(2)
		\task[\textbf{A.}]\begin{figure}[H]
			\centering
			\includegraphics[height=3cm,width=5cm]{diagram-20210824(2)-crop}

		\end{figure}
		\task[\textbf{B.}]\begin{figure}[H]
			\centering
			\includegraphics[height=3cm,width=5cm]{diagram-20210824(3)-crop}

		\end{figure}
		\task[\textbf{C.}]\begin{figure}[H]
			\centering
			\includegraphics[height=3cm,width=5cm]{diagram-20210824(4)-crop}

		\end{figure}
		\task[\textbf{D.}]\begin{figure}[H]
			\centering
			\includegraphics[height=3cm,width=5cm]{diagram-20210824(5)-crop}
		\end{figure}
	\end{tasks}
	\begin{answer}
		$\mathrm{n}$ : The ground state is given by $\psi_{100}=\frac{1}{\sqrt{4 \pi}}\left(\frac{1}{a_{0}}\right)^{3 / 2} e^{-r / a_{0}}$ Radial probability function $P(r)=|\psi|^{2} r^{2}=\frac{1}{4 \pi} \frac{1}{a_{0}^{3}} r^{2} e^{-2 r / a_{0}}$\\
		The correct option is \textbf{(d)}
	\end{answer}
	\begin{minipage}{\textwidth}
		\item An electron in the ground state of the hydrogen atom has the wave function $\psi(\vec{r})=\frac{1}{\sqrt{\pi a_{0}^{3}}} e^{-\left(\frac{r}{a_{0}}\right)}$, where $a_{0}$ is constant. The expectation value of the operator $\hat{Q}=z^{2}-r^{2}$, where $z=r \cos \theta$ is
		(Hint: $\left.\int_{0}^{\infty} e^{-a r} r^{n} d r=\frac{\sqrt{n}}{a^{n+1}}=\frac{(n-1) !}{a^{n+1}}\right)$
		\exyear{GATE 2014}
	\end{minipage}
	\begin{tasks}(2)
		\task[\textbf{A.}] $\frac{-a_{0}^{2}}{2}$
		\task[\textbf{B.}]$-a_{0}^{2}$
		\task[\textbf{C.}]$\frac{-3 a_{0}^{2}}{2}$
		\task[\textbf{D.}]$-2 a_{0}^{2}$
	\end{tasks}
	\begin{answer}
		$$\langle\hat{Q}\rangle=\left\langle z^{2}\right\rangle-\left\langle r^{2}\right\rangle \Rightarrow a_{0}^{2}-3 a_{0}^{2}=-2 a_{0}^{2}$$
	\end{answer}
	\begin{minipage}{\textwidth}
		\item A hydrogen atom is in the state
		$$
		\psi=\sqrt{\frac{8}{21}} \psi_{200}-\sqrt{\frac{3}{7}} \psi_{310}+\sqrt{\frac{4}{21}} \psi_{321},
		$$
		where $n, l, m$ in $\psi_{n l m}$ denote the principal, orbital and magnetic quantum numbers, respectively. If $\vec{L}$ is the angular momentum operator, then the average value of $L^{2}$ is $\cdots \quad \hbar^{2}$
		\exyear{GATE 2014}
	\end{minipage}
	\begin{answer}
		If $L^{2}$ will measure on state $\psi$ the measurement is $0 \hbar^{2}, 2 \hbar^{2}$ and $6 \hbar^{2}$ with probability
		$$
		\frac{8}{21}, \frac{3}{7}, \frac{4}{21} \text { so },\left\langle L^{2}\right\rangle=2 \hbar^{2} \times \frac{3}{7}+6 \hbar^{2} \times \frac{4}{21}=2 \hbar^{2}
		$$
	\end{answer}
	\begin{minipage}{\textwidth}
		\item An electric field $\vec{E}=E_{0} \hat{z}$ is applied to a Hydrogen atom in $n=2$ excited state. Ignoring spin the $n=2$ state is fourfold degenerate, which in the $|l, m\rangle$ basis are given by $|0,0\rangle,|1,1\rangle,|1,0\rangle$ and $|1,-1\rangle$. If $H^{\prime}$ is the interaction Hamiltonian corresponding to the applied electric field, which of the following matrix elements is nonzero?
		\exyear{GATE 2019}
	\end{minipage}
	\begin{tasks}(2)
		\task[\textbf{A.}] $\left\langle 0,0\left|H^{\prime}\right| 0,0\right\rangle$
		\task[\textbf{B.}] $\left\langle 0,0\left|H^{\prime}\right| 1,1\right\rangle$
		\task[\textbf{C.}]$\left\langle 0,0\left|H^{\prime}\right| 1,0\right\rangle$
		\task[\textbf{D.}] $\left\langle 0,0\left|H^{\prime}\right| 1,-1\right\rangle$
	\end{tasks}
	\begin{answer}
		The correct option is \textbf{(c)}
	\end{answer}
\end{enumerate}

QM -CSIR/chapter/hydrogen atom solutions.tex


\section{Python}

\subsection{Important Concepts}
Python is a language with many characteristics, ones more obvious and easy to understand, like in-fix notation, others are more complex, like Dynamic Typing and Automatic Memory Allocation.

Moreover, many programming principles like anonymous functions, map, zip are important and should be addressed in order to achieve flexible and overall good programming skills.

Beforehand, check the Python Notebook and the slides from Rodrigo Ventura in the ``Additional Material'' Folder. They are very complete and give a proper insight on how everything works.

Tuples are faster than lists. So, if the idea is doing a very big iteration where there's no memory problem, converting to tuple before probably helps.

However, regarding speed, always use built-in functions and libraries! Almost always what is slowing the program down is a huge for loop... Libraries are compiled already and are very optimised, therefore by using them you are probably running very optimised C code, which couldn't be faster than it is.

\subsubsection{Lambda and Anonymous functions}
Lambda is the keyword used to make an anonymous function. The following 2 pieces of code do exactly the same.

\begin{lstlisting}[language=python]
    def my_key(x):
    return x[0]

    l.sort(key=my_key)

    OR

    l.sort(key = lambda x: x[1])
\end{lstlisting}

And a short example is:
\begin{lstlisting}[language=python]
    -----short example on how cool python is-----
    def make_multiplier(factor):
    return lambda x: factor*x

    f = make_multiplier(2)
    ---------------------------------------------
\end{lstlisting}

Therefore, lambda is nothing more than defining a function, without giving it a name. It helps keeping the code simple specially when the function is just going to be used once.

\subsubsection{\_\_main\_\_}
This can be perfectly understood in the following \href{https://stackoverflow.com/questions/419163/what-does-if-name-main-do}{\ul{stackoverflow post}}.

In a nutshell, when a script is executed python assigns many names to certain variables like \_\_main\_\_. Is the script is executed in the terminal, then main will be the name of that script (without the .py). The \ii{if \_\_name\_\_ == '\_\_main\_\_':} aims to distinguish between situation where the script was imported, so that the functions it contains can be used elsewhere (in this case the main name won't be the name of the script which is stored in \_\_name\_\_) and the situation where the script is directly called in the terminal. If the script is directly called, for it to do something, something has to execute and that's usually what goes inside that if. When the script is imported, most of the times only the definitions matter and the calls will be made in the script that is calling that one.

Therefore, is a good tool in case you want to make a script importable but also callable.

\subsubsection{List Comprehensions}

Is possible to reduce verbose on appending to lists with for loops.

\image{Python/2.png}{.5}

\subsection{Some useful tools}
To check if a variable points to a certain data type:
\begin{lstlisting}[language=python]
    isinstance(var, [list, tuple, int])
\end{lstlisting}

\vspace{1cm}
To define functions with optional arguments and call them in the incorrect order:

\begin{lstlisting}[language=python]

    def draw_point(x, y, color='red', thickness=2):
        print('x =', x, 'y =', y, 'color=', color, 'thickness=', thickness)

    x = 1
    y = 2
    draw_point(x,y,'blue', 5)

    draw_point(x,y,thickness=2, color='blue')

\end{lstlisting}

\subsubsection{Unpacking Argument Lists}

\href{https://docs.python.org/3.7/tutorial/controlflow.html#unpacking-argument-lists}{\ul{Unpacking Argument Lists}}

A very useful trick to pass many arguments at once.

\subsection{Anaconda}
Basically, it is the Python distribution. Because Python is so big, with so many packages and Python development is becoming quite big, a program to install things was created.

Go to the official Anaconda website and download the installation script for Python 3 and for Linux: \href{https://www.anaconda.com/distribution/}{\ul{anaconda.com/distribution}}

Install \href{https://linuxize.com/post/how-to-install-anaconda-on-ubuntu-18-04/}{\ul{Linuxize - How to Install Anaconda on Ubuntu 18.04}}

\subsubsection{Package Manager}
\href{https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-pkgs.html}{\ul{User's Guide for Package Manager}}

Has instructions on how to install non-conda packages and many other useful things. It allowed the installation of streamlit!

\subsubsection{Broken Jupyter}

Tornado 6.0 breaks jupyter notebooks. Is required to uninstall it through pip and/or pip3 and through anaconda! Downgrade to 4.5.3. (This subsubsection will probably be removed in the future when it's fixed.)

\subsubsection{Other}

(base) Problem \href{https://askubuntu.com/questions/1026383/why-does-base-appear-in-front-of-my-terminal-prompt}{\ul{Ask Ubundu (base) in terminal}} - then close and open the terminal to take effect.

\subsection{Pandas}
\href{https://towardsdatascience.com/pandas-tips-and-tricks-33bcc8a40bb9?gi=29663f5b3e5
}{\ul{Check this link :)}}

\subsection{Jupyter Notebooks}
\par Along with Anaconda comes a full installation of the most recent python version and the Jupyter Notebooks, which are awesome to write python.
\par Here are some very useful shortcuts to tame that beast:
\vspace{.5cm}

\begin{center}
    There are 2 modes of shortcutting:
\begin{itemize}
    \item The Command Mode (when border of the cell is blue). Press ESC to
    access this mode.
    \item The Edit Mode (when border of the cell is green). Press ENTER to access this mode.
\end{itemize}

The Edit Mode is clearly superior:
\begin{itemize}
    \item Ctrl + Enter to run cell
    \item Shift + Enter to run cell and get directly to the next
    \item Ctrl + D to delete a whole line
    \item Ctrl + arrows jumps words (usual)
    \item Ctrl + Backspace deletes whole word (usual)
\end{itemize}

The Command Mode can be usefull sometimes, especially when adding cells is needed, but remember that the cell has to have a blue border:
\begin{itemize}
    \item A - insert cell before
    \item B - insert cell after
    \item DD - delete current cell
    \item Z - undo cell deletion
    \item M - markdown input type (Check: \href{https://gtribello.github.io/mathNET/assets/notebook-writing.html}{\ul{Netbook MarkDown}})
\end{itemize}

\end{center}

\subsection{Spyder}

Jupyter Notebooks is very fun for prototyping, however, to hard debug and develop some complex stuff, Spyder is the tool.

Install Anaconda. It is the absolute best way of getting everything up to date and running smoothly with the least complications. Check \href{https://linuxize.com/post/how-to-install-anaconda-on-ubuntu-18-04/}{\ul{here}} for the complete tutorial on how to do so (very easy).

After installing Anaconda, check the \href{https://github.com/spyder-ide/spyder/releases}{\ul{Official Spyder Releases github page}} and install the most recent one. \bb{Also remember never to run pip install spyder or similar or it can completely break Anaconda installation}.

Some useful shortcuts in Spyder:
\begin{itemize}
    \item Add \# \% \% to separate cells.
    \item Ctrl + Enter - Run cell
    \item Alt + Ctrl + Enter - Debug cell
    \item Ctrl + Shift + I - Go to Console
    \item Ctrl + Shift + E - Go to Editor
\end{itemize}

Note: spyder debugger is fucking shit. You can't get comfortably inside functions. VS Code does the job fairly well for the time being.

\subsection{Keras - A powerful API for TensorFlow}

Keras is simply the way of doing neural networks. It can't get easier than that!
TensorFlow was created by Google and PyTorch, it's rival, was created by Facebook. Not only is Google a better company in terms of the use of NN to do things, but
\href{https://towardsdatascience.com/tensorflow-vs-pytorch-vs-keras-for-nlp-exxact-8e51dd13c3f5}{\ul{this website}} also agrees that TensorFlow 2.0 is a complete game changer since it also declares keras as it's main API, therefore making it very very easy to do complex computations.

Overall, two things are fundamental to have success with these tools:
\begin{enumerate}
    \item To understand the functions and what all options mean, is necessary to have many mathematical expression in the head
    \item Have a very good control of the API - Keras. This section will take care of this step, trying, as best as possible, to explain the first step as well.
\end{enumerate}

However, learning how to use the api can take a while. Following are the main parts of a NN.

\subsubsection{Basic Flow - Image Classification Example}

Imports, data load, normalization, on mean and on stddev(not included in the example), create model, add layers, check summary, configure callback(s) and the optimizer, compile the model to apply the loss function and the optimizer, fit the model, predict with the model, plot training and validation loss curves, plot confusion matrices and accuracy scores opposing the predicted data with the actual test data outcomes.

\begin{lstlisting}[language = python]
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
import tensorflow.keras as keras
from keras import utils, layers, models, callbacks, optimizers
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
from sklearn.metrics import confusion_matrix, accuracy_score

x = np.load("mnist_train_data.npy")
y = np.load("mnist_train_labels.npy")

x_test = np.load("mnist_test_data.npy")
y_test = np.load("mnist_test_labels.npy")

print(x.shape)
print(y.shape)

plt.imshow(np.squeeze(x[5]))

#normalizing
x_norm = x / 255
x_test_norm = x_test / 255

#y to categorical
labels = keras.utils.to_categorical(y, num_classes=None)

model = keras.models.Sequential()

model.add(keras.layers.Flatten(input_shape = (28,28,1)))
#in case the samples are one dimensional, Input layer is enough.

#after the first layer it is not necessary to specify the input size
model.add(Dense(64, activation='relu'))
model.add(Dense(128, activation='relu'))
model.add(Dense(10, activation='softmax'))

#Expected: Layer 1 weights: (784+1) * 64 = 50240
#          Layer 2 weights: (64 +1) * 128 = 8320
#          Layer 3 weights: (128+1) *  10 = 1290
#                                Total   = 59850
model.summary()

"""
Example of CNN:
model2.add(Conv2D(16,kernel_size=(3,3), activation='relu'))
model2.add(MaxPooling2D(pool_size=(2,2)))

model2.add(Conv2D(32,kernel_size=(3,3), activation='relu'))
model2.add(MaxPooling2D(pool_size=(2,2)))

model2.add(Flatten(input_shape = (5,5,32)))
model2.add(Dense(64, activation='relu'))
model2.add(Dense(10, activation='softmax'))
"""

stopper = callbacks.EarlyStopping(patience=15, restore_best_weights=True)
#restore_best_weights - restore weights from best epoch

# min_delta may me zero because the epochs after the best only count if there's improvement

Adam = optimizers.Adam(lr=0.01, clipnorm = 1)

model.compile(loss='categorical_crossentropy', optimizer=Adam)

hist1 = \
    model.fit(x = x_norm, y=labels, batch_size=300, epochs=400,\
        verbose = 0, callbacks = [stopper], validation_split=0.3)

y_pred_labels = model.predict(x_test_norm)

y_pred = np.argmax(y_pred_labels, axis=1)

plt.plot(hist1.history['loss'])
plt.plot(hist1.history['val_loss'])
plt.title('Model loss')
plt.ylabel('Loss')
plt.xlabel('Epoch')
plt.legend(['Train', 'Test'], loc='upper left')

sns.heatmap(matrix,annot=True,cbar=True)
plt.ylabel('True Label')
plt.xlabel('Predicted Label')
plt.title('Confusion Matrix')

print('Accuracy:', accuracy_score(y_test, y_pred, normalize=True)* 100, '%')
\end{lstlisting}

\subsubsection{Sequential Model}
There are Sequential and Functional Models. Functional is when the mess it too big. Very likely not be needed since Sequential can do much more that I know how to do, including image analysis, convolutional and recurrent NN.
To learn the basic steps of it:
\href{https://keras.io/getting-started/sequential-model-guide/}{\ul{Keras-Getting started with the Sequential Model}}
And to learn the detailed methods of it and their functions:
\href{https://keras.io/models/sequential/}{\ul{The Sequential Model - all steps}}

\subsubsection{An optimizer}

An optimizer is one of the two arguments required for compiling a Keras model - optimizes the casual gradient descent algorithm to achieve a faster convergence, i.e to find the minimum faster. We've seen a few methods on adaptive step size and momentums.
\begin{lstlisting}[language=python]
    model.add(Dense(32, input_dim=784))
    model.add(Dense(32, input_shape=(784,)))
\end{lstlisting}

They are equivalent and both simply mean: Add a fully connected layer with 32 outputs and 784 one dimensional inputs.

\subsection{Plotting}

Very similar to MATLAB, but different.

It all depends on all complex you want to go.

Way 1:
\begin{lstlisting}[language=python]
    plt.subplot(1,2,1) #n_rows, n_cols, index
    plt.plot(x,y) #or sns.heatmap...
    plt.xlabel("hey")
    plt.ylabel("no")
    plt.title("yello")
\end{lstlisting}

Way 2:
\begin{lstlisting}[language=python]
    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15,5))
    fig.suptitle('Confusion Matrices comparison')

    sns.heatmap(matrix,annot=True,cbar=False, ax = ax1) %#ax1.plot(x, y)
    ax2.set(xlabel='True Label', ylabel='Predicted Label')
    ax1.set_title('With EarlyStopping')

    sns.heatmap(matrix2,annot=True,cbar=False, ax = ax2)
    ax2.set_title('Without EarlyStopping')
    ax2.set(xlabel='True Label', ylabel='Predicted Label')
\end{lstlisting}

Way 3 - a great midterm between the other two:
\begin{lstlisting}[language=python]
    f = plt.figure(figsize=(10,3))

    ax1 = plt.subplot(1,2,1)
    sns.heatmap(matrix,annot=True,cbar=False, ax = ax1)

    ax2 = plt.subplot(1,2,2)
    sns.heatmap(matrix2,annot=True,cbar=False, ax = ax2)
    plt.title("helo")
\end{lstlisting}

\href{https://stackoverflow.com/questions/10388462/matplotlib-different-size-subplots}{\ul{In case is necessary to have plots of different sizes}}

\subsection{Artificial Intelligence: A Modern Approach - Search Configuration}

Clone the 3 repositories below.

\href{https://github.com/aimacode/aima-python}{\ul{Aima Code}}

\href{https://github.com/aimacode/aima-data/tree/f6cbea61ad0c21c6b7be826d17af5a8d3a7c2c86}{\ul{Aima Data}}

\href{https://github.com/jiffyclub/ipythonblocks/blob/master/ipythonblocks/ipythonblocks.py}{\ul{ipythonblocks GitHub}}

Then put the ipythonblocks.py and the data folder inside the aima-python folder. That way the Jupyter notebooks should work

\subsection{From Python 2 to Python 3}
In Linux, if 2to3 is installed, one may simply run:
\begin{lstlisting}[language=bash]
    2to3 -w -n file.py
\end{lstlisting}
This will write the file translated into python 3 in the same file(-w) and without creating a backup(-n).

\subsection{Good Practices for Python Code}

This section is irrelevant if you only code alone for yourself. In case someone else will see your code, then you should write it as everyone else likes it. You'll probably agree with most if not all of these guidelines.

\href{https://www.python.org/dev/peps/pep-0008/}{\ul{Style Guide for Python Code}}

Some of the ones I tend to violate the most:
\begin{itemize}
    \item Variable names with underscore (underline)
\end{itemize}


\def\pathToRoot{../../}
\input{\pathToRoot/headers/uebungsheader.tex}

\def\issolution{}

\begin{document}

% {Sheet number}{headline}{deadline}
\exercisehead{7}{Regularization}{12.01.2021, 23:59}

\section*{Exercises}

\begin{exercise}[Norm penalty][0.5 + 0.5 + 0.5 = 1.5]
\end{exercise}

Read \href{https://www.deeplearningbook.org/contents/regularization.html}{Chapter 7} on Regularization from Deep Learning book.

\begin{enumerate}

	\item Why do we penalize only weights, but not biases? Explain in your own words.
	\item Let's consider a case of fitting linear regression to our Titanic dataset from previous exercise: we want to predict the \textit{price of the ticket}.
	As the input we use \textit{passenger class} and \textit{age}. As we found out, \textit{age} does not provide much information for our model.
	If we apply L2 norm penalty, which weight do we expect to be penalized more? Why? Think about the curvature of the loss function.
	\item Explain, why L1 norm is used as a feature extraction mechanism. Support your answer with an illustration of the effect of L1 (similar to Figure 7.1). What effect will L1 norm regularization have on the model defined above?

\end{enumerate}

\begin{solution}
   % write the solution here

\end{solution}

\begin{exercise}[Dataset Augmentation][0.5 + 1 + 0.5 + 0.5 = 2.5]

Read \href{https://neptune.ai/blog/data-augmentation-nlp}{this article} by Shahul Es and  about data augmentation in natural language processing. For a fuller picture, you can also have a look at \href{https://amitness.com/2020/05/data-augmentation-for-nlp/}{this article} by Amit Chaudhary. Answer the following questions:

\begin{enumerate}

	\item In computer vision, data augmentation happens on the go, whereas in NLP data is augmented before training.
	Why is it so? Explain in 3-4 sentences.
	\item Data augmentation in NLP is very task-specific and should be applied carefully.
	Provide an example of a augmentation method \& task pair where data augmentation might harm the model. Justify your answer (2-4 sentences).
	A list of NLP tasks you can find \href{https://natural-language-understanding.fandom.com/wiki/List_of_natural_language_processing_tasks}{here} (you can also give an example with a task not from this list).
	\item If we perform K-fold cross-validation, do we augment data before or after splitting the data? Why? Give at least two reasons.
	\item Does the proportion of augmented data depend on the size of the training dataset? Explain why. Hint: think about the relation between training dataset size and overfitting.

\end{enumerate}

\end{exercise}

\begin{solution}
   % write the solution here

\end{solution}

\begin{exercise}[Bagging and Dropout][1 + 1 + 2 = 4]
\end{exercise}

\begin{enumerate}
	\item Why can dropout be considered as an approximation to Bagging? Explain in two or
three sentences.
	\item Do we apply dropout during the inference? Justify your answer.
	\item Write a pseudocode for \href{https://cutt.ly/tjfq78f}{inverted dropout} implementation commenting each step.

\end{enumerate}

\begin{solution}
   % write the solution here

\end{solution}

\begin{exercise}[Adverserial Training in NLP][2]

In the lecture, you were introduced to adverserial training applied in computer vision.
On the internet, find how adverserial training is applied in NLP.
Describe \textbf{four} adverserial techniques: give examples and short descriptions (2-3 sentences). Provide the source of your answer.

\end{exercise}

\begin{solution}
   % write the solution here

\end{solution}

\section*{Submission instructions}

\framebox{
	\begin{minipage}{\linewidth}
		The following instructions are mandatory. If you are not following them, tutors can
		decide to not correct your exercise.
	\end{minipage}
}

\begin{itemize}
    \item You have to submit the solutions of this assignment sheet as a team of 2-3 students.
    \item  Hand in a \textbf{single} PDF file with your solutions.
    \item Make sure to write the student Teams ID and the name of each
    member of your team on your submission.
    \item Your assignment solution must be uploaded by only \textbf{one} of your team members to the course website.
    \item If you have any trouble with the submission, contact your tutor \textbf{before} the deadline.
\end{itemize}

\end{document}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% UMB-CS240-2016S: Programming in C
% Copyright 2016 Pejman Ghorbanzade <pejman@ghorbanzade.com>
% Creative Commons Attribution-ShareAlike 4.0 International License
% More info: https://github.com/ghorbanzade/UMB-CS240-2016S
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section*{Question 2}

The following functions compile and run as expected.
Briefly explain what each function is used for.
For each function, write a simple \texttt{main()} function that calls the function.

\lstset{language=c,tabsize=4}
\begin{enumerate}[label=(\alph*)]
\item \lstinline{}
\lstinputlisting[firstline=16,lastline=33]{\resDirectory/str2int.c}
\item \lstinline{}
\lstinputlisting[firstline=17,lastline=29]{\resDirectory/matrix.c}
\newpage
\item \lstinline{}
\lstinputlisting[firstline=16,lastline=27]{\resDirectory/adjacent.c}
\item \lstinline{}
\lstinputlisting[firstline=12,lastline=15]{\resDirectory/strcpy.c}
\end{enumerate}


\documentclass[12pt]{article}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{hyperref}
\usepackage[pdftex]{graphicx}
%\usepackage{physymb}
%\usepackage{wrapfig}
\usepackage{braket}
\usepackage{subcaption}
\title{PH 304: Assignment 3}

\author{Manish Goregaokar (120260006)}
\date{February 15, 2015}
\begin{document}
\maketitle

\section*{Problem 1}
Calculating the regular way,

The initial entropy is ($\frac32 k_B T=E_1/N_1=E_2/N_2$)

\begin{align*}
S_i = S_1 + S_2 &= N_1k_B\left(\ln V_1\left(\frac{4\pi em}{3}\frac E N\right)^{3/2}\right) + N_2 \dots\\
&= N_1k_B(\ln V_1 + s) + N_2k_B(\ln V_2 + s) \qquad s = \ln \left(\frac{4\pi em}{3}\frac E N\right)^{3/2}
\end{align*}

The final entropy is

\begin{align*}
S_f  &=  (N_1+N_2)k_B\ln\left((V_1+V_2)\left(\frac{4\pi em}{3}\frac{E_1+E_2}{N_1+N_2}\right)^{3/2}\right)\\
 &=  (N_1+N_2)k_B\ln\left((V_1+V_2)\left(\frac{4\pi em}{3}\frac E N\right)^{3/2}\right)\\
 &=N_1 k_B\ln(V_1 +V_2) + N_2 k_B \ln (V_1 + V_2) + k_B(N_1 +N_2)s
\end{align*}

Subtracting, we get that the change in entropy is $\Delta S = N_1k_B\ln\frac{V}{V_1}+ N_2k_B\ln\frac{V}{V_2}$

However, this has overcounted situations where effectively identical molecules are distributed across the barrier. We must remove all cases of exchanges, which involves dividing the original $\Omega$ by $N!$.

This manifests itself as a decrease in entropy by $k_B\ln N!$, which gives a net difference of $k_B\ln N!-k_B\ln N_1!-k_B\ln N_2!$

\begin{align*}
\Delta S_{corr} &= k_B(\ln N!-\ln N_1!-\ln N_2!)\\
&= k_B ((N_1+N_2)\ln (N_1+N_2) - N_1\ln N_1 -N_2\ln N_2)\\
&= k_B (N_1\ln\frac N N_1 +N_2\ln\frac N N_2 )\\
&= k_B (N_1\ln\frac V V_1 +N_2\ln\frac V V_2 )\qquad\because \frac N V = \frac{N_1} V_1 = \frac{N_2} V_2\\
&= \Delta S
\end{align*}

Since the necessary correction $\Delta S_{corr}$ is the same as the original change in entropy, The net change, $\Delta S - \Delta S_{corr} = 0$, so the net entropy of mixing is zero.

\section*{Problem 2}

The total number of states of this system is $\Omega(E,N)$. If a single oscillator has energy $\epsilon$, the number of states with this energy will be the number of states of $N-1$ oscillators of energy $E-\epsilon$ since they are independent. Thus, the probability is $P(\epsilon)=\frac{\Omega(E-\epsilon,N-1)}{\Omega(E,N)}$

Since $\Omega(E,N)=\frac{(2\pi)^N}{N!}\left(\frac{2E}{\omega}\right)^N$, we get that this probability distribution is $\frac{N! \left(\frac{E}{\omega }\right){}^{-N} \left(\frac{E-e}{\omega }\right){}^{N-1}}{4 \pi  (N-1)!}$, which simplifies to $\frac{N \omega  E^{-N} \left(E-e\right){}^{N-1}}{4 \pi }$

Thus, the probability distribution is $P(e) = \frac{N \omega  E^{-N} \left(E-e\right){}^{N-1}}{4 \pi }$, where $e = \frac{p^2}{2 m}+\frac{1}{2} m q^2 \omega ^2$. At large $N$, we get $P(\epsilon) = \frac{\omega  e^{-\frac{\epsilon}{T k_B}}}{4 \pi  T k_B}$ since $E=Nk_B T$, thus getting $\boxed{P(p,q) = \frac{\omega  e^{-\frac{\frac{p^2}{2 m}+\frac{1}{2} m q^2 \omega ^2}{T k_B}}}{4 \pi  T k_B}}$

Integrating $\int_{-\infty}^\infty\int_{-\infty}^\infty\frac{p^2}{2m} P(p,q)\mathrm dp\mathrm dq$, we get $\boxed{\frac{k_B T}{4}}$, the average kinetic energy of the individual oscillator. Integrating  $\int_{-\infty}^\infty\int_{-\infty}^\infty\frac{m\omega q^2}{2} P(p,q)\mathrm dp\mathrm dq$, we also get $\boxed{\frac{k_B T}{4}}$, which is the average potential energy of the individual oscillator.
\section*{Problem 3}

We need to choose $n$ atoms out of $N$ to remove them, so the number of ways to remove $n$ atoms is $\boxed{^NC_n}$

We need to choose an ordered set of $n$ sites out of $M$ , so we have $\boxed{^MC_n}$ as the number of ways to place these $n$ atoms.

Thus, the total number of equiprobable states is $\Omega(N,M,n) = ^NC_n\times^MC_n$
\begin{align*}
\therefore S(E, N, M) &= k_B\ln \Omega\\
&= k_B\ln \left(\frac{N!}{n!(N-n)!}\frac{M!}{n!(M-n)!}\right)\\
&=k_B(N\ln N -N + M\ln M - M -n\ln n + n -(N-n)\ln(N-n)\\ &\quad+ N - n -(M-n)\ln(M-n) + M - n -n\ln n + n)\\
&= k_B(N\ln N + M\ln M  -(N-n)\ln(N-n)-(M-n)\ln(M-n)-2 n\ln n)\\
&= k_B(N\ln N + M\ln M  -(N-n)\ln(N-n)-(M-n)\ln(M-n)\\&\quad\quad-n\ln\frac{n}{N} -n\ln\frac{n}{N}-n\ln N - n\ln M)\\
&= -k_B \left(n\ln\frac n N + n\ln\frac n M + (N-n)\ln\left(1- \frac n N\right)+ (M-n)\ln\left(1- \frac n M\right) \right)
\end{align*}

Thus, entropy is $\boxed{-k_B \left(n\ln\frac n N + n\ln\frac n M + (N-n)\ln\left(1- \frac n N\right)+ (M-n)\ln\left(1- \frac n M\right) \right)}$, where $n=\frac E\Delta$

Now, $\frac 1 T = \frac{\partial S}{\partial E}= \frac{\partial S}{\Delta\partial n}$

\begin{align*}
\therefore \frac{\Delta}{k_B T}& = \frac{\partial S}{\partial n}\\
&=  \log \left(1-\frac{n}{N}\right)-\log \left(\frac{n}{N}\right))+\log \left(1-\frac{n}{M}\right)-\log \left(\frac{n}{M}\right)\\
&= \ln\left(\frac{N}{n} -1\right)+\ln\left(\frac{M}{n} -1\right)\\
&= \ln\left(\frac{N\Delta}{E}-1\right)+ \ln\left(\frac{N\Delta}{E}-1\right)
\end{align*}

Thus, $\boxed{T =\frac{\Delta}{k_B\ln\left(\frac{N\Delta}{E}-1\right)+ \ln\left(\frac{N\Delta}{E}-1\right)}}$

Rearranging, we have $\ln\left(\frac{N}{n} -1\right)+\ln\left(\frac{M}{n} -1\right) = \frac{\Delta}{k_B T}$, or $\ln\frac{(N-n)(M-n)}{n^2}=\frac{\Delta}{k_B T}$, so we get $\boxed{\frac{n^2}{(N-n)(M-n)} = e^{-\frac{\Delta}{k_B T}}}$

For large $\Delta$, $\frac{n^2}{(N-n)(M-n)} = 0$, so $\boxed{n = 0}$. For small $\Delta$, $\frac{n^2}{(N-n)(M-n)} = 1$, we get $\boxed{n = \frac{MN}{M+N}}$

If $M=n$, then $e^{\frac{\Delta}{k_B T}} = \frac{(N-n)^2}{n^2} = (f^{-1}-1)^2$, where $f=n/N$

This gives us $f =\frac{1}{1 + e^{\frac{\Delta}{2k_B T}}}$

For $\Delta=1$, the value of this expression is $\boxed{1.6\times 10^{-17}}$ for $T=300K$, and $\boxed{9.15 \times 10^{-6}}$ for $T=1000K$

\end{document}

Course material/PH 304 - Statistical Physics/Assignment 3/assign3.tex


\documentclass{article}
\usepackage[utf8]{inputenc}

\title{Probability Info Write-Up}
\author{kyjeckland }
\date{October 2021}

\begin{document}

\maketitle

\section{Desert Tiles}
Here, we look at the probabilities and expected values associated with desert tiles in the simplest case of only one camel. In this instance, the camel can move at most 3 spaces, so we will restrict the distance of the desert tile to within 3 tiles. Notice that each possible movement value is equiprobable, so the probability that the camel lands on the desert tile is exactly $\frac{1}{3}$ regardless of the spot selected for the desert tile. Next, we will take a look at the expected value in the three different cases.

\textbf{Case 1:} The desert tile is immediately in front of the camel.In this case, if a $1$ is rolled the camel will move $2$ spaces. If we let $Z$ be the random value that represents the distance the camel moves, then the expected value is calculated as follows:

\begin{center} $E(Z) = \frac{1}{3}*2 + \frac{1}{3}*2+\frac{1}{3}*3 = \frac{2}{3}+\frac{2}{3}+\frac{3}{3} = \frac{7}{3} \approx 2.33$
\end{center}

\textbf{Case 2:} The desert tile is two tiles in front of the camel. If a $2$ is rolled here, then the camel will move $3$ spaces. As above, we find the expected value as follows:

\begin{center} $E(Z) = \frac{1}{3}*1+\frac{1}{3}*3+\frac{1}{3}*3 = \frac{7}{3} \approx 2.33$
\end{center}

\textbf{Case 3:} The third and final case is when the desert tile is 3 spaces away. Here, if 3 is rolled, the camel will move 4 spaces. The expected value is then:
\begin{center} $E(Z) = \frac{1}{3}*1+\frac{1}{3}*2+\frac{1}{3}*4 = \frac{7}{3} \approx 2.33$
\end{center}

\section{Crazy Camels}
The crazy camels move in the opposite direction of the other camels, but only pose a threat if a camel lands on top of them. Here we will go over the simplest case of one normal camel and one crazy camel, and evaluate the probabilities and expected values in each case. The probabilities in this scenario are more complex than those in the desert tile scenario. We will assume that both the crazy camel and normal camel's dice are the only ones that have yet to be rolled. Notice that if the crazy camel goes first, the probability that the normal camel moves backward is exactly 0. Furthermore, the probability of this specific crazy camel moving upon the crazy die being rolled is $.5$, because there are $3$ ways it can move out of a total of $6$ possibilities. Suppose $x$ is the distance of the crazy camel from the normal camel. Given this information, it follows that the probability the normal camel will move backward is
 $&=\frac{1}{3}\frac{1}{2}\frac{1}{2} = \frac{1}{12}$

The probability that the normal camel lands on the crazy camel but the crazy camel doesn't move would then be
\begin{center}$\frac{1}{2}\frac{1}{3}\frac{1}{2}+\frac{1}{2}\frac{1}{3} = \frac{1}{12}+\frac{1}{6} = \frac{1}{12}+\frac{2}{12} = \frac{3}{12} = \frac{1}{4}$
\end{center}
With this information, we can then calculate the expected values of each possible distance.

\textbf{1 tile away:}
\\\begin{center} $E(Z) = 1*\frac{1}{4} + 2*\frac{1}{3}+3*\frac{1}{3} -\frac{1}{36}+\frac{2}{36} = \frac{11}{6} \approx 1.83$
\end{center}

\textbf{2 tiles away:}
\\\begin{center} $E(Z) = 1*\frac{1}{3} + 2*\frac{1}{4}+3*\frac{1}{3} -\frac{1}{36}+\frac{1}{36} = \frac{11}{6} \approx 1.83$
\end{center}

\textbf{3 tiles away:}
\\\begin{center} $E(Z) = 1*\frac{1}{3} + 2*\frac{1}{3}+3*\frac{1}{4} +\frac{2}{36}+\frac{1}{36} = \frac{11}{6} \approx 1.83$
\end{center}

And so the expected values are all the same, as with the desert tiles. How, then, do we judge which position is 'better' or 'worse,' and can we generalize this sort of thinking to apply to more complex scenarios?

\end{document}


\chapter{Conclusion}
\label{chp:conc}

Throughout this report the theory of different subjects within statistical learning were elaborated upon. Related to each topic exercises were implemented in Python to give a deeper understanding for each method.

Different methods for prediction and how they differ were discussed. Linear regression was the only method presented for predicting a quantitative response variable, but it showed to be less appropriate for classification. For classification logistic regression and linear discriminant analysis were explained. Choosing between these two mainly depended on whether the classes had a Gaussian distribution. If this was the case then linear discriminant analysis would likely outperform logistic regression and vice versa. For more non-linear data with more complex decision boundaries quadratic discriminant analysis was presented. If the decision boundary was even more complex then K-nearest neighbor classifier was presented as an option. Therefore no method can be seen as better than the other, since choosing between the prediction methods is all about the type of response and the data.

Resampling methods for getting information about the properties of the models were also explained in the report. Bootstrap can be used for quantifying uncertainty in a model. It showed a good fit for estimating the standard error of coefficients in the exercises. For more complex data the bootstrap method might not be applied as simply.
Validation set approach and cross-validation were presented as methods for estimating the test error. Validation set approach was a simpler and less computationally expensive approach but it can lead to and overestimation of the test error. Leave-one-out cross-validation and k-fold cross-validation were also presented. In general a 5 or 10 fold cross-validation is preferred as a good compromise for the bias-variance trade-off.

For subset selection best subset selection along with forward and backward stepwise selection were explained. Best subset selection estimates the most optimal numbers of predictors and which predictors to use more precisely because it iterates through all combinations. However, when more than 40 predictors are available, then best subset selection becomes infeasible. Instead forward and backward stepwise selection can be used as two more computationally efficient methods which both often estimates similar models to best subset selection.

When the true label is unknown then unsupervised learning techniques can be used. Unlike supervised learning there is no simple goal and the analysis is often more exploratory. K-means clustering and hierarchical clustering was presented in the report. If the data in fact has a hierarchical structure then this is likely to estimate the clusters well. If the data does not have a hierarchical structure then K-means is preferred even though the number of clusters must be prespecified.

The report has given an introduction to all the topics presented in the course \emph{Decision support systems}. Furthermore it has given insight in the advantages and disadvantages of the different methods and understanding when a method is applicable.


\lab{Introduction to Matplotlib: 3D Plotting and Animations}{Introduction to Matplotlib: 3D Plotting and Animations}
\label{lab:lab0}

\objective{3D plots and animations are useful in visualizing solutions to ODEs and PDEs found in many dynamics and control problems.
In this lab we explore the functionality contained in the 3D plotting and animation libraries in Matplotlib.}

\section*{Introduction}
Matplotlib is a Python library that contains tools for creating plots in multiple dimensions.
The library contains important classes that are needed to create plots.
The most important objects to understand in this lab are figure objects, axes objects, and line objects. These three objects are created using the following code.

\begin{lstlisting}
>>> import matplotlib.pyplot as plt
>>> fig = plt.figure()                  # Create figure object.
>>> ax = fig.add_subplot(111)           # Create axes object.
>>> line2d, = plt.plot([],[])           # Create empty 2D Line object
>>> line3d, = plt.plot([],[],[])        # Create empty 3D line object
\end{lstlisting}

Recall that \li{plt.figure()} creates a \li{matplotlib.figure.Figure} object, which is the window that is displayed when \li{plt.show()} is called.
3D plotting and animation both require explicitly defining the \li{Figure} object, as shown above.
This allows for the object to be updated and modified, as will be explained later in the lab.

\li{Figure} objects contain \li{matplotlib.axes._subplots.AxesSubplot} objects, called \textit{axes}. Axes are spaces to plot on, and are created by the \li{add_subplot()} method of a \li{Figure} object. Figures can have multiple axes.
%For more information, see [reference Intro to Matplotlib 1 here].

Calling \li{plt.plot()} returns a list of line objects.
For example, supposing \li{x1}, \li{y1}, \li{x2}, and \li{y2} are arrays containing data for two separate curves, then calling \li{plt.plot(x1, y1, x2, y2)} will return a list with two elements.
Each element of the list is a \li{matplotlib.lines.Line2D} object.
If the axes is three-dimensional, then the returned list will contain \li{matplotlib.lines.Line3D} objects.
Because this function call returns a list, if only one line is plotted, adding a trailing comma to the variable name will assign the name to the first element of the returned list.
You can alternatively reference the zero index of the returned list, but using a trailing comma is standard.

\begin{comment}
As mentioned in the Introduction to Matplotlib lab in Volume 1, 3D plots and animations are useful in visualizing solutions to ODEs and PDEs found in many dynamics and control problems.
This lab covers basics for 3D plotting, animating 2D plots, and animating 3D trajectories and 3D surfaces.

In Matplotlib, plots are created using figure objects.
It is possible to create a plot without explicitily instantiating the figure object, but creating an instance separately allows for useful functions to be called on that object. Initializing a figure object is done using the following code:

\begin{lstlisting}
>>> fig = plt.figure()
\end{lstlisting}

3D plotting and animation both require the figure object to be created explicitly.
\end{comment}
\begin{comment}
\begin{info}
There are different options for plotting in Jupyter Notebook.
In the past we have used \li{\%matplotlib inline}.
For labs with animation we will use \li{\%matplotlib notebook} instead.
This is an interactive regime that allows animations to be displayed without creating a new window.
\end{info}
\end{comment}

\section*{Animation Background}
The animation library in Matplotlib contains a class called \li{FuncAnimation}.
We will use this class throughout this lab. \li{FuncAnimation} requires a user-defined \textit{update} function that controls the plot for each frame of the animation.
This grants the user wide flexibility and control of the resulting animation.
The following steps describe the process of creating a simple animated plot using the \li{FuncAnimation} class.
\begin{enumerate}
\item Compute all data to be plotted.
\item Explicitly define figure object.
\item Define line objects to be altered dynamically.
\item Create function to update line objects.
\item Create \li{FuncAnimation} object.
\item Display using \li{plt.show()}.
\end{enumerate}

These steps will be explained by way of an example.
The arrays \li{x} and \li{y} contain data giving the location of a particle moving in the plane.
To visualize this motion, one could animate the particle as well as display the trajectory that the particle has traveled.
For this animation, two separate \li{Line2D} objects must be created on an axes object.
The first, \li{particle} will be for the position of the particle itself, and the second, \li{traj} will be for the trajectory that the particle has traveled.
Note that these objects are created with empty lists of data.
The update function will be used to dynamically set the data to be plotted in these line objects.

\begin{lstlisting}
>>> import matplotlib.animation as animation
>>> import numpy as np
>>> t = np.linspace(0,2*np.pi,100)
>>> x = np.sin(t)
>>> y = t**2
>>> fig = plt.figure()
>>> ax = fig.add_subplot(111)
>>> ax.set_xlim((-1.1,1.1))
>>> ax.set_ylim((0,40))
>>> particle, = plt.plot([],[], marker='o', color='r')
>>> traj, = plt.plot([],[], color='r', alpha=0.5)
\end{lstlisting}

The update function must be defined a specific way in order to interact properly with the
\li{matplotlib.animation.FuncAnimation} object.
The update function must accept the current frame index as its first input parameter and it must return a list or tuple of line objects.
The current frame index is used to access the data to be plotted in the current frame.
Both 2D and 3D line objects have the built-in method \li{.set_data()}.
This function takes in two one-dimensional arrays representing x and y values to plot.
This allows a single line object to display different data for each frame.
Inside the update function, \li{.set_data()} is called on the line objects with the relevant data as inputs.

\begin{lstlisting}
>>> def update(i):
>>>     particle.set_data(x[i],y[i])
>>>     traj.set_data(x[:i+1],y[:i+1])
>>>     return particle,traj
\end{lstlisting}

\begin{comment}
Three main objects are required for animating plots in Matplotlib.
First, you need to create a figure object as explained above.
Then you need to create a \li{matplotlib.lines.Line2D} or \li{matplotlib.lines.Line3D} object (depending on the dimension of the graph you are animating).
The following code shows one way to initialize a \li{Line2D} object:

\begin{lstlisting}
>>> line, = plt.plot([],[])
\end{lstlisting}

As explained above, \li{plt.plot()} returns a list.
Thus setting the output equal to \li{line,} is equivalent to setting the variable named \li{line} to the first element of the returned list.
Accessing the zero index of the list would yield the equivalent result, but it is conventional to use the trailing comma.

One figure and line objects have been initialized, an update function needs to be defined and a \li{FuncAnimation} object created.
The \li{FuncAnimation} constructor takes in the figure and the update function as inputs.
It calls the update function at each frame of the animation.

The update function must take the time frame index as its first parameter.
The \li{Line2D} and \li{Line3D} objects have the built in function call \li{set_data()}.
This function takes in 2 1D arrays representing x and y values (or a 2xn array with x and y values). Inside the update function, the \li{set_data()} function is called on the line objects using the frame index to specify the data.
Then the line object is returned, as shown below:

\begin{lstlisting}
>>> def update(i):
>>>     line.set_data(x, y[i])
>>>     return line,
\end{lstlisting}
\end{comment}

Next, the \li{FuncAnimation} object is created.
The argument \li{frames} specifies the iterable representing the frame indices.
If \li{frames} is an integer, it is treated as the iterable \li{range(frames)}.
After the \li{FuncAnimation} object is created, \li{plt.show()} displays the animation.

\begin{lstlisting}
>>> ani = FuncAnimation(fig, update, frames=range(100), interval=25)
>>> plt.show()
\end{lstlisting}

The following table shows more parameters that can be passed into FuncAnimation.

\begin{table}[H]
\centering
\begin{tabular}{r|l}
Parameter & Description\\
\hline
\li{fargs} (tuple) & Additional arguements to pass update function\\
\li{interval} (float) & Delay between frames in milliseconds\\
\li{repeat} (bool) & Determines whether animation repeats (Default True)\\
\li{blit} (bool) & Determines whether blitting is used (Default False)\\
\end{tabular}
\end{table}

\begin{info}
When using FuncAnimation, it is essential that a reference is kept to the instance of the class.
The animation is advanced by a timer and if a reference is not held for the object, \li{Python} will automatically garbage collect and the animation will stop.
\end{info}

\subsection*{Embedding Animations}
Some systems may struggle with using \li{plt.show()} to display an animation.
In this case, it may be easier to embed the animation using the HTML5 API.
Jupyter notebooks use HTML to display their contents, so we can leverage this and use HTML5's video capabilities to insert video directly into a notebook.

To embed the video directly into a notebook using HTML5 you must use the \li{IPython.display} module.
This module will be able to interpret an encoded HTML5 video, which \li{matplotlib.animation} can create.
This method tends to be much more simple than rendering the animation to an \li{.mp4} file and then embedding that file into a notebook, as it does not require an outside encoder, and it tends to be encounter fewer bugs than using \li{plt.show()}.
Here is a snippet you may reference to embed an animation using \li{IPyton.display}
\begin{lstlisting}
# required import statements
from IPython.display import HTML
import matplotlib.pyplot as plt
from matplotlib import animation

# disable interactive mode
plt.ioff()
'''
Here we would insert whatever code needed to create the animation
such as instantiating the fig object and defining the update function
'''
# create animation
ani = animation.FuncAnimation(fig, update, frames=N, interval=i)
# render as html5 and embed
HTML(ani.to_html5_video())
\end{lstlisting}

\begin{problem}
Use the FuncAnimation class to animate the function $y=\sin(x+0.1t)$ where $x \in [0,2\pi]$, and $t$ ranges from $0$ to $100$ seconds.
\end{problem}

\section*{3D Plotting Introduction}
3D plotting is very similar to 2D plotting.
The main difference is that a set of 3D axes must be created within the figure object.
A 3D axes object is created using the additional keyword argument \li{projection='3d'}, as shown below. Note that the Axes3D submodule must first be imported in order to create the 3D axis.

\begin{comment}
Like animations, 3D plotting also requires the initialization of a figure object.
After creating a figure object, you can create a 3D axis object by calling \li{gca()} or \li{add_subplot(111)} on the figure object with the keyword argument \li{projection='3d'} as shown below. Both of these functions return a 3D axis object. Note, the 3d projection argument is contained in the Axes3D library.
\end{comment}

\begin{lstlisting}
>>> from mpl_toolkits.mplot3d import Axes3D
>>>
>>> # Create figure object.
>>> fig = plt.figure()
>>>
>>> # Create 3D axis object using add_subplot().
>>> ax = fig.add_subplot(111, projection='3d')
\end{lstlisting}

\subsection*{3D Static Plotting}
\begin{comment}, as shown below.

\begin{lstlisting}
>>> x = np.linspace(0,5,10)
>>> y = np.linspace(1,3,10)
>>> z = np.linspace(-1,-1,10)
>>> plt.plot(x, y, z)
>>> plt.show()
\end{lstlisting}
\end{comment}

When the axes object is explicitly defined, plots are generated by calling the chosen plot function (such as \li{ax.plot()} on the axes object.
Additional information on the use of axes objects can be found here: \href{https://matplotlib.org/api/axes\_api.html}{https://matplotlib.org/api/axes\_api.html}.

\begin{problem}
The orbits for Mercury, Venus, Earth, and Mars are stored in the file \li{orbits.npz}.
The file contains four NumPy arrays: \li{mercury}, \li{venus}, \li{earth}, and \li{mars}.
The first column of each array contains the x-coordinates, the second column contains the y-coordinates, and the third column contians the z-coordinates of each planet, all relative to the Sun, and expressed in AU (astronomical units, the average distance between Earth and the Sun, approximately 150 million kilometers).

Use \li{np.load('orbits.npz')} to load the data for the four planets' orbits.
Create a 3D plot of the orbits, and compare your results with Figure \ref{lab0:3dplot}.
\end{problem}

\begin{figure}[H]
\centering
\includegraphics[width=\textwidth]{figures/orbits.png}
\caption{The solution to Problem 2.}
\label{lab0:3dplot}
\end{figure}

\subsection*{3D Animations}
The key difference between 2D and 3D animations is that the \li{.set_data()} method does not support setting the \li{z} values.
Instead, set the \li{x} and \li{y} values with \li{.set_data()} as before, and then set the \li{z} values with \li{.set_3d_properties()}.
The \li{.set_3d_properties()} function call is also made inside the update function.

Animation in 3D requires more careful consideration than in the 2D case.
When \li{matplotlib} displays a 3D plot, it does so in an interactive figure that allows the user to change the camera angle and position.
Since 3D rendering is more computationally expensive than 2D rendering, interactive views of 3D animations often have poor framerates and choppy rendering.
The solution is to use the tools in the \li{matplotlib.animation} module to render the animation and then embed that rendered animation within a Jupyter Notebook.
This can be done in two ways: embedding the video directly using an HTML5 API, or encoding the animation to an \li{.mp4} video and embedding that video.
Embedding the video directly tends to be easier, as \li{matplotlib} does not come with an \li{.mp4} video encoder, while it can encode video for HTML5.

\subsubsection*{Saving Animations}
Saving the animation by encoding it to a \li{.mp4} file will allow you to display the video inline inside a Jupyter Notebook, or view it using any video player supporting the chosen filetype.

Unfortunately, Matplotlib does not come with a built-in video encoder.
The \li{matplotlib.animation} module supports several third-party encoders. FFmpeg is a lightweight solution which can be obtained from: \href{https://www.ffmpeg.org/download.html}{https://www.ffmpeg.org/download.html}.

To prevent the animation from displaying while it is being rendered as video, use \li{plt.ioff()}.
This turns off matplotlib's interactive mode until \li{plt.ion()} is called.
After creating the animation object, use its \li{.save()} method with the desired filename to render and save the video. The following code is given for reference:

\begin{lstlisting}
animation.writer = animation.writers['ffmpeg']
plt.ioff()    # Turn off interactive mode to hide rendering animations

# Code to create figure, axes, update function
ani = animation.FuncAnimation(fig,update,frames,interval)
ani.save('my_animation.mp4')
\end{lstlisting}
To display the \li{.mp4} video in a Jupyter Notebook, run the following code in a separate markdown cell:
\begin{lstlisting}
<video src="planet_ani.mp4" controls>
\end{lstlisting}

\begin{problem}
Each row of the arrays in \li{orbits.npz} gives the position of the planets at evenly spaced time points. The arrays correspond to 1400 points in time over a 700 day period (beginning on 2018-5-30).
Create a 3D animation of the planet orbits.
Display lines for the trajectories of the orbits and points for the current positions of the planets at each point in time.
Your \li{update()} function will need to return a list of Line3D objects, one for each orbit trajectory and one for each planet position marker.
Embed your animated plot.
\end{problem}

\section*{Surface Plotting}
3D surface plotting is very similar to regular 3D plotting discussed earlier.
The difference with surface plots is that they require first creating a \textit{meshgrid} for X and Y. Meshgrids are created using the NumPy command \li{np.meshgrid(x, y)} where \li{x} and \li{y} are 1D arrays representing the x and y coordinates of the grid.
This function creates 2D arrays \li{X} and \li{Y} that combined give cartesian cordinates for every point made from the \li{x} and \li{y} arrays.

Once a meshgrid is defined, a surface plot is generated by calling \li{ax.plot_surface(X, Y, Z)}, where \li{Z} is a 2D array of height values that is the same shape as \li{X} and \li{Y}.

\begin{problem}
Make a surface plot of the bivariate normal density function given by:

$$f(\mathbf{x})=\frac{1}{\sqrt{det(2\pi \Sigma)}}\exp{\left[-\frac{1}{2}(\mathbf{x} - \mu)^T \Sigma^{-1} (\mathbf{x} - \mu)\right]}$$

where $\mathbf{x}=[x,y]^T$, $\mu=[0,0]^T$ is the mean vector, and $$\Sigma = \begin{bmatrix} 1 & 3/5 \\ 3/5 & 2 \end{bmatrix}$$ is the covariance matrix. Compare your results with Figure \ref{lab0:surf}.
\end{problem}

\begin{figure}[H]
\centering
\includegraphics[width=100mm]{figures/normal_density.png}
\caption{The solution to Problem 4.}
\label{lab0:surf}
\end{figure}

\subsection*{Surface Animations}
Animating a 3D surface is slightly different from animating a parametric curve in 3D.
The object created by \li{.plot_surface()} does not have a \li{.set_data()} method.
Instead, use \li{ax.clear()} to empty the axes at each frame, followed by a new call to \li{ax.plot_surface()}.
Note that the axes limits must be reset after \li{ax.clear()} is called.

\begin{problem}
Use the data in \li{vibration.npz} to produce a surface animation of the solution to the wave equation for an elastic rectangular membrane.
The file contains three NumPy arrays: \li{X}, \li{Y}, \li{Z}.
\li{X} and \li{Y} are meshgrids of shape \li{(300,200)} corresponding to 300 points in the y-direction and 200 points in the x-direction, giving a 2x3 rectangle with one corner at the origin.
\li{Z} is of shape \li{(150,300,200)}, giving the height of the vibrating membrane at each (x,y) point for 150 values of time.
In the language of partial differential equations, this is the solution to the following initial/boundary value problem:

$$u_{tt} = 6^2(u_{xx}+u_{yy})$$
$$(x,y) \in [0,2]\times[0,3], t \in [0,5]$$
$$u(t,0,y)=u(t,2,y)=u(t,x,0)=u(t,x,3) = 0$$
$$u(0,x,y) = xy(2-x)(3-y)$$
\end{problem}


\documentclass[14pt]{article}

\usepackage{fancyhdr}
\usepackage{extramarks}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{tikz}
\usepackage[plain]{algorithm}
\usepackage{algpseudocode}
\usepackage{enumitem}
\usepackage{relsize}
\usepackage{scrextend}
\usepackage{graphicx}

\usetikzlibrary{automata,positioning}

%
% Basic Document Settings
%

\topmargin=-0.45in
\evensidemargin=0in
\oddsidemargin=0in
\textwidth=6.5in
\textheight=9.0in
\headsep=0.25in

\linespread{1.1}

\pagestyle{fancy}
\lhead{\hmwkAuthorName}
\chead{\hmwkClass\ (\hmwkClassInstructor): \hmwkTitle}
\rhead{\firstxmark}
\lfoot{\lastxmark}
\cfoot{\thepage}

\renewcommand\headrulewidth{0.4pt}
\renewcommand\footrulewidth{0.4pt}

\setlength\parindent{0pt}

%
% Create Problem Sections
%

\newcommand{\enterProblemHeader}[1]{
    \nobreak\extramarks{}{Problem \arabic{#1} continued on next page\ldots}\nobreak{}
    \nobreak\extramarks{Problem \arabic{#1} (continued)}{Problem \arabic{#1} continued on next page\ldots}\nobreak{}
}

\newcommand{\exitProblemHeader}[1]{
    \nobreak\extramarks{Problem \arabic{#1} (continued)}{Problem \arabic{#1} continued on next page\ldots}\nobreak{}
    \stepcounter{#1}
    \nobreak\extramarks{Problem \arabic{#1}}{}\nobreak{}
}

\setcounter{secnumdepth}{0}
\newcounter{partCounter}
\newcounter{homeworkProblemCounter}
\setcounter{homeworkProblemCounter}{1}
\nobreak\extramarks{Problem \arabic{homeworkProblemCounter}}{}\nobreak{}

%
% Homework Problem Environment
%
% This environment takes an optional argument. When given, it will adjust the
% problem counter. This is useful for when the problems given for your
% assignment aren't sequential. See the last 3 problems of this template for an
% example.
%
\newenvironment{homeworkProblem}[1][-1]{
    \ifnum#1>0
        \setcounter{homeworkProblemCounter}{#1}
    \fi
    \section{Problem \arabic{homeworkProblemCounter}}
    \setcounter{partCounter}{1}
    \enterProblemHeader{homeworkProblemCounter}
}{
    \exitProblemHeader{homeworkProblemCounter}
}

%
% Homework Details
%   - Title
%   - Due date
%   - Class
%   - Section/Time
%   - Instructor
%   - Author
%

\newcommand{\hmwkClass}{CS 219}
\newcommand{\hmwkTitle}{Homework\ \#7}
\newcommand{\hmwkDueDate}{October 19th, 2016}
\newcommand{\hmwkDueTime}{4:00pm}
\newcommand{\hmwkClassInstructor}{Dr. Egbert}
\newcommand{\hmwkAuthorName}{Matthew J. Berger}

%
% Title Page
%

\title{
    \vspace{2in}
    \textmd{\textbf{\hmwkClass:\ \hmwkTitle}}\\
    \normalsize\vspace{0.1in}\small{Due\ on\ \hmwkDueDate\ at \hmwkDueTime}\\
    \vspace{0.1in}\large{\textit{\hmwkClassInstructor}}
    \vspace{3in}
}

\author{\textbf{\hmwkAuthorName}}
\date{}

\renewcommand{\part}[1]{\textbf{\large Part \Alph{partCounter}}\stepcounter{partCounter}\\}

%
% Various Helper Commands
%

% Useful for algorithms
\newcommand{\alg}[1]{\textsc{\bfseries \footnotesize #1}}

% Alias for the Solution section header
\newcommand{\solution}{\textbf{\large Solution:}}

% Alias for answers
\newcommand{\answer}{\textbf{\large Answer: }}

\begin{document}

\maketitle

\pagebreak

\begin{homeworkProblem}[12]
	\begin{itemize}
		\item[12.6)] Compare zero-, one-, two-, and three-address machines by writing programs to compute $X=\frac{(A+B \times C)}{(D-E \times F)}$ for each of the four machines. The instructions available for use are as follows:
		\item[]\begin{table}[h]\hfil
			\def\arraystretch{1.5}%
			\begin{tabular}{|l|l|l|l|}
				\hline
				0 Address & 1 Address & 2 Address & 3 Address \\
				\hline
				PUSH M & LOAD M  & MOVE(X $\leftarrow$ Y)            & MOVE(X $\leftarrow$ Y) \\
				\hline
				POP M  & STORE M & ADD(X $\leftarrow$ X + Y)         & ADD(X $\leftarrow$ Y + Z) \\
				\hline
				ADD    & ADD M   & SUB(X $\leftarrow$ X - Y)         & ADD(X $\leftarrow$ Y - Z) \\
				\hline
				SUB    & SUB M   & MUL(X $\leftarrow$ X $\times$ Y ) & MUL(X $\leftarrow$ Y $\times$ Z) \\
				\hline
				MUL    & MUL M   & DIV(X $\leftarrow \frac{X}{Y})$   & DIV(X $\leftarrow \frac{Y}{Z}$) \\
				\hline
				DIV    & DIV M   & & \\
				\hline
			\end{tabular}
		\end{table}

		\solution

		\item[]\begin{table}[h]\hfil
			\def\arraystretch{1.5}%
			\begin{tabular}{|l|l|l|l|}
				\hline
				0 Address & 1 Address & 2 Address & 3 Address \\
				\hline
				PUSH B & LOAD  F & MUL B,C & MUL B,C \\
				PUSH C & MUL   E & ADD A,B & ADD X,A,B \\
				MUL    & STORE X & MUL E,F & MUL E,F \\
				PUSH A & LOAD  D & SUB D,E & SUB D,E \\
				ADD    & SUB   X & DIV A,D & DIV X,D \\
				PUSH D & STORE X & & \\
				PUSH E & LOAD  C & & \\
				PUSH F & MUL   B & & \\
				MUL    & ADD   A & & \\
				SUB    & DIV   X & & \\
				DIV    & & & \\
				POP  X & & & \\

				\hline

			\end{tabular}
		\end{table}

		\pagebreak

		\item [12.7)] Consider a hypothetical computer with an instruction set of only two n-bit instructions. The first bit specifies the opcode, and the remaining bits specify one of the 2n-1 n-bit words of main memory. The two instructions are as follows:
		\begin{itemize}
			\item \textbf{SUBS X} = Subtract the contents of location X from the accumulator, and store the result in location X and the accumulator.
			\item \textbf{JUMP X} = Place address X in the program counter.
		\end{itemize}
		A word in main memory may contain either an instruction or a binary number in twos complement notation. Demonstrate that this instruction repertoire is reasonably complete by specifying how the following operations can be programmed. Pick any two from a.) to e.).
		\begin{enumerate}[label=\alph*)]
			\item Data transfer: Location X to accumulator, accumulator to location X.
			\item Addition: Add contents of location X to accumulator.
			\item Conditional branch.
			\item Logical OR.
			\item I/O Operations.
		\end{enumerate}

		\solution

		\begin{itemize}
			\item[a.)] \underline{\textbf{Data Transfer:}} Location X to accumulator, accumulator to location X. Y,Z are used as scratch space left at zero. X is moved to the accumulator using:
			\begin{enumerate}[label=\arabic*.)]
				\item subs Z
				\item subs Z
				\item subs X
				\item subs Z
				\item subs Z
				\item subs X
			\end{enumerate}
		    and the accumulator to X using:
			\begin{enumerate}[label=\arabic*.)]
				\item subs Z
				\item subs X
				\item subs X
				\item subs Z
				\item subs X
				\item subs Z
				\item subs Z
				\item subs X
			\end{enumerate}
			\pagebreak
			\item[b.)] \underline{\textbf{Addition:}} Add contents of location X to accumulator.
			Addition will be equivalent to the negative accumulator minus X.
			\begin{enumerate}[label=\arabic*.)]
				\item subs Y
				\item subs Z
				\item subs Z
				\item subs X
				\item subs Y
				\item subs Z
				\item subs Z
				\item subs X
				\item subs Z
				\item subs Z
				\item subs Y
			\end{enumerate}

		\end{itemize}

	\end{itemize}
\end{homeworkProblem}

\begin{homeworkProblem}[13]
	\begin{itemize}
		\item[13.1)] Given the following memory values and a one-address machine with an accumulator, what values do the following instructions load into the accumulator?\\

			Word 20 contains 40.\\
			Word 30 contains 50.\\
			Word 40 contains 60.\\
			Word 50 contains 70.
		\begin{enumerate}[label=\alph*.)]
			\item LOAD IMMEDIATE 20
			\item LOAD DIRECT 20
			\item LOAD INDIRECT 20
			\item LOAD IMMEDIATE 30
			\item LOAD DIRECT 30
			\item LOAD INDIRECT 30
		\end{enumerate}

		\solution

		\begin{enumerate}[label=\alph*.)]
			\item 20
			\item 40
			\item 60
			\item 30
			\item 50
			\item 70
		\end{enumerate}

		\pagebreak

		\item[13.3)] An address field in an instruction contains decimal value 14. Where is the corresponding operand located for:
		\begin{itemize}
			\item immediate addressing?
			\item direct addressing?
			\item indirect addressing?
			\item register addressing?
			\item register indirect addressing?
		\end{itemize}

		\solution

		\begin{itemize}
			\item The address field
			\item Memory location 14
			\item The memory location whose address is in the memory location 14
			\item Register 14
			\item The memory location whose address is in register 14
		\end{itemize}

		\item[13.5)] A PC-relative mode branch instruction is 3-bytes long. The address of the instruction, in decimal, is 256028. Determine the branch target address if the signed displacement in the instruction is -31.

		\solution

		The next instruction is at \underline{256031} and the branch is \underline{-31}, so the next address is $256031-31=\underline{\textbf{256000}}$.

		\item[13.7)] How many times does the processor need to refer to memory when it fetches and executes an indirect-address-mode instruction is:
			\begin{enumerate}[label=\alph*)]
				\item A computation requiring a single operand
				\item A branch
			\end{enumerate}

		\solution
			\begin{enumerate}[label=\alph*)]
				\item Fetch instruction, Fetch effective address, Fetch operand. \underline{\textbf{3 memory references}}
				\item Fetch instruction, Fetch effective address and transfer to PC. \underline{\textbf{2 memory references}}
			\end{enumerate}

		\item[13.11)] Consider a processor that includes a base with indexing addressing mode. Suppose an instruction is encountered that employs this addressing mode and specifies a displacement of 1970, in decimal. Currently the base and index register contain the decimal numbers 48,022 and 8, respectively. What is the address of the operand?

		\solution

		Base + index + signed displacement = $48,022 + 8 + 1970 = 50,000_{10} =$ \underline{\textbf{0xC350}}
	\end{itemize}
\end{homeworkProblem}

\end{document}


\documentclass[a4paper,12pt]{article}
%\documentclass[a4paper,12pt]{scrartcl}

\usepackage{xltxtra}

\input{../preamble.tex}

% \usepackage[spanish]{babel}
% \decimalpoint

% \setromanfont[Mapping=tex-text]{Linux Libertine O}
% \setsansfont[Mapping=tex-text]{DejaVu Sans}
% \setmonofont[Mapping=tex-text]{DejaVu Sans Mono}

\title{Homework \#05: Continuous Random Variable}
\author{Isaac Ayala Lozano}
\date{2020-02-03}

\begin{document}
\maketitle

\section{Problem}
\begin{enumerate}
 \item Plot the probability distribution functions for the Exponential and Gamma Distributions.
 Consider $\lambda \in \{0.5, 1, 2\}$ for the Exponential Distribution and $\alpha \in \{2, 3, 7, 7\}, \lambda \in \{0.5, 2, 2, 1\}$ for the Gamma Distribution.
 \item Provide proof that the Gamma distribution is indeed a probability density function.
 \item Relate the Uniform, Exponenial and Gamma Distributions to physical phenomena or engineering problems.
\end{enumerate}

\pagebreak

\section{Solution}

Plots are presented for the Probability Density Function of the Exponential (figure \ref{fig: exp}) and Gamma (figure \ref{fig: gamma}) Distributions.

\begin{figure}[htb!]
\centering
\import{./img/}{hw05_exp.tex}
\caption{Exponential Distribution.}
\label{fig: exp}
\end{figure}

\begin{figure}[htb!]
\centering
\import{./img/}{hw05_gamma.tex}
\caption{Gamma Distribution.}
\label{fig: gamma}
\end{figure}

\pagebreak

We provide proof that the Gamma Distribution probability function is indeed a probability density function.
From \cite{stewart2009probability}, the Gamma Distribution is defined as

\begin{equation}
 f(x) \equiv f(x;\beta, \alpha) = \dfrac{1}{\beta^\alpha \Gamma(\alpha)} x^{\alpha-1} \exp(-x/\beta), \quad x>0
\end{equation}

By establishing $\alpha = r$ and $\beta = 1/ \mu$, the function can be rewritten as

\begin{equation}
 f(x) = \dfrac{\mu (\mu x)^{r-1} \exp(-\mu x)}{\Gamma(r)}
\end{equation}

this form is also known as the \emph{Erlang-r} distribution.
Integrating this new expression we have the following

\begin{align}
 \int_0^\infty f(x)dx &= \int_0^\infty \dfrac{\mu (\mu x)^{r-1} \exp(-\mu x)}{\Gamma(r)}dx\\
 u &= \mu x \\
 du &= \mu dx\\
 &= \dfrac{1}{\Gamma(r)} \int_0^\infty (u)^{r-1} \exp(-u) du
\end{align}

Recall the definition of the Gamma function

\begin{equation}
 \Gamma(\alpha) = \int_0^\infty y^{\alpha-1}exp(-y) dy
\end{equation}

Thus the equation is simplified as

\begin{align}
 \int_0^\infty f(x)dx &= \dfrac{\Gamma(r)}{\Gamma(r)}\\
 &= 1
\end{align}

We present examples of the three distributions studied.

\begin{itemize}
 \item Uniform Distribution. Events in which all results have the same probability of occurring such as the odds of any number of a six sided die being rolled or the result of a coin toss.
 \item Exponential Distribution. Events related to time and events, such as number of number of calls received during an hour, rainfall in a year, distribution of gas molecules at fixed temperature and pressure.
 \item Gamma Distribution. Also used for events related to time and rate, such as the amount of customers arriving to a restaurant in a specific time interval. Consider a restaurant where customers arrive at x customer per hour, what is the probability of k customers arriving in a given time interval?
\end{itemize}

\printbibliography

\pagebreak
\appendix
\section{Octave Code}
\lstinputlisting[language=Matlab]{hw05_plots.m}

\end{document}


\documentclass[a4paper, 11pt]{article}
\usepackage{comment} % enables the use of multi-line comments (\ifx \fi)
\usepackage{lipsum} %This package just generates Lorem Ipsum filler text.
\usepackage{fullpage} % changes the margin
\usepackage{amsmath} %Always include this.

\begin{document}
%Header-Make sure you update this information!!!!
\noindent
\large\textbf{Term Project} \hfill \textbf{FirstName LastName} \\
\normalsize Mth 327 \hfill Teammates: Student1, Student2 \\
Prof. Olimb \hfill Due  Date: XX/XX/XX \\

\section*{Executive Summary}
Put your problem statement here!\\
 Example of a Citation\cite[p.219]{Robotics}. Here's Another Citation\cite{Flueck}

\section*{Background/Theory} %Label section titles approportate to your project.
\lipsum[2]

\section*{Assumptions and Justification} %Label section titles approportate to your project.
\lipsum[2]

\section*{Methodology and Results}
For reference, here is a matrix\\
$A =\begin{matrix}1&2\\3&4\end{matrix}$\\
and here is a centered matrix with brackets.
\begin{center}$A =\begin{bmatrix}1&2\\3&4\end{bmatrix}$\end{center}

\lipsum[3]
% to comment sections out, use the command \ifx and \fi. Use this technique when writing your pre lab. For example, to comment something out I would do:
%  \ifx
%	\begin{itemize}
%		\item item1
%		\item item2
%	\end{itemize}
%  \fi

\section*{Analysis \& Testing} %This section may not be needed.
\lipsum[6]

\section*{Summary}
\lipsum[7]

\section*{Attachments}
\begin{itemize}
\item Attachment 1
\end{itemize}

\begin{thebibliography}{9}
\bibitem{Robotics} Fred G. Martin \emph{Robotics Explorations: A Hands-On Introduction to Engineering}. New Jersey: Prentice Hall.
\bibitem{Flueck}  Flueck, Alexander J. 2005. \emph{ECE 100}[online]. Chicago: Illinois Institute of Technology, Electrical and Computer Engineering Department, 2005 [cited 30
August 2005]. Available from World Wide Web: (http://www.ece.iit.edu/~flueck/ece100).
\end{thebibliography}

\end{document}


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% UMB-CS240-2016S: Programming in C
% Copyright 2016 Pejman Ghorbanzade <pejman@ghorbanzade.com>
% Creative Commons Attribution-ShareAlike 4.0 International License
% More info: https://github.com/ghorbanzade/UMB-CS240-2016S
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\section*{Question 4}

Since fall of You-Know-Who, Parvati Patil has been working as payroll clerk at Ministry of Magic.
She is responsible for preparing and reporting of payroll checks.
To prevent fraud and curruption, the Ministry has prohibited any use of magic in department of Human Resources.
Parvati is therefore required to calculate all payrolls as Muggles do.
Given the number of employees working at the Ministry, she has been thinking to write a software that automates payroll calculations for all employees.

Depending on their status, employees have different pay rates in the Ministry as given in Table \ref{tablequestion5}.
As is shown, part time and interns are paid by hour whereas full time employees are paid regardless of the number of hours they have worked.

You are asked to help in design of the payroll system for the Ministry.
Each employee has a name, an ID, the number of hours he has worked in a month and a status which is either full time, part time or intern.

\begin{enumerate}
\item Define a structure \texttt{employee} to enclose required information for a given employee.
\item Write a function that takes data of an employee and returns an initialized employee structure.
\item Write a function that takes an employee structure and calculates the monthly pay for that employee.
\item Write a function that takes an array of employee structures and returns the total amount to be paid to those employees.
\end{enumerate}

\begin{table}[H] \centering
\begin{tabular}{r c}
\textbf{Status} & \textbf{Rate}\\
\hline
Full Time & 800 Galleons / Month\\
Part Time & 4 Galleons / Hour\\
Intern & 2 Galleons / Hour
\end{tabular}
\caption{Pay rates for employees of Ministry of Magic}
\label{tablequestion5}
\end{table}


\section{Practical FEFF}

\begin{slide}{ Practical {\feff} }

  \begin{cenpage}{120mm}

{\RedEmph{Good News}}: you don't have to worry about the hard parts (mostly)!

\begin{enumerate}
\item Start with a structure close to the {\BlueEmph{local}} atomic structure
  of your sample, and generate x,y,z coordinates for the atoms. Often a
  crystal structure is close -- it does not have to be perfect!

\item Run {\feff}.  This creates a {\feffndat} files for each path.

\item Use these  Path Files in to model measured XAFS.
\end{enumerate}

  {\larch} and {\xasviewer} helps you find crystal structures, create and
  edit input filess, run {\feff}, and sort and use the results.

\begin{columns}
      \begin{column}[T]{88mm}
        \begin{block}{ run {\feff} in a specified folder  }
          \begin{semiverbatim} {\footnotesize larch>
{\LFunc{feff8l}}({\LString{'feff8.inp'}}, folder={\LString{'CuS\_Feff'}}) }
          \end{semiverbatim}
        \end{block}

      \end{column}
      \begin{column}[T]{1mm} \end{column}
    \end{columns}

    \vmm\vmm

  Having good starting structures can be important, but you do not need the
  exact structure to use {\feff}.

\vmm

  You can (may need to) mix structural models to model real data.

  \end{cenpage}

\end{slide}

\subsection{Anatomy of {\file{feff.inp}}}
\begin{frame}[fragile] \frametitle{Anatomy of {\file{feff.inp}}}

  \begin{cenpage}{135mm}
    \begin{cenpage}{100mm}
      {\feff} is a very old program that runs from an input file -- it
      {\bf{must}} be called {\file{feff.inp}}.   {\larch} can help with this.
      \vmm

      Each calculation should be in its own folder/subdirectory.
    \end{cenpage}

\begin{tabular}{lr}
  \begin{CodeBlock}{48mm}{\file{feff.inp} file ({\feff} 8):}
{\Blue{TITLE    FeO, rock salt structure}}
{\Blue{EDGE K}}
{\Blue{S02   1.0}}
{\Blue{CONTROL  1 1 1 1 1 1}}  {\Red{\# which parts of code to run}}
{\Blue{PRINT    1 0 0 0 0 3}}  {\Red{\# which output files to write}}
{\Blue{RPATH   6.0}}   {\Red{\# How far in R to build paths}}

{\Blue{POTENTIALS }}      {\Red{\# list of Atomic Potentials}}
* potential  z  label
      0     26  Fe   {\Red{\# Absorbing Atom}}
      1      8  O    {\Red{\# 1 Potential for each Z}}
      2     26  Fe

{\Blue{ATOMS}}  {\Red{\# list of Atomic X, Y, Z, Potential }}
 0.00000     0.00000     0.00000    0   Fe
 0.00000     0.00000    -2.13870    1   O
-2.13870     0.00000     0.00000    1   O
 0.00000    -2.13870     0.00000    1   O
 0.00000     0.00000     2.13870    1   O
 2.13870     0.00000     0.00000    1   O
 0.00000     2.13870     0.00000    1   O
 0.00000     2.13870     2.13870    2   Fe
 0.00000    -2.13870    -2.13870    2   Fe
\end{CodeBlock} &
\begin{minipage}{67mm}

\onslide<2->

{\file{feff.inp}} includes:

\vmm

\begin{enumerate}
  \onslide<2->\item A list of unique Atomic Potentials:
  \begin{itemize}
  \item 1 Absorbing Atom, always Potential 0
  \item 1 Potential per atomic species (Z)
  \end{itemize}

  \onslide<2->\item List of atomic coordinates (in {\AA}):

  {\hmm\hmm}  $x$, $y$, $z$, $Pot$

  for each atom in the cluster of atoms

  \vmm
  The cluster can be non-crystalline.

  \vmm
  The absorber can be at (0, 0, 0), or not.

  \vmm
  Hint:  better off removing H atoms!

\end{enumerate}
\end{minipage}\\
\end{tabular}

  \onslide<2->
   \begin{postitbox}{90mm}
     \Red{You can edit the atomic positions, and add or change potentials.}
   \end{postitbox}

 \end{cenpage}
\end{frame}

\begin{slide}{Input Parameters for {\file{feff.inp}  ({\feff} 8)}}

\begin{cenpage}{125mm}
  {\feff} has many  inputs, but only a few of them are really
  important for EXAFS Analysis (some are {\Red{\texttt{required}}}, some {\Blue{\texttt{optional}}}):

  \begin{description}[POTENTIALSXX]
  \item[{\Red{\texttt{EDGE}}}]   which edge  absorbs  the x-ray ({\tt{K}}, {\tt{L3}}, etc.)

  \item[{\Red{\texttt{POTENTIALS}}}]   list of atomic potentials (0 = absorbing atom)

  \item[{\Red{\texttt{ATOMS}}}]   list of atomic $x$, $y$, $z$, Potential

  \item[{\Red{\texttt{CONTROL}}}]   which ``Modules'' to run.  Use  ``1 1 1 1 1 1''.

  \item[{\Red{\texttt{PRINT}}}]   which ``Outputs'' to write. Use  ``1 0 0  0 0 3''.

  \item[{\Blue{\texttt{RPATH}}}]   how far out (in {\AA}) to consider the  cluster of atoms.

  \item[{\Blue{\texttt{POLARIZATION}}}]   polarization vector of incident
    x-ray (in same coordinate system as atomic coordinates)

  \item[{\Blue{\texttt{EXCHANGE}}}]   which model to use for the exchange energy.
    Use the default (Hedin-Lundqvist model) unless you know why.

  \end{description}

\end{cenpage}

\end{slide}

\subsection{ Converting CIF data into {\feff} inputs}
\begin{frame}[fragile] \frametitle{Converting CIF data into {\feff} inputs}

\begin{cenpage}{125mm}

  {\larch} comes with about 9000 crystral structures from the

  \begin{cenpage}{75mm}
    American Mineralogist Crystal Structure Database
  \end{cenpage}

  and can convert these as {\RedEmph{Crystallographic Information Format}}
  (CIF) files to {\feff} input files.

  \vmm
  It handles fractional site occupancy with weighted substitutions -- use
  with caution!

  \vmm
  It can also read some CIF files from other sources (work-in-progress!).

  \vmm
  This is a convenient way to generate a {\feff} input, but not necessary!

\begin{postitbox}{84mm}
  The cluster used by {\feff} does not need to be a crystal!
\end{postitbox}

\vmm
Atomic clusters may also come from:

\vmm
Protein Data Bank, Molecular Dynamics Simulations,
Density Functional Theory Calculations, \ldots.

\end{cenpage}
\end{frame}

\begin{slide}{{\feff}:  what {\em{should}} you worry about?}

  \begin{cenpage}{125mm}

  Using {\feff} in a real analysis, we'll be {\BlueEmph{refining}}
  bond lengths and coordination numbers.  For this to work, here are some
  ``rules of thumb'':

\begin{enumerate}\setlength{\parskip}{3mm}
   \item refining distances by {\bf{more than $0.1\rm\,\AA$}} probably means
  the calculation should be re-run -- the overlap of atomic potentials may
  not be accurate.

   \item  refining energy origins $E_0$ by {\bf{more than $10\rm\,eV$}} may
  mean the calculation -- or the selection of $E_0$ in the data reduction
  -- is wrong or needs to be re-examined.

 \item  {\feff} allows you set $S_0^2$ and  $\sigma^2$ values in
   {\feffinp}.  {\bf{Do not set these in {\feffinp}}}.

\item You {\bf{should set $S_0^2$}} in the analysis for a particular group
  of data (central atom, beamline energy resolution, etc), based on an
  experimental standard, or have uncertain values for $N$.

\end{enumerate}

\end{cenpage}

\end{slide}


\documentclass[a4paper,11pt,reqno]{amsart}

% --------------------------------------------------------
% Packages
% --------------------------------------------------------
\usepackage[utf8]{inputenc}
\usepackage[foot]{amsaddr}
\usepackage{amsmath,amsfonts,amssymb,amsthm,mathrsfs,bm}
\usepackage[margin=0.95in]{geometry}
\usepackage{color}
\usepackage[dvipsnames]{xcolor}
\usepackage{mathtools,graphicx}
\usepackage{tcolorbox}
\usepackage{listings}
\usepackage{textcomp}
\usepackage{hyperref}

% --------------------------------------------------------
% Custom Colours
% --------------------------------------------------------
\definecolor{CommentGreen}{rgb}{0.0,0.4,0.0}
\definecolor{Background}{rgb}{0.9,1.0,0.85}
\definecolor{lrow}{rgb}{0.914,0.918,0.922}
\definecolor{drow}{rgb}{0.725,0.745,0.769}

% --------------------------------------------------------
% Typesetting Python code
% --------------------------------------------------------
\lstloadlanguages{Python}%
\lstset{
    language=Python, upquote=true, frame=single,
    basicstyle=\small\ttfamily,
    backgroundcolor=\color{yellow!30},
    keywordstyle=[1]\color{NavyBlue}\bfseries,
    keywordstyle=[2]\color{RubineRed},
    keywordstyle=[3]\color{orange!90}\bfseries,
    keywordstyle=[4]\color{Green!90}\bfseries,
    identifierstyle=,
    commentstyle=\usefont{T1}{pcr}{m}{sl}\color{MidnightBlue}\small,
    stringstyle=\color{purple},
    showstringspaces=false, tabsize=4, morekeywords={import,as},
    morekeywords=[2]{args,__init__},
    morekeywords=[3]{@property},
    morekeywords=[4]{self},
    morecomment=[l][\color{blue}]{...},
    numbers=none, firstnumber=1,
    numberstyle=\tiny\color{blue},
    stepnumber=1, xleftmargin=10pt, xrightmargin=10pt
}

\synctex=1

\hypersetup{
    unicode=false, pdftoolbar=true,
    pdfmenubar=true, pdffitwindow=false, pdfstartview={FitH},
    pdftitle={ELE2024 Coursework}, pdfauthor={A. Author},
    pdfsubject={ELE2024 coursework}, pdfcreator={A. Author},
    pdfproducer={ELE2024}, pdfnewwindow=true,
    colorlinks=true, linkcolor=red,
    citecolor=blue, filecolor=magenta, urlcolor=cyan
}

% --------------------------------------------------------
% CUSTOM COMMANDS
% --------------------------------------------------------
\renewcommand{\Re}{\mathbf{re}}
\renewcommand{\Im}{\mathbf{im}}
\newcommand{\R}{\mathbb{R}}
\newcommand{\N}{\mathbb{N}}
\newcommand{\C}{\mathbb{C}}
\newcommand{\lap}{\mathscr{L}}
\newcommand{\dd}{\mathrm{d}}
\newcommand{\smallmat}[1]{\left[ \begin{smallmatrix}#1 \end{smallmatrix} \right]}

% --------------------------------------------------------
% Opening: Title and Author Names
%          Modify this section
% --------------------------------------------------------
\title[ELE2024 Coursework]{LaTeX template for the ELE2024 coursework}

\author[F. Author]{First Author}
\author[A. Someone]{Andreas Someone}
\address[F. Author and A. Someone]{Email addresses: \href{mailto:f.author@qub.ac.uk}%
{f.author@qub.ac.uk} and
\href{mailto:a.someone.ac.uk}{a.someone@qub.ac.uk}.}
\thanks{Some note goes here.  Version 0.0.1. Last updated:~\today.}

% --------------------------------------------------------
% Beginning of your document
% --------------------------------------------------------
\begin{document}

\maketitle

\section{Part A}

\subsection{Question Q1}\label{sec:q1}
You may format inline equations using the dollar sign
like this $x = 1 = \alpha$ and $y = x^2 - \sqrt{z}$.
Equations are like this:
\begin{align}
 \label{eq:lti_state_update}
 x_{k+1} = A x_k + Bu_k.
\end{align}
Here is an equation with the Laplace transform
\begin{equation}
    \lap \{e^{at}\} = \frac{1}{s-a},
\end{equation}
for all complex numbers $s\in\C$ with $\Re(s)>a$.
The inverse Laplace transform is denoted like this $\lap^{-1}$.

\subsection{Question Q2}
Refer to other sections as Section~\ref{sec:q1}. An example of a numbered list
\begin{enumerate}
 \item first item,
 \item second item.
\end{enumerate}
Links are \href{https://google.com}{like this}. We also have \textbf{boldface}, \textit{italics},
\emph{emphasised}, \texttt{truetype}, \textsc{Small Caps} and so on.

\subsection{More math} Denote the real numbers as $\R$ and the complex numbers
as $\C$. Example of a limit:
\begin{equation}
    z = \lim_{s\to0^+}\frac{s+1}{s^3+s^2-5s+9}.
\end{equation}
Another example
\begin{equation}
    \lim_{s\to\infty} \frac{s+1}{s^3+s^2-5s+9}.
\end{equation}
Example of an integral
\begin{equation}
    \int_0^\infty e^{-s\tau}f(\tau)\dd\tau.
\end{equation}
Three aligned equations
\begin{align}
    a =& 1,
    \\
    b =& 2,
    \\
    c =& 3.
\end{align}
Two aligned equations without equation numbers
\begin{align*}
    a =& 1,
    \\
    b =& 2.
\end{align*}
Mathematical derivations aligned at the ``='' sign:
\begin{align}
    \frac{1}{2+3j} {}={}& \frac{2-3j}{(2+3j)(2-3j)}
    \notag\\
    {}={}& \frac{2-3j}{2^2 + 3^2}
    \notag\\
    {}={}& \frac{2-3j}{13}
    \notag\\
    {}={}&\frac{2}{13} - j\frac{3}{13}.
\end{align}
More mathematical derivations:
\begin{align*}
 & as + 4 + 2s = b + (8+a)s
\\
 \Leftrightarrow{}& (a+2)s + 4 = b + (8+a)s
\\
 \Leftrightarrow{}& (a+2)s - (8+a)s = b - 4.
\end{align*}
Boldface math: $\bm{x}$. Vectors:
\begin{equation}
\bm{x} =
    \begin{bmatrix}
    x_1
    \\
    x_2
    \\
    x_3
    \end{bmatrix}.
\end{equation}
Another example: According to Taylor's Theorem:
\begin{equation}
    \phi(x) \approx \phi(x_0) + \phi'(x_0)(x-x_0).
\end{equation}
Partial derivatives:
\begin{equation}
 \label{eq:partial_derivatives}
 \frac{\partial f(x, y)}{\partial x} = x^2\cos(xy).
\end{equation}

\begin{tcolorbox}[title={Common typesetting mistakes}]
\textbf{Important Note:}
Write $\cos x$, not $cos x$.
Write $\sin x$, $\log x$, etc --- not $sin x$, $log x$ and so on.
Write $\lim_{s\to 0^+} sF(s)$, not $lim_{s\to 0^+} sF(s)$.
Write $F(s)$, not $F(S)$.
Write $xy$, or $x\dot y$, but not $x*y$ --- that would be the \textit{convolution} of $x$ with $y$, not their product, so
\begin{equation}
 2 * 3 = 3t^2.
\end{equation}
To denote a variable with a subscript, write $x_1$, not $x1$.
For superscripts, write $x^2$, not $x2$.
Denote a variable by $x$, not x.
Double quotes are ``like this'', not "like this".
Reference equations using: Equation \eqref{eq:partial_derivatives};
not Equation \ref{eq:partial_derivatives} and not Equation (12).
\end{tcolorbox}

% You can write comments like this
\begin{figure}
 \centering
 \includegraphics[width=0.65\linewidth]{figures/lateral_position}
 \caption{You may of course include figures in your document. Make sure your figures have
          legible axis labels. Figures of about this size are perfectly legible.}
\end{figure}

\end{document}


\documentclass[12pt]{article}
\usepackage{graphics}
\begin{document}

\section*{NYU Engineering Physics 1---In-class Exam 3}

\vfill

\paragraph{Name:} ~

\paragraph{email:} ~

\vfill

This exam consists of two problems.  Write only in this booklet.  Be
sure to show your work.

\vfill ~

\clearpage

\section*{Problem 1}

In class we showed that a circular object of mass $m$, radius $R$ and
moment of inertia $I$ rolls without slipping down an inclined plane
(angle $\theta$ to the horizontal) with acceleration
$$
a=\frac{g\,\sin\theta}{1+\frac{I}{m\,R^2}} \quad .
$$ What is the force of friction $\vec{f}$ (magnitude and direction)
acting on the rolling object?

\vfill

Now compare a thin hoop with a uniform disk, both of the same mass and
radius, rolling down the inclined plane.  Which is subject to the
greater frictional force?  \emph{Note that you do not have to know the
exact moment of inertia expressions---nor do you have to get the first
part correct---in order to get this part correct.}

\vfill ~

\clearpage

\section*{Problem 2}

~\hfill\includegraphics{../mp/stick_n_force.eps}\hfill~\\ A stick of
mass $m$ and length $\ell$, initially at rest, experiences a force $F$
at one end, as shown, for a \emph{very short} time $\Delta t$.  This
impulse causes the stick to move (translate) and to rotate.  At what
angular speed $\omega$ does the stick end up rotating around its
center of mass?  Its moment of inertia is $(1/12)\,m\,\ell^2$.
\emph{Assume this is happening in outer space; there is no gravity or
friction.}

\clearpage

\centerline{\textsl{[this page intentionally left blank]}}

\end{document}