Lagrange's formula handles points that are not equally spaced. Numerical integration uses similar ideas to find the area under a curve.
Lagrange's Interpolation Formula
For n + 1 points (x0, y0), …, (xn, yn), which need not be equally spaced, Lagrange's formula is y(x) = Σ y(i) L(i)(x), where L(i)(x) = Π over j ≠ i of (x − x(j))/(x(i) − x(j)). Each L(i) is 1 at x(i) and 0 at the other points, so the sum passes through every data point. For three points, y = y0 (x − x1)(x − x2)/((x0 − x1)(x0 − x2)) + y1 (x − x0)(x − x2)/((x1 − x0)(x1 − x2)) + y2 (x − x0)(x − x1)/((x2 − x0)(x2 − x1)). It is simple to write, but if a point is added, all the terms must be recomputed.
Numerical Integration
Numerical integration, or quadrature, finds an approximate value of ∫ from a to b of f(x) dx when f has no simple integral or is known only as a table. The interval [a, b] is divided into n equal strips of width h = (b − a)/n, giving points x0, …, xn with values y0, …, yn.
Definition: Numerical integration. The approximate evaluation of a definite integral using values of the function at a set of points, by replacing the curve with simpler shapes over small strips.
Trapezoidal Rule
The trapezoidal rule joins neighbouring points by straight lines and adds the areas of the trapeziums: ∫ f(x) dx ≈ (h/2)[y0 + yn + 2(y1 + y2 + … + y(n−1))]. It works for any number of strips. Its error is of order h², so halving h cuts the error to about a quarter.
Simpson's One-Third Rule
Simpson's 1/3 rule fits a parabola through each set of three points, which gives a better fit: ∫ f(x) dx ≈ (h/3)[y0 + yn + 4(y1 + y3 + …) + 2(y2 + y4 + …)]. The odd-numbered ordinates get weight 4 and the even interior ones weight 2. The number of strips n must be even. Its error is of order h⁴, so it is much more accurate than the trapezoidal rule for smooth functions, and it is exact for cubic polynomials.
Table 3.5: Integration rules
| Rule | Weights | Condition |
|---|---|---|
| Trapezoidal | 1, 2, 2, …, 2, 1 (× h/2) | Any n |
| Simpson's 1/3 | 1, 4, 2, 4, …, 4, 1 (× h/3) | n even |
| Error order | Trapezoidal h², Simpson h⁴ | Smaller h is better |
Example 3.5: Integrating by both rules
Problem: Evaluate ∫ from 0 to 1 of 1/(1 + x²) dx with h = 0.25 by the trapezoidal rule and Simpson's 1/3 rule. The exact value is π/4 = 0.785398.
Solution:
- y values: 1, 0.941176, 0.8, 0.64, 0.5.
- Trapezoidal: (0.25/2)[1 + 0.5 + 2(0.941176 + 0.8 + 0.64)] = 0.782794.
- Simpson: (0.25/3)[1 + 0.5 + 4(0.941176 + 0.64) + 2(0.8)] = 0.785392.
Result: The trapezoidal rule gives 0.782794 and Simpson's rule 0.785392, very close to the exact 0.785398.
f = lambda x: 1 / (1 + x * x)
a, b, n = 0, 1, 4
h = (b - a) / n
y = [f(a + i * h) for i in range(n + 1)]
trap = h / 2 * (y[0] + y[-1] + 2 * sum(y[1:-1]))
odd, even = sum(y[1:-1:2]), sum(y[2:-1:2])
simp = h / 3 * (y[0] + y[-1] + 4 * odd + 2 * even)
print(round(trap, 6), round(simp, 6))
Output
0.782794 0.785392
Common mistakes
- Using Simpson's 1/3 rule with an odd number of strips.
- Mixing up the weights 4 and 2 in Simpson's rule.
- Forgetting the factor h/2 or h/3 at the front.
Key takeaways
- Lagrange's formula works for unequal spacing.
- Trapezoidal: (h/2)[y0 + yn + 2(sum of the others)].
- Simpson's 1/3: (h/3)[y0 + yn + 4(odd) + 2(even)], with n even.
- Simpson's rule is more accurate for smooth functions.
Practice questions
- When is Lagrange's formula preferred?
- What condition must n satisfy for Simpson's 1/3 rule?
- Write the trapezoidal rule.
Answers to the practice questions
- When the data points are not equally spaced.
- The number of strips n must be even.
- (h/2)[y0 + yn + 2(y1 + … + y(n−1))].
Sources
- Official BCA syllabus Mathematics Foundations to Computer Science II, course code 25BCA402DS01, Unit III
