Wednesday, 4 May 2011

Approximate Integration - the logic

I previously gave a formula (Simpson's Rule) for finding the approximate integral of a function f(x) between x=a and x=b (b>a). This consisted of a sum of n bracketed terms corresponding to the approximate area under the curve of n pairs of strips, with each strip being of width h.

This sum provides the logic for calculating the area under the curve and hence a value for the definite integral as follows:-

Input A (the lower limit of the integration)
Input B (the upper limit of the integration)
Input D (the number of pairs of strips)
Store 0 in M (this will hold the sum of the area)
Store (B-A)/2D in Y (the value of h, the width of a strip)
Store 1 in C (C indicates which pair of strips is being processed)
While C is less than or equal to D
   Store 1 in B (B now indicates one of three ordinates)
   Label 0
   If B=1 then
      Store A+2(C-1)Y in X (left ordinate)
   EndIf
   If B=2 then
      Store A+2CY in X (right ordinate)
   EndIf
   If B=3 then
      Store A+(2C-1)Y in X (middle ordinate)
   EndIf
   Store f(X) in X
   Store YX/3 in X
   If B=3 then
      Store 4X in X
   EndIf
   Store M+X in M (the summation)
   Store B+1 in B
   If B is less than or equal to 3 then
      Goto Label 0
   EndIf
   Store C+1 in C
WhileEnd
Display M

No comments:

Post a Comment