0

There is a two-dimensional random walk that one can find here which works perfectly in Octave. However, when I tried to write a one-dimensional random walk program, I got an error. Here is the program:

t=[];
x=[];
for i=1:100000
    J=rand;
    if J<0.5
        x(i+1)=x(i)+1;
        t(i+1)=t(i)+1;
    else
        x(i+1)=x(i)-1;
        t(i+1)=t(i)+1;
    end
end

plot(t,x)

Here is the error:

error: A(I): index out of bounds; value 1 out of bound 0

Thank you.

Ahaan S. Rungta
  • 2,323
  • 2
  • 12
  • 15

4 Answers4

4

No need for a loop:

N = 100000;
t = 1:N;
x = cumsum(2*(rand(1,N)<.5)-1);
plot(t,x)

enter image description here

For the 2D case you could use the same approach:

N = 100000;
%// t = 1:N; it won't be used in the plot, so not needed
x = cumsum(2*(rand(1,N)<.5)-1);
y = cumsum(2*(rand(1,N)<.5)-1);
plot(x,y)
axis square

enter image description here

Luis Mendo
  • 110,752
  • 13
  • 76
  • 147
  • 1
    cumsum is really efficient for this. I only see a tiny caveat in this answer, it starts at -1 or +1. This can of course easily be corrected by adding a 0 at the beginning if needed. – yoh.lej Jan 22 '15 at 21:59
  • @yoh.lej You are right. But then, in the OP code it's not even defined what the initial value of `x` should be – Luis Mendo Jan 22 '15 at 22:18
  • @LuisMendo yes it was only in the link. For the 2D you could also do `x = cumsum(2*(rand(2,N)<.5)-1,2)` and then plot one line as a function of the other, it saves a line, however after a few tests it does not seem faster except for rather small N, up to 100 maybe and it actually is slower for large N. – yoh.lej Jan 22 '15 at 22:39
  • @yoh.lej Yes, I also thought of that, but it didn't seem to add anything essentially new. Another alternative would be to use complex numbers: `z = cumsum(2*(rand(1,N)<.5)-1 + 1j*2*(rand(1,N)<.5)-1j); plot(z)` – Luis Mendo Jan 22 '15 at 22:45
2

You get an error because you ask MATLAB to use x(1) in the first iteration when you actually defined x to be of length 0. So you need to either initialize x and t with the proper size:

x=zeros(1,100001);
t=zeros(1,100001);

or change your loop to add the new values at the end of the vectors:

    x(i+1)=[x(i) x(i)+1];
yoh.lej
  • 1,104
  • 6
  • 14
  • Good suggestions, but your answer doesn't explain the source of the OP's error: `x(1)` was used, but undefined – horchler Jan 22 '15 at 21:21
  • Thanks for the remark, I edited the answer accordingly. – yoh.lej Jan 22 '15 at 21:28
  • @yoh.lej Thanks for the response! +1. But two questions: 1. Why did the first program work despite not doing this correctly? 2. Why does it still not work? Try it; I get the same error. – Ahaan S. Rungta Jan 22 '15 at 21:49
  • 1
    in the link the author initialized x and y with `x(1)=0;` and `y(1)=0;` – yoh.lej Jan 22 '15 at 21:53
  • what do you mean not work? do you still get an error? or is it not the result you expected. I get something similar to what Luis Mendo posted. For computational efficiency though his solutions is a lot better – yoh.lej Jan 22 '15 at 21:55
1

Since t and x are empty, therefore, you cannot index them through x(i+1) and x(i). I believe you should intialize x and t with all zeros.

Yang Zhang
  • 130
  • 6
1

In the first iteration, i = 1, you have x(2) = x(1) +or- 1 while x has dimension of zero. You should define the starting point for x and t, which is usually the origin, you can also change the code a little bit,

x = 0;
N = 100000;
t = 0 : N;
for i = 1 : N
    x(i+1) = x(i) + 2 * round(rand) - 1;
end
plot(t,x)
Rashid
  • 4,326
  • 2
  • 29
  • 54