Numerical Integration Calculator
Estimate the area under a curve from a table of values, with the trapezoidal rule and Simpson's rule side by side.
What it does
Some functions have no antiderivative you can write down, and some have one that is too awkward to use. When that happens the area under the curve is found by splitting it into pieces and adding up simple shapes instead, and the two rules you will meet first are the trapezoidal rule and Simpson's rule.
The trapezoidal rule is the older and the simpler. The DLMF, the digital library of mathematical functions published by NIST, writes the composite version as the step times half the first value, all the interior values, and half the last value. It also gives the error term, which is minus the length of the range times the step squared over twelve, multiplied by the second derivative at some point inside the range.
Simpson's rule comes next in the same reference. It takes every other point with a weight of four, every second point in between with a weight of two and the ends with a weight of one, all multiplied by a third of the step. Its error term involves the fourth derivative and the fourth power of the step, which is why it is much the better rule for a smooth curve.
The OpenStax calculus text behind this page puts both in plain terms. The trapezoidal rule approximates the curve with piecewise linear functions and the midpoint rule with piecewise constant ones, and it notes that the trapezoidal rule tends to overestimate a concave curve and underestimate a convex one, while the midpoint rule tends to average the two errors out.
This page takes a table of values, checks that the x values are equally spaced, and returns both estimates, the difference between them, which is a rough measure of the error in the trapezoidal answer, and the average value of the function over the range. It is the tool for when you have measured data rather than a formula.
How to use
- Enter the points as x,y pairs separated by semicolons, for example 0,0; 1,1; 2,4; 3,9; 4,16.
- Use at least 3 points, and more if you can, because the error of both rules falls as the spacing gets smaller.
- Make sure the x values are in order and evenly spaced, since the rules assume equal steps.
- Read the trapezoidal result, which always works and is exact for a straight line.
- Read the Simpson result when the number of intervals is even, and compare it with the trapezoidal one.
- Read the difference between them as a measure of how much the answer might still move if you added more points.
Formula
- h
- Step between the points, the length of the range divided by the number of intervals.
- f0 to fn
- The y values of the table, in order.
- n
- Number of intervals, which is one less than the number of points.
The error of the composite trapezoidal rule is of the order of the step squared, while the error of the composite Simpson's rule is of the order of the step to the fourth, so halving the step makes the trapezoidal error four times worse and the Simpson error sixteen times better. The average value of the function over the range is the integral divided by the length of the range.
Worked example
Example: the area under y equals x squared from 0 to 4
Defaults: the five points 0,0; 1,1; 2,4; 3,9; 4,16.
- The table has 5 points, so there are 4 intervals with a step of 1.
- The trapezoidal rule gives the step times half the first value, plus the three interior values, plus half the last value: 1 x (0 over 2 + 1 + 4 + 9 + 16 over 2) = 22.000000.
- Simpson's rule weights the ends by 1, the odd points by 4 and the even interior point by 2: 1 over 3 x (0 + 4 x 1 + 2 x 4 + 4 x 9 + 16) = 21.333333.
- The difference between the two is 0.666667, which is a fair measure of the error in the trapezoidal answer.
- The exact integral of x squared from 0 to 4 is 64 over 3 = 21.333333, so here Simpson's rule is exact and the trapezoidal rule is 3.1 percent high.
- The average value of the function over the range is the integral divided by the length, 21.333333 over 4 = 5.500000.
- The tool shows 22.000000, 21.333333, 0.666667, 5.500000 and a table of 4 intervals with a step of 1.000000.
Reference table
| Points | Intervals | Trapezoidal | Simpson's rule |
|---|---|---|---|
| 5 points | 4 | 22.000000 | 21.333333 |
| 9 points | 8 | 21.500000 | 21.333333 |
| 17 points | 16 | 21.375000 | 21.333333 |
| 33 points | 32 | 21.343750 | 21.333333 |
Limits
- The rules need evenly spaced points, and the tool refuses a table that does not have them.
- They approximate a curve by simple shapes, so a sharp corner or a vertical step in the data produces a large error that more points in the wrong place will not fix.
- The error bounds in the reference depend on the second or fourth derivative of the function, which you often do not know when the data is measured rather than calculated.
- The tool reports the difference between the two rules as a guide to the error, but that is a rule of thumb and not a bound.
Common mistakes
- Using Simpson's rule with an odd number of intervals, when the rule does not apply at all.
- Forgetting the weights in Simpson's rule and averaging the values instead.
- Leaving out the halves on the first and last values in the trapezoidal rule, which changes the answer by a quarter of a step.
- Using a table with unevenly spaced x values and expecting the answer to mean anything.
- Reading the difference between the two rules as the true error. It is a good clue, but the true error can be larger.
Try it yourself
Quick checks. Answers sit tucked under each question.
What happens if the table is split more finely?
With 9 points the trapezoidal rule gives 21.500000 and Simpson's rule 21.333333, and with 17 points they give 21.375000 and 21.333333. The trapezoidal error falls by a factor of about four each time the step is halved, while the Simpson error falls by a factor of about sixteen, which is the square root of the step improvement against the fourth power.
Why is the trapezoidal rule 3.1 percent high for this function?
Because y equals x squared curves upwards, and the trapezoidal rule draws straight lines across each interval, so every piece sits above the curve. The OpenStax text says the same thing: the rule overestimates a concave up function and underestimates a concave down one.
What is the average value of x squared between 0 and 4?
5.500000, which is the integral of 21.333333 divided by the length of the range, 4. It is a useful number because it says that over that interval the function behaves on average as if it were the constant 5.5.
Questions
When do I use numerical integration?
When there is no convenient antiderivative, when the function is only known as a table of measured values, or when the data is noisy. It is also the basis of numerical methods in physics and engineering, from the orbit of a satellite to the load on a structure.
Which rule is more accurate?
Simpson's rule, by a wide margin. The DLMF gives the trapezoidal error as the step squared and the Simpson error as the step to the fourth, so for the same table Simpson's rule is usually far closer to the truth.
Why does Simpson's rule need an even number of intervals?
Because it fits a parabola through every three points, so the points have to pair up. With an odd number of intervals the last panel can be handled with a separate rule, but the simple version does not apply.
Can I use a table with unevenly spaced points?
Not with these rules. The trapezoidal rule can be generalised to uneven spacing by using the width of each panel, but Simpson's rule cannot, and the tool asks for even spacing so that both numbers are comparable.
What is the average value of a function?
The integral over the range divided by the length of the range, which gives the constant that would give the same integral. The OpenStax text defines the absolute and relative error of an estimate in the same section, which is what you use to judge how good the estimate is.
How do I know which result to trust?
Compare the two rules and then add more points. If the two numbers are close, both are close to the true value, and if they disagree, the answer is still moving and the table needs more points in the place where the curve bends most.
Sources
- NIST digital library of mathematical functions, section 3.5: quadrature areas, with the error terms
- OpenStax Calculus Volume 2, section 3.6: numerical integration, the trapezoidal rule and Simpson's rule