How can I draw a histogram from Maxwell's distribution using C#?

In summary: Random rand = new Random(); int[] bins = new int[binsCount]; double mass = 14 * 1.67e-27; double T = 300; for (int i = 0; i < time; i++) { double gauss = MaxwellBolzman.Next(rand, mass, T); int index = Normalization.Normal
  • #1
user366312
Gold Member
89
3
Homework Statement
I need to create a histogram of Maxwell-Boltzman's distribution.
Relevant Equations
https://en.wikipedia.org/wiki/Maxwell%E2%80%93Boltzmann_distribution#Distribution_for_the_speed
I have written the following source code:

Maxwell-Bolzman:
using System;

public class CommonDistributions
{
    public static double Uniform(Random random)
    {
        return random.NextDouble();
    }

    static double Gaussian(Random random)
    {
        return Math.Sqrt(-2 * Math.Log(Uniform(random))) * Math.Cos(2 * Math.PI * Uniform(random));
    }
    public static double Gaussian(Random random, double mu, double sigma)
    {
        return sigma * Gaussian(random) + mu;
    }
}

public class MaxwellBolzman
{
    static double KB = 1.38064852e-23;

    static double MaxwellVariance(double mass, double temperature)
    {
        return Math.Sqrt(KB * temperature / mass);
    }

    static double MaxwellComponent(Random random, double mass, double temperature)
    {
        double mu = 0.0;
        double sigma = MaxwellVariance(mass, temperature);

        return CommonDistributions.Gaussian(random, mu, sigma);
    }
    public static double Maxwell(Random random, double mass, double temperature)
    {
        double one = MaxwellComponent(random, mass, temperature);
        double two = MaxwellComponent(random, mass, temperature);
        double thr = MaxwellComponent(random, mass, temperature);

        return Math.Sqrt(one * one + two * two + thr * thr);
    }
}

public static class Normalization
{
    public static int Normalize(double n_bins, double mu, double sigma, double gaussian)
    {
        var z = (gaussian - mu) / sigma;

        if (z > 3 || z < -3)
        {
            return -1;
        }
        else
        {
            return (int)((z + 3) * n_bins / 6d);
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        const double N = 1000000;
        int time = (int)N;
        int binsCount = 51;

        Random rand = new Random();
        int[] bins = new int[binsCount];

        double mass = 14 * 1.67e-27;
        double T = 300;

        for (int i = 0; i < time; i++)
        {
            double gauss = MaxwellBolzman.Next(rand, mass, T);

            int index = Normalization.Normalize(binsCount, mass, T, gauss);

            if (index >= 0)
            {
                bins[index]++;
            }
        }

        PointPairList list = new PointPairList();
        for (int i = 0; i < bins.Length; i++)
        {
            list.Add(i, bins[i]);
        }

        PlotForm form = new PlotForm("Maxwell-Bolzman");
        form.AddBar(list, "Actual", Color.Blue);
        form.AxisChange();
        form.ShowDialog();
    }
}

It gives the following output:

fFjeQ.png


But, according to this link, the graph should look like the following:

k9539.png
 
Physics news on Phys.org
  • #2
I didn't go through the code/algorithm in detail, but this line would seem to be the reason that the program cuts off there, no? Where did the number come from? Did you expect the distribution to be done by then? Also, are you wanting to plot all 4 distributions from the graph you posted? Sorry if it's obvious, but I didn't really go through your code and assumptions in much detail yet.

int binsCount = 51;
 

1. How do I import the necessary libraries for drawing a histogram in C#?

In order to draw a histogram in C#, you will need to import the System.Windows.Forms.DataVisualization.Charting library. This library contains the necessary classes and methods for creating and customizing charts and graphs, including histograms.

2. What is the syntax for creating a histogram in C#?

The syntax for creating a histogram in C# using the System.Windows.Forms.DataVisualization.Charting library is as follows:

Chart chart = new Chart();
chart.Series.Add("Histogram");
chart.Series["Histogram"].ChartType = SeriesChartType.Column;
chart.Series["Histogram"].Points.DataBindXY(xValues, yValues);
chart.ChartAreas.Add("ChartArea");
chart.ChartAreas["ChartArea"].AxisX.Title = "X Values";
chart.ChartAreas["ChartArea"].AxisY.Title = "Frequency";
chart.Titles.Add("Histogram");

3. How can I generate random numbers from a Maxwell's distribution in C#?

You can use the Random class in C# to generate random numbers from a Maxwell's distribution. The following code snippet shows how to do this:

Random rand = new Random();
double a = rand.NextDouble();
double b = rand.NextDouble();
double c = Math.Sqrt(-2 * Math.Log(a)) * Math.Cos(2 * Math.PI * b);
double maxwell = Math.Sqrt(2 / Math.PI) * c;

4. Can I customize the appearance of my histogram in C#?

Yes, you can customize the appearance of your histogram in C# by using the properties and methods available in the System.Windows.Forms.DataVisualization.Charting library. For example, you can change the color, width, and style of the bars in your histogram, as well as the titles and labels of the axes and chart.

5. Is there a way to save my histogram as an image file in C#?

Yes, you can save your histogram as an image file in C# by using the SaveImage method of the Chart class. This method allows you to specify the file format (such as JPEG or PNG) and the file path where you want to save the image. For example:

chart.SaveImage("histogram.jpg", ChartImageFormat.Jpeg);

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
Replies
1
Views
749
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
1
Views
751
  • Programming and Computer Science
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
23
Views
5K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
9
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
Back
Top