Insights Blog
-- Browse All Articles --
Physics Articles
Physics Tutorials
Physics Guides
Physics FAQ
Math Articles
Math Tutorials
Math Guides
Math FAQ
Education Articles
Education Guides
Bio/Chem Articles
Technology Guides
Computer Science Tutorials
Forums
Intro Physics Homework Help
Advanced Physics Homework Help
Precalculus Homework Help
Calculus Homework Help
Bio/Chem Homework Help
Engineering Homework Help
Trending
Featured Threads
Log in
Register
What's new
Search
Search
Search titles only
By:
Intro Physics Homework Help
Advanced Physics Homework Help
Precalculus Homework Help
Calculus Homework Help
Bio/Chem Homework Help
Engineering Homework Help
Menu
Log in
Register
Navigation
More options
Contact us
Close Menu
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Forums
Homework Help
Engineering and Comp Sci Homework Help
How can I draw a histogram from Maxwell's distribution using C#?
Reply to thread
Message
[QUOTE="user366312, post: 6327028, member: 660326"] [B]Homework Statement:[/B] I need to create a histogram of Maxwell-Boltzman's distribution. [B]Relevant Equations:[/B] [URL]https://en.wikipedia.org/wiki/Maxwell%E2%80%93Boltzmann_distribution#Distribution_for_the_speed[/URL] I have written the following source code: [CODE lang="csharp" title="Maxwell-Bolzman" highlight="79, 81"]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(); } }[/CODE] It gives the following output: [ATTACH type="full" alt="fFjeQ.png"]260719[/ATTACH] But, according to [URL='https://en.wikipedia.org/wiki/Maxwell%E2%80%93Boltzmann_distribution#Distribution_function']this link[/URL], the graph should look like the following: [ATTACH type="full" alt="k9539.png"]260720[/ATTACH] [/QUOTE]
Insert quotes…
Post reply
Forums
Homework Help
Engineering and Comp Sci Homework Help
How can I draw a histogram from Maxwell's distribution using C#?
Back
Top