Simpson’s Rule

See all articles from T-S Horizons n4
Author(s): Ken Lewis
Publication: T-S Horizons
Pub Details: T-S Horizons n4 (Issue 4)
Date: March 1984
Pages: 10-11

Frequently, it is desirable to determine the integral of a continuous function f(x) over some interval a≤x≤b. A simple and accurate method of doing this is to use Simpson’s Rule, named after the Englishman Thomas Simpson (1710-1761). The procedure assumes a partitioning which divides interval [a,b] into n subintervals of equal length h=(b-a)/n, where n must be an even number. Effectively, three consec tive points, for example (Xo, f(Xo)), (X₁, f(X₁)), (X₂, f(X₂)) are fit to a parabola whose equation in this case can be shown to be:

Equation of the parabola p(x)
Equation of the parabola p(x)

Since p(x) is only a second degree polynomial, we can find ∫ₓ₀ˣ² P(x)dx.

This will evaluate to ∫ₓ₀ˣ² P(x)dx = h/3 [f(x₀)+4f(x₁)+f(x₂)]. This later expression approximates the area under the graph of f on the interval [x₀,x₂]. The same is done for the subintervals [x₂, x₄], [x₄, x₆]…..[xₙ₋₂, xₙ]. Each of these indices must be even, which makes it necessary for n to be even. The sum of the areas under the parabolas thus obtained serves as an approximation to

Simpson's Rule formula
Simpson's Rule formula

This is the famous Simpson’s Rule.

THE PROGRAM

The program must be changed (statement 310, right hand side) to incorporate the function you want to integrate. This should be altered so that the right hand side of the equation is the integrand of your problem. Once this is done, then put, the program in the “RUN” mode. Only two additional pieces of information are required at this point, i.e., the limits of integration. Input first the lower limit, then the upper limit of integration. The program will lease iterating when two successive computations of the integral do not differ by more than .00001 (This can be changed to whatever tolerance you desire (line 230)).

 10 REM SIMPSON
20 INPUT A
30 INPUT B
40 LET SUMP=0
50 LET N=6
60 LET DELTAX=(B-A)/N
70 LET SUME=0
80 LET SUMO=0
90 FOR K=2 TO N STEP 2
100 LET X=A+DELTAX*(K-1)
110 GOSUB 300
120 LET SUMO=SUMO+4*F*DELTAX/3
130 NEXT K
140 FOR K=3 TO N STEP 2
150 LET X=A+DELTAX*(K-1)
160 GOSUB 300
170 LET SUME=SUME+2*F*DELTAX/3
180 NEXT K
190 LET X=A
200 GOSUB 300
205 LET F0=F*DELTAX/3
210 LET X=B
215 GOSUB 300
220 LET FN=F*DELTAX/3
225 LET SUMN=F0+SUME+SUMO+FN
230 IF (SUMN-SUMP)<=.0001 THEN GOTO 350
235 LET SUMP=SUMN
238 LET N=2*N
240 GOTO 60
250 REM **********************
260 REM ****CHANGE FUNCTION****
270 REM ****WITH EACH NEW******
280 REM ****PROBLEM**********
290 REM **IN LINE 310********
300 REM SUBROUTINE
310 LET F=X
320 RETURN
350 PRINT "THE INTEGRAL BETWEEN"
360 PRINT "THE LIMITS",A,"AND",B
370 PRINT "EVALUATES TO BE",SUMN
380 STOP

EXAMPLE:

Suppose we desire to determine the integral ∫₃.₅⁵·⁶ xdx. The first step is to make sure that the integrand (x) is placed on the right hand side of the equation in statement 310 of the program. Then the “RUN” mode is entered. A prompt for the lower limit of the integral is given “INPUT LOWER LIMIT”, and the value 3.5 is entered. Similarly, the upper limit is prompted, and the value 5.6 is input. The answer is printed out, as shown, and is 44.247.

Note: Type-in program listings on this website use ZMAKEBAS notation for graphics characters.