%%%%%%%%%%%%%
%                                 %
%  Beamer Template  % 
%   with: handout        %
%   and: twoup print    %
%                                 %
%%%%%%%%%%%%%
\newif\ifhandout
\newif\iftwoup
% \handouttrue		% uncomment to get handouts; default is false; 		links are active
% \twouptrue  		% uncomment to get 'two-up' handouts; default is false;	links are dead
\ifhandout
% setup for printed handouts: no overlays, print 2 up, grey background
\documentclass[10pt,handout,hyperref={colorlinks=true,linkcolor=blue,citecolor=citelink,urlcolor=gray}]{beamer}
	\iftwoup
	\usepackage{pgfpages}
	\pgfpagesuselayout{2 on 1}[letterpaper,border shrink=5mm]
	\mode<handout>{\setbeamercolor{background canvas}{bg=black!5}}
	\fi
\else
% setup for class presentation slides
\documentclass[10pt,hyperref={colorlinks=true,linkcolor=blue,citecolor=citelink,urlcolor=gray}]{beamer}
\fi

\usepackage{mathptmx}
\usefonttheme{professionalfonts}
\usepackage{amsmath,amssymb}% ,amscd}
\usepackage{multicol}

%\usepackage{listings}

\usetheme{CambridgeUS}%Singapore}
 % w/o nav bar: 	Bergen Madrid Pittsburgh
 % tree nav bar: 	Antibes JuanLesPins Montpellier
 % TOC sidebar: 	Berkely PaloAlto Goettingen Hanover Marburg
 % mini-frame nav:   Berlin Darmstadt Dresden Frankfurt Singapore Szeged
 % sect & subtitles:   Copenhagen Luebeck Malmoe Warsaw
 % others:			AnnArbor Boadilla CambridgeUS Ilmenau Rochester

\usecolortheme{crane}
 % complete: 	albatross, beetle, crane, dove, fly, seagull
 % inner:		lily orchid
 % outer:		whale seahorse
 % others: 	beaver wolverine
