15
HK Kim Slightly modified 3/1/09, 2/28/06 Firstly written at March 2005 DM21869/Computational Numerical Analysis/15_cna.doc Available at http://bml.pusan.ac.kr BML/SME/PNU 1 15 Polynomial Interpolation Introduction to Interpolation Newton Interpolating Polynomial Lagrange Interpolating Polynomial Inverse Interpolation Extrapolation and Oscillations T (°C) ρ (kg/m 3 ) μ (Ns/m 2 ) v (m 2 /s) -40 0 20 50 100 150 200 250 300 400 500 1.52 1.29 1.20 1.09 0.946 0.835 0.746 0.675 0.616 0.525 0.457 1.51 × 10 -5 1.71 × 10 -5 1.80 × 10 -5 1.95 × 10 -5 2.17 × 10 -5 2.38 × 10 -5 2.57 × 10 -5 2.75 × 10 -5 2.93 × 10 -5 3.25 × 10 -5 3.55 × 10 -5 0.99 × 10 -5 1.33 × 10 -5 1.50 × 10 -5 1.79 × 10 -5 2.30 × 10 -5 2.85 × 10 -5 3.45 × 10 -5 4.08 × 10 -5 4.75× 10 -5 6.20 × 10 -5 7.77 × 10 -5 How can you determine the density, dynamic or kinimatic viscosity that you desire at a temperature? the simplest approach is to determine the equation for the straight line connecting the two adjacent values and use this equation to estimate the parameters at the desired intermediate temperature known as "linear interpolation" (perfectly adequate in many cases) error can be introduced when the data exhibits significant curvature let's explore a number of different approaches for obtaining adequate estimates! Density (ρ), dynamic viscosity (μ), and kinematic viscosity (v) as a function of temperature (T) at 1 atm as reported by White (1999).

15 Polynomial Interpolation - Welcome|Radiation …bml.pusan.ac.kr/resources/15_cna_students.pdf · MATLAB functions: Polyfit and Polyval ents ... M-file: Newtint

  • Upload
    hadung

  • View
    234

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 15 Polynomial Interpolation - Welcome|Radiation …bml.pusan.ac.kr/resources/15_cna_students.pdf · MATLAB functions: Polyfit and Polyval ents ... M-file: Newtint

HK Kim Slightly modified 3/1/09, 2/28/06

Firstly written at March 2005

DM21869/Computational Numerical Analysis/15_cna.doc Available at http://bml.pusan.ac.krBML/SME/PNU

1

15 Polynomial Interpolation

Introduction to Interpolation Newton Interpolating Polynomial Lagrange Interpolating Polynomial Inverse Interpolation Extrapolation and Oscillations

T (°C) ρ (kg/m3) μ (N⋅s/m2) v (m2/s)

-40 0

20 50 100 150 200 250 300 400 500

1.52 1.29 1.20 1.09

0.946 0.835 0.746 0.675 0.616 0.525 0.457

1.51 × 10-5 1.71 × 10-5 1.80 × 10-5 1.95 × 10-5 2.17 × 10-5 2.38 × 10-5 2.57 × 10-5 2.75 × 10-5 2.93 × 10-5 3.25 × 10-5 3.55 × 10-5

0.99 × 10-5 1.33 × 10-5 1.50 × 10-5 1.79 × 10-5 2.30 × 10-5 2.85 × 10-5 3.45 × 10-5 4.08 × 10-5 4.75× 10-5 6.20 × 10-5 7.77 × 10-5

How can you determine the density, dynamic or kinimatic viscosity that you desire at a temperature?

→ the simplest approach is to determine the equation for the straight line connecting the two adjacent

values and use this equation to estimate the parameters at the desired intermediate temperature

→ known as "linear interpolation" (perfectly adequate in many cases)

→ error can be introduced when the data exhibits significant curvature

→ let's explore a number of different approaches for obtaining adequate estimates!

Density (ρ), dynamic viscosity (μ), and kinematic viscosity (v) as a function of temperature (T) at 1 atm as reported by White (1999).

Page 2: 15 Polynomial Interpolation - Welcome|Radiation …bml.pusan.ac.kr/resources/15_cna_students.pdf · MATLAB functions: Polyfit and Polyval ents ... M-file: Newtint

HK Kim Slightly modified 3/1/09, 2/28/06

Firstly written at March 2005

DM21869/Computational Numerical Analysis/15_cna.doc Available at http://bml.pusan.ac.krBML/SME/PNU

2

Introduction to Interpolation

Polynomial interpolation

• to estimate intermediate values between precise data points

• to determine the unique (n – 1)th-order polynomial that fits n data points

