In: Computer Science
Python Please
Simulating a random walk in two dimensions.
For this part, you will use a two-dimensional grid where the x axis goes from -50 to +50 and the y axis also goes from -50 to +50. The particle will start at the middle of the left edge (x, y) = (−50,0), and we’ll put the outlet/absorber in the middle of the right edge (x, y) = (+50,0). Again, the particle will diffuse around randomly between these points and when the absorbing point at (x, y) = (+50,0) is reached, the simulation ends. At each time step, the particle can move one step on the grid and your simulation will update the particle’s x and y positions accordingly. Here are the rules for the random walk:
At each time step:
1. If the particle is not at an edge, it can take a step of one grid spacing in any of four directions: ±x, ±y; these should all be equally probable.
2. If the particle is at one of the edges, but not at the absorber, it cannot pass out of the box. It should have a 50% probability of taking a step perpendicularly away from the wall, and a 25% chance of stepping in either of the directions along the wall.
3. If the particle is at a corner, it has a 50% chance of going in either of the two directions along the walls.
4. If the particle touches the absorbing point at (x, y) = (+50,0), the random walk ends.
Part 2. Simulate a single 2d random walk until absorption.
D. (20 pts) Write a program that executes one ‘walk’ and calculates the number of steps the particle takes before being absorbed – the residence time of our two-dimensional lake for that tracer. You will be assessed on whether you have correctly implemented the walking rules above.
E. (10 pts) Make a plot of the trajectory taken by the particle (positions in x,y).
a. Label the x and y axes appropriately, and give the plot a title
b. Indicate the starting position with a black asterisk and the end position with a red asterisk.
F. (5 pts) Print the time (i.e. number of steps) it took for the particle to be absorbed.
Program:
import random
import pandas as pd
import matplotlib.pyplot as plt
for_graph = pd.DataFrame(columns= ['X', 'Y'])
x, y = -46, 0 # Input the starting point here.
x_list = [x]
y_list = [y]
while (x != -45 or y != 0): # Change Value of x and y according to where you want the absorbtion point to be
if x in range(-50, 50) and y in range(-50, 50):
a, b = x - 1, x + 1
i, j = y - 1, y + 1
if a in range(-50, 50) and b in range(-50, 50):
random_x = random.choices((a, b, x), (0.34, 0.33, 0.33))
if random_x == [x]:
random_y = random.choices((i, j), (0.5, 0.5))
y = int(random_y[0])
y_list = y_list + random_y
else:
random_y = y
y = random_y
y_list = y_list + [random_y]
x_list = x_list + random_x
x = int(random_x[0])
elif x == 50 or y == 50:
a = x - 1
i, j = y + 1, y - 1
random_x = random.choices((a, x), (0.75, 0.25)) # Increased probability to move away from the walls
if random_x == [x]:
random_y = random.choices((i, j), (0.5, 0.5))
y = int(random_y[0])
y_list = y_list + random_y
else:
random_y = y
y = random_y
y_list = y_list + [random_y]
x_list = x_list + random_x
x = int(random_x[0])
elif x == -50 or y == -50:
a = x + 1
i, j = y + 1, y - 1
random_x = random.choices((a, x), (0.5, 0.5))
if random_x == [x]:
random_y = random.choices((i, j), (0.75, 0.25)) # Increased probability to move away from the walls
else:
random_y = y
y = random_y
y_list = y_list + [random_y]
x_list = x_list + random_x
x = int(random_x[0])
for_graph['X'] = x_list
for_graph['Y'] = y_list
print(f"Number of steps taken for the particle to be absorbed is: {for_graph.index.stop}") # Number of steps display
display(for_graph)
for_graph.plot(x='X', y='Y', title = 'Particle Movement Tracker')
plt.show()
Output:
Note: I have taken a relatively smaller distance between the start and end points to simulate the output. You can change the start and end points in the program or can insert a input function.