% vim: set sts=4 et :
\documentclass[11 pt,xcolor={dvipsnames,svgnames,x11names,table}]{beamer}

\usetheme[
	bullet=circle,		% Other option: square
	bigpagenumber,		% circled page number on lower right
	topline=true,			% colored bar at the top of the frame 
	shadow=false,			% Shading for beamer blocks
	watermark=BG_lower,	% png file for the watermark
	]{Flip}

\usepackage{bm}
\usepackage{tikz,pgfplots}
\usepackage{graphicx}

% SOME COMMANDS THAT I FIND HANDY
% \renewcommand{\tilde}{\widetilde} % dinky tildes look silly, dosn't work with fontspec
\newcommand{\comment}[1]{\textcolor{comment}{\footnotesize{#1}\normalsize}} % comment mild
\newcommand{\Comment}[1]{\textcolor{Comment}{\footnotesize{#1}\normalsize}} % comment bold
\newcommand{\COMMENT}[1]{\textcolor{COMMENT}{\footnotesize{#1}\normalsize}} % comment crazy bold
\newcommand{\Alert}[1]{\textcolor{Alert}{#1}} % louder alert
\newcommand{\ALERT}[1]{\textcolor{ALERT}{#1}} % loudest alert
%% "\alert" is already a beamer pre-defined
\newcommand*{\Scale}[2][4]{\scalebox{#1}{$#2$}}%

\graphicspath{{images/}}	% Put all images in this directory. Avoids clutter.

% suppress frame numbering for backup slides
% you always need the appendix for this!
\newcommand{\backupbegin}{
	\newcounter{framenumberappendix}
	\setcounter{framenumberappendix}{\value{framenumber}}
}
\newcommand{\backupend}{
	\addtocounter{framenumberappendix}{-\value{framenumber}}
	\addtocounter{framenumber}{\value{framenumberappendix}} 
}

\begin{document}
\tikzstyle{every picture}+=[remember picture]

{
\setbeamertemplate{sidebar right}{\llap{\includegraphics[width=\paperwidth,height=\paperheight]{bubble2}}}
\begin{frame}[c]%{\phantom{title page}} 
\begin{center}
\begin{center}
	\begin{columns}
		\begin{column}{0.9\textwidth}
			\flushright%\fontspec{Trebuchet MS}
			\bfseries \Huge {Root Finding}
		\end{column}
		\begin{column}{0.2\textwidth}
		  %\includegraphics[width=\textwidth]{SHiP-2}
		\end{column}
	\end{columns}
\end{center}
	\quad
	\vspace{3em}
\begin{columns}
\begin{column}{0.6\textwidth}
\flushright \vspace{-1.8em} {%\fontspec{Trebuchet MS}
    \large Marcin Chrzaszcz, Danny van Dyk\\\vspace{-0.1em}\small \href{mailto:mchrzasz@cern.ch}{mchrzasz@cern.ch}, \href{mailto:dany.van.dyk@gmail.com}{danny.van.dyk@gmail.com}}

\end{column}
\begin{column}{0.4\textwidth}
\includegraphics[height=1.3cm]{uzh-transp}
\end{column}
\end{columns}

\vspace{1em}
\vspace{0.5em}
	\textcolor{normal text.fg!50!Comment}{Numerical Methods, \\ 26. September, 2016}
\end{center}
\end{frame}
}

\begin{frame}{Plan for today}
    \begin{itemize}
        \item \alert{General problem of finding the root of a function}\\
            $\xi$ is a root of $f$ iff $f(\xi) = 0$.
        \vfill
        \item \alert{Begin with $D=1$}\\
            What properties of $f(x)$ can we use to find one, any, or all roots
            of $f$? What are the requirements on $f(x)$?
        \vfill
        \item \alert{How about $D>1$?}\\
            Can we generalize root finding from $D=1$ do arbitrary $D$?
    \end{itemize}
\end{frame}

\begin{frame}{General iterative procedure}
    Let's assume that
    \begin{itemize}
        \item $f(x)$ is a our function of interest,
        \item $\xi$ is the only root of $f$,
        \item we have a point $x_0$ close to $\xi$
    \end{itemize}
    We now want to find a sequence $\lbrace x_0, x_1, \dots \rbrace$ that
    \begin{enumerate}
        \item converges toward $\xi$: $\lim_{k\to \infty} x_k = \xi$,
        \item is iterative: $x_{k + 1} = \Phi[f](x_k)$
    \end{enumerate}
    We can attempt to Taylor expand $f$ around $x_0$ to order $N$, in order
    to obtain the generator $\Phi[f]$ for the iteration:
    \begin{block}{Expansion around $x_0$}
    \begin{equation*}
        f(\xi) = 0 = \sum_{n=0}^N \frac{(\xi - x_0)^n}{n!} f^{(n)}(x_0)
    \end{equation*}
    where $f^{(n)}(x_0)$ is the $n$th derivative of $f$ at the position $x_0$.
    \end{block}
\end{frame}

\begin{frame}{Newton-Raphson method \hfill ($N=1$)}
    \begin{block}{Expansion for $N=1$}
        \begin{equation}
            \tag{*}
            f(\xi) = 0 = f(x_0) + (\xi - x_0) \cdot f'(x_0) + \mathcal{O}((\xi - x_0)^2)
        \end{equation}
    \end{block}
    \vfill
    Algorithm:
    \begin{enumerate}
        \item start with index $k=0$
        \item solve equation (*), assuming a vanshing approximation error:
            \begin{gather*}
                x_{k + 1} \leftarrow x_k - \frac{f(x_k)}{f'(x_k)} \approx \xi + \mathcal{O}((\xi - x_k)^2)\\
                \Phi[f](x) \equiv x - \frac{f(x)}{f'(x)}
            \end{gather*}
        \item if $f(x_{k + 1}) \leq t$, where $t$ is an a-prior threshold, then stop; otherwise
            jump back to step \#2.
    \end{enumerate}
\end{frame}

\begin{frame}{Illustration of Newton-Raphson}
    \begin{columns}
    \begin{column}[T]{.5\textwidth}
    \begin{overlayarea}{\textwidth}{\textheight}
        $\bm{k=0}$:
        \only<2->{%
        \begin{equation*}
            x_0 := 1
        \end{equation*}
        }
        \only<3->{%
            $\bm{k=1}$:
            \begin{align*}
                x_0     & = 1 &
                f(x_0)  & = +0.54 \\
                        &     &
                f'(x_0) & = -0.84 \\
            \end{align*}
        }
        \only<4->{%
            \begin{equation*}
                {\color{red} x_1 \leftarrow 1.64}
            \end{equation*}
        }
        \only<6->{%
            $\bm{k=2}$:
            \begin{align*}
                x_1     & = 1.64 &
                f(x_0)  & = -0.07 \\
                        &     &
                f'(x_0) & = -0.997 \\
            \end{align*}
        }
        \only<7->{%
            \begin{equation*}
                {\color{red} x_2 \leftarrow 1.57}
            \end{equation*}
        }
    \end{overlayarea}
    \end{column}
    \begin{column}[T]{.5\textwidth}
    \hspace{-1cm}
    \resizebox{1.1\textwidth}{!}{
    \begin{tikzpicture}
        \begin{axis}[%
            axis x line=center,
            axis y line=center,
            ymin=-0.2,ymax=0.8,
            xmin=0.0,xmax=1.8,
            title=$\cos(x)$
        ]
            \addplot[thick,black,domain=0:3.14] {
                cos(deg(x))
            };
            \only<2-3>{
                \addplot[thick,blue,only marks,mark=+,mark size=5pt] coordinates {
                    ( 1, 0.54030 )
                };
            }
            \only<3-4>{
                \addplot+[thick,blue,domain=0:3.14,mark=none] {
                    0.54030 - 0.84147 * (x - 1)
                };
            }
            \only<4-5>{
                \addplot[thick,red,only marks,mark=o,mark size=5pt] coordinates {
                    ( 1.6421, 0.0 )
                };
                \draw[thick,red] (axis cs:1.6421, 0) -- (axis cs:1.6421, -0.0712);
            }
            \only<5-6>{
                \addplot[thick,blue,only marks,mark=+,mark size=5pt] coordinates {
                    ( 1.6421, -0.0712 )
                };
            }
            \only<6-7>{
                \addplot+[thick,blue,domain=0:3.14,mark=none] {
                    -0.0712 - 0.99746 * (x - 1.6421)
                };
            }
            \only<7->{
                \addplot[thick,red,only marks,mark=o,mark size=5pt] coordinates {
                    ( 1.5707, 7.8e-5 )
                };
            }
        \end{axis}
    \end{tikzpicture}
    }
    \vfill
    \begin{overlayarea}{\textwidth}{.3\textheight}
        \only<8->{
        \begin{tabular}{c|cc}
            $k$ & $x_k$ & $f(x_k)$\\
            \hline
            $0$ & $1.00$ & $+0.54$\\
            $1$ & $1.64$ & $-0.07$\\
            $2$ & $1.57$ & $+0.00$\\
            \hline
            $\xi$&$1.5708$ & $0$
        \end{tabular}
        }
    \end{overlayarea}
    \end{column}
    \end{columns}
\end{frame}

\begin{frame}{Modified Newton-Raphson method \hfill ($N=2$)}
    \begin{block}{Expansion for $N=2$}
        \vspace{-2\medskipamount}
        \begin{equation}
            \tag{*}
            f(\xi) = 0 = f(x_0) + (\xi - x_0) \cdot f'(x_0) + \frac{1}{2} (\xi - x_0)^2 f''(x_0) + \mathcal{O}((\xi - x_0)^3)
        \end{equation}
    \end{block}
    \vfill
    Algorithm:
    \begin{enumerate}
        \item start with index $k=0$
        \item solve equation (*), assuming a vanshing approximation error:
            \begin{align*}
                a_\pm
                    & \leftarrow x_k - \frac{f'(x_k) \pm \sqrt{[f'(x_k)]^2 - 2 f(x_k) f''(x_k)}}{f''(x_k)}\\
                    & \approx \xi + \mathcal{O}((\xi - x_k)^3)
            \end{align*}
        \item if $|f(a_+)| < |f(a_-)|$, then $x_{k + 1} \leftarrow a_+$; otherwise $x_{k + 1} \leftarrow a_-$
        \item if $f(x_{k + 1}) \leq t$, where $t$ is an a-prior threshold, then stop; otherwise
            jump back to step \#2.
    \end{enumerate}
\end{frame}

\begin{frame}{Rate of convergence}
    Does this iterative procedure converge? If yes, how fast? How can we quantify the rate of convergence?

    \begin{itemize}
        \item we have \alert{local} convergence of order $p \leq 1$, if for all $x \in U(\xi)$
        \begin{equation*}
            || \Phi[f](x) - \xi|| \leq C \cdot || x - \xi||^p\,,\quad\text{with}\quad C \geq 0\,.
        \end{equation*}
        Note: if $p = 1$ then we must have $C < 1$.
        \item we have \alert{global} convergence if $U(\xi) = \mathbb{R}$
        \item for $D=1$ we can calculate $p$ is $\Phi[f](x)$ differentiable to sufficient degree:
        \begin{align*}
            \Phi(x) - \xi = \Phi(x) - \Phi(\xi) = \frac{(x - \xi)^p}{p!} + o(||x - \xi||^p)
        \end{align*}
    \end{itemize}
\end{frame}

\begin{frame}{Rate of convergence for Newton-Raphson}
    We have $\Phi(x) \equiv \Phi[f](x)$:
    \begin{itemize}
        \item $\Phi(\xi) = \xi$ by construction
        \item $\Phi'(\xi) = \frac{f(\xi) \cdot f''(\xi)}{[f'(\xi)]^2} = 0$ \alert{(*)} by construction
        \item $\Phi''(\xi) = \frac{f''(\xi)}{f'(\xi)}$
    \end{itemize}
    We can therefore write
    \begin{equation}
        \Phi(x) - \Phi(\xi) = \frac{(x - \xi)^2}{2!} \Phi''(\xi) + o(||x - \xi||^2)\,.
    \end{equation}
    The Newton-Raphson method converges therefore \emph{at least quadratically}, i.e.: it is a
    second-order method.
    \vfill
    \alert{(*): only if $f'(\xi) \neq 0$}, which is equivalent to $\xi$ is a simple root of $f$.
\end{frame}

\begin{frame}{Honorable mention: Horner Scheme}
    If $f(x) \equiv p_n(x)$ is a polynomial of degree $n$ in $x$, there might be multiple roots of $f$, i.e.
    roots $\xi_{n}$ with $f'(\xi_n) = 0$.\\
    \vfill
    The Horner Scheme allows to efficiently calculate all the (multiple) roots
    of the polynomial $p_n(x)$.\\
    \vfill
    However, this is of limited use: These polynomials usually only arise in computing
    the characteric polynomial $\chi_M$ of a matrix $M$. In this case, the roots of $\chi_M$
    correspond to eigenvalue of $M$. However, the are better/more stables ways to \alert{numerically} compute
    all eigenvalues of $M$.
\end{frame}

\begin{frame}{Newton-Raphson for $D > 1$}
\end{frame}

\backupbegin

\begin{frame}\frametitle{Backup}

\end{frame}

\backupend
\end{document}
