28
數數數數 2008 Applied Mathematics, NDHU 1 Lagrange polynomial Polynomial interpolation Lecture 4II

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

Embed Size (px)

Citation preview

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

數值方法2008 Applied Mathematics, NDHU

1

Lagrange polynomial Polynomial interpolation

Lecture 4II

Page 2: 數值方法 2008 Applied Mathematics, NDHU1  Lagrange polynomial  Polynomial interpolation Lecture 4II

數值方法2008 Applied Mathematics, NDHU

2

poly

Find the polynomial defined by given zeros

poly returns coefficients of a polynomial with roots –5,0 and 5

poly([-5 0 5])

Page 3: 數值方法 2008 Applied Mathematics, NDHU1  Lagrange polynomial  Polynomial interpolation Lecture 4II

數值方法2008 Applied Mathematics, NDHU

3

polyval

Polynomial evaluationy 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);

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

數值方法2008 Applied Mathematics, NDHU

4

p3(x)=(x-5)(x+5)(x-0)

-5 0 5-50

-40

-30

-20

-10

0

10

20

30

40

50

-5 0 5-50

-40

-30

-20

-10

0

10

20

30

40

50

x=[4 1 3 -4];y=polyval(p,x);hold on;plot(x,y,'ro')

-4 -3 -2 -1 0 1 2 3 4-50

-40

-30

-20

-10

0

10

20

30

40

Given data, find an interpolating polynomial

Page 5: 數值方法 2008 Applied Mathematics, NDHU1  Lagrange polynomial  Polynomial interpolation Lecture 4II

數值方法2008 Applied Mathematics, NDHU

5

Polynomial Interpolation

Paired dataEach pair (xi,yi) specifies a point on a

target polynomialyi = f(xi) for all iGiven paired data, find an interpolating

polynomial

Page 6: 數值方法 2008 Applied Mathematics, NDHU1  Lagrange polynomial  Polynomial interpolation Lecture 4II

數值方法2008 Applied Mathematics, NDHU

6

Strategy

Use a linear combination of Lagrange polynomials determined by all xi to express the interpolating polynomial

Page 7: 數值方法 2008 Applied Mathematics, NDHU1  Lagrange polynomial  Polynomial interpolation Lecture 4II

數值方法2008 Applied Mathematics, NDHU

7

Lagrange polynomial

n knots,n Lagrange polynomials

],...,[ 1 nxxx

ij, if 0,

if ,1)(

j

ii

xx

xxxL

Page 8: 數值方法 2008 Applied Mathematics, NDHU1  Lagrange polynomial  Polynomial interpolation Lecture 4II

數值方法2008 Applied Mathematics, NDHU

8

Mathematical expression

otherwise 0,

if ,1)(

ii xxxL

ni

n

ii

i

ii

i

i

i xx

xx

xx

xx

xx

xx

xx

xxxL

1

1

1

1

1

1)(

Page 9: 數值方法 2008 Applied Mathematics, NDHU1  Lagrange polynomial  Polynomial interpolation Lecture 4II

數值方法2008 Applied Mathematics, NDHU

9

1)(1

1

1

1

1

1

ni

ni

ii

ii

ii

ii

i

iii xx

xx

xx

xx

xx

xx

xx

xxxL

0)(1

1

1

1

1

1

ni

nj

ii

ij

ii

ij

i

j

iji xx

xx

xx

xx

xx

xx

xx

xxxL

Proof

Page 10: 數值方法 2008 Applied Mathematics, NDHU1  Lagrange polynomial  Polynomial interpolation Lecture 4II

數值方法2008 Applied Mathematics, NDHU

10

n

ijj ji

j

ni

n

ii

i

ii

i

i

i

xx

xx

xx

xx

xx

xx

xx

xx

xx

xxxL

1

1

1

1

1

1

1

)(

Product form

Page 11: 數值方法 2008 Applied Mathematics, NDHU1  Lagrange polynomial  Polynomial interpolation Lecture 4II

數值方法2008 Applied Mathematics, NDHU

11

Lagrange polynomial

Given n knots,

Let Li denote the ith Lagrange polynomial

Li is a polynomial of degree n-1

Li has n-1 roots:

Normalization: Li satisfies

Li(xi)=1

niii xxxxx ,...,,,,..., 111

nii xxxx ,...,,,..., 111

Page 12: 數值方法 2008 Applied Mathematics, NDHU1  Lagrange polynomial  Polynomial interpolation Lecture 4II

數值方法2008 Applied Mathematics, NDHU

12

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 xi

xzeros=[x(1:i-1) x(i+1:n)];pi=poly(xzeros);

Page 13: 數值方法 2008 Applied Mathematics, NDHU1  Lagrange polynomial  Polynomial interpolation Lecture 4II

數值方法2008 Applied Mathematics, NDHU

13

Implementation: Normalization

c=polyval(pi,x(i));pi=pi/c;

Normalization condition1)( ii xL

Page 14: 數值方法 2008 Applied Mathematics, NDHU1  Lagrange polynomial  Polynomial interpolation Lecture 4II

數值方法2008 Applied Mathematics, NDHU

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 Li

Page 15: 數值方法 2008 Applied Mathematics, NDHU1  Lagrange polynomial  Polynomial interpolation Lecture 4II

數值方法2008 Applied Mathematics, NDHU

15

x_knot consists of n knotsx_in collects input valuesEvaluate the ith Lagrange polynomial Li

defined by knots in x_knoty= Li(x_in)

y=lagrange_poly(x_in,x_knot,i)

Evaluation of the ith Lagrange polynomial

Page 16: 數值方法 2008 Applied Mathematics, NDHU1  Lagrange polynomial  Polynomial interpolation Lecture 4II

數值方法2008 Applied Mathematics, NDHU

16

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

n

ijj ji

j

i

xx

xx

xL

1

)(

Page 17: 數值方法 2008 Applied Mathematics, NDHU1  Lagrange polynomial  Polynomial interpolation Lecture 4II

數值方法2008 Applied Mathematics, NDHU

17

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; endendreturn

Direct Implementation of the product form

Page 18: 數值方法 2008 Applied Mathematics, NDHU1  Lagrange polynomial  Polynomial interpolation Lecture 4II

數值方法2008 Applied Mathematics, NDHU

18

x_in=linspace(-2,3);y=lagrange_poly(x_in,[-1 0 1 2],1);plot(x_in,y);

n=4,i=1 (strategy II)

Page 19: 數值方法 2008 Applied Mathematics, NDHU1  Lagrange polynomial  Polynomial interpolation Lecture 4II

數值方法2008 Applied Mathematics, NDHU

19

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');

Page 20: 數值方法 2008 Applied Mathematics, NDHU1  Lagrange polynomial  Polynomial interpolation Lecture 4II

數值方法2008 Applied Mathematics, NDHU

20

x_in=linspace(-2,3);y=lagrange_poly(x_in,[-1 0 1 2],2);plot(v,y);

n=4,i=2 (strategy II)

Page 21: 數值方法 2008 Applied Mathematics, NDHU1  Lagrange polynomial  Polynomial interpolation Lecture 4II

數值方法2008 Applied Mathematics, NDHU

21

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');

Page 22: 數值方法 2008 Applied Mathematics, NDHU1  Lagrange polynomial  Polynomial interpolation Lecture 4II

數值方法2008 Applied Mathematics, NDHU

22

Polynomial Interpolation

Paired dataEach pair (xi,yi) specifies a point on a

target polynomialyi = f(xi) for all iGiven paired data, find an interpolating

polynomial

Page 23: 數值方法 2008 Applied Mathematics, NDHU1  Lagrange polynomial  Polynomial interpolation Lecture 4II

數值方法2008 Applied Mathematics, NDHU

23

Input

x

y

A sample from an unknown function

Page 24: 數值方法 2008 Applied Mathematics, NDHU1  Lagrange polynomial  Polynomial interpolation Lecture 4II

數值方法2008 Applied Mathematics, NDHU

24

Polynomial interpolation

Given Find a polynomial that satisfies

for all i

,...,1 ),,( niyx ii

)( ii yxf

Page 25: 數值方法 2008 Applied Mathematics, NDHU1  Lagrange polynomial  Polynomial interpolation Lecture 4II

數值方法2008 Applied Mathematics, NDHU

25

Interpolating polynomial is with degree n-1

)()(1

n

iii xLyxp

Interpolating polynomial

Page 26: 數值方法 2008 Applied Mathematics, NDHU1  Lagrange polynomial  Polynomial interpolation Lecture 4II

數值方法2008 Applied Mathematics, NDHU

26

Verification

)(

)()(

)()(

i

iii

ikikkiii

kikki

y

xLy

xLyxLy

xLyxp

Page 27: 數值方法 2008 Applied Mathematics, NDHU1  Lagrange polynomial  Polynomial interpolation Lecture 4II

數值方法2008 Applied Mathematics, NDHU

27

)()(1

n

iii xLyxp

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

Page 28: 數值方法 2008 Applied Mathematics, NDHU1  Lagrange polynomial  Polynomial interpolation Lecture 4II

數值方法2008 Applied Mathematics, NDHU

28

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); endreturn