align the random number in straight line that will restrict in the box dimension

4 visualizaciones (últimos 30 días)
N=40;
phi=0.6;
% Dimension of the box
L=15;
% Diameter of particles
d=sqrt(4*phi*L^2/(pi*N));
a=d/2;
% Perturbation
epsilon=[0.0001];
for run=1:1
for i=1:N
% place particle i randomly
% x position will be randomly from -7.5 to 7.5
X0in(2*i-1)=L*(rand(1)-1/2);
% y position will be randomly from -7.5 to 7.5
X0in(2*i)=L*(rand(1)-1/2);
end
end
I want to generate 40 different positions in the square box from -7.5 to 7.5 for both x and y direction. Because it has higher area fraction with larger number of position (40), hence it is so hard for Matlab to give me the result without overlapping. I want to generate and put them in a straight line order from -7.5 to 7.5 then move a little bit if they overlap. But I dont know how to put the random number that aligns in the straight line in this box ( dont matter if they overlap or not). Note: I will draw circle for each X-Y position with diameter d.
Any helps would be nice. Thanks
  1 comentario
Walter Roberson
Walter Roberson el 8 de Mzo. de 2013
A straight line along which axis? Are you referring to sort() ?
Question: why are you not using
X0in = L * (rand(2*N) - 1/2);

Iniciar sesión para comentar.

Respuesta aceptada

Image Analyst
Image Analyst el 8 de Mzo. de 2013
Editada: Image Analyst el 8 de Mzo. de 2013
Phong: Try this code where I compare distances of the point to all other points before I decide whether or not to accept it:
clc;
maxNumberOfParticles = 20;
phi=0.6;
% Dimension of the box
L=15;
tic;
% Diameter of particles
particleDiameter = sqrt(4*phi*L^2/(pi*maxNumberOfParticles));
particleRadius = particleDiameter / 2;
% Perturbation
epsilon=[0.0001];
numberOfRuns = 1;
p = 1; % Particle counter/index.
for run = 1 : numberOfRuns
while p <= maxNumberOfParticles % Keep going until we have all the particle.
% Place particle p randomly
% x position will be randomly from -7.5 to 7.5
trialX = L*(rand(1)-1/2);
% y position will be randomly from -7.5 to 7.5
trialY = L*(rand(1)-1/2);
if p > 1
% Calculate distances from the trial point to all other points.
distances = sqrt((trialX - x).^2 + (trialY - y).^2);
% Find the minimum distance to any other particle.
[minDistance, minIndex] = min(distances);
% Assign this one if it's not closer to any otehr particle than particleRadius.
if minDistance > particleRadius
x(p) = trialX;
y(p) = trialY;
% Print out coordinates of the particle that is "Too close"
% just for fun/informational purposes.
fprintf('Accepting trial particle #%d located at (%.3f, %.3f)\n',...
p, trialX, trialY);
p = p + 1;
else
% Print out coordinates of the particle that is "Too close"
% just for fun/informational purposes.
fprintf('Skipping trial particle #%d located at (%.3f, %.3f) because it is too close to particle %d at (%.3f, %.3f)\n',...
p, trialX, trialY, minIndex, x(minIndex), y(minIndex));
end
else
% This is the first particle, so just take it.
x(p) = trialX;
y(p) = trialY;
p = p + 1;
end
end
end
toc;
Output in command window:
Accepting trial particle #2 located at (-0.886, 6.843)
Accepting trial particle #3 located at (4.781, 3.424)
Accepting trial particle #4 located at (-4.863, -2.094)
Skipping trial particle #4 located at (-4.668, -7.482) because it is too close to particle 11 at (-4.892, -7.108)
Skipping trial particle #4 located at (-2.754, 2.994) because it is too close to particle 22 at (-3.464, 3.735)
Skipping trial particle #4 located at (1.879, 0.646) because it is too close to particle 31 at (2.195, 0.318)
Accepting trial particle #5 located at (-0.914, -3.189)
Skipping trial particle #5 located at (0.025, 3.923) because it is too close to particle 8 at (-0.074, 3.096)
Accepting trial particle #6 located at (3.936, 1.141)
etc.....
Accepting trial particle #36 located at (-5.493, 2.572)
Skipping trial particle #36 located at (1.065, -4.953) because it is too close to particle 12 at (1.224, -5.758)
Skipping trial particle #36 located at (-5.285, -0.359) because it is too close to particle 6 at (-5.652, 0.066)
Accepting trial particle #37 located at (6.122, 0.783)
Accepting trial particle #38 located at (-7.006, -6.692)
Skipping trial particle #38 located at (4.576, -0.729) because it is too close to particle 20 at (5.196, 0.090)
Accepting trial particle #39 located at (-1.760, 4.345)
Accepting trial particle #40 located at (-2.036, 0.485)
Elapsed time is 0.007953 seconds.
  2 comentarios
Phong Pham
Phong Pham el 8 de Mzo. de 2013
It still has overlapped but I fix it. However. if I want to run numberofparticle=40 then it finishes with x and y coordinate of 39. It missed one particle. Thanks
Image Analyst
Image Analyst el 8 de Mzo. de 2013
Editada: Image Analyst el 8 de Mzo. de 2013
The line should have said:
while p <= maxNumberOfParticles
I fixed it. I don't know why you say there is still overlap - I don't see how that could be true. Thanks for marking it Accepted.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre General Applications en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by