I sometimes need a function, and want it to be both weird and sexy. To define this function, I want to be unstructured, I don’t want to limit my imagination. I want to be able to define a set of ordered points, and then have a function that when plotted, its graph passes through them. But I need more than just being able to plot the function, I need to know its exact expression, so that I can use it later. Can we do that? That is, can we find a closed form to express such a function?
And as Obama would have said many years ago, yes we can!
Given
Do you want to find it? I want. Let’s go!
A polynomial, what?
A polynomial of degree
The set

Polynomials of
If we plot
A system of equations
Our problem is finding the coefficients’ values for which the graph of
And what can we do to find them? Well, the points need to be in the image of
To find the answer to our problem, we need to solve the system.
Things look good a priori, as in general we can only find the values of
Bring on the matrices
We switch to matrix notation and define
so that then we can describe our problem more compactly as
In particular,
A solution, but not THE solution
Thanks to good old Algebra, after switching the notation, we see that in the end, the solution is to simply get
End of story? Well…just hold on to your belts!
First, we should ask ourselves whether
Second, to compute
Fortunately, Lagrange came to the rescue and saved us all.
Lagrange Interpolation
Lagrange changed a little bit the way we were thinking about the problem, and proposed to think the polynomial
and then proved that the solution was to define
Analyzing the expressions, we see that
I mean…what a beast, such a clever and elegant solution. Chapeau!
The interpolation in action
Let’s analyse one case to convince ourselves! Say we want a function for the following

Points that must be on the image of
I guess we would all panic if we are asked to solve this, but we just learned about Lagrange’s interpolation, so we know we can find the polynomial of degree
In particular, let’s gather the functions that compose
xx = np.linspace(0, 4, 200)
a1phi1 = -2.8 * \
((xx - 0.93) * (xx - 1.73) * (xx - 2.57) * (xx - 3.49)) / \
((0.36 - 0.93) * (0.36 - 1.73) * (0.36 - 2.57) * (0.36 - 3.49))
a2phi2 = 1.64 * \
((xx - 0.36) * (xx - 1.73) * (xx - 2.57) * (xx - 3.49)) / \
((0.93 - 0.36) * (0.93 - 1.73) * (0.93 - 2.57) * (0.93 - 3.49))
a3phi3 = -0.5 * \
((xx - 0.36) * (xx - 0.93) * (xx - 2.57) * (xx - 3.49)) / \
((1.73 - 0.36) * (1.73 - 0.93) * (1.73 - 2.57) * (1.73 - 3.49))
a4phi4 = 2.77 * \
((xx - 0.36) * (xx - 0.93) * (xx - 1.73) * (xx - 3.49)) / \
((2.57 - 0.36) * (2.57 - 0.93) * (2.57 - 1.73) * (2.57 - 3.49))
a5phi5 = -1.8 * \
((xx - 0.36) * (xx - 0.93) * (xx - 1.73) * (xx - 2.57)) / \
((3.49 - 0.36) * (3.49 - 0.93) * (3.49 - 1.73) * (3.49 - 2.57))
And let’s just plot and see how these functions look like.

Functions that compose
The first feeling when you look at this is “oh, it will be hard to prove that this truly adds up to what we need”.
But to simplify, recall that each function focuses on one point at a time, while ensuring it does not bother the other.
This is why, for the points highlighted, there is only one where
Final step, let’s add these functions and see what we get.

Resulting
An image sometimes is worth more than a million words, so I will limit myself to simpley say beautiful.
Generalizing: any and all polyomials?!
A natural question that follows is whether we could not have found a more “compact” solution that the one that Lagrange proposed.
In short, the answer is no, but let’s reason about it.
For example, if we were able to find some linear dependency among the
The fact that the set of Lagrange’s functions
- we can produce all the polynomials of degree
, and; - for each polynomial, there is a unique combination of the
functions that produce them.
For example, another basis for the same sub-space is the canonical one, conformed by the functions

Polynomials of degree
I know, if we compare the functions on the plot we just obtained for the canonical basis with the ones from Lagrange’s interpolation we calculated before, it is human to hesitate …are we sure that the very clever formulation of Lagrange is also a basis? Well, only reason can defeat fear, so let’s prove it to convince ourselves.
To verify that Lagrange’s functions conform a basis of the
If we can show that
As the aforementioned condition must hold for all
but based on our previous analysis, we know that this ends up evaluating to
We need to agree that Lagrange’s solution is simply beautiful, isn’t it?
Implementing Lagrange’s interpolation
Now that we know many things about Lagrange’s interpolation, the last point I want to address is how to implement it efficiently in code.
To reason about this, let’s first write down the final expression of
And I will continue writing soon…