$$ \newcommand{\uex}{{u_{\small\mbox{e}}}} \newcommand{\Aex}{{A_{\small\mbox{e}}}} \newcommand{\half}{\frac{1}{2}} \newcommand{\tp}{\thinspace .} \newcommand{\E}[1]{\hbox{E}\lbrack #1 \rbrack} \newcommand{\Var}[1]{\hbox{Var}\lbrack #1 \rbrack} \newcommand{\Std}[1]{\hbox{Std}\lbrack #1 \rbrack} \newcommand{\Oof}[1]{\mathcal{O}(#1)} $$

 

 

 

Introduction to computing with finite difference methods

Hans Petter Langtangen [1, 2]

[1] Center for Biomedical Computing, Simula Research Laboratory
[2] Department of Informatics, University of Oslo

Oct 5, 2014

Table of contents

Finite difference methods
      A basic model for exponential decay
      The Forward Euler scheme
            Step 1: Discretizing the domain
            Step 2: Fulfilling the equation at discrete time points
            Step 3: Replacing derivatives by finite differences
            Step 4: Formulating a recursive algorithm
      The Backward Euler scheme
      The Crank-Nicolson scheme
      The unifying \( heta \)-rule
      Constant time step
      Compact operator notation for finite differences
Implementation
      Making a solver function
            Function for computing the numerical solution
            Integer division
            Doc strings
            Formatting of numbers
            Running the program
            Plotting the solution
      Verifying the implementation
            Running a few algorithmic steps by hand
      Computing the numerical error as a mesh function
      Computing the norm of the numerical error
            Scalar computing
      Plotting solutions
      Experiments with computing and plotting
            Combining plot files
            Plotting with SciTools
      Memory-saving implementation
Analysis of finite difference equations
      Experimental investigation of oscillatory solutions
      Exact numerical solution
      Stability
      Comparing amplification factors
      Series expansion of amplification factors
      The fraction of numerical and exact amplification factors
      The global error at a point
      Integrated errors
      Truncation error
      Consistency, stability, and convergence
Exercises
      Exercise 1: Visualize the accuracy of finite differences
      Exercise 2: Explore the \( heta \)-rule for exponential growth
Model extensions
      Generalization: including a variable coefficient
      Generalization: including a source term
            Schemes
      Implementation of the generalized model problem
            Deriving the \( heta \)-rule formula
            The Python code
            Coding of variable coefficients
      Verifying a constant solution
      Verification via manufactured solutions
      Extension to systems of ODEs
General first-order ODEs
      Generic form of first-order ODEs
      The \( heta \)-rule
      An implicit 2-step backward scheme
      Leapfrog schemes
            The ordinary Leapfrog scheme
            The filtered Leapfrog scheme
      The 2nd-order Runge-Kutta method
      A 2nd-order Taylor-series method
      The 2nd- and 3rd-order Adams-Bashforth schemes
      The 4th-order Runge-Kutta method
      The Odespy software
      Example: Runge-Kutta methods
            Remark about using the \( heta \)-rule in Odespy
      Example: Adaptive Runge-Kutta methods
Exercises
      Exercise 3: Experiment with precision in tests and the size of \( u \)
      Exercise 4: Implement the 2-step backward scheme
      Exercise 5: Implement the 2nd-order Adams-Bashforth scheme
      Exercise 6: Implement the 3rd-order Adams-Bashforth scheme
      Exercise 7: Analyze explicit 2nd-order methods
      Problem 8: Implement and investigate the Leapfrog scheme
      Problem 9: Make a unified implementation of many schemes
Applications of exponential decay models
      Scaling
      Evolution of a population
      Compound interest and inflation
      Radioactive Decay
            Deterministic model
            Stochastic model
            Relation between stochastic and deterministic models
      Newton's law of cooling
      Decay of atmospheric pressure with altitude
            Multiple atmospheric layers
            Simplification: \( L=0 \)
            Simplification: one-layer model
      Compaction of sediments
      Vertical motion of a body in a viscous fluid
            Overview of forces
            Equation of motion
            Terminal velocity
            A Crank-Nicolson scheme
            Physical data
            Verification
            Scaling
      Decay ODEs from solving a PDE by Fourier expansions
Exercises
      Exercise 10: Derive schemes for Newton's law of cooling
      Exercise 11: Implement schemes for Newton's law of cooling
      Exercise 12: Find time of murder from body temperature
      Exercise 13: Simulate an oscillating cooling process
      Exercise 14: Radioactive decay of Carbon-14
      Exercise 15: Simulate stochastic radioactive decay
      Exercise 16: Radioactive decay of two substances
      Exercise 17: Simulate the pressure drop in the atmosphere
      Exercise 18: Make a program for vertical motion in a fluid
      Project 19: Simulate parachuting
      Exercise 20: Formulate vertical motion in the atmosphere
      Exercise 21: Simulate vertical motion in the atmosphere
      Exercise 22: Compute \( y=|x| \) by solving an ODE
      Exercise 23: Simulate growth of a fortune with random interest rate
      Exercise 24: Simulate a population in a changing environment
      Exercise 25: Simulate logistic growth
      Exercise 26: Rederive the equation for continuous compound interest
Bibliography

Finite difference methods for partial differential equations (PDEs) employ a range of concepts and tools that can be introduced and illustrated in the context of simple ordinary differential equation (ODE) examples. This is what we do in the present document. By first working with ODEs, we keep the mathematical problems to be solved as simple as possible (but no simpler), thereby allowing full focus on understanding the key concepts and tools. The choice of topics in the forthcoming treatment of ODEs is therefore solely dominated by what carries over to numerical methods for PDEs.

Theory and practice are primarily illustrated by solving the very simple ODE \( u'=-au \), \( u(0)=I \), where \( a>0 \) is a constant, but we also address the generalized problem \( u'=-a(t)u + b(t) \) and the nonlinear problem \( u'=f(u,t) \). The following topics are introduced:

The exposition in a nutshell. Everything we cover is put into a practical, hands-on context. All mathematics is translated into working computing codes, and all the mathematical theory of finite difference methods presented here is motivated from a strong need to understand strange behavior of programs. Two fundamental questions saturate the text:
  • How to we solve a differential equation problem and produce numbers?
  • How to we trust the answer?

Read »