0% found this document useful (0 votes)
17 views

L8 CurveFitting(Interpolation)

The document covers numerical methods focused on curve fitting and interpolation, detailing techniques such as polynomial interpolation, Newton's divided difference, and Lagrange interpolating polynomials. It explains the concepts of interpolation and extrapolation, providing methods for estimating function values between and beyond data points. Additionally, it discusses error estimation in interpolating polynomials and includes examples to illustrate the application of these methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

L8 CurveFitting(Interpolation)

The document covers numerical methods focused on curve fitting and interpolation, detailing techniques such as polynomial interpolation, Newton's divided difference, and Lagrange interpolating polynomials. It explains the concepts of interpolation and extrapolation, providing methods for estimating function values between and beyond data points. Additionally, it discusses error estimation in interpolating polynomials and includes examples to illustrate the application of these methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 60

MAT 202 E

NUMERICAL METHODS

Curve Fitting
Interpolation

“These notes are only to be used in class presentations”

Textbook: Numerical Methods for Engineers, S.C. Chapra, R.P. Canale,


7th edition, 2015
Interpolation
• Estimation of a function value at an intermediate point that lie
between precise data points.

• A continuous function f  x  may be used to represent the ‘n+1’ data


values with f  x  passing through the ‘n+1’ points. Then one can
find the value of y at any other value of x. This is called
interpolation.

• If 'x' falls outside the range of 'x' for which the data is given, it is
no longer interpolation but instead is called extrapolation.
Interpolation & Extrapolation

•Interpolation:
Data to be found are within the
range of observed data.

•Extrapolation:
Data to be found are beyond the
range of observation. (not reliable)
•Polynomial Interpolation
–Newton Interpolation
–Lagrange Interpolation

•Spline Interpolation
–Quadratic Spline
–Cubic Spline
Polynomial interpolation

• Polynomial interpolation involves finding a polynomial of order ‘n’


that passes through the ‘n+1’ points.
• For (n+1) data points there is only one polynomial of order n that
passes through all the points.

f ( x)  a0  a1 x  a2 x 2    an x n

• nth order polynomial has n+1 coefficients and these coefficients can
be uniquely determined by n+1 points.
Newton’s divided difference polynomial method

Linear Interpolation

• Given ( x0 , y0 ), ( x1, y1 )
fit a straight line a through two
data points

f1 ( x )  b0  b1 ( x  x0 )
• f1 (x) : a first-order interpolating
polynomial.
At x=x0
f ( x0 )  b0  b1 ( x0  x0 )  f ( x0 )  b0
At x=x1
f ( x1 )  b0  b1 ( x1  x0 )  f ( x0 )  b1 ( x1  x0 )
f ( x1 )  f ( x0 )
b1 
x1  x0
f ( x1 )  f ( x0 )
f ( x )  f ( x0 )   x  x0 
x1  x0

f ( x1 )  f ( x0 )
 f x1 , x0  1st finite divided difference
x1  x0

f1 ( x )  f ( x0 )  f x1, x0 x  x0 
Quadratic Interpolation

• Given ( x0 , y0 ), ( x1 , y1 ), ( x2 , y2 )
fit a parabola through three
data points

f 2 ( x )  b0  b1 ( x  x0 )  b2 ( x  x0 )( x  x1 )

• f2 (x) : a second-order interpolating polynomial.


x  x0 b0  f ( x0 )
f ( x1 )  f ( x0 )
x  x1 b1 
x1  x0
f ( x2 )  f ( x1 ) f ( x1 )  f ( x0 )

x2  x1 x1  x0
x  x2 b2 
x 2  x0
f [ x2 , x1 ]  f [ x1 , x0 ]
b2   f [ x2 , x1 , x0 ] 2nd divided difference
x2  x0

f 2 ( x )  f ( x0 )  f x1, x0 x  x0   f x2 , x1, x0 ( x  x0 )( x  x1 )


