L8 CurveFitting(Interpolation)
L8 CurveFitting(Interpolation)
NUMERICAL METHODS
Curve Fitting
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
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 )
( 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 )
( 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 ,......, xn1 , y n1 , 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
Rn f n 1 ( x ) f n ( x ) f n 1 ( x ) f n ( x ) Rn
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
x x1 x x0
f1 ( x) f ( x0 ) f ( x1 )
x0 x1 x1 x0
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.
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) ?
f ( x) a0 a1 x a2 x an x
2 n
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.
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 ,......, xn1 , y n1 , xn , y n data points
• Fitting linear splines to the data involves connecting two
neighboring points with a straight lines.
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
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 x9
f (5) 1.3
Solution
Quadratic Splines
Given (n+1) x0 , y0 , x1 , y1 ,......, xn1 , y n1 , xn , y n data points
•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
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
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 x9
f (5) 0.66
Solution
Cubic Splines
Given (n+1) x0 , y0 , x1 , y1 ,......, xn1 , y n1 , xn , y n
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
6a1 x0 2b1 0
6an xn 2bn 0
• 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 x=5, we will use the polynomial for the 2nd interval and
f(5)=1.1029