trapezoidalRule
trapezoidalRule(
f,lower,upper,intervals?):number
Defined in: packages/math-analysis/src/trapezoidal-rule.ts:36
Approximates the area under a curve by slicing it into equal-width trapezoids and adding them up.
This is numerical integration at its most direct: f is evaluated at evenly
spaced points and the curve between consecutive points is replaced by the
straight line joining them. Use it when you have a function you can evaluate
but cannot integrate symbolically — which is most functions worth
integrating.
The error is O(h²) where h = (upper − lower) / intervals, so halving the
step quarters the error. That holds until the discretisation error falls
below the accumulated rounding error, past which adding panels stops helping;
for a smooth integrand over a unit interval that floor arrives around a
million panels.
O(intervals) calls to f; O(1) memory — the panels are accumulated, never
collected.
Parameters
Section titled “Parameters”(x) => number
The integrand.
number
Lower limit of integration.
number
Upper limit of integration.
intervals?
Section titled “intervals?”number = 1000
Number of panels; must be a positive integer.
Returns
Section titled “Returns”number
The approximated integral.
Example
Section titled “Example” trapezoidalRule(Math.sin, 0, Math.PI, 10); // 1.9835235375094546 trapezoidalRule(Math.sin, 0, Math.PI, 1000); // 1.9999983550656624, against an exact 2 ```;
## Throws
When `intervals` is not a positive integer.
## See
[Numerical integration](https://anthochamp.pages.grenadine.io/node-essentials/topics/math/calculus/quadrature/)