\documentclass[english,notitlepage]{revtex4-1}  % defines the basic parameters of the document
%For preview: skriv i terminal: latexmk -pdf -pvc filnavn
\let\tablenum\relax % to avoid clash with siunitx


% if you want a single-column, remove reprint

% allows special characters (including æøå)
\usepackage[utf8]{inputenc}
%\usepackage[english]{babel}

%% note that you may need to download some of these packages manually, it depends on your setup.
%% I recommend downloading TeXMaker, because it includes a large library of the most common packages.

\usepackage{amssymb}  % mathematical symbols (physics imports amsmath)
\usepackage{amsmath}
\usepackage{graphicx}         % include graphics such as plots
\usepackage{xcolor}           % set colors
\usepackage{hyperref}         % automagic cross-referencing (this is GODLIKE)
\usepackage{listings}         % display code
\usepackage{subfigure}        % imports a lot of cool and useful figure commands
\usepackage{float}
%\usepackage[section]{placeins}
\usepackage{algorithm}
\usepackage[noend]{algpseudocode}
\usepackage{subfigure}
\usepackage{tikz}
\usepackage{siunitx}
\usetikzlibrary{quantikz}
% defines the color of hyperref objects
% Blending two colors:  blue!80!black  =  80% blue and 20% black
\hypersetup{ % this is just my personal choice, feel free to change things
    colorlinks,
    linkcolor={red!50!black},
    citecolor={blue!50!black},
    urlcolor={blue!80!black}}

%% Defines the style of the programming listing
%% This is actually my personal template, go ahead and change stuff if you want



%% USEFUL LINKS:
%%
%%   UiO LaTeX guides:        https://www.mn.uio.no/ifi/tjenester/it/hjelp/latex/
%%   mathematics:             https://en.wikibooks.org/wiki/LaTeX/Mathematics

%%   PHYSICS !                https://mirror.hmc.edu/ctan/macros/latex/contrib/physics/physics.pdf

%%   the basics of Tikz:       https://en.wikibooks.org/wiki/LaTeX/PGF/Tikz
%%   all the colors!:          https://en.wikibooks.org/wiki/LaTeX/Colors
%%   how to draw tables:       https://en.wikibooks.org/wiki/LaTeX/Tables
%%   code listing styles:      https://en.wikibooks.org/wiki/LaTeX/Source_Code_Listings
%%   \includegraphics          https://en.wikibooks.org/wiki/LaTeX/Importing_Graphics
%%   learn more about figures  https://en.wikibooks.org/wiki/LaTeX/Floats,_Figures_and_Captions
%%   automagic bibliography:   https://en.wikibooks.org/wiki/LaTeX/Bibliography_Management  (this one is kinda difficult the first time)
%%   REVTeX Guide:             http://www.physics.csbsju.edu/370/papers/Journal_Style_Manuals/auguide4-1.pdf
%%
%%   (this document is of class "revtex4-1", the REVTeX Guide explains how the class works)


%% CREATING THE .pdf FILE USING LINUX IN THE TERMINAL
%%
%% [terminal]$ pdflatex template.tex
%%
%% Run the command twice, always.
%% If you want to use \footnote, you need to run these commands (IN THIS SPECIFIC ORDER)
%%
%% [terminal]$ pdflatex template.tex
%% [terminal]$ bibtex template
%% [terminal]$ pdflatex template.tex
%% [terminal]$ pdflatex template.tex
%%
%% Don't ask me why, I don't know.

