Lesson 19 Individual Assignment

Individual means that you do it yourself. You won't learn to code if you don't struggle for yourself and write your own code. Remember that while you can discuss the general (algorithmic) way to solve a problem, you should not even be looking at anyone else's code or showing anyone else your code for an individual assignment.
Review the Group Work guidelines on Cavas and/or ask an instructor if you have any questions.

Programming Practice

Be sure to spell all function names correctly - misspelled functions will lose points (and often break anyway since no one is sure what to type to call it). If you prefer showing your earlier, scratch work as you figure out what you are doing, please be sure that you make a final, complete, correct last function in its own cell that you then call several times to test. In other words, separate your thought process/working versions from the final one (a comment that tells us which is the final version would be lovely).

Every function should have at least a docstring at the start that states what it does (see Lesson3 Team Notebook if you need a reminder). Make other comments as necessary.

Make sure that you are running test cases (plural) for everything and commenting on the results in markdown. Your comments should discuss how you know that the test case results are correct.

Part 1: Rejection Method

A1. Define a nonuniform function that:


In [ ]:

A2. Explain the distribution you used, provide a mathematical formula in LaTeX, and a source.

B. Test nonuniform by plotting a few histograms of the random numbers and write a brief interpretation of the test results.


In [ ]:

Part 2: Maxwell Boltzmann Distribution

At any given temperature, not all gas particles have the same velocity – some particles are moving slowly, others are moving rapidly. For a given temperature, $T$, and particle mass, $m$, the probability distribution of speeds of particles, $P(v)$, is given by:

$$P(v)=\left (\frac{m}{2\pi k_BT} \right)^{3/2} 4\pi v^2 e^{-mv^2/2k_BT}$$


same equation written slightly differently:
$$P(v) = \sqrt{\left(\frac{m}{2 \pi k_BT}\right)^3}\, 4\pi v^2 e^{- \frac{mv^2}{2k_BT}}$$

where $k_B$ is Boltzmann’s constant $(1.38066×10^{-23} J/K)$.

The two different representations of the equation are given since it is a relatively complex equation and you'll have to program it, and we want to make sure you see it as clearly as you can. We recommend that you take it in pieces. Also make sure that you are familiar with the functions that you need from the math and/or numpy library to make your equation work.

This particle distribution is used to model the molecular behavior of ideal gasses in thermodyanimcs. It can help us find the most probable speed of a kind of molecule at a specific temperature, and the area under the curve of this distribution is used to estimate the number of molecules that fall within a certain range of speeds. For example...

image reference: ChemWiki

Where $E_a$ is for activation energy for a reaction and you can see that as you increase the temperature ($T$) you shift the distribution of the molecules to higher speeds, and more molecules are above the activation energy (more area under the curve).

C. Define a MaxwellBoltzmann function that:

  • uses the rejection method to generate a list of velocity values following the Maxwell Boltzmann distribution (equation above).
  • takes 1 parameter - the number of velocities to generate
  • returns a list of velocity values following the Maxwell-Boltzmann distibution.
  • plots a histogram of the values generated

Notes

  • Assume a particle mass of $3.35×10^{-27} kg$ (mass of an H2 molecule) and a temperature of 300 K.
  • You will need to define an appropriate cut-off velocity, $v_{max}$, in order to truncate the infinite range of velocities to a finite one for a given temperature.
  • Also, be careful at first using high values for your parameter, but once you get everything programmed correctly, you should be using values above 1000 to get enough sample to "smooth out" your distribution.

In [ ]:

D. Test your MaxwellBoltzmann function by using 2500 for your parameter, examine the histogram of the velocities that your function plots and write a brief interpretation of the test results and how you know your $v_{max}$ was selected appropriately.


In [ ]:

hint: when you're done, your distribution should have a shape like this, generic (no units) version: