Preface
Algorithms and implementations
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
Computer language: Python
Making a solver function
Integer division
Doc strings
Formatting numbers
Running the program
Plotting the solution
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.1: Define a mesh function and visualize it
Problem 1.2: Differentiate a function
Problem 1.3: Experiment with divisions
Problem 1.4: Experiment with wrong computations
Problem 1.5: Plot the error function
Problem 1.6: Change formatting of numbers and debug
Analysis
Experimental investigations
Discouraging numerical solutions
Detailed experiments
Stability
Exact numerical solution
Stability properties derived from the amplification factor
Accuracy
Visual comparison of amplification factors
Series expansion of amplification factors
The ratio of numerical and exact amplification factors
The global error at a point
Integrated error
Truncation error
Consistency, stability, and convergence
Various types of errors in a differential equation model
Model errors
Data errors
Discretization errors
Rounding errors
Discussion of the size of various errors
Exercises
Problem 2.1: Visualize the accuracy of finite differences
Problem 2.2: Explore the \( \theta \)-rule for exponential growth
Problem 2.3: Explore rounding errors in numerical calculus
Generalizations
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 3.1: Experiment with precision in tests and the size of \( u \)
Exercise 3.2: Implement the 2-step backward scheme
Exercise 3.3: Implement the 2nd-order Adams-Bashforth scheme
Exercise 3.4: Implement the 3rd-order Adams-Bashforth scheme
Exercise 3.5: Analyze explicit 2nd-order methods
Project 3.6: Implement and investigate the Leapfrog scheme
Problem 3.7: Make a unified implementation of many schemes
Models
Scaling
Dimensionless variables
Dimensionless numbers
A scaling for vanishing initial condition
Evolution of a population
Exponential growth
Logistic growth
Compound interest and inflation
Newton's law of cooling
Radioactive decay
Deterministic model
Stochastic model
Relation between stochastic and deterministic models
Generalization of the radioactive decay modeling
Chemical kinetics
Irreversible reaction of two substances
Reversible reaction of two substances
Irreversible reaction of two substances into a third
A biochemical reaction
Spreading of diseases
Predator-prey models in ecology
Decay of atmospheric pressure with altitude
The general model
Multiple atmospheric layers
Simplifications
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
Viscoelastic materials
Decay ODEs from solving a PDE by Fourier expansions
Exercises
Problem 4.1: Radioactive decay of Carbon-14
Exercise 4.2: Derive schemes for Newton's law of cooling
Exercise 4.3: Implement schemes for Newton's law of cooling
Exercise 4.4: Find time of murder from body temperature
Exercise 4.5: Simulate an oscillating cooling process
Exercise 4.6: Simulate stochastic radioactive decay
Problem 4.7: Radioactive decay of two substances
Exercise 4.8: Simulate a simple chemical reaction
Exercise 4.9: Simulate an \( n \)-th order chemical reaction
Exercise 4.10: Simulate a biochemical process
Exercise 4.11: Simulate spreading of a disease
Exercise 4.12: Simulate predator-prey interaction
Exercise 4.13: Simulate the pressure drop in the atmosphere
Exercise 4.14: Make a program for vertical motion in a fluid
Project 4.15: Simulate parachuting
Exercise 4.16: Formulate vertical motion in the atmosphere
Exercise 4.17: Simulate vertical motion in the atmosphere
Problem 4.18: Compute \( y=|x| \) by solving an ODE
Problem 4.19: Simulate fortune growth with random interest rate
Exercise 4.20: Simulate a population in a changing environment
Exercise 4.21: Simulate logistic growth
Exercise 4.22: Rederive the equation for continuous compound interest
Exercise 4.23: Simulate the deformation of a viscoelastic material
Scientific software engineering
Implementations with functions and modules
Mathematical problem and solution technique
A first, quick implementation
A more decent program
Prefixing imported functions by the module name
Implementing the numerical algorithm in a function
Do not have several versions of a code
Making a module
Example on extending the module code
Documenting functions and modules
Logging intermediate results
User interfaces
Command-line arguments
Positional command-line arguments
Option-value pairs on the command line
Creating a graphical web user interface
Tests for verifying implementations
Doctests
Unit tests and test functions
Test function for the solver
Test function for reading positional command-line arguments
Test function for reading option-value pairs
Classical class-based unit testing
Sharing the software with other users
Organizing the software directory tree
Publishing the software at GitHub
Downloading and installing the software
Classes for problem and solution method
The problem class
The solver class
Improving the problem and solver classes
Automating scientific experiments
Available software
The results we want to produce
Combining plot files
Running a program from Python
The automating script
Making a report
Publishing a complete project
Exercises
Problem 5.1: Make a tool for differentiating curves
Problem 5.2: Make solid software for the Trapezoidal rule
Problem 5.3: Implement classes for the Trapezoidal rule
Problem 5.4: Write a doctest and a test function
Problem 5.5: Investigate the size of tolerances in comparisons
Exercise 5.6: Make use of a class implementation
Problem 5.7: Make solid software for a difference equation
Summarizing multiple-choice questions
Quiz
Exercise 6.1: Characterize a finite difference
Exercise 6.2: Characterize a finite difference
Exercise 6.3: What is the problem with this program?
Exercise 6.4: Is the solution correct?
Exercise 6.5: Is this a proper test function?
Exercise 6.6: Rewrite an expression with array arithmetics
Exercise 6.7: What is the truncation error?
Exercise 6.8: Recognize a programming language
Exercise 6.9: Recognize a programming language
Exercise 6.10: Recognize a programming language
Exercise 6.11: Recognize a programming language
Exercise 6.12: What is SymPy?
Exercise 6.13: Testing of code
Exercise 6.14: What kind of scheme is this?
Exercise 6.15: What kind of scheme is this?
Exercise 6.16: What kind of scheme is this?
References