Write functions to calculate the definite integral of a function  ƒ(x)  using all five of the following methods:

 rectangular
 left
 right
 midpoint
 trapezium
 Simpson's
 composite
Your functions should take in the upper and lower bounds (a and b), and the number of approximations to make in that range (n).

Assume that your example already has a function that gives values for  ƒ(x) .

Simpson's method is defined by the following pseudo-code:


 Pseudocode: Simpson's method, composite



procedure quad_simpson_composite(f, a, b, n)
    h := (b - a) / n
    sum1 := f(a + h/2)
    sum2 := 0

    loop on i from 1 to (n - 1)
        sum1 := sum1 + f(a + h * i + h/2)
        sum2 := sum2 + f(a + h * i)
                                            
    answer := (h / 6) * (f(a) + f(b) + 4*sum1 + 2*sum2)




Demonstrate your function by showing the results for:

   ƒ(x) = x3,       where   x   is     [0,1],       with           100 approximations.   The exact result is     0.25               (or 1/4)
   ƒ(x) = 1/x,     where   x   is   [1,100],     with        1,000 approximations.   The exact result is     4.605170+     (natural log of 100)
   ƒ(x) = x,         where   x   is   [0,5000],   with 5,000,000 approximations.   The exact result is   12,500,000
   ƒ(x) = x,         where   x   is   [0,6000],   with 6,000,000 approximations.   The exact result is   18,000,000


See also

   Active object for integrating a function of real time.
   Special:PrefixIndex/Numerical integration for other integration methods.


      *FLOAT64
      @% = 12 : REM Column width
 
      PRINT "Function     Range          L-Rect      R-Rect      M-Rect      Trapeze     Simpson"
      FOR func% = 1 TO 4
        READ x$, l, h, s%
        PRINT x$, ; l " - " ; h, FNlrect(x$, l, h, s%) FNrrect(x$, l, h, s%) ;
        PRINT FNmrect(x$, l, h, s%) FNtrapeze(x$, l, h, s%) FNsimpson(x$, l, h, s%)
      NEXT
      END
 
      DATA "x^3", 0,    1,     100
      DATA "1/x", 1,  100,    1000
      DATA "x",   0, 5000, 5000000
      DATA "x",   0, 6000, 6000000
 
      DEF FNlrect(x$, a, b, n%)
      LOCAL i%, d, s, x
      d = (b - a) / n%
      x = a
      FOR i% = 1 TO n%
        s += d * EVAL(x$)
        x += d
      NEXT
      = s
 
      DEF FNrrect(x$, a, b, n%)
      LOCAL i%, d, s, x
      d = (b - a) / n%
      x = a
      FOR i% = 1 TO n%
        x += d
        s += d * EVAL(x$)
      NEXT
      = s
 
      DEF FNmrect(x$, a, b, n%)
      LOCAL i%, d, s, x
      d = (b - a) / n%
      x = a
      FOR i% = 1 TO n%
        x += d/2
        s += d * EVAL(x$)
        x += d/2
      NEXT
      = s
 
      DEF FNtrapeze(x$, a, b, n%)
      LOCAL i%, d, f, s, x
      d = (b - a) / n%
      x = b : f = EVAL(x$)
      x = a : s = d * (f + EVAL(x$)) / 2
      FOR i% = 1 TO n%-1
        x += d
        s += d * EVAL(x$)
      NEXT
      = s
 
      DEF FNsimpson(x$, a, b, n%)
      LOCAL i%, d, f, s1, s2, x
      d = (b - a) / n%
      x = b : f = EVAL(x$)
      x = a + d/2 : s1 = EVAL(x$)
      FOR i% = 1 TO n%-1
        x += d/2
        s2 += EVAL(x$)
        x += d/2
        s1 += EVAL(x$)
      NEXT
      x = a
      = (d / 6) * (f + EVAL(x$) + 4 * s1 + 2 * s2)