In: Statistics and Probability
Compute and plot the path(s) of a set of random walkers which are confined by
a pair of barriers at +B units and -B units from the origin (where the walkers
all start from).
A random walk is computed by repeatedly performing the calculation
x
j+1
= x
j
+ s
where s is a number drawn from the standard normal distribution (‘randn
’
in
MATLAB). For example, a walk of N steps would be handled by the code fragment
x(1) = 0;
for j = 1:N
x(j+1) = x(j) + randn(1,1);
end
There are three possible ways that the walls can "act":
a. Reflecting - In this case, when the new position is outside the walls, the
walker is "bounced" back by the amount that it exceeded the barrier.
That is,
when x
j+1
> B,
x
j+1
= B - |B - x
j+1
|
when x
j+1
< (-B),
x
j+1
= (-B) + |(-B) - x
j+1
|
If you plot the paths, you should not see any positions that are beyond |B|
units from the origin.
b. Absorbing - In this case, if a walker hits or exceeds the wall positions, it
"dies" or is absorbed and the walk ends. For this case, it is of interest
to determine the mean lifetime of a walker (i.e., the mean and distribution
of the number of steps the "average" walker will take before being absorbed).
c. Partially absorbing - This case is a combination of the previous two cases.
When a walker encounters a wall, "a coin is flipped" to see if the walker
reflects or is absorbed. Assuming a probability p (0 < p < 1) for
reflection, the pseudo-code fragment that follows uses the MATLAB uniform
random-number generator to make the reflect/absorb decision:
if rand < p
reflect
else
absorb
end
What do you do with all the walks that you generate? Compute statistics, of course.
You should answer questions like
What is the average position of the walkers as a function of time?
What is the standard deviation of the position of the walkers as a function
of time?
Does the absorbing or reflecting character influence these summaries?
For the absorbing/partial-reflection case, a plot of the number of surviving
walkers as a function of step numbers is a very interesting thing. Useful, informative and
interesting, particularly if graphically displayed.
Report Requirement
In your project report, you need to
Describe the process of getting the answers and explain your graph or table. You have
to make it understandable to students with basic knowledge of the pre-requisites and
the course materials covered so far. In general, you should type it up in a word
document and copy and paste any relevant results from MATLAB.
Append your MATLAB programs at the end of your document or submit them as
separate files.
Organize your report according to the questions asked.
Submitting only MATLAB files will result in very few points.
matlab code:
%% Program Starts here
clc;
close all;
clear all;
walkers=1000;
%walkers=input('how many walkers do you want to simulate?')
Nsteps = 1000;
B=3;
%N = input('how many steps do you want?')
%menu for how barrier acts
%case 1 reflect/ bounce back in
%case 2 end walk
%case 3 flip coin to decide bounce or end walk
prompt = 'Please Enter your Choice for how barrier acts? ';
option = input(prompt);
if option==3
prompt = 'Enter the Probability of reflection(0-1) ';
p_val = input(prompt);
end
x = zeros(walkers,Nsteps);
for w= 1:walkers
for k = 1:Nsteps %for each walker
x(w,k+1)= x(k)+ randn(1,1);
switch option
case 1
if x(w,k+1)> B
x(w,k+1)=B-abs(B-x(w,k+1));
else
x(w,k+1)=-B+abs(-B-x(w,k+1));
end
case 2
if (x(w,k+1)>= B) || (x(w,k+1)<= -B)
break;
end
case 3
p=randn(1,1);
if p<p_val
if x(w,k+1)> B
x(w,k+1)=B-abs(B-x(w,k+1));
else
x(w,k+1)=-B+abs(-B-x(w,k+1));
end
else
if x(w,k+1)>= B || x(w,k+1)<= -B
break;
end
end
end
end
hold on;
grid on;
plot(x(w,:))
end
output: