# The Simpson Rule: Two numerical sweeties

Discussion in 'Scientific Statistics Math' started by Luis A. Afonso, Jul 7, 2009.

1. ### Luis A. AfonsoGuest

The Simpson Rule: Two numerical sweeties

_________Integral (from 0 to 1). [4/ (1+x^2)]
***************************************

It provides, needing only 32 intervals, the value of pi with 10 decimal places exact, namely = 3.14159Â´26536.

REM "pisimp"
CLS
DEFDBL A-Z
DEF fnw (x) = 4 / (1 + x * x)
nn = 32
pi = 4 * ATN(1)
h = 1 / nn
FOR n = 0 TO nn
IF INT(n / 2) = n / 2 THEN k = 2
IF INT(n / 2) <> n / 2 THEN k = 4
IF n = 0 OR n = nn THEN k = 1
x = n * h
s = s + h / 3 * k * fnw(x)
NEXT n
PRINT USING " #.########## "; s
PRINT USING " #.########## "; pi
END

___Normal Standard Dstribution Function
***************************************
___Phi ---> chose 6 or 10 exact decimal places.

Example :
________ Phi(.5) = 0.691463
________ Phi(-1) = 0.15865Â´52539

REM "simp"
CLS
DEFDBL A-Z
INPUT " INPUT z ---> PHI(z) "; z
PRINT " EXACT to 5 decimal places, input nn=200"
PRINT " EXACT to 10, input nn=2000 "
INPUT " nn= "; nn
pi = 4 * ATN(1): c = 1 / SQR(2 * pi)
h = (z + 20) / nn
FOR n = 0 TO nn
IF INT(n / 2) = n / 2 THEN k = 2
IF INT(n / 2) <> n / 2 THEN k = 4
IF n = 0 OR n = nn THEN k = 1
x = -20 + n * h
s = s + h / 3 * k * c * EXP(-x * x / 2)
NEXT n
IF nn = 200 THEN GOTO 200
LOCATE 10, 50
PRINT USING " #.########## "; s: GOTO 210
200 LOCATE 10, 50
PRINT USING " #.######"; s
210 END

Luis A. Afonso

Luis A. Afonso, Jul 7, 2009