A worked example on scientific computing with Python

Authors:Hans Petter Langtangen (hpl at simula.no)
Date:Jul 27, 2016

Contents

This worked example

  • fetches a data file from a web site,
  • applies that file as input data for a differential equation modeling a vibrating system,
  • solves the equation by a finite difference method,
  • visualizes various properties of the solution and the input data.

The following programming topics are illustrated:

  • basic Python constructs: variables, loops, if-tests, arrays, functions
  • flexible storage of objects in lists,
  • storage of objects in files (persistence),
  • downloading files from the web,
  • user input via the command line,
  • signal processing and FFT,
  • curve plotting of data,
  • unit testing,
  • symbolic mathematics,
  • modules.

All files can be forked at https://github.com/hplgit/bumpy.

Optimal background for reading this note

  • some interest in exploring physics through numerical simulation
  • some very basic knowledge of
    • differential equations
    • finite difference approximations
    • Python or Matlab
  • significant interest in exploring Python for scientific computations to solve a real-world physical problem (with low mathematical complexity)

Notice

You can read in two ways: either as a detailed example on using Python for solving differential equations (some very basic Python knowledge is preferred) or just to get an impression of how Python can be used in a Matlab-like fashion.

If you need motivation for using Python as programming language, see Appendix: Quick motivation for programming with Python. Lists of many useful tutorials and introductions to Python, with emphasis on scientific computing, are found in Appendix: Scientific Python resources.