12321)( −++++= n

n xaxaxaaxf L

← only one straight line (i.e., a first-order polynomial) to connect two points

← only one parabola (i.e., a second-order polynomial) to connect three points

Note that MATLAB represents polynomial coefficients using decreasing powers as in

nnnn pxpxpxpxf ++++= −−−

12

21

1)( L

Determining polynomial coefficients

• generating n linear algebraic equations and solving simultaneously for n coefficients

→ straightforward way

Examples of interpolating polynomials: (a) first-order (linear) connecting two points, (b) second-order (quadratic or parabolic) connecting three points, and (c) third-order (cubic) connecting four points.

Page 3: 15 Polynomial Interpolation - Welcome|Radiation …bml.pusan.ac.kr/resources/15_cna_students.pdf · MATLAB functions: Polyfit and Polyval ents ... M-file: Newtint

HK Kim Slightly modified 3/1/09, 2/28/06

Firstly written at March 2005

DM21869/Computational Numerical Analysis/15_cna.doc Available at http://bml.pusan.ac.krBML/SME/PNU

3

Example ------------------------------ ----------------------------------------------------------

Determine the coefficie

----------------------------

nts of the parabola, , that passes through the last three density values

om the above Table.

Sol.)

322

1)( pxpxpxf ++=

fr

4570)5250)( 4006160)( 300

3

22

11

..xfx.xfx

===

( 5003 xfx =

==

0 1; 250000 500 1];

6 0.525 0.457]';

0000000000

hen we have

etermine a value of density at a temperature of 350°C:

------------------------------------------------------------------------------------------------------------------------------------------

Note that the coefficient matrix in the above example;

→ very ill-conditioned system → sensitive to round-off errors

)

herefore, alternative approaches are needed: the Newton and the Lagrange polynomials.

322

1

322

1

322

