h=(end-beg)/N

ai=h*(fun(beg)+fun(end))/2

for I=1, N-1

   x=beg+i x h

    ai=ai+h*fun(x)

enddo

ai=ai ´ h                              

 

End point trap rule

                The use of the end points complicates the evaluation in two ways.  First the end values need to be divided by 2, secondly they need to be calculated and this may involve analytically expanding functions such as (1-cos(x))/x2. 

There are advantages:

 

1.       The points evaluated are the ones actually used.

2.       Placing N points in the center of each region allows Nà2N with N new function evaluations.

3.       Evaluations with two different values of N allow the error term to be estimated and removed. Richardson.doc

                The code accurate to order h2 for end point trap rule is in the box to the left[1]. 

Start with Midpoint Traprule.doc Equation (10)

The values of f(x­i) need to be found.

Start with the expressions at ±(hi/2  

 Add these two to find that

 

Or equivalently

      

Note that there are only even derivatives in this expansion.  Substitute this into to find

Use to Integrate f’’

Or

               

To this accuracy the sum of fiv is the difference of the values of f’’’, so that   becomes

So that

 

 

The coefficients of the h2 and h4 terms are larger than those in midpoint integration, but the powers of h are the same.

Testing

                Let h=1

  1.  
  2.  Checks the - 1/12 in the h2 term

  3. Checks the +1/720 in the h4 term


This means that a Lagrange polynomial through 6 points will integrate exactly.

Figure 1 7 points with intervals h, 5 internal points, and 6 half interval points.

 

 

 

 



[1] A very common error in end point trap rule is an incorrect evaluation of the point at either x0 or xN.  When this happens the integral has an error equal to (f(x0)-factual­(x0))*h.  This is determined by plotting ai as a function of h, rather than h2.  A straight line for three values does not mean that this is a reasonable extrapolation.  It means that there is an error in a single function value.