f ( x1 )  f ( x0 )
b0  f ( x0 ) b1 
x1  x0
f ( x1 )  f ( x0 )
f ( x2 )  f ( x0 )  ( x2  x0 )
f ( x2 )  f ( x0 )  b1 ( x2  x0 ) x1  x0
x  x2 b2  b2 
( x2  x0 )( x2  x1 ) ( x2  x0 )( x2  x1 )

( f ( x2 )  f ( x1 )  f ( x1 )  f ( x0 ))( x2  x1 ) ( f ( x1 )  f ( x0 ))( x2  x1  x1  x0 )

( x2  x1 ) ( x1  x0 )
x  x2 b2 
( x2  x0 )( x2  x1 )

( f ( x2 )  f ( x1 ))( x2  x1 ) ( f ( x1 )  f ( x0 ))( x2  x1 )  ( f ( x1 )  f ( x0 ))( x1  x0 )


 ( f ( x1 )  f ( x0 )) 
( x2  x1 ) ( x1  x0 )
b2 
( x2  x0 )( x2  x1 )

( f ( x2 )  f ( x1 ))( x2  x1 ) ( f ( x1 )  f ( x0 ))( x2  x1 )

( x2  x1 ) ( x1  x0 )
b2 
( x2  x0 )( x2  x1 )
f ( x2 )  f ( x1 ) f ( x1 )  f ( x0 )

( x2  x1 ) ( x1  x0 )
b2 
( x2  x0 )
General Form of Newton’s Interpolating Polynomials

Given (n+1) x0 , y0 , x1 , y1 ,......, xn1 , y n1 , xn , y n  data points

f [ xi , xi 1,  x1 ]  f [ xi 1, xi  2 ,  x0 ]
bi  f [ xi , xi 1,  x0 ] 
xi  x0
xi f(xi) 1st div. diff. 2nd div. diff. 3rd div. diff.

b0
x0 f ( x0 ) b1
f [ x1 , x 0 ] b2
x1 f ( x1 ) f [ x 2 , x1 , x 0 ] b3
f [ x 2 , x1 ] f [ x3 , x2 , x1 , x0 ]
x2 f ( x2 ) f [ x3 , x 2 , x1 ]
f [ x3 , x 2 ]
x3 f ( x3 )
Newton’s divided difference interpolating polynomial

f n ( x )  f ( x0 )  f x1 , x0  x  x0   f x2 , x1 , x0 ( x  x0 )( x  x1 )  
 f xn , xn 1 ,, x0 ( x  x0 )( x  x1 )( x  xn 1 )

n n 1
f n ( x)   f [ x j , x j 1 ,  x0 ] ( x  x j )
j 0 j 0
Example: Calculate f(7) using Newton’s interpolating polynomial

x 1 2 4 8
f(x) 1 3 7 11
xi f(xi) 1st div. diff 2nd div. diff 3rd div. diff

1 1
2
2 3 0
2 -0.0238
4 7 -0.1667
1
8 11

f 3 ( x)  1  2( x  1)  0.0238( x  1)( x  2)( x  4)


f 3 (7)  10.8571
Error Estimation in Newton’s Interpolating Polynomials
• Structure of interpolating polynomials is similar to the Taylor series
expansion, i.e. finite divided differences are added sequentially to
capture the higher order derivatives.

• For an nth-order interpolating polynomial, an analogous relationship for


the error is:

Rn  f n 1 ( x )  f n ( x )  f n 1 ( x )  f n ( x )  Rn

• If an additional point (xn+1 , f(xn+1)) is available, then

Rn  f [ xn1 , xn , xn1 ,, x0 ]( x  x0 )( x  x1 )( x  xn )


• This result is based on the assumption that the series is strongly convergent. i.e.
(n+1)th-order prediction is closer to the true value than the nth-order prediction.
Lets add another data point to the previous example and estimate R3

x 1 2 4 8 9
f(x) 1 3 7 11 12
xi f(xi) 1st div. diff 2nd div. diff 3rd div. diff 4th div. diff

