Introduction to Numerical Methods/Interpolation

From testwiki
Jump to navigation Jump to search

Objectives

  • understand interpolation
  • derive Newton’s divided difference method of interpolation
  • derive Lagrangian method of interpolation
  • apply the interpolation methods to solve problems
  • find derivatives and integrals of discrete functions using interpolation

Resources

Interpolation

Interpolation is the process of deriving a simple function from a set of discrete data points so that the function passes through all the given data points (i.e. reproduces the data points exactly) and can be used to estimate data points in-between the given ones. It is necessary because in science and engineering we often need to deal with discrete experimental data. Interpolation is also used to simplify complicated functions by sampling data points and interpolating them using a simpler function. Polynomials are commonly used for interpolation because they are easier to evaluate, differentiate, and integrate - known as polynomial interpolation.

It can be proven that given n+1 data points it is always possible to find a polynomial of order/degree n to pass through/reproduce the n+1 points.

Direct Method

Given n+1 data points the direct method assumes the following polynomial:

y=f(x)=a0+a1x+a2x2+...+anxn

With n+1 values for x and the n+1 corresponding values for y we can solve for a0,a1,...,an by solving the n+1 simultaneous linear equations, which is known as the direct method.

For example, given two data points (x0,y0) and (x1,y1) we can use a linear function y=f(x)=a0+a1x to pass through the two data points:

y0=a0+a1x0
y1=a0+a1x1

Once we solve for a0and a1 (the coefficients of f(x)) we can use the function as the basis for interpolation - estimating the missing data points in-between.

Newton's Method

In Newton's method the interpolating function is written in Newton polynomial(a.k.a Newton form). For example, given one data point (x0,y0) we can only derive a polynomial of order zero: y=f(x)=a0. Because f(x0)=y0 the newton polynomial of order zero is y=f(x)=y0.

Given two data points we can write Newton's polynomial in the form of y=f(x)=a0+a1(xx0). Plugging in the two data points gives us

a0=y0
a1=y1y0x1x0

Obviously this function passes through both data points (i.e. accurately reproduces the two data points). The first derivative of the function at x=x0 is f(x0)=a1=y1y0x1x0, which matches the result from the forward divided difference method.

Given three data points we can write Newton's polynomial in the form of

y=f(x)=a0+a1(xx0)+a2(xx1)(xx0)

Plugging in the three data points gives us

As y0=a0 we get a0=y0
As y1=a0+a1(x1x0) we get a1=y1y0x1x0
As y2=a0+a1(x2x0)+a2(x2x1)(x2x0)
we get
a2=y2a0a1(x2x0)(x2x1)(x2x0)=y2y0y1y0x1x0(x2x0)(x2x1)(x2x0)=y2y0y1y0x1x0((x2x1)+(x1x0))(x2x1)(x2x0)=y2y0y1y0x1x0(x2x1)(y1y0)(x2x1)(x2x0)=y2y1y1y0x1x0(x2x1)(x2x1)(x2x0)=y2y1x2x1y1y0x1x0x2x0

This third degree polynomial function passes all three data points (the second derivative and the third derivative at x0 and x1match that from the divided difference method).

From the two examples we can see the coefficients of a Newton polynomial follow a pattern known as divided difference. For examplea1=y1y0x1x0 is called divided difference of order 1 (denoted as f[x0,x1]) because it depends on x0 and x1. The divided difference notation can be used to write the general order (form) Newton polynomial:

f(x)=f[x0]+f[x0,x1](xx0)+f[x0,x1,x2](xx0)(xx1)+...
+f[x0,x1,x2,...,xn](xx0)(xx1)(xx2)...(xxn1)

Where

a0=f[x0]=y0 because f[xi]=yi by definition
a1=f[x0,x1]=f[x1]f[x0]x1x0
a2=f[x0,x1,x2]=f[x1,x2]f[x0,x1]x2x0
ak=f[x0,x1,x2,,xk1,xk]=f[x1,x2,,xk1,xk]f[x0,x1,x2,,xk1]xkx0

We can calculate the coefficients of Newton polynomial using the following table

