17 Lagrange Interpolation Mathematica Program

download 17 Lagrange Interpolation Mathematica Program

of 14

description

Mathematica interpolation theory

Transcript of 17 Lagrange Interpolation Mathematica Program

  • Lesson 17

    Lagrange Interpolation

    Suppose we know the values of some function f(x) at n+ 1 distinct grid points

    a = x0, x1, x2, ..., xn = b (17.1)

    Denote the values of the function at each of these points as

    fk = f(xk), k = 0, 1, 2, ..., n (17.2)

    The problem of interpolation is to find an approximate (numerical) value for f(x)at any point x [a, b] that does not necessarily correspond to one of the grid points.

    (x1,f1)

    (x2,f2) (x4,f4)

    (x5,f5)

    (x3,f3)

    x1 x2 x3 x4 x5

    (x1,f1)

    (x2,f2) (x4,f4)

    (x5,f5) f(x)

    x1 x2 x3 x4 x5

    (x3,f3)

    Function known only at the grid points

    The unknown function f(x) goes through the grid points

    109

  • 110 LESSON 17. LAGRANGE INTERPOLATION

    The simplest method is linear interpolation: draw line segments connecting eachpair of consecutive grid points (xk, fk) and (xk+1, fk+1). For xk x xk+1 we have:

    y = fk +m(x xk) = fk + fk+1 fkxk+1 xk (x xk) (17.3)

    (x1,f1)

    (x2,f2)(x4,f4) (x5,f5)

    x1 x2 x3 x4 x5

    (x3,f3)

    In general, unless the grid points are very close, linear interpolation does not give veryaccurate result. A better approximation would be given by a polynomial. The key isto find the right polynomial, not just any polynomial that goes through the points.As it turns out, it is possible to find a polynomial that approximates the function toany desired degree of accuracy. This result is called the Weirstrass Approxima-tion Theorem. Furthermore, given any n + 1 points it is possible to find a uniquepolynomial of minimum degree that fits all the points. For example, any two pointscan be fit by a line; and three non-collinear points can be fit by a unique parabola;any four points that do not line on the same line or on the same parabola can be fitby a unique cubic; and so forth.

    Let us suppose that we have defined n+ 1 unique points

    (x0, f0), (x1, f1), . . . , (xn, fn) (17.4)

    wherex0 < x1 < < xn (17.5)

    and that we want to find the polynomial of lowest order

    P (x) = a0 + a1x+ a2x2 + + anxn (17.6)

    Math 481ACalifornia State University Northridge

    2008, B.E.ShapiroLast revised: November 16, 2011

  • LESSON 17. LAGRANGE INTERPOLATION 111

    to these points. We begin by substituting the points 17.4 into the polynomial to getn+ 1 equations in the n+ 1 unknowns a0, a1, . . . , an:

    f0 = a0 + a1x0 + a2x20 + + anxn0 (17.7)

    f1 = a0 + a1x1 + a2x21 + + anxnn (17.8)

    ...

    fn = a0 + a1xn + a2x2n + + anxnn (17.9)

    which we can write as the matrix system1 x0 x

    20 xn0

    1 x1 x21 xn1

    ......

    1 xn x2n xnn

    a0a1...an

    =f0f1...fn

    (17.10)This equation has a solution if the matrix of coefficients is non-singular. But becausethe points are distinct the lines of the matrix form a linearly independent set of vectors(proof left as an exercise). Hence the matrix is non-singular. To find the a0, a1, . . .we could use Gaussian elimination or some other method as we have discussed. Itturns out that this is not necessary because the form of the matrix allows us to writea much simpler iterative process for finding these coefficients.

    We will actually present two different methods for constructing the polynomial: theLagrange method (in this section) and the Newton method (in the next section). Be-cause of uniqueness both polynomials will be identical; however, they are constructeddifferently. The Newton method is particularly useful when one needs to calculatenumbers by hand, as was done in the 19th century. The Lagrange method, which wewill discuss first, is somewhat more intuitive. Before providing the general form, wewill illustrate the technique with linear (n=1) and quadratic (n=2) interpolation.

    For n = 1 we start with two points (x0, f0), (x1, f1) that we want to fit a line to. Ofcourse we have already done it, but this time we will construct the line in such a waythat the method can be easily extending to higher degree fits (with more points). Wedefine the functions

    L0(x) =x x1x0 x1 (17.11)

    L1(x) =x x0x1 x0 (17.12)

    and we observe that

    L0(x0) = 1 L0(x1) = 0 (17.13)

    L1(x0) = 0 L1(x1) = 1 (17.14)

    2008, B.E.ShapiroLast revised: November 16, 2011

    Math 481ACalifornia State University Northridge

  • 112 LESSON 17. LAGRANGE INTERPOLATION

    We write this more compactly as

    Li(xj) = ij =

    {1, i = j,

    0, i 6= j (17.15)

    known as the Kroeneker delta function (for the German mathematician LeopoldKroeneker, 1823-1891). Next, we define the function

    P (x) =1

    k=0

    Li(x)fi (17.16)

    = L0(x)f0 + L1(x)f1 (17.17)

    =x x1x0 x1f0 +

    x x0x1 x0f1 (17.18)

    We observe that P (xi) = fi and that P is linear in x. Hence it is the equation ofa line that goes through both points (xi, fi), i = 0, 1. A rearrangement of this givesequation 17.3.

    For n = 2 we have 3 points: (x0, f0), (x1, f1), and (x2, f2). Again, we have alreadysolved for the equation of a parabola through three points in the previous section,but we will do it this time by extending the Lagrange technique. We define the threefunctions

    L0(x) =(x x1)(x x2)

    (x0 x1)(x0 x2) (17.19)

    L1(x) =(x x0)(x x2)

    (x1 x0)(x1 x2) (17.20)

    L2(x) =(x x0)(x x1)

    (x2 x0)(x2 x1) (17.21)

    We observe that

    L0(x0) = 1 L0(x1) = 0 L0(x2) = 0 (17.22)

    L1(x0) = 0 L1(x1) = 1 L1(x2) = 0 (17.23)

    L2(x0) = 0 L2(x1) = 0 L2(x2) = 1 (17.24)

    or in general Li(xj) = ij, as before with the linear functions. Then we define thefunction

    P (x) = L0(x)f0 + L1(x)f1 + L2(x)f2 =2

    k=0

    Li(x)fi (17.25)

    We observe now that P (x) is quadratic, and that P (xi) = fi. Thus it goes throughall three points, and hence by uniqueness it is the only parabola that goes throughall three points.

    Math 481ACalifornia State University Northridge

    2008, B.E.ShapiroLast revised: November 16, 2011

  • LESSON 17. LAGRANGE INTERPOLATION 113

    In the general case it becomes more convenient to add a second index indicating theorder of the polynomials to the L functions. Thus we rename our linear functionsfrom L0 and L1 to L10 and L11, and our quadratic functions L0, L1, and L2 becomeL20, L21, and L22. The general definition is

    Lnk(x) =n

    j=0,j 6=k

    x xjxk xj (17.26)

    for k = 0, . . . , n. It is easily observed that (a) each of the Lnk has degree n; and that(b) that Lnk(xi) = ik. Hence the polynomial

    P (x) =nk=0

    Lnk(x)fk (17.27)

    is also of degree at most k and that P (xj) = fj. Thus P (x) is our interpolatingpolynomial, and we have derived the following result.

    Theorem 17.1 (Lagrange Interpolating Polynomial). Suppose that we are giventhe values of the function f(x) at n + 1 distinct points x0, . . . , xn, which we denoteby f0, . . . , fn. Then the n

    th Lagrange interpolating polynomial

    P (x) =nk=0

    Lnk(x)fk =nk=0

    fk

    nj=0,j 6=k

    x xjxk xj (17.28)

    is a polynomial of degree at most n that matches f(x) at each of the xi.

    Example 17.1. Let f(x) =

    1 + x. Construct the Lagrange polynomial of degreeat most 2 to interpolate the point f(0.45) using grid points at x0 = 0, x1 = 0.6 andx2 = 0.9.

    Solution. First we calculate the fi:

    f0 = f(x0) = f(0) =

    1 = 1 (17.29)

    f1 = f(x1) = f(0.6) =

    1.6 1.265 (17.30)f2 = f(x2) = f(0.9) =

    1.9 1.378 (17.31)

    So the Lagrange polynomial is

    P (x) = L20(x)f0 + L21(x)f1 + L22(x)f2 (17.32)

    = L20(x) + 1.27L21(x) + 1.38L22(x) (17.33)

    2008, B.E.ShapiroLast revised: November 16, 2011

    Math 481ACalifornia State University Northridge

  • 114 LESSON 17. LAGRANGE INTERPOLATION

    Where

    L20(x) =(x 0.6)(x 0.9)(0 0.6)(0 0.9) = 1.85(x 0.6)(x 0.9) (17.34)

    L21(x) =(x 0)(x 0.9)

    (0.6 0)(0.6 0.9) = 5.56x(x 0.9) (17.35)

    L22(x) =(x 0)(x 0.6)

    (0.9 0)(0.9 0.6) = 3.70x(x 0.6) (17.36)

    Thus

    P (x) = L20(x) + 1.27L21(x) + 1.38L22(x) (17.37)

    = 1.85(x 0.6)(x 0.9) 1.27(5.56)x(x 0.9)+ 1.38(3.70)x(x 0.6) (17.38)

    = 1.85(x 0.6)(x 0.9) 7.03x(x 0.9) + 5.11x(x 0.6) (17.39)= 0.999 + 0.486x 0.07x2 (17.40)

    Hence

    P (0.45) 0.999 + (0.486)(0.45) 0.07)(0.45)2 = 1.20 (17.41)

    We summarize the algorithm for Lagrange Interpolation here.1

    Algorithm LagrangeInterpolatingFunctionsInput: x0, . . . , xn, xFor i = 0, 1, . . . , n,

    Define the set Ui={x0, . . . , xn} {xi}Define numerator = 1, denominator = 1For j = 0, . . . , n 1

    numerator = numerator (x Uij)denominator = denominator (xi Uij)

    End ForLni = numerator/denominator

    End ForReturn the list {Ln1, . . . , Lnn}

    Algorithm LagrangeInterpolatingPolynomialInput: x0, . . . , xn, f0, . . . , fn, xLet L be the list LagrangeInterpolatingFunctions(x0, . . . , xn, x)

    1The notation A B, where A and B are sets, means the relative complement of the set B inthe set A, e.g., all of the elements of A that are not in B. For an ordered set Ui, the notation Uijmeans the jth element of Ui. An ordered set is also called a List.

    Math 481ACalifornia State University Northridge

    2008, B.E.ShapiroLast revised: November 16, 2011

  • LESSON 17. LAGRANGE INTERPOLATION 115

    P = f0 L0 + f1 L1 + + fn LnReturn P

    We now illustrate how to calculate the Lagrange Interpolating Polynomials both an-alytically and numerically in Mathematica. First we make a few observations. Therelative complement of the set B in A is given by Complement[A, B]., e.g,

    In:=

    U={x1, x2, x3, x4, x5}

    Complement[U, {x4}]

    Out:=

    {x1, x2, x3, x5}

    Next, we observe that if U is a list such as the one defined above, then Map[f, U]returns the result of f[u], for every element u of U. Recall that f/@U is a shorthandfor Map[f, U],

    In:=

    f/@U

    Out:=

    {f[x1], f[x2], f[x3], f[x4], f[x5]}

    Suppose that f[x] represents the function f(x) = x 3. We can calculate somevalue, say f(u) in two different ways. The first is the usual way,

    In:=

    f[x_]:=x-3;

    f[u]

    Out:=

    u-3

    2008, B.E.ShapiroLast revised: November 16, 2011

    Math 481ACalifornia State University Northridge

  • 116 LESSON 17. LAGRANGE INTERPOLATION

    The second way is to use pure functions:

    In:=

    (#-3)&[u]

    Out:=

    u-3

    Pure functions allow us to define a function and use it in a single statement. In-stead of saying f[x] we replace the f with the pure function (#-3)&. The symbol &tells us where the function definition ends, and the symbol # is used in place of thefunctions argument x. We can also combine pure functions with the Map function.This is convenient because it lets us map an expression that we are only going to useonce; otherwise wed have to use an extra line of code to define an unnecessary extravariable to hold the function. Thus

    In:=

    f[x_]:= x-3; V=Map[f, U]

    and

    In:=

    V=(#-3)&/@U

    both return the identical output

    Out:=

    {-3 + x1, -3 + x2, -3 + x3, -3 + x4, -3 + x5}

    Next, we observe that the generalization of multiplication in Mathematicais the Timesfunction. Times can take an arbitrary number of arguments and returns their prod-uct. Thus

    In:=

    Times[-3+x1, -3+x2, -3+x3, -3+x4, -3+x5]

    Math 481ACalifornia State University Northridge

    2008, B.E.ShapiroLast revised: November 16, 2011

  • LESSON 17. LAGRANGE INTERPOLATION 117

    Out:=

    (-3 + x1) (-3 + x2) (-3 + x3) (-3 + x4) (-3 + x5)

    To multiply out the elements of V we need to take all the elements of V and place themas arguments to Times. We do this with the Apply command, which has a shorthandof @@. The following are:

    In:=

    Apply[Times, V]

    In:=

    Times@@V

    and both return the same thing (recall the definition of V, above):

    Out:=

    (-3 + x1) (-3 + x2) (-3 + x3) (-3 + x4) (-3 + x5)

    Now suppose we want to combine these two functions. We want to subtract 3 fromevery element of the list U, which we can do with Map, and then take the product ofthe results with Apply and Times:

    In:=

    Times@@(#-3)&/@U

    or In:=

    Apply[Times, Map[(# - 3)&, U]]

    both of which return

    Out:=

    (-3 + x1) (-3 + x2) (-3 + x3) (-3 + x4) (-3 + x5)

    With this we can define a function to calculate the Lagrange Interpolating Functionsin Mathematica.

    2008, B.E.ShapiroLast revised: November 16, 2011

    Math 481ACalifornia State University Northridge

  • 118 LESSON 17. LAGRANGE INTERPOLATION

    LagrangeInterpolatingFunctions[{xj__}, x_] :=

    Module[ {i, n, xi, xjc, L, xgrid, num, den},

    xgrid = {xj};

    n = Length[xgrid];

    L = {};

    For[i = 1, i

  • LESSON 17. LAGRANGE INTERPOLATION 119

    Out:=

    {0.125, 1.125, -0.25}

    Next we observe that the dot product of two lists A and B of the same length iscalculated with the dot operator, which is a period:

    In:=

    {f1, f2, f3, f4}.{L1, L2, L3, L4}

    Out:=

    f1 L1 + f2 L2 + f3 L3 + f4 L4

    So we can repeat our previous example as follows:

    In:=

    f[x_]:= Sqrt[1.0+x];

    points = {0.0, 0.6, 0.9};

    (f/@points).LagrangeInterpolatingFunctions[points, 0.45]

    Out:=

    1.20342

    We can get the Lagrange Interpolating Polynomial withIn:=

    (f /@ points).LagrangeInterpolatingFunctions[points, x] // Expand

    Out:=

    1.+ 0.483656 x - 0.0702286 x^2

    Theorem 17.2 (Error Bounds for Lagrange Interpolation). Suppose that f(x)is n+ 1 times continuously differentiable, and suppose that the points x0, . . . , xn aredistinct. Then for any x [a, b] there exists a number c [a, b] such that

    f(x) = P (x) +fn+1(c)(x x0)(x x1) (x xn)

    (n+ 1)!(17.42)

    2008, B.E.ShapiroLast revised: November 16, 2011

    Math 481ACalifornia State University Northridge

  • 120 LESSON 17. LAGRANGE INTERPOLATION

    where

    P (x) =nk=0

    fkLnk(x) =nk=0

    fk

    nj=0,j 6=k

    x xjxk xj (17.43)

    Proof. If x = xk and P (xk) = fk for some k, then the second term in equation 17.42is zero, regardless of the value of c, and the result holds identically.

    So suppose that x 6= xk for all k, and define the function

    g(t) = f(t) P (t) [f(x) P (x)]ni=0

    t xix xi (17.44)

    Then

    g(xk) = f(xk) P (xk) [f(x) P (x)]ni=0

    xk xix xi = 0 (17.45)

    The second equality follows because (a) by construction, f(xk) = P (xk), so the firstterm is zero; and (b) for some i we have i = k and hence there is a factor of xk xkin the numerator of the second term, making it zero as well. Furthermore,

    g(x) = f(x) P (x) [f(x) P (x)]ni=0

    x xix xi (17.46)

    = f(x) P (x) [f(x) P (x)] (17.47)= 0 (17.48)

    Hence g(t) = 0 as the n + 2 numbers x, x0, . . . , xn. Since it is also continuouslydifferentiable n+1 times, then by the generalized Rolles theorem, theorem 4.4, thereexists at least one number c (a, b) such that g(n+1)(c) = 0. Differentiating g(t) atotal of n+ 1 times,

    g(n+1)(t) = f (n+1)(t) P (n+1)(t) [f(x) P (x)] d(n+1)

    dt(n+1)

    ni=0

    t xix xi (17.49)

    hence at t = c we have

    0 = g(n+1)(c) (17.50)

    = f (n+1)(c) P (n+1)(c) [f(x) P (x)] d(n+1)

    dt(n+1)

    ni=0

    t xix xi

    t=c

    (17.51)

    = f (n+1)(c) P (n+1)(c) [f(x) P (x)]ni=0(x xi)

    d(n+1)

    dt(n+1)

    ni=0

    (t xi)t=c

    (17.52)

    Now sinceP (t) = a0 + a1t+ + antn (17.53)

    Math 481ACalifornia State University Northridge

    2008, B.E.ShapiroLast revised: November 16, 2011

  • LESSON 17. LAGRANGE INTERPOLATION 121

    then P (n+1)(t) = 0 for all t, and hence P (n+1)(c) = 0, so that

    0 = f (n+1)(c) [f(x) P (x)]ni=0(x xi)

    d(n+1)

    dt(n+1)

    ni=0

    (t xi)t=c

    (17.54)

    Furthermore,

    d(n+1)

    dt(n+1)

    ni=0

    (t xi) = d(n+1)

    dt(n+1)(t x1)(t x2)(t x3) (t xn) (17.55)

    =d(n+1)

    dt(n+1)[tn + (stuff) tn1 + (more stuff) tn2 + ]

    (17.56)

    = (n+ 1)! (17.57)

    Substituting equation 17.57 into equation 17.58 gives

    0 = f (n+1)(c) [f(x) P (x)]ni=0(x xi)

    (n+ 1)! (17.58)

    Solving for f(x) gives us equation 17.42, completing the proof.

    Example 17.2. Suppose you want to make a table of the natural logarithms overthe range 1 x 100. What step size is sufficient to ensure that linear interpolationbetween each successive pair of points will be accurate to within 105?

    Solution. From equation 17.42 we have

    |f(x) P (x)| =fn+1(c)(x x0)(x x1) (x xn)(n+ 1)!

    (17.59)For linear interpolation we use n = 1 (there are two points, x0 and x1, so that

    |f(x) P (x)| =f (2)(c)(x x0)(x x1)2!

    (17.60) 1

    2max |f (c)| max |(x x0)(x x1)| (17.61)

    on each interval. Since f(x) = log x we have f (x) = 1/x and f (x) = 1/x2. Themaximum value of | 1/x2| on [1, 100] is 1, so that

    |f(x) P (x)| 12

    max |(x x0)(x x1)| (17.62)

    To find the maximum value of g(x) = (x x0)(x x1) = x2 (x0 + x1)x + x0x1on [x0, x1] we observe that the maximum either occurs at an endpoint or at a pointwhere g(x) = 0. At the endpoints g(x) = 0. So first we differentiate:

    0 = g(x) = 2x (x0 + x1) (17.63)

    2008, B.E.ShapiroLast revised: November 16, 2011

    Math 481ACalifornia State University Northridge

  • 122 LESSON 17. LAGRANGE INTERPOLATION

    which gives a possible maximum at x = (x0 + x1)/2. The value of g at this point isg(x0 + x12) = (x0 + x12 x0

    )(x0 + x1

    2 x1

    ) (17.64)=

    (x1 x02)(

    x0 x12

    ) (17.65)=h2

    4(17.66)

    where h is the size between entries in the table (the number we are solving for).Substituting equation 17.66 into example 17.62 gives

    |f(x) P (x)| h2

    8(17.67)

    Since we want to ensure that the error is no larger than 105 we set

    h2

    8< 105 (17.68)

    orh