Why aren't my if statements working

4 views (last 30 days)
This program is a 1d random walk. The random walk works. The walker is supposed to reflect or bounce inside a range (-10 to 10). I used an if statement for this. But my if statements dont work at all. Its as if the statements didnt exist at all. The program behaves the same whether I have it or not. There has to be a mistake in my code. Please help.
iterations= 1;
Num_steps = 1000;
Random_steps_r = NaN(Num_steps,iterations);
for i = 1:iterations
Random_steps_r(:,i) = cumsum(-1 + 2 * round(rand(Num_steps,1)),1);
end
x=Random_steps_r;
deltax=diff(x);
xMax=10;
xMin=-10;
if all(x(1:999)+deltax>xMax)
x=xMax-(x+deltax-xMax);
elseif all(x(1:999)+deltax<xMin)
x=xMin-(x+deltax-xMin);
end
comet(x)

Accepted Answer

Walter Roberson
Walter Roberson on 23 Nov 2018

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!