Info

This question is closed. Reopen it to edit or answer.

How to store the number of iterations used until the while loop condition is met?

1 view (last 30 days)
I am coding a 2D collision problem in a 11x11 grid. In my while loop, I am trying to code that when an if condition of both particles A and B share the same coordinates, the number of steps used until this condition is met gets stored in the Moves array and it doesn't work the way I want it as 0, 1, and 2 gets stored in the Moves array and that's IMPOSSIBLE since it's a 11x11 grid and A & B can't just move 1, 2, or no steps until they meet knowing that they are allowed to move randomly in any direction one tile pers step. Here's my code so far.
I STILL NEED HELP!
for j = 1:5000
% run while loop if Collision is not found.
Collision = 0; k = 0;
while Collision == 0 && k < 1000
k = k + 1;
% Using the RandWalk_2D function for the new position of A and B.
[xAkp1, yAkp1] = RandWalk_2D(xAk,yAk,BC);
[xBkp1, yBkp1] = RandWalk_2D(xBk,yBk,BC);
% Create the path of particle A.
xAkPATH = [xAk - 0.5, xAk + 0.5, xAk + 0.5, xAk - 0.5];
yAkPATH = [yAk - 0.5, yAk - 0.5, yAk + 0.5, yAk + 0.5];
xAkp1PATH = [xAkp1 - 0.5, xAkp1 + 0.5, xAkp1 + 0.5, xAkp1 - 0.5];
yAkp1PATH = [yAkp1 - 0.5, yAkp1 - 0.5, yAkp1 + 0.5, yAkp1 + 0.5];
% Create the path of particle B.
xBkPATH = [xBk - 0.5, xBk + 0.5, xBk + 0.5, xBk - 0.5];
yBkPATH = [yBk - 0.5, yBk - 0.5, yBk + 0.5, yBk + 0.5];
xBkp1PATH = [xBkp1 - 0.5, xBkp1 + 0.5, xBkp1 + 0.5, xBkp1 - 0.5];
yBkp1PATH = [yBkp1 - 0.5, yBkp1 - 0.5, yBkp1 + 0.5, yBkp1 + 0.5];
% Update new positions of A and B.
xAk = xAkp1; yAk = yAkp1;
xBk = xBkp1; yBk = yBkp1;
% if condition when both A and B collide to restart a new trial.
if xAk == xBk && yAk == yBk
% Collision happens!
Collision = 1;
% Storing the number of moves in this trial in the 'Moves' array.
Moves(j) = k;
end
end
end
  2 Comments
Jackson Burns
Jackson Burns on 2 Aug 2019
Your code seems fine to me, but I can'tbe sure without seeing:
RandWalk_2D
I suspect the error is in there.
Mina Mansour
Mina Mansour on 2 Aug 2019
Here is the function RandWalk_2D
function [x, y] = RandWalk_2D(x0, y0, BC)
% Function (RandWalk_2D)
% This function gives the ability for the particle to move randomly in all direction on the grid.
% There are five posibilities either upwards, downwards, left, right, or stay still.
% When at the boundries, the particle cannot pass one of them, but instead it forfeits it's turn.
% Setting a random number for movement.
r = rand;
% Moving upwards.
if r <= 0.2
x = x0; y = y0 + 1;
if y >= BC(1)
y = BC(1);
end
% Moving to the right.
elseif 0.2 < r && r <= 0.4
x = x0 + 1; y = y0;
if x >= BC(2)
x = BC(2);
end
% Moving downwards.
elseif 0.4 < r && r <= 0.6
x = x0; y = y0 - 1;
if y <= BC(3)
y = BC(3);
end
% Moving to the left.
elseif 0.6 < r && r <= 0.8
x = x0 - 1; y = y0;
if x <= BC(4)
x = BC(4);
end
% Staying in position.
elseif 0.8 < r && r < 1
x = x0; y = y0;
end
end

Answers (0)

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!