Search code examples
matlabparticlesdirection

invert the direction of a particles in matlab


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.


Solution

  • 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!