# Trapezoidal Rule for numerical integration

1. Jul 20, 2008

### xaenn

The trapezoidal rule for numerical integration is based on the idea that when we partition our larger interval into subintervals, we can approximate the area over each subinterval by calculating the area of the trapezoid formed by connecting the value of the function at the left and right endpoints of the subinterval with a straight line.

In all of the calculus textbooks I was looking through, this is only illustrated using a strictly positive function. When you start looking at a function that is positive and negative, there is no guarantee that over a given subinterval you even get a trapezoid (see the attached image). In that image there are 5 subintervals, and the middle one certainly doesn't yield a trapezoid. If we calculate the actual area over that subinterval it is going to be two triangles, and yield something quite different from what the area of a trapezoid would have been.

I don't know a whole lot about numerical analysis, but I'm guessing this would be one additional source of error in this method? Although you are calculating the area for a trapezoid over each subinterval, in some cases the actual (approximated) area you're dealing with isn't even a trapezoid. I don't know if there's much more to say about this other than that I found it very curious, but I would love to hear the perspective of someone with a stronger numerical analysis background on this issue.

Regards,
-Xaenn

#### Attached Files:

• ###### trapezoidal_rule.pdf
File size:
11.5 KB
Views:
322
2. Jul 21, 2008

### Integral

Staff Emeritus
I don't see a problem.

Let h be the step size. The area of your trapezoid is given by:

$$A_n = h \frac {f(x_n) + f(x_{n+1})} 2$$

Now if you have either $f(x_n) =0$ or $f(x_{n+1})=0$

you get :

$$A_n = h \frac {f(x)} 2$$ where f(x) is the non zero side.

this is the area of a triangle. Looks like it works just fine to me.

Edit:

I just looked at your graph again. I now see where a potential problem lies. Not in the triangles, but having a root in the middle of a interval, the trapezoid method does not give a good area in this case, this is why smaller intervals are better. I think you will find that this sort of error is covered in the usual error analysis of this method. By this I mean that you know from the start that there is an inherent error in the method, so this is not unexpected.

Edit yet agian.

Your graph is NOT x + sin(x) perhaps just sin(x).

Last edited: Jul 21, 2008
3. Jul 21, 2008

### xaenn

You're probably right; I am sure this type of error probably is covered in the error analysis. I just found it curious because the thought that the trapezoidal rule would give you a shape other than a trapezoid never crossed my mind.
This was pure laziness on my part. The original function I was dealing with was x + sin(x), which of course didn't have this problem. I just forgot to change the label when I was playing around with it. ; ) Thanks for your response.

If anyone could suggest any good numerical analysis references I could look at for an in-detail error analysis that would be excellent.

4. Jul 22, 2008

### Integral

Staff Emeritus
Last edited by a moderator: Apr 23, 2017
5. Jul 22, 2008

### HallsofIvy

The idea of the integral as an area is only valid where the function has positive values. In order to apply any kind of numerical integration, you need to do regions where the function values are positive and regions where the function values are negative separately.

6. Jul 22, 2008

### maze

If you notice, the error is biggest where the function has the largest second derivative (in magnitude). This is no coincidence - you can show that the error, E, is given by
E = k h^2 f''(w)

where h is the step width, w is some point in the domain, and k is a constant.

7. Jul 22, 2008

### Integral

Staff Emeritus
As a addition to Halls post, you simply cannot blindly apply a numerical method. You need to keep a careful eye on what it is doing and how it is working. If you blindly apply the trapezoid rule to your example you get that the integral of sin from 0 to 2pi= zero. Which is exact. What error?

8. Jul 23, 2008

### xaenn

Or rather in an ideal world we can look at a function, break it into regions where it is positive and negative, and then consider them separately as signed areas. The only problem being that a priori you don't know where those areas are going to be. Thus, there is something of a jump between the theoretical and practical. As long as we recognize that, and that there may be resulting error because of that, I don't see it as a problem.

Would you care to elaborate on why this is no coincidence (without me actually working out all of the calculations as to why this is the maximum error)?

Of course. I wouldn't propose to just blindly apply such a method. When we're talking about error we're talking about maximum possible error anyway. It's just too easy to trick all of the first year students with an amazing Riemann sum that uses 1 interval and gets the exact answer. : )

Thanks for the book suggestion as well. I'll look into it.

Thanks again to everyone for their responses.

9. Jul 23, 2008

### maze

Ahh certainly. Let's consider all functions that have second derivative less than or equal to a certain constant. If you try to find the function that generates the absolute maximum error possible under this constraint, you will find that the error can be no greater than a series of piecewise parabolas, one parabola over each section of the approximation (In fact, it will be less except under extremely contrived circumstances since the derivatives and second derivatives must match at the joint). The maximum error for a single piece is then just the area between a parabola and a line, which you can find, and then multiply by the number of pieces to find a bound on the total error.

http://img413.imageshack.us/img413/1505/trapezoiderrorqv5.png [Broken]

Last edited by a moderator: May 3, 2017
10. Jul 23, 2008

### xaenn

Thank you very much for the explanation and image. Extremely insightful.

Last edited by a moderator: May 3, 2017
11. Jul 23, 2008

### Ben Niehoff

I don't see how a sign change is relevant? The "trapezoid" in this case consists of a positive triangle and a negative triangle. The mathematics still give the correct signed area.

If you are trying to calculate unsigned areas, then what you are doing is not strictly integration. No matter what numerical method you use, in that case you will have to find where the function crosses the x-axis and integrate each region separately.

12. Aug 8, 2008

### xaenn

Now that I've gotten a chance to pick up this book and go through it I just wanted to say it is a very nice introduction to numerical analysis. The presentation is clear and reading through the book has been extremely insightful. It's also piqued my interest to the point where I'd really like to study more numerical analysis. Thanks again for the recommendation.

I now feel like I have a good understanding of (basic) numerical integration, whereas before I really was clueless. I was looking at the picture pained by Thomas' calculus, which had little to do with what numerical integration actually is.
You are right that a sign change isn't really relevant. I thought it was because I mistakenly understood that the trapezoid rule came from finding the area of a trapezoid, rather than being named that way simply because the area coincides with that of a trapezoid in the case where the function of interest is one-signed.

Last edited by a moderator: May 3, 2017