% -*- latex -*-
% FILE: "/home/evmik/jobs/wm/2015_fall_practical_computing_for_scientists/hw03/hw03.tex"
% LAST MODIFICATION: "Wed, 09 Sep 2015 10:56:04 -0400 (evmik)"
% (C) 2010 by Eugeniy Mikhailov, <evgmik@gmail.com>
% $Id:$
\documentclass[letter,12pt]{article}

%---------------------------------------------------------------
\usepackage{listings}
\usepackage{color}
\usepackage{fullpage}
%---------------------------------------------------------------

\begin{document}
\newcommand{\problem}[1]{%
	{\flushleft  \bf #1\\}
}
\newcommand{\hw}[1]{%
	\begin{center}
		\Large  \bf Homework #1%
	\end{center}%
}
\newcommand{\mat}[1]{% matlab code
{\color{blue}\texttt{#1}}%
}

%---------------------------------------------------------------
\hw{03}

{\bf Out of problem 1,2, and 3 chose only one to implement}. Problems 4, 5,
and 6 are to be done unconditionally.

{\bf With your email submission attach  listings of each function which you
implemented except maybe for problem 6}.

General requirements: 
\begin{enumerate}
\item 
	All root finding functions must have optional outputs
	with the function value at solution point, and number of iterations. So the
	general root finding function definition should look like \\
	\texttt{function [x\_sol, f\_at\_x\_sol, N\_iterations] = find\_root\_method(f\_handle,\ldots)}
\item {\bf Name your function and use its parameters order exactly as
	prescribed in a problem assignment}.
	The TA will run tests assuming the prescribed naming
	scheme. If your code is working but does not follow the
	specification points will be reduced.
\item
	Check for the possible user {\bf misuse} of the algorithms, think
	what could go wrong. 
	All relevant input parameters should be validated
	against possible user errors.
	If user
	submits wrong input parameters, your function must exit with an
	error message immediately without attempts to correct the wrong
	behavior. See \mat{error} documentation about how to do it.
\item
	Test your implementation with at least $f(x)=\exp(x)-5$ and the initial bracket [0,3],
	but do not limit yourself to only this case.
	\begin{itemize}
		\item If the initial bracket is not applicable (for example
			in the Newton-Raphson algorithm) use the right
			limit of the test bracket as a starting point of
			the algorithm.
	\end{itemize}
\item
	All methods should be tested for the following parameters eps\_f=1e-8 and
	eps\_x=1e-10.
\end{enumerate}





\problem{Problem 1 - optional (5 points)}
%---------------------------------------------------------------
Write a proper implementation of the bisection algorithm.
Define your function as\\
\texttt{ function [x\_sol, f\_at\_x\_sol, N\_iterations] =bisection(f, xn, xp, eps\_f, eps\_x)}

\problem{Problem 2 - optional (5 points)}
%---------------------------------------------------------------
Write proper implementation of the false position algorithm.
Define your function as\\
\texttt{ function [x\_sol, f\_at\_x\_sol, N\_iterations] = regula\_falsi(f, xn, xp, eps\_f, eps\_x)}



\problem{Problem 3 - optional (5 points)}
%---------------------------------------------------------------
Write a proper implementation of the secant algorithm.
Define your function as\\
\texttt{ function [x\_sol, f\_at\_x\_sol, N\_iterations] = secant(f, x1, x2, eps\_f, eps\_x)}




\problem{Problem 4 (5 points)}
%---------------------------------------------------------------
Write a proper implementation of  Newton-Raphson algorithm.
Define your function as\\
\texttt{ function [x\_sol, f\_at\_x\_sol, N\_iterations] = NewtonRaphson(f,
xstart, eps\_f, eps\_x, df\_handle)}.
Note that \texttt{df\_handle} is a handle to calculate derivative of the
function \texttt{f} it could be either analytical representation of $f'(x)$
or its numerical estimate via the central difference formula.


\problem{Problem 5 (5 points)}
%---------------------------------------------------------------
Write a proper implementation of  Ridders' algorithm.
Define your function as\\
\texttt{ function [x\_sol, f\_at\_x\_sol, N\_iterations] = Ridders(f, x1, x2, eps\_f, eps\_x)}



\problem{Problem 6 (5 points)}
%---------------------------------------------------------------
For each method find roots of the following two functions
\begin{enumerate}
	\item $f1(x)=\cos(x)-x$ with the  'x' initial  bracket [0,1]
	\item $f2(x)=\tanh(x-\pi)$ with the  'x' initial  bracket [-10,10]
\end{enumerate}
Make a comparison table for the above algorithms with following rows
\begin{enumerate}
	\item Method name
	\item root of $f1(x)$
	\item initial bracket or starting value used for $f1$
	\item Number of iterations to solve $f1$
	\item root of $f2(x)$
	\item initial bracket or starting value used for $f2$
	\item Number of iterations to solve $f2$
\end{enumerate}
make columns corresponding to 3 algorithms which you have chosen to implement.

If  algorithm diverges  with  suggested initial  bracket,  indicate so  and
appropriately modify  the bracket, indicate  the modified bracket in  the above
table as  well. Make  your conclusions  about speed  and robustness  of the
methods.


\problem{Bonus problem 7 (2 points)}
%---------------------------------------------------------------

Plot  the  $\log_{10}$  of  the  absolute error  of  $\sin(x)$  derivative  at
$x=\pi/4$  calculated  with  forward   and  central  difference  methods vs
the  $\log_{10}$  of the  $h$  value.  See \mat{loglog}  help  for  plotting  with
logarithmic  axes.  The values of $h$  should  cover  the   range  $10^{-16}
\cdots10^{-1}$  (see Matlab  \mat{logspace} function designed for such cases).

Why error decreases as $h$ goes down and then starts to increase?

The location of the minimum of the absolute error  indicates the optimal value
of $h$. 
%---------------------------------------------------------------
\end{document}
%---------------------------------------------------------------
