You are on page 1of 18

# ECM6 Computational Methods :

Slide 1 of 10

Lecture 14a
Lagrange Interpolation
Brian G. Higgins
Department of Chemical Engineering & Materials Science
University of California, Davis
April 2014, Hanoi, Vietnam

ECM6Lecture14aVietnam_2014.nb

Concept of Interpolation
In a previous lecture we were given a set of data points

88x0 , y0 <, 8x1 , y1 <, 8x2 , y2 <, 8x3 , y3 <, , 8xM , yM <<
and our goal was to find a polynomial that was a best fit of the data in the least squares sense. That
is, we selected the coefficients ai of the polynomial

P HxL = a0 + a1 x + a2 x2 + a3 x3 + + an xn
such that square of the error
M

E2 HPL = HP Hxi L - yi L2
i=1

was minimized. This was called linear regression. An alternative approach is to find a polynomial PHxL
such that

P Hxi L = yi , i = 0, 1, 2, , M
Thus we want to find the polynomial that passes through each data point.
In other words we require that the error at each xi be zero. This is the concept of polynomial interpolation.
We will see later in this lecture that interpolation is the corner stone for constructing algorithms for
implementing numerical integration.

ECM6Lecture14aVietnam_2014.nb

## Linear Interpolation Formula

Suppose we are given a set of 2 data points:

## 88x0 , y0 <, 8x1 , y1 <<

We would like to find a linear polynomial

P1 HxL = a0 + a1 x
such that

P1 Hx0 L = a0 + a1 x0 = y0
P1 Hx1 L = a0 + a1 x1 = y1
Thus we have two equations for the two unknowns a0 and a1 . Solving this system of linear equations
gives

a1 =

y1 - y0
x1 - x0

a0 =

x1 y0 - x0 y1
x1 - x0

P1 HxL =

x1 y0 - x0 y1
x1 - x0

y1 - y0
x1 - x0

## We can rearrange this formula to give

P1 HxL =

Hx - x1 L
Hx0 - x1 L

y0 +

Hx - x0 L
Hx1 - x0 L

x0 x x1

y1 ,

This formula is a linear interpolation of y between x0 and x1 . Linear interpolation is widely used to
interpolate thermodynamic tables, as illustrated in the following example.

Example 1
Thermodynamic steam tables give the enthalpy of steam at 20 MPa as a function of temperature

T (C)
70
80

h (kJ/kg)
309.26
350.78

We will use linear interpolation to determine the enthalpy h at temperatures between 70 and 80 C .
Substituting the above data points into Eq. (10) gives

h HTL =

HT - 80L
H70 - 80L

309.26 +

HT - 70L
H80 - 70L

350.78

## = -HT - 80L 30.926 + HT - 70L 35.078

= 18.62+ 4.152 T
If we have more that two data points then it is possible to determine a higher order polynomial. In short
if we have n+1 data points we can find n+1 interpolating conditions given by Eq. (4) to determine n+1
polynomial conditions. We discuss this next

ECM6Lecture14aVietnam_2014.nb

If we have more that two data points then it is possible to determine a higher order polynomial. In short
if we have n+1 data points we can find n+1 interpolating conditions given by Eq. (4) to determine n+1
polynomial conditions. We discuss this next

ECM6Lecture14aVietnam_2014.nb

## Lagrange Interpolation Formula Part 1

Suppose we are given n+1 data points of the form

88x0 , y0 <, 8x1 , y1 <, 8x2 , y2 <, 8x3 , y3 <, , 8xn , yn <<
and we want to determine a polynomial of degree n or less which passes through those points. We
begin our discussion by considering following polynomial
P HxL = c0 Hx - x1 L Hx - x2 L Hx - x3 L Hx - xn L
+ c1 Hx - x0 L Hx - x2 L Hx - x3 L Hx - xn L
+ c2 Hx - x0 L Hx - x1 L Hx - x3 L Hx - xn L
+
+ cn Hx - x0 L Hx - x1 L Hx - x2 L Hx - xn-1 L
Note that each term in the above expression is a polynomial of degree n. Thus the degree of P(x) is
also n.

## Finding the Coefficients ci

Next we want to determine the values of ci such that the polynomial passes through all the yi in our
data set.
If we set x = x0 it follows from the construction of our polynomial that
P Hx0 L = c0 Hx0 - x1 L Hx0 - x2 L Hx0 - x3 L Hx0 - xn L = y0
Solving for c0 gives

c0 =

y0
Hx0 - x1 L Hx0 - x2 L Hx0 - x3 L Hx0 - xn L

## By inspection we can deduce the remaining coefficients

y1
c1 =
Hx1 - x0 L Hx1 - x2 L Hx1 - x3 L Hx1 - xn L
c2 =

y2
Hx2 - x0 L Hx2 - x1 L Hx2 - x3 L Hx2 - xn L

## ci = yi HHxi - x0 L Hxi - x1 L Hxi - x2 L Hxi - xi-1 L Hxi - xi+1 L Hxi - xn LL

cn =

yn
Hxn - x0 L Hxn - x1 L Hxn - x2 L Hxn - xn-1 L

ECM6Lecture14aVietnam_2014.nb

## Lagrange Interpolation Formula Part 2

Substituting the above coefficients (see previous slide) into the polynomial we get
N

P HxL = Pj HxL
j=0

## where the P j HxL is of the form

N

Pj HxL = yj
k=0

x - xk
xj - xk

kj

Note we have color coded the indices to emphasize how the product sum is formed. If we write out the
interpolating polynomial explicitly we get
Hx - x1 L Hx - x2 L Hx - x3 L Hx - xn L

P HxL =

## Hx0 - x1 L Hx0 - x2 L Hx0 - x3 L Hx0 - xn L

y0

Hx - x0 L Hx - x2 L Hx - x3 L Hx - xn L

## Hx1 - x0 L Hx1 - x2 L Hx1 - x3 L Hx0 - xn L

y1

+
+

Hx - x0 L Hx - x1 L Hx - x2 L Hx - xn-1 L
Hxn - x0 L Hxn - x1 L Hxn - x2 L Hxn - xn-1 L

yn

Thus if you select one of the interpolating points x j , then all P j HxL terms vanish except the Pi HxL for the
given point of interest. It is sometimes more convenient to express the functions as

Pj HxL = yj fj ,
N

where

fj =
k=0

x - xk
xj - xk

kj

so that
N

f0 =
k=0

x - xk
x0 - xk

x - x1

x - x2

x - x3

x0 - x1 x0 - x2 x0 - x3

x - xN
x0 - xN

k0

f1 =
k=0

x - xk
x0 - xk

x - x0

x - x2

x - x3

x1 - x0 x1 - x2 x1 - x3

x - xN
x1 - xN

k1

etc
The set of f j 's can be thought of as set of basis functions, and y j the interpolation points. Note further
that since kj, the denominator in the product sum can never be zero.

ECM6Lecture14aVietnam_2014.nb

The set of f j 's can be thought of as set of basis functions, and y j the interpolation points. Note further
that since kj, the denominator in the product sum can never be zero.

ECM6Lecture14aVietnam_2014.nb

## Example Using Lagrange Interpolation Formula

Example 1
Determine the interpolation polynomial for the points

{{0,-3},{1,0},{3,30},{5,132}}
Substituting these values into our interpolating polynomial (19) gives

P HxL =

## Hx-1L Hx-3L Hx-5L

H0-1L H0-3L H0-5L

H-3L

Hx-3L Hx-5L
+ Hx-0L
H0L
H1-0L H1-3L H1-5L

## Hx-0L Hx-1L Hx-5L

H3-0L H3-1L H3-5L

H30L

## Hx-0L Hx-1L Hx-3L

H5-0L H5-1L H5-3L

H132L

Note that since y1 = 0 , the second term in the expression vanishes. Expanding the above polynomial
gives

P HxL = x2 + 2 x - 3
Here is a plot of the data and the polynomial

ECM6Lecture14aVietnam_2014.nb

## data = 880, - 3<, 81, 0<, 83, 30<, 85, 132<<;