1 1
2
2 3 0
2 -0.0238
4 7 -0.1667 0.00595
1 0.0238
8 11 0
1
9 12

f 4 ( x)  1  2( x  1)  0.0238( x  1)( x  2)( x  4)  0.00595( x  1)( x  2)( x  4)( x  8)


f 4 (7)  10.3214
R3  f 4 (7)  f3 (7)  0.5357
Lagrange Interpolating Polynomials
• The Lagrange interpolating polynomial is simply a
reformulation of the Newton’s polynomial that avoids the
computation of divided differences:

• nth order polynomial: interpolate n+1 points


n
f n ( x)   Li ( x) f ( xi ) •The Lagrangian coefficient, Li(x) will be
i 0 1 at x=xi and will be 0 all other data
points.
•Thus, fn(xi)=f(xi) for all i, meaning that
n x  xj the Lagrangian polynomial passes through
Li ( x)   all the data points as expected.
j 0 xi  x j
j i
Linear polynomial: interpolate 2 points

x  x1 x  x0
f1 ( x)  f ( x0 )  f ( x1 )
x0  x1 x1  x0

2nd order polynomial: interpolate 3 points

f 2 ( x) 
 x  x1 x  x2 
f ( x0 )
x0  x1 x0  x 2 

 x  x0 x  x2 
f ( x1 )
x1  x0 x1  x 2 

x  x0 x  x1  f ( x )
x2  x0 x2  x1  2
A visual depiction of the rationale behind
the Lagrange polynomial . The figure
shows a second order case:

f 2 ( x) 
 x  x1  x  x2 
f ( x0 )
x0  x1 x0  x 2 


x  x0 x  x2  f ( x )
x1  x0 x1  x 2  1


x  x0 x  x1  f ( x )
x2  x0 x2  x1  2
Each of the three terms passes through
one of the data points and zero at the
other two. The summation of the three
terms must, therefore, be unique second
order polynomial f2(x) that passes exactly
through three points.
• The forms of the Lagrangian and Newton polynomials look
different, but they are actually equivalent if the data points are the
same.
• The Lagrangian coefficients are easier to calculate since the
divided difference computation is not required.
• However, if you are adding new data points, the Newton method
is more efficient since the earlier coefficients remain the same.
Example: Calculate f(7) using Lagrange interpolating polynomial

x 1 2 4 8
f(x) 1 3 7 11

1 1 7 11
f 3 ( x)  ( x  2)( x  4)( x  8)  ( x  1)( x  4)( x  8)  ( x  1)( x  2)( x  8)  ( x  1)( x  2)( x  4)
21 4 24 168
f 3 (7)  10.8571
Solution
Coefficients of an Interpolating Polynomial
• Although both the Newton and Lagrange polynomials are well suited for
determining intermediate values between points, they do not provide a
polynomial in conventional form:
f ( x )  a0  a1 x  a2 x 2    an x n
• Since n+1 data points are required to determine n+1 coefficients,
simultaneous linear systems of equations can be used to calculate “a”s.

f ( x0 )  a0  a1 x0  a2 x02   an x0n 1 x0 x02  x0n   a0   f ( x0 ) 


    
1 x1 x12  x1n   a1   f ( x1 ) 
f ( x1 )  a0  a1 x1  a2 x12   an x1n
1 x2 x22  x2n  a2    f ( x2 ) 
     
          
f ( xn )  a0  a1 xn  a2 xn2   an xnn 1 xn2  xnn  an   f ( xn )
 xn

Where “x”s are the knowns and “a”s are the unknowns.
• However, this approach results in a highly unstable (ill-conditioned) system of
equations. Therefore, Newton or Lagrange interpolation is preferred.
Inverse Interpolation

Given (x0, f(x0)), (x1, f(x1)), (x2, f(x2)), …, (xn, f(xn)) and a
function value f(xk), how do you determine (xk) ?

