Department of Applied Mathematics and Theoretical Physics

Numerical Analysis Course

Polynomial interpolation

Given $n + 1$ distinct real points $x_0,..., x_n$ and real numbers $f_0,..., f_n$, we seek a polynomial function $p$ such that $p(x_i)=f_i$ for all $i = 0,...,n$. Such a polynomial function is a polynomial interpolant.


Lagrange formula

To determine a polynomial interpolant given $x_i$ and $f_i$ as above, it is convenient to use Lagrange formula (first discovered by Waring):

$p(x) = \sum_{k=0}^{n} f_k \prod_{l = 0, l \neq k}^{n} \frac{x-x_l}{x_k - x_l}$

where $x\in R$. Note that this is the unique polynomial of degree at most $n$ fitting through the given data points and values.


Running the downloadable MATLAB® code on this page opens a GUI which allows you to play with different interpolation points, always using Lagrange interpolation.

Notice that the scaling of the axes might change.


In this GUI, the interpolation is made using equispaced points, Chebyshev points, and custom points.

Equispaced points : There is not much to say here. The x-coordinates of the interpolation nodes are equispaced.

Chebyshev points : These are the roots of the Chebyshev polynomial of the first kind. For a given $n$, the Chebyshev points are given

$x_i = 5\cos\left(\frac{2i-1}{2n}\pi\right)$ for $i = 1,...,n$ (the multiplicative factor of 5 is because we are working with the symmetric interval [-5,5]).

They are particularly good points since they minimise Runge's phenomenon (look at the error graphs to see this!)

Custom points : By clicking on the Custom axis, you can activate a pointer. Click on the coordinate point where you want to place the new interpolation node. There is a Reset button to clear all the custom interpolation nodes created so far.

The interface

Edit box Function : Here you can enter a function f(x) to be interpolated. You will have to enter it using MATLAB® notation, e.g. x^2 is NOT accepted, while x.^2 is accepted. The preset function is Runge's example, namely $f(x) = \frac{1}{x^2 +1}$. Once you have entered the function, press the Interpolate button to update the graphs.

Edit box No. of nodes: : Here you can enter the number of interpolation nodes. This number will affect only the interpolations done with equispaced and Chebyshev points. The preset number of nodes is 10. Once you have entered the number of nodes, press the Interpolate button to update the graphs.

Push button Interpolate : Press to update the graphs. All the graphs will show the original function in blue and the interpolation in red.

Panel Show error : Select *Equispaced if you want to see the error (in magnitude) of the interpolation with equispaced points; select Chebyshev if you want to see the (in magnitude) of the interpolation with Chebyshev points; select Custom if you want to see the error (in magnitude) of the interpolation with custom points. After you select any of the options, the error graph will automatically update. Note that while the Custom option is on, the graph of the error will update automatically every time a new custom interpolation node is added.

Push Button Reset: This button only affects the interpolation with custom points. It clears all the previous custom interpolation added.

Push button Close : Press this button to close the GUI.

The Code

All files as .zip archive: