% -*- latex -*-
% FILE: "/home/evmik/jobs/wm/2016_fall_practical_computing_for_scientists/hw04/hw04.tex"
% LAST MODIFICATION: "Mon, 12 Sep 2016 14:40:50 -0400 (evmik)"
% (C) 2010 by Eugeniy Mikhailov, <evgmik@gmail.com>
% $Id:$
\documentclass[letter,12pt]{article}

%---------------------------------------------------------------
%\usepackage{listings}
\usepackage[framed]{matlab-prettifier}
\usepackage{color}
\usepackage{fullpage}
\usepackage{enumitem} % control over itemize, list environment
%---------------------------------------------------------------

\begin{document}
\newcommand{\hwtitle}[1]{%
	\begin{center}
		\Large  \bf Homework #1%
	\end{center}%
}

\newcommand{\mat}[1]{% matlab code
%{\color{blue}\texttt{#1}}%
	% this one does not work inside of captions
	{\lstinline[style = Matlab-editor, basicstyle = \mlttfamily, columns=fixed]!#1!}%
	% this one work but style is different
	%{\lstinline[columns=fixed]!#1!}%
}
%---------------------------------------------------------------
\newcommand{\problem}[1]{% homework problem
	%{\item  \bf #1%}
\item {\bf #1{}:}% the next blank line is important

}

\newenvironment{homework}[2]%
{%
	\hwtitle{#1}
	#2% Prerequisits

	\begin{enumerate}[
		label={\bf Problem\ \arabic*}, 
		leftmargin=0pt,
		start,
		labelindent=0pt,
		widest=20, align=left, itemindent=!
		]
}%
{%
	\end{enumerate}
}%
%---------------------------------------------------------------

%---------------------------------------------------------------
\begin{homework}{04}{
%Prerequisites:

{\bf Out of problem 1 and 2 choose only one to implement}. Problems 3, 4,
and 5 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 ordered exactly as
	prescribed in the 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.
\item 
	If the initial bracket is not applicable (for example,
	in the Newton-Raphson algorithm) use the right end 
	of the test bracket as the starting point of
	the algorithm.
\item
	All methods should be tested for the following parameters
	\mat{eps_f=1e-8} and \mat{eps_x=1e-10}.
\end{enumerate}
}





%\problem{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{optional (5 points)}
%---------------------------------------------------------------
Write proper implementation of the false position algorithm.
Define your function as\\
\mat{ function [x_sol, f_at_x_sol, N_iterations] = regula_falsi(f, xn, xp, eps_f, eps_x)}



\problem{optional (5 points)}
%---------------------------------------------------------------
Write a proper implementation of the secant algorithm.
Define your function as\\
\mat{ function [x_sol, f_at_x_sol, N_iterations] = secant(f, x1, x2, eps_f, eps_x)}




\problem{(5 points)}
%---------------------------------------------------------------
Write a proper implementation of  Newton-Raphson algorithm.
Define your function as\\
\mat{ function [x_sol, f_at_x_sol, N_iterations] = NewtonRaphson(f,
xstart, eps_f, eps_x, df_handle)}.
Note that \mat{df\_handle} is a handle to calculate derivative of the
function \mat{f} it could be either analytical representation of $f'(x)$
or its numerical estimate via the central difference formula.


\problem{(5 points)}
%---------------------------------------------------------------
Write a proper implementation of  Ridders' algorithm.
Define your function as\\
\mat{ function [x_sol, f_at_x_sol, N_iterations] = Ridders(f, x1, x2, eps_f, eps_x)}



\problem{(5 points)}
%---------------------------------------------------------------
For each of the root finding implementation of your homework
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 an algorithm diverges  with the suggested initial  bracket:  indicate so,
appropriately modify  the bracket, and show  the modified bracket in  the above
table as  well. Make  your conclusions  about speed  and robustness  of the
methods.


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

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

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

Note: the location of the minimum of the absolute error  indicates the optimal value
of $h$ for this particular case.
%---------------------------------------------------------------
\end{homework}
\end{document}
%---------------------------------------------------------------
