lecture 5 polynomial approximation

44
數數數數 1 Polynomial Interpolation Example Limitation Polynomial Approximation (fitting) Line fitting Quadratic curve fitting Polynomial fitting Lecture 5 Polynomial Lecture 5 Polynomial Approximation Approximation

Upload: ona

Post on 12-Jan-2016

53 views

Category:

Documents


2 download

DESCRIPTION

Lecture 5 Polynomial Approximation. Polynomial Interpolation Example Limitation Polynomial Approximation (fitting) Line fitting Quadratic curve fitting Polynomial fitting. Polynomials. Polynomial determined by zeros in [-4,0,3]. P=poly([-4 0 3]);. Plot polynomial P. x=linspace(-5,5); - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Lecture 5 Polynomial Approximation

數值方法 1

Polynomial InterpolationExampleLimitation

Polynomial Approximation (fitting)Line fittingQuadratic curve fittingPolynomial fitting

Lecture 5 Polynomial Lecture 5 Polynomial ApproximationApproximation

Page 2: Lecture 5 Polynomial Approximation

數值方法 2

PolynomialsPolynomials

P=poly([-4 0 3]); Polynomial determined byzeros in [-4,0,3]

x=linspace(-5,5);y=polyval(P,x);plot(x,y);

Plot polynomial P

Page 3: Lecture 5 Polynomial Approximation

數值方法 3

Sampling of paired dataSampling of paired data

P=poly([-4 0 3]); Produce a polynomialwith zeros [-4,0,3]

x=rand(1,6)*10-5;y=polyval(P,x);plot(x,y, 'ro');

Plot paired data

Page 4: Lecture 5 Polynomial Approximation

數值方法 4

Polynomial interpolationPolynomial interpolation

P=poly([-4 0 3]); n=6Produce a polynomialwith zeros [-4,0,3]

x=rand(1,n)*10-5;y=polyval(P,x);plot(x,y, 'ro');

Plot the interpolating polynomial

v=linspace(min(x),max(x));z=int_poly(v,x,y);plot(v,z,'k');

Page 5: Lecture 5 Polynomial Approximation

數值方法 5

Source codes

Demo_ipDemo_ip

Page 6: Lecture 5 Polynomial Approximation

數值方法 6

m=3, n=5m=3, n=5

>> demo_ipdata size n=5

Page 7: Lecture 5 Polynomial Approximation

數值方法 7

m=3, n=6m=3, n=6

>> demo_ipdata size n=6

Page 8: Lecture 5 Polynomial Approximation

數值方法 8

m=3, n=10m=3, n=10

>> demo_ipdata size n=10

Page 9: Lecture 5 Polynomial Approximation

數值方法 9

>> demo_ipdata size n=12

m=3, n=12m=3, n=12

Page 10: Lecture 5 Polynomial Approximation

數值方法 10

P=poly([-4 0 3]);n=6; Produce a polynomialwith zeros [-4,0,3]

x=rand(1,n)*10-5;y=polyval(P,x);nois=rand(1,n)*0.5-0.25;plot(x,y+nois, 'ro');

Plot the interpolating polynomial

v=linspace(min(x),max(x));z=int_poly(v,x,y+nois);plot(v,z,'k');

Sample with noiseSample with noise

Page 11: Lecture 5 Polynomial Approximation

數值方法 11

Demo_ip_noisyDemo_ip_noisy

source codes

Page 12: Lecture 5 Polynomial Approximation

數值方法 12

Noise dataNoise data

►Noise ratioNoise ratio Mean of abs(noise)/abs(signal) Mean of abs(noise)/abs(signal)

noise_ratio =

0.0074

Page 13: Lecture 5 Polynomial Approximation

數值方法 13

Numerical resultsNumerical results

Small noise causes intolerant interpolation

>> demo_ip_noisydata size n:12

noise_ratio =

0.0053

Page 14: Lecture 5 Polynomial Approximation

數值方法 14

Numerical resultsNumerical results

Small noise causes intolerant interpolation

>> demo_ip_noisydata size n:13

noise_ratio =

0.0031

Page 15: Lecture 5 Polynomial Approximation

數值方法 15

Interpolation Vs Interpolation Vs approximationapproximation

► An interpolating polynomial is expected to satisfy An interpolating polynomial is expected to satisfy all constraints posed by paired dataall constraints posed by paired data

► An interpolating polynomial is unable to retrieve an An interpolating polynomial is unable to retrieve an original target function when noisy paired data are original target function when noisy paired data are provided provided

► For noisy paired data, the goal of polynomial fitting For noisy paired data, the goal of polynomial fitting is revised to minimize the mean square is revised to minimize the mean square approximating errorapproximating error

Page 16: Lecture 5 Polynomial Approximation

數值方法 16

Polynomial approximationPolynomial approximation

►Given paired data, (xGiven paired data, (xii,y,yii), i=1,…,n,), i=1,…,n,

