1 Lagrange polynomial Polynomial interpolation Lecture 4II 數值

Lecture 4II
 Lagrange polynomial
 Polynomial interpolation
數值方法2008 Applied Mathematics,
NDHU
1
poly

Find the polynomial defined by given
zeros
poly([-5 0 5])

poly returns coefficients of a polynomial with
roots –5,0 and 5
數值方法2008 Applied Mathematics,
NDHU
2
polyval
v=linspace(-5,5);
p=poly([-5 0 5]);
y=polyval(p,v);
Polynomial evaluation
 y stores a result of substituting elements
in v to a polynomial defined by roots: 5,
0 and -5

數值方法2008 Applied Mathematics,
NDHU
3
50

50
p3(x)=(x-5)(x+5)(x-0)
40
30
20
10
40
0
30
-10
20
-20
10
-30
0
-40
-10
-50
-5
-20
0
5
-30
x=[4 1 3 -4];y=polyval(p,x);hold on;plot(x,y,'ro')
-40
-50
-5
0
5
40
30
20
10
0
Given data, find an interpolating polynomial
-10
-20
-30
-40
-50
-4
-3
數值方法2008 Applied Mathematics,
NDHU
-2
-1
0
1
2
3
4
4
Polynomial Interpolation
Paired data
 Each pair (xi,yi) specifies a point on a
target polynomial
 yi = f(xi) for all i
 Given paired data, find an interpolating
polynomial

數值方法2008 Applied Mathematics,
NDHU
5
Strategy

Use a linear combination of Lagrange
polynomials determined by all xi to
express the interpolating polynomial
數值方法2008 Applied Mathematics,
NDHU
6
Lagrange polynomial
n knots, x  [ x1 ,..., xn ]
 n Lagrange polynomials

Li ( x)  1, if x  xi
 0, if x  x j , j  i
數值方法2008 Applied Mathematics,
NDHU
7
Mathematical expression
x  x1
x  xi 1 x  xi 1
x  xn
Li ( x) 


xi  x1 xi  xi 1 xi  xi 1 xi  xn
Li ( x)  1, if x  xi
 0, otherwise
數值方法2008 Applied Mathematics,
NDHU
8
Proof
xi  x1 xi  xi 1 xi  xi 1 xi  xn
Li ( xi ) 


1
xi  x1 xi  xi 1 xi  xi 1 xi  xn
x j  x1 x j  xi 1 x j  xi 1 x j  xn
Li ( x j i ) 


0
xi  x1 xi  xi 1 xi  xi 1 xi  xn
數值方法2008 Applied Mathematics,
NDHU
9
Product form
x  x1
x  xi 1 x  xi 1
x  xn
Li ( x) 


xi  x1 xi  xi 1 xi  xi 1 xi  xn
x  xj

j 1 xi  x j
n
j i
數值方法2008 Applied Mathematics,
NDHU
10
Lagrange polynomial
Given n knots, x1 ,..., xi 1 , xi , xi 1 ,..., xn
Let Li denote the ith Lagrange polynomial
 Li is a polynomial of degree n-1
 Li has n-1 roots:
x1 ,..., xi 1 , xi 1 ,..., xn

Normalization: Li satisfies
Li(xi)=1
數值方法2008 Applied Mathematics,
NDHU
11
Implementation: n-1 roots

x is a vector that consists of n distinct
knots
xzeros=[x(1:i-1) x(i+1:n)];
pi=poly(xzeros);

pi is a polynomial whose roots are all
knots except for xi
數值方法2008 Applied Mathematics,
NDHU
12
Implementation: Normalization
c=polyval(pi,x(i));
pi=pi/c;
Normalization condition
Li ( xi )  1
數值方法2008 Applied Mathematics,
NDHU
13
The ith Lagrange polynomial
xzeros=[x(1:i-1) x(i+1:n)];
p_i=poly(xzeros);
c=polyval(p_i,x(i));
p_i=p_i/c;
STRATEGY I : use matlab functions to
determine Li
數值方法2008 Applied Mathematics,
NDHU
14
Evaluation of the ith Lagrange
polynomial
y=lagrange_poly(x_in,x_knot,i)
x_knot consists of n knots
 x_in collects input values
 Evaluate the ith Lagrange polynomial Li
