Professional Documents
Culture Documents
Computational Project
Computational Project
Pradeep Kumar S
Roll .No.13101067
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.
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.