100 DEFDBL A-H, O-Z
105 DEF FNA (x AS DOUBLE) = SQR(1! + COS(x) * COS(x))
110 A = 0!: REM This is the lower endpoint of [a,b].
120 B = 4! * ATN(1!): REM This is the upper endpoint of [a,b].
130 SUM = 0!: REM This is an accumulator.
135 REM The user is asked for a number of intervals, n, even and > 0.
140 INPUT "Enter number of intervals (must be even) "; N
145 IF N <= 0 OR (2 * FIX(N / 2) - N < 0) THEN PRINT "Input error": GOTO 140
150 H = (B - A) / N: REM The sub-interval size h = (b-a)/n.
160 FOR I = 1 TO N STEP 2: REM The "FOR" loop is done n/2 times.
170 SUM = SUM + FNA(A + (I - 1) * H)
175 REM: PRINT A + (I - 1) * H, FNA(A + (I - 1) * H)
180 SUM = SUM + 4! * FNA(A + I * H)
185 REM: PRINT A + I * H, FNA(A + I * H)
190 SUM = SUM + FNA(A + (I + 1) * H)
195 REM: PRINT A + (I + 1) * H, FNA(A + (I + 1) * H)
200 NEXT I: REM After loop, the sum is y_0+4*y_1+2*y_2+...+4*y_{n-1}+y_n.
210 PRINT "Sum = "; SUM: PRINT " Value of integral = "; SUM * H / 3
220 REM This short program will integrate a function FNA(x) from x = a to b.
230 PRINT "Theoretical value approximately "; 2 * SQR(2) * 1.35064388#