\setbeamertemplate{blocks}[rounded][shadow=true]
\setbeamertemplate{navigation symbols}{}
%\logo{\thepage}
\author[Intro to Differential Equations]{}
\institute{MAT 3220}
\date{Fall '13}

\newtheorem{proposition}{Proposition}
\newtheorem{experiment}{Experiment}
\newtheorem{project}{Project}
\newtheorem{exercise}{Exercise}
\newtheorem{exercises}{Exercises}


\newcounter{e_temp}
%\newcounter{ei_temp}
%\newcounter{eii_temp}

%%%%%%%%
\begin{document}
\graphicspath{{Images/}}

%%%%%%
\begin{frame}[t]
\frametitle{Formulas: \href{http://www-history.mcs.st-and.ac.uk/Biographies/Euler.html}{Euler's} Method}
\vspace{-2ex}
\begin{block}{Initial Value Problem: $y^\prime = f(t,y)$ and $y(t_0)=y_0$}
\vspace{-3ex}
\renewcommand{\arraystretch}{2.4}
\begin{tabular}{@{} r p{0.6\textwidth}}
\textsc{Taylor's Theorem:} 	&	$y(t_1) = y(t_0) + y^\prime(t_0)h +\frac12 y^{\prime\prime}(\xi) h^2$ \\ 
\textsc{Differentiating $f(t,y)$:}	&	If $y^\prime=f(t,y)$, then\footnotemark\
							$y^{\prime\prime} =\dfrac{\partial f}{\partial t} + \dfrac{\partial f}{\partial y}\cdot y^\prime$ \\
\textsc{Euler's Method Step:}	&	$\begin{cases} \,t_{n+1} =t_n + h \\ y_{n+1} = y_n + y^\prime(t_n,y_n)\,h \end{cases}$ \\ 
\textsc{One-Step Error:}	&	$\varepsilon = y_k - y(t_k)$  \hfill {\small\it (error = predicted $-$ actual\/)} \\
\textsc{One-Step Error Bound:} &	If $\left|y^{\prime\prime}\right| \le M$, 
					then $|\varepsilon| \le \frac{M}{2} h^2$ \\
\textsc{Total Error Bound:}	&	If $\left|y^{\prime\prime}\right| \le M$ and 
						$C=\frac12(t_n-t_0)M$, \newline	then	$|\varepsilon_T| \le C\cdot h  = \mathcal{O}(h) = \mathcal{O}(1/n)$
\end{tabular}
\end{block}
\footnotetext{From $d\!f = \frac{\partial f}{\partial t}\,dt+\frac{\partial f}{\partial y}\,dy$.}
\end{frame}

%%%%%%
\begin{frame}[t]
\frametitle{Formulas: \href{http://www-history.mcs.st-and.ac.uk/Biographies/Heun.html}{Heun's} Method}
\vspace{-2ex}
\begin{block}{Initial Value Problem: $y^\prime = f(t,y)$ and $y(t_0)=y_0$}
\vspace{-1ex}
\renewcommand{\arraystretch}{2.5}%
\begin{tabular}{@{} r p{0.6\textwidth}}
\textsc{Heun's Method Step:}	&	Let $m_L=f(t_n,y_n)$ and set $y_p = y_n + m_Lh$. Then find $m_R = f(t_n+h,y_p)$. The step is:
							\vspace{-1ex}
							\[	\begin{cases} t_{n+1} =t_n + h \\ y_{n+1} = y_n + \frac12\left[m_L+m_R\right]h \end{cases}
								\vspace{-1ex}	\]
							\mbox{\rule{0pt}{12pt}} \hfill {\small (Heun's is a \emph{predictor-corrector method:}\newline 
								\mbox{} \hfill $y_p \blacktriangleleft$ predictor; $m_R\blacktriangleleft$ corrector)} \\
\textsc{One-Step Error Bound:} &	If $\left| y^{\prime\prime}\right| \le M$, 
					then $|\varepsilon| \le \frac{M}{12}\,h^3$ \\
\textsc{Total Error Bound:}	&	If $\left|y^{\prime\prime}\right| \le M$, 
						then	$|\varepsilon_T|  = \mathcal{O}(h^2) = \mathcal{O}(1/n^2)$
\end{tabular}
\end{block}
\end{frame}

%%%%%%
\begin{frame}[t]
\frametitle{Formulas: Maple Code}
\vspace{-2ex}
{\small
\begin{block}{Maple Code for Solving $y^\prime = f(t,y)$ when $y(t_0)=y_0$}
\texttt{Euler} $:= (P,h) \to [P_1+h,\; P_2+f(P_1,P_2)\cdot h]$;	\\
\texttt{Heun} $:= (P,h) \to [P_1+h,\; P_2+ \frac12\cdot(f(P_1, P_2)+f(P_1+h,\; P_2+f(P_1, P_2)\cdot h))\cdot h]$ \\[2ex]

\texttt{DESolve} $:=$ \texttt{proc}$(t0,\, tn,\, y0,\, h,\, \text{\it Method\/})$	\\
\quad \texttt{local} $N,\, Pts,\, i$;	\\
\quad \texttt{global} $f,$ Euler,\, Heun; 	\\
\quad $N$ := ceil$((tn-t0)/h)$; 	\\
\quad $Pts$ := $[t0, y0],\; \text{\it Method\/}([t0, y0], h)$; 	\\
\quad for $i$ from $2$ to $N$ do 	\\
\qquad $Pts$ := $Pts, \text{\it Method\/}(Pts[i], h)$;	\\
\qquad end do; 	\\
\quad return($[Pts]$);	\\
\quad end proc: \\[2ex]

{\it $\blacktriangleright$ Example:} First define $f(t,y) = 0.05\,y\cdot(1-y/5)$. Then \\
{\it theEulerPts} := \texttt{DESolve}$(0,\, 5.0,\, 3.0,\, 0.5,$ Euler$)$;		\\
{\it theHeunPts} := \texttt{DESolve}$(0,\, 5.0,\, 3.0,\, 0.5,$ Heun$)$;		\\
\texttt{plot}$(\,[\text{\it theEulerPts},\, \text{\it theHeunPts}\,]\,)$;

\end{block}
}
\end{frame}

%%%%%%
\begin{frame}[t]
\frametitle{Formulas: \href{http://www-history.mcs.st-and.ac.uk/Biographies/Runge.html}{Runge}\,-\href{http://www-history.mcs.st-and.ac.uk/Biographies/Kutta.html}{Kutta} Methods}

\begin{block}{Initial Value Problem: $y^\prime = f(t,y)$ and $y(t_0)=y_0$}
\vspace{-1ex}
\renewcommand{\arraystretch}{2.5}%
\begin{tabular}{@{} r p{0.6\textwidth}}
\textsc{Runge-Kutta Method Step:}	&	Let\; $k_1 = f(t_n,\, y_n)$, 	\\[-3ex]
							&	\qquad $k_2 = f(t_n+\frac{h}{2},\, y_n+k_1\!\cdot\!\frac{h}{2})$,	\\[-3ex]
							&	\qquad $k_3 = f(t_n+\frac{h}{2},\, y_n+k_2\!\cdot\!\frac{h}{2})$,	\\[-3ex]
							&	\qquad $k_4 = f(t_n+h,\,y_n+k_3\!\cdot\!h)$.	\\[-3ex]
							&	Then 	\\[-4ex]
							&	\qquad $y_{n+1} = y_n + \tfrac16\cdot(k_1+2k_2+2k_3+k_4)\cdot h$		\\
\textsc{One-Step Error Bound:} &	If $| y^{(4)}| \le M$, 
					then $|\varepsilon| \le C\cdot h^5$ \\
\textsc{Total Error Bound:}	&	If $|y^{(4)}| \le M$, 
						then	$|\varepsilon_T|  = \mathcal{O}(h^4) = \mathcal{O}(1/n^4)$
\end{tabular}
\end{block}
\end{frame}

%%%%%%
\begin{frame}[t]
\frametitle{Formulas: RK4 Maple Code}
\vspace{-2ex}
{\small
\begin{block}{RK4 Maple Code for Solving $y^\prime = f(t,y)$}
\texttt{RK4} $:=$ \texttt{proc}$(P,\, h)$	\\
\quad \texttt{local} $k,\,Q$;	\\
\quad $k_1$ := $f(P_1,\,P_2)$; 	\\
\quad $k_2$ := $f(P_1+h/2,\,P_2+k_1\cdot h/2)$; 	\\
\quad $k_3$ := $f(P_1+h/2,\,P_2+k_2\cdot h/2)$; 	\\
\quad $k_4$ := $f(P_1+h,\,P_2+k_3\cdot h)$; 	\\
\quad $Q$ := $\left[P_1+h,\, P_2+\frac16 (k_1+2\,k_2+2\,k_3+k_4)\cdot h\right]$; 	\\
\quad return($Q$);	\\
\quad end proc: \\[4ex]

{\it $\blacktriangleright$ Example:} First define $f(t,y) = 0.05\,y\cdot(1-y/5)$. Then find $y(5)$ when $y_0=3$: \\
$t0,\, tn,\, y0,\, h$ \texttt{;=} $0,\, 5.0,\, 3.0,\, 0.5$; \\
{\it theRK4Pts} := \texttt{DESolve}$(t0,\, tn,\, y0,\, h,$ RK4$)$;		\\
\texttt{plot}$(\,[\text{\it theRK4Pts}\,]\,)$;

\end{block}
}

\end{frame}

%%%%%%
\begin{frame}[t]
\frametitle{Comparison}
\vspace{-2ex}
\begin{block}{Comparing the Methods}
\centerline{\renewcommand{\arraystretch}{2}%
\begin{tabular}{*4{c}}
\textbf{Method} & \textbf{Order} & \textbf{Basis} & \textbf{Cost} \\
\hline
Euler's method & $\mathcal{O}(1/n)$ & Left endpoint integration & Low \\
Heun's method & $\mathcal{O}(1/n^2)$ & Trapezoid rule & Medium \\
Runge-Kutta method\footnotemark & $\mathcal{O}(1/n^4)$ & Simpson's rule & High \\
\end{tabular}}
\end{block}
\vfill

\begin{block}{Test Case}
Apply each of the methods to the initial value problem
\vspace{-1ex}
\[	y^\prime = 9\,y\cdot(1-y), \quad y_0=1.25
	\vspace{-1ex}	\]
to calculate $y(10)$ beginning with $h=0.29$.
\end{block}
\vfill

\footnotetext{If you're curious about the derivation of RK methods, see, e.g., \href{http://nptel.iitm.ac.in/courses/111104030/pdf_lectures/lecture9.pdf}{\it Runge-Kutta Methods}}
\end{frame}

%%%%%%
\begin{frame}
\frametitle{Comparison Spoiler}
\centerline{\includegraphics[width=0.75\textwidth]{ComparisonExample.pdf}}
\end{frame}

%%%%%%
\begin{frame}[t]
\frametitle{Formulas: Systems}
\vspace{-2ex}
\begin{block}{Using Numerical Methods for Systems}
Simply apply the method using all variables in the step formula:
\vspace{-1ex}
\[	\begin{cases} x^{\prime}(t) = f(t,x,y) \\ y^{\prime}(t) = g(t,x,y)\end{cases} \text{\quad with\quad} \begin{cases}x(t_0)=x_0 \\ y(t_0) = y_0 \end{cases}
	\vspace{-1ex}	\]
is approximated with Euler's method by
\vspace{-1ex}
\[	\begin{cases} \,t_{n+1} =\, t_n + h \\ x_{n+1} = x_{n} + f(t_n,x_n,y_n)\cdot h \\ y_{n+1} = y_{n} + g(t_n,x_n,y_n)\cdot h \end{cases}	\]

We can use the Maple function \\
\quad\texttt{EulerSystem} := $[P,\,h] \to [P_1+h,\; P_2 + f(P_1,\, P_2,\, P_3)\cdot h,\; P_3 + g(P_1,\, P_2,\, P_3)\cdot h]$; \\[2ex]

{\footnotesize\textbf{NB:} Numerical methods can be applied to a higher order DE by converting it to a system:
\vspace{-1ex}
\[	y^{\prime\prime}+a(t)\,y^{\prime} + b(t)\,y = f(t) \implies \begin{cases} x=y^{\prime} \\ x^{\prime} = f(t) - a(t)\,x - b(t)\,y\end{cases}	\]}
\vspace{-1ex}
% In vector notation: $\vec{P}_{n+1} = \vec{P}_n + {\scriptsize\begin{bmatrix}1 \\ f(\vec{P}) \\ g(\vec{P}) \end{bmatrix}}\cdot h$
\end{block}
\end{frame}

\end{document}
\end
