Matlab exercise

Discussion in 'Programmer's Corner' started by smarch, Jul 8, 2010.

1. smarch Thread Starter Active Member

Mar 14, 2009
52
0
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:

Code ( (Unknown Language)):
1.
2. [COLOR=black][FONT=Courier New]y(1) = 0;[/FONT][/COLOR][FONT=Courier New][/FONT]
3. [COLOR=black][FONT=Courier New]x(1) = 0;[/FONT][/COLOR][FONT=Courier New][/FONT]
4. [COLOR=blue][FONT=Courier New]for[/FONT][/COLOR][COLOR=black][FONT=Courier New] n=1:10000,[/FONT][/COLOR][FONT=Courier New][/FONT]
5. [COLOR=black][FONT=Courier New]    p = rand(1,2);[/FONT][/COLOR][FONT=Courier New][/FONT]
6. [COLOR=blue][FONT=Courier New]if[/FONT][/COLOR][COLOR=black][FONT=Courier New] p(1)<0.25[/FONT][/COLOR][FONT=Courier New][/FONT]
7. [COLOR=black][FONT=Courier New]    x(n+1)=x(n)-1;[/FONT][/COLOR][FONT=Courier New][/FONT]
8. [COLOR=blue][FONT=Courier New]else[/FONT][/COLOR][COLOR=black][FONT=Courier New] [/FONT][/COLOR][COLOR=blue][FONT=Courier New]if[/FONT][/COLOR][COLOR=black][FONT=Courier New] p(1)>0.75[/FONT][/COLOR][FONT=Courier New][/FONT]
9. [COLOR=black][FONT=Courier New]        x(n+1)=x(n)+1;[/FONT][/COLOR][FONT=Courier New][/FONT]
10. [COLOR=black][FONT=Courier New]    [/FONT][/COLOR][COLOR=blue][FONT=Courier New]else[/FONT][/COLOR][COLOR=black][FONT=Courier New] x(n+1)=x(n);[/FONT][/COLOR][FONT=Courier New][/FONT]
11. [COLOR=black][FONT=Courier New]    [/FONT][/COLOR][COLOR=blue][FONT=Courier New]end[/FONT][/COLOR][COLOR=black][FONT=Courier New];[/FONT][/COLOR][FONT=Courier New][/FONT]
12. [COLOR=blue][FONT=Courier New]end[/FONT][/COLOR][FONT=Courier New][/FONT]
13. [COLOR=black][FONT=Courier New]   [/FONT][/COLOR][COLOR=blue][FONT=Courier New]if[/FONT][/COLOR][COLOR=black][FONT=Courier New] p(2)<0.25[/FONT][/COLOR][FONT=Courier New][/FONT]
14. [COLOR=black][FONT=Courier New]    y(n+1)=y(n)-1;[/FONT][/COLOR][FONT=Courier New][/FONT]
15. [COLOR=blue][FONT=Courier New]else[/FONT][/COLOR][COLOR=black][FONT=Courier New] [/FONT][/COLOR][COLOR=blue][FONT=Courier New]if[/FONT][/COLOR][COLOR=black][FONT=Courier New] p(2)>0.75[/FONT][/COLOR][FONT=Courier New][/FONT]
16. [COLOR=black][FONT=Courier New]        y(n+1)=y(n)+1;[/FONT][/COLOR][FONT=Courier New][/FONT]
17. [COLOR=black][FONT=Courier New]    [/FONT][/COLOR][COLOR=blue][FONT=Courier New]else[/FONT][/COLOR][COLOR=black][FONT=Courier New] y(n+1)=y(n);[/FONT][/COLOR][FONT=Courier New][/FONT]
18. [COLOR=black][FONT=Courier New]    [/FONT][/COLOR][COLOR=blue][FONT=Courier New]end[/FONT][/COLOR][COLOR=black][FONT=Courier New];[/FONT][/COLOR][FONT=Courier New][/FONT]
19. [COLOR=black][FONT=Courier New]   [/FONT][/COLOR][COLOR=blue][FONT=Courier New]end[/FONT][/COLOR][FONT=Courier New][/FONT]
20. [COLOR=blue][FONT=Courier New]end[/FONT][/COLOR][FONT=Courier New][/FONT]
21. [COLOR=black][FONT=Courier New]plot(x,y)[/FONT][/COLOR][FONT=Courier New][/FONT]
22.

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.