10
Lecture 17: Polynomial interpolation. Runge example. Download rungepoly .m Runge phenomenon – polydomial wiggle associated rith high degree polynomial interpolation

Lecture 17: Polynomial interpolation. Runge example. Download rungepoly.m Runge phenomenon – polydomial wiggle associated rith high degree polynomial interpolation

Embed Size (px)

Citation preview

Page 1: Lecture 17: Polynomial interpolation. Runge example. Download rungepoly.m Runge phenomenon – polydomial wiggle associated rith high degree polynomial interpolation

Lecture 17: Polynomial interpolation.Runge example.

Download

rungepoly.m

Runge phenomenon – polydomial wiggle associatedrith high degree polynomial interpolation

Page 2: Lecture 17: Polynomial interpolation. Runge example. Download rungepoly.m Runge phenomenon – polydomial wiggle associated rith high degree polynomial interpolation

function rungepoly(n)% interpolate Runge function with polynomial% estimated at uniformly spaced points % input: n - degree of polynomial

% plot runge function

xmin=-1;xmax=1;

f = inline('1./(1 + 12*x.^2)'); x = linspace(xmin,xmax,200); % interpolation with equidistant points

x = linspace(xmax,xmin,n); y = f(x);

p=polyfit(x,y,n-1);xvec=linspace(xmin,xmax,500);yvec=polyval(p,xvec);yexact= f(xvec);plot(xvec,yvec,'b',x,y,'ro',xvec,yexact,'r--');legend('Interpolation','Interpolating points','Exact solution');

end

Page 3: Lecture 17: Polynomial interpolation. Runge example. Download rungepoly.m Runge phenomenon – polydomial wiggle associated rith high degree polynomial interpolation

Result:>> rungepoly(10)

-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1-0.2

0

0.2

0.4

0.6

0.8

1

1.2

Interpolation

Interpolating pointsExact solution

Page 4: Lecture 17: Polynomial interpolation. Runge example. Download rungepoly.m Runge phenomenon – polydomial wiggle associated rith high degree polynomial interpolation

Result:>> rungepoly(20)

-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1-0.5

0

0.5

1

1.5

2

Interpolation

Interpolating pointsExact solution

Page 5: Lecture 17: Polynomial interpolation. Runge example. Download rungepoly.m Runge phenomenon – polydomial wiggle associated rith high degree polynomial interpolation

Runge example with Chebyshev interpolation.

Download

rungecheb.m

The nth Chebyshev polynomial is given by Tn(x) = cos(n arrcos(x))All the zeros of Tn(x) lie in [-1,+1] and can be calculated by xi = cos((2i-1) pi/2n) where odd is an odd integer.The use of the zeros of the Chebyshev nodes as interpolation points ensures that the maximum of the interpolation error will be minimized.

Page 6: Lecture 17: Polynomial interpolation. Runge example. Download rungepoly.m Runge phenomenon – polydomial wiggle associated rith high degree polynomial interpolation

function rungecheb(n)% interpolate Runge function with polynomial% estimated at Chebychev points % input: n - degree of polynomial

% plot runge function

xmin=-1;xmax=1;

f = inline('1./(1 + 12*x.^2)'); x = linspace(xmin,xmax,200); % interpolation with equidistant points

x = chebroots(n); y = f(x);

p=polyfit(x,y,n-1);xvec=linspace(xmin,xmax,500);yvec=polyval(p,xvec);yexact= f(xvec);plot(xvec,yvec,'b',x,y,'ro',xvec,yexact,'r--');legend('Interpolation','Interpolating points','Exact solution');title('interpolation at Chebyshev points')

end

Page 7: Lecture 17: Polynomial interpolation. Runge example. Download rungepoly.m Runge phenomenon – polydomial wiggle associated rith high degree polynomial interpolation

function x = chebroots(n)% compute roots of Chebyshev polinomial%% input: n - order of polynomial

for k=1:n x(k)=cos( pi*(2*k-1)/(2*n) ); end

end

Page 8: Lecture 17: Polynomial interpolation. Runge example. Download rungepoly.m Runge phenomenon – polydomial wiggle associated rith high degree polynomial interpolation

Result:>> rungecheb(10)

-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1-0.2

0

0.2

0.4

0.6

0.8

1

1.2

Interpolation

Interpolating pointsExact solution

-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 10

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1interpolation at Chebyshev points

Interpolation

Interpolating pointsExact solution

function Compare to equally spaced points:

Page 9: Lecture 17: Polynomial interpolation. Runge example. Download rungepoly.m Runge phenomenon – polydomial wiggle associated rith high degree polynomial interpolation

Result:>> rungecheb(20)

-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1-0.5

0

0.5

1

1.5

2

Interpolation

Interpolating pointsExact solution

Compare to equally spaced points:

-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 10

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1interpolation at Chebyshev points

Interpolation

Interpolating pointsExact solution

Page 10: Lecture 17: Polynomial interpolation. Runge example. Download rungepoly.m Runge phenomenon – polydomial wiggle associated rith high degree polynomial interpolation

The following data are related to the life expectances of citizens of two European regions. 1975 1980 1985 1990WE: 72.8 74.2 75.2 76.4 EE: 70.2 70.2 70.3 71.2Interpolate each set of data with a polynomial of degree 3 and estimate the life expectances in 1970, 1983, and 1988. Plot each fit along with the data and label your plots.

Inclass