Question

In: Computer Science

Python Please Simulating a random walk in two dimensions. For this part, you will use a...

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.

Solutions

Expert Solution

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.


Related Solutions

Please use Python 3 3). Write a function that writes a series of random numbers to...
Please use Python 3 3). Write a function that writes a series of random numbers to a text file named ‘random_number.txt’. Each random number should be in the range of 1 through 500. The function should let the user specify how many random numbers the file will hold. Then write another function that reads the random numbers from the ‘random_number.txt’ file, displays the numbers, and then displays the total of the numbers and the number of random numbers read from...
For python! You walk into a fast food restaurant and order fries, a burger, and a...
For python! You walk into a fast food restaurant and order fries, a burger, and a drink. Create a simple text based user interactive program keeps track of the menu items you order. Use a dictionary to keep track of your menu items and price and another dictionary to keep track of your order and quantity.  Fries are $3.50  Burger are $5.00  Drinks are $1.00  Sales tax rate is 7.25% menu = { "burger":5.00, "fries":3.50, "drink":1.00...
Please succinctly explain the difference between Random Walk and Martingale hypothesis
Please succinctly explain the difference between Random Walk and Martingale hypothesis
how do you code a psuedo-random probability(%) in python? please provide an example.
how do you code a psuedo-random probability(%) in python? please provide an example.
Use the reflection principle to find the number of paths for a simple random walk from...
Use the reflection principle to find the number of paths for a simple random walk from S0=2 to S15= 5 that do not hit the level at k=6.
The first random number generator comes from Python itself. To use it you need to import...
The first random number generator comes from Python itself. To use it you need to import the Python package. Then call the random() method. See below. import random print (random.random()) It is important to know that a random number generator really is random. If so, it should have uniform distribution between 0 and 1. Test the random number generators in Python for uniformity.
In this part of the assignment, you will need to calculate the cross-correlation of two random...
In this part of the assignment, you will need to calculate the cross-correlation of two random signals. In the Appendix: Part II, we include the function ccorr that gives you the crosscorrelation of two input vectors. MATLAB provides other built-in function that may be useful for this part of the assignment • max: gives you the maximum value of a vector. For more information type help max find: gives you the index corresponding to a value of a vector. For...
***PYTHON PLEASE*** I'd also like to use decimal to two places when calculating the money $$...
***PYTHON PLEASE*** I'd also like to use decimal to two places when calculating the money $$ values for budget and fee areas. Thank you! -------------------------------------------------------- Create a Park class. Include the following seven data members: Name of Park Location Type of Park (National, State or Local) Fee Number of Employees Number of visitors reported for the past 12 months Annual Budget Write your __init__ class In addition write four separate instance methods that: Return a string containing the name of...
Walk then Run Compute your average velocity in the following two cases. (a) You walk 77.7...
Walk then Run Compute your average velocity in the following two cases. (a) You walk 77.7 m at a speed of 1.22 m/s and then run 77.7 m at a speed of 3.05 m/s along a straight track. m/s (b) You walk for 1.69 min at a speed of 1.22 m/s and then run for 1.69 min at 3.05 m/s along a straight track. Incorrect: Your answer is incorrect. m/s (c) Graph x versus t for both cases and indicate...
PLEASE USE PYTHON THANK YOU In the game of Lucky Sevens, the player rolls a pair...
PLEASE USE PYTHON THANK YOU In the game of Lucky Sevens, the player rolls a pair of dice. If the dots add up to 7, the player wins $4; otherwise, the player loses $1. Suppose that, to entice the gullible, a casino tells players that there are many ways to win: (1, 6), (2, 5), and soon. A little mathematical analysis reveals that there are not enough ways to win to make the game worthwhile; however, because many people's eyes...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT