$$ \newcommand{\half}{\frac{1}{2}} \newcommand{\tp}{\thinspace .} \newcommand{\uex}{{u_{\small\mbox{e}}}} \newcommand{\Aex}{{A_{\small\mbox{e}}}} \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

Jul 24, 2015

Table of contents

Finite difference methods
      A basic model for exponential decay
      The Forward Euler scheme
      The Backward Euler scheme
      The Crank-Nicolson scheme
      The unifying \( \theta \)-rule
      Constant time step
      Mathematical derivation of finite difference formulas
      Compact operator notation for finite differences
Implementation
      Making a solver function
      Verifying the implementation
      Computing the numerical error as a mesh function
      Computing the norm of the error mesh function
      Experiments with computing and plotting
      Memory-saving implementation
Exercises
      Exercise 1: Define a mesh function and visualize it
      Exercise 2: Differentiate a function
      Exercise 3: Experiment with integer division
      Exercise 4: Experiment with wrong computations
      Exercise 5: Plot the error function
      Exercise 6: Change formatting of numbers and debug
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 7: Visualize the accuracy of finite differences
      Exercise 8: Explore the \( \theta \)-rule for exponential growth
Model extensions
      Generalization: including a variable coefficient
      Generalization: including a source term
      Implementation of the generalized model problem
      Verifying a constant solution
      Verification via manufactured solutions
      Computing convergence rates
      Extension to systems of ODEs
General first-order ODEs
      Generic form of first-order ODEs
      The \( \theta \)-rule
      An implicit 2-step backward scheme
      Leapfrog schemes
      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
      Example: Adaptive Runge-Kutta methods
Exercises
      Exercise 9: Experiment with precision in tests and the size of \( u \)
      Exercise 10: Implement the 2-step backward scheme
      Exercise 11: Implement the 2nd-order Adams-Bashforth scheme
      Exercise 12: Implement the 3rd-order Adams-Bashforth scheme
      Exercise 13: Analyze explicit 2nd-order methods
      Problem 14: Implement and investigate the Leapfrog scheme
      Problem 15: Make a unified implementation of many schemes
Applications of exponential decay models
      Scaling
      Evolution of a population
      Compound interest and inflation
      Newton's law of cooling
      Radioactive decay
      Chemical kinetics
      Spreading of diseases
      Decay of atmospheric pressure with altitude
      Compaction of sediments
      Vertical motion of a body in a viscous fluid
      Decay ODEs from solving a PDE by Fourier expansions
Exercises
      Exercise 16: Radioactive decay of Carbon-14
      Exercise 17: Derive schemes for Newton's law of cooling
      Exercise 18: Implement schemes for Newton's law of cooling
      Exercise 19: Find time of murder from body temperature
      Exercise 20: Simulate an oscillating cooling process
      Exercise 21: Simulate stochastic radioactive decay
      Exercise 22: Radioactive decay of two substances
      Exercise 23: Simulate a simple chemical reaction
      Exercise 24: Simulate an \( n \)-th order chemical reaction
      Exercise 25: Simulate spreading of a disease
      Exercise 26: Simulate a biochemical process
      Exercise 27: Simulate the pressure drop in the atmosphere
      Exercise 28: Make a program for vertical motion in a fluid
      Project 29: Simulate parachuting
      Exercise 30: Formulate vertical motion in the atmosphere
      Exercise 31: Simulate vertical motion in the atmosphere
      Exercise 32: Compute \( y=|x| \) by solving an ODE
      Exercise 33: Simulate growth of a fortune with random interest rate
      Exercise 34: Simulate a population in a changing environment
      Exercise 35: Simulate logistic growth
      Exercise 36: Rederive the equation for continuous compound interest
Summarizing multiple-choice questions
      Exercise 37: Characterize a finite difference
      Exercise 38: Characterize a finite difference
      Exercise 39: What is the problem with this program?
      Exercise 40: Is the solution correct?
      Exercise 41: Is this a proper test function?
      Exercise 42: Rewrite an expression with array arithmetics
      Exercise 43: What is the truncation error?
      Exercise 44: Recognize a programming language
      Exercise 45: Recognize a programming language
      Exercise 46: Recognize a programming language
      Exercise 47: Recognize a programming language
      Exercise 48: What is SymPy?
      Exercise 49: Testing of code
      Exercise 50: What kind of scheme is this?
      Exercise 51: What kind of scheme is this?
      Exercise 52: What kind of scheme is this?
Bibliography

Finite difference methods for partial differential equations (PDEs) employ a range of concepts and tools that can be introduced and illustrated by way simple ordinary differential equation (ODE) examples. The aim of the present document is to lay a foundation for understanding numerical methods for PDEs by first meeting the fundamental ideas in a simpler ODE setting. With the ODEs, the mathematical problems are kept as simple as possible (but no simpler!), allowing full focus on the understanding of key concepts and tools. The choice of ODE topics to be covered here is thus solely determined by what carries over to the world of numerical solution 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 more general model problem \( u'=-a(t)u + b(t) \) and the completely general, nonlinear problem \( u'=f(u,t) \). The following list of topics will be elaborated on.

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 why we occasionally obtain strange results from the programs. Two fundamental questions saturate the text:
  • How do we solve a differential equation problem and produce numbers?
  • How do we know that the answer is correct?
Besides answering these two questions, you will learn a lot about mathematical modeling in general and the interplay between physics, mathematics, numerical methods, and computer science.

Read »