Lab VII: Random Systems and Random Walk (In Two Dimensions)
Lab VII: Random Systems and Random Walk (In Two Dimensions)
function [x,y]=random_walk_single(Nsteps,plot_or_not)
x=rand_disc(Nsteps,0.5);
y=rand_disc(Nsteps,0.5);
if ( strcmp(plot_or_not,"yes") )
cumx=cumsum(x);
cumy=cumsum(y);
axis([min(cumx),max(cumx),min(cumy),max(cumy)]);
for n=1:length(cumx)
plot(cumx(1:n),cumy(1:n),’b-*’);
pause(0.1)
endfor
elseif ( strcmp(cplot,"no") )
continue
else
printf("Wrong plot option. Available options are : yes and no.\n");
endif
endfunction
What this function really does is executed on the first two lines where the random walk is generated, however, it
performs a lot of additional tasks before returning the output. Let’s examine the highlighted parts of the code :
• The argument plot or not is a string that accepts two values yes and no. String arguments cannot simply be
compared using the == but a specialized string function, strcmp must be called.
• If a plot has been asked for, we first find the cumulative sum of all the steps using cumsum. This will help us to
figure out the dimensions of the plot, which we set using the axis command.
• We then loop over n, creating a plot of the first n points at each step. This produces a plot which really looks
like the time evolution of a random walk with connected steps.
• If no plot has been asked for the code does nothing but just continues.
• If any option has been entered other than yes or no, then the code prints an error sign.