Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
random walk & random number

Subject: random walk & random number

From: nawal

Date: 6 Nov, 2013 11:59:08

Message: 1 of 6

i've created a random wak and plotted it but what I’m stuck on is the last bit (the rg2) because if I run the file I get a value for rg2 and if I run it again I get different value. I want to find a way to get the different values of rg2 at once instead of trying to run it every time and with that I need to plot the values. I don’t know how to do this bit and I’m stuck so if anyone knows how to do please tell me.
r=[0 0];
X=[0];Y=[0];
for t= 1:1:100
      B=rand(1,1)*4;
    if B<1
        new_position=r+[1 0];
    elseif B<2
        new_position=r+[0 1];
    elseif B<3
        new_position=r+[-1 0];
    else
        new_position=r+[0 -1];
    end
    X=[X new_position(1)];
    Y=[Y new_position(2)];
    r=new_position;
end
plot(X,Y)
rg2=mean(X.^2)+mean(Y.^2)-mean(X)^2-mean(Y)^2;

Subject: random walk & random number

From: Roger Stafford

Date: 6 Nov, 2013 18:08:18

Message: 2 of 6

"nawal " <nawalyarey@gmail.com> wrote in message <l5dauc$5p$1@newscl01ah.mathworks.com>...
> i've created a random wak and plotted it but what I’m stuck on is the last bit (the rg2) because if I run the file I get a value for rg2 and if I run it again I get different value. I want to find a way to get the different values of rg2 at once instead of trying to run it every time and with that I need to plot the values. I don’t know how to do this bit and I’m stuck so if anyone knows how to do please tell me.
> r=[0 0];
> X=[0];Y=[0];
> for t= 1:1:100
> B=rand(1,1)*4;
> if B<1
> new_position=r+[1 0];
> elseif B<2
> new_position=r+[0 1];
> elseif B<3
> new_position=r+[-1 0];
> else
> new_position=r+[0 -1];
> end
> X=[X new_position(1)];
> Y=[Y new_position(2)];
> r=new_position;
> end
> plot(X,Y)
> rg2=mean(X.^2)+mean(Y.^2)-mean(X)^2-mean(Y)^2;
- - - - - - - - - -
  What you have calculated here in rg2 is a kind of sample variance of the distance from a mean position throughout a hundred-step "random walk". Naturally this variance will vary from one hundred-step run to another since it is only a sample variance and not the theoretical variance for this stochastic process. However a simple plot of the rg2 values using matlab's 'plot' function would be rather chaotic and not particularly informative. I would think it preferable to make a histogram plot of rg2 values over a large number of repeated trials. To accomplish this it would be wise to enclose your code, including the necessary initializations, in an outer for-loop to do the appropriate repetition and collect the rg2 values in a vector, rather than manually repeating your code.

Roger Stafford

Subject: random walk & random number

From: nawal

Date: 6 Nov, 2013 20:32:08

Message: 3 of 6

"Roger Stafford" wrote in message <l5e0ii$d19$1@newscl01ah.mathworks.com>...
> "nawal " <nawalyarey@gmail.com> wrote in message <l5dauc$5p$1@newscl01ah.mathworks.com>...
> > i've created a random wak and plotted it but what I’m stuck on is the last bit (the rg2) because if I run the file I get a value for rg2 and if I run it again I get different value. I want to find a way to get the different values of rg2 at once instead of trying to run it every time and with that I need to plot the values. I don’t know how to do this bit and I’m stuck so if anyone knows how to do please tell me.
> > r=[0 0];
> > X=[0];Y=[0];
> > for t= 1:1:100
> > B=rand(1,1)*4;
> > if B<1
> > new_position=r+[1 0];
> > elseif B<2
> > new_position=r+[0 1];
> > elseif B<3
> > new_position=r+[-1 0];
> > else
> > new_position=r+[0 -1];
> > end
> > X=[X new_position(1)];
> > Y=[Y new_position(2)];
> > r=new_position;
> > end
> > plot(X,Y)
> > rg2=mean(X.^2)+mean(Y.^2)-mean(X)^2-mean(Y)^2;
> - - - - - - - - - -
> What you have calculated here in rg2 is a kind of sample variance of the distance from a mean position throughout a hundred-step "random walk". Naturally this variance will vary from one hundred-step run to another since it is only a sample variance and not the theoretical variance for this stochastic process. However a simple plot of the rg2 values using matlab's 'plot' function would be rather chaotic and not particularly informative. I would think it preferable to make a histogram plot of rg2 values over a large number of repeated trials. To accomplish this it would be wise to enclose your code, including the necessary initializations, in an outer for-loop to do the appropriate repetition and collect the rg2 values in a vector, rather than manually repeating your code.
>
> Roger Stafford

Subject: random walk & random number

From: nawal

Date: 6 Nov, 2013 20:39:14

Message: 4 of 6

"Roger Stafford" wrote in message <l5e0ii$d19$1@newscl01ah.mathworks.com>...
> "nawal " <nawalyarey@gmail.com> wrote in message <l5dauc$5p$1@newscl01ah.mathworks.com>...
> > i've created a random wak and plotted it but what I’m stuck on is the last bit (the rg2) because if I run the file I get a value for rg2 and if I run it again I get different value. I want to find a way to get the different values of rg2 at once instead of trying to run it every time and with that I need to plot the values. I don’t know how to do this bit and I’m stuck so if anyone knows how to do please tell me.
> > r=[0 0];
> > X=[0];Y=[0];
> > for t= 1:1:100
> > B=rand(1,1)*4;
> > if B<1
> > new_position=r+[1 0];
> > elseif B<2
> > new_position=r+[0 1];
> > elseif B<3
> > new_position=r+[-1 0];
> > else
> > new_position=r+[0 -1];
> > end
> > X=[X new_position(1)];
> > Y=[Y new_position(2)];
> > r=new_position;
> > end
> > plot(X,Y)
> > rg2=mean(X.^2)+mean(Y.^2)-mean(X)^2-mean(Y)^2;
> - - - - - - - - - -
> What you have calculated here in rg2 is a kind of sample variance of the distance from a mean position throughout a hundred-step "random walk". Naturally this variance will vary from one hundred-step run to another since it is only a sample variance and not the theoretical variance for this stochastic process. However a simple plot of the rg2 values using matlab's 'plot' function would be rather chaotic and not particularly informative. I would think it preferable to make a histogram plot of rg2 values over a large number of repeated trials. To accomplish this it would be wise to enclose your code, including the necessary initializations, in an outer for-loop to do the appropriate repetition and collect the rg2 values in a vector, rather than manually repeating your code.
>
> Roger Stafford
.............................
>
What do you mean by outer for loop? I've just started learning matlab so I'm still not familiar with it.

Subject: random walk & random number

From: Roger Stafford

Date: 6 Nov, 2013 21:11:06

Message: 5 of 6

"nawal " <nawalyarey@gmail.com> wrote in message <l5e9di$77$1@newscl01ah.mathworks.com>...

> "Roger Stafford" wrote in message <l5e0ii$d19$1@newscl01ah.mathworks.com>...
> > ..... To accomplish this it would be wise to enclose your code, including the necessary initializations, in an outer for-loop to do the appropriate repetition and collect the rg2 values in a vector, rather than manually repeating your code.

> What do you mean by outer for loop? I've just started learning matlab so I'm still not familiar with it.
- - - - - - - - - -
  By "outer loop" I mean doing something like: for k = 1:10000 in front of the code you wrote and at its end storing the rg2 you have calculated in a vector RG2 having 10000 (or whatever number of runs you select) elements with the line:

 RG2(k) = rg2;

After that of course you need an 'end' to terminate this outer loop. Also you need to delete the plot command.

  I should point out that the code for producing a vector rg2 for a large number of runs can easily be vectorized rather than using nested for-loops, and besides having fewer lines of code it might execute faster.

 N = 10000; % Choose the desired number of hundred-step runs
 n = 100; % The number of steps in each run
 r = ceil(4*rand(n,N)); % Compute random integers from 1 to 4
 px = [1;0;-1;0]; py = [0;1;0;-1]; % Use these to translate them to 1, 0, and -1
 X = cumsum([zeros(1,N);reshape(px(r),n,N)],1); % Compute the random walks
 Y = cumsum([zeros(1,N);reshape(py(r),n,N)],1);
 rg2 = mean(X.^2,1)+mean(Y.^2,1)-mean(X,1).^2-mean(Y,1).^2; % Get variances

The quantity rg2 will be an N-element row vector containing your "distance" variance for each of the N runs. You can use this to compute a histogram for it.

  Note: Of course you do not get to see the individual random walk plots this way. For that you would have to set N equal to 1 in the above and do plot(X,Y) to see a typical walk.

Roger Stafford

Subject: random walk & random number

From: nawal

Date: 13 Nov, 2013 15:40:22

Message: 6 of 6

"Roger Stafford" wrote in message <l5eb9a$s59$1@newscl01ah.mathworks.com>...
> "nawal " <nawalyarey@gmail.com> wrote in message <l5e9di$77$1@newscl01ah.mathworks.com>...
>
> > "Roger Stafford" wrote in message <l5e0ii$d19$1@newscl01ah.mathworks.com>...
> > > ..... To accomplish this it would be wise to enclose your code, including the necessary initializations, in an outer for-loop to do the appropriate repetition and collect the rg2 values in a vector, rather than manually repeating your code.
>
> > What do you mean by outer for loop? I've just started learning matlab so I'm still not familiar with it.
> - - - - - - - - - -
> By "outer loop" I mean doing something like: for k = 1:10000 in front of the code you wrote and at its end storing the rg2 you have calculated in a vector RG2 having 10000 (or whatever number of runs you select) elements with the line:
>
> RG2(k) = rg2;
>
> After that of course you need an 'end' to terminate this outer loop. Also you need to delete the plot command.
>
> I should point out that the code for producing a vector rg2 for a large number of runs can easily be vectorized rather than using nested for-loops, and besides having fewer lines of code it might execute faster.
>
> N = 10000; % Choose the desired number of hundred-step runs
> n = 100; % The number of steps in each run
> r = ceil(4*rand(n,N)); % Compute random integers from 1 to 4
> px = [1;0;-1;0]; py = [0;1;0;-1]; % Use these to translate them to 1, 0, and -1
> X = cumsum([zeros(1,N);reshape(px(r),n,N)],1); % Compute the random walks
> Y = cumsum([zeros(1,N);reshape(py(r),n,N)],1);
> rg2 = mean(X.^2,1)+mean(Y.^2,1)-mean(X,1).^2-mean(Y,1).^2; % Get variances
>
> The quantity rg2 will be an N-element row vector containing your "distance" variance for each of the N runs. You can use this to compute a histogram for it.
>
> Note: Of course you do not get to see the individual random walk plots this way. For that you would have to set N equal to 1 in the above and do plot(X,Y) to see a typical walk.
>
> Roger Stafford
> - - - - - - - - - -

Thanks for the help Roger.

If you don't mind me asking i have another problem, now that i have the random walk i'm trying to the non-reversing random walk. At each point you could got in 4 directions, how do i store the previous point so it doesn't visiting it again.
here is the code:
r=[0 0];
X=[0];Y=[0];
for
t= 1:1:100
B=rand(1,1)*4;
if B<1
new_position=r+[1 0];
elseif B<2
new_position=r+[0 1];
elseif B<3
new_position=r+[-1 0];
else
new_position=r+[0 -1];
end
X=[X new_position(1)];
Y=[Y new_position(2)];
r=new_position;
end
plot(X,Y)
rg2=mean(X.^2)+mean(Y.^2)-mean(X)^2-mean(Y)^2
for
t= 1:10;
A=(rg2);
mean(rg2);
end
  

Nawal

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us