Now, we can reverse things and use what we might call a right Riemann sum,

ignoring the first sample point at X nought, and

summing up all the rest using the backward difference of x.

As approximations, these Riemann sums are not terribly good.

On any given sub-interval, you're probably going to estimate too low or too high.

There's a better way to do things.

Instead of using rectangles, one could use a trapezoid.

This is the basis for what is called the trapezoid rule of numerical integration.

What's the area of one of these trapezoidal elements?

Well, of course, it's the same as taking the midpoint and

evaluating the area of the rectangle defined there by.

When one works out what that means in terms of the sequence,

it is the midpoint height, that is fn plus one half,

delta f at n times the width delta x at n.

Where here we're using the forward difference.

If we sum this up over all of the intervals,

let's say as n goes from 0 to N- 1, then one obtains

a much better estimate for the definite integral of f.

A few remarks are in order.

First, the trapezoid rule is really just an average of the left and

the right hand Riemann sums.

And second, if you're in the context of a uniform grid,

that is, the spacing between the h values is a constant, h,

then the trapezoid rule takes on a very nice form.

It is this width or step size, h,

times the following weighted sum of the function value's f.

The first point is weighted with coefficient one half.

The last point, f sub N, likewise.

All of the other interior sample points have weights one and

so one simply takes the sum.

This is a very simple formula both to remember and to use.

There's an even better approximation that is called Simpson's rule,

and its derivation tends to be unexplained in most calculus courses,

but we're going to take the time and do it.

Simpson's rule is a third order method.

That means we approximate the integrand f by a polynomial of degree three.

How do you approximate a function with a polynomial?

Well, you already know the answer to that.

We do a Taylor expansion.

Let's set things up so that we're expanding about x = 0 for convenience.

We keep all terms of order three and below and

pack everything else into big O of x to the fourth.

We're going to focus on the range,

as x goes from negative h to positive h, where h is our step size.

Therefore, the higher order terms can be written as big O of h to the fourth.

That's gonna be important later.

Now what do we want to do?

We want to integrate from negative h to h, and so

we can integrate each term in this Taylor expansion.

Now keeping in mind that these derivatives are all evaluated at zero and

hence constant we see that because we're integrating Over a symmetric domain.

All of the odd degree terms in X integrate out to give us 0.

We're left with a much simpler integral.

How do we do that?

Well, the first term is a constant, f evaluated at 0.

That integrates to that constant times x.

The second term has an x squared in it.

That integrates to x cubed over 3 with the constants out in front.

The error terms, big O of h to the fourth, when we're integrating from

negative h to h, still gives us something that is in big O of h to the fourth.

Now when we evaluate from negative h to h,

we can simply evaluate from 0 to h, and pull out a constant of 2 if we like.

This gives us an expression for the integral that requires knowing

the function value at 0, and the second derivative of that function at 0.

Now, the first term is gonna be easy.

If I'm given sample points, let's say at X n and X n-1 and X n+1.

Then evaluating at the middle point, that is, at Xn, is simple.

That function is simply f sub n.

But how do we estimate the second derivative of our integrand, when

all we know is the function value where we are, and to the left and the right?