> Lagrange Interpolation with Maple

Lagrange Interpolation with Maple


# This Maple session shows how to implement
# Lagrange interpolation for the function 
# exp(-10x^2) (a Gaussian distribution)
# at 5 nodes (4 intervals) on the interval [-1,1]. To import
# this into a Maple session, first save
# it as a text file (click on File, then Save As,
# and type in a file name with a .txt suffix).
# Then open a Maple session and 
# click on File, and then click on Import Text.
# A dialogue window will appear and you can
# click on the file name in your  home directory
# that you gave earlier.
# As an interesting exercise, try decreasing the step size
# to 1/4 and increasing the number of nodes to 9 (8 intervals).
--------------------------------------------------------------------------------
> f:=x->exp(-10*x^2);

                                                 2
                             f := x -> exp(- 10 x )
--------------------------------------------------------------------------------
> for i from 0 to 4 do
> x[i]:=-1+i*.5
> od;

                                   x[0] := -1.

                                   x[1] := -.5

                                    x[2] := 0

                                   x[3] := .5

                                   x[4] := 1.0
--------------------------------------------------------------------------------
> for i from 0 to 4 do
> L[i]:=product((x-x[j])/(x[i]-x[j]),j=0..i-1)*product((x-x[k])/(x[i]-x[k]),k=i+1..4)
> od;

      L[0] :=  - 1. ( - 2.000000000 x - 1.000000000) x

          ( - .6666666667 x + .3333333334) ( - .5000000000 x + .5000000000)

      L[1] :=  - 2.000000000 (2.000000000 x + 2.000000000) x

          ( - 1.000000000 x + .5000000000) ( - .6666666667 x + .6666666667)

      L[2] := (1. x + 1.) (2.000000000 x + 1.000000000)

          ( - 2.000000000 x + 1.000000000) ( - 1.000000000 x + 1.000000000)

L[3] := 2.000000000 (.6666666667 x + .6666666667) (1.000000000 x + .5000000000)

    x ( - 2.000000000 x + 2.000000000)

L[4] := 1.000000000 (.5000000000 x + .5000000000) (.6666666667 x + .3333333334)

    x (2.000000000 x - 1.000000000)
--------------------------------------------------------------------------------
> lagr:=sum(f(x[p])*L[p],p=0..4);

lagr :=  - .00004539992976 ( - 2.000000000 x - 1.000000000) x

    ( - .6666666667 x + .3333333334) ( - .5000000000 x + .5000000000)

     - .1641699972 (2.000000000 x + 2.000000000) x

    ( - 1.000000000 x + .5000000000) ( - .6666666667 x + .6666666667) +

    (1. x + 1.) (2.000000000 x + 1.000000000) ( - 2.000000000 x + 1.000000000)

    ( - 1.000000000 x + 1.000000000) + .1641699972 (.6666666667 x + .6666666667)

     (1.000000000 x + .5000000000) x ( - 2.000000000 x + 2.000000000)

     + .00004539992976 (.5000000000 x + .5000000000)

    (.6666666667 x + .3333333334) x (2.000000000 x - 1.000000000)
--------------------------------------------------------------------------------
> plot({f(x),lagr},x=-1..1,y=-2..2);
--------------------------------------------------------------------------------
> i:='i';

                                     i := i
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------