1

)500()500(4570

)400()400(5250

)300()300(6160

ppp.

ppp.

ppp.

++=

++=

++=

⎪⎭

⎪⎬

⎪⎩

⎪⎨

⎧=

⎪⎭

⎪⎬

⎪⎩

⎪⎨

⎥⎥⎥

⎢⎢⎢

457.0525.0616.0

1500000,2501400000,1601300000,90

3

2

1

ppp

>> format long

>> A = [90000 300 1; 160000 40

>> b = [0.61

>> p = A\b

p = 0.00000115000000

-0.00171500000000

1.0270

T

027.1001715.000000115.0)( 2 +−= xxxf

D

567625.0027.1)350(001715.0)350(00000115.0)350( 2 =+−=f

--

⎪⎭

⎪⎬

⎪⎩

⎪⎨

⎧=

⎪⎭

⎪⎬

⎪⎨

⎥⎥⎤

⎢⎢⎡

)()()(

11

3

2

1

2

1

222

121

xfxfxf

pp

xxxx

⎩⎥⎦⎢⎣ 1 33

23 pxx

>> cond(A

ans =

5.8932e+006

T

MATLAB functions: Polyfit and Polyval

ents

• when the number of data points = the number of coefficients → polynomial interpolation

• used when the number of data points ≥ the number of coeffici → polynomial regression

Page 4: 15 Polynomial Interpolation - Welcome|Radiation …bml.pusan.ac.kr/resources/15_cna_students.pdf · MATLAB functions: Polyfit and Polyval ents ... M-file: Newtint

HK Kim Slightly modified 3/1/09, 2/28/06

Firstly written at March 2005

DM21869/Computational Numerical Analysis/15_cna.doc Available at http://bml.pusan.ac.krBML/SME/PNU

4

Newton's divided-difference

Newton Interpolating Polynomial interpolating polynomial

Linear interpolation

sing similar triangles;

U

121 xxxx −−1211 )()()()( xfxfxfxf −

=−

hen we have the Newton linear-interpolating formula;

here f1(x) = first-order interpolating polynomial

• smaller interval → better approximation

T

w

le ------ ------------------------------------------------------------------------------ -------

Estimate the natural logarithm of 2 u linear interpolat

Examp ------------------ --- ----

sing ion. First, perform the computation by interpolating

etween and . Then, repeat the proced

.386294 at th is 0.6931472.

----------------------------------------------------------------------------------------------------------

b 01ln =

). Note th

791759.16ln =

e true value of

ure, but use a smaller interval from 1ln to ln 4

(1 2ln

----------------------------------

Graphical depiction of linear interpolation. The shaded areas indicate the similar triangles used to derive the Newton linear-interpolation formula.

Page 5: 15 Polynomial Interpolation - Welcome|Radiation …bml.pusan.ac.kr/resources/15_cna_students.pdf · MATLAB functions: Polyfit and Polyval ents ... M-file: Newtint

HK Kim Slightly modified 3/1/09, 2/28/06

Firstly written at March 2005

DM21869/Computational Numerical Analysis/15_cna.doc Available at http://bml.pusan.ac.krBML/SME/PNU

5

Q n

• three data points

dratic polynomial = second-order polyno ia

uadratic interpolatio

• qua m l = parabola

))(()()( 2131212 xxxxbxxbbxf −−+−+= → similar to the Taylor series expansion

considering the following coefficients

g points x1 and x2

→ second-order curvature

→ similar to the finite-divided-difference approximation

of the second derivative (see Chap. 4)

)( 11 xfb =

→ slope of the line connectin

Example --------------------------------------------------------------------------------------------------------------------

second-order Newton polynomial to estimate

------------------------------------------------------------------------------------------------------------------------------------------

Employ a 2 with the three points; 1ln , 4ln , and 6ln . ln

--

Page 6: 15 Polynomial Interpolation - Welcome|Radiation …bml.pusan.ac.kr/resources/15_cna_students.pdf · MATLAB functions: Polyfit and Polyval ents ... M-file: Newtint

HK Kim Slightly modified 3/1/09, 2/28/06

Firstly written at March 2005

DM21869/Computational Numerical Analysis/15_cna.doc Available at http://bml.pusan.ac.krBML/SME/PNU

6

General form of Newton's interpolating polynomials

eneralize to fit an (n – 1)th-order polynomial to n data points.

G

)())(()()( 1211211 −− −−−++−+= nnn xxxxxxbxxbbxf LL

Evaluate the coefficients with n data points; :

r vided differences;

• first finite divided difference

nbbb ,,, 21 L )](,[,)],(,[)],(,[ 2211 nn xfxxfxxfx L

],,,,[

],,[],[

)(

121

1233

122

11

xxxxfb

xxxfbxxfb

xfb

nnn K−=

===

M

whe e the bracketed functions are finite di

ji

jiji xx

xfxfxxf

−=

)()(],[

• second finite divided difference ki

kjjikji xx

xxfxxfxxxf

−−

=],[],[

],,[

• nth finite divided difference 1

12121121

],,,[],,,[],,,,[ xfxxxxf =Kxx

xxxfxx

n

nnnnnn −

− −−−−

KK

Then we have the general form of Newton's interpolating polynomial:

• not

• rec

],,[))((],[)()()( 1232112111 xxxfxxxxxxfxxxfxf n− −−+−+=

],,,,[)())(( 121121 xxxxfxxxxxx nnn KLL −−−−−++

necessary be equally-spaced data points as well as be in ascending order

ursive system: higher-order differences computed from lower-order differences

Page 7: 15 Polynomial Interpolation - Welcome|Radiation …bml.pusan.ac.kr/resources/15_cna_students.pdf · MATLAB functions: Polyfit and Polyval ents ... M-file: Newtint

HK Kim Slightly modified 3/1/09, 2/28/06

Firstly written at March 2005

DM21869/Computational Numerical Analysis/15_cna.doc Available at http://bml.pusan.ac.krBML/SME/PNU

7

Example --------------------------------------------------------------------- --------------------------------------------

mploy a third-order Newton's interpolating polynomial to

ol.)

---

E estimate with the four points; x1 = 1, x2 = 4, x3 = 6, 2ln

and x4 = 5.

S ))()(())(()()( 32142131213 xxxxxxbxxxxbxxbbxf −−−+−−+−+=

The first divided differences:

he second divided differences:

d differences:

Then we have

xi f(xi) First Second Third

T

The third divide

1 4 6 5

0 1.386294 1.791759 1.609438

0.4620981 0.2027326 0.1823216

-0.05187311 -0.02041100

0.007865529

------------------------------------------------------------------------------------------------------------------------------------------ --

Page 8: 15 Polynomial Interpolation - Welcome|Radiation …bml.pusan.ac.kr/resources/15_cna_students.pdf · MATLAB functions: Polyfit and Polyval ents ... M-file: Newtint

HK Kim Slightly modified 3/1/09, 2/28/06

Firstly written at March 2005

DM21869/Computational Numerical Analysis/15_cna.doc Available at http://bml.pusan.ac.kr BML/SME/PNU

8

MATLAB

>> format long

4 6 5]';

>> Newtint(x,y,2)

ans =

0.62876857890841

M-file: Newtint

<An M-file to implement Newton interpolation>

>> x = [1

>> y = log(x);

Page 9: 15 Polynomial Interpolation - Welcome|Radiation …bml.pusan.ac.kr/resources/15_cna_students.pdf · MATLAB functions: Polyfit and Polyval ents ... M-file: Newtint

HK Kim Slightly modified 3/1/09, 2/28/06

Firstly written at March 2005

DM21869/Computational Numerical Analysis/15_cna.doc Available at http://bml.pusan.ac.krBML/SME/PNU

9

Lagrange Interpolating Polynomial

Suppose we formulate a linear interpolating polynomial as the weighted average of the two values:

)()()( 2211 xfLxfLxf +=

where the L's are the weighting coefficients (straight lines).

• L1 should be equal to 1 at x1 and 0 at x2 →

• L2 should be equal to 0 at x1 and 1 at x2 →

Then we have

: linear Lagrange interpolating polynomial

Extending to the three points → second-order Lagrange interpolating polynomial

)())((

))(()(

))(())((

)())((

))(()( 3

2313

212

3212

311

3121

322 xf

xxxxxxxx

xfxxxx

xxxxxf

xxxxxxxx

xf−−−−

+−−−−

+−−−−

=

• three parabolas with each one passing through one the points and equaling zero at the other two

• unique parabola by the sum of three parabolas

A visual depiction of the rationale behind Lagrange interpolating polynomials. The figure shows the first-order case. Each of the two terms of the above equation passes through one of the points and is zero at the other. The summation of the two terms must, therefore, be the unique straight line that connects the two points.

Page 10: 15 Polynomial Interpolation - Welcome|Radiation …bml.pusan.ac.kr/resources/15_cna_students.pdf · MATLAB functions: Polyfit and Polyval ents ... M-file: Newtint

HK Kim Slightly modified 3/1/09, 2/28/06

Firstly written at March 2005

DM21869/Computational Numerical Analysis/15_cna.doc Available at http://bml.pusan.ac.krBML/SME/PNU

10

Let us extend to higher-order Lagrange polynomials (generalization):

∑=

− =n

iiin xfxLxf

11 )()()(

where

n = the number of data points

Example --------------------------------------------------------------------------------------------------------------------

Use a Lagrange interpolating polynomial of the first and second order to evaluate the density of unused motor oil at

T = 15 °C based on the following data:

ol.)

------------------------------------------------------------------------------------------------------------------------------------------

212.0)( 40800.0)( 2085.3)( 0

33

22

11

======

xfxxfxxfx

S

--

Page 11: 15 Polynomial Interpolation - Welcome|Radiation …bml.pusan.ac.kr/resources/15_cna_students.pdf · MATLAB functions: Polyfit and Polyval ents ... M-file: Newtint

HK Kim Slightly modified 3/1/09, 2/28/06

Firstly written at March 2005

DM21869/Computational Numerical Analysis/15_cna.doc Available at http://bml.pusan.ac.krBML/SME/PNU

11

MATLAB M-file: Lagrange

<An M-file to implement Lagrange interpolation>

>> format long

>> T = [-40 0 20 50]';

>> d = [1.52 1.29 1.2 1.09]';

>> deinsity = Lagrange(T,d,15)

density =

1.22112847222222

Page 12: 15 Polynomial Interpolation - Welcome|Radiation …bml.pusan.ac.kr/resources/15_cna_students.pdf · MATLAB functions: Polyfit and Polyval ents ... M-file: Newtint

HK Kim Slightly modified 3/1/09, 2/28/06

Firstly written at March 2005

DM21869/Computational Numerical Analysis/15_cna.doc Available at http://bml.pusan.ac.krBML/SME/PNU

12

Inverse Interpolation

x 1 2 3 4 5 6 7

f(x) 1 0.5 0.3333 0.25 0.2 0.1667 0.1429

Determine the value of x that corresponded to f(x) = 0.3

→ f(x) = 1/x ∴ x = 1/0.3 = 3.3333

→ inverse interpolation: to determine the corresponding value of x when you are given a value of f(x)

f(x) → new x 1 0.5 0.3333 0.25 0.2 0.1667 0.1429

x → new f(x) 1 2 3 4 5 6 7

→ nonuniform spacing on the abscissa

→ leading to oscillations! (ill-conditioned polynomial fitting)

• alternative strategy

- evenly spaced x's in most cases

- to fit an nth-order interpolating polynomial, fn(x)

- to find the value of x that makes this polynomial equal to the given f(x)

→ root-location methods (see Chaps. 5 or 6)

For the three points: (2, 0.5), (3, 0.3333), and (4, 0.25), applying a quadratic polynomial:

08333.1375.0041667.0)( 22 +−= xxxf

→ 08333.1375.0041667.03.0 2 +−= xx

→ 295842.3704158.5

)041667.0(278333.0)041667.0(4)375.0(375.0 2

=−−±

=x

Page 13: 15 Polynomial Interpolation - Welcome|Radiation …bml.pusan.ac.kr/resources/15_cna_students.pdf · MATLAB functions: Polyfit and Polyval ents ... M-file: Newtint

HK Kim Slightly modified 3/1/09, 2/28/06

Firstly written at March 2005

DM21869/Computational Numerical Analysis/15_cna.doc Available at http://bml.pusan.ac.krBML/SME/PNU

13

Extrapolation and Oscillations

Extrapolation

• the process of estimating a value of f(x) that lies outside the range of the known base points, nxxx ,,, 21 K

• ease divergence from the prediction with the true curve

Example --------------------------------------------------------------------------------------------------------------------

he population in millions of the United States from 1920 to 2000 can be tabulated as

T

Year 1920 1930 1940 1950 1960 1970 1980 1990 2000

Population 106.46 123.08 132.12 152.27 180.67 205.05 227.23 249.46 281.42

Fit a seventh-order polynomial to the first 8 points (1920 to 1990). Use it to compute the population in 2000 by

xtrapolation and compare your prediction with the actual result.

l.)

.12 152.27 180.67 205.05 227.23 249.46];

>> p = polyfit(t, pop, 7)

e

So

>> t = [1920:10:1990];

>> pop = [106.46 123.08 132

Illustration of the possible divergence of an extrapolated prediction. The extrapolation is based on fitting a parabola through the first three known points.

Page 14: 15 Polynomial Interpolation - Welcome|Radiation …bml.pusan.ac.kr/resources/15_cna_students.pdf · MATLAB functions: Polyfit and Polyval ents ... M-file: Newtint

HK Kim Slightly modified 3/1/09, 2/28/06

Firstly written at March 2005

DM21869/Computational Numerical Analysis/15_cna.doc Available at http://bml.pusan.ac.krBML/SME/PNU

14

r ng

y centering

POLYFIT.

>> ts = (t – 1955)/35;

>> p = polyfit(ts, pop, 7)

l(p, (2000 – 1955)/35)

175.0800

/35);

------------------------------------------------------------------------------------------------------------------------------------------

Oscillations

→ higher-order polynomials tend to be very ill-conditioned (highly sensitive to round-off error)

Wa ni : Polynomial is badly conditioned. Remove repeated data

points or tr and scaling as described in HELP

>> polyva

ans =

>> tt = linspace(1920,2000);

>> pp = polyval(p, (tt-1955)

>> plot(t,pop,'o',tt,pp)

--

• "more is better"?

→ not true for polynomial interpolation

Example --------------------------------------------------------------------------------------------------------------------

1901, Carl Runge ublished a study on the dangers of higher-order polynomial interpolation. He looked at the

following simple-looking function:

In p

22511)(

xxf

+=

which is now called Runge's function. He took equidistantly spaced data points from this function over the interval

[-1, 1]. He then used interpolating polynomials of increasing order and found that as he took more points, the

polynomials and the original curve differed considerably. Further, the situation deteriorated greatly as the order was

increased. Duplicate Runge's result by using the polyfit and polyval functions to fit fourth- and tenth-order

olynomials to 5 and 11 equally spaced points generated with the above equation. Create plots of your results along

e sampled values and the complete Runge's function.

p

with th

Page 15: 15 Polynomial Interpolation - Welcome|Radiation …bml.pusan.ac.kr/resources/15_cna_students.pdf · MATLAB functions: Polyfit and Polyval ents ... M-file: Newtint

HK Kim Slightly modified 3/1/09, 2/28/06

Firstly written at March 2005

DM21869/Computational Numerical Analysis/15_cna.doc Available at http://bml.pusan.ac.kr

BML/SME/PNU

15

l.)

>> x = linspace(-1,1,5);

→ automatically creates 100 points

>> p = polyfit(x,y,4);

>> yr = 1./(1+25*xx.^2);

,'--')

>> x = linspace(-1,1,11);

>> y10 = polyval(p,xx);

>>

---- --------------------------------------------------------

• worse at the ends of the interval with higher-order polynomial

• avoid the use of higher-order polynomials in most engineering and scientific contexts

So

>> y = 1./(1+25*x.^2);

>> xx = linspace(-1,1);

>> y4 = polyval(p,xx);

>> plot(x,y,'o',xx,y4,xx,yr

>> y = 1./(1+25*x.^2);

>> p = polyfit(x,y,10);

plot(x,y,'o',xx,y10,xx,yr,'--')

-- ------------------------------------------------------------------------------