Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Program to calculate pi
#6
Long time ago... 
"Esimating of pi by using the Monte Carlo Method" with graphics...
Code: (Select All)
_TITLE " Estimating of PI using the Monte Carlo Method "
'written May 08, 2018 by Bruno Schaefer, Losheim am See, Germany
'
' A simple Monte Carlo method to compute the value of Pi uses some very basic geometric relationships.
' Consider a circle of radius R inscribed in a square with a side length of 2R.
' If R=1, them the area of the circle is Pi*R^2 = Pi. Now consider the upper right quadrant.
' The area of the quarter circle is (Pi·R2)/4 = Pi/4. The area of the quadrant is R^2 = 1.
' Consider a random point (x,y) in the quadrant. Note the probability it is below the curve is simply
' the area of the region under the curve compared to the total area of the quadrant.
' This is simply Pi/4.
' To compute Pi using this method, one needs to generate N random points (x,y) in the quadrant.
' Count the number of these points that are below the curve (x^2 + y^2 < R^2), a "hit",
' here this number is called H.
' Pi/4 can be approximated as the number of hits divided by the total number of points generated by
' noting that H/G approaches Pi/4 as N goes to infinity.
DIM mcpi AS _FLOAT, x AS _FLOAT, y AS _FLOAT
N = 10000000 '! number of points on the whole area
H = 0 ' ! number of points in the quarter of a circle
RANDOMIZE TIMER
SCREEN _NEWIMAGE(630, 690, 256)
CLS , 15
COLOR 0, 15
WINDOW (-40, -30)-(590, 640)
FOR y = 40 TO 590 STEP 55
    LINE (15, y)-(565, y), 7 '  helplines parallel to x
NEXT y
FOR x = 15 TO 565 STEP 55
    LINE (x, 40)-(x, 590), 7 '   helplines parallel to y
NEXT x
FOR y = 40 TO 590 STEP 55
    LINE (12, y)-(18, y), '  ticks y-axis
NEXT y
FOR x = 15 TO 565 STEP 55 '  Ticks x-axis
    LINE (x, 37)-(x, 43) ', 7
NEXT x
LINE (15, 40)-(565, 40) 'x-axis
LINE (15, 40)-(15, 590) 'y-axis
LINE (15, 590)-(565, 590) 'upper line
LINE (565, 40)-(565, 590) 'right line
LOCATE 4, 3: PRINT " 1.0"
LOCATE 11, 3: PRINT " 0.8"
LOCATE 18, 3: PRINT " 0.6"
LOCATE 25, 3: PRINT " 0.4"
LOCATE 32, 3: PRINT " 0.2"
LOCATE 39, 3: PRINT " 0.0"
LOCATE 40, 7: PRINT "0.0"
LOCATE 40, 20: PRINT "0.2"
LOCATE 40, 34: PRINT "0.4"
LOCATE 40, 48: PRINT "0.6"
LOCATE 40, 61: PRINT "0.8"
LOCATE 40, 75: PRINT "1.0"
LOCATE 1, 30: PRINT " " + CHR$(227) + " = " + STR$(_PI(1)) + " "
FOR i = 1 TO N
    vx = RND
    vy = RND
    xWert = 15 + vx * 550
    yWert = 40 + vy * 550
    IF (hypot(vx, vy) <= 1.0) THEN
        LET H = H + 1
        PSET (xWert, yWert), 12
    ELSE
        PSET (xWert, yWert), 9
    END IF
    mcpi = 4 * H / i
    LOCATE 2, 30: PRINT " N = " + STR$(i) + " "
    LOCATE 3, 30: PRINT " " + CHR$(227) + "(est.) = " + STR$(mcpi) + "             "
NEXT i
END

FUNCTION hypot (x, y)
    hypot = SQR(x ^ 2 + y ^ 2)
END FUNCTION
Reply


Messages In This Thread
Program to calculate pi - by Kernelpanic - 02-26-2023, 10:18 PM
RE: Program to calculate pi - by david_uwi - 03-04-2023, 07:29 PM
RE: Program to calculate pi - by Kernelpanic - 03-04-2023, 10:51 PM
RE: Program to calculate pi - by bplus - 03-04-2023, 11:43 PM
RE: Program to calculate pi - by RokCoder - 03-06-2023, 12:04 PM
RE: Program to calculate pi - by BSpinoza - 03-06-2023, 05:05 PM
RE: Program to calculate pi - by bplus - 03-06-2023, 06:18 PM
RE: Program to calculate pi - by RokCoder - 03-06-2023, 07:29 PM
RE: Program to calculate pi - by JRace - 03-07-2023, 12:34 AM



Users browsing this thread: 7 Guest(s)