On Schemes for Exponential Decay - GitHub...

23
,

Transcript of On Schemes for Exponential Decay - GitHub...

Page 1: On Schemes for Exponential Decay - GitHub Pageshplgit.github.io/doconce/doc/pub/slides/demo/demo_red... · 2018-08-27 · On Schemes for Exponential Decay Hans Petter Langtangen 1

On Schemes for Exponential Decay

Hans Petter Langtangen1,2

Center for Biomedical Computing, Simula Research Laboratory1

Department of Informatics, University of Oslo2

Sep 24, 2015

0 1 2 3 4 50.2

0.0

0.2

0.4

0.6

0.8

1.0

numericalexact

c© 2015, Hans Petter Langtangen. Released under CC Attribution 4.0 license

Page 2: On Schemes for Exponential Decay - GitHub Pageshplgit.github.io/doconce/doc/pub/slides/demo/demo_red... · 2018-08-27 · On Schemes for Exponential Decay Hans Petter Langtangen 1

Goal

The primary goal of this demo talk is to demonstrate how to write

talks with DocOnce and get them rendered in numerous HTML

formats.

Layout

This version utilizes beamer slides with the theme red_shadow.

Page 3: On Schemes for Exponential Decay - GitHub Pageshplgit.github.io/doconce/doc/pub/slides/demo/demo_red... · 2018-08-27 · On Schemes for Exponential Decay Hans Petter Langtangen 1

1 Problem setting and methods

2 Results

Page 4: On Schemes for Exponential Decay - GitHub Pageshplgit.github.io/doconce/doc/pub/slides/demo/demo_red... · 2018-08-27 · On Schemes for Exponential Decay Hans Petter Langtangen 1

Problem setting and methods

Page 5: On Schemes for Exponential Decay - GitHub Pageshplgit.github.io/doconce/doc/pub/slides/demo/demo_red... · 2018-08-27 · On Schemes for Exponential Decay Hans Petter Langtangen 1

We aim to solve the (almost) simplest possible di�erentialequation problem

u′(t) = −au(t) (1)

u(0) = I (2)

Here,

t ∈ (0,T ]

a, I , and T are prescribed

parameters

u(t) is the unknown function

The ODE (1) has the initial

condition (2)

Page 6: On Schemes for Exponential Decay - GitHub Pageshplgit.github.io/doconce/doc/pub/slides/demo/demo_red... · 2018-08-27 · On Schemes for Exponential Decay Hans Petter Langtangen 1

The ODE problem is solved by a �nite di�erence scheme

Mesh in time: 0 = t0 < t1 · · · < tN = T

Assume constant ∆t = tn − tn−1

un: numerical approx to the exact solution at tn

The θ rule,

un+1 =1− (1− θ)a∆t

1 + θa∆tun, n = 0, 1, . . . ,N − 1

contains the Forward Euler (θ = 0), the Backward Euler (θ = 1),

and the Crank-Nicolson (θ = 0.5) schemes.

Page 7: On Schemes for Exponential Decay - GitHub Pageshplgit.github.io/doconce/doc/pub/slides/demo/demo_red... · 2018-08-27 · On Schemes for Exponential Decay Hans Petter Langtangen 1

The ODE problem is solved by a �nite di�erence scheme

Mesh in time: 0 = t0 < t1 · · · < tN = T

Assume constant ∆t = tn − tn−1

un: numerical approx to the exact solution at tn

The θ rule,

un+1 =1− (1− θ)a∆t

1 + θa∆tun, n = 0, 1, . . . ,N − 1

contains the Forward Euler (θ = 0), the Backward Euler (θ = 1),

and the Crank-Nicolson (θ = 0.5) schemes.

Page 8: On Schemes for Exponential Decay - GitHub Pageshplgit.github.io/doconce/doc/pub/slides/demo/demo_red... · 2018-08-27 · On Schemes for Exponential Decay Hans Petter Langtangen 1

The ODE problem is solved by a �nite di�erence scheme

Mesh in time: 0 = t0 < t1 · · · < tN = T

Assume constant ∆t = tn − tn−1

un: numerical approx to the exact solution at tn

The θ rule,

un+1 =1− (1− θ)a∆t

1 + θa∆tun, n = 0, 1, . . . ,N − 1

contains the Forward Euler (θ = 0), the Backward Euler (θ = 1),

and the Crank-Nicolson (θ = 0.5) schemes.

Page 9: On Schemes for Exponential Decay - GitHub Pageshplgit.github.io/doconce/doc/pub/slides/demo/demo_red... · 2018-08-27 · On Schemes for Exponential Decay Hans Petter Langtangen 1

