2.1. Polynomial interpolation#
The United States carries out a census of its population every 10 years. Suppose we want to know the population at times in between the census years, or to estimate future populations. One technique is to find a polynomial that passes through all of the data points.1
Given \(n\) points \((t_1,y_1),\ldots,(t_n,y_n)\), where the \(t_i\) are all distinct, the polynomial interpolation problem is to find a polynomial \(p\) of degree less than \(n\) such that \(p(t_i)=y_i\) for all \(i\).
As posed in Definition 2.1.1, the polynomial interpolation problem has a unique solution. Once the interpolating polynomial is found, it can be evaluated anywhere to estimate or predict values.
Interpolation as a linear system#
Given data \((t_i,y_i)\) for \(i=1,\ldots,n\), we seek a polynomial
such that \(y_i=p(t_i)\) for all \(i\). These conditions are used to determine the coefficients \(c_1\ldots,c_n\):
These equations form a linear system for the coefficients \(c_i\):
or more simply, \(\mathbf{V} \mathbf{c} = \mathbf{y}\). The matrix \(\mathbf{V}\) is of a special type.
Given distinct values \(t_1,\ldots,t_n\), a Vandermonde matrix for these values is the \(n\times n\) matrix appearing in (2.1.2).
Polynomial interpolation can therefore be posed as a linear system of equations with a Vandermonde matrix.
Attention
Recall that the demos in this and later chapters omit the statement
using FundamentalsNumericalComputation
that is needed to run some of the statements.
We create two vectors for data about the population of China. The first has the years of census data and the other has the population, in millions of people.
year = [1982,2000,2010,2015];
pop = [1008.18, 1262.64, 1337.82, 1374.62];
It’s convenient to measure time in years since 1980. We use .-
to subtract a scalar from every element of a vector. We will also use a floating-point value in the subtraction, so the result is also in double precision.
A dotted operator such as .-
or .*
acts elementwise, broadcasting scalar values to match up with elements of an array.
t = year .- 1980.0
y = pop;
Now we have four data points \((t_1,y_1),\dots,(t_4,y_4)\), so \(n=4\) and we seek an interpolating cubic polynomial. We construct the associated Vandermonde matrix:
An expression inside square brackets and ending with a for
statement is called a comprehension. It’s often an easy and readable way to construct vectors and matrices.
V = [ t[i]^j for i=1:4, j=0:3 ]
4×4 Matrix{Float64}:
1.0 2.0 4.0 8.0
1.0 20.0 400.0 8000.0
1.0 30.0 900.0 27000.0
1.0 35.0 1225.0 42875.0
To solve for the vector of polynomial coefficients, we use a backslash to solve the linear system:
A backslash \
is used to solve a linear system of equations.
c = V \ y
4-element Vector{Float64}:
962.2387878787875
24.127754689754774
-0.5922620490620537
0.00684386724386731
The algorithms used by the backslash operator are the main topic of this chapter. As a check on the solution, we can compute the residual.
y - V*c
4-element Vector{Float64}:
0.0
0.0
2.2737367544323206e-13
0.0
Using floating-point arithmetic, it is not realistic to expect exact equality of quantities; a relative difference comparable to \(\macheps\) is all we can look for.
By our definitions, the elements of c
are coefficients in ascending-degree order for the interpolating polynomial. We can use the polynomial to estimate the population of China in 2005:
The Polynomials
package has functions to make working with polynomials easy and efficient.
p = Polynomial(c) # construct a polynomial
p(2005-1980) # include the 1980 time shift
1302.2043001443
The official population value for 2005 was 1303.72, so our result is rather good.
We can visualize the interpolation process. First, we plot the data as points.
The scatter
function creates a scatter plot of points; you can specify a line connecting the points as well.
scatter(t,y, label="actual", legend=:topleft,
xlabel="years since 1980", ylabel="population (millions)",
title="Population of China")
We want to superimpose a plot of the polynomial. We do that by evaluating it at a vector of points in the interval. The dot after the name of the polynomial is a universal way to apply a function to every element of an array, a technique known as broadcasting.
The range
function constructs evenly spaced values given the endpoints and either the number of values, or the step size between them.
Adding a dot to the end of a function name causes it to be broadcast, i.e., applied to every element of a vector or matrix.
# Choose 500 times in the interval [0,35].
tt = range(0,35,length=500)
# Evaluate the polynomial at all the vector components.
yy = p.(tt)
foreach(println,yy[1:4])
962.2387878787875
963.9282039963299
965.6118068288089
967.2896105457506
Now we use plot!
to add to the current plot, rather than replacing it.
The plot
function plots lines connecting the given \(x\) and \(y\) values; you can also specify markers at the points.
By convention, functions whose names end with the bang !
change the value or state of something, in addition to possibly returning output.
plot!(tt,yy,label="interpolant")
Exercises#
Suppose you want to interpolate the points \((-1,0)\), \((0,1)\), \((2,0)\), \((3,1)\), and \((4,2)\) by a polynomial of as low a degree as possible.
(a) ✍ What is the maximum necessary degree of this polynomial?
(b) ✍ Write out a linear system of equations for the coefficients of the interpolating polynomial.
(c) ⌨ Use Julia to solve the system in (b) numerically.
(a) ✍ Say you want to find a cubic polynomial \(p\) such that \(p(-1) =-2\), \(p'(-1) =1\), \(p(1) = 0\), and \(p'(1) =-1\). (This is known as a Hermite interpolant.) Write out a linear system of equations for the coefficients of \(p\).
(b) ⌨ Use Julia to solve the linear system in part (a), and make a plot of \(p\) over \(-1 \le x \le 1\).
⌨ Here are population figures (in millions) for three countries over a 30-year period (from United Nations World Population Prospects, 2019).
1990
2000
2010
2020
United States
252.120
281.711
309.011
331.003
India
873.278
1,056.576
1,234.281
1,380.004
Poland
37.960
38.557
38.330
37.847
(a) Use cubic polynomial interpolation to estimate the population of the USA in 2005.
(b) Use cubic polynomial interpolation to estimate when the population of Poland peaked during this time period.
(c) Use cubic polynomial interpolation to make a plot of the Indian population over this period. Your plot should be well labeled and show a smooth curve as well as the original data points.
⌨ Here are the official population figures for the state of Delaware, USA, every ten years from 1790 to 1900: 59096, 64273, 72674, 72749, 76748, 78085, 91532, 112216, 125015, 146608, 168493, 184735. For this problem, use
\[ t = \frac{\text{year} - 1860}{10} \]as the independent (time) variable.
(a) Using only the data from years 1860 to 1900, plot the interpolating polynomial over the same range of years. Add the original data points to your plot as well.
(b) You might assume that adding more data will make the interpolation better. But this is not always the case. Use all the data above to create an interpolating polynomial of degree 11, and then plot that polynomial over the range 1860 to 1900. In what way is this fit clearly inferior to the previous one? (This phenomenon is studied in Chapter 9.)
- 1
We’re quite certain that the U.S. Census Bureau uses more sophisticated modeling techniques than the one we present here!