Let’s say we have two sets of real numbers $\{x_1, x_2, ..., x_{n+1}\}$ and $\{y_1, y_2, ..., y_{n+1}\}$ with $x_i \ne x_j$ if $i \ne j$ (i.e. the first set has no duplicate values).
Here’s an interesting question:
<aside>
How can we find a polynomial $p(x)$ of degree less than or equal to $n$, such that $p(x_i) = y_i$ for $i = 1, 2, ..., n+1$? (i.e. $p(x_1) = y_1$, $p(x_2) = y_2$, etc. up to $p(x_{n+1}) = y_{n+1}$)
</aside>
Visually, this means that we want $p(x)$ to pass through the points $(x_1, y_1)$, $(x_2, y_2)$, etc. up to $(x_{n+1}, y_{n+1})$.
This is what Lagrange interpolation can help us with! To use Lagrange interpolation, we need to define these functions:
$$ L_1(x) = \frac{(x-x_2)(x-x_3)\cdots(x-x_{n+1})}{(x_1-x_2)(x_1-x_3)\cdots(x_1-x_{n+1})} $$
$$ L_2(x) = \frac{(x-x_1)(x-x_3)\cdots(x-x_{n+1})}{(x_2-x_1)(x_2-x_3)\cdots(x_2-x_{n+1})} $$
We keep defining these functions in a similar fashion up to:
$$ L_{n+1}(x) = \frac{(x-x_1)(x-x_2)\cdots(x-x_n)}{(x_{n+1}-x_1)(x_{n+1}-x_2)\cdots(x_{n+1}-x_n)} $$
The polynomial $p(x)$ that solves our original problem is:
$$ p(x) = y_1L_1(x) + y_2L_2(x) + \cdots + y_{n+1}L_{n+1}(x) $$
Here’s a Desmos graph that shows Lagrange interpolation creating a smooth curve through any 3 points you give it! And here’s a version with 5 points.