pi ka value
pi ka value
Experiment - 5
5.1 - Aim:
To find the value of Pi and to integrate a given function using acceptance-
rejection method.
5.2 - Theory:
1. Estimation of pi:
We consider a circle of radius 1 enclosed within a 2*2 square. The area of
circle is 2 = π, the area of square is 4. If we divide area of circle by the
area
of square, we get π/4. We then generate a large number of points which can
be in any position within the square, i.e., between (-1,1) and (1,1). If we
divide total number of points within the circle, Ninner by the total number of
points Ntotal , we should get a value approximate to π/4.
5.3 - Pseudocode:
• Declare the lower and upper limit, and other variables and get the
number of experiments from user.
• Generate random numbers that lie inside the square and count the
number of these points that lie inside the circle.
• Find π using the equation mentioned above.
• Define a function and to find its value in the given limits use the
equation given above.
5.4 - Algorithm :
• Define a square with side length (centered at the origin) and a circle
with radius 1(also centered at the origin).
• Generate random points within the square by using random x and y
coordinates between -1 and 1.
• Count how many of these random points fall inside the circle (i.e.,
distance from the origin≤ 1).
• Calculate the ratio of points inside the circle to the total number of
points generated
• Multiply this ratio by 4 to estimate the value of π.
• Define a bounding box that encloses the function’s curve and find the
maximum value M, of the function within this box.
• Generate random points (x,y) within the bounding box.
• Calculate the function’s value at the random x-coordinate, f(x).
• Generate a random value, u, between 0 and M.
• If u≤f(x), accept the point (x,y); otherwise , reject it .
• Repeat steps 7-10 for a large number of random points.
• Calculate the ratio of accepted points to the total generated points and
multiply it by the area of the bounding box to estimate the integral of
the function.
• Display the results.
MP SURYANSH DEV SINGH 2407027
5.5 - Flowchart
Start
Estimate of pi = (points
inside the circle/total
points)*4
def print_pi(self):
print(f"Value of Pi :: {self.value}")
5.7 - Result:
5.9 - Analysis:
On the basis of above table, we can conclude that:
1. If we assign the value 100 the estimated value of pi is near the value of
pi.
2. The estimated integral value also varies because it gives us random
values in every run.
MP SURYANSH DEV SINGH 2407027
3. On increasing the sample space our estimated value goes closer to the
value of pi.
4. In the last sample, we can see that the estimated value of pi is exactly
to the original values of pi.