# 2D Random Walk Please Explain What These For Loops Are Doing

11 views (last 30 days)
Kelly McGuire on 16 Jan 2019
Commented: Kelly McGuire on 18 Jan 2019
Could someone explain how and what these for loops are doing in the 2D random walk?
clc
clear all
NumberOfSimulations = input('How many rivers? \n');
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)
figure(1);
hold on;
plot(Saved);

#### 1 Comment

Walter Roberson on 17 Jan 2019
t is strange.
The loop will, I figure, increment x for at least the first n/2 + 20 steps as I figure that t will be >= 1 until then.

Jan on 16 Jan 2019
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
Thanks Jan for the response. I don't quite understand the contents of the second for loop. I'm trying to understand what the variable t is calculating and the comparison between unifs and t in the next line.
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!