Ch5-Interpolation & Curve Fitting
Ch5-Interpolation & Curve Fitting
CH V
5.1 Introduction
This is the simplest kind of interpolations. Suppose we are given a table of values:
x f (x)
----------------------------------
x0 f (x0)
x1 f (x1)
… ……
… ……
xn f (xn)
Numerical Analysis Ch V: Interpolation and Curve-Fitting
If we wish to find the value of f ( x̄ ) where x̄ is somewhere between x3 and x4. Using
linear interpolation, we draw a line between the two tabulated points f(x 3) and f(x4) as
shown in figure (5.3). We are approximating the curve in this region by a straight line.
Using the similar triangles, we form the proportion:
BC DE AC
= BC = DE
AC AE AE
x̄ − x 3
x4 − x3 [ 4
= f ( x )−f ( x 3 )]
x̄ − x 3
P ( x̄ ) = f ( x )int =f ( x3 ) + [ f ( x 4 ) − f ( x3 ) ]
x 4 − x3
In general, to find the value of f(x) for some x located between x i and xi+1 in the table
of given values, then the interpolated value p(x), which is only an approximation for
f(x), is given by:
x − xi
P ( x ) = f ( xi ) + [ f ( x i+1 ) − f ( x i ) ]
x i+1 − x i
Note that if x = xi then p(xi) = f(xi) and if x = xi+1 then p(xi+1) = f(xi+1)
Example 5.1
Sin 00 = 0.0
Sin 100 = 0.17365
Solution
6.5 − 0
P (6.5) = f (0 ) + [ f (10 ) − f (0 ) ]
10 −0
6.5
P (6.5) = 0.0+ [ 0.17365 − 0.0 ]
10
= 0.11287
Numerical Analysis Ch V: Interpolation and Curve-Fitting
This has a substantial error and if we want the best possible accuracy from such an
interpolation, we should pick two points xi and xi+1 which are close to the needed value
of x̄
For example, if we pick the two values at 60 and 70, we get:
Sin 60 = 0.10453
Sin 70 = 0.12187
6.5 − 6
P (6.5) = f (6 ) + [ f (7 ) − f (6 ) ]
7−6
0.5
P (6.5) = 0.10453 + [ 0.12187 − 0.10453 ]
1
= 0.1132
This is more close to the correct answer actually.
x − xi
P ( x ) = f ( xi ) + [ f ( x i+1 ) − f ( x i ) ]
x i+1 − x i
and rearrange the right hand side so that the formula becomes:
P( x )=
{ x i +1−x i } {
f ( x i +1 )−f ( x i )
x + f ( x i )−
f ( x i+1 )−f ( x i )
x i +1−x i
xi
}
Once we chose the two tabular points x i and xi+1 , the two quantities in brackets are
constants, which we could call a1 and a0. In this case we can express P(x) in the form
of a first degree polynomial as:
P(x) = a1 x + a0
Numerical Analysis Ch V: Interpolation and Curve-Fitting
In this polynomial, for any set of n+1 data points ( x i , yi ) for i = 0,1,2,3,….,n the
interpolating polynomial is given by:
Where:
( x-x 0 ) ( x-x1 ) .. . ..( x-x i-1 )( x-x i+1 ) . .. .. .( x-x n-1 ) ( x-x n )
Li ( x )=
( xi -x 0 ) ( xi -x 1 ) .. . ..( x i -x i-1 )( x i -x i+1 ) . .. .. .( x i -x n-1 ) ( x i -x n )
Example 5.2
x0 = 1 y0 = 1
x1 = 3 y1 = 5
x2 = 6 y2 = 10
Solution:
There are 3 data points, hence there will be a second order interpolating polynomial
given by:
Where:
Numerical Analysis Ch V: Interpolation and Curve-Fitting
( x-x 1 ) ( x-x 2 )
L0 ( x )=
( x 0 -x 1 ) ( x 0 -x 2 )
( x-3) ( x-6 ) 1
¿ = ( x 2 − 9 x + 18 )
( 1-3 ) ( 1-6 ) 10
( x-x 0 ) ( x-x 2 )
L1 ( x )=
( x 1 -x 0 ) ( x 1 -x 2 )
( x-1) ( x-6 ) 1
¿ =− ( x2 − 7 x + 6 )
( 3-1 ) ( 3-6 ) 6
( x-x 0 ) ( x-x 1 )
L2 ( x )=
( x 2 -x 0 ) ( x 2 -x 1 )
( x-1) ( x-3 ) 1
¿ = ( x2 − 4 x + 3 )
( 6-1 ) ( 6-3 ) 15
Then
1 1 1
Q2 ( x ) = [ ( x 2 − 9 x +18 )] × 1 + [− ( x 2 − 7x +6 ) × 5] + [ ( x2 − 4 x +3 )] × 10
10 6 15
1
¿− ( x 2 − 34 x + 18 )
15
As a check, the three values of x should be substituted into the polynomial to give:
2. If new data points are added to a set of points that has already been operated on,
the whole process must be started again from the beginning.
Numerical Analysis Ch V: Interpolation and Curve-Fitting
As alternative approach to find the interpolating polynomials Q n(x) that pass exactly
through n+1 data points given as ( xi, yi ) i = 0,1,2,…..,n is based on polynomial
representation given by:
Where C0 , C1, C2 , ……… Cn can be determined for equally spaced values of x as:
C0 = y 0
Δy Δ y0 = y1 − y0
C1 = 0
h Δ 2 y 0 = Δ ( Δ y 0 ) = Δy 1 − Δy 0
Δ2 y 0 and
C2 = 2
2h Where h = x i+1 − x i
In general
j
Δ y0
C j= j
j! h
j j−1 j−1
Where: Δ yi = Δ y i+1 − Δ yi which can be taken from the following table.
∆y3 ∆3y2
x4 y4 ∆2y3
∆y4
x5 y5
Numerical Analysis Ch V: Interpolation and Curve-Fitting
Example 5.3
Given the following data based on the function y = cos x where x is in degrees:
x 20 25 30 35 40
h = 5, x0 = 20
Δ j y0
C j= j
j! h
C0 = 0.93969
Δ y0
C1=
h = -0.006676
2
Δ y0
C2 = 2
2! h = -0.000138
Δ3 y0
C3 = 3
3! h = 0.0000004
Δ4 y 0
C 4=
4 ! h 4 = 0.000000003
Problems:
5.- Use Lagrangian polynomials to obtain an interpolating polynomial for the
data
x 0.0 0.1 0.2 0.3
y 0.1 0.1005 0.102 0.1046
and use it to estimate the value of y when x = 0.4 [ Answer y(0.4) = 0.1084]
2- For the following set of data, express the third interpolating polynomial in its
Newton’s forward difference form.
X 0 2 4 6
y 10 7 0 -11
t (min) 0 5 10 15 20 25
T (oC) 90 62.8 45.8 35.6 29.5 25.8
If we are seeking a function to follow closely a large number of data points measured
in an experiment, it is often more practical to seek a function which represents a “best
fit” to the data rather than one which passes through all points exactly.
Given n data points (xi, yi), i=0,1,2,….,n, the required “best fit” polynomial can be
written in the form:
p (x) = a0 + a1 x + a2 x2 + ….. + an xn
At each of the n tabulated points, the resulting p(xi) will be approximately equal to the
tabulated yi with an error which we shall call Ei
We could then write the following n equations, one for each tabulated point:
y0 – p(x0) = E0
y1 – p(x1) = E1
y2 – p(x2) = E2
⋮
yn – p(xn) = En
Or, in general,
yi – p(xi) = Ei
Can we make the sum of the errors equals to zero? We can but it is not desirable as the
data may be fitted to a completely wrong curve.
Can we make the sum of the absolute errors equal to zero? No, since then each of the
Ei would in turn have to be zero.
Instead, we can minimize the sum of the squares of Ei. We used the squares to preserve
the plus sign in order to keep the Ei from canceling each other out.
The sum of the squares of the difference between p(x) and the actual values of y is
given by:
n
S = ∑ [ y i −p ( x i ) ]2
i=1
which can be minimized by taking the partial first derivatives of S with respect to each
of the constants, a0, a1, a2,…… an and putting the result equals to zero, thus
∂S ∂S ∂S ∂S
= = = ..... = =0
∂ a 0 ∂ a1 ∂ a2 ∂ an
Numerical Analysis Ch V: Interpolation and Curve-Fitting
∂S
= 2 ∑ ( y i − a0 - a 1 x i - a 2 x 2 …. . - a n x n ) (−1 ) = 0
∂ a0 i i
∂S
= 2 ∑ ( y i − a0 - a 1 xi - a 2 x 2 …. . - a n x n ) (−xi ) = 0
∂ a1 i i
∂S
= 2 ∑ ( yi − a0 - a 1 xi - a 2 x 2 …. . - a n x n ) (−x 2 ) = 0
∂ a2 i i i
... .
∂S
= 2 ∑ ( y i − a0 - a 1 x i - a 2 x 2 …. . - a n x n ) (−x n ) = 0
∂ an i i
This linear system of equations can be written in matrix form and be solved using any
of the learned techniques. The matrix form is:
[ ] [ ][ ]
nn n nn n n n n n n
n ∑ x i ∑ x 2 . . . . ∑ x n ¿ ∑ x i ∑ x 2∑ x 3 . . . . ∑ x n+ 1 ¿ ∑ x 2 ∑ x 3 ∑ x 4 . . . ∑ x n+ 2 ¿ [ . . . . . . . . . . . . ¿ ] ¿ ¿
i=1 i=1 i i=1 i i=1 i=1 i i=1 i i=1 i i=1 i i=1 i i=1 i i=1 i
¿
Example 5.4
Suppose in an experiment, a heavy object was dropped from a height of 1100 feet. Let
x stands for the time, in seconds, after the object was dropped. If the height was
measured to the nearest 10 feet for the first 4 seconds and was as in the next table:
xi 0 1 2 3 4
yi 1100 1080 1040 96 840
0
Use least squares curve fitting method to fit the measured data to a first, second and
third order polynomial and use these to estimate the distance at 2.5 seconds.
Numerical Analysis Ch V: Interpolation and Curve-Fitting
Solution:
First order polynomial:
The polynomial will be in the form: y = a0 + a1 x
[ ]
n
n∑ xi ¿ ¿ ¿ ¿
i= 1
The set of equations are: ¿
Therefore we need to sum the values of x, y, x2, and xy. This is best done in table
form.
N x Y X2 Xy
1 0 1100 0 0
2 1 1080 1 1080
3 2 1040 4 2080
4 3 960 9 2880
5 4 840 16 3360
∑ 10 5020 30 9400
Using the matrix form and the table to construct the equations:
a0 = 1132
a1 = -64
Y = 1132 – 64 x
[ ( ( ))] =minimum
2
π xi
S= ∑ n y i− a+b sin
i=¿ ¿ 10
∂S ∂ S
Solving for = =0 one would arrive to the following two equations in the two
∂a ∂b
unknowns, a and b
( )
n n
πx
na+ ∑ sin b=¿ ∑ y i ¿
i=1 10 i=1
( ) ( ) ( )
n n n
πx 2 πx πx
∑ sin 10
a+ ∑ sin
10
b=¿ ∑ y i sin
10
¿
i=1 i =1 i=1
Solving the two equations and obtaining values of a and b, one can use the function
y=a+b sin
πx
10 ( )
to calculate the value of y at any value of x.
In case the data is to be fitted to an exponential function, which represents the behavior of
many real life systems, which has the form of
In dealing with this relation to find values of A and B, the easiest approach is to linearise the
function by taking the natural logarithm of both sides:
When n (order of polynomial) becomes large, in many cases, one may get oscillatory
behavior in the resulting polynomial. This was shown by Runge when he interpolated data
based on a simple function of
1
y=
1+25 x2
on an interval of [–1, 1]. For example, take six equidistantly spaced points in [–1, 1] and find
y at these points as given in Table 1.
Now through these six points, one can pass a fifth order polynomial
−11 5 4 −11 3 2 −11 −1
f 5 ( x )=3 . 1378×10 x +1 .2019 x −3 . 3651×10 x −1 .7308 x +1 .0004×10 x+5 . 6731×10 ,
−1≤x≤1
through the six data points. On plotting the fifth order polynomial (figure 5.4) 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
5.5), 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 to use the technique known as
spline interpolation. The most common spline interpolations used are linear, quadratic, and
cubic splines.
Numerical Analysis Ch V: Interpolation and Curve-Fitting
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)
.Figure 5.4 5th order polynomial interpolation with six equidistant points
2.1
8.0
4.0
y
0
1- 5.0- 0 5.0 1
4.0-
x
laimonyloP redrO ht5 )2^x*52+1(/1 noitcnuF
The condition for a cubic spline is to pass a set of cubics through the given data points.
For the cubic spline to be continues, it requires that both slope and curvature be the same for
the pair of cubics that join at each point.
The cubic for the ith interval, which lies between point (xi , yi) and (xi+1 , yi+1) is in the form:
The mathematical formulation continues using the two conditions of equating the 1st and 2nd
derivatives (slope and curvature) of the each adjacent cubics. This ends up with the values of
a, , bi , ci , di are found in terms of the values of the 2nd derivatives as:
Values of the second derivatives are calculated from the following system of equations:
Here there are n-2 equations but n unknowns. 2 additional equations involving S 1 and Sn can
be obtained by specifying the conditions at the end intervals of the whole curve.
[ ]
2 ( h1+ h2 ) h2 ¿ ⋯ ¿
h2 2 ( h2+ h3 ) h3 ¿ ¿ ¿ ¿ ¿ ⋱ ¿ ¿ ¿ ⋯ ¿ h n−2¿ 2 ( h n−2 +h n−1) ¿
h3 2 ( h 3 + h4 ) h4 ¿ ¿
Condition 2: S1 = S2 Sn = Sn-1
[ ]
( 3 h1 +2 h2 ) h2 ¿ ⋯ ¿
h2 2 ( h 2+ h3 ) h3 ¿ ¿ ¿ ¿ ¿ ⋱ ¿ ¿ ¿ ⋯ ¿ hn−2 ¿ ( 2 hn−2 +3 hn−1 ) ¿
h3 2 ( h3 +h 4 ) h4 ¿ ¿
[
2 2
(h ¿ ¿ 1+2 h2 ) h2−h1
( h1 +h2 ) ¿ ¿ ⋯ ¿ h 2n−2−h2n−1
h2 h2 ¿ ¿ h3 ¿ 2 ( h3 +h 4 ) ¿ h 4 ¿ ¿ ¿ ¿ ⋮ ¿ ¿ ¿ ¿ ⋱ ¿ ¿ ¿ ⋯ ¿ ¿ ( hn−
hn−2
h2 2 ( h2 +h3 ) h3 ¿
In each case the S vector is reduced to n-2 elements and the coefficient matrix becomes
square of size (n-2) (n-2). For each of the cases, the right hand side vector is the same.
Example:
Fit the following set of data using cubic spline curve fitting technique. Use the three end
conditions; natural spline, parabolic runs out spline, and cubic run out spline.
f(x) x
-8 0
-7 1
0 2
19 3
56 4
Note that there are 4 data points meaning there will be 3 cubic polynomials of the form
y = ai(x-xi)3 + bi(x-xi)2 + ci(x-xi) + di
Condition 1:
¿
So that
S1 = 0 S2 = 6.4285 S3 = 10.2857 S4 = 24.4285 S5 = 0
Numerical Analysis Ch V: Interpolation and Curve-Fitting
Condition 2:
¿
So that
S1 = 4.8 S2 = 4.8 S3 = 12.0 S4 = 19.2 S5 = 19.2
Condition 3:
¿
So that
S1 = 0 S2 = 6.0 S3 = 12.0 ` S4 = 18.0 S5 = 24.0
gives;
Condition 1:
cubic No. 1 a1 = 1.0714 b1 = 0 c1 = -0.0714 d1 = -8
Then the interpolating polynomial for the 1 interval would be;
st
y = 1.0714(x-xi)3 - 0.0714(x-xi) - 8
cubic No. 2 a2 = b2 = c2 = d2 =
cubic No. 3 a3 = b3 = c3 = d3 =
Condition 2:
cubic No. 1 a1 = b1 = c1 = d1 =
cubic No. 2 a2 = b2 = c2 = d2 =
cubic No. 3 a3 = b3 = c3 = d3 =
Condition 3:
cubic No. 1 a1 = b1 = c1 = d1 =
cubic No. 2 a2 = b2 = c2 = d2 =
cubic No. 3 a3 = b3 = c3 = d3 =
The splines strength can be demonstrated by the extreme extent, in its ability to correlate data
which doesn’t follow any specific pattern without a single polynomial’s extreme behavior.
Clearly there is no relation between these data points. A spline, however, can interpolate all
100 points without the drastic behavior that the necessary 99th degree polynomial would
exhibit.