Matlab exercise

Thread Starter

smarch

Joined Mar 14, 2009
52
I need a bit of help with this:


Write an m–file to simulate a random walk in a 2–D plane starting at the origin (0,0). Suppose that at some stage in the walk the walker is at (x,y). Generate two random numbers between 0 and 1 using the rand(1,2) function. If the first of the random numbers is less than 1/4 reduce x by 1, if it is between 1/4 and 3/4 leave x unchanged and if it greater than 3/4 increase x by 1. In this way the probability of moving left is 1/4 , the probability of remaining at the same x coordinate is 1/2 and the probability of moving right is 1/4. Repeat the process with the second random number and the y coordinate. In this way the particle can move to any of the locations indicated below. It will stay put with probability 1/4 ´ 1/4 = 1/4 will move due north, south, east or west with probability 1/2 ´ 1/4 = 1/8 each and will move diagonally with probability 1/4 ´ 1/4 = 1/16. Note that 1/4 + 4 ´ 1/8 + 4 ´ 1/16 = 1.
Start you particle at (x,y) = (0,0) and run the process 10,000 times using a for loop. Plot the path of your particle

I have done all this using the code:

Rich (BB code):
y(1) = 0;
x(1) = 0;
for n=1:10000,
    p = rand(1,2);
if p(1)<0.25
    x(n+1)=x(n)-1;
else if p(1)>0.75
        x(n+1)=x(n)+1;
    else x(n+1)=x(n);
    end;
end
   if p(2)<0.25
    y(n+1)=y(n)-1;
else if p(2)>0.75
        y(n+1)=y(n)+1;
    else y(n+1)=y(n);
    end;
   end
end
plot(x,y)

But the next part then asks:

Now edit the m-file to become a function whose input is the number of steps you wish to run the random walk for and whose outputs are the maximum distance and the average distance that the particle gets from the origin.

I am really unsure what to do here, I don't really understand what it is asking me if I'm honest.

Any help please?
 
Top