\documentclass{article}

\usepackage{graphicx}
\usepackage{setspace}
\usepackage{listings}
\usepackage{color}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{float}
\usepackage[justification=centering]{caption}
\usepackage{subcaption}
\usepackage[margin=0.75in]{geometry}
\usepackage[parfill]{parskip} 
\usepackage{fancyhdr}
\usepackage{titlesec}
\usepackage{lipsum}
\usepackage{enumitem}
\usepackage{cancel}

\titleformat{\subsection}[runin]{\normalfont \large \bfseries}
    {\thesubsection}{1em}{}

\renewcommand{\thesubsection}{\indent(\alph{subsection})}

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


    \definecolor{mygreen}{RGB}{28,172,0} % color values Red, Green, Blue
    \definecolor{mylilas}{RGB}{170,55,241}


    \lstset{language=Matlab,%
        %basicstyle=\color{red},
        breaklines=true,%
        morekeywords={matlab2tikz},
        keywordstyle=\color{blue},%
        morekeywords=[2]{1}, keywordstyle=[2]{\color{black}},
        identifierstyle=\color{black},%
        stringstyle=\color{mylilas},
        commentstyle=\color{mygreen},%
        showstringspaces=false,%without this there will be a symbol in the places where there is a space
        numbers=left,%
        numberstyle={\tiny \color{black}},% size of th_sume numbers
        numbersep=9pt, % this defines how far the numbers are from the text
        emph=[1]{for,end,break},emphstyle=[1]\color{red}, %some words to emphasise
        %emph=[2]{word1,word2}, emphstyle=[2]{style},    
        xleftmargin=2em
    }
    %\lstset{basicstyle=\small,
    %        keywordstyle=\color{mauve},
    %        identifierstyle=\color{dkgreen},
    %        stringstyle=\color{gray},
    %        numbers=left,
    %        xleftmargin=5em
    %        }


\title{ECE 456 - Problem Set 2}
\date{2021-03-01}
\author{David Lenfesty \\ lenfesty@ualberta.ca
    \and Phillip Kirwin \\ pkirwin@ualberta.ca}
    
\pagestyle{fancy}

\fancyhead[L]{\textbf{ECE 456} - Problem Set 2}
\fancyhead[R]{David Lenfesty and Phillip Kirwin}
\fancyfoot[C]{Page \thepage}
\renewcommand{\headrulewidth}{1pt}
\renewcommand{\footrulewidth}{1pt}

\begin{document}
    \doublespacing
    \maketitle
    \thispagestyle{empty}
    \singlespacing
    \newpage
    hello
    \section*{Problem 1}

        \begin{enumerate}[align=left,leftmargin=*,labelsep=1em,label=\bfseries(\alph*)] 
            \item %1a
            Code: 
            \begin{lstlisting}[language=Matlab]
clear all;
%physical constants in MKS units

hbar = 1.054e-34;
q = 1.602e-19;
m = 9.110e-31;

%generate lattice

N  = 100;                   %number of lattice points
n  = [1:N];                 %lattice points
a  = 1e-10;                 %lattice constant
x  = a * n;                 %x-coordinates
t0 = (hbar^2)/(2*m*a^2)/q;  %encapsulating factor
L  = a * (N+1);             %total length of consideration

%set up Hamiltonian matrix 

U = 0*x; %0 potential at all x
main_diag  = diag(2*t0*ones(1,N)+U,0); %create main diagonal matrix
lower_diag = diag(-t0*ones(1,N-1),-1); %create lower diagonal matrix
upper_diag = diag(-t0*ones(1,N-1),+1); %create upper diagonal matrix

H = main_diag + lower_diag + upper_diag; %sum to get Hamiltonian matrix

[eigenvectors,E_diag] = eig(H); %"eigenvectors" is a matrix wherein each 
                                %column is an eigenvector
                                %"E_diag" is a diagonal matrix where the
                                %corresponding eigenvalues are on the
                                %diagonal.
                                
E_col = diag(E_diag); %folds E_diag into a column vector of eigenvalues

% return eigenvectors for the 1st and 50th eigenvalues

phi_1  = eigenvectors(:,1);
phi_50 = eigenvectors(:,50);

% find the probability densities of position for 1st and 50th eigenvectors

P_1 = phi_1 .* conj(phi_1);
P_50 = phi_50 .* conj(phi_50);

% Find first N analytic eigenvalues
E_col_analytic = (1/q) * (hbar^2 * pi^2 * n.*n) / (2*m*L^2);

% Plot the probability densities for 1st and 50th eigenvectors

figure(1); clf; h = plot(x,P_1,'kx',x,P_50,'k-');
grid on; set(h,'linewidth',[2.0]); set(gca,'Fontsize',[18]);
xlabel('POSITION [m]'); ylabel('PROBABILITY DENSITY [1/m]');
legend('n=1','n=50');

% Plot numerical eigenvalues
figure(2); clf; h = plot(n,E_col,'kx'); grid on;
set(h,'linewidth',[2.0]); set(gca,'Fontsize',[18]);
xlabel('EIGENVALUE NUMBER'); ylabel('ENERGY [eV]');
axis([0 100 0 40]);

% Add analytic eigenvalues to above plot

