Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Computational Project - 01

Pradeep Kumar S
Roll .No.13101067

a ) Monte Carlo Algorithm for finding value of Pi:


Generate two uniform random deviates 'X' and 'Y' over the interval [0,10). The
FORTRAN has an inbuilt function called "RAND(SEED)" which generates random
number over the interval [0,1) which is then scaled according to the need.
Calculate the Cartesian distance 'r' to the point they correspond to, r 2 X 2 Y 2
Test if 'r' falls within the circle determined by the maximum radius of integration 'R'.
If r<= R, count this test as a "hit".
If r>R, count the test as a "miss"
After N total trials, the area of the circle is approximated by,
No. of Hits
Area of Circle
Area of Square
No. of Throws
Area of Circle

Therefore, Pi 4

N hits
4R 2
NThrows

N hits
NThrows

FORTRAN CODE :
PROGRAM findpi
Integer seed,NTHROW,HITS,MISS,Ra,i
REAL*8 X,Y,R,PI,RATIO
NTHROW = 1000000000
seed = 1
Ra = 10
HITS = 0
MISS = 0
OPEN(30,file='pi.dat')
OPEN(12,file='hits.dat')
OPEN(13,file='miss.dat')
WRITE(30,*) 'Variables = iteration,PI,pi_real'
WRITE(12,*) 'Variables = i_h,X_h,Y_h'
WRITE(13,*) 'Variables = i_m,X_m,Y_m'
CALL srand(seed)
DO i =1,NTHROW
X = 10*rand(0)
Y = 10*rand(0)
R = SQRT(X*X + Y*Y)

IF (i.LE.1000) THEN
GOTO 15
15
IF (R.LE.Ra) THEN
WRITE(12,200) i,X,Y
200 format(8G20.8)
END IF
IF (R.GT.Ra) THEN
WRITE(13,300) i,X,Y
300 format(8G20.8)
END IF
END IF
IF(R.LE.Ra) THEN
HITS =HITS + 1
END IF
IF(R.GT.Ra) THEN
MISS = MISS + 1
END IF
IF (i.EQ.10) THEN
GOTO 5
ELSEIF (i.EQ.100) THEN
GOTO 5
ELSEIF (i.EQ.1000) THEN
GOTO 5
ELSEIF (i.EQ.10000) THEN
GOTO 5
ELSEIF (i.EQ.100000) THEN
GOTO 5
ELSEIF (i.EQ.1000000) THEN
GOTO 5
ELSEIF (i.EQ.10000000) THEN
GOTO 5
ELSEIF (i.EQ.100000000) THEN
GOTO 5
ELSEIF (i.EQ.1000000000) THEN
GOTO 5
ELSE
GOTO 10
ENDIF
5 CALL output(i,HITS)
10
CONTINUE
END DO
RATIO = REAL(HITS)/REAL(NTHROW)
PI = 4.0*RATIO
PRINT*, PI,HITS
END
SUBROUTINE output(i,HITS)
INTEGER HITS ,i

100

print*, HITS
RATIO=REAL(HITS)/REAL(i)
PI=4.0*RATIO
WRITE(30,100) i,PI, 4*atan(1.d0)
format(8G20.8)
END

Results :
1) Hit and Miss Scatter Plot : This 2D scatter plot corresponds to 1000 trials. Shows how darts thrown are scattered
over a quadrant of square.

2) Sensitivity plot : After 10^9 th trial, the Pi value obtained through Monte carlo simulation converges with the Pi
value obtained from trigonometric method. The Pi (MC simulation) value is 3.1416090, while analytical
Pi(Trigonometric) is 3.1415927.

b ) Monte Carlo Algorithm for finding volume of sphere:


Generate two uniform random deviates 'X', 'Y', 'Z' over the interval [0,20). The
FORTRAN has an inbuilt function called "RAND(SEED)" which generates random
number over the interval [0,1) which is then scaled according to the need.
Calculate the Cartesian distance 'r' to the point they correspond to, r 2 X 2 Y 2 Z 2
Test if 'r' falls within the sphere determined by the maximum radius of integration 'R'.
If r<= R, count this test as a "hit".
If r>R, count the test as a "miss"
After N total trials, the volume of the sphere is approximated by,
No. of Hits
Volume of Sphere
Volume of Cube
No. of Throws
Therefore, Volume of Sphere

N hits
8R 3
NThrows

FORTRAN CODE:
PROGRAM findvolume
Integer seed,NTHROW,HITS,MISS,Ra,i
REAL*8 X,Y,Z,R,Vsphere,RATIO
NTHROW = 1000000000
seed = 1
Ra = 20
HITS = 0
MISS = 0
OPEN(30,file='Vsphere.dat')
OPEN(12,file='hits.dat')
OPEN(13,file='miss.dat')
WRITE(30,*) 'Variables = iteration,Vsphere,Vsphere_real'
WRITE(12,*) 'Variables = i_h,X_h,Y_h,Z_h'
WRITE(13,*) 'Variables = i_m,X_m,Y_m,Z_m'

CALL srand(seed)
DO i =1,NTHROW
X = 20*rand(0)
Y = 20*rand(0)
Z = 20*rand(0)
R = SQRT(X*X + Y*Y + Z*Z)
IF (i.LE.1000) THEN
GOTO 15
15
IF (R.LE.Ra) THEN
WRITE(12,200) i,X,Y,Z
200 format(8G20.8)
END IF
IF (R.GT.Ra) THEN

WRITE(13,300) i,X,Y,Z
300 format(8G20.8)
END IF
END IF
IF(R.LE.Ra) THEN
HITS =HITS + 1
END IF
IF(R.GT.Ra) THEN
MISS = MISS + 1
END IF
IF (i.EQ.10) THEN
GOTO 5
ELSEIF (i.EQ.100) THEN
GOTO 5
ELSEIF (i.EQ.1000) THEN
GOTO 5
ELSEIF (i.EQ.10000) THEN
GOTO 5
ELSEIF (i.EQ.100000) THEN
GOTO 5
ELSEIF (i.EQ.1000000) THEN
GOTO 5
ELSEIF (i.EQ.10000000) THEN
GOTO 5
ELSEIF (i.EQ.100000000) THEN
GOTO 5
ELSEIF (i.EQ.1000000000) THEN
GOTO 5
ELSE
GOTO 10
ENDIF
5 CALL output(i,HITS,Ra)
10
CONTINUE
END DO
RATIO = REAL(HITS)/REAL(NTHROW)
Vsphere = RATIO*(Ra**3)
PRINT*, Vsphere,HITS
END
SUBROUTINE output(i,HITS,Ra)
INTEGER HITS,i,Ra
print*, HITS
RATIO=REAL(HITS)/REAL(i)
Vsphere= RATIO*(Ra**3)
WRITE(30,100) i,Vsphere, 4*atan(1.d0)*REAL(4.0/3.0)*((Ra/2)**3)
100 format(8G20.8)
END

Results :
a) Hit and Miss Scatter Plot : The 3D scatter plot corresponds to 1000 trials. Depicts the number of hits and miss.

b) Sensitivity Plot : After 10^9 th trial, the volume of the sphere obtained through Monte carlo simulation converges
with the volume of sphere obtained through analytical method. The value obtained through MC simulation is
4188.8770, while the analytical value is 4188.7903.

You might also like