Lesson 21 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 a docstring at the start that states what it does (see Lesson3 Team Notebook if you need a reminder), and several other helpful comments.

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: Plotting History

Copy your RandomBox class definition from Lesson21 Team below, and quickly double check that it works. (If you didn't finish it in the Team Notebook, finish it here and now.)


In [ ]:


In [ ]:

A. Modify your RandomBox class and methods so that it gives you a line plot of the history of the simulation with a horizontal line at equilibrium. (you've written or at least planned most of this code in Lessons 20 and 21). Make a few objects, run them through the simulation and plot the results to make sure it all works.


In [ ]:


In [ ]:

B. In Model3 of Lesson 21 you identified some repeated code in the run_simulation method. Create a helper method for the duplicate code, and call the helper method from inside both loops.


In [ ]:

Make sure to run a few test cases to make sure that your code works and makes interpretable plots that make sense.


In [ ]:

Comment on/briefly explain your results.

Part 2: Simulation Statistics

C. Next, add the code to keep track of the statistics. Review the equation for the mean square fluctuations in the team notebook and add code to keep track of the running totals. Run your simulation and make sure the totals seem reasonable. You should add in print commands that will be commented out once your code completely works.


In [ ]:

D. Finally, add code to calculate the averages, the mean square fluctuations, and the relative magnitude of the fluctuations. Since this is a lot of code, we’ll put it in another helper method (calculate_statistics). The run_simulation method should call calculate_statistics, passing it all the values that it has tracked during the simulation. The calculate_statistics method should have multiple parameters, and it should use those values to calculate the averages, mean square fluctuations, and the relative magnitude of the fluctuations. While debugging, you’ll want to print all these values. After your code is working, your method should continue to print the mean square fluctuations and relative magnitude of the fluctuations.

When you're done here, your code should:

  • print time to equilibrium, mean square fluctuations, and relative magnitude of the fluctuations
    • or print a message if equilibrium is not reached
  • make a well labeled line plot of the history of the simulation with a horizontal line at the predicted equilibrium

In [ ]:

E. Run simulations for the number of particles, $N$, where $N = 16, 64, 100, 400, 800, 3600$ (leave the time constant and make sure it is well past the time it takes to get to equilibrium for the largest $N$). Leave these results in your notebook. How do the results of your simulation (time to equilibrium, mean square fluctuations, and relative magnitude of the fluctuations) depend on the total number of particles, $N$?


In [ ]:

F. Run simulations for different times, $t$, where $t = 100, 1000, 2500, 5000, 10000$ (leave the number of particles constant). Leave these results in your notebook. How do the results of your simulation (time to equilibrium, mean square fluctuations, and relative magnitude of the fluctuations) depend on the time, $t$?


In [ ]: