Lecture 4 II Lagrange polynomial Polynomial interpolation 2008

  • Slides: 28
Download presentation
Lecture 4 II Ø Lagrange polynomial Ø Polynomial interpolation 數值方法 2008 Applied Mathematics, NDHU

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

poly l Express a polynomial according to given zeros poly([-5 0 5]) l poly.

poly l Express a polynomial according to given zeros poly([-5 0 5]) l poly. m returns coefficients of a polynomial with roots – 5, 0 and 5 數值方法 2008 Applied Mathematics, NDHU 2

>> poly([-5 0 5]) ans = 1 0 -25 0 數值方法 2008 Applied Mathematics,

>> poly([-5 0 5]) ans = 1 0 -25 0 數值方法 2008 Applied Mathematics, NDHU 3

polyval x=linspace(-5, 5); p=poly([-5 0 5]); y=polyval(p, x); Polynomial evaluation l polyval. m substitutes

polyval x=linspace(-5, 5); p=poly([-5 0 5]); y=polyval(p, x); Polynomial evaluation l polyval. m substitutes elements in x to polynomial p is a vector that represents a polynomial with roots 5, 0 and -5 l 數值方法 2008 Applied Mathematics, NDHU 4

Lagrange polynomial l n knots, l l Li denotes the ith Lagrange polynomial that

Lagrange polynomial l n knots, l l Li denotes the ith Lagrange polynomial that responds one to xi and zeros to the other knots 數值方法 2008 Applied Mathematics, NDHU 5

Mathematical expression 數值方法 2008 Applied Mathematics, NDHU 6

Mathematical expression 數值方法 2008 Applied Mathematics, NDHU 6

Proof 數值方法 2008 Applied Mathematics, NDHU 7

Proof 數值方法 2008 Applied Mathematics, NDHU 7

Proof 數值方法 2008 Applied Mathematics, NDHU 8

Proof 數值方法 2008 Applied Mathematics, NDHU 8

Product form 數值方法 2008 Applied Mathematics, NDHU 9

Product form 數值方法 2008 Applied Mathematics, NDHU 9

Lagrange polynomial Given n knots, Let Li denote the ith Lagrange polynomial l Li

Lagrange polynomial Given n knots, Let Li denote the ith Lagrange polynomial l Li is a polynomial of degree n-1 l Li has n-1 roots: l Normalization: Li satisfies Li(xi)=1 數值方法 2008 Applied Mathematics, NDHU 10

Polynomial of n-1 roots lx is a vector that consists of n distinct knots

Polynomial of n-1 roots lx is a vector that consists of n distinct knots xzeros=[x(1, : i-1) x(i+1: n)]; pi=poly(xzeros); l pi is a polynomial whose roots are all knots except for xi 數值方法 2008 Applied Mathematics, NDHU 11

Normalization c=polyval(pi, x(i)); pi=pi/c; Normalization condition 數值方法 2008 Applied Mathematics, NDHU 12

Normalization c=polyval(pi, x(i)); pi=pi/c; Normalization condition 數值方法 2008 Applied Mathematics, NDHU 12

Implementation I l Apply poly. m and polyval. m xzeros=[x(1: i-1) x(i+1: n)]; pi=poly(xzeros);

Implementation I l Apply poly. m and polyval. m xzeros=[x(1: i-1) x(i+1: n)]; pi=poly(xzeros); c=polyval(pi, x(i)); pi=pi/c; 數值方法 2008 Applied Mathematics, NDHU 13

Lagrange polynomial evaluation y=lagrange_poly(v, x, i) % x contains n knots % Substitute elements

Lagrange polynomial evaluation y=lagrange_poly(v, x, i) % x contains n knots % Substitute elements in v to Li defined by given knots % y= Li(v) 數值方法 2008 Applied Mathematics, NDHU 14

Implementation II y=lagrange_poly(v, x, i) y=ones(size(v)) n=length(x) for j=1: n i==j Y y=…. 數值方法

Implementation II y=lagrange_poly(v, x, i) y=ones(size(v)) n=length(x) for j=1: n i==j Y y=…. 數值方法 2008 Applied Mathematics, NDHU 15

