Search code examples
matlabmatlab-figure

Creating graphs that show the distribution in space of a large number of 2D Random Walks at three different time points


So essentially I have this code here that I can use to generate a 2D Random Walk discretely along N number of steps with M number of walkers. I can plot them all on the same graph here.

clc;
clearvars;
N = 500; % Length of the x-axis, also known as the length of the random walks.
M = 3; % The amount of random walks.
x_t(1) = 0;
y_t(1) = 0;
for m=1:M
    for n = 1:N % Looping all values of N into x_t(n).
        A = sign(randn); % Generates either +1/-1 depending on the SIGN of RAND.
        x_t(n+1) = x_t(n) + A;
        A = sign(randn); % Generates either +1/-1 depending on the SIGN of RAND.
        y_t(n+1) = y_t(n) + A;
    end
    plot(x_t, y_t);
    hold on
end
grid on;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0.05, 1, 0.95]);
axis square;

Now, I want to be able to Create graphs that show the distribution in space of the positions of a large number (e.g. n = 1000) random walkers, at three different time points (e.g. t = 100, 200 and 300 or any three time points really).

I'm not sure how to go about this, I need to turn this into a function and iterate it through itself three different times and store the coordinates? I have a rough idea but iffy on actually implementing. I'd assume the safest and least messy way would be to use subplot() to create all three plots together in the same figure.

Appreciate any assistance!


Solution

  • You can use cumsum to linearize the process. Basically you only want to cumsum a random matrix composed of [-1 and 1].

    clc;
    close all;
    M = 50; % The amount of random walks.
    steps = [10,200,1000]; % here we analyse the step 10,200 and 1000
    cc = hsv(length(steps)); % manage the color of the plot
    %generation of each random walk
    x = sign(randn(max(steps),M));
    y = sign(randn(max(steps),M));
    xs = cumsum(x);
    xval = xs(steps,:);
    ys = cumsum(y);
    yval = ys(steps,:);
    
    hold on
    for n=1:length(steps)
        plot(xval(n,:),yval(n,:),'o','markersize',1,'color',cc(n,:),'MarkerFaceColor',cc(n,:));
    end
    
    legend('10','200','1000')
    axis square
    grid on;
    

    Results:

    enter image description here

    EDIT:

    Thanks to @LuisMendo that answered my question here, you can use a binomial distribution to get the same result:

    steps = [10,200,10000];
    cc = hsv(length(steps)); % manage the color of the plot
    M = 50;
    DV = [-1 1];
    p = .5; % probability of DV(2)
    % Using the @LuisMendo binomial solution:
    for ii = 1:length(steps)
        SDUDx(ii,:) = (DV(2)-DV(1))*binornd(steps(ii), p, M, 1)+DV(1)*steps(ii);
        SDUDy(ii,:) = (DV(2)-DV(1))*binornd(steps(ii), p, M, 1)+DV(1)*steps(ii);
    end
    hold on
    for n=1:length(steps)
        plot(SDUDx(n,:),SDUDy(n,:),'o','markersize',1,'color',cc(n,:),'MarkerFaceColor',cc(n,:));
    end
    legend('10','200','1000')
    axis square
    grid on;
    

    What is the advantage ? Even if you have a big number of steps, let's say 1000000, matlab can handle it. Because in the first solution you have a bruteforce solution, and in the second case a statistical solution.