You are on page 1of 2

Chun Kit Peng

ME 322-4PR

Problem:
Estimate the value for Pi by generating N random points inside the area of the square and
generating M random points inside the area of the circle.

Solution:
𝑁 = 𝐴𝑟𝑒𝑎𝑆𝑞 = 4𝑟 2
M=𝐴𝑟𝑒𝑎𝑐𝑖𝑟 = 𝜋𝑟 2
𝜋 =4*M/N
𝑥2 + 𝑦2 = 1

Pseudocode:

DO
Point (1) =10000: Point (2) =300000: Point (3) =500000: Pi=zeros (1, 3): M=0: N=0
DOFOR J=1, 3
x=rand(Point(j),1)
y=rand(Point(j),1)
DOFOR i=1, Point(j)
N=N+1
IF x(i)^2+y(i)^2<=1 THEN
M=M+1
ENDIF
ENDDO
Pi(j)=4*M/N
ENDDO
DISPLAY Pi
ENDO
Results:
The accuracy of the approximation of pi depends on the amount of points we generate inside the
square and the circle. For example, if we generated ten points to estimate the value of pi, the
results wouldn’t be as accurate as if we were to generate 500000 points.

Conclusion:
The Matlab code proved to successfully estimate the value of pi. The limitations of the code lay
within the number of points generated, as we increase the amount of points we also increase the
precision of our pi estimation.

You might also like