Computational Tutorials

The tutorials below introduce some computational tools in Python that will be useful in various physics classes. They are designed to get you started quickly by explaining example code that you can modify. There are also links to additional documentation where you can learn more. The tutorials are written as Jupyter notebooks (formerly known as IPython notebooks).

The programs require Python with the scipy and matplotlib libraries.  Any of the following free options are suggested:

  • Use Google Colab to edit and run Jupyter notebooks on a cloud based system. It is free and doesn't require any software to be installed. As the name implies, it also lets you collaborate. You can access files on your Google Drive (see examples). It is helpful to enable the "Text Editor for Google Drive" app in Google Drive (click on "New", scroll down to "More", click on "Connect more apps", and search for "TextEditor") .
  • Use CoCalc (Collaborative Calculation in the Cloud) and edit Python programs or Jupyter notebooks on a cloud based system. Free accounts can be a bit slow.
  • Install the Anaconda Python distribution and either use Spyder to edit and run programs or use Jupyter Notebook with files on your computer.

It is a good idea to start all of your programs with the following line (note that there are 2 underscores before and after "future"):
     from __future__ import division, print_function
If you use Python 2, this will avoid the result of division being truncated to an integer (for example, "1/2" will not give zero) and will use the newer form of the print function (instead of the older print "Hello World", use the newer print("Hello World")).  If you use Python 3, this will have no effect.

Click on the links below to view HTML versions of the tutorials (produced by nbviewer). From within the notebook viewer, you can copy segments of Python code from a tutorial. You can also download a tutorial as a Jupyter notebook (.ipynb file), which allows you to edit it.

Matrix Solution for a Set of Linear Equations

An Introduction to Making Graphs

Text, Math, and Numbers in Figures

A Brief Introduction to Typesetting Mathematics with LaTeX

Reading and Writing Data Files

Uncertainty Calculations

Histogramming and Binning Data

Linear Regression (put in same directory as the program using the "linear_fit" function)

Curve Fitting (put in the same directory as programs using the "general_fit" function)

Root Finding (Solutions to a Transcendental Equation)

Numerical Integration (Quadrature)

Solving Differential Equations with the Euler-Cromer Method

Solving Ordinary Differential Equations (ODEs)

Making Contour Plots

Making Vector Field Plots

Using the Relaxation Method to Solve Laplace's Equation

If you are already familiar with Matlab, R, or IDL, Mathesaurus will show you what equivalent commands in Python are.

These tutorials are maintained by Alan DeWeerd.

web stats