Question

Part 1. Simulate a single 1d random walk until absorption. Walking Rules 1. If the particle...

Part 1. Simulate a single 1d random walk until absorption.

Walking Rules

1. If the particle is not at the right edge or the left edge, it can take a step of one grid spacing either to the left or to the right with equal probability.

2. If the particle is at the left edge (x = −50), it cannot leave the system and should always take a step to the right (sometimes called a “reflective” boundary condition).

3. If the particle touches x = +50, the random walk ends.

A. (15 pts) Write a program that executes one ‘walk’ starting from x = −50 and calculates the number of steps the particle takes before being ‘absorbed’. Since each step takes one unit of time, you are also finding the time to absorption, also called the residence (or first passage time). You will be assessed on whether you have correctly implemented the walking rules above. It might be helpful to use the following function: np.random.randint(low,high) which picks a random integer between the integer value ‘low’ and ‘high’ (including low but excluding high).

B. (5 pts) Make a plot of the x position of the particle as a function of step number. Use the matplotlib library. Label the horizontal and vertical axes appropriately and give the plot a title

C. (5 pts) Print the time (i.e. number of steps) it took for the particle to be absorbed.

Homework Answers

Answer #1

ANSWER:

  • I have provided the properly commented code in both text and image format so you can easily copy the code as well as check for correct indentation.
  • I have provided the output image of the code so you can easily cross-check for the correct output of the code.
  • Have a nice and healthy day!!

CODE TEXT

# importing functions
import numpy as np
import matplotlib.pyplot as plt

# A. Executing one walk
# defining initial position of x
x = -50
# defining x_steps list to record all steps
x_steps = []
# while loop till x touches +50
while x<50:
# if x is -50 always move right
if x==-50:
x +=1
else:
# otherwise move randomly
# using np.random.randint to generate number 1 or 2
rand = np.random.randint(1,3)
# if 1 move left
if rand==1:
x -=1
else:
# otherwise right
x += 1
# recording current x in list x_steps
x_steps.append(x)
  
# B. plot x position w.r.t stepnumber using matplotlib library
X = np.arange(1,len(x_steps)+1)
# using plot function and passing X and Y axis values
plt.plot(X,x_steps)
# labeling plot
plt.xlabel("Step Number")
plt.ylabel("X-Position")
plt.title("Particle X-Position as a function of Step Number")
plt.show()

# C. time it took to get particle to absorbed
print("Time taken by particle to get absorbed is: {} secs".format(len(x_steps)))

CODE IMAGE

OUTPUT IMAGE

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT