Numerical Methods – Lecture 4

Numerical Methods – Lecture 4
Curve Fitting – Polynomial Interpolation
by Pavel Ludvík
Introduction
In this lesson we look at ways of fitting a smooth curve to
supplied data points. That is, given a set of pairs of values
(xi , yi ), we construct a continuous function y = f (x) that in some
sense represents an underlying function implied by the data
points. Having produced such an approximating function we
could for example, estimate a value for f (x) where x is not one
of the xi .
Numerical Methods – Lecture 4
by Pavel Ludvík
2/1
Introduction
In this lesson we look at ways of fitting a smooth curve to
supplied data points. That is, given a set of pairs of values
(xi , yi ), we construct a continuous function y = f (x) that in some
sense represents an underlying function implied by the data
points. Having produced such an approximating function we
could for example, estimate a value for f (x) where x is not one
of the xi .
We show two approaches to the problem.
1.
Polynomial Interpolation: We find a polynomial passing
through all the given points.
2.
Least Squares Approximation: Among the functions
of the given form we find the one with the best approximation properties with respect to given data.
Numerical Methods – Lecture 4
by Pavel Ludvík
2/1
Polynomial Interpolation
Numerical Methods – Lecture 4
by Pavel Ludvík
3/1
Weierstrass Approximation Theorem
Theorem
Suppose that f is defined and continuous on [a, b]. For each
ε > 0, there exists a polynomial P(x), with property that
|f (x) − P(x)| < ε, for all x in [a, b].
Numerical Methods – Lecture 4
by Pavel Ludvík
4/1
Main Theorem of Polynomial Interpolation
Theorem
Let (x0 , y0 ), . . . , (xn , yn ) be n pints in the plane with distinct xi .
Then there exists one and only one polynomial p of degree n
or less that satisfies P(xi ) = yi for i = 0, . . . , n.
Numerical Methods – Lecture 4
by Pavel Ludvík
5/1
Interpolating Polynomial in a Basic Form
Let (xi , yi ), i = 0, . . . , n be nodes with different xi -s. Then there
exists a unique polynomial
Pn (x) = a0 + a1 x + a2 x2 + · · · + an xn
of degree no greater then n such that
a0 + a1 xi + a2 xi2 + · · · + an xin = yi ,
This linear system

1 x0

 1 x1

 1 x2

 . .
 . .
 . .
i = 0, . . . , n.
can be rewritten in a
 
x02 . . . x0n
a0


x12 . . . x1n   a1
 

x22 . . . x2n 
 ·  a2

 .
.. . .
.

. .. 
.
  ..
1 xn xn2 . . .
xnn
matrix form as
 

y0
 

  y1 
 

  y2 
=
.
  . 
  . 
  . 
an
yn
Numerical Methods – Lecture 4
by Pavel Ludvík
6/1
Interpolating Polynomial in a Basic Form
Exercises
1.
Find the basic form of an interpolating polynomial for the
data
xi
yi
2.
i=0
i=1
i=2
i=3
0
1
2
2
3
4
5
0
Search Matlab help for the command polyfit and check
your answer of the previous exercise.
Numerical Methods – Lecture 4
by Pavel Ludvík
7/1
Lagrange Interpolating Polynomial
Suppose that we are presented with n + 1 points
(x0 , y0 ), . . . , (xn , yn ). For each k = 0, . . . , n, define the degree n
polynomial
Lk (x) =
(x − x0 ) · · · (x − xk−1 )(x − xk+1 ) · · · (x − xn )
.
(xk − x0 ) · · · (xk − xk−1 )(xk − xk+1 ) · · · (xk − xn )
The Lagrange interpolating polynomial is then
Pn = y0 L0 (x) + · · · + yn Ln (x).
Numerical Methods – Lecture 4
by Pavel Ludvík
8/1
Lagrange Interpolating Polynomial
Exercises
1.
What are the values of Li (xj ) if i = j and i 6= j?
2.
Find the Lagrange interpolating polynomial for the data in
the previous section.
3.
Use Lagrange interpolating polynomial to approximate
f (0.43) if
f (0) = 1, f (0.25) = 1.64872, f (0.5) = 2.71828, f (0.75) = 4.48169.
Numerical Methods – Lecture 4
by Pavel Ludvík
9/1
Newton’s Divided Differences
Definition
Denote by y[x0 , . . . xn ] the coefficient of the xn term in the
(unique) polynomial that interpolates (x0 , y0 ), . . . , (xn , yn ). We
call them Newton’s divided differences.
Theorem
We can compute the Newton’s divided differences as
y[xk ] = yk ,
y[xk+1 ] − y[xk ]
y[xk , xk+1 ] =
,
xk+1 − xk
y[xk+1 , xk+2 ] − y[xk , xk+1 ]
y[xk , xk+1 , xk+2 ] =
,
xk+2 − xk
y[xk+1 , xk+2 , xk+3 ] − y[xk , xk+1 , xk+2 ]
y[xk , xk+1 , xk+2 , xk+3 ] =
,
xk+3 − xk
...
Numerical Methods – Lecture 4
by Pavel Ludvík
10 / 1
Newton’s Interpolating Polynomial
Theorem
Suppose that we are presented with n + 1 points
(x0 , y0 ), . . . , (xn , yn ). Then
Pn (x) =
n
X
y[x0 , . . . , xi ](x − x0 ) · · · (x − xi−1 )
i=0
= y[x0 ] + y[x0 , x1 ](x − x0 ) + . . . + y[x0 , . . . , xn ](x − x0 ) · · · (x − xn−1 )
is a Newton’s interpolating polynomial.
Numerical Methods – Lecture 4
by Pavel Ludvík
11 / 1
Newton’s Interpolating Polynomial
Theorem
Suppose that we are presented with n + 1 points
(x0 , y0 ), . . . , (xn , yn ). Then
Pn (x) =
n
X
y[x0 , . . . , xi ](x − x0 ) · · · (x − xi−1 )
i=0
= y[x0 ] + y[x0 , x1 ](x − x0 ) + . . . + y[x0 , . . . , xn ](x − x0 ) · · · (x − xn−1 )
is a Newton’s interpolating polynomial.
Example
Find the Newton’s polynomial for the data:
xi
yi
i=0
i=1
i=2
0
2
3
1
4
5
Numerical Methods – Lecture 4
by Pavel Ludvík
11 / 1
Newton’s Interpolating Polynomial
Answer
We compute the Newton’s divided differences:
xi
0
3
4
y[xi ]
y[xi , xi+1 ]
y[xi , xi+1 , xi+2 ]
2
1
5
1
−3
13
12
4
The Newton’s interpolating polynomial is then:
p2 (x) = 2 −
1
13
1
13
(x − 0) +
(x − 0)(x − 3) = 2 − x +
x(x − 3) .
3
12
3
12
Numerical Methods – Lecture 4
by Pavel Ludvík
12 / 1
Newton’s Interpolating Polynomial
Exercises
1.
Add a point (5, −1) to the data at previous slide and find
the Newton’s interpolating polynomial again.
2.
Look at the command diff in Matlab. Find a simple way
how for a given data (x0 , y0 ) . . . (xn , yn ) obtain the Newton’s
differences y[xi , xi+1 ].
3.
Try to write a function newtodd(x,y,k) generating the
Newton’s interpolating differences of any wanted order k
(Hint: Do not use diff command).
Numerical Methods – Lecture 4
by Pavel Ludvík
13 / 1
Answer to the Exercise 3
function c=newtdd(x,y,k) %vector of differences
n=length(x);
if k>n-1, disp(’Incorrect value of k.’), break, end
for j=1:n
v(j,1)=y(j); %Fill in y column of Newton triangle
end
for i=2:k+1 %For column i,
for j=1:n+1-i %fill in column from top to bottom
v(j,i)=(v(j+1,i-1)-v(j,i-1))/(x(j+i-1)-x(j));
end
end
c=v(1:n-k,k+1) %Read the desired differences
Numerical Methods – Lecture 4
by Pavel Ludvík
14 / 1