Trapezoidal Rule for numerical integration

In summary: 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. ... 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 byE = k h^2 f''(w)where h is the step width, w is some point in the domain, and k is a constant. ... 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.
  • #1
xaenn
16
0
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
 

Attachments

  • trapezoidal_rule.pdf
    11.5 KB · Views: 624
Physics news on Phys.org
  • #2
I don't see a problem.

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

[tex] A_n = h \frac {f(x_n) + f(x_{n+1})} 2 [/tex]

Now if you have either [itex] f(x_n) =0 [/itex] or [itex] f(x_{n+1})=0 [/itex]

you get :

[tex] A_n = h \frac {f(x)} 2 [/tex] 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:
  • #3
Integral said:
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.
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.
Your graph is NOT x + sin(x) perhaps just sin(x).
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.
 
  • #5
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
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
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
HallsofIvy said:
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.
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.

maze said:
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)
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)?

Integral said:
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?
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
xaenn said:
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)?

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
 
Last edited by a moderator:
  • #10
maze said:
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
Thank you very much for the explanation and image. Extremely insightful.
 
Last edited by a moderator:
  • #11
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
Integral said:

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.
Ben Niehoff said:
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.

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:

What is the Trapezoidal Rule for numerical integration?

The Trapezoidal Rule is a method for approximating the area under a curve by dividing it into trapezoids and summing their areas. It is commonly used in numerical integration to estimate the value of an integral when the exact solution is unknown.

How does the Trapezoidal Rule work?

The Trapezoidal Rule works by dividing the interval of integration into equal segments, creating trapezoids that approximate the shape of the curve. The area of each trapezoid is then calculated and summed to estimate the total area under the curve.

What is the formula for the Trapezoidal Rule?

The formula for the Trapezoidal Rule is: h/2 * (f(a) + 2f(x1) + 2f(x2) + ... + 2f(xn-1) + f(b)), where h is the width of each trapezoid, f(x) is the function being integrated, and a and b are the limits of integration.

What are the advantages of using the Trapezoidal Rule for numerical integration?

The Trapezoidal Rule is a relatively simple and easy-to-use method for approximating the area under a curve. It can also provide a more accurate result than other numerical integration methods, such as the Midpoint Rule or Simpson's Rule, for certain types of functions.

What are the limitations of the Trapezoidal Rule?

The Trapezoidal Rule is limited in its accuracy, especially when used to approximate the area under a curve with sharp turns or discontinuities. It also requires a larger number of trapezoids to be more accurate, which can be time-consuming and computationally expensive.

Similar threads

Replies
24
Views
2K
Replies
16
Views
2K
Replies
3
Views
435
Replies
8
Views
1K
  • Calculus and Beyond Homework Help
Replies
4
Views
699
Replies
2
Views
1K
Replies
2
Views
1K
  • Calculus and Beyond Homework Help
Replies
2
Views
1K
Replies
5
Views
3K
Back
Top