x0y0=f[x0]f[x0,x1]x1y1=f[x1]f[x0,x1,x2]f[x1,x2]f[x0,x1,x2,x3]x2y2=f[x2]f[x1,x2,x3]f[x2,x3]x3y3=f[x3]

because

f[xi]=yi
f[xi,xi+1]=f[xi+1]f[xi]xi+1xi

The following figures shows the dependency between the divided differences:

A table for solving the coefficients of a Newton's polynomial.

For example, given data points (1,6), (2,11), (3,18), and (4,27) we can draw the following table:

ixiyif[xi,xi+1]f[xi,xi+1,xi+2]f[xi,xi+1,xi+2,xi+3]0x0=1y0=f[x0]=6f[x0,x1]=f[x1]f[x0]x1x0=11621=5f[x0,x1,x2]=f[x1,x2]f[x0,x1]x2x0=7531=1f[x0,x1,x2,x3]=f[x1,x2,x3]f[x0,x1,x2]x3x0=1141=01x1=2y1=f[x1]=11f[x1,x2]=f[x2]f[x1]x2x1=181132=7f[x1,x2,x3]=f[x2,x3]f[x1,x2]x3x1=9742=12x2=3y2=f[x2]=18f[x2,x3]=f[x3]f[x2]x3x2=271843=93x3=4y3=f[x3]=27

The four data points lie on a polynomial of order 2, which is why the coefficient a3(f[x0,x1,x2,x3]) is zero. Given [a0,a1,a2]=[6,5,1] the result polynomial is:

y=a0+a1(xx0)+a2(xx1)(xx0)=6+5×(x1)+1×(x2)(x1)=x2+2x+3

Once the coefficients of a Newton's polynomial are solved, we can evaluate the polynomial function for any x. A Newton polynomial is often rewritten in a nested form:

f(x)=a0+(xx0)(a1+(xx1)(a2+(xx2)(a3+...+an(xxn1))...),

because this nested form of interpolating polynomial is easier to evaluate because x only appears in the function n times.

For example, the nested form of a third order interpolating polynomial is:

f(x)=a0+(xx0)(a1+(xx1)(a2+(xx2)a3))

The algorithm of Newton's method and its implementation can be found in this Jupyter notebook.

Lagrange Form

Lagrange polynomial is another form used for polynomial interpolation. It is called a form because with a given set of distinct points the interpolating polynomial is unique. We can arrive at the same polynomial through different methods.

The Lagrange form specifies the interpolation polynomial as:

fn(x):=i=0nLi(x)f(xi)
Li(x):=0mnmixxmxixm=(xx0)(xix0)(xxi1)(xixi1)(xxi+1)(xixi+1)(xxn)(xixn),

where n is the order of the polynomial.

For example, given two data points we get n=1 and

f(x)=L0f(x0)+L1f(x1)=xx1x0x1f(x0)+xx0x1x0f(x1)

Obviously the function curve passes both data points. The first derivative also matches that of the divided difference method:

f(x)=f(x0)x0x1+f(x1)x1x0=f(x1)f(x0)x1x0

Spline Interpolation

Spline interpolation uses a number of polynomial functions to interpolate a set of data points with each polynomial for two adjacent data points. The Spline method is necessary because often times when the order of the polynomial become large polynomial interpolation shows oscillatory behavior (instability known as Runge's phenomenon). The following iPython notebook shows an example that suffers this issue: [1]

Spline method is not another method for finding polynomial interpolation of a discrete function, but instead it results in a piecewise polynomial (splines) in order to avoid the oscillatory behavior. The most common spline interpolations are linear, quadratic, and cubic splines. Linear interpolation uses lines to connect each pair of consecutive data points resulting in a piecewise interpolation.

Interpolation example linear

A quadratic spline uses a quadratic polynomial to connect consecutive data points.

Quadratic spline six segments

A function f(x) is a quadratic spline if the following conditions are true:

  1. The domain of f(x) is an interval [a, b].
  2. f(x) and f(x) are continuous on [a, b].
  3. The data points xi such that a=x0<x1<<xn=b and f(x) is a polynomial of order at most 2 on each subinterval [xi,xi+1].

Template:BookCat