Presentation is loading. Please wait.

Presentation is loading. Please wait.

數值方法 2008 Applied Mathematics, NDHU1  Lagrange polynomial  Polynomial interpolation Lecture 4II.

Similar presentations


Presentation on theme: "數值方法 2008 Applied Mathematics, NDHU1  Lagrange polynomial  Polynomial interpolation Lecture 4II."— Presentation transcript:

1 數值方法 2008 Applied Mathematics, NDHU1  Lagrange polynomial  Polynomial interpolation Lecture 4II

2 數值方法 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])

3 數值方法 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);

4 p 3 (x)=(x-5)(x+5)(x-0) 數值方法 2008 Applied Mathematics, NDHU4 x=[4 1 3 -4];y=polyval(p,x);hold on;plot(x,y,'ro') Given data, find an interpolating polynomial

5 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

6 Strategy Use a linear combination of Lagrange polynomials determined by all x i to express the interpolating polynomial 數值方法 2008 Applied Mathematics, NDHU6

7 7 Lagrange polynomial n knots, n Lagrange polynomials

8 數值方法 2008 Applied Mathematics, NDHU8 Mathematical expression

9 數值方法 2008 Applied Mathematics, NDHU9 Proof

10 數值方法 2008 Applied Mathematics, NDHU10 Product form

11 數值方法 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

12 數值方法 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);

13 數值方法 2008 Applied Mathematics, NDHU13 Implementation: Normalization c=polyval(pi,x(i)); pi=pi/c; Normalization condition

14 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

15 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

16 數值方法 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

17 數值方法 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

18 x_in=linspace(-2,3); y=lagrange_poly(x_in,[-1 0 1 2],1); plot(x_in,y); 數值方法 2008 Applied Mathematics, NDHU18 n=4,i=1 (strategy II)

19 數值方法 2008 Applied Mathematics, NDHU19 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');

20 x_in=linspace(-2,3); y=lagrange_poly(x_in,[-1 0 1 2],2); plot(v,y); 數值方法 2008 Applied Mathematics, NDHU20 n=4,i=2 (strategy II)

21 數值方法 2008 Applied Mathematics, NDHU21 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');

22 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

23 數值方法 2008 Applied Mathematics, NDHU23 Input x y A sample from an unknown function

24 數值方法 2008 Applied Mathematics, NDHU24 Polynomial interpolation Given Find a polynomial that satisfies for all i

25 Interpolating polynomial is with degree n- 1 數值方法 2008 Applied Mathematics, NDHU25 Interpolating polynomial

26 數值方法 2008 Applied Mathematics, NDHU26 Verification

27 數值方法 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

28 數值方法 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


Download ppt "數值方法 2008 Applied Mathematics, NDHU1  Lagrange polynomial  Polynomial interpolation Lecture 4II."

Similar presentations


Ads by Google