1. Using one of the interpolation methods (Newton or


Lagrange), obtain the function f(x) :

f ( x)  a0  a1 x  a2 x    an x
2 n

2. Plug in the value of f(x) in the above equation and find


one of the real roots of the resultant equation using a root
finding method:

a0  a1 x  a2 x    an x  f ( xk )  0
2 n
Example: Given the data find x corresponding to f(x)=0.3

x 2 3 4
f(x) 0.5 0.3333 0.25

x  3.2958
Solution
Spline Interpolation
•There are cases where polynomials can lead to erroneous results
because of round off error and overshoot.

The function (black line) has a sharp


or sudden change at x = 0.
Polynomial interpolations work
poorly.
a) third order
b) fifth order
c) seventh order.
•An alternative approach is to apply lower order polynomials to
subsets of data pts. Such connecting polynomials are called spline
functions.

Linear spline
f ( x)  ax  b

Quadratic spline
f ( x)  ax 2  bx  c

Cubic spline
f ( x)  ax3  bx 2  cx  d
Linear Splines

Given (n+1) x0 , y0 , x1 , y1 ,......, xn1 , y n1 , xn , y n  data points
• Fitting linear splines to the data involves connecting two
neighboring points with a straight lines.

n+1 data points


n intervals
(n-1) interior points

• Data points where splines meet are called knots.


• Use either Lagrange or Newton interpolations to determine the
equations for the straight lines
f ( x1 )  f ( x 0 )
f ( x )  f ( x0 )  ( x  x 0 ), x 0  x  x1
x1  x 0

f ( x 2 )  f ( x1 )
 f ( x1 )  ( x  x1 ), x1  x  x 2
x2  x1

. f ( xi )  f ( xi 1 )
 f ( xi 1 ).  ( x  xi 1 ) xi 1  x  xi
xi  xi 1
.
f ( x n )  f ( x n 1 )
 f ( x n 1 )  ( x  x n 1 ), x n 1  x  x n
x n  x n 1

Note the terms of


f ( xi )  f ( x i1 )
xi  x i 1

in the above function are simply slopes between xi 1 and x i .


These equations can be used to evaluate the function at any
point between x0 and xn

• First locate the interval within which the point lies.


• Then use the appropriate equation to determine the
function value within the interval.

Shortcomings:
• Linear splines aren’t smooth, they don’t have continuous
derivatives. At the knots, the slopes change abrubtly so the 1st
derivative of the function is discontinuous.
Example: Fit the data with 1st order splines. Evaluate the function at
x=5.

x 3 4.5 7 9
f(x) 2.5 1.0 2.5 0.5

f ( x)   x  5.5 3  x  4.5
f ( x)  0.6 x  1.7 4.5  x  7
f ( x)   x  9.5 7 x9
f (5)  1.3
Solution
Quadratic Splines
Given (n+1) x0 , y0 , x1 , y1 ,......, xn1 , y n1 , xn , y n  data points

• Derive a 2nd order polynomial for each interval between data


points.
Quadratic splines
f ( x)  a1 x 2  b1 x  c1 x0  x  x1
 a2 x 2  b2 x  c2 x1  x  x2

 ai x  bi x  ci
2
xi 1  x  xi

 an x 2  bn x  cn xn 1  x  xn

•For n+1 points , there are two end points and n-1 interior
points. The number of intervals is n.
•Each interval has one quadratic polynomial, there are 3n
unknown coefficients (ai,bi & ci ) to be determined.
Conditions and Equations used to determine Coefficients

1. At each interior point, the two neighboring quadratic


polynomials have to pass this point, resulting in 2(n-1)
equations
At an interior point xi, i=1 to n-1, set values equal to data point

ai 1 xi  bi 1 xi  ci 1  f ( xi )
2

ai xi  bi xi  ci  f ( xi )
2
2. The first and last quadratics must pass through the end points
resulting in 2 more equations.
At end points, x0 and xn, set values equal to data point

