Question: Use python We will write code to describe the random - walk of an ant which starts at the origin. At each time step the

Use python
We will write code to describe the random-walk of an ant which starts at the origin. At each time step the ant jumps either to the left or the right. Let's assume that the jump can therefore be any number in the range [1,1)(so the jumps can be different sizes, in addition to different directions). Consider the situation where the ant makes a total of 100 jumps.
a) In the cell below, write a program to run 500 experiments of this random walk. For each of the experiments, you need to save the final position in an array, for example, dist.
Plot a histogram of the final positions that the ant reaches: plt.hist(dist,number of bins). Remember to label your axes.
b) The distance from the starting point (the origin) can be found using the np.abs function, which returns the absolute value of a quantity. Write code that plots the histogram of the absolute values of your final position in the previous part.
c) Transform your code in part a) into a function that takes as input the number of experiments (Ne) and the number of steps (Ns). Name your function ant_walk() and return the average distance travelled by the ant. To calculate the average distance you can use np.mean(dist)
d) Using a for loop, call your function, each time changing the number of steps,
but keeping the number of experiments equal to 500. As steps use 50,100,150,
200,...,2000.
Plot the average distance moved as a function of the number of steps
( plt.plot (x,y)
Hint: You can create a list of steps and loop over the list. An example is given:
for s in steps:
print(s)
The index s will step through the values in the list steps. You can pass this
index to your function. To save the value of the mean, you can create an empty list
and append the mean value to it using .append()
L =[]
for s in steps:
L. append (s)
This code just copies all the values of steps into the list L.
# Write code here
e) Use the NumPy function power () to plot different integer powers of L versus
steps. See if you can identify the power which yields a straight line plot, i.e.
plot Ln versus n for a choice of n which makes Ln close to a straight
line. Before you explore this, you might like to generate 'good' values of L by
running your code in the previous cell for 10000 experiments for each step value.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!