The ODE problem is solved by a �nite di�erence scheme

Mesh in time: 0 = t0 < t1 · · · < tN = T

Assume constant ∆t = tn − tn−1

un: numerical approx to the exact solution at tn

The θ rule,

un+1 =1− (1− θ)a∆t

1 + θa∆tun, n = 0, 1, . . . ,N − 1

contains the Forward Euler (θ = 0), the Backward Euler (θ = 1),

and the Crank-Nicolson (θ = 0.5) schemes.

Page 10: On Schemes for Exponential Decay - GitHub Pageshplgit.github.io/doconce/doc/pub/slides/demo/demo_red... · 2018-08-27 · On Schemes for Exponential Decay Hans Petter Langtangen 1

The ODE problem is solved by a �nite di�erence scheme

Mesh in time: 0 = t0 < t1 · · · < tN = T

Assume constant ∆t = tn − tn−1

un: numerical approx to the exact solution at tn

The θ rule,

un+1 =1− (1− θ)a∆t

1 + θa∆tun, n = 0, 1, . . . ,N − 1

contains the Forward Euler (θ = 0), the Backward Euler (θ = 1),

and the Crank-Nicolson (θ = 0.5) schemes.

Page 11: On Schemes for Exponential Decay - GitHub Pageshplgit.github.io/doconce/doc/pub/slides/demo/demo_red... · 2018-08-27 · On Schemes for Exponential Decay Hans Petter Langtangen 1

The Forward Euler scheme explained

http://youtube.com/PtJrPEIHNJw

Page 12: On Schemes for Exponential Decay - GitHub Pageshplgit.github.io/doconce/doc/pub/slides/demo/demo_red... · 2018-08-27 · On Schemes for Exponential Decay Hans Petter Langtangen 1

Implementation

Implementation in a Python function:

def solver(I, a, T, dt, theta):"""Solve u'=-a*u, u(0)=I, for t in (0,T]; step: dt."""dt = float(dt) # avoid integer divisionN = int(round(T/dt)) # no of time intervalsT = N*dt # adjust T to fit time step dtu = zeros(N+1) # array of u[n] valuest = linspace(0, T, N+1) # time mesh

u[0] = I # assign initial conditionfor n in range(0, N): # n=0,1,...,N-1

u[n+1] = (1 - (1-theta)*a*dt)/(1 + theta*dt*a)*u[n]return u, t

Page 13: On Schemes for Exponential Decay - GitHub Pageshplgit.github.io/doconce/doc/pub/slides/demo/demo_red... · 2018-08-27 · On Schemes for Exponential Decay Hans Petter Langtangen 1

How to use the solver function

A complete main program

# Set problem parametersI = 1.2a = 0.2T = 8dt = 0.25theta = 0.5|\pause|

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

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

Page 14: On Schemes for Exponential Decay - GitHub Pageshplgit.github.io/doconce/doc/pub/slides/demo/demo_red... · 2018-08-27 · On Schemes for Exponential Decay Hans Petter Langtangen 1

1 Problem setting and methods

2 Results

Page 15: On Schemes for Exponential Decay - GitHub Pageshplgit.github.io/doconce/doc/pub/slides/demo/demo_red... · 2018-08-27 · On Schemes for Exponential Decay Hans Petter Langtangen 1

Results

Page 16: On Schemes for Exponential Decay - GitHub Pageshplgit.github.io/doconce/doc/pub/slides/demo/demo_red... · 2018-08-27 · On Schemes for Exponential Decay Hans Petter Langtangen 1

The Crank-Nicolson method shows oscillatory behavior fornot su�ciently small time steps, while the solution shouldbe monotone

0 1 2 3 4 5t

0.2

0.0

0.2

0.4

0.6

0.8

1.0

u

Method: theta-rule, theta=0.5, dt=1.25

numericalexact

0 1 2 3 4 5 6t

0.0

0.2

0.4

0.6

0.8

1.0

u

Method: theta-rule, theta=0.5, dt=0.75

numericalexact

0 1 2 3 4 5t

0.0

0.2

0.4

0.6

0.8

1.0

u

Method: theta-rule, theta=0.5, dt=0.5

numericalexact

0 1 2 3 4 5t

0.0

0.2

0.4

0.6

0.8

1.0

u

Method: theta-rule, theta=0.5, dt=0.1

numericalexact

Page 17: On Schemes for Exponential Decay - GitHub Pageshplgit.github.io/doconce/doc/pub/slides/demo/demo_red... · 2018-08-27 · On Schemes for Exponential Decay Hans Petter Langtangen 1

The artifacts can be explained by some theory

Exact solution of the scheme:

un = An, A =1− (1− θ)a∆t

1 + θa∆t.

Key results:

Stability: |A| < 1

No oscillations: A > 0

∆t < 1/a for Forward Euler (θ = 0)

∆t < 2/a for Crank-Nicolson (θ = 1/2)

Concluding remarks:

Only the Backward Euler scheme is guaranteed to always give

qualitatively correct results.

Page 18: On Schemes for Exponential Decay - GitHub Pageshplgit.github.io/doconce/doc/pub/slides/demo/demo_red... · 2018-08-27 · On Schemes for Exponential Decay Hans Petter Langtangen 1

The artifacts can be explained by some theory

Exact solution of the scheme:

un = An, A =1− (1− θ)a∆t

1 + θa∆t.

Key results:

Stability: |A| < 1

No oscillations: A > 0

∆t < 1/a for Forward Euler (θ = 0)

∆t < 2/a for Crank-Nicolson (θ = 1/2)

Concluding remarks:

Only the Backward Euler scheme is guaranteed to always give

qualitatively correct results.

Page 19: On Schemes for Exponential Decay - GitHub Pageshplgit.github.io/doconce/doc/pub/slides/demo/demo_red... · 2018-08-27 · On Schemes for Exponential Decay Hans Petter Langtangen 1

The artifacts can be explained by some theory

Exact solution of the scheme:

un = An, A =1− (1− θ)a∆t

1 + θa∆t.

Key results:

Stability: |A| < 1

No oscillations: A > 0

∆t < 1/a for Forward Euler (θ = 0)

∆t < 2/a for Crank-Nicolson (θ = 1/2)

Concluding remarks:

Only the Backward Euler scheme is guaranteed to always give

qualitatively correct results.

Page 20: On Schemes for Exponential Decay - GitHub Pageshplgit.github.io/doconce/doc/pub/slides/demo/demo_red... · 2018-08-27 · On Schemes for Exponential Decay Hans Petter Langtangen 1

The artifacts can be explained by some theory

Exact solution of the scheme:

un = An, A =1− (1− θ)a∆t

1 + θa∆t.

Key results:

Stability: |A| < 1

No oscillations: A > 0

∆t < 1/a for Forward Euler (θ = 0)

∆t < 2/a for Crank-Nicolson (θ = 1/2)

Concluding remarks:

Only the Backward Euler scheme is guaranteed to always give

qualitatively correct results.

Page 21: On Schemes for Exponential Decay - GitHub Pageshplgit.github.io/doconce/doc/pub/slides/demo/demo_red... · 2018-08-27 · On Schemes for Exponential Decay Hans Petter Langtangen 1

The artifacts can be explained by some theory

Exact solution of the scheme:

un = An, A =1− (1− θ)a∆t

1 + θa∆t.

Key results:

Stability: |A| < 1

No oscillations: A > 0

∆t < 1/a for Forward Euler (θ = 0)

∆t < 2/a for Crank-Nicolson (θ = 1/2)

Concluding remarks:

Only the Backward Euler scheme is guaranteed to always give

qualitatively correct results.

Page 22: On Schemes for Exponential Decay - GitHub Pageshplgit.github.io/doconce/doc/pub/slides/demo/demo_red... · 2018-08-27 · On Schemes for Exponential Decay Hans Petter Langtangen 1

The artifacts can be explained by some theory

Exact solution of the scheme:

un = An, A =1− (1− θ)a∆t

1 + θa∆t.

Key results:

Stability: |A| < 1

No oscillations: A > 0

∆t < 1/a for Forward Euler (θ = 0)

∆t < 2/a for Crank-Nicolson (θ = 1/2)

Concluding remarks:

Only the Backward Euler scheme is guaranteed to always give

qualitatively correct results.

Page 23: On Schemes for Exponential Decay - GitHub Pageshplgit.github.io/doconce/doc/pub/slides/demo/demo_red... · 2018-08-27 · On Schemes for Exponential Decay Hans Petter Langtangen 1

The artifacts can be explained by some theory

Exact solution of the scheme:

un = An, A =1− (1− θ)a∆t

1 + θa∆t.

Key results:

Stability: |A| < 1

No oscillations: A > 0

∆t < 1/a for Forward Euler (θ = 0)

∆t < 2/a for Crank-Nicolson (θ = 1/2)

Concluding remarks:

Only the Backward Euler scheme is guaranteed to always give

qualitatively correct results.