the approximating polynomial is the approximating polynomial is required to minimize the mean required to minimize the mean square error of approximating ysquare error of approximating yi i by by f(xf(xii))

2

1

))((1

E

n

iii yxf

n

Page 17: Lecture 5 Polynomial Approximation

數值方法 17

Polynomial fittingPolynomial fitting

fa1d_polyfit.m

Page 18: Lecture 5 Polynomial Approximation

數值方法 18

Line fittingLine fitting

►Minimizaing the mean square Minimizaing the mean square approximating errorapproximating error

2

1

)(1

b)E(a,

n

iii ybax

n

Target: baxy

Page 19: Lecture 5 Polynomial Approximation

數值方法 19

Line fittingLine fitting

Red:Approximating polynomial

>> fa1d_polyfitinput a function: x.^2+cos(x) :x+5keyin sample size:300polynomial degree:1

E =

8.3252e-004

a=1;b=5

Page 20: Lecture 5 Polynomial Approximation

數值方法 20

Line fittingLine fitting

Red:Approximating polynomial

a=3;b=1/2

>> fa1d_polyfitinput a function: x.^2+cos(x) :3*x+1/2keyin sample size:30polynomial degree:1

E =

0.0010

Page 21: Lecture 5 Polynomial Approximation

數值方法 21

Objective function IObjective function I

►Line fitting Line fitting

2

1fitting-Line1 )(

1b)(a,E)(E

n

iii ybax

n

Target: baxy

Page 22: Lecture 5 Polynomial Approximation

數值方法 22

►EE1 1 is a quadratic function of a and bis a quadratic function of a and b

►Setting derivatives of ESetting derivatives of E1 1 to zero to zero leads to leads to

a linear systema linear system 2

1fitting-Line1 )(

1b)(a,E)(E

n

iii ybax

n

Page 23: Lecture 5 Polynomial Approximation

數值方法 23

0)(2

0)(2

0

)(1

b)(a,E)(E

1

1

1

1

1

2

1fitting-Line1

n

iii

i

n

iii

n

iii

ybaxndb

dE

xybaxnda

dEd

dE

ybaxn

a linear system

Page 24: Lecture 5 Polynomial Approximation

數值方法 24

n

ii

n

ii

n

iii

n

ii

n

ii

n

iii

n

iiiii

n

iii

i

n

iii

ynbax

yxbxax

ybax

xybxax

ybaxndb

dE

xybaxnda

dE

11

111

2

1

1

2

1

1

1

1

0)(

0)(

0)(2

0)(2

Page 25: Lecture 5 Polynomial Approximation

數值方法 25

n

ii

n

iii

n

ii

n

ii

n

ii

n

ii

n

ii

n

iii

n

ii

n

ii

y

yx

b

a

nx

xx

ynbax

yxbxax

1

1

1

11

2

11

111

2

Page 26: Lecture 5 Polynomial Approximation

數值方法 26

n

ii

n

iii

n

ii

n

ii

n

ii

n

ii

n

iii

n

ii

n

ii

n

ii

y

yx

nx

xx

b

a

y

yx

b

a

nx

xx

1

1

1

1

11

2

1

1

1

11

2

Page 27: Lecture 5 Polynomial Approximation

數值方法 27

Objective function IIObjective function II

►Quadratic polynomial fittingQuadratic polynomial fitting

2

1

2

gurveFittinQuadraticC2

)(1

c)b,(a,E)(E

n

iiii ycbxax

n

Target: cbxaxy 2

Page 28: Lecture 5 Polynomial Approximation

數值方法 28

►EE2 2 are quadraticare quadratic

►Setting derivatives of ESetting derivatives of E2 2 to zero to zero leads to leads to

a linear systema linear system

2

1

2

gurveFittinQuadraticC2

)(1

c)b,(a,E)(E

n

iiii ycbxax

n

Page 29: Lecture 5 Polynomial Approximation

數值方法 29

0)(2

0)(2

0)(2

0

)(1

c)b,(a,E)(E

1

23

1

22

2

1

22

2

2

1

2gurveFittinQuadraticC2

n

iiii

i

n

iiii

i

n

iiii

n

iiii

ycbxaxndc

dE

xycbxaxndb

dE

xycbxaxnda

dEd

dE

ycbxaxn

a linear system

Page 30: Lecture 5 Polynomial Approximation

數值方法 30

0)(

0)(

0)(

1

2

1

2

2

1

2

n

iiii

i

n

iiii

i

n

iiii

ycbxax

xycbxax

xycbxax

0)(

0)(

0)(

1

2

1

23

1

2234

n

iiii

n

iiiiii

n

iiiiii

ycbxax

xycxbxax

xycxbxaxˋ

Page 31: Lecture 5 Polynomial Approximation

數值方法 31

0)(

0)(

0)(

1

2

1

23

1

2234

n

iiii

n

iiiiii

n

iiiiii

ycbxax

xycxbxax

xycxbxaxˋ

n

ii

n

ii

n

ii

n

iii

n

ii

n

ii

n

ii

n

iii

n

ii

n

ii

n

ii

yncbxax

xycxbxax

xycxbxax

111

2

111

2

1

3

1

2

1

2

1

3

1

4

Page 32: Lecture 5 Polynomial Approximation

數值方法 32

n

ii

n

ii

n

ii

n

iii

n

ii

n

ii

n

ii

n

iii

n

ii

n

ii

n

ii

yncbxax

xycxbxax

xycxbxax

111

2

111

2

1

3

1

2

1

2

1

3

1

4

n

ii

n

iii

n

iii

n

ii

n

ii

n

ii

n

ii

n

ii

n

ii

n

ii

n

ii

y

xy

xy

c

b

a

nxx

xxx

xxx

1

1

1

2

11

2

11

2

1

3

1

2

1

3

1

4

Page 33: Lecture 5 Polynomial Approximation

數值方法 33

Quadratic polynomial Quadratic polynomial fittingfitting

►Minimization of an approximating Minimization of an approximating errorerror

2

1

2 )(1

c)b,E(a,

n

iiii ycbxax

n

Target: cbxaxy 2

Page 34: Lecture 5 Polynomial Approximation

數值方法 34

Quadratic poly fittingQuadratic poly fitting

input a function: x.^2+cos(x) :3*x.^2-2*x+1keyin sample size:20polynomial degree:2

E =

6.7774e-004a=3b=-2c=1

Page 35: Lecture 5 Polynomial Approximation

數值方法 35

Data driven polynomial Data driven polynomial approximationapproximation

►Minimization of Mean square error Minimization of Mean square error (mse)(mse)

►Data driven polynomial Data driven polynomial approximationapproximation f : a polynomial pf : a polynomial pmm

Polynomial degree m is less than data Polynomial degree m is less than data size nsize n

2

1

))((1

E

n

iiim yxp

n

Page 36: Lecture 5 Polynomial Approximation

數值方法 36

i allfor )( 0E

))((1

E 2

1

imi

n

iiim

xpy

yxpn

Special case: polynomial Special case: polynomial interpolationinterpolation

Page 37: Lecture 5 Polynomial Approximation

數值方法 37

POLYFIT: Fit polynomial to dataPOLYFIT: Fit polynomial to data

►polyfit(x,y,m)polyfit(x,y,m) x : input vectors or predictorsx : input vectors or predictors y : desired outputsy : desired outputs m : degree of interpolating polynomialm : degree of interpolating polynomial

►Use m to prevent from over-fittingUse m to prevent from over-fitting►Tolerance to noiseTolerance to noise

Page 38: Lecture 5 Polynomial Approximation

數值方法 38

POLYFIT: Fit polynomial to POLYFIT: Fit polynomial to datadata

P=poly([-4 0 3]); n=20;m=3; A polynomial determinedby zeros in [-4,0,3]

x=rand(1,20)*10-5;y=polyval(P,x);nois=rand(1,20)*0.5-0.25;plot(x,y+nois, 'ro');

Plot the interpolating polynomial

v=linspace(min(x),max(x));p=polyfit(x,y+nois,m);hold on;plot(v,polyval(p,v),'k');

Page 39: Lecture 5 Polynomial Approximation

數值方法 39

Non-polynomialNon-polynomial

► sinsin

fx=inline('sin(x)'); n=20;m=3

x=rand(1,n)*10-5;y=fx(x);nois=rand(1,n)*0.5-0.25;plot(x,y+nois, 'ro');hold on

Page 40: Lecture 5 Polynomial Approximation

數值方法 40

Under-fittingUnder-fitting

v=linspace(min(x),max(x));p=polyfit(x,y+nois,m);hold on;plot(v,polyval(p,v),'k');

m=3

Under-fitting due to approximating non-polynomial by low-degree polynomials

Page 41: Lecture 5 Polynomial Approximation

數值方法 41

Intolerant mseIntolerant mse

>> mean((polyval(p,x)-(y+nois)).^2)

ans =

0.0898

Under-fitting causes intolerant mean square error

Page 42: Lecture 5 Polynomial Approximation

數值方法 42

m=4;v=linspace(min(x),max(x));p=polyfit(x,y+nois,m);hold on;plot(v,polyval(p,v), 'b');

Under-fittingUnder-fitting

M=4

Page 43: Lecture 5 Polynomial Approximation

數值方法 43

>> fa1d_polyfitinput a function: x.^2+cos(x) :sin(x)keyin sample size:50polynomial degree:5

E =

0.0365

Fitting non-polynomialFitting non-polynomial

Page 44: Lecture 5 Polynomial Approximation

數值方法 44

>> fa1d_polyfitinput a function: x.^2+cos(x) :tanh(x+2)+sech(x)keyin sample size:30polynomial degree:5

E =

0.0097

Fitting non-polynomialFitting non-polynomial