Assignment 9
Due date: Thursday, March 30th at midnight (Thursday night).
Let's perform a Monte Carlo analysis to study the Buffon needle problem. The problem is thus: suppose your floor is made of floor boards, and each board has a width \(t\). If you have a needle, of length \(\ell\), and you randomly drop it onto the floor, what is the probability that the needle will land on one of the edges of the boards?
As it turns out, this problem can be solved exactly, as you can see by following the above link. We will perform a Monte Carlo anlysis of this problem to see if these predictions are correct. We will do this by assuming that, for a single throw of the needle, the centre of the needle, \(x\), can be located anywhere between the one edge of the boards and the centre: \(0 \le x \le \frac{t}{2}\) (the symmetry of the problem means we only need to look at half the width). Similarly, the angle of the needle relative to the edge of the board, \(\theta\), can range from \(0 \le \theta \le \frac{\pi}{2}\), where \(\theta = 0\) is parallel to the edge of the board.
0) You must use version control ("git"), as you develop your code. We suggest you start, from the Linux command line, by creating a new directory, e.g. assignment9, cd into that directory and initialize a git repository ("git init"
) within it, and perform "git add ..., git commit"
repeatedly as you add to your code. You will hand in the output of "git log"
for your assignment repository as part of the assignment. You must have a significant number of commits representing the modifications, alterations and changes to your code. If your log does not show a significant number of commits with meaningful comments you will lose marks.
1) Create a file named Buffon_Utilities.py
containing the following functions.
1a) Write a function which takes two arguments, \(a\) and \(b\), randomly samples from the uniform distribution between \(a\) and \(b\), and returns the sampled value.
1b) Write a function which, given \(x\) and \(\theta\) and \(\ell\) (the length of the needle), returns a boolean indicating whether or not the needle is touching the edge of the board.
1c) Write a function which, given \(\ell\), \(t\), and \(n\), simulates the tossing of a needle \(n\) times. It should return the number of times, out of \(n\), that the needle touches the edge of the board.
1d) Write a function which takes \(\ell\) and \(t\) as arguments. Using the formulae given in the link above, the function should return the theoretical probability of a single needle toss touching a board edge.
1e) Write a function which takes a probability, \(\ell\) and \(t\) as arguments. The function should return an estimate for the value of \(\pi\). This will require rearranging the equation used in part 1d) above. Be sure to not copy-and-paste code!
One application of this problem is to estimate the value of \(\pi\).
2) Create a Python script called Buffon_Analysis.py
that will perform the following steps:
- import your utilities file
Buffon_Utilities.py
, - takes 2 arguments from the command line, \(l\) and \(t\).
- simulates the tossing of a needle 1000 times, using the values of \(l\) and \(t\) supplied, and prints out some sentences describing the predicted probability for the given values, the simulated probability, and the value of \(\pi\) based on the simulated probability.
Your script should output something like this, when run from the shell terminal:
$
$ python Buffon_Analysis.py 1.5 1
The predicted probability is 0.7786080607366548
The simulated probability is 0.783
The calculated value of pi is 3.123971090212096
$
Be sure to comment and document your functions. Defensive programming of the functions is not required, but defensive programming of the script is expected.
Submit your Buffon_Utilities.py
and Buffon_Analysis.py
files, and the output of git log
from your assignment repository.
Both Python code files must be added and committed frequently to the repository. To capture the output of git log
use redirection (git log > git.log
, and hand in the git.log
file).
Assignments will be graded on a 10 point basis. Due date is March 30th 2023 (midnight), with 0.5 penalty point per day off for late submission until the cut-off date of April 6th 2023, at 11:00am.