數值方法 2008 Applied Mathematics, NDHU1 Lagrange polynomial Polynomial interpolation Lecture 4II
數值方法 2008 Applied Mathematics, NDHU2 poly Find the polynomial defined by given zeros poly returns coefficients of a polynomial with roots –5,0 and 5 poly([-5 0 5])
數值方法 2008 Applied Mathematics, NDHU3 polyval Polynomial evaluation y stores a result of substituting elements in v to a polynomial defined by roots: 5, 0 and -5 v=linspace(-5,5); p= poly([-5 0 5]); y=polyval(p,v);
p 3 (x)=(x-5)(x+5)(x-0) 數值方法 2008 Applied Mathematics, NDHU4 x=[ ];y=polyval(p,x);hold on;plot(x,y,'ro') Given data, find an interpolating polynomial
Polynomial Interpolation Paired data Each pair (x i,y i ) specifies a point on a target polynomial y i = f(x i ) for all i Given paired data, find an interpolating polynomial 數值方法 2008 Applied Mathematics, NDHU5
Strategy Use a linear combination of Lagrange polynomials determined by all x i to express the interpolating polynomial 數值方法 2008 Applied Mathematics, NDHU6
7 Lagrange polynomial n knots, n Lagrange polynomials
數值方法 2008 Applied Mathematics, NDHU8 Mathematical expression
數值方法 2008 Applied Mathematics, NDHU9 Proof
數值方法 2008 Applied Mathematics, NDHU10 Product form
數值方法 2008 Applied Mathematics, NDHU11 Lagrange polynomial Given n knots, Let L i denote the ith Lagrange polynomial L i is a polynomial of degree n-1 L i has n-1 roots: Normalization: L i satisfies L i (x i )=1
數值方法 2008 Applied Mathematics, NDHU12 Implementation: n-1 roots x is a vector that consists of n distinct knots pi is a polynomial whose roots are all knots except for x i xzeros=[x(1:i-1) x(i+1:n)]; pi=poly(xzeros);
數值方法 2008 Applied Mathematics, NDHU13 Implementation: Normalization c=polyval(pi,x(i)); pi=pi/c; Normalization condition
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 L i 數值方法 2008 Applied Mathematics, NDHU14
x_knot consists of n knots x_in collects input values Evaluate the ith Lagrange polynomial L i defined by knots in x_knot y= L i (x_in) y=lagrange_poly(x_in,x_knot,i) 數值方法 2008 Applied Mathematics, NDHU15 Evaluation of the ith Lagrange polynomial
數值方法 2008 Applied Mathematics, NDHU16 STRATEGY II : Product form evaluation T=length(x_in); n=length(x_knot); y=ones(1,T); for j=1:n j~=i a=(x_in-x_knot(j))/(x_knot(i)-x_knot(j)); y=y.*a T EXIT
數值方法 2008 Applied Mathematics, NDHU17 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 return Direct Implementation of the product form
x_in=linspace(-2,3); y=lagrange_poly(x_in,[ ],1); plot(x_in,y); 數值方法 2008 Applied Mathematics, NDHU18 n=4,i=1 (strategy II)
數值方法 2008 Applied Mathematics, NDHU19 n=4,i=1 (strategy I) x=[ ];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');
x_in=linspace(-2,3); y=lagrange_poly(x_in,[ ],2); plot(v,y); 數值方法 2008 Applied Mathematics, NDHU20 n=4,i=2 (strategy II)
數值方法 2008 Applied Mathematics, NDHU21 n=4,i=2 (strategy I) x=[ ];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');
Polynomial Interpolation Paired data Each pair (x i,y i ) specifies a point on a target polynomial y i = f(x i ) for all i Given paired data, find an interpolating polynomial 數值方法 2008 Applied Mathematics, NDHU22
數值方法 2008 Applied Mathematics, NDHU23 Input x y A sample from an unknown function
數值方法 2008 Applied Mathematics, NDHU24 Polynomial interpolation Given Find a polynomial that satisfies for all i
Interpolating polynomial is with degree n- 1 數值方法 2008 Applied Mathematics, NDHU25 Interpolating polynomial
數值方法 2008 Applied Mathematics, NDHU26 Verification
數值方法 2008 Applied Mathematics, NDHU27 function y_out=int_poly(x_in,x,y) for i=1:n y_out=zeros(size(x_in)); n=length(x); y_out=y_out+lagrange_poly(x_in,x,i).*y(i); EXIT
數值方法 2008 Applied Mathematics, NDHU28 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