Thanks for looking at this page. I'm new to C# and I would like to know why this piece of code increments the values. Essentially, I am looking to measure the velocity of individual particles in a system, however, the results from the text file shows that each value of velocity adds to the next. What I need is to put a count of each value for velocity into a bin, for example a count of 1 for velocities between 0 and 0.5 go into bin 1 and 0.5 to 1.0 into bin 2 and so on, so then the values of each bin can be written into a text file which I could then turn into a histogram to see how often each velocity in a certain bin occurs. My piece of code is below. I am very grateful for any help which I am given. Thank you for looking at the page.
private void computeVelocity()
{
//calc velocity of each particle for printing
double binSize = 0.5;
double velocityOfParticle = 0.0;
double averageSquareVelocity = 0.0;
int maxArrayValue = 0;
int binArraySize = 0;
int z;
double[] velocityArray = new double[Particle.allParticles.Count];
for (int counter = 0; counter < Particle.allParticles.Count; counter++)
{
averageSquareVelocity = Particle.allParticles[counter].SquareVelocity;
velocityOfParticle = Math.Sqrt(averageSquareVelocity);
velocityArray[counter] = velocityOfParticle;
}
maxArrayValue = Convert.ToInt32(Math.Round(velocityArray.Max()));
binArraySize = Convert.ToInt32(Math.Round(maxArrayValue / binSize));
//numberOfBins = Math.Round(maxArrayValue / binSize);
int [] binCountArray = new int[numberOfParticles];
int incrementTerm = 0;
Array.Clear(binCountArray, 0, binCountArray.Length);
for(double counter = 0.0; counter <= maxArrayValue; counter = counter + binSize)
{
if(counter > binSize)
{
incrementTerm = incrementTerm + 1;
}
z = 0;
for(int i = 0; i < numberOfParticles; i++)
{
if(velocityArray[i] <= counter)
{
z++;
}
}
binCountArray[incrementTerm] = z;
}
foreach (var item in binCountArray)
{
VelocityValues.WriteLine(item);
}
}
if(velocityArray[i] <= counter)
This will add everything under counter, instead of under counter and over counter-binSize. Corrected (probably):
private void computeVelocity()
{
//calc velocity of each particle for printing
double binSize = 0.5;
double velocityOfParticle = 0.0;
double averageSquareVelocity = 0.0;
int maxArrayValue = 0;
int binArraySize = 0;
int z;
double[] velocityArray = new double[Particle.allParticles.Count];
for (int counter = 0; counter < Particle.allParticles.Count; counter++)
{
averageSquareVelocity = Particle.allParticles[counter].SquareVelocity;
velocityOfParticle = Math.Sqrt(averageSquareVelocity);
velocityArray[counter] = velocityOfParticle;
}
maxArrayValue = Convert.ToInt32(Math.Round(velocityArray.Max()));
binArraySize = Convert.ToInt32(Math.Round(maxArrayValue / binSize));
//numberOfBins = Math.Round(maxArrayValue / binSize);
int [] binCountArray = new int[numberOfParticles];
int incrementTerm = 0;
Array.Clear(binCountArray, 0, binCountArray.Length);
for(double counter = 0.0; counter <= maxArrayValue; counter = counter + binSize)
{
if(counter > binSize)
{
incrementTerm = incrementTerm + 1;
}
z = 0;
for(int i = 0; i < numberOfParticles; i++)
{
if((velocityArray[i] <= counter) && (velocityArray[i] > counter - binSize))
{
z++;
}
}
binCountArray[incrementTerm] = z;
}
foreach (var item in binCountArray)
{
VelocityValues.WriteLine(item);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.