defined by knots in x_knot
 y= Li(x_in)

數值方法2008 Applied Mathematics,
NDHU
15
STRATEGY II : Product form evaluation
T=length(x_in);
n=length(x_knot);
y=ones(1,T);
Li ( x )
n

j 1
j i
x  xj
for j=1:n
EXIT
xi  x j
j~=i
T
a=(x_in-x_knot(j))/(x_knot(i)-x_knot(j));
y=y.*a
數值方法2008 Applied Mathematics,
NDHU
16
Direct Implementation of the product
form
function y=lagrange_poly(x_in,x_knot,i)
T=length(x_in);
y=ones(1,T);
n=length(x_knot);
for j=1:n
if j~=i
a=(x_in-x_knot(j))/(x_knot(i)-x_knot(j));
y=y.*a;
end
end
return
數值方法2008 Applied Mathematics,
NDHU
17
n=4,i=1 (strategy II)
x_in=linspace(-2,3);
y=lagrange_poly(x_in,[-1 0 1 2],1);
plot(x_in,y);
數值方法2008 Applied Mathematics,
NDHU
18
n=4,i=1 (strategy I)
x=[-1 0 1 2];i=1;n=length(x);
xzeros=[x(1:i-1) x(i+1:n)];
p_i=poly(xzeros);
p_i=pi/polyval(p_i,x(i));
x_in=linspace(-2,3);
plot(x_in,polyval(p_i,x_in),'r');
數值方法2008 Applied Mathematics,
NDHU
19
n=4,i=2 (strategy II)
x_in=linspace(-2,3);
y=lagrange_poly(x_in,[-1 0 1 2],2);
plot(v,y);
數值方法2008 Applied Mathematics,
NDHU
20
n=4,i=2 (strategy I)
x=[-1 0 1 2];i=2;n=length(x);
xzeros=[x(1:i-1) x(i+1:n)];
p_i=poly(xzeros);
p_i=p_i/polyval(p_i,x(i));
x_in=linspace(-2,3);
plot(v,polyval(p_i,x_in),'r');
數值方法2008 Applied Mathematics,
NDHU
21
Polynomial Interpolation
Paired data
 Each pair (xi,yi) specifies a point on a
target polynomial
 yi = f(xi) for all i
 Given paired data, find an interpolating
polynomial

數值方法2008 Applied Mathematics,
NDHU
22
Input
A sample from an unknown function
y
數值方法2008 Applied Mathematics,
NDHU
x
23
Polynomial interpolation
Given ( xi , yi ), i  1,..., n
 Find a polynomial that satisfies

f ( xi )  yi
for all i
數值方法2008 Applied Mathematics,
NDHU
24
Interpolating polynomial

Interpolating polynomial is with degree n1
n
p( x )   yi Li ( x )
i 1
數值方法2008 Applied Mathematics,
NDHU
25
Verification
p ( xi )   yk Lk ( xi )
k
 yi Li ( xi )   yk Lk ( xi )
k i
 yi Li ( xi )
 yi
數值方法2008 Applied Mathematics,
NDHU
26
n
p( x )   yi Li ( x )
i 1
function y_out=int_poly(x_in,x,y)
y_out=zeros(size(x_in));
n=length(x);
for i=1:n
EXIT
y_out=y_out+lagrange_poly(x_in,x,i).*y(i);
數值方法2008 Applied Mathematics,
NDHU
27
function y_out=int_poly(x_in,x,y)
y_out=zeros(size(x_in));
n=length(x);
for i=1:n
y_out=y_out+lagrange_poly(x_in,x,i).*y(i);
end
return
數值方法2008 Applied Mathematics,
NDHU
28