\documentclass{scrartcl}
\usepackage{pdfpages}
\usepackage[utf8]{inputenc}
\usepackage{float}
\usepackage{graphicx}
\usepackage[ruled,vlined]{algorithm2e}
\usepackage{subcaption}
\usepackage{hyperref}
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{plotmarks}
\usetikzlibrary{arrows.meta}
\usepgfplotslibrary{patchplots}
\usepackage{grffile}
\usepackage{amsmath}
\usepackage{subcaption}
\usepgfplotslibrary{external}
\tikzexternalize
\usepackage[margin=2.5cm]{geometry}

% To compile:
% sed -i 's#title style={font=\\bfseries#title style={yshift=1ex, font=\\tiny\\bfseries#' *.tex
% luatex -enable-write18 -shellescape main.tex

\pgfplotsset{every x tick label/.append style={font=\tiny, yshift=0.5ex}}
\pgfplotsset{every title/.append style={font=\tiny, align=center}}
\pgfplotsset{every y tick label/.append style={font=\tiny, xshift=0.5ex}}
\pgfplotsset{every z tick label/.append style={font=\tiny, xshift=0.5ex}}

\setlength{\parindent}{0cm}
\setlength{\parskip}{0.5\baselineskip}

\title{Optimization methods -- Homework 3}
\author{Claudio Maggioni}

\begin{document}

\maketitle

\section{Exercise 1}

\subsection{Exercise 1.1}

Please consult the MATLAB implementation in the files \texttt{Newton.m}, \texttt{GD.m}, and \texttt{backtracking.m}.
Please note that, for this and subsequent exercises, the gradient descent method without backtracking activated uses a
fixed $\alpha=1$ despite the indications on the assignment sheet. This was done in order to comply with the forum post
on iCorsi found here: \url{https://www.icorsi.ch/mod/forum/discuss.php?d=81144}.

\subsection{Exercise 1.2}

Please consult the MATLAB implementation in the file \texttt{main.m} in section 1.2.

\subsection{Exercise 1.3}

Please find the requested plots in figure \ref{fig:1}. The code used to generate these plots can be found in section 1.3 of \texttt{main.m}.

\begin{figure}[h]
	\begin{subfigure}{0.5\textwidth}
		\resizebox{\textwidth}{\textwidth}{\includegraphics{ex1-3.jpg}}
		\caption{Zoomed plot on $x = (-1,1)$ and $y = (-1,1)$}
	\end{subfigure}
	\begin{subfigure}{0.5\textwidth}
		\resizebox{\textwidth}{\textwidth}{\input{ex1-3-gd}}
		\caption{Complete plot}
	\end{subfigure}
	\caption{Steps in the energy landscape for Newton and GD methods}\label{fig:1}
\end{figure}

\subsection{Exercise 1.4}

Please find the requested plots in figure \ref{fig:gsppn}. The code used to generate these plots can be found in section 1.4 of \texttt{main.m}.

\begin{figure}[h]
	\begin{subfigure}{0.45\textwidth}
		\resizebox{\textwidth}{\textwidth}{\includegraphics{1-4-grad-nonlog.jpg}}
		\caption{Gradient norms \\(zoomed, y axis is linear for this plot)}
	\end{subfigure}
	\begin{subfigure}{0.45\textwidth}
		\resizebox{\textwidth}{\textwidth}{\includegraphics{1-4-ys-nonlog.jpg}}
		\caption{Objective function values \\(zoomed, y axis is linear for this plot)}
	\end{subfigure}
	\begin{subfigure}{0.45\textwidth}
		\resizebox{\textwidth}{\textwidth}{\includegraphics{1-4-grad.jpg}}
		\caption{Gradient norms (zoomed)}
	\end{subfigure}
	\begin{subfigure}{0.45\textwidth}
		\resizebox{\textwidth}{\textwidth}{\includegraphics{1-4-ys.jpg}}
		\caption{Objective function values (zoomed)}
	\end{subfigure}
	\begin{subfigure}{0.45\textwidth}
		\resizebox{\textwidth}{\textwidth}{\includegraphics{1-4-grad-large.jpg}}
		\caption{Gradient norms}
	\end{subfigure}
	\begin{subfigure}{0.45\textwidth}
		\centering
		\resizebox{\textwidth}{\textwidth}{\includegraphics{1-4-ys-large.jpg}}
		\caption{Objective function values}
	\end{subfigure}
	\caption{Gradient norms and objective function values (y-axes) w.r.t. iteration numbers (x-axis) for Newton and GD methods (y-axis is log scaled, points at $y=0$ not shown due to log scale)}\label{fig:gsppn}
\end{figure}

\section{Exercise 1.5}

TBD

\section{Exercise 2}

\subsection{Exercise 2.1}

Please consult the MATLAB implementation in the file \texttt{BGFS.m}.

\subsection{Exercise 2.2}

Please consult the MATLAB implementation in the file \texttt{main.m} in section 2.2.

\subsection{Exercise 2.3}

Please find the requested plots in figure \ref{fig:3}. The code used to generate these plots can be found in section 2.3 of \texttt{main.m}.

\begin{figure}[h]
	\centering
	\resizebox{.6\textwidth}{.6\textwidth}{\input{ex2-3}}
	\caption{Steps in the energy landscape for BGFS method}\label{fig:3}
\end{figure}

\subsection{Exercise 2.4}

Please find the requested plots in figure \ref{fig:4}. The code used to generate these plots can be found in section 2.4 of \texttt{main.m}.

\begin{figure}[h]
	\begin{subfigure}{0.5\textwidth}`
		\resizebox{\textwidth}{\textwidth}{\input{ex2-4-grad}}
		\caption{Gradient norms}
	\end{subfigure}
	\begin{subfigure}{0.5\textwidth}
		\resizebox{\textwidth}{\textwidth}{\input{ex2-4-ys}}
		\caption{Objective function values}
	\end{subfigure}
	\caption{Gradient norms and objective function values (y-axes) w.r.t. iteration numbers (x-axis) for BFGS method (y-axis is log scaled, points at $y=0$ not shown due to log scale)}\label{fig:4}
\end{figure}

\subsection{Exercise 2.5}

TBD

\end{document}
