Integrate

I = InterpolatingFunction.Integrate(f) integrates the interpolating function f over its whole interval, I = InterpolatingFunction.Integrate(f, a, b) from $a$ to $b$, $$ I = \int_a^b f(x) \, dx. $$

Input

  • f : an interpolating function.
  • a, b : (optional) Reals, the limits. For $a > b$ the result is $-\int_b^a f(x) dx$. Both limits must lie inside the interval of f, from its first to its last point (for a DKBSpline its knots); otherwise it is an error.

Output

  • I : Real, the integral.

Spline, HermiteCSpline and BSpline are integrated by Gauss–Legendre quadrature on every interval between two knots, with enough points to be exact for the polynomial on that interval: the integral is exact for the interpolating function. A DKBSpline is integrated with the Gauss–Legendre points per knot interval of the atom it comes from. It is not a polynomial because of its terms in $1/r$, and the quadrature converges exponentially with the number of points.

Example

Input

Example.Quanty
-- x^2 sampled at 5 points, interpolated by a B-spline of degree 2, which is exact for x^2
x = {0, 0.5, 1, 1.5, 2}
y = {}
for i = 1, #x do y[i] = x[i]^2 end
f = InterpolatingFunction.BSpline(x, y, 2)
print(string.format("from 0 to 2:   %.12f (exact %.12f)", InterpolatingFunction.Integrate(f), 8/3))
print(string.format("from 1 to 0.3: %.12f (exact %.12f)", InterpolatingFunction.Integrate(f, 1, 0.3), (0.3^3 - 1)/3))
ok, message = pcall(function() return InterpolatingFunction.Integrate(f, 0, 3) end)
print(message)
Example.out
from 0 to 2:   2.666666666667 (exact 2.666666666667)
from 1 to 0.3: -0.324333333333 (exact -0.324333333333)
Example.Quanty:8: error in InterpolatingFunction.Integrate, integration limits out of interpolation range

Table of contents

Print/export