How To Put A Polynomial Through Points: Ed Bueler
How To Put A Polynomial Through Points: Ed Bueler
Ed Bueler
September 2012
Ed Bueler (MATH 310 Numerical Analysis) How to put a polynomial through points September 2012 1 / 29
purpose
Ed Bueler (MATH 310 Numerical Analysis) How to put a polynomial through points September 2012 2 / 29
an example of the problem
suppose you have a function y = f (x) which goes through these points:
Ed Bueler (MATH 310 Numerical Analysis) How to put a polynomial through points September 2012 3 / 29
a picture of the problem
2
y
-1
-2
-2 -1 0 1 2 3 4 5 6
x
Ed Bueler (MATH 310 Numerical Analysis) How to put a polynomial through points September 2012 4 / 29
how to find P(x)
P(x) = c0 + c1 x + c2 x 2 + c3 x 3
MAKE SURE that you are clear on how I got these equations, and that
you can do the same thing in an example with different points or different
polynomial degree
Ed Bueler (MATH 310 Numerical Analysis) How to put a polynomial through points September 2012 5 / 29
a linear system
1 −1 (−1)2 (−1)3
c0 2
1 0 2 3
0 0 c1
=
3
1 3 32 33 c2 4
1 5 52 53 c3 0
Ed Bueler (MATH 310 Numerical Analysis) How to put a polynomial through points September 2012 6 / 29
how to easily find P(x)
the forward slash does not work because of the sizes of the matrix and the vector are not right:
>> v = b / A % NOT CORRECT for our A and b; wrong sizes
Ed Bueler (MATH 310 Numerical Analysis) How to put a polynomial through points September 2012 8 / 29
did we solve the problem?
the polynomial we found had better go through the points:
>> 3.000000 + 0.983333*(-1) - 0.066667*(-1)^2 -0.050000*(-1)^3
ans = 2
>> 3.000000 + 0.983333*(0) - 0.066667*(0)^2 -0.050000*(0)^3
ans = 3
>> 3.000000 + 0.983333*(3) - 0.066667*(3)^2 -0.050000*(3)^3
ans = 4.0000
>> 3.000000 + 0.983333*(5) - 0.066667*(5)^2 -0.050000*(5)^3
ans = -1.0000e-05
2
y
-1
-2
-2 -1 0 1 2 3 4 5 6
x
Ed Bueler (MATH 310 Numerical Analysis) How to put a polynomial through points September 2012 9 / 29
the general case
the equations which determine P(x) say that the polynomial goes
through the points:
P(xi ) = yi for i = 1, 2, . . . , n
Ed Bueler (MATH 310 Numerical Analysis) How to put a polynomial through points September 2012 10 / 29
the pattern in the matrix, for the general case
as a matrix:
x12 x1n−1
1 x1 ...
1 x2 x22 ... x2n−1
A=
.. ..
. .
1 xn xn2 ... xnn−1
and b is a column vector with entries yi : b = [y1 y2 . . . yn ]0
as before, this gives a system of n equations, Av = b
the matrix A is called a Vandermonde matrix, from about 1772
Ed Bueler (MATH 310 Numerical Analysis) How to put a polynomial through points September 2012 11 / 29
Vandermonde matrix, built-in
two comments:
◦ oops! the columns are in reversed order, compared to our choice
◦ note that only the x-coordinates are needed to build A, and not the
y -coordinates
we easily fix the column order to agree with our earlier ordering using
“fliplr”, which stands for “flip left-to-right”:
>> A = fliplr(vander([-1 0 3 5]))
A =
1 -1 1 -1
1 0 0 0
1 3 9 27
1 5 25 125
Ed Bueler (MATH 310 Numerical Analysis) How to put a polynomial through points September 2012 12 / 29
Vandermonde matrix method for polynomial interpolation
A = fliplr(vander([-1 0 3 5]));
b = [2 3 4 0]’;
v = A \ b
after the coefficents v are computed, they form P(x) this way:
thus we can plot the 4 points and the polynomial this way:
now, often we want to do a polynomial fit problem like this many times for
different data
so, is it quick? here are some facts to know about solving these systems:
◦ if there are n points then the matrix A has n rows and n columns
◦ internally in M ATLAB, the linear system A v = b is solved by Gaussian
elimination
◦ Gaussian elimination does about 23 n3 arithmetic operations (i.e. additions,
subtractions, multiplications, divisions) to solve such a linear system
so finding the coefficients of the polynomial P(x) through n points takes
about n3 operations
but then you need more operations to evaluate that polynomial, which is
what you usually do with it
Ed Bueler (MATH 310 Numerical Analysis) How to put a polynomial through points September 2012 14 / 29
“new” idea: Newton’s form
P(x) = c0 + c1 (x − x1 ) + c2 (x − x1 )(x − x2 )+
· · · + cn−1 (x − x1 )(x − x2 ) . . . (x − xn−1 )
Ed Bueler (MATH 310 Numerical Analysis) How to put a polynomial through points September 2012 15 / 29
Newton’s form example: 4 points
with the n = 4 points (−1, 2), (0, 3), (3, 4), (5, 0) we can write
P(x) = c0 + c1 (x + 1) + c2 (x + 1)(x) + c3 (x + 1)(x)(x − 3)
this polynomial must go through the four points, so:
c0 =2
c0 + c1 (0 + 1) =3
c0 + c1 (3 + 1) + c2 (3 + 1)(3) =4
c0 + c1 (5 + 1) + c2 (5 + 1)(5) + c3 (5 + 1)(5)(5 − 3) =0
note that lots of terms are zero!
the system of equations has the form
Mw=b
where M is a triangular matrix, b is the same as in the Vandermonde
form, and w has the unknown coefficients:
w = [c0 c1 c2 c3 ]0
Ed Bueler (MATH 310 Numerical Analysis) How to put a polynomial through points September 2012 16 / 29
Newton’s form example, cont.
1 1
P(x) = 2 + (x + 1) − (x + 1)(x) − (x + 1)(x)(x − 3)
6 20
MAKE SURE you can do this yourself, on a similar example
Ed Bueler (MATH 310 Numerical Analysis) How to put a polynomial through points September 2012 17 / 29
Newton’s form example, cont.2
1 1
P(x) = 2 + (x + 1) − (x + 1)(x) − (x + 1)(x)(x − 3)
6 20
an uninteresting calculation puts it in standard form:
59 1 2 1 3
P(x) = 3 + x− x − x
60 15 20
= 3 + 0.983333x − 0.066667x 2 − 0.05x 3
Ed Bueler (MATH 310 Numerical Analysis) How to put a polynomial through points September 2012 18 / 29
Newton’s form for polynomial interpolation: example code
Ed Bueler (MATH 310 Numerical Analysis) How to put a polynomial through points September 2012 19 / 29
Newton’s form shows there is a unique interpolating polynomial
the diagonal entries are all nonzero as long as the x-coordinates are distinct
we can calculate the determinant of this Newton form matrix M
because the matrix is triangular, the determinant is the product of the diagonal:
Y
det M = (xi − xj ) 6= 0
i>j
Ed Bueler (MATH 310 Numerical Analysis) How to put a polynomial through points September 2012 20 / 29
Lagrange’s idea: no systems at all!
the pattern needs attention: I. the numerator and denominator have the
same pattern, but the denominator is a constant with no variable “x”; II.
`i (x) has no “(x − xi )” factor in the numerator, nor “(xi − xi )” factor in the
denominator; III. as long as the xi are distinct, we never divide by zero
Ed Bueler (MATH 310 Numerical Analysis) How to put a polynomial through points September 2012 21 / 29
Lagrange’s idea: polynomials which “hit one point”
-1
-2
-3
-2 -1 0 1 2 3 4 5 6
MAKE SURE make sure you can find the Lagrange polynomials if I give
you the x-values of n points
Ed Bueler (MATH 310 Numerical Analysis) How to put a polynomial through points September 2012 22 / 29
Lagrange’s idea, cont.
the picture on the last page illustrates what is generally true of the
Lagrange polynomials:
(
1, j = i,
`i (xj ) =
0, otherwise.
also, the Lagrange polynomials for the 4 points are each of degree 3
so why does this help find P(x)?
recall that we have values yi which we want the polynomial P(x) to “hit”
that is, we want this to be true for each i:
P(xi ) = yi
Ed Bueler (MATH 310 Numerical Analysis) How to put a polynomial through points September 2012 23 / 29
Lagrange’s idea, cont.2
and
P(x2 ) = y1 `1 (x2 ) + y2 `2 (x2 ) + y3 `3 (x2 ) + y4 `4 (x2 )
= y1 · 0 + y2 · 1 + y3 · 0 + y4 · 0
= y2 ,
and so on
Ed Bueler (MATH 310 Numerical Analysis) How to put a polynomial through points September 2012 24 / 29
Lagrange’s idea, cont.3
on the last slide we saw that P(xi ) = yi because the polynomials `i (x)
help “pick out” the point xi in the general expression ∗ on the last slide
we can say this more clearly using summation notation:
◦ the polynomial is a sum of the Lagrange polynomials with coefficients yi :
4
X
P(x) = yi `i (x)
i=1
◦ when we plug in one of the x-coordinates of the points, we get only one
“surviving” term in the sum:
4
X X
P(xj ) = yi `i (xj ) = yj · 1 + yi · 0 = yj
i=1 i6=j
Ed Bueler (MATH 310 Numerical Analysis) How to put a polynomial through points September 2012 25 / 29
returning to our 4-point example
for our 4 concrete points (−1, 2), (0, 3), (3, 4), (5, 0), we can
slightly-simplify the Lagrange polynomials we have computed already:
1
`1 (x) = − x(x − 3)(x − 5)
24
1
`2 (x) = + (x + 1)(x − 3)(x − 5)
15
1
`3 (x) = − (x + 1)(x)(x − 5)
24
1
`4 (x) = + (x + 1)(x)(x − 3)
60
so the polynomial which goes through our points is
1 1
P(x) = −(2) x(x − 3)(x − 5) + (3) (x + 1)(x − 3)(x − 5)
24 15
1 1
− (4) (x + 1)(x)(x − 5) + (0) (x + 1)(x)(x − 3)
24 60
a tedious calculation simplifies this to
59 1 2 1 3
x− x −
P(x) = 3 + x ,
60 15 20
which is exactly what we found earlier
Ed Bueler (MATH 310 Numerical Analysis) How to put a polynomial through points September 2012 26 / 29
so, is the Lagrange scheme a good idea?
n
X
P(x) = yi `i (x)
i=1
Q P
note “ ” is a symbol for a product, just like “ ” is a symbol for sum
we solve no linear systems and we just write down the answer! yeah!
is this scheme a good idea in practice?
NOT REALLY!
Ed Bueler (MATH 310 Numerical Analysis) How to put a polynomial through points September 2012 27 / 29
so, is the Lagrange scheme a good idea? cont.
we have seen that actually using the formulas to find a familiar form for
P(x) is . . . awkward
the problem with the Lagrange form is that even when we write down the
correct linear combination of Lagrange polynomials `i (x) to give P(x), we
do not have quick ways of getting:
◦ either the coefficients ai in the standard form,
P(x̄) = ȳ
Ed Bueler (MATH 310 Numerical Analysis) How to put a polynomial through points September 2012 28 / 29
conclusion: how to do polynomial interpolation
Ed Bueler (MATH 310 Numerical Analysis) How to put a polynomial through points September 2012 29 / 29