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.


 
1) FUNCTIONS
 
{def left_rect {lambda {:f :x :h} {:f :x}}}
-> left_rect
 
{def mid_rect {lambda {:f :x :h} {:f {+ :x {/ :h 2}}}}}
-> mid_rect
 
{def right_rect {lambda {:f :x :h} {:f {+ :x :h}}}} 
-> right_rect 
 
{def trapezium {lambda {:f :x :h} {/ {+ {:f :x} {:f {+ :x :h}}} 2}}}
-> trapezium
 
{def simpson 
 {lambda {:f :x :h}
  {/ {+ {:f :x} {* 4 {:f {+ :x {/ :h 2}}}} {:f {+ :x :h}}} 6}}}
-> simpson
 
{def cube {lambda {:x} {* :x :x :x}}}
-> cube
 
{def reciprocal {lambda {:x} {/ 1 :x}}}
-> reciprocal
 
{def identity {lambda {:x} :x}}
-> identity
 
{def integrate 
 {lambda {:f :a :b :steps :meth}
  {let { {:f :f} {:a :a} {:steps :steps} {:meth :meth}
         {:h {/ {- :b :a} :steps}} 
       } {* :h {+ {S.map {{lambda {:meth :f :a :h :i}
                                  {:meth :f {+ :a {* :i :h}} :h}
                                 } :meth :f :a :h}
                         {S.serie 1 :steps}} }}}}}
-> integrate
 
{def methods left_rect mid_rect right_rect trapezium simpson}
-> methods
 
2) TESTS
 
We apply the following template
 
{b ∫*function* from *a* to *b* steps *steps*}
{table
 {tr {td exact value:} {td *value*}}  // the awaited value
 {S.map {lambda {:m}
          {tr {td :m}
              {td {integrate *function* *a* *b* *steps* :m}} }}
        {methods}} }
 
to the given *functions* from *a* to *b* with *steps* 
and we get:
 
∫x3 from 0 to 100 steps 100         (computed in 13ms)
exact value: 	0.25                  // 1/4
left_rect 	0.25502500000000006
mid_rect 	0.26013825000000007
right_rect 	0.26532800000000006
trapezium 	0.2601765
simpson 	0.260151
 
∫1/x from 1 to 100 steps 1000       (computed in 94ms)
exact value: 	4.605170185988092     // log(100)
left_rect 	4.55698105751468
mid_rect 	4.511421425235764
right_rect 	4.467888185754358
trapezium 	4.512434621634517
simpson 	4.511759157368674
 
∫x from 0 to 5000 steps 5000000     (computed in ... 560000m)
exact value: 	12500000              // 5000*5000/2
left_rect 	12500002.5
mid_rect 	12500005
right_rect 	12500007.5
trapezium 	12500005
simpson 	12500005
 
∫x from 0 to 6000 steps 6000      (computed in 420ms) too impatient for 6000000, sorry 
exact value: 	18000000            // 6000*6000/2
left_rect 	18003000
mid_rect 	18006000
right_rect 	18009000
trapezium 	18006000
simpson 	18006000
 