# PH204 Physics Simulation

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

# Resources

Course information

Python Visualizer

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.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.3

Kapitza's pendulum video