../_images/book_cover.jpg

This notebook contains an excerpt from the Python Programming and Numerical Methods - A Guide for Engineers and Scientists, the content is also available at Berkeley Python Numerical Methods.

The copyright of the book belongs to Elsevier. We also have this interactive book online for a better learning experience. The code is released under the MIT license. If you find this content useful, please consider supporting the work on Elsevier or Amazon!

< 17.4 Lagrange Polynomial Interpolation | Contents | 17.6 Summary and Problems >

Newton’s Polynomial Interpolation

Newton’s polynomial interpolation is another popular way to fit exactly for a set of data points. The general form of the an n1 order Newton’s polynomial that goes through n points is:

f(x)=a0+a1(xx0)+a2(xx0)(xx1)++an(xx0)(xx1)(xxn)

which can be re-written as:

f(x)=ni=0aini(x)

where $ni(x)=i1j=0(xxj)$

The special feature of the Newton’s polynomial is that the coefficients ai can be determined using a very simple mathematical procedure. For example, since the polynomial goes through each data points, therefore, for a data points (xi,yi), we will have f(xi)=yi, thus we have

f(x0)=a0=y0

And f(x1)=a0+a1(x1x0)=y1, by rearranging it to get a1, we will have:

a1=y1y0x1x0

Now, insert data points (x2,y2), we can calculate a2, and it is in the form:

a2=y2y1x2x1y1y0x1x0x2x0

Let’s do one more data points (x3,y3) to calculate a3, after insert the data point into the equation, we get:

a3=y3y2x3x2y2y1x2x1x3x1y2y1x2x1y1y0x1x0x2x0x3x0

Now, see the patterns? These are called divided differences, if we define:

f[x1,x0]=y1y0x1x0
f[x2,x1,x0]=y2y1x2x1y1y0x1x0x2x0=f[x2,x1]f[x1,x0]x2x1

We continue write this out, we will have the following iteration equation:

f[xk,xk1,,x1,x0]=f[xk,xk1,,x2,x2]f[xk1,xk2,,x1,x0]xkx0

We can see one beauty of the method is that, once the coefficients are determined, adding new data points won’t change the calculated ones, we only need to calculate higher differences continues in the same manner. The whole procedure for finding these coefficients can be summarized into a divided differences table. Let’s see an example using 5 data points:

x0y0f[x1,x0]x1y1f[x2,x1,x0]f[x2,x1]f[x3,x2,x1,x0]x2y2f[x3,x2,x1]f[x4,x3,x2,x1,x0]f[x3,x2]f[x4,x3,x2,x1]x3y3f[x4,x3,x2]f[x4,x3]x4y4

Each element in the table can be calculated using the two previous elements (to the left). In reality, we can calculate each element and store them into a diagonal matrix, that is the coefficients matrix can be write as:

y0f[x1,x0]f[x2,x1,x0]f[x3,x2,x1,x0]f[x4,x3,x2,x1,x0]y1f[x2,x1]f[x3,x2,x1]f[x4,x3,x2,x1]0y2f[x3,x2]f[x4,x3,x2]00y3f[x4,x3]000y40000

Note that, the first row in the matrix is actually all the coefficients that we need, i.e. a0,a1,a2,a3,a4. Let’s see an example how we can do it.

TRY IT! Calculate the divided differences table for x = [-5, -1, 0, 2], y = [-2, 6, 1, 3].

import numpy as np
import matplotlib.pyplot as plt

plt.style.use('seaborn-poster')

%matplotlib inline
Copy to clipboard
def divided_diff(x, y):
    '''
    function to calculate the divided
    differences table
    '''
    n = len(y)
    coef = np.zeros([n, n])
    # the first column is y
    coef[:,0] = y
    
    for j in range(1,n):
        for i in range(n-j):
            coef[i][j] = \
           (coef[i+1][j-1] - coef[i][j-1]) / (x[i+j]-x[i])
            
    return coef

def newton_poly(coef, x_data, x):
    '''
    evaluate the newton polynomial 
    at x
    '''
    n = len(x_data) - 1 
    p = coef[n]
    for k in range(1,n+1):
        p = coef[n-k] + (x -x_data[n-k])*p
    return p
Copy to clipboard
x = np.array([-5, -1, 0, 2])
y = np.array([-2, 6, 1, 3])
# get the divided difference coef
a_s = divided_diff(x, y)[0, :]

# evaluate on new data points
x_new = np.arange(-5, 2.1, .1)
y_new = newton_poly(a_s, x, x_new)

plt.figure(figsize = (12, 8))
plt.plot(x, y, 'bo')
plt.plot(x_new, y_new)
Copy to clipboard
[<matplotlib.lines.Line2D at 0x11bd4e630>]
Copy to clipboard
../_images/chapter17.05-Newtons-Polynomial-Interpolation_6_1.png

We can see that the Newton’s polynomial goes through all the data points and fit the data.

< 17.4 Lagrange Polynomial Interpolation | Contents | 17.6 Summary and Problems >