How to avoid stepping back on random 2D walker

for n = 1:N .
A = randi(4);
rr=rand;
if rr < 1/4
A = 1+mod(A,4);
elseif rr < 1/2
A = 1+mod(A-2,4);
end
if A==1
y_t(n+1)=y_t(n)+1;%north
x_t(n+1)=x_t(n);
elseif A==2
y_t(n+1)=y_t(n)-1;
x_t(n+1)=x_t(n); %south
elseif A==3
x_t(n+1)=x_t(n)+1;%east
y_t(n+1)=y_t(n);
elseif A==4
x_t(n+1)=x_t(n)-1;%west
y_t(n+1)=y_t(n);
else
end
end

1 commentaire

Image Analyst
Image Analyst le 28 Déc 2018
Please type control-a then control-i in MATLAB before pasting here to make sure your indenting is corrected.

Réponses (2)

Walter Roberson
Walter Roberson le 28 Déc 2018

0 votes

you obtained the main code from me in another question . The code I provided cannot step backwards . The code keeps the current direction number 50 percent of the time and alters the direction number by 1 otherwise . A direction number change of 2 would be needed to go backwards and the code II provided cannot change by 2.

4 commentaires

elseif X==2
x_t(n+1)=x_t(n)+1;%goes east
y_t(n+1)=y_t(n);
elseif X==3
x_t(n+1)=x_t(n)-1;%goes west
y_t(n+1)=y_t(n);
No, it was important that X==3 be south not west !!
And the X=randi() should be once before the loop, just to give it an initial direction.
You should also consider assigning an initial location.
Your code appears to be broken for the case where neutron leaves the shield.
  • You are only testing x_t, not y_t as well, but shields are 2D.
  • Once it is found to be outside of [0 T] then it continues in a straight line more and more negative or more and more positive. Is that what you want?
  • It would probably make more sense to move the location test to before the assignment to rr, like
if x_t(n) >= 0 && x_t(n) <= T_x && y_t(n) >= 0 && y_t(n) <= T_y
rr=rand;
if rr < 1/4
X = 1+mod(X,4); %next higher chance direction
elseif rr < 1/2
X = 1+mod(X-2,4); %next lower chance direction
end
else
%direction stays the same
end
if X==1
y_t(n+1)=y_t(n)+1;%goes north
x_t(n+1)=x_t(n);
elseif X==2
x_t(n+1)=x_t(n)+1;%goes east
y_t(n+1)=y_t(n);
elseif X==3
x_t(n+1)=x_t(n);%goes south
y_t(n+1)=y_t(n)-1;
else
x_t(n+1)=x_t(n)-1;%goes west
y_t(n+1)=y_t(n);
end
Ege Tunç
Ege Tunç le 28 Déc 2018
changes on y direction are north and south, changes on x direction east and west. Am I wrong?
Also if neutrons leaves shield any side they must continues as a straigth line.The only problem is checking last step's direction to avoid step back
Ege Tunç
Ege Tunç le 28 Déc 2018
oh wait now i understood north,east,south,west order is important okay step back issue is done. Ty mate!
Yes, the order is important. The code maintains a current direction, X, which is maintained (50%) or turn left relative to the current direction (X-1, wrapped if necessary), or turn right (X+1, wrapped if necessary) relative to the current direction.
What I'd probably do is to compute all your directions in advance, and then remove disallowed ones. That way you don't have to check each one in the loop.
maxNumExpectedSteps = 100; % A million, or whatever - some big number, bigger than you expect to ever need.
directions = randi(4, 1, maxNumExpectedSteps)
% Remove elements where the next direction
% would have been in a disallowed direction.
% If it's going in direction 1, don't let it go in direction 3
% So replace the pattern [1, 3] with 1
directions = strrep(directions, [1, 3], 1);
% If it's going in direction 2, don't let it go in direction 4
% So replace the pattern [2, 4] with 2
directions = strrep(directions, [2, 4], 2);
% If it's going in direction 3, don't let it go in direction 1
% So replace the pattern [3, 1] with 3
directions = strrep(directions, [3, 1], 3);
% If it's going in direction 4, don't let it go in direction 2
% So replace the pattern [4, 2] with 4
directions = strrep(directions, [4, 2], 4);
% Now you're guaranteed to never go in a disallowed direction.
Now, start your loop getting the direction (1 through 4) from the "directions" array.
for k = 1 : numSteps
thisDirection = directions(k);
if thisDirection == 1
% Code to move in direction 1.
elseif thisDirection == 2
% Code to move in direction 2.
elseif thisDirection == 3
% Code to move in direction 3.
elseif thisDirection == 4
% Code to move in direction 4.
end
end

5 commentaires

Ege Tunç
Ege Tunç le 6 Jan 2019
Modifié(e) : Ege Tunç le 6 Jan 2019
the reason was our TAs are issues about similarity, They are looking for any basic excuse to break huge points at the slightest opportunity, even it's asked by myself at this community page and code is written by me.
p.s: Noted that your warning and fixed.
But the code you posted here in this Question was written by me in a previous one of your questions.
Ege Tunç
Ege Tunç le 6 Jan 2019
yes, i know but, after his warning i tried to find non modified version of the code where i used after some modification. However, i cannot find that version so i added codes which belongs to you.
So if the TA's don't let you use code by us, what can you do? I suppose renaming the variables is also not enough. So I guess if you don't want any code, you should state that in advance. I guess we could just alter your existing code written by you rather than giving improved algorithms.
Ege Tunç
Ege Tunç le 6 Jan 2019
Modifié(e) : Ege Tunç le 6 Jan 2019
well, taking some part of codes or getting some modifications are not strictly forbidden. since, my code needed some modifications and these modifications were included in your code. I have take some part of them with some modifications. however, one of the TA’s is so sensitive, I just realized this(idk why). when she sees any little similarity, she prefers to assume the whole paper might be stolen and seeks for . When I realize this, i paniced. however, codes from this topic used for modifications of some part of mine. this is my bad, sorry for that. tried to fix/readd codes that i deleted

Cette question est clôturée.

Question posée :

le 28 Déc 2018

Clôturé :

le 20 Août 2021

Community Treasure Hunt

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

Start Hunting!

Translated by