Hi I programmed a 1d random walker and I am trying to implement a capture zone, where the program will stop if the walker remains in a specific range of values for a certain amount of time. The code I have looks like this:
steps = 1000; %sets the number of steps to 1000
rw = cumsum(-1 + 2 * round(rand(steps,1)),1); %Set up our random walk with cumsum
%Now we will set up our capture zone between 13-18 for fun
if rw >= 13 & rw <= 18
dwc = dwc + 1 %Dwelling counted ticks up every time walker is in 13-18
else dwc = 0; %Once it leaves, it returns to 0
end
while dwc >= 5
fprintf('5 steps or more within range after %d steps, so so breaking out.\n', rw);
break
end
figure(7)
comet(rw); %This will plot our random walk
grid on; %Just to see the capture zone better
hold on;
line(xlim, [13, 13], 'Color', 'r');
line(xlim, [18, 18], 'Color', 'r');
hold off;
title('1d Random Walk with Capture Zone');
xlabel('Steps');
ylabel('Position');
It will run through the walk, but it will never break in the capture zone. I am sure it has been in the capture zone for longer than 5 steps on multiple occasions but it keeps running anyway. Any help is appreciated.