% -*- latex -*-
% FILE: "/home/evmik/jobs/wm/2012_fall_practical_computing_for_scientists/hw03/hw03.tex"
% LAST MODIFICATION: "Tue, 11 Sep 2012 11:09:18 -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
implement 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 below}. TA will run tests assuming the prescribed naming
	scheme. If your code is working but does not follow the
	specification it will lead to point reduction.
\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
	submit wrong input parameters, your function must exit with an
	error message immediately without attempts to correct wrong
	behavior. See \mat{error} documentation about how to do it.
\item
	Test your implementation with at least $f(x)=\exp(x)-5$ at initial bracket [0,3],
	but do not limit yourself to only this case.
	\begin{itemize}
		\item Where ever the initial bracket is not applied (for example 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 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 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 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 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 the root 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 chose to implement.

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


\problem{Bonus problem 7 (5 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}$  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 start to increase?

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