a1 x0  b1 x0  c1  f ( x0 )
2

an xn  bn xn  cn  f ( xn )
2

3. At each interior point, the first-order derivatives of the two


neighboring polynomials are equal, resulting in (n-1) equations

At an interior point xi, i=1 to n-1, set derivatives equal

2ai 1 xi  bi 1  2ai xi  bi
4. The last equation is obtained by letting the second-order
derivative of the first polynomial equal zero (totally arbitrary
and may be changed).
Set second derivative to zero at first point

a1  0

Shortcomings:
• Straight line connecting the 1st two points
• The spline for the last interval seems to swing too high
Example: Fit the data with 2nd order splines. Evaluate the function at
x=5.
x 3 4.5 7 9
f(x) 2.5 1.0 2.5 0.5

f ( x)   x  5.5 3  x  4.5
f ( x)  0.64 x 2  6.76 x  18.46 4.5  x  7
f ( x)  1.6 x 2  24.6 x  91.3 7 x9
f (5)  0.66
Solution
Cubic Splines
Given (n+1) x0 , y0 , x1 , y1 ,......, xn1 , y n1 , xn , y n 

• Derive a 3rd order polynomial for each interval between data


points.
f ( x )  a1 x 3  b1 x 2  c1 x  d1 x0  x  x1
 a2 x 3  b2 x 2  c2 x  d 2 x1  x  x2

 ai x 3  bi x 2  ci x  d i xi 1  x  xi

 an x 3  bn x 2  cn x  d n xn 1  x  xn
•Each interval has one cubic polynomial, there are 4n unknown
coefficients (ai,bi , ci & di ) to be determined.
Conditions and Equations used to determine Coefficients

1. At each interior point, the two neighboring cubic polynomials


have to pass this point, resulting in 2(n-1) equations

At an interior point xi, i=1 to n-1, set values equal to data point

ai 1 xi  bi 1 xi  ci 1 xi  d i 1  f ( xi )
3 2

ai xi  bi xi  ci xi  d i  f ( xi )
3 2
2. The first and last cubics must pass through the end points
resulting in 2 more equations.
At end points, x0 and xn, set values equal to data point

a1x03  b1x0 2  c1x0  d1  f ( x0 )


3 2
an xn  bn xn  cn xn  d n  f ( xn )
3. At each interior point, the first-order and 2nd-order derivatives of
the two neighboring polynomials are equal, resulting in 2(n-1)
equations
At an interior point xi, i=1 to n-1, set derivatives equal

3ai 1 xi 2  2bi 1 xi  ci 1  3ai xi 2  2bi xi  ci


6ai 1 xi  2bi 1  6ai xi  2bi
4. The last equations are obtained by letting the second-order
derivative of the first and last polynomials equal zero .

Set second derivative to zero at first point and last point.

6a1 x0  2b1  0
6an xn  2bn  0

The function becomes a straight line at the ent points. It is called


a natural spline.
Alternative method
• Lagrange interpolating polynomial for second derivative

• Integrate twice to get f(x)


• Constants can be evaluated by applying interval end
conditions
• First derivatives at knots must be equal
• Rearranging terms we get the following relationship

• If the above equation is written for the interior knots, we have (n-1)
equations with (n+1) unknowns. For natural cubic spline, the 2nd
derivatives at the end knots are zero.
• This gives us n-1 equation with n-1 unknowns – the second
derivatives
• Once we solve for the second derivatives, we can plug it into the
previous equations to solve for the splines
Example: Fit the data with cubic splines. Evaluate the function at
x=5.

x 3 4.5 7 9
f(x) 2.5 1.0 2.5 0.5
At x=x1=4.5
At x=x2=7
Solve the system of equations to find the second derivatives
For the 1st interval:
Substituting for other intervals
For the second interval

For the third interval

For x=5, we will use the polynomial for the 2nd interval and
f(5)=1.1029

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy