# Web toy: Lagrange's interpolation formula

Lagrange's interpolation formula says that given any finite set of points $\{(x_1,y_1),\ldots,(x_n,y_n)\}$ with all the $x_i$ distinct, you can construct the unique polynomial of minimum degree passing through all of those points by writing down $$\sum_{i=1}^n \left( y_i \prod_{\substack{j=1\\ j\neq i}}^n \frac{x-x_j}{x_i-x_j} \right)$$ For example, in the concrete case of three points, this would work out to $$y_1 \frac{(x-x_2)(x-x_3)}{(x_1-x_2)(x_1-x_3)} + y_2 \frac{(x-x_1)(x-x_3)}{(x_2-x_1)(x_2-x_3)} + y_3 \frac{(x-x_1)(x-x_2)}{(x_3-x_1)(x_3-x_2)}$$ This works because each of those fractions takes the value $1$ at the appropriate one of the $x_i$, and takes the value $0$ at all the other $x_i$. That in turn is because each factor of $(x-x_i)$ in the numerator makes the fraction zero at $x_i$ no matter what its denominator is; so the numerator gives you a polynomial which is zero at all but one of the $x_i$, and then to make it exactly $1$ at the remaining one, you find out what value it currently takes there, and divide by whatever that is.

Here's an HTML5 web toy that lets you play with this formula. Drag the control points around with the left mouse button, and the program will constantly recompute a polynomial going through all of them and display its coefficients and equation. Middle-click or Shift-click in an empty area to create an extra point; middle-click or Shift-click on an existing point to remove it.

(Tested on Firefox only, so far. Requires SVG and Javascript.)