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 randomwalk 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 so the jumps can be different sizes, in addition to different directions Consider the situation where the ant makes a total of jumps.
a In the cell below, write a program to run 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: plthistdistnumber of bins Remember to label your axes.
b The distance from the starting point the origin can be found using the npabs 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 antwalk and return the average distance travelled by the ant. To calculate the average distance you can use npmeandist
d Using a for loop, call your function, each time changing the number of steps,
but keeping the number of experiments equal to As steps use
Plot the average distance moved as a function of the number of steps
pltplot
Hint: You can create a list of steps and loop over the list. An example is given:
for s in steps:
prints
The index 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 in steps:
L append s
This code just copies all the values of steps into the list
# Write code here
e Use the NumPy function power to plot different integer powers of versus
steps. See if you can identify the power which yields a straight line plot, ie
plot versus for a choice of which makes close to a straight
line. Before you explore this, you might like to generate 'good' values of by
running your code in the previous cell for experiments for each step value.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
