PH204 Physics Simulation

Upload submissions via Blackboard or email attachment if there are problems.

Resources

Course information

Python Visualizer

Tutorial; NumPy; SciPy; matplotlib; PH-113 Python Primer; SciPy Lectures

Part 0: The fundamentals

See PH-113 Laboratory Skills/Python Primer

Competence 0

Part 1: Fundamentals, functions, and finding numerical solutions

Material 1

Prime-number checking function

def isprime(N):
    if N <= 2:
        return False
    for n in range(2,N):
        if (N%n)==0:
            return False
        if n**2 > N:
            break
    return True

Code for use in Assignment 1 Question 3: projectile.py

Part 2: Numerical integrals, interpolation, and fitting to data

Newton's method implementation

def newton(f, x0, fprime=None, tol=1e-6, maxiter=50):
    if fprime is None:
        def fprime(x):
            h = 1e-6
            return (f(x+h)-f(x))/h
    x = x0
    for n in range(maxiter):
        x -= f(x)/fprime(x)
        if abs(f(x)) < tol:
            return x
    return x

Lecture 2.1; Practice 2.1; solutions

Lecture 2.2; Practice 2.2; solutions

Lecture 2.3

Data for using in Assignment 1: saturn.txt and spectra.csv

Part 3: Matrices, randomness, and solving ODEs

Various solvers available for use here: solvers.py

from solvers import euler, rk4, rkscipy, sde

Euler should generally not be used for real problems.

RK4 is generally pretty great, but RK6 is better and is implemented in scipy.integrate.

Lecture 3.1; Practice 3.1; solutions

Lecture 3.2; Practice 3.2; solutions

Lecture 3.3

Kapitza's pendulum video

Part 4: Numerical stability, Multiprocessing, Classes, Symbolic

Lecture 4.1; Practice 4.1; solutions a & solutions b

Lecture 4.2; ising.py; Practice 4.2; solutions

Lecture 4.3; Tektronix.py; Mathematica;

social