i have a problem with an algorithm, i have a space with x,y coordinates and a low and upper bound. I have also some particles within this space free to move. I want that any particle remains into my space and if one of those are moving out the bounders i need to change its direction. If there is no way to bring back the particle i can create a particle in a randomic point within my space. I was thinking with a for loop that check each particles and when one moves out to bring it back to the opposit direction, but i don' t know how to write it.
Asked
Active
Viewed 97 times
-4
-
1you should add some example input and desired output – m.s. May 05 '15 at 14:22
-
can you give a small sample of your data and the expected result you want from this data? – GameOfThrows May 05 '15 at 14:22
-
i have in input the position and the velocity vector of a particle, if this vector goes out the upper bound or the lower bound i need to reverse the direction of the vector, so in this way i have my particle within the space. – Andy May 05 '15 at 14:47
-
one example i tried is: for i = 0 : number_particles ; if ( velocity(i, 7:11) > upperBound || lowerBound % i have 5 dimensions so i need 5 columns for the velocity, after that i would write somthing for the condition to turn back the vector of velocity – Andy May 05 '15 at 14:51
-
In order to give more information, please edit the question, do not use comments for that. – A. Donda May 06 '15 at 01:58
1 Answers
3
Without your information, I am going to guess how I'd do it:
part_location=rand(10,2); % 10 particles
part_direction=rand(10,2); % non-normalized direction so it has also speed
boundaries=[0,0;1 1]; % square boundary from 0 to 1; not going to use it so I dont write your whole code.
for ii=1:nsteps_simulation
% update particle position using direction
% do it
part_location= ... ?
% check if particles are inside the boundary
inside=sum(part_location>0 && part_location<1,2)==2;
outside=~inside;
% now you know which particles are inside and wich outside.
% Inverting the direction should be easy
part_direction=...?
end
Apologise if the code is not complete, but nobody is going to write it for you! However, I hope that I have given you a clear structure of how you should design an algorithm for this. Of course, depending on your data/application you'd need to modify the structure a bit, but this is probably the most you'll get without more information or show us what you tried!

Ander Biguri
- 35,140
- 11
- 74
- 120
-
Thank you, of course i will wtite by myself but you explained me my problems! – Andy May 05 '15 at 15:08
-
1@Andy If you found it useful and you think this is the answer you were looking for, accept the asnwer for the sake of the community! – Ander Biguri May 05 '15 at 15:14
-
-
It makes sure that the point is inside the boundary. If only one is inside (`sum(p,2)==1`) then the other coordinate is out. – Ander Biguri May 06 '15 at 08:41
-
-
I think I would not only revert the direction, but mirror the particle outside of the box into the box. That would be the cleanest implementation of a reflection in discrete time. – A. Donda May 08 '15 at 18:15