TITLE: On Schemes for Exponential Decay
AUTHOR: Hans Petter Langtangen Email:hpl@simula.no {copyright|CC BY} at Center for Biomedical Computing, Simula Research Laboratory & Department of Informatics, University of Oslo
DATE: today

FIGURE: [fig/CN_logo, width=300 frac=0.3]

## Comments start with a # and are visible as comments in the
## output. Lines starting with ## are also comments, but not visible
## in the output.

# Each slide starts with !split and a title inside 5 = on each side
# (i.e., DocOnce subsections are used to specify slide titles,
# sections are used for parts/sections of the talk to appear in a
# table of contents)

!split
===== Goal =====

The primary goal of this demo talk is to demonstrate how to write
talks with "DocOnce": "https://github.com/hplgit/doconce"
and get them rendered in numerous HTML formats.
# #if FORMAT in ("html", "pdflatex", "latex")
!bnotice Layout
This version
utilizes ${SLIDE_TYPE} slides with the theme `${SLIDE_THEME}`.
!enotice
# #endif

## Demonstrate use of Mako variables set on the command line
## (see last sentence too). We use Preprocess (run before Mako)
## to leave out the Mako test on SLIDE_THEME and SLIDE_TYPE.
## In that way, we do not need to specify these variables on
## the command line when compiling for LaTeX or other formats
## (e.g. for handouts).

# #if FORMAT == "html"
% if SLIDE_TYPE != 'doconce':
!bnotice
Speaker notes show up by
% if SLIDE_TYPE == 'reveal':
pressing "s" (but you must run reveal locally and
some packages installed, see "this description": "https://github.com/hakimel/reveal.js/#installation").
% elif SLIDE_TYPE == 'deck':
pressing "n".
% elif SLIDE_TYPE == 'csss':
pressing "Ctrl-P".
% elif SLIDE_TYPE == 'html5slides':
pressing "p".
% elif SLIDE_TYPE == 'dzslides':
clicking on the arrow head before Details below.
% elif SLIDE_TYPE == 'csss':
using the "onstage.html": "https://raw.github.com/hplgit/doconce/master/bundled/dzslides/shells/onstage.html" embedding page.
% endif
% endif
!enotice
# #endif

## Add speaker notes (can also use preprocess or mako for this)

!bnotes
The talk investigates the accuracy of three finite difference
schemes for the ordinary differential equation $u'=-au$ with the
aid of numerical experiments. Numerical artifacts are in particular
demonstrated.
!enotes

## Parts in slides that come up as \section{} in Beamer
## are typeset with section heading (7 =) - this is used
## to divide a talk into natural parts such as methods and
## results in the present demo

!split
======= Problem setting and methods =======
# Short title: Problem

FIGURE: [fig/method, width=600 frac=0.8]

## New slide is indicated by !split at the beginning of a line:

!split
===== We aim to solve the (almost) simplest possible differential equation problem =====

## Use !bslidecell XY and !eslidecell as begin and end for
## defining a cell XY in a table layout of the slide.
## X=0,1,..., Y=0,1,... XY=00 is the upper left cell.
## Here we define cell 00 and 01 (bullet list to the left
## and figure to the right).

!bslidecell 00

## latex blocks are surrounded by !bt and !et

!bt
\begin{align}
u'(t) &= -au(t)
label{ode}\\
u(0)  &= I
label{initial:value}
\end{align}
!et

## Demonstrate bullet list, inline mathematics, and
## references to equations

Here,

 * $t\in (0,T]$
 * $a$, $I$, and $T$ are prescribed parameters
 * $u(t)$ is the unknown function
 * The ODE (ref{ode}) has the initial condition (ref{initial:value})

!eslidecell

## Figures can have a width parameter specifying the pixel
## width in HTML. A caption can be added (after [...]).

!bslidecell 01
FIGURE: [fig/teacher2, width=250 frac=0.5]
!eslidecell

!split
===== The ODE problem is solved by a finite difference scheme =====

## !bpop and !epop surround elements that are to pop up
## as we progress in the slide show.
## They will often not work if there is code inside.

!bpop
 * Mesh in time: $0= t_0< t_1 \cdots < t_N=T$
 * Assume constant $\Delta t = t_{n}-t_{n-1}$
 * $u^n$: numerical approx to the exact solution at $t_n$
!epop

!bpop
The $\theta$ rule,

!bt
\[
u^{n+1} = \frac{1 - (1-\theta) a\Delta t}{1 + \theta a\Delta t}u^n,
\quad n=0,1,\ldots,N-1
\]
!et
contains the "Forward Euler":
"http://en.wikipedia.org/wiki/Forward_Euler_method" ($\theta=0$),
the "Backward Euler":
"http://en.wikipedia.org/wiki/Backward_Euler_method" ($\theta=1$),
and the "Crank-Nicolson":
"http://en.wikipedia.org/wiki/Crank-Nicolson" ($\theta=0.5$)
schemes.
!epop


!split
## Next: embed YouTube video

===== The Forward Euler scheme explained =====

## !bblock and !eblock are used to get frames or in Beamer the block
## environment

!bblock
MOVIE: [http://youtu.be/PtJrPEIHNJw, width=640 height=480]
!eblock


!split
## Next: exemplify copying code directly from file (start
## with the line matching "def solver" and ending with
## "def verify_three", but not including this last line).

===== Implementation =====

!bblock Implementation in a Python function:

@@@CODE src/solver.py  fromto: def solver@def exact_solution

!eblock

!split
===== How to use the solver function =====

## Demonstrate how to use the |\pause| command to pop up
## parts of the code in beamer (ignored in all other formats)
## (minted or lstlisting package must be used with beamer)

!bblock A complete main program
!bc pypro
# Set problem parameters
I = 1.2
a = 0.2
T = 8
dt = 0.25
theta = 0.5
|\pause|

from solver import solver, exact_solution
u, t = solver(I, a, T, dt, theta)
|\pause|

import matplotlib.pyplot as plt
plt.plot(t, u, t, exact_solution)
plt.legend(['numerical', 'exact'])
plt.show()
!ec
!eblock

!split
======= Results =======

FIGURE: [fig/results, width=600 frac=0.8]

!split
===== The Crank-Nicolson method shows oscillatory behavior for not sufficiently small time steps, while the solution should be monotone =====

## Figure filenames do not need extension - the right version is picked

FIGURE: [fig/CN, width=600 frac=0.9]

!split
===== The artifacts can be explained by some theory =====

!bpop
Exact solution of the scheme:

!bt
\[ u^n = A^n,\quad A = \frac{1 - (1-\theta) a\Delta t}{1 + \theta a\Delta t}\thinspace .\]
!et
!epop

Key results:

!bpop
 * Stability: $|A| < 1$
 * No oscillations: $A>0$
 * $\Delta t < 1/a$ for Forward Euler ($\theta=0$)
 * $\Delta t < 2/a$ for Crank-Nicolson ($\theta=1/2$)
!epop

## The summary environment appears in a box in most formats
## (warning, notice, question are other environments)
!bpop
!bsummary Concluding remarks:
Only the Backward Euler scheme is guaranteed to always give
qualitatively correct results.
!esummary
!epop