0% found this document useful (0 votes)
2 views

pi ka value

gvfyghjn

Uploaded by

suryanshdsingh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

pi ka value

gvfyghjn

Uploaded by

suryanshdsingh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

MP SURYANSH DEV SINGH 2407027

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.

2. Integration by acceptance rejection:


Monte Carlo Integration is a process of solving integrals having numerous
values to integrate upon. The Monte Carlo process uses the theory of large
numbers and random sampling to approximate values that are very close to
the actual solution of the integral. It works on the average of a function
denoted by. Then we can expand as the summation of the values divided
by
the number of points in the integration and solve the left-hand side of the
equation to approximate the value of the integration at the right-hand side.
The derivation is as follows:
𝜋
𝑟
MP SURYANSH DEV SINGH 2407027

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

Input the values of pi

Estimate of pi = (points
inside the circle/total
points)*4

Use integrate function = integral/


num_samples

Define the range of “a” and “b”

Print the estimate value of pi and integral


value
MP SURYANSH DEV SINGH 2407027

5.6 - Python code:


from random import uniform
class Pi:
def __init__(self, n):
self.n = n
@property
def value(self):
circle_points = 0
for _ in range(self.n):
x_coor = uniform(-1, 1)
y_coor = uniform(-1, 1)
if x_coor**2 + y_coor**2 <= 1:
circle_points += 1
return ((circle_points/self.n)*4)

def print_pi(self):
print(f"Value of Pi :: {self.value}")

def function(self, x):


return x**2

def integral(self, b, a):


upper_limit = b
lower_limit = a
value_function = 0
maxyValue = self.function(b)
minyValue = self.function(a)
for _ in range(self.n):
xValue = uniform(a, b)
yValue = uniform(0, maxyValue)
if yValue <= self.function(xValue):
value_function += 1
#print(value_function)
#print(upper_limit - lower_limit)
#print(value_function/self.n)
#print(maxyValue, minyValue)
print(((upper_limit -
lower_limit)*value_function*maxyValue)/self.n)
square_points = int(input("enter your number:"))
to = Pi(square_points)
to.function(0)
to.integral(1, 0)
to.print_pi()
MP SURYANSH DEV SINGH 2407027

5.7 - Result:

5.8 - Observation table:

NUM SAMPLES ESTIMATED VALUE OF PI ESTIMATED INTEGRAL VALUE

100 3.12 0.32

1000 3.152 0.321

10000 3.1384 0.3338

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.

You might also like