# D/Monte Carlo Simulation

Figure 1. Vegas Slot Machines (By Yamaguchi先生, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=57295504 (Links to an external site.))
Introduction
For this lab, we’re going to use the Monte Carlo method to estimate the value of pi. Wikipedia has a pretty good definition of the Monte Carlo Method (Links to an external site.):
“Monte Carlo methods, or Monte Carlo experiments, are a broad class of computational (Links to an external site.) algorithms (Links to an external site.) that rely on repeated random sampling (Links to an external site.) to obtain numerical results. The underlying concept is to use randomness (Links to an external site.) to solve problems that might be deterministic (Links to an external site.) in principle. They are often used in physical (Links to an external site.) and mathematical (Links to an external site.) problems and are most useful when it is difficult or impossible to use other approaches. Monte Carlo methods are mainly used in three problem classes:[1] (Links to an external site.) optimization (Links to an external site.), numerical integration (Links to an external site.), and generating draws from a probability distribution (Links to an external site.).
In physics-related problems, Monte Carlo methods are useful for simulating systems with many coupled (Links to an external site.) degrees of freedom (Links to an external site.), such as fluids, disordered materials, strongly coupled solids, and cellular structures (see cellular Potts model (Links to an external site.), interacting particle systems (Links to an external site.), McKean–Vlasov processes (Links to an external site.), kinetic models of gases (Links to an external site.)).
Other examples include modeling phenomena with significant uncertainty (Links to an external site.) in inputs such as the calculation of risk (Links to an external site.) in business and, in mathematics, evaluation of multidimensional definite integrals (Links to an external site.) with complicated boundary conditions (Links to an external site.). In application to systems engineering problems (space, oil exploration (Links to an external site.), aircraft design, etc.), Monte Carlo–based predictions of failure, cost overruns (Links to an external site.) and schedule overruns are routinely better than human intuition or alternative “soft” methods.[2] (Links to an external site.)
Our task will be to apply this Monte Carlo method to numerically estimate the value of pi. The value of pi can be estimated numerically by comparing the area of a circle to the square in which it is inscribed.

Figure 2. Circle Inscribed in a Square
The reason why this works can be shown with some simple arithmetic:

Procedure
As usual, create a script, include a comment at the top explaining the purpose of the script, your name, and the date it was created.
1. Use the men function to ask the user how accurate they would like their estimate. Set the choices to .1, .01, and .001. Write a switch statement to set a variable tol to .1, .01 or .001 based on the user’s choice.
2. Write a separate function to perform the Monte Carlo simulation. Starting with n = 100, generate n random ordered pairs (x,y) between 0 and 1.
While the absolute value of the difference between your estimate and the true value of pi is less than or equal to your tolerance, perform the Monte Carlo simulation and report to the user how many iterations it took to achieve the desired accuracy.
3.
Turn-in Instructions
You will need to submit .m files for each function you define. Each .m file should include your name and function description in comments. Upload your files via Canvas. Let me know if you have any questions!