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.


' version 17-09-2015
' compile with: fbc -s console
 
#Define screen_width 1024
#Define screen_height 256
ScreenRes screen_width, screen_height, 8
Width screen_width\8, screen_height\16
 
Function f1(x As Double) As Double
    Return x^3
End Function
 
Function f2(x As Double) As Double
    Return 1/x
End Function
 
Function f3(x As Double) As Double
    Return x
End Function
 
Function leftrect(a As Double, b As Double, n As Double, _
ByVal f As Function (ByVal As Double) As Double) As Double
 
    Dim As Double sum, x = a, h = (b - a) / n
 
    For i As UInteger = 1 To n
        sum = sum + h * f(x)
        x = x + h
    Next
 
    leftrect = sum
End Function
 
Function rightrect(a As Double, b As Double, n As Double, _
ByVal f As Function (ByVal As Double) As Double) As Double
 
    Dim As Double sum, x = a, h = (b - a) / n
 
    For i As UInteger = 1 To n
        x = x + h
        sum = sum + h * f(x)
    Next
 
    rightrect = sum
End Function
 
Function midrect(a As Double, b As Double, n As Double, _
ByVal f As Function (ByVal As Double) As Double) As Double
 
    Dim As Double sum, h = (b - a) / n, x = a + h / 2
 
    For i As UInteger = 1 To n
        sum = sum + h * f(x)
        x = x + h
    Next
 
    midrect = sum
End Function
 
Function trap(a As Double, b As Double, n As Double, _
ByVal f As Function (ByVal As Double) As Double) As Double
 
    Dim As Double x = a, h = (b - a) / n
    Dim As Double sum = h * (f(a) + f(b)) / 2
 
    For i As UInteger = 1 To n -1
        x = x + h
        sum = sum + h * f(x)
    Next
 
    trap = sum
End Function
 
Function simpson(a As Double, b As Double, n As Double, _
ByVal f As Function (ByVal As Double) As Double) As Double
 
    Dim As UInteger i
    Dim As Double sum1, sum2
    Dim As Double h = (b - a) / n
 
    For i = 0 To n -1
        sum1 = sum1 + f(a + h * i + h / 2)
    Next i
 
    For i = 1 To n -1
        sum2 = sum2 + f(a + h * i)
    Next i
 
    simpson = h / 6 * (f(a) + f(b) + 4 * sum1 + 2 * sum2)
End Function
 
' ------=< main >=------
 
Dim As Double y
Dim As String frmt = " ##.##########"
 
Print
Print "function     range       steps  leftrect      midrect       " + _
                              "rightrect     trap          simpson "
 
Print "f(x) = x^3   0 - 1         100";
Print Using frmt; leftrect(0, 1, 100, @f1); midrect(0, 1, 100, @f1); _
rightrect(0, 1, 100, @f1); trap(0, 1, 100, @f1); simpson(0, 1, 100, @f1)
 
Print "f(x) = 1/x   1 - 100      1000";
Print Using frmt; leftrect(1, 100, 1000, @f2); midrect(1, 100, 1000, @f2); _
                    rightrect(1, 100, 1000, @f2); trap(1, 100, 1000, @f2); _
                    simpson(1, 100, 1000, @f2)
 
frmt = " #########.###"
Print "f(x) = x     0 - 5000  5000000";
Print Using frmt; leftrect(0, 5000, 5000000, @f3); midrect(0, 5000, 5000000, @f3); _
                    rightrect(0, 5000, 5000000, @f3); trap(0, 5000, 5000000, @f3); _
                    simpson(0, 5000, 5000000, @f3)
 
Print "f(x) = x     0 - 6000  6000000";
Print Using frmt; leftrect(0, 6000, 6000000, @f3); midrect(0, 6000, 6000000, @f3); _
                    rightrect(0, 6000, 6000000, @f3); trap(0, 6000, 6000000, @f3); _
                    simpson(0, 6000, 6000000, @f3)
 
' empty keyboard buffer
While InKey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End