P@x_D := x3 + 2 x - 3
plt1 = ListPlot@data, PlotStyle 8Red, PointSize@LargeD<,
Frame True, FrameLabel 8Style@"x", 16D, Style@"PHxL", 16D<D;
plt2 = Plot@P@xD, 8x, 0, 5<, PlotStyle 8Blue, Thick<,
Frame True, FrameLabel 8"x", "PHxL"<D;
Show@plt1, plt2, PlotRange AllD
120
100

PHxL

80
60
40
20
0
0

10

ECM6Lecture14aVietnam_2014.nb

## Mathematica Implementation of Lagrange Polynomials

Background
The Mathematica function InterpolatingPolynomial generates the interpolating polynomial we have
discussed above. Thus suppose we want to have a polynomial pass through the points
{{0,-3},{1,0},{3,30},{5,132}}

## Then the interpolating polynomial is

P3 = InterpolatingPolynomial@880, - 3<, 81, 0<, 83, 30<, 85, 132<<, xD
- 3 + x H3 + H- 1 + xL H1 + xLL

## If we simplify this expression we get the earlier result

Simplify@P3D
- 3 + 2 x + x3

Example 1
In this example we are given the following data points
{{1,4},{2,8},{3,4},{5,-2}}

And we want to find the interpolating polynomial that passes through the points
P3 = InterpolatingPolynomial@881, 4<, 82, 8<, 83, 4<, 85, - 2<<, xD
4 + 4 + -4 +

13
12

H- 3 + xL H- 2 + xL H- 1 + xL

## Simplifying this expression gives

P3 Simplify
4+

1
12

H- 1 + xL I222 - 113 x + 13 x2 M

## Here is a plot of the data and the interpolating polynomial

ECM6Lecture14aVietnam_2014.nb

## data = 881, 4<, 82, 8<, 83, 4<, 85, - 2<<;

plt1 = ListPlot@data, PlotStyle 8Red, PointSize@LargeD<,
Frame True, FrameLabel 8Style@"x", 16D, Style@"PHxL", 16D<D;
plt2 = Plot@P3, 8x, 0, 6<, PlotStyle 8Thick, Blue<,
Frame True, FrameLabel 8"x", "PHxL"<D;
Show@plt1, plt2, PlotRange AllD

PHxL

-5

-10

## Interpolation in Terms of Basis functions

For the above example we can identify the basis functions fi as

f0 HxL =

f1 HxL =

f2 HxL =

f3 HxL =

Hx - 2L Hx - 3L Hx - 5L
H1 - 2L H1 - 3L H1 - 5L
Hx - 1L Hx - 3L Hx - 5L
H2 - 1L H2 - 3L H2 - 5L
Hx - 1L Hx - 2L Hx - 5L
H3 - 1L H3 - 2L H3 - 5L
Hx - 1L Hx - 2L Hx - 3L
H5 - 1L H5 - 2L H5 - 3L

## Let us plot this polynomials. We define the following basis functions

f0 =
f1 =
f2 =
f3 =

Hx - 2L Hx - 3L Hx - 5L
H1 - 2L H1 - 3L H1 - 5L
Hx - 1L Hx - 3L Hx - 5L
H2 - 1L H2 - 3L H2 - 5L
Hx - 1L Hx - 2L Hx - 5L
H3 - 1L H3 - 2L H3 - 5L
Hx - 1L Hx - 2L Hx - 3L
H5 - 1L H5 - 2L H5 - 3L

;
;
;
;

11

ECM6Lecture14aVietnam_2014.nb

## This gives the same result

data = 881, 4<, 82, 8<, 83, 4<, 85, - 2<<;
plt1 = ListPlot@data, PlotStyle 8Red, PointSize@LargeD<,
Frame True, FrameLabel 8Style@"x", 16D, Style@"PHxL", 16D<D;
plt2 = Plot@4 f0 + 8 f1 + 4 f2 - 2 f3 , 8x, 0, 6<, PlotStyle 8Thick, Blue<,
Frame True, FrameLabel 8"x", "PHxL"<D;
Show@plt1, plt2, PlotRange AllD

