0

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.

  • I presume the code is not complete? There is no loop around the code that tests for the capture zone. It only tests it once! – Cris Luengo Dec 01 '17 at 03:14
  • Also, your `break` breaks out of the `while` loop it is in, but nothing happens in that loop. Test `dwc` using `if`. – Cris Luengo Dec 01 '17 at 03:15
  • To be honest I thought the code was complete. This is my first random walker and I've never used break before. Would you mind informing me as to what a possible solution for this code would look like? – Garrett Penna Dec 01 '17 at 03:18

1 Answers1

1

You code isn't doing what you think. There is no loop to step through to count steps & check for capture (... you don't need a loop for that anyway)

First this issue: rw is a 1000x1 array. So you if statement condition rw >= 13 & rw <= 18 will likewise return an 1000x1 logical. Which won't make a lot of since.

Second issue is you never modify the condition of the while inside the loop so it will either pass over it or get stuck in and endless loop.

while dwc >= 5
...
break
end

Edit linear version with now loops:

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
captureCheck      = rw >= 13 & rw <= 18;

%Counts the number of consecutive steps within the capture zone.
consecStepsInZone = diff([0 (find( ~(captureCheck(:).' > 0))) numel(captureCheck) + 1])- 1;  

fprintf('The max number of consecutive steps in the zone is: %d\n',max(consecStepsInZone));
Aero Engy
  • 3,588
  • 1
  • 16
  • 27
  • thanks, looking forward to seeing what needs to be improved on. It's hard to wrap my head around these things a lot of the time. – Garrett Penna Dec 01 '17 at 03:32
  • See the edit for a check of the number of steps in the zone in your random walk. – Aero Engy Dec 01 '17 at 03:35
  • That works great, thank you. But how do I get the walker to "stop" if it reaches say 10 consecutive steps in the capture zone? – Garrett Penna Dec 01 '17 at 03:39
  • @GarrettPenna There is nothing to stop so I don't know what you mean. Since this code is linearized (no loops) nothing is running that needs stopped. The second line of code already determined all 1000 steps of the walk. However, you could find in `rw` when you reach your 10 step threshold and then crop the walk `rw` to that point. Is that what you mean? – Aero Engy Dec 01 '17 at 04:25