Animation with MATLAB: Random walk in 2D space

  • Posted on: 9 August 2011
  • By: Anand
This tutorial will show you how you can make a small animation to show the random walk in 2D space. Like other programs, animation is really easy. Let us see how we can do it.
Random Walk
Random walk is a walk or series of steps where each steps taken by the object is independent of previous steps. It is also known as drunken walk where the person does not know where to go and each steps taken by him is random.
We will consider the discreet case in 2D space and the steps allowed are only four as shown in the figure 1.
Fig 1 : 2D space: Possible directions for random walk
At each time step, the object chooses a number randomly out of {1,2,3,4} and depending on that number, it will choose one of the four direction and take a step. Similar process is followed at each time step and this continues for infinite time. Random walk is the basis of many natural process like Brownian motion.
Simulation of Random Walk
Since it is an iterative process, we will need to use a loop for simulation of this. We will first simulate a single step then we will put a loop around it.

So we need to put four things.
1. assign the current position r = [x,y]

r=[0 0];

2. Get a random number B to select the direction
We will generate a number B with two binary values [b1, b2].
This two values combined will select the direction as following
\[ \begin{array}{|r|c|l|L|}
B & & Direction &  rStep \\
----- & ----- & -------------- & ---------------- \\
[1 ,1] &-> &+X direction & [1 ,0] \\ 
[1 ,0 ] &-> &+Y direction & [0 ,1]\\
[0 ,1 ] &-> &-X direction &[-1 ,0]\\
[0, 0 ] &-> &-Y direction &[0 ,-1]\\
\end{array}
\]

B1=rand(1,2);
B=B1>0.5;

B will be 0 or 1 depending on whether B1 is less than 0.5 or not.

3. calculate new position of object with the step in the selected direction.
    rnew = r + rStep;

    if B==1
        rnew=r+[1 0];
    elseif B(1)==1
            rnew=r+[0 1];
    elseif B(2)==1
        rnew=r+[-1 0];
    else
        rnew=r+[0 -1];
    end
Remember if you put  B==1 as condition inside if, it will be considered true only when all the elements in B satisfies the condition.
So rnew=r+[1 0] only when B(1) and B(2) both are 1. 
Otherwise if one of them is 0, it will check which one is 1 and decide on. In the end, if no conditions are met, it means that B is [0 0] and negative Y direction is chosen.

4. Update the position with this new value

%UPDATE the new position
r=rnew;

Now let us put together

    r=[0 0];
    B1=rand(1,2);
    B=B1>0.5;

    if B==1
        rnew=r+[1 0];
    elseif B(1)==1
            rnew=r+[0 1];
    elseif B(2)==1
        rnew=r+[-1 0];
    else
        rnew=r+[0 -1];
    end

    %UPDATE the new position

     r=rnew;

Now put a Loop around it to make it an iterative process.

r=[0 0];
for t= 0:0.1:100
    B1=rand(1,2);
    B=B1>0.5;

    if B==1
        rnew=r+[1 0];
    elseif B(1)==1
            rnew=r+[0 1];
    elseif B(2)==1
        rnew=r+[-1 0];
    else
        rnew=r+[0 -1];
    end
   
    %UPDATE the new position
     r=rnew;
end

Here the step size is 1 and time step is 0.1 so the speed of the walk is 10 unitdistance/unittime
Animation of the walk
Only thing left is now to show the walk at each time. We will plot the step by a line which will connect the previous r to the current rnew.

plot([r(1) rnew(1)],[r(2) rnew(2)]);

Since you are adding each step on the previous trace, you need to use hold on atleast once so that previous steps are not deleted.

hold on;
plot([r(1) rnew(1)],[r(2) rnew(2)]);

Now the complete code looks like t a Loop around it to make it an iterative process.

r=[0 0];
for t= 0:0.1:100
    B1=rand(1,2);
    B=B1>0.5;

    if B==1
        rnew=r+[1 0];
    elseif B(1)==1
            rnew=r+[0 1];
    elseif B(2)==1
        rnew=r+[-1 0];
    else
        rnew=r+[0 -1];
    end
   

    hold on;
    plot([r(1) rnew(1)],[r(2) rnew(2)]);

    %UPDATE the new position
     r=rnew;
end

when you run the above code, it does not give any output. The missing thing is the command 'drawnow'
Draw Now
When MATLAB sees a series of equations(commands) and a plot command in between, it calculates the graphics but it ignores the plot command at the moment to save time and when all the computation is done, then only it shows the plots. But since you want the steps to be shown at each time, we can force MATLAB to flush the plot at the moment by using drawnow command.

So the final code looks like as

r=[0 0];
for t= 0:0.1:100
    B1=rand(1,2);
    B=B1>0.5;

    if B==1
        rnew=r+[1 0];
    elseif B(1)==1
            rnew=r+[0 1];
    elseif B(2)==1
        rnew=r+[-1 0];
    else
        rnew=r+[0 -1];
    end
   

    hold on;
    plot([r(1) rnew(1)],[r(2) rnew(2)]);

    drawnow ;
    %UPDATE the new position
     r=rnew;
end

Run this code and you will get the following plot figure 2, You can increase time limit by changing the for loop time vector.
Figure 2 : Random Walk
Adding Drift
In the plot you can see that, as time passes object starts coming back to origin. It never stays at origin but it has the tendency to move around the origin and Expected translation at t= infinity is 0. This is because selection of each direction is eqaully probable since you compared with 0.5. So
\[
Pr[Selected Direction = +X ] = P[B(1)=1 \& B(2)=1] \] \[=P(b_1 =1) \times P(b_2 =1) = P(b>0.5) \times P(b >0.5)=\frac{1}{4}
\]
Similarly is the other cases.
Now let us change the probabilities and see the effect.

r=[0 0];
for t= 0:0.1:100
    B1=rand(1,2);
    B=B1>[0.4 0.3];

    if B==1
        rnew=r+[1 0];
    elseif B(1)==1
            rnew=r+[0 1];
    elseif B(2)==1
        rnew=r+[-1 0];
    else
        rnew=r+[0 -1];
    end
   

    hold on;
    plot([r(1) rnew(1)],[r(2) rnew(2)]);

    drawnow ;
    %UPDATE the new position
     r=rnew ;
end

Now we are comparing b1 with 0.4 and b2 = 0.3. So the probability becomes
\[
Pr[Selected Direction = +X ] = P[B(1)=1 \& B(2)=1]\] \[
=P(b_1 =1) * P(b_2 =1) = P(b>0.4)*P(b >0.3)=0.6*0.7=0.42
\]
Similarly with other cases.
The result is shown in figure 3. You can clearly observe a tendency to move in +XY directions. This shows the motion of the object when a drifting force exists in the medium.
Fig 3: Random walk with drift