Chapter 4
Chapter 4
Curve Fitting
Introduction: Many times, Data is often given for discrete values along a range. However, you may require
estimates at points between the discrete values. In addition, you may require a simplified version of a complicated
function. To do this, a simpler function may be derived to fit these values. Such process is called Curve fitting.
There are two general approaches for curve fitting that are distinguished from each other on the basis of the amount of
error associated with the data.
First, where the data exhibits a significant degree of error, the strategy is to derive a single curve that represents the
general trend of the data, not necessarily passing through the data points. This approach is called Least-Squares
regression.
Second, where the data is known to be very precise, the basic approach is to fit a curve or a series of curves that pass
directly through each of the data points. Such approach is called Interpolation.
4.2 Interpolation
Suppose we are given n+1 discrete points such as ( x 0 , y 0 ) , ( x 1 , y 1 ) , . .. .. . , ( x n−1 , y n−1 ) , ( x n , y n ) . So, how then does
one find the value of y at any other value of x ? Well, 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.
Of course, if x falls outside the range of x for which the data is given, it is no longer interpolation but instead is
called extrapolation.
So what kind of function f ( x ) should one choose? A polynomial is a common choice for an interpolating function
because polynomials are easy to
(A) evaluate,
(B) differentiate, and
(C) integrate relative to other choices such as a trigonometric and exponential series.
Polynomial interpolation involves finding a polynomial of order n that passes through the n+1 points.
To illustrate this method, linear and quadratic interpolation is presented first. Then, the general form of Newton’s
divided difference polynomial method is presented. To illustrate the general form, cubic interpolation is shown in
Figure 1.
x3 , y3
x1, y1
f x
x2 , y2
x0 , y0
x
Figure 1 Interpolation of discrete data.
Linear Interpolation
Given
( x 0 , y 0 ) and ( x 1 , y 1 ), fit a linear interpolant through the data. Noting y=f ( x ) and y 1 =f ( x 1 ) , assume the
linear interpolant f 1 ( x ) is given by (Figure 2)
f 1 ( x )=b0 +b1 ( x− x0 )
Since at
x=x 0 ,
f 1 ( x 0 )=f ( x 0 )=b0 +b1 ( x 0−x 0 )=b0
and at
x=x 1 ,
f 1 ( x 1 )=f (x 1 )=b 0 +b1 ( x 1 −x 0 )
=f ( x 0 )+b1 ( x1 −x 0 )
giving
f ( x 1 )−f ( x 0 )
b 1=
x 1−x 0
So
b 0=f ( x 0 )
f ( x 1 )−f ( x 0 )
b 1=
x 1−x 0
Gambella University Chapter 4, Curve Fitting (Interpolation)
2 of 33
giving the linear interpolant as
f 1 ( x )=b0 + b1 ( x− x0 )
f ( x 1 )−f ( x0 )
f 1 ( x )=f ( x 0 )+ ( x−x 0 )
x 1 −x 0
x1 , y1
f1 x
x0 , y0
x
Figure 2 Linear interpolation.
Example 1
The upward velocity of a rocket is given as a function of time in Table 1 (Figure 3).
Determine the value of the velocity at t =16 seconds using first order polynomial interpolation by Newton’s divided
difference polynomial method.
Solution
For linear interpolation, the velocity is given by
v (t )=b0 +b1 (t−t 0 )
Since we want to find the velocity at t =16 , and we are using a first order polynomial, we need to choose the two
data points that are closest to t =16 that also bracket t =16 to evaluate it. The two points are t =15 and t =20 .
Then
t 0 =15 , v (t 0 )=362 . 78
t 1=20 , v (t 1 )=517 . 35
gives
b 0=v (t 0 )
=362 . 78
Figure 3 Graph of velocity vs. time data for the rocket example.
Hence
v (t )=b0 +b1 (t−t 0 )
=362 . 78+30 .914 (t−15 ), 15≤t ≤20
At t=16 ,
v (16 )=362. 78+30 . 914(16−15)
=393 . 69 m/s
If we expand
v (t )=362. 78+30 . 914 (t−15 ), 15≤t ≤20
we get
v (t )=−100 . 93+30 .914 t , 15≤t ≤20
and this is the same expression as obtained in the direct method.
Quadratic Interpolation
Given ( x 0 , y 0 ), ( x 1 , y 1 ), and ( x 2 , y 2 ), fit a quadratic interpolant through the data. Noting y=f ( x ), y 0 =f ( x 0 ),
y 1 =f ( x 1 ), and y 2 =f ( x 2 ) , assume the quadratic interpolant f 2 ( x ) is given by
f 2 ( x )=b0 +b1 (x −x 0 )+b2 ( x−x 0 )( x−x 1 )
At
x=x 0 ,
f 2 ( x 0 )=f ( x 0 )=b 0 +b 1 ( x 0− x0 )+b 2 ( x 0 −x 0 )( x0 −x 1 )
=b0
b 0=f ( x 0 )
At
x=x 1
f 2 ( x 1 )=f ( x1 )=b0 +b1 ( x 1 −x 0 )+b2 ( x 1 −x 0 )(x 1 −x 1 )
x1 , y1
x2 , y2
f 2 x
x0 , y0
x
Figure 4 Quadratic interpolation.
Example 2
The upward velocity of a rocket is given as a function of time in Table 2.
x2 f x2 f x3 , x2 , x1
f x3 , x 2
x3 f x3
Example 3
The upward velocity of a rocket is given as a function of time in Table 3.
a) Determine the value of the velocity at t =16 seconds with third order polynomial interpolation using Newton’s
divided difference polynomial method.
b) Using the third order polynomial interpolant for velocity, find the distance covered by the rocket from t=11 s to
t=16 s .
c) Using the third order polynomial interpolant for velocity, find the acceleration of the rocket at t=16 s .
Solution
a) For a third order polynomial, the velocity is given by
v (t )=b0 +b1 (t−t 0 )+b 2 (t−t 0 )(t−t 1 )+b 3 (t−t 0 )(t−t 1 )(t−t 2 )
Since we want to find the velocity at t=16 , and we are using a third order polynomial, we need to choose the four
t =10 , t 1=15 ,
data points that are closest to t =16 that also bracket t =16 to evaluate it. The four data points are 0
t 2=20 , and t 3=22. 5 .
Then
t 0 =10 , v (t 0 )=227 . 04
t 1=15 , v (t 1 )=362 .78
t 2=20 , v (t 2 )=517 . 35
t 3=22. 5 , v (t 3 )=602 . 97
gives
b 0=v [ t 0 ]
[ ]
16
t2 t3 t4
= −4 . 2541 t+21 . 265 +0 .13204 +0 .0054347
2 3 4 11
=1605 m
c) The acceleration at t =16 is given by
d
a (16)= v (t )|t=16
dt
d
a (t )= v (t )
dt
d
= (−4 . 2541+21. 265 t+0. 13204 t 2 +0 . 0054347 t 3 )
dt
=21 . 265+0 . 26408t +0 . 016304 t 2
a (16)=21. 265+0 . 26408(16 )+0 .016304 (16 )2
=29 . 664 m/s2
x3 , y3
x1, y1
f x
x2 , y2
x0 , y0
x
¿ j≠i ¿ ¿ ¿ ¿
n
Li ( x ) is a weighting function that includes a product of n−1 terms with terms of j =i omitted. The application of
Lagrangian interpolation will be clarified using an example.
Example 1
The upward velocity of a rocket is given as a function of time in Table 1.
Table 1 Velocity as a function of time.
t
(s)
v (t ) (m/s)
0 0
10 227.04
15 362.78
20 517.35
22.5 602.97
30 901.67
x1 , y1
f1 x
x0 , y0
x
Figure 3 Linear interpolation.
Since we want to find the velocity at t =16 , and we are using a first order polynomial, we need to choose the two
t =15 and t 1=20 .
data points that are closest to t =16 that also bracket t =16 to evaluate it. The two points are 0
Then
¿ j≠0 ¿¿¿ ¿
1
t −t 1
=
t 0 −t 1
¿ j≠1 ¿¿¿ ¿
1
t −t 0
=
t 1−t 0
Hence
t−t 1 t−t 0
v (t )= v (t 0 )+ v (t 1 )
t 0 −t 1 t 1 −t 0
t−20 t−15
= (362 . 78)+ (517 . 35), 15≤t≤20
15−20 20−15
16−20 16−15
v (16 )= (362. 78)+ (517 . 35 )
15−20 20−15
=0. 8(362.78)+0 . 2(517.35)
=393 . 69 m/s
You can see that 0
L (t )=0 . 8 and L1 (t )=0 .2 are like weight ages given to the velocities at t =15 and t =20 to
calculate the velocity at t =16 .
Quadratic Interpolation
y
x1 , y1
x2 , y2
f 2 x
x0 , y0
x
Figure 4 Quadratic interpolation.
Example 2
The upward velocity of a rocket is given as a function of time in Table 2.
a) Determine the value of the velocity at t =16 seconds with second order polynomial interpolation using Lagrangian
polynomial interpolation.
b) Find the absolute relative approximate error for the second order polynomial approximation.
Solution
a) For second order polynomial interpolation (also called quadratic interpolation), the velocity is given by
2
v (t )=∑ Li (t )v (t i )
i=0
=L0 (t )v (t 0 )+L1 (t )v (t 1 )+L2 (t )v (t 2 )
Since we want to find the velocity at t =16 , and we are using a second order polynomial, we need to choose the three
data points that are closest to t =16 that also bracket t =16 to evaluate it. The three points are
t 0 =10 , t1 =15 , and t 2 =20 .
Then
t 0 =10 , v ( t 0 )=227 . 04 t 1=15 , v ( t 1 ) =362. 78 t 2=20 , v ( t 2 ) =517 . 35
gives
¿ j≠0 ¿¿¿ ¿
2
( )( )
t −t 1 t −t 2
=
t 0 −t 1 t 0 −t 2
¿ j≠1 ¿¿¿ ¿
2
( )( )
t −t 0 t −t 2
=
t 1 −t 0 t 1 −t 2
¿ j≠2 ¿¿¿ ¿
2
( )( )
t −t 0 t −t 1
=
t 2 −t 0 t 2 −t 1
Hence
( )( ) ( )( ) ( )( )
t −t 1 t−t 2 t−t 0 t−t 2 t−t 0 t −t 1
v ( t )= v ( t 0 )+ v ( t 1 )+ v ( t 2 ) , t 0≤t ≤t 2
t 0 −t 1 t 0 −t 2 t 1 −t 0 t 1−t 2 t 2 −t 0 t 2−t 1
(16−15)(16−20) (16−10 )(16−20)
v (16 )= (227 .04 )+ (362 .78 )
(10−15 )(10−20) (15−10 )(15−20 )
(16−10 )(16−15)
+ (517 . 35 )
(20−10 )(20−15 )
x3 , y3
x1, y1
f 3 x
x2 , y2
x0 , y0
Figure 5 Cubic interpolation. x
Since we want to find the velocity at t =16 , and we are using a third order polynomial, we need to choose the four
t =10 ,
data points closest to t =16 that also bracket t =16 to evaluate it. The four points are 0
t1 =15 , t 2=20 and
t 3=22. 5 . Then
¿ j≠0 ¿¿¿ ¿
3
( )( )( )
t −t 1 t −t 2 t −t 3
=
t 0 −t 1 t 0 −t 2 t 0−t 3
¿ j≠1 ¿¿¿ ¿
3
( )( )( )
t −t 0 t −t 2 t −t 3
=
t 1 −t 0 t 1 −t 2 t 1 −t 3
¿ j≠2 ¿¿¿ ¿
3
( )( )( )
t −t 0 t −t 1 t−t 3
=
t 2 −t 0 t 2 −t 1 t 2 −t 3
¿ j≠3 ¿¿¿ ¿
3
( )( )( )
t −t 0 t −t 1 t −t 2
=
t 3 −t 0 t 3 −t 1 t 3−t 2
Hence
( )( )( ) ( )( )( )
t−t 1 t −t 2 t −t 3 t −t 0 t −t 2 t −t 3
v ( t )= v (t 0 )+ v ( t 1)
t 0 −t 1 t 0 −t 2 t 0−t 3 t 1 −t 0 t 1 −t 2 t 1 −t 3
( )( )( ) ( )( )( )
t −t 0 t −t 1 t−t 3 t −t 0 t −t 1 t −t 2
+ v ( t 2 )+ v ( t 3 ) , t 0 ≤t ≤t 3
t 2 −t 0 t 2−t 1 t 2 −t 3 t 3−t 0 t 3 −t 1 t 3 −t 2
(16−15)(16−20)(16−22 .5 ) (16−10)(16−20 )(16−22 .5 )
v (16 )= (227 . 04 )+ (362 .78 )
(10−15 )(10−20)(10−22. 5) (15−10)(15−20)(15−22. 5 )
(16−10)(16−15 )(16−22 .5 )
+ (517 . 35)
(20−10)(20−15 )(20−22 .5 )
(16−10)(16−15 )(16−20 )
+ (602 . 97 )
(22 .5−10 )(22 . 5−15 )(22. 5−20)
=(−0. 0416)(227 . 04)+(0 . 832)(362 .78)+(0 . 312)(517 . 35)+(−0 . 1024)(602. 97 )
=392 . 06 m/s
b) The absolute percentage relative approximate error,
|∈ |
a for the value obtained for v (16 ) can be obtained by
comparing the result with that obtained using the second order polynomial (Example 2)
392.06−392.19
|∈a|=| |×100
392.06
=0. 033269 %
c) The distance covered by the rocket between t=11 s to t=16 s can be calculated from the interpolating
polynomial as
[ ]
16
t2 t3 t4
= −4 . 245 t+21. 265 +0 .13195 +0 .00544
2 3 4 11
=1605 m
d) The acceleration at t =16 is given by
d
a ( 16 )= v ( t )|t=16
dt
Given that
v (t )=−4 .245+21 . 265 t+0 . 13195 t 2 +0 .00544 t 3 , 10≤t ≤22. 5
d
a ( t )= v ( t )
dt
d
= (−4 . 245+21 .265 t+0 .13195 t 2 +0 . 00544 t 3 )
dt
=21 . 265+0 . 26390t +0 . 01632t 2 , 10≤t ≤22. 5
a (16)=21. 265+0 . 26390(16 )+0 .01632(16 )2 =29 . 665 m/s2
Note: There is no need to get the simplified third order polynomial expression to conduct the differentiation. An
expression of the form
( )( )( )
t −t 1 t−t 2 t −t 3
L0 ( t )=
t 0 −t 1 t 0 −t 2 t 0 −t 3
gives the derivative without expansion as
( )( ) ( )( ) ( )( )
d t −t 1 t −t 2 t −t 2 t −t 3 t −t 3 t −t 1
( L0 (t ) ) = t −t t −t + t −t t −t + t −t t −t
dt 0 1 0 2 0 2 0 3 0 3 0 1
x3 , y3
x1, y1
f x
x2 , y2
x0 , y0
x
Figure 1 Interpolation of discrete data.
Now through these six points, one can pass a fifth order polynomial
f 5 ( x )=3. 1378×10−11 x 5 +1 .2019 x 4 −3 . 3651×10−11 x 3 −1 .7308 x 2 +1 .0004×10−11 x+5 . 6731×10−1 ,
−1≤x ≤1
through the six data points. On plotting the fifth order polynomial (Figure 2) and the original function, one can see
that the two do not match well. One may consider choosing more points in the interval [–1, 1] to get a better match,
but it diverges even more (see Figure 3), where 20 equidistant points were chosen in the interval [–1, 1] to draw a 19th
order polynomial. In fact, Runge found that as the order of the polynomial becomes infinite, the polynomial diverges
in the interval of −1< x <−0 . 726 and 0 . 726< x <1 .
So what is the answer to using information from more data points, but at the same time keeping the function
true to the data behavior? The answer is in spline interpolation. The most common spline interpolations used are
linear, quadratic, and cubic splines.
0.8
0.4
y
0
-1 -0.5 0 0.5 1
-0.4
x
5th Order Polynomial Function 1/(1+25*x^2)
1.2
0.8
0.4
y
0
-1 -0.5 0 0.5 1
-0.4
x
5th Order Polynomial Function 1/(1+25*x^2)
(x3, y3)
(x1, y1)
(x2, y2)
(x0, y0)
x
Figure 4 Linear splines.
f ( x 1 )−f ( x 0 )
f ( x )=f ( x 0 )+ ( x −x 0 ) ,
x 1 −x0 x 0 ≤x≤x 1
f ( x 2 )−f ( x 1 )
=f ( x 1 )+ ( x−x 1 ) ,
x 2 −x1 x 1≤x ≤x 2
.
.
.
f ( x n )−f ( x n−1 )
=f ( x n−1 )+ (x −x n−1 ),
x n−x n−1 x n−1 ≤x≤x n
Note the terms of
f ( x i )−f ( xi−1 )
xi −x i−1
x x
in the above function are simply slopes between i−1 and i .
Example 1
The upward velocity of a rocket is given as a function of time in Table 2 (Figure 5).
Table 2 Velocity as a
function of time.
t (s) v (t ) (m/s)
0 0
10 227.04
15 362.78
20 517.35
22.5 602.97
30 901.67
GambellaFigure
University
5 Graph of velocity vs. time data for theChapter
rocket 4, Curve Fitting (Interpolation)
example.
20 of 33
Determine the value of the velocity at t =16 seconds using linear splines.
Solution
Since we want to evaluate the velocity at t =16 , and we are using linear splines, we need to choose the two data
t =15 and t 1=20 .
points closest to t =16 that also bracket t =16 to evaluate it. The two points are 0
Then
t 0 =15 , v (t 0 )=362 . 78
t 1=20 , v (t 1 )=517 . 35
gives
v (t 1 )−v (t 0 )
v (t )=v (t 0 )+ (t−t 0 )
t 1−t 0
517 .35−362 .78
=362 . 78+ (t−15 )
20−15
=362. 78+30 . 913(t−15 ) , 15≤t ≤20
At t=16 ,
v (16 )=362. 78+30 . 913(16−15 )
=393.7 m/s
Linear spline interpolation is no different from linear polynomial interpolation. Linear splines still use data only from
the two consecutive data points. Also at the interior points of the data, the slope changes abruptly. This means that the
first derivative is not continuous at these points. So how do we improve on this? We can do so by using quadratic
splines.
Quadratic Splines
In these splines, a quadratic polynomial approximates the data between two consecutive data points. Given
( x 0 , y 0 ) , ( x 1 , y 1 ) , .. .. . ., ( x n−1 , y n−1) , ( x n , y n ) , fit quadratic splines through the data. The splines are given by
2
f ( x )=a 1 x +b1 x +c 1 , x 0 ≤x≤x 1
=a2 x 2 +b 2 x+ c 2 , x 1≤x ≤x 2
.
.
.
=an x 2 + bn x +c n , x n−1 ≤x≤x n
So how does one find the coefficients of these quadratic splines? There are 3 n such coefficients
a i , i=1,2,.....,n
b i , i=1,2,.....,n
a) Determine the value of the velocity at t =16 seconds using quadratic splines.
b) Using the quadratic splines as velocity functions, find the distance covered by the rocket from t=11 s to
t=16 s .
c) Using the quadratic splines as velocity functions, find the acceleration of the rocket at t=16 s .
d)
Solution
a) Since there are six data points, five quadratic splines pass through them.
v (t )=a1 t 2 + b1 t+ c1 , 0≤t ≤10
=a2 t 2 +b 2 t +c 2 , 10≤t ≤15
=a3 t 2 +b 3 t+ c3 , 15≤t ≤20
=a4 t 2 + b 4 t + c 4 , 20≤t ≤22. 5
=a5 t 2 +b 5 t+ c5 , 22 .5≤t ≤30
The equations are found as follows.
1. Each quadratic spline passes through two consecutive data points.
a 1 t 2 + b1 t + c 1 passes through t =0 and t =10 .
a 1 ( 0 )2 +b1 ( 0 )+ c 1=0 (1)
2
a 1 (10) +b1 (10)+c 1 =227 . 04 (2)
][ [ ]
(15)
Combining Equation (1) – (15) in matrix form gives
[
a1
0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 b1 0
100 10 1 0 0 0 0 0 0 0 0 0 0 0 0 c1 227 . 04
0 0 0 100 10 1 0 0 0 0 0 0 0 0 0 a2 227 . 04
0 0 0 225 15 1 0 0 0 0 0 0 0 0 0 362 . 78
b2
0 0 0 0 0 0 225 15 1 0 0 0 0 0 0 362 . 78
0 0 0 0 0 0 400 20 1 0 0 0 0 0 0 c2 517 . 35
0 0 0 0 0 0 0 0 0 400 20 1 0 0 0 a3 517 . 35
0 0 0 0 0 0 0 0 0 506 .25 22 .5 1 0 0 0 b3 = 602 . 97
0 0 0 0 0 0 0 0 0 0 0 0 506 .25 22. 5 1 c3 602 . 97
0 0 0 0 0 0 0 0 0 0 0 0 900 30 1
a4 901 . 67
20 1 0 −20 −1 0 0 0 0 0 0 0 0 0 0 0
b4
0 0 0 30 1 0 −30 −1 0 0 0 0 0 0 0 0
0 0 0 0 0 0 40 1 0 −40 −1 0 0 0 0 c4 0
0 0 0 0 0 0 0 0 0 45 1 0 −45 −1 0 a5 0
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 b5 0
c5
11 15
[ ]
3 2 15
t t
= 0 .8888 +4 . 928 +88 . 88 t
3 2 11
[ ]
16
t3 t2
+ −0 .1356 +35 . 66 −141. 61t
3 2 15
=1217 . 35+378 . 53
=1595 . 9 m
Objective: To find the nth degree polynomial P(x ) that passes through the n+1 data points. i.e P ( x i )= y i
( x−x 0 )
P1 ( x ) =[1+ △ ] y0
1!h
( x−x 0 ) ( x −x 0) ( x−x 1 ) 2
P2 ( x ) =[1+ △+ 2
△ ] y0
1!h 2!h
Activity: Drive Newton’s forward formulas from Newton’s divided difference methods for constant step size h
( x−x n )
P1 ( x ) =[1+ ∇ ] yn
1!h
Find a polynomial of degree three which coincides or fits at the given data values using Newton’s forward
and backward formula.
Determine the value of the velocity at t =7 seconds using the Newton’s forward and backward method of
interpolation of first and second order polynomial.