In: Advanced Math
This problem is also a Monte Carlo simulation, but this time in the continuous domain: must use the following fact: a circle inscribed in a unit square
has as radius of 0.5 and an area of ?∗(0.52)=?4.π∗(0.52)=π4.
Therefore, if you generate num_trials random points in the unit square, and count how many land inside the circle, you can calculate an approximation of ?
For this problem, you must create code in python
(A) Draw the diagram of the unit square with inscribed circle and 500 random points, and calculate the value of ?
SOLUTION:
Given That data
==>a Monte Carlo simulation, but this time in the continuous domain: must use the following fact: a circle inscribed in a unit square has as radius of 0.5.
==>an area of ?∗(0.52)=?4.π∗(0.52)=π4
Therefore, if you generate number of_trials random points in the unit square, and count how many land inside the circle, you can calculate an approximation of ? For this problem, you must create code in python.
So
A)
The R code for drawing the diagram and calculating the value of PI is given below. (500 random points are generated).
from numpy import random import math import matplotlib.pyplot as plt x =-0.5+random.random_sample(500) y = -0.5+random.random_sample(500) x1 = x[x*x+y*y<0.25] y1 = y[x*x+y*y<0.25] circle = plt.Circle((0, 0),fill=None, radius=0.5) points = [[-0.5, -0.5], [0.5, -0.5], [0.5, 0.5], [-0.5, 0.5],[-0.5,-0.5]] line = plt.Polygon(points, fill=None, edgecolor='r') plt.gca().add_patch(circle) plt.gca().add_patch(line) plt.scatter(x1,y1) plt.axis('scaled') pi = 4*len(x1)/500 plt.title("n=500, $\pi$=%0.4f" % pi) plt.show()
The diagram given below.
The value of PI is==> .