An archive of Mark's Spring 2018 Numerical Analysis course.

An interpolating polynomial

mark

Find the cubic polynomial that interpolates the data:

x 1 2 4 8
y 3 -1 4 -2

Express your result in both Lagrange and Newton form.

funmanbobyjo

Using the code for Lagrange

  In [6]:def lagrange_p(x): return \
         3*((x-2)*(x-4)*(x-8))/((1-2)*(1-4)*(1-8))-\
         ((x-1)*(x-4)*(x-8))/((2-1)*(2-4)*(2-8))+\
         4*((x-1)*(x-2)*(x-8))/((4-1)*(4-2)*(4-8))-\
         2*((x-1)*(x-2)*(x-4))/((8-1)*(8-2)*(8-4))
         [lagrange_p(x) for x in [1,2,4,8]]
  Out[6]: [3.0, -1.0, 4.0, -2.0]

The code for Newtons

  In [11]: import numpy as np 
           def newton_poly(xs,ys):
           n = len(xs)
           p = np.poly1d(ys[0])
           for k in range(n):
             prod = np.poly1d(1)
             for j in range(k):
               prod = prod*np.poly1d([1,-xs[j]])
             c = (ys[k]-p(xs[k]))/prod(xs[k])
             p = p + c*prod
           return p
           newton_poly([1,2,4,8],[3,-1,4,-2])

 Out[11]: poly1d([ -0.4047619 ,   5.        , -16.16666667,  14.57142857])

I’m not sure if this is right for the newton’s method because I am having trouble understanding what what the output means here.

mark

Since this question is in the “Quiz or test prep” category, I recommend that you think about this problem using the tools available to you in that context - i.e. pencil and paper. Can you look at your Lagrange form and try to reformulate it in LaTeX?

Having said that, it’s certainly reasonable to use the code to check your answers from that angle. Also, the output from the newton_poly command indicates the coefficients of the interpolating polynomial. Thus,

p(x) = -0.4047619 + 5 x -16.16666667 x^2 + 14.57142857 x^3.
Cornelius

The Lagrange form for the polynomial that interpolates the data is:
3\bigg(\frac{(x-2)(x-4)(x-8)}{(1-2)(1-4)(1-8)}\bigg)-\bigg(\frac{(x-1)(x-4)(x-8)}{(2-1)(2-4)(2-8)}\bigg)+4\bigg(\frac{(x-1)(x-2)(x-8)}{(4-1)(4-2)(4-8)}\bigg)-2\bigg(\frac{(x-2)(x-4)(x-1)}{(8-2)(8-4)(8-1)}\bigg).

The process for obtaining this formula is straightforward, but tedious.
Suppose you have x_0, x_1,x_2 with corresponding y_0.y_1,y_2, then the Lagrange form looks like:
y_0\bigg(\frac{(x-x_1)(x-x_2)}{(x_0-x_1)(x_0-x_2)}\bigg)+y_1\bigg(\frac{(x-x_0)(x-x_2)}{(x_1-x_0)(x_1-x_1)}\bigg)+y_2\bigg(\frac{(x-x_1)(x-x_0)}{(x_2-x_1)(x_2-x_0)}\bigg)

Cornelius

To obtain the Newton’s form we follow the following process:
First set
P_0(x)=3

Then P_1(x)=P_0(x)+c(x-1).

Set P_1(2)=-1 and solve for c.

P_1(2)=3+c(2-1)=-1 \rightarrow3+c=-1 \rightarrow c=-4

So P_1(x)= P_0(x)-4(x-1).

Repeat this process for all data values.

I believe that the final Newton form is:
P_4(x)=3-4(x-1)+\frac{13}{6}(x-1)(x-2)-\frac{17}{42}(x-1)(x-2)(x-4)

mark