Asked by Kelly McGuire
on 16 Jan 2019

Could someone explain how and what these for loops are doing in the 2D random walk?

clc

clear all

%Ask for number of rivers

NumberOfSimulations = input('How many rivers? \n');

%Ask for number of steps

n = input('How many steps? \n'); % number of steps, nt increasing and n(t-1) decreasing

%StartPoint

x0 = 0;

%End point after n steps

xtarg = 40;

Saved = NaN*zeros(n+1,NumberOfSimulations+2); %Initializes Array

Saved(1,:) = x0; %Fills first row with x0 value

Saved(n+2,:) = xtarg; %Fills last row with xtarg value

for q = 1:NumberOfSimulations

unifs = rand(n+1,1);

x = x0;

for i = 0:(n-1)

t = (1-(xtarg-x)/(n-i))/2;

if unifs(i+1,1) <= t

x = x-1;

else

x = x+1;

end

Saved(i+2,q) = x;

end

end

dlmwrite('Saved.txt',Saved,'delimiter','\t','precision',3)

load Saved.txt

figure(1);

hold on;

plot(Saved);

Answer by Jan
on 16 Jan 2019

Accepted Answer

The question is not really clear. You can read in the documentation, what the for command does:

doc for

But maybe you ask for the contents of the loops. Some parts are trivial, so it is not clear, which information is not clear to you yet. Therefor a specific question would be smarter.

% Run a loop from q=1 until q=NumberOfSimulations

for q = 1:NumberOfSimulations

% Get n+1 random numbers between 0 and 1

unifs = rand(n+1,1);

% Set x to the initial value:

x = x0;

% Run a loop from i=0 to i=n-1

for i = 0:(n-1)

% Define a parameter t according to the current value of x

% and the value of the iteration counter:

t = (1-(xtarg-x)/(n-i))/2;

% If the random value defined in the outer loop is smaller

% or equal than t, subtract 1 from x, otherwise add 1:

if unifs(i+1,1) <= t

x = x-1;

else

x = x+1;

end

% Store the current value of x in the output matrix "Saved":

Saved(i+2,q) = x;

end

end

Actually this code does not contain complicated functions. Maybe the debugger helps you to understand it: Set a breakpoint in the code and run the program line by line. Check the changes in the WorkspaceBrowser to see, what's going on. The purpose of functions is explained in the documentation, so use help or doc to find a description.

Kelly McGuire
on 17 Jan 2019

Jan
on 17 Jan 2019

As simplification omit unifs but create the random number dynamically:

for i = 0:(n-1)

t = (1-(xtarg-x)/(n-i))/2;

if rand <= t

x = x-1;

else

x = x+1;

end

xtarg is the endpoint of the andom walk. t is a probability depending on the distance to the endpoint and to the number of iterations. If t is e.g. 0.4, the condition rand <= 0.4 means, that with a probability of 40% x is decreased by 1, and 60% that x is increased.

Kelly McGuire
on 18 Jan 2019

Ah ok, thanks Jan!

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## Walter Roberson (view profile)

Direct link to this comment:https://nl.mathworks.com/matlabcentral/answers/439961-2d-random-walk-please-explain-what-these-for-loops-are-doing#comment_661284

Sign in to comment.