Python & Physics Revision: Programming, Lists, Arrays, Algorithms, & Physics Concepts, Study notes of Physics

This revision sheet provides an overview of the key concepts in python programming, lists, arrays, algorithms, and physics that will be covered in the phy300 course. It includes instructions on how to assign variables, create lists and arrays, access list elements, loop through lists, create spheres, and use functions. Additionally, it covers important physics concepts such as newton's laws, euler method, and monte carlo algorithm.

Typology: Study notes

Pre 2010

Uploaded on 08/09/2009

koofers-user-u4v-1
koofers-user-u4v-1 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
PHY300- Revision Sheet
You should look over carefully all posted lecture notes. Additionally I would
recommend looking at your lab writeups including a brief review of the main
elements in your final lab codes. The questions will be mostly short reponse
questions - write out your answer in the format requested (eg. up to 4 lines of
code, a few short sentences or a single name). They will be designed to test
basic understanding of both programing and physics issues there will be no
long calculations or difficult code to interpret. To try and clarify the kinds of
things I will expect you to be able to handle please study the examples below
- if you don’t understand something I suggest you go back to the appropriate
lecture.
1 Python
1. Know how to assign and do arithmetic operations on simple variables
in Python eg.
x=1.0
y=2.0
z=x/y
print z
2. Know how to create a list
Explicitly eg. x=[1.0,2.0,3.0]
Using the arange() function eg x=arange(1.0,4.0,1.0) Notice
the latter yields (real) floating point numbers (1.0,2.0,3.0) but not
4.0. For an integer list use the simple range() function.
3. Know how to access an element of a list eg x[2] which would yield the
result 3.0 on the above list. Remember list elements start with index
0 not 1.
4. Know how to loop over all elements of a list
for i in range(0,4):
x[i]=x[i]+3.0
1
pf3
pf4
pf5

Partial preview of the text

Download Python & Physics Revision: Programming, Lists, Arrays, Algorithms, & Physics Concepts and more Study notes Physics in PDF only on Docsity!

PHY300- Revision Sheet

You should look over carefully all posted lecture notes. Additionally I would recommend looking at your lab writeups including a brief review of the main elements in your final lab codes. The questions will be mostly short reponse questions - write out your answer in the format requested (eg. up to 4 lines of code, a few short sentences or a single name). They will be designed to test basic understanding of both programing and physics issues – there will be no long calculations or difficult code to interpret. To try and clarify the kinds of things I will expect you to be able to handle please study the examples below

  • if you don’t understand something I suggest you go back to the appropriate lecture.

1 Python

  1. Know how to assign and do arithmetic operations on simple variables in Python eg.

x=1. y=2. z=x/y print z

  1. Know how to create a list
    • Explicitly eg. x=[1.0,2.0,3.0]
    • Using the arange() function eg x=arange(1.0,4.0,1.0) Notice the latter yields (real) floating point numbers (1.0,2.0,3.0) but not 4.0. For an integer list use the simple range() function.
  2. Know how to access an element of a list eg x[2] which would yield the result 3.0 on the above list. Remember list elements start with index 0 not 1.
  3. Know how to loop over all elements of a list

for i in range(0,4): x[i]=x[i]+3.

Again, notice that range(0,4) produces the list (0,1,3)

  1. Know how to create an array. Arrays are like lists but are more efficient in numerical work. A 1D array can be defined by passing it a list eg y=array([1.0,2.0,3.0]), or y=array(arange(1.0,4.0,1.0)).
  2. If we want to define a one dimensional array of say 100 floats which are initially zero we can say y=zeros((100),Float). A two dimensional array of size (100,100) is defined similarly y=zeros((100,100),Float). Elements may be accessed using the command y[i][j] or y[i,j]
  3. Example of using an array in conjunction with a for loop

for i in range (0,100): for j in range (0,100): y[i][j]=y[i][j]+1.

  1. while loops. Eg the main loop of a simulation might read

while(1): t=t+dt update(y) if(t>10.0): finishup()

  1. Note tabbing crucial in Python. In above code everything at same tab level is executed each time around the while loop. But only if the if statement is true does control pass to the function finishup(). The latter is said to be under control of the if statement.
  2. if statements allow flow through program to depend on logical tests (see above)
  3. Functions. Take arguments. Compute one or more values. Must be defined either in a separate module or earlier in the code before being used. Simpifies reading of long/complex code.

def update(y): for i in range (0,100): for j in range (0,100): y[i][j]=y[i][j]+1.

2 Algorithms

  1. Euler. An equation of form dy dt = f (y, t) is solved with the iteration

y[n+1]=y[n]+dt*f(y[n],t)

Notice this uses a Python array/list to hold the values of y at different discrete times t=ndt.

  1. Know how to write Newton’s 2nd law as a pair of such equations.
  2. Leapfrog algorithm:

x[n+1]=x[n]+p[n]dt+a[n]dtdt0. p[n+1]=p[n]+dt0.5(a[n]+a[n+1])

  1. Simple Monte Carlo algorithm. ∫ (^) b

a

f (x)dx ∼

b − a N

∑^ N

i=

f (xi)

where xi are independent random numbers uniformly distributed in range a < x < b. Error goes as (< x^2 i > − < xi >^2 )/

N.

  1. Bisection algorithm. Find a zero of some function f (x) by finding some interval xl < x < xu in which f (xl) ∗ f (xu) < 0. Examine midpoint and from the sign of f (x) there determine a new interval for the zero which is now half the original interval.
  2. Metropolis algorithm. For simulating system with many d.o.f undergo- ing thermal fluctuations. Update system according to probability e−∆H where H is energy. Look in notes for details of the algorithm steps.

3 Physics

  1. Newton’s laws describing motion of particle in some potential V (x).
  2. Extension to collections of particles. Mutual separation dependent forces and molecular dynamics simulations. Expalnation of pressure and temperature in terms of molecular motion eg. change in momen- tum under collision with walls, mean kinetic energy.
  3. Statistical mechanics. Give up on detailed description of system and discuss only probabilities of finding system in some microstate. Ba- sic assumption – probability is e−H/kT^. Critical phenomena. Tune eg temperature. System undergoes phase transition. Characterized by power law behavior for thermodynamic quantities. Critical exponents. Structure at all length scales.
  4. 2d Ising model. Basics of model. Energy function. Details of Metropo- lis algorithm.
  5. Waves. Arise as collective behavior of an ensemble of atoms/molecules which oscillate around equilibrium positions according to approximately harmonic forces. Velocity of waves in terms of spring constant k, mass of atoms m and interatom separation a

v^2 = ka^2 /m

  1. Quantum systems. Know Schr¨odinger equation (time independent and time dependent). Probability interpretation. Normalization of wave- function. Numerical solution – need Ψ to go to zero fast enough as x → ±∞ so that

∫ dxΨ∗Ψ = finite.