hold on;
plot(n,E_col_analytic,'k-');
legend({'Numerical','Analytical'},'Location','northwest');

            \end{lstlisting}
            %
            \begin{figure}[H]
                \centering
                \begin{subfigure}{0.4\linewidth}
                    \centering
                    \includegraphics[width=\textwidth]{q1a_1and50.png}
                    \caption{probability densities for \(n=1\)\\and \(n=50\).}
                    \label{fig:q3_ne}
                \end{subfigure}%
                \begin{subfigure}{0.4\linewidth}
                    \centering
                    \includegraphics[width=\textwidth]{q1a_eigenvals.png}
                    \caption{Comparison of first 101 numerical and analytic eigenvalues.}
                    \label{fig:q3_current}
                \end{subfigure}
            \end{figure}
            \item %1b
            \begin{enumerate}[align=left,leftmargin=*,labelsep=0em,label=(\roman*)] 
                \item %1bi
                The analytical solution is

                \begin{equation}
                    \phi(x) = A sin \left( \frac{n \pi}{L} x \right)
                    \label{eq:analytical}
                \end{equation}

                In order to normalise this equation it must conform to the following:

                \begin{equation}
                    \int_{0}^{L} \left| \phi(x) \right| ^2 dx = 1.
                    \label{eq:normalise}
                \end{equation}

                We use the following identity:

                \begin{equation}
                    \int\sin^2{(\alpha x)}\:dx = \frac{1}{2} x -\frac{1}{4\alpha}\sin{(2\alpha x)}.
                    \label{eq:integral_ident}
                \end{equation}

                Given that $sin$ of a real value is always real, we can disregard the norm operation, and directly relate (\ref{eq:analytical}) 
                to the above identity.
                Evaluating the integral gives us the following relationship

                \begin{equation*}
                    \frac{1}{A^2} = \frac{1}{2} L - \cancel{\frac{L}{4n \pi} sin \left( \frac{2n \pi}{L} L  \right)} - \cancel{\frac{1}{2} \cdot 0} + \cancel{\frac{L}{4n \pi} sin(0)}
                \end{equation*}

                From this, we find:

                \begin{equation*}
                    A = \sqrt{\frac{2}{L}}
                \end{equation*}

                \item %1bii

                {\em I really don't like this explanation}.

                The output of the numerical sum will change, as the value of $\alpha$ changes, by a factor if $\alpha$.
                To correct for this, we need to factor the result of the summation by $\alpha$, which corresponds to

                % Idk how to explain this at all, I need to chew on it internally and come back later.
                \begin{equation*}
                    foo
                \end{equation*}

                This means that B must be
                \begin{equation*}
                    B = \sqrt{\frac{2 \alpha}{L}}
                \end{equation*}

            \end{enumerate}

            \item %1c
            \begin{enumerate}[align=left,leftmargin=*,labelsep=0em,label=(\roman*)]
                \item %1ci

                From the base form of $\phi _\ell = Bsin \left( \frac {n \pi}{L} a \ell \right)$, we can see that
                $\phi _ {\ell + 1}$ and $\phi _ {\ell - 1}$ correspond to the trigonometric identities
                $sin(\alpha + B) = sin(\alpha)cos(B) + cos(\alpha)sin(B)$ and
                $sin(\alpha + B) = sin(\alpha)cos(B) + cos(\alpha)sin(B)$, respectively, where
                $\alpha = \frac{n \pi \alpha \ell}{L}$ and $B = \frac{n \pi \ell}{L}$.

                Fro
                
                \item %1cii
                \lipsum[1]
                \item %1ciii
                \lipsum[1]
                \item %1civ
                \lipsum[1]
            \end{enumerate}
            \item %1d
            \begin{enumerate}[align=left,leftmargin=*,labelsep=0em,label=(\roman*)]
                \item %1di
                \lipsum[1]
                \item %1dii
                \lipsum[1]
                \item %1diii
                \lipsum[1]
                \item %1div
                \lipsum[1]
            \end{enumerate}

        \end{enumerate}


    \section*{Problem 2}

        \begin{enumerate}[align=left,leftmargin=*,labelsep=1em,label=\bfseries(\alph*)] 
           
            \item %2a
            \lipsum[1]
            \item %2b
            \lipsum[1]
            \item %1c
            \lipsum[1]
            \item %1d
            \lipsum[1]
            \item %1e
            \lipsum[1]
            \item %1f
            \lipsum[1]

        \end{enumerate}
        
    \section*{Problem 3}

        \begin{enumerate}[align=left,leftmargin=*,labelsep=1em,label=\bfseries(\alph*)]
           
            \item %2a
            \lipsum[1]
            \item %2b
            \begin{enumerate}[align=left,leftmargin=*,labelsep=0em,label=(\roman*)]
                \item %1di
                \lipsum[1]
                \item %1dii
                \lipsum[1]
                \item %1diii
                \lipsum[1]
                \item %1div
                \lipsum[1]
                \item %1dv
                \lipsum[1]
                \item %1dvi
                \lipsum[1]
                \item %1dvii
                \lipsum[1]
            \end{enumerate}
            \item %1c
            \lipsum[1]
            \item %1d
            \lipsum[1]

        \end{enumerate}
        
    \section*{Problem 4}

        \begin{enumerate}[align=left,leftmargin=*,labelsep=1em,label=\bfseries(\alph*)]
           
            \item %2a
            \lipsum[1]
            \item %2b
            \begin{enumerate}[align=left,leftmargin=*,labelsep=0em,label=(\roman*)]
                \item %1di
                \lipsum[1]
                \item %1dii
                \lipsum[1]
                \item %1diii
                \lipsum[1]
            \end{enumerate}

        \end{enumerate}
        
    
\end{document}