4. Monte Carlo Simulation
Sometimes it is difficult to obtain an analytical solution to a problem. Monte Carlo methods rely on repeated random sampling to obtain numerical results. Randomness may be used to solve some deterministic problems.
4.1. Calculate the approximate value of \(\pi\).
Example 1 |
|
Use the Monte Carlo method to evaluate (a) the approximate value of \(\pi\). |
Solution:
(a) the approximate value of \(\pi\)
\[\begin{align*}
\frac{A_{square}} {A_{circle}} &= \frac{(2rad)^2}{\pi \times rad^2} \\
\pi &= \frac{(2rad)^2}{rad^2} \times \frac {A_{circle}} {A_{square}} \\
\pi &= \frac{(2rad)^2}{rad^2} \times \frac {N_{circle}} {N_{square}}
\end{align*}\]
You may use the equation of a circle \(r^2 = x^2 + y^2\) to know when a point is inside the circle. |
Estimated Prob = 0.79600 Real Prob = 0.78540 Estimated Area = 12.73600 Real Area = 12.56637 Estimated PI = 3.18400 Real PI = 3.14159 |
Figure 4. Monte Carlo \(\pi\) calculation
|
(b) the area of a triangle without \(\frac{1}{2} bh\)
\[\begin{align*}
\frac{Triangle_A}{Square_A} &= \frac{Triangle_{dots}}{Square_{dots}} \\
Triangle_A &= \frac{Triangle_{dots}}{Square_{dots}} \times Square_A
\end{align*}\]
You may use the equation of a straight line \(y = mx + c\) or just the gradient to know when a point is inside or outside the desired triangle. |
Triangle Area = 2.0000 |
Figure 5. Monte Carlo area calculation
|
(c) evaluating \(\int_{-3}^3 e^{-x^2}\) using Monte Carlo
\[\begin{align*}
I &= \int_a^b g(x) dx \\
I_n &\approx \frac{1}{n} \sum_{i=1}^n I_i \\
&\approx \frac{b-a}{n} \sum_{i=1}^n g(a + (b-a) U_i)
\end{align*}\]
|
Monte Integral 1.80e+00 |
Scipy Integral = 1.77e+00 |
Figure 6. Monte Carlo area calculation
|
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Comments