Random walk problem.

4 次查看(过去 30 天)
I'm doing part 3 of this assignment. I got the thing to work. But i can't get the walker to bounce back. My for loop for that doesn't seem to work. Any advice?
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;
if Random_steps_r>=10
x=9;
elseif Random_steps_r<=-10
x=-9;
end
comet(x)
The part that doesn't work is the last forloop.
thanks

采纳的回答

Image Analyst
Image Analyst 2018-11-21
This looks like homework and many people uploading homework problems edit them away after they've been answered. We will be sure to not to do majority of work (so you won't be cheating) if you promise not to delete the question after we help you.
Hint: Basically you might do something like
if x+deltaX > xMax
x = xMax - (x + deltaX - xMax); % Reflect about xMax
end
  1 个评论
Not_sharpest_tool.mat
Thats understandable. I was looking for someone to point out an obvious mistake i've might have missed. Thanks. I will try another method regardless.

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2018-11-23
You deleted the question with your revised attempt. My Answer for it was:
Random_steps_r = NaN(Num_steps,iterations);
x=Random_steps_r;
Random_steps_r starts as NaN. Then you assign it to x, so x starts with NaN.
deltaX=diff(x);
x started with NaN, so deltaX being diff() applied to NaN, is going to be all NaN, since Nan-Nan is nan. If iterations > 1 then deltax is going to be a 2D array of NaN since diff() applied to a 2D array (that is not a vector) is 2D.
if isnan(deltaX)
deltaX=0;
end
remember that if applied to an array of values is considered true if all() of the elements are non-zero. We have demonstrated that deltaX is all nan, so even though isnan(deltaX) is a 2D array, it is true that all() of the elements are nan and so the condition is considered true. So deltaX is assigned a scalar 0. Seems a complicated way to assign 0 to deltaX.
for i = 1:iterations
Random_steps_r(:,i) = cumsum(-1 + 2 * round(rand(Num_steps,1)),1);
if x+deltaX>xMax
x=xMax-(x+deltaX-xMax);
elseif x+deltaX<xMin
x=xMin-(x+deltaX-xMin);
end
end
So Random_steps_r is changed every iteration of the loop, but deltaX is not changed, so deltaX will stay scalar 0. x is assigned to every iteration.
The values being generated for Random_steps_r are not used for anything within the loop so you could improve performance by using rand(Num_steps,iterations) to calculate all of Random_steps_r outside of any loop.
The values calculated for x do not depend upon random steps and are not used in the calculation of random steps, so performance could be improved by calculating the final result of applying the rules iterations times.
Now, looking back, we initialized x as all NaN, so it still has that value at the time you test x+deltaX>xMax . NaN plus anything is NaN and NaN greater than a value is always false. So x+deltaX>xMax is going to calculate a 2D array of all false. The implicit all() applied to arrays of values in if statements will certainly result in false. So the if will not be true, and you head on the to elseif branch. There, x (still nan) is involved in a similar calculation, and NaN lessthan a value is always false, implicit all() is false as well. So the elseif branch is false. Therefore x is not changed inside the loop, and the end result of iterations repetitions of that will be to leave x unchanged. Therefore x will end up staying the NaN that it started with.
If you were to initialze x to something non-NaN then beware the implicit all() of if statements: it would only take a false in one of the locations for the test to be considered false, and only take a false in one location for the elseif to be considered false. Therefore it is entirely plausible that even with x all non-NaN that you would quickly get to a point where x could no longer change.
It is bad style, confusing to readers, to rely upon the behavior of if applied to vectors and arrays. Even if the behavior is exactly what you want to have happen, readers would find it clearer if you use all() explicitly, like
elseif all(x(:)+deltaX > xMax)
  1 个评论
Not_sharpest_tool.mat
Sorry I deleted the question. I corrected the mistake and got the behavior I wanted. I'll make sure to follow your advise on adding all befor if statements

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by