Evaluation of product form by for-looping function y=lagrange_poly(v, x, i) % evaluation of Li

Evaluation of product form by for-looping function y=lagrange_poly(v, x, i) % evaluation of Li defined by knots in x y=ones(size(v)); n=length(x); for j=1: n if j~=i y=y. *(v-x(j))/(x(i)-x(j)); end return 數值方法 2008 Applied Mathematics, NDHU 16

n=4, i=1 v=linspace(-2, 3); y=lagrange_poly(v, [-1 0 1 2], 1); plot(v, y); 數值方法 2008

n=4, i=1 v=linspace(-2, 3); y=lagrange_poly(v, [-1 0 1 2], 1); plot(v, y); 數值方法 2008 Applied Mathematics, NDHU 17

n=4, i=1 x=[-1 0 1 2]; i=1; n=length(x); xzeros=[x(1: i-1) x(i+1: n)]; pi=poly(xzeros); pi=pi/polyval(pi,

n=4, i=1 x=[-1 0 1 2]; i=1; n=length(x); xzeros=[x(1: i-1) x(i+1: n)]; pi=poly(xzeros); pi=pi/polyval(pi, x(i)); v=linspace(-2, 3); plot(v, polyval(pi, v), 'r'); 數值方法 2008 Applied Mathematics, NDHU 18

n=4, i=2 (Implementation I) x=[-1 0 1 2]; i=2; n=length(x); xzeros=[x(1: i-1) x(i+1: n)];

n=4, i=2 (Implementation I) x=[-1 0 1 2]; i=2; n=length(x); xzeros=[x(1: i-1) x(i+1: n)]; pi=poly(xzeros); pi=pi/polyval(pi, x(i)); v=linspace(-2, 3); plot(v, polyval(pi, v), 'r'); plot(v, 0); 數值方法 2008 Applied Mathematics, NDHU 19

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

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

Polynomial interpolation l Find a polynomial well interpolating given points l Input: paired data,

Polynomial interpolation l Find a polynomial well interpolating given points l Input: paired data, S={(xi, yi)}i l Output: a polynomial p(x) that pass all points in S xi p(x) 數值方法 2008 Applied Mathematics, NDHU yi 21

Sampling A sample from an unknown target function y 數值方法 2008 Applied Mathematics, NDHU

Sampling A sample from an unknown target function y 數值方法 2008 Applied Mathematics, NDHU x 22

Polynomial interpolation l Given l Find a polynomial that satisfies for all i 數值方法

Polynomial interpolation l Given l Find a polynomial that satisfies for all i 數值方法 2008 Applied Mathematics, NDHU 23

Interpolating polynomial l. A linear combination of n Lagrange polynomials defined by n knots

Interpolating polynomial l. A linear combination of n Lagrange polynomials defined by n knots lf is a polynomial of degree n-1 l Li denotes the ith Lagrange polynomial 數值方法 2008 Applied Mathematics, NDHU 24

Verification 數值方法 2008 Applied Mathematics, NDHU 25

Verification 數值方法 2008 Applied Mathematics, NDHU 25

Evaluation of interpolating polynomial l function z=int_poly(v, x, y) % x contains n knots

Evaluation of interpolating polynomial l function z=int_poly(v, x, y) % x contains n knots % y contains desired targets % substitute elements in v to f 數值方法 2008 Applied Mathematics, NDHU 26

Evaluation of interpolating polynomial l function z=int_poly(v, x, y) n=length(x) z=zeros(size(v)) for i=1: n

Evaluation of interpolating polynomial l function z=int_poly(v, x, y) n=length(x) z=zeros(size(v)) for i=1: n % call lagrange_poly(v, x, i) to evaluate Li(v) 數值方法 2008 Applied Mathematics, NDHU 27

function z=int_poly(v, x, y) z=zeros(size(v)); n=length(x); for i=1: n z=z+lagrange_poly(v, x, i)*y(i); end return

function z=int_poly(v, x, y) z=zeros(size(v)); n=length(x); for i=1: n z=z+lagrange_poly(v, x, i)*y(i); end return 數值方法 2008 Applied Mathematics, NDHU 28