Skip to content

Instantly share code, notes, and snippets.

@cstein
Created May 2, 2012 13:51
Show Gist options
  • Save cstein/2576651 to your computer and use it in GitHub Desktop.
Save cstein/2576651 to your computer and use it in GitHub Desktop.
Solution of exercise 1
import random
import pylab as plt
npart = 100
nsteps = 10000
dt = 0.001
X = [random.random() for i in range(npart)]
Y = [2*(random.random() - 0.5) for i in range(npart)]
dX = [2*(random.random() - 0.5) for i in range(npart)]
dY = [2*(random.random() - 0.5) for i in range(npart)]
# make nsteps steps
for n in range(nsteps):
# loop over each particle
for i in range(npart):
# if the particle is exiting the box in the x-direction
if abs(X[i] + dt*dX[i])>1:
dX[i] = -dX[i]
if abs(Y[i] + dt*dY[i])>1:
dY[i] = -dY[i]
# make the actual step
X[i] = X[i]+dt*dX[i]
Y[i] = Y[i]+dt*dY[i]
# plot final positions
plt.clf()
plt.plot(X,Y,'bo')
plt.axis((-1,1,-1,1))
plt.savefig('final_positions.png')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment