In: Computer Science
This problem is adapted from Exercise 37 of Langtangen’s “A Primer on Scientific Programming with Python”.
Simulating gas particles in a closed space is something you can do with a Monte Carlo type simulation – simulate a period of time (using a loop) and “move” particles around (change values for (x,y) coordinates) for each unit of time. This kind of simulation is called a “random walk”.
Download the file randomwalk.py(i posted it below). This implements a Monte Carlo simulation for a random 2-dimensional walk. It uses numpy arrays, but it does not use them idiomatically.
Your task for this exercise is to change this program to use numpy arrays idiomatically. That is, the block
for i in range(np): direction = random.randint(1, 4) if direction == NORTH: ypositions[i] += 1 elif direction == SOUTH: ypositions[i] -= 1 elif direction == EAST: xpositions[i] += 1 elif direction == WEST: xpositions[i] -= 1
should be rewritten to use numpy arrays of random integers.
Below is randomwalk.py
import random
random.seed(10122019)
import numpy
import matplotlib.pyplot as plt
def random_walk_2D(np, ns):
# xpositions = numpy.zeros(size=np) #### part 1
# ypositions = numpy.zeros(size=np)
positions = numpy.zeros(size=(np, 2)) #### part 2
# extent of the axis in the plot:
xymax = 3*numpy.sqrt(ns); xymin = -xymax
for _ in range(ns):
for i in range(np):
direction = random.randint(1, 4)
if direction == NORTH:
ypositions[i] += 1
elif direction == SOUTH:
ypositions[i] -= 1
elif direction == EAST:
xpositions[i] += 1
elif direction == WEST:
xpositions[i] -= 1
# xpositions += numpy.random.randint(-1, 2, size=np) #### part
1
# ypositions += numpy.random.randint(-1, 2, size=np) #### part
1
positions += numpy.random.randint(-1, 2, size=(np, 2)) #### part
2
# plt.plot(xpositions, ypositions, 'ko') #### part 1
plt.plot(positions[:,0], positions[:,1], 'ko') #### part2
plt.axis([xymin, xymax, xymin, xymax])
plt.title('{particles} particles after {steps}
steps'.format(particles=np, steps=ns))
plt.savefig('tmp_{steps}.pdf'.format(steps=ns))
return positions[:,0], positions[:,1]
np = 100 # number of particles
ns = 100 # number of steps
x, y = random_walk_2D(np, ns)
ANSWER:
CODE TEXT
#Below is randomwalk.py
import numpy
# seeding random of numpy
numpy.random.seed(10122019)
import matplotlib.pyplot as plt
def random_walk_2D(np, ns):
# xpositions = numpy.zeros(size=np) #### part 1
# ypositions = numpy.zeros(size=np)
positions = numpy.zeros(shape=(np, 2)) #### part 2
# extent of the axis in the plot:
xymax = 3*numpy.sqrt(ns); xymin = -xymax
for _ in range(ns):
# need random array of 1 and -1 and not 0, because moment of
particle
# in each step is compullsory
# step 1. fetching random array of 1 and 0, of size npx2
random_array = numpy.random.randint(0, 2, size=(np, 2))
# step 2. converting 0's to -1
# using numpy.where to find 0's and replacing them with -1
con_array=numpy.where(random_array==0,-1,random_array)
# adding the con_array to positions array to modify positions for
this step
positions += con_array
# plt.plot(xpositions, ypositions, 'ko') #### part 1
plt.plot(positions[:,0], positions[:,1], 'ko') #### part2
plt.axis([xymin, xymax, xymin, xymax])
plt.title('{particles} particles after {steps}
steps'.format(particles=np, steps=ns))
plt.savefig('tmp_{steps}.pdf'.format(steps=ns))
return positions[:,0], positions[:,1]
np = 100 # number of particles
ns = 100 # number of steps
x, y = random_walk_2D(np, ns)
CODE IMAGE
OUTPUT IMAGE