Can you go from the integral of x squared to the summation of x squared?

At first glance, $\frac{x^3}{3}$ and $\frac{x(x+1)(2x+1)}{6}$ seem like very different functions. Finding the sum of $n^2$ can be represented as a piecewise function: $f(x) = 0$ when $-1 ≤ x < 0$, $f(x) = 1$ when $0 ≤ x < 1$, $f(x) = 4$ when $1 ≤ x < 2$, $f(x) = 9$ when $2 ≤ x < 3$ and so on.

However, if we integrate a function that passes through the midpoints of each endpoint, such as $(-0.5, 0)$, $(0.5, 1)$, $(1.5, 4)$, the extreme values at either side will cancel out and produce a more accurate value. This is similar to using a midpoint rule to approximate an integral, only here we are using an integral to approximate a summation.

Therefore we have $f(x) = (x+0.5)^2$ and the area under it is:

$$\int (x+0.5)^2 \ \mathrm{d} x = \frac{(x+0.5)^3}{3} = \frac{1}{3}x^3 + \frac{1}{2}x^2+\frac{1}{4}x + \frac{1}{24}$$

which only differs by a linear term to:

$$\frac{x(x+1)(2x+1)}{6} = \frac{1}{3}x^3+\frac{1}{2}x^2+\frac{1}{6}x$$

The error comes from the fact that around $x = 0.5, 1.5, 2.5 \cdots$, $f(x)$ is not quite an odd function. Because the derivative of $f(x)$ is increasing, the right-hand side has slightly more area above the piecewise function compared to the left-hand side.

Now your task is to try to correct for the error.


The difference between the integral and the actual sum is the difference of $n^3/3$ and $\frac {n(n+1)(2n+1)}{6}$ that is $$\frac{(n+1)^2}{6}$$

Which grows very fast with $n$