\newcommand{\tderiv}[2]{\frac{\mathrm{d} #1}{\mathrm{d} #2}} % defines a command for total derivatives
\newcommand{\pderiv}[2]{\frac{\partial #1}{\partial #2}} % defines a command for partial derivatives
\newcommand{\ptderiv}[3]{\frac{\mathrm{d}^{#3} #1}{\mathrm{d} #2^{#3}}} % defines a command for total derivatives of arbitrary power
\newcommand{\ppderiv}[3]{\frac{\partial^{#3} #1}{\partial #2^{#3}}} % defines a command for partial derivatives of arbitrary power

\begin{document}

\title{Solutions to Project 2}      % self-explanatory
\author{Lars Bogner}          % self-explanatory
\date{\today}                             % self-explanatory
\noaffiliation                            % ignore this, but keep it.


\maketitle 
    
\textit{Code hosted at \url{https://github.uio.no/larsbog/FYS4150}}
    
\section*{Problem 1}
Using the rescaling of the positions $\hat x \equiv \frac{x}{L}$ we define the following relations:
\begin{equation}
    u_x \equiv u, \quad u_{\hat x} \equiv \hat u, \quad \text{with} \quad u(x) = \hat u(\hat x) = u (L \hat x). 
\end{equation}
Now we can rewrite the equation as
\begin{equation}
    \begin{aligned}
        \gamma \ptderiv{u(x)}{x}{2} &= - F u(x)\\
        \gamma \ptderiv{u(L \hat x)}{(L \hat x)}{2} &= - F u(L \hat x).
        \end{aligned}
\end{equation}
In this step we simply replaced $x$ by the equivalent expression $L \hat x$. Now we can use the chain rule to rewrite the left hand side:
\begin{equation}
        \gamma \ppderiv{u(L \hat x)}{\hat x}{2} \underbrace{\ppderiv{\hat x}{(L \hat x)}{2}}_{=L^{-2}} = - F u(L \hat x).
\end{equation}
Rearranging the factors gives the final result:
\begin{equation}
        \ptderiv{\hat u (\hat x)}{\hat x}{2} = - \frac{F L^2}{\gamma} \hat u(\hat x) \equiv - \lambda \hat u(\hat x).
\end{equation}

\section*{Problem 2}
The code is implemented in \texttt{src/project2/arma\_eigenproblem\_checker.cpp}. The reusable functions for the tridiagonal matrix, the analytical solution and a wrapper around the Armadillo eigenvalue solver are implemented in \texttt{./src/helpers.cpp}.

\section*{Problem 3}
\subsection*{Subproblem (a)}
The function with the signature \texttt{double max\_offdiag\_symmetric(const arma::mat\& A, int\& k, int\& l)} finds the largest off-diagonal element in the symmetric matrix \texttt{A} and returns its value. The algorithm works like described in Algorithm \ref{alg:maxoffdiag}. The code is implemented in \texttt{./src/helpers.cpp}.

\begin{figure}
    \begin{algorithm}[H]
        \caption{Finding the largest off-diagonal element in a symmetric matrix}
        \label{alg:maxoffdiag}
        \begin{algorithmic}
            \Function{max\_offdiag\_symmetric}{$A, k, l$}
            \State $n \gets$ number of rows of $A$
            \State $\mathrm{max} \gets 0$
            \For{$i = 0$ to $n-1$}
                \For{$j = i+1$ to $n-1$}
                    \If{$|A_{ij}| > \mathrm{max}$}
                        \State $\mathrm{max} \gets |A_{ij}|$
                        \State $k \gets i$
                        \State $l \gets j$
                    \EndIf
                \EndFor
            \EndFor
            \State \Return $\mathrm{max}$
            \EndFunction
        \end{algorithmic}
    \end{algorithm}
\end{figure}

\subsection*{Subproblem (b)}
The code correctly identifies the largest off-diagonal element as $0.7$ at position $(1, 2)$. The function call is tested in \texttt{src/project2/off\_diagonal\_finder.cpp}.


\section*{Problem 4}
The Jacobi rotation algorithm is implemented as described in the lecture notes. The implementation is found in \texttt{src/helpers.cpp} as the function \texttt{void jacobi\_eigensolver(const arma::mat\& A, double eps, arma::vec\& eigenvalues, arma::mat\& eigenvectors, const int maxiter, int\& iterations, bool\& converged)}. It uses the \texttt{jacobi\_rotate} function to perform the actual rotation. The algorithm is tested in \texttt{src/\-project2/\-arma\_\-eigenproblem\_\-checker.cpp}. It modifies the eigenvalues and eigenvectors passed by reference. The function also returns the number of iterations used and whether the algorithm converged within the maximum number of iterations by modifying the variables passed by reference.

\section*{Problem 5}
\subsection*{Subproblem (a)}
The scaling of the number of iterations is tested in \texttt{src/project2/scaling\_tester.cpp}. The results are shown in \autoref{fig:scaling}. On the double logarithmic scale, the scaling is approximately linear, indicating a polynomial relation between the number of iterations and the matrix size $N$.
\begin{figure}
    \centering
    \includegraphics[width=0.7\textwidth]{include/scaling_data_iterations.pdf}
    \caption{Scaling of the number of iterations with the matrix size $N$. }
    \label{fig:scaling}
\end{figure}

\subsection*{Subproblem (b)}
The scaling is also tested for a dense matrix, i.e. a matrix without the tridiagonal structure. The results show nearly no dependance on the density of the matrix. This is to be expected, since the Jacobi algorithm creates new non-zero elements in each iteration, quickly filling up the matrix before conerging to the solution.

\section*{Problem 6}
\begin{figure}
    \centering
    \includegraphics[width=\textwidth]{include/eigen_data_10_plot.pdf}
    \caption{Comparison of the numerical and analytical solution of the buckling beam problem for $N=10$.}
    \label{fig:buckling_beam_10}
\end{figure}
\begin{figure}
    \centering
    \includegraphics[width=\textwidth]{include/eigen_data_100_plot.pdf}
    \caption{Comparison of the numerical and analytical solution of the buckling beam problem for $N=100$.}
    \label{fig:buckling_beam_100}
\end{figure}

The numerical and analytical solution of the buckling beam problem is compared in \autoref{fig:buckling_beam_10} for $N=10$ and in \autoref{fig:buckling_beam_100} for $N=100$. The numerical solution is in very good agreement with the analytical solution, even for the small value of $N=10$. For $N=100$, the solutions are visually indistinguishable. Nevertheless, the absolute error between the solutions is displayed on the right-hand side of the figures. The code to generate the plots is found in \texttt{src/project2/python/buckling\_beam\_plotter.py}.
\bibliography{include/citations}
\end{document}
 