Why is the conditional statement not working as per the condition given?
horizon=0.3;
spacing=0.1;
length=1;
numberOfParticlesAlongX=floor(length/spacing);
numberOfParticlesInDomain=numberOfParticlesAlongX; %Number of particles in X-direction attributed to domain
xComponent(1:numberOfParticlesAlongX)=((1:numberOfParticlesAlongX)-1)*spacing; %Co-ordinate of each particle stored in xComponent
CORD=xComponent;
for i=1:numberOfParticlesInDomain
p=1;
for j=1:numberOfParticlesInDomain
if i~=j %Condition for checking undesired presence of a particle in its own neighbourhood
d=abs(CORD(i)-CORD(j)); %Distance between the two particles is stored in’d’ for comparison with the horizon value
if d<=horizon
neighbourhoodArray(i,p)=j;
p=p+1; %’p’ is incremented at every step to store only the particles within the horizon distance of a particular particle.
end
end
end
end
disp(neighbourhoodArray);
This code is part of a project on material failure modelling. I need to store all the particles (labelled as ‘j’ in the for loop) within a certain distance (lablled as ‘horizon’) from a particular particle (labelled as ‘i’ in the for loop). With the given data of horizon=0.3, spacing (between particles)=0.1, and length=1, number of particles in the domain will come out to be 10. The x-component of 1st particle is ‘0’ and that of the last one is ‘0.9’, as in the ‘xComponent’ array.
Now, with the given data, for the first particle (with CORD(1)=0), the neighbourhood particles should be those at CORD(2)=0.1 (Particle-2), CORD(3)=0.2 (Particle-3), CORD(3)=0.3 (Particle-4). But, in the neighbourhoodArray array, for the 1st particle, only Particle-2 and Particle-3 are being shown to be in the neighbourhood of Particle-1, which is incorrect.
I ran the code statement-by-statement using ‘Step’ and found that the compiler is not executing the statement under "if d<=horizon" when ‘d’ and ‘horizon’ have the same value (=0.3). I really don’t understand what the problem is with the conditional statement, as I think the logic that I have used is correct.horizon=0.3;
spacing=0.1;
length=1;
numberOfParticlesAlongX=floor(length/spacing);
numberOfParticlesInDomain=numberOfParticlesAlongX; %Number of particles in X-direction attributed to domain
xComponent(1:numberOfParticlesAlongX)=((1:numberOfParticlesAlongX)-1)*spacing; %Co-ordinate of each particle stored in xComponent
CORD=xComponent;
for i=1:numberOfParticlesInDomain
p=1;
for j=1:numberOfParticlesInDomain
if i~=j %Condition for checking undesired presence of a particle in its own neighbourhood
d=abs(CORD(i)-CORD(j)); %Distance between the two particles is stored in’d’ for comparison with the horizon value
if d<=horizon
neighbourhoodArray(i,p)=j;
p=p+1; %’p’ is incremented at every step to store only the particles within the horizon distance of a particular particle.
end
end
end
end
disp(neighbourhoodArray);
This code is part of a project on material failure modelling. I need to store all the particles (labelled as ‘j’ in the for loop) within a certain distance (lablled as ‘horizon’) from a particular particle (labelled as ‘i’ in the for loop). With the given data of horizon=0.3, spacing (between particles)=0.1, and length=1, number of particles in the domain will come out to be 10. The x-component of 1st particle is ‘0’ and that of the last one is ‘0.9’, as in the ‘xComponent’ array.
Now, with the given data, for the first particle (with CORD(1)=0), the neighbourhood particles should be those at CORD(2)=0.1 (Particle-2), CORD(3)=0.2 (Particle-3), CORD(3)=0.3 (Particle-4). But, in the neighbourhoodArray array, for the 1st particle, only Particle-2 and Particle-3 are being shown to be in the neighbourhood of Particle-1, which is incorrect.
I ran the code statement-by-statement using ‘Step’ and found that the compiler is not executing the statement under "if d<=horizon" when ‘d’ and ‘horizon’ have the same value (=0.3). I really don’t understand what the problem is with the conditional statement, as I think the logic that I have used is correct. horizon=0.3;
spacing=0.1;
length=1;
numberOfParticlesAlongX=floor(length/spacing);
numberOfParticlesInDomain=numberOfParticlesAlongX; %Number of particles in X-direction attributed to domain
xComponent(1:numberOfParticlesAlongX)=((1:numberOfParticlesAlongX)-1)*spacing; %Co-ordinate of each particle stored in xComponent
CORD=xComponent;
for i=1:numberOfParticlesInDomain
p=1;
for j=1:numberOfParticlesInDomain
if i~=j %Condition for checking undesired presence of a particle in its own neighbourhood
d=abs(CORD(i)-CORD(j)); %Distance between the two particles is stored in’d’ for comparison with the horizon value
if d<=horizon
neighbourhoodArray(i,p)=j;
p=p+1; %’p’ is incremented at every step to store only the particles within the horizon distance of a particular particle.
end
end
end
end
disp(neighbourhoodArray);
This code is part of a project on material failure modelling. I need to store all the particles (labelled as ‘j’ in the for loop) within a certain distance (lablled as ‘horizon’) from a particular particle (labelled as ‘i’ in the for loop). With the given data of horizon=0.3, spacing (between particles)=0.1, and length=1, number of particles in the domain will come out to be 10. The x-component of 1st particle is ‘0’ and that of the last one is ‘0.9’, as in the ‘xComponent’ array.
Now, with the given data, for the first particle (with CORD(1)=0), the neighbourhood particles should be those at CORD(2)=0.1 (Particle-2), CORD(3)=0.2 (Particle-3), CORD(3)=0.3 (Particle-4). But, in the neighbourhoodArray array, for the 1st particle, only Particle-2 and Particle-3 are being shown to be in the neighbourhood of Particle-1, which is incorrect.
I ran the code statement-by-statement using ‘Step’ and found that the compiler is not executing the statement under "if d<=horizon" when ‘d’ and ‘horizon’ have the same value (=0.3). I really don’t understand what the problem is with the conditional statement, as I think the logic that I have used is correct. matlab, conditional statement, array MATLAB Answers — New Questions