PHxL

-5

-10

x
Now if we expand a function in terms of these polynomials in terms of a basis set of nth order, then we
must have n+1 terms
n

y HxL = yj fj HxL
j =0

where y j is the value of the function at the interpolation points. Here is plot of the individual basis
functions
Plot@8f0 , f1 , f2 , f3 <, 8x, 0, 6<,
PlotStyle 88Thick, Blue<, 8Red, Thick<, 8Magenta, Thick<, 8Thick, Orange<<,
Frame True, FrameLabel 8Style@"x", 16D, Style@"fi ", 16D<D
3

fi

12

-1

-2
0

x
Recall that the data used to generate these basis functions had values xo = 1, x1 = 2, x2 = 3, x3 = 5. This
plot illustrates that the basis function satisfy
fi Ixj M = 0, i j

ECM6Lecture14aVietnam_2014.nb

13

fi Ixj M = 0, i j
fi Ixj M = 1, i = j

## Continuity of Mathematicas Interpolation Function

Mathematicas Interpolation function generates a piecewise function through the data points. Consider
the following example
dataSin = Table@8x, Sin@3 xD<, 8x, 0, 2, 0.2<D;
myfunc = Interpolation@dataSinD
Plot@myfunc@xD, 8x, 0, 2<, Epilog 8Red, PointSize@0.02D, Map@Point@D &, dataSinD<D
InterpolatingFunction@880., 2.<<, <>D
1.0

0.5

0.5

1.0

1.5

2.0

-0.5

-1.0

Although the interpolating function is continuous at the data points, the derivatives are not continuous
Plot@myfunc '@xD, 8x, 0, 1<, PlotStyle Thick, Frame TrueD
3

-1

-2

-3
0.0

0.2

0.4

0.6

0.8

1.0

14

ECM6Lecture14aVietnam_2014.nb

## Plot@myfunc ''@xD, 8x, 0, 1<, PlotStyle Thick, Frame TrueD

-2
-3
-4
-5
-6
-7
-8
0.0

0.2

0.4

0.6

0.8

1.0

ECM6Lecture14aVietnam_2014.nb

## Application of Interpolating Polynomials

Numerical Integration
Suppose we want to evaluate the integral
b

f HxL x
a

An interpolating polynomial of the function f(x) that goes through the points a and b is
f HxL = f HaL f1 HxL + f HbL f2 HxL
where the interpolating polynomials are
f1 HxL =

Hx - bL
Ha - bL

, f2 HxL =

Hx - aL
Hb - aL

Since we have two interpolating points, the interpolating polynomial is linear. Hence our integral then
becomes
b

a

b

f HxL x :f HaL

Hx - bL2 - b2

1
2

2 Ha - bL

+ f HbL

Hx - aL2 - a2
2 Hb - aL

## 8f HbL Hb - aL - f HaL Ha - bL<

Hb - aL
2

8f HaL + f HbL<

Graphical Representation
This is called the trapezoidal rule. Here is a graphical interpretation

>

b
a

15

16

ECM6Lecture14aVietnam_2014.nb

## The area below the curve is:

Area = Hb - aL f HbL +

1
2

Hb - aL Hf HaL - f HbLL
f HaL

= Hb - aL f HbL + Hb - aL

= Hb - aL :f HbL -

Hb - aL
2

f HbL
2

8f a)+f(b)}

2
+

f HaL
2

>

f HbL
2

ECM6Lecture14aVietnam_2014.nb

## Numerical Integration part 2:

Improving the Accuracy
Example 1
Example 2

17

18

ECM6Lecture14aVietnam_2014.nb

References
The following textbooks were useful in preparing these notes:
B. Bradie, A Friendly Introduction to Numerical Analysis, Pearson Prentice Hall, 2006
A. J. Pettofrezzo, Introductory Numerical Analysis, Dover Publications, 1984
P. Wellin, R. Gaylord & S. Kamin, An Introduction to Programming with Mathematica, Cambridge
University Press, 2005