Programs and data

This page contains Python programs and data that accompany the book Computational Physics, 2nd edition, by Mark Newman. You're welcome to download and use these resources freely. Comments and questions are welcome.

Resources are listed by chapter, programs first, then data files, if any. Also included are the programs from the appendices. There are no programs or data for chapters 1 or 12.

All the resources on this page can be downloaded at once, as a single zip file, by clicking here.


Chapter 2: Python programming for physicists

dropped.py Calculate the position of a ball dropped from a tower
evenodd.py Check two integers to ensure one is even and the other odd
fibonacci.py Print out the Fibonacci numbers up to 1000
polar.py Convert from polar to Cartesian coordinates
rydberg.py Print out the wavelengths of hydrogen lines

Chapter 3: Graphics and visualization

circular.py Make a density plot from the data in a file
hrdiagram.py Calculate and display a Hertzsprung–Russell diagram for a catalog of nearby stars
lattice.py Create a visualization of a square lattice
revolve.py Make a circle revolve around the screen
ripples.py Calculate and display the interference pattern generated by two circular sets of waves
spin.py Make a square spin around on the screen
 
circular.txt Data for the circular pattern in Fig. 3.5
stars.txt Catalog of temperatures and magnitudes for 7860 nearby stars
stm.txt STM measurements of the (111) surface of silicon
sunspots.txt Data on sunspots since 1749

Chapter 4: Accuracy and speed

madelung.py Calculate the Madelung constant for sodium chloride

Chapter 5: Integrals and derivatives

autodiff.py Demonstration of automatic differentiation
gaussint.py Evaluate an integral using Gaussian quadrature
intinf.py Evaluate an integral over an infinite domain
trapezoidal.py Evaluate an integral using the trapezoidal rule
 
altitude.txt Altitude in meters of points on the Earth's surface
stm.txt STM measurements of the (111) surface of silicon
velocities.txt Velocity of a particle over time

Chapter 6: Solution of linear and nonlinear equations

atanh.py Calculate an inverse hyperbolic tangent by Newton's method
buckingham.py Find the minimum of the Buckingham potential using golden ratio search
ferromag.py Calculate the magnetization of a ferromagnet
gausselim.py Solve simultaneous equations by Gaussian elimination
springs.py Calculate the motion of system of masses and springs
springsb.py Simpler program for the masses and springs

Chapter 7: Fourier transforms

dft.py Calculate a DFT the slow way
fft.py Example of a fast Fourier transform
 
blur.txt Digitized blurry image
dow.txt Daily closing values of the Dow from 2006 to 2010
dow2.txt Daily closing values of the Dow from 2004 to 2008
piano.txt Waveform of a single note played on a piano
pitch.txt An oscillating signal with noise
sunspots.txt Data on sunspots since 1749
trumpet.txt Waveform of a single note played on a trumpet

Chapter 8: Ordinary differential equations

bulirsch.py Solve the nonlinear using the Bulirsch-Stoer method
euler.py Solve a differential equation using Euler's method
odeinf.py Solve a differential equation out to infinity
odesim.py Solve simultaneous first-order differential equations
rk2.py Solve a differential equation using 2nd-order Runge-Kutta
rk4.py Solve a differential equation using 4th-order Runge-Kutta
squarewell.py Solve the Schrodinger equation in a square well
throw.py Calculate a trajectory using the shooting method

Chapter 9: Partial differential equations

laplace.py Solve Laplace's equation using the Jacobi method
neumann.py Solve the heat equation with Neumann (fixed gradient) boundary conditions
heat.py Solve the heat equation using FTCS

Chapter 10: Random processes and Monte Carlo methods

decay.py Calculate the decay of a radioactive sample
gengauss.py Monte Carlo integration using importance sampling and Gaussian random numbers
lcg.py Linear congruential random number generator
mcint.py Monte Carlo integration
mcsim.py Monte Carlo simulation of an ideal gas
rutherford.py Rutherford scattering
salesman.py Solution of the traveling salesman problem

Chapter 11: Data science

exokde.py Make a kernel density estimate of the distribution of exoplanet radii
exoplanets.py Make a histogram of exoplanet radii
hooke.py Fit a straight line to estimate a spring constant
isotopes.py Make a scatter plot of the atomic masses and atomic numbers of isotopes
sound.py Perform a quadratic fit to data for the speed of sound
 
exoplanets.txt Radii of known exoplanets
hooke.txt Data for Hooke's law example
isotopes.txt List of atomic isotopes
sound.txt Speed of sound at various temperatures

Useful programs

banded.py Solve a banded system of linear equations
colormaps.py Colormaps useful for physics visualizations
dcst.py Fast discrete cosine and sine transforms
gaussxw.py Calculate integration points for Gaussian quadrature
meshplot.py Make a density plot of data on a triangulated mesh
qdraw.py Functions for drawing and animating simple diagrams