You are on page 1of 9

5

Curve Fitting

One of the oldest and most applied problems in mathematics is the problem of
constructing an approximation to a given function from a discrete set of data pairs. The
fundamental two dimensional curve-fitting problem from a discrete set of (x, y) data pairs
(x1, y1) through (xn, yn) is to predict the value of y when the value of x is specified. There
are a number of ways to do this curve-fitting, depending on the kind of data available and
the specific conditions placed on the resulting curve.
In this chapter, we consider methods which do not account for random errors in
the x and y values as in the least squares approximation. The data will be treated as
accurate and determine a curve that passes through the data points exactly. The most
general and powerful tools for curve-fitting is approximation theory and the simpler but
less robust technique is interpolation. Classical methods for fitting curves involve
polynomial models.

5.1 POLYNOMIAL INTERPOLATION


If a set of n data values (x1, y1) through (xn, yn) are given to represent y as a single-
valued function of x, one can find a unique polynomial of degree (n-1) that passes through
the data points. For example, we can find a unique straight line through two points, and
we can find a unique quadratic that passes through 3 points.
The polynomial model for y may be given as
𝑦 = 𝑎0 + 𝑎1 𝑥 + 𝑎2 𝑥 2 + ⋯ + 𝑎𝑛−1 𝑥 𝑛−1
and use the data pairs (x1, y1) to (xn, yn) to write the n equations and form a system of
linear algebraic equations to determine the coefficients ai. These equations are
𝑦𝑗 = 𝑎0 + 𝑎1 𝑥𝑗 + 𝑎2 (𝑥𝑗 )2 + ⋯ + 𝑎𝑛−1 (𝑥𝑗 )𝑛−1 ; 𝑗 = 1, 2, ⋯ , 𝑛
and the solution of the linear system may be determined by the methods of Chapter 2.
However, solving the system of linear equations is an inefficient way of obtaining a
representation for y. Other polynomial models provide a more efficient ways of
predicting y for a given value of x. These models appear quite different from the above
model, nevertheless, they produce the same unique curve through n data points.

5.1.1 Langrange Interpolation


The Langrange formula is perhaps the best known of the classical formulas for
interpolating among n data pairs (x1, y1) through (xn, yn). It is used to represent y as a
function of x in the form

[5.1a]
𝑛

𝑦(𝑥) = ∑ 𝐿𝑗 𝑦𝑗
𝑗=1

in which the multiplying polynomials Lj are given by


[5.1b]
𝑛
(𝑥 − 𝑥𝑖 )
𝐿𝑗 = ∏ [ ]
(𝑥𝑗 − 𝑥𝑖 )
𝑖=1
𝑖≠𝑗

The popularity of Langrange formula is well known because it is easy to code and
the data are not required to be specified with x in ascending or descending order.
Although the computation of y is simple, but for large values of n, the method is still not
efficient. The Langrange’s form is useful for interpolating tables and for deriving formulas
for numerical differentiation and integration.

Example 5.1. For the data pairs (x1, y1), (x2, y2) (x3, y3), fit a curve using the Langrange
interpolation.
Solution:
𝑦(𝑥) = 𝐿1 𝑦1 + 𝐿2 𝑦2 + 𝐿3 𝑦3
when j = 1, i = 2, 3; j = 2, i = 1, 3; j = 3, i = 1,2
𝑥 − 𝑥2 𝑥 − 𝑥3 𝑥 − 𝑥1 𝑥 − 𝑥3 𝑥 − 𝑥1 𝑥 − 𝑥2
𝑦(𝑥) = ( )( ) 𝑦1 + ( )( ) 𝑦2 + ( )( )𝑦
𝑥1 − 𝑥2 𝑥1 − 𝑥3 𝑥2 − 𝑥1 𝑥2 − 𝑥3 𝑥3 − 𝑥1 𝑥3 − 𝑥2 3
Example 5.2 Compute ln 9.2 when ln 9.0 = 2.1972, ln 9.5 = 2.2513, ln 11.0 = 2.3979
Solution:
𝑦(𝑥) = ln(𝑥)
𝑥 − 9.5 𝑥 − 11.0 𝑥 − 9.0 𝑥 − 11.0
𝑦(𝑥) = ( )( ) 2.1972 + ( )( ) 2.2513
9.0 − 9.5 9.0 − 11.0 9.5 − 9.0 9.5 − 11.0
𝑥 − 9.0 𝑥 − 9.5
+( )( ) 2.3979
11.0 − 9.0 11.0 − 9.5
𝑦(𝑥) = 2.1972(𝑥 2 − 20.5𝑥 + 104.5) − 3.0017(𝑥 2 − 20𝑥 + 99) + 0.7993(𝑥 2 − 18.5𝑥
+ 85.5)
𝑦(𝑥) = −0.0052𝑥 2 + 0.2050𝑥 + 0.756
hence,
ln(9.2) = 𝑦(9.2) = 2.2192

5.1.2 Newton’s General Interpolating Formula


Newton’s general interpolating formula for data pairs (x1, y1) through (xn,yn) has
the form
[5.2] 𝑦(𝑥) = 𝑎0 + 𝑎1 (𝑥 − 𝑥1 ) + 𝑎2 (𝑥 − 𝑥1 )(𝑥 − 𝑥2 ) + ⋯
+𝑎𝑛−1 (𝑥 − 𝑥1 )(𝑥 − 𝑥2 ) ⋯ (𝑥 − 𝑥𝑛−1 )

The coefficients aj are generally obtained by computing a set of quantities known


as divided differences. The notation for divided difference is given by
𝑓[𝑥𝑘 ] = 𝑦𝑘
𝑓[𝑥𝑘+1 ] − 𝑓[𝑥𝑘 ] 𝑦𝑘+1 − 𝑦𝑘
𝑓[𝑥𝑘 , 𝑥𝑘+1 ] = =
𝑥𝑘+1 − 𝑥𝑘 𝑥𝑘+1 − 𝑥𝑘
𝑦𝑘+2 − 𝑦𝑘+1 𝑦𝑘+1 − 𝑦𝑘
𝑓[𝑥𝑘+1 , 𝑥𝑘+2 ] − 𝑓[𝑥𝑘 , 𝑥𝑘+1 ] 𝑥𝑘+2 − 𝑥𝑘+1 − 𝑥𝑘+1 − 𝑥𝑘
𝑓[𝑥𝑘 , 𝑥𝑘+1 , 𝑥𝑘+2 ] = =
𝑥𝑘+2 − 𝑥𝑘 𝑥𝑘+2 − 𝑥𝑘
In general,
[5.3]
𝑓[𝑥𝑘+1 , 𝑥𝑘+2 , ⋯ , 𝑥𝑘+𝑗 ] − 𝑓[𝑥𝑘 , 𝑥𝑘+1 , ⋯ , 𝑥𝑘+𝑗−1 ]
𝑓[𝑥𝑘 , 𝑥𝑘+1 , 𝑥𝑘+2 , ⋯ , 𝑥𝑘+𝑗 ] =
𝑥𝑘+𝑗 − 𝑥𝑘
Let us denote the divided differences as
[5.4] 𝑓𝑘,𝑗 = 𝑓[𝑥𝑘 , 𝑥𝑘+1 , ⋯ , 𝑥𝑘+𝑗 ]
𝑗 = 0, 1, 2, ⋯ , 𝑛 − 1
𝑘 = 1, 2, ⋯ , 𝑛

The coefficients aj of Equation [5.2] are related to the divided differences by


[5.5] 𝑎𝑗 = 𝑓1,𝑗 ;
If we let 𝑓𝑘,𝑗 as the elements of an array at the jth sweep of the computation process,
where j = 0, 1, 2, …, n-1, the elements of the computation process may be illustrated in
Figure 5.1 using four data pairs.

j=0 j=1 j=2 j=3


y1 = f1,0
f1,1
y2 = f2,0 f1,2
f2,1 f1,3
y3 = f3,0 f2,2
f3,1
y4 = f4,0

Figure 5.1 Elements fk,j of the jth computation process

Example 5.3 The elapsed time t(sec) for a car to accelerate to a speed v(mph) from an
initial speed of 30 mph is described by the data in Table 5.1. Fit a polynomial to describe
the function using Newton’s form. Estimate the time for the car to accelerate to a speed
of 50 mph.
Table 5.1 Car’s speed versus time to accelerate
i 1 2 3 4
v(mph) 30 40 55 70
t(sec) 0.0 1.6 4.8 8.9

Solution: Since t = t(v)


j= 0; t1,0 = t1 = 0; t2,0 = t2 = 1.6; t3,0 = 4.8; t4,0 = 8.9
j = 1;
𝑡 −𝑡 1.6−0 1.6
𝑡1,1 = 𝑡[𝑣1 , 𝑣2 ] = 𝑣 2−𝑣1 = 40−30 =
2 1 10
𝑡 −𝑡 4.8−1.6 3.2
𝑡2,1 = 𝑡[𝑣2 , 𝑣3 ] = 𝑣 3−𝑣2 = =
3 2 55−40 15
𝑡 −𝑡 8.9−4.8 4.1
𝑡3,1 = 𝑡[𝑣3 , 𝑣4 ] = 𝑣 4−𝑣3 = =
4 3 70−55 15

j = 2;
3.2 1.6
𝑡[𝑣2 ,𝑣3 ]−𝑡[𝑣1 ,𝑣2 ] − 1.6
15 10
𝑡1,2 = 𝑡[𝑣1 , 𝑣2 , 𝑣3 ] = = 55−30 = 750
𝑣3 −𝑣1
4.1 3.2
𝑡[𝑣3 ,𝑣4 ]−𝑡[𝑣2 ,𝑣3 ] − 0.9
15 15
𝑡2,2 = 𝑡[𝑣2 , 𝑣3 , 𝑣4 ] = = =
𝑣4 −𝑣2 70−40 450

j = 3;
0.9 1.6
𝑡[𝑣2 ,𝑣3 ,𝑣4 ]−𝑡[𝑣1 ,𝑣2 ,𝑣3 ] − 0.3
450 750
𝑡1,3 = 𝑡[𝑣1 , 𝑣2 , 𝑣3 , 𝑣4 ] = = = − 9000
𝑣4 −𝑣1 70−30

Therefore the fitted polynomial is


1.6 1.6 0.3
𝑡 = 𝑡(𝑣) = (𝑣 − 30) + (𝑣 − 30)(𝑣 − 40) − (𝑣 − 30)(𝑣 − 40)(𝑣 − 55)
10 750 9000
When v = 50 mph
t = 3.59 sec

5.1.3 Neville’s Algorithm

Recall that the Newton interpolation involved two stages: the computation of
coefficient and the evaluation of the polynomial. Neville’s algorithm is a method that
requires about the same effort as the first stage of the Newton interpolation. It is used
with n pairs of data (x1, y1) through (xn, yn) to obtain y(x) directly for a given value of x.
The aim of the method is to evaluate a polynomial of degree (n-1) by a process
that is similar to the computation of the coefficients of the Newton polynomial. However,
the quantities fk,j in Figure 5.1 are now

𝑓𝑘,0 = 𝑦𝑘
[(𝑥 − 𝑥𝑘+𝑗 )𝑓𝑘,𝑗−1 − (𝑥 − 𝑥𝑘 )𝑓𝑘+1,𝑗−1 ]
𝑓𝑘,𝑗 =
𝑥𝑘 − 𝑥𝑘+𝑗
𝑗 = 1, 2, ⋯ , 𝑛 − 1
𝑘 = 1, 2, ⋯ , 𝑛 − 𝑗
The interpolated value y(x) for a given x is the last computed quantity y(x) = f1, n-1.

Example 5.4. Estimate t when v is 50 mph using Nevilles’ Algorithm.


Solution:
𝑡(50) = 𝑡1,3
𝑎𝑡 𝑗 = 0; 𝑡1,0 = 0 ; 𝑡2,0 = 1.6 ; 𝑡3,0 = 4.8 ; 𝑡4,0 = 8.9
𝑎𝑡 𝑗 = 1;
[(𝑣 − 𝑣2 )𝑡1,0 − (𝑣 − 𝑣1 )𝑡2,0 ] (50 − 40)0 − (50 − 30)1.6
𝑡1,1 = = = 3.2
𝑣1 − 𝑣2 30 − 40

[(𝑣 − 𝑣3 )𝑡2,0 − (𝑣 − 𝑣2 )𝑡3,0 ] (50 − 55)1.6 − (50 − 40)4.8 56


𝑡2,1 = = =
𝑣2 − 𝑣3 40 − 55 15
[(𝑣 − 𝑣4 )𝑡3,0 − (𝑣 − 𝑣3 )𝑡4,0 ] (50 − 70)4.8 − (50 − 55)8.9 103
𝑡3,1 = = =
𝑣3 − 𝑣4 55 − 70 30

𝑎𝑡 𝑗 = 2;
[(𝑣 − 𝑣3 )𝑡1,1 − (𝑣 − 𝑣1 )𝑡2,1 ] (50 − 55)3.2 − (50 − 30)(56/15) 272
𝑡1,2 = = =
𝑣1 − 𝑣3 30 − 55 75
56 103
[(𝑣 − 𝑣4 )𝑡2,1 − (𝑣 − 𝑣2 )𝑡3,1 ] (50 − 70)(15) − (50 − 40)( 30 ) 109
𝑡2,2 = = =
𝑣2 − 𝑣4 40 − 70 30
𝑎𝑡 𝑗 = 3;
272 109
[(𝑣 − 𝑣4 )𝑡1,2 − (𝑣 − 𝑣1 )𝑡2,2 ] (50 − 70)( 75 ) − (50 − 30)( 30 )
𝑡1,3 = = = 3.63
𝑣3 − 𝑣4 30 − 70
Therefore,
𝑡(50) = 3.63 𝑠𝑒𝑐
5.2 SPLINE INTERPOLATION

The curves produced by polynomial interpolation pass through the n specified


data points and the curve may exhibit strong oscillations between the data points when
polynomials of high degree are used. As shown in Figure 5.2, suppose that the given data
points are approximations to a straight line. By forcing a fourth degree polynomial to
pass through five points, the curve that is produced will deviate significantly from the
line.

Figure 5.2. Polynomial Interpolation

This suggests that the interpolation might be unsatisfactory if we insist on increasing the
order of the polynomial. However, if we keep the order of the polynomial fixed and use it
over different intervals, with the length of the interval decreasing, then interpolation can
be very accurate. This type of curve fitting is known as spline interpolation. The spline
interpolation is applied to n ordered pairs of data. We seek (n-1) curves that connects
points 1 and 2, points 2 and 3, … , and points (n-1) and n as shown in Figure 5.3.

b) first order splines a) Second order splines

c) Third order splines

Figure 5.3 Spline Interpolation


5.2.1 Cubic Splines
The objective of the cubic spline is to derive third order polynomials for each
interval between data points. In addition, the two curves connecting points (k-1) and k
and points k and (k+1) are required to have the same slope at point k so that the resulting
curve fit is both continuous and smooth.

From Langrange Interpolation at (xk, yk) and (xk+1, yk+1)


 x  x k 1   x  xk 
y"   ( y" ) k   ( y" ) k 1
 x k  x k 1   x k 1  x k 
or simply
y"  A( y" ) k  b( y" ) k 1 eq.1
Integrating eq. 1 gives

y'  A ( y" )k (x  x k 1)  B ( y" )k 1(x  x k )  C1 eq.2


2 2

A second integration gives

y
A(x k 1  x )
2

( y" ) k  B( x  x k ) 2 ( y" ) k 1
 C1x  C2 eq.3
6
At (xk,yk), A =1, B = 0 and at (xk+1,yk+1),A = 0, B = 1. Determine C1 and C2 and substitute the
values into equation 2 and 3

y  Ay k  By k 1 
(A 3

 A)(y" ) k  (B3  B)(y" ) k 1 ( x k 1  x k )
eq.4
6

y  y k [3B 2  1)(y" ) k 1  (3A 2  1)(y" ) k ](x k 1  x k )


y'  k 1  eq.5
x k 1  x k 6
We use equation 4 to equate (y’)k between points k and k+1
to (y’)k between points k-1 and k
 x k  x k 1  x  x k 1  x  xk 
  ( y" ) k 1   k 1  ( y" ) k   k 1  ( y" ) k 1
 6   3   6 
y  yk y  y k 1
 k 1  k
x k 1  x k x k  x k 1
k  1,2,3,...(n  1)

For natural splines, (y”)1 = (y”) n = 0

Example 5.5 Use natural spline to estimate the elapsed time t(sec) for a car to accelerate
From 30 mph to 50 mph
i 1 2 3 4
v 30 40 55 70
t 0 1.6 4.8 8.9

Solution:

d2t
let t" 
dv 2
(25 3 )(t" ) 2  (15 6 )(t" )3  3.215  1.610
(15 6 )(t" ) 2  (30 3 )(t" )3  4.115  3.215

Using cramer’s rule, (t”)2 = 0.00497297; (t”)3 = 0.00475676


For v= 50 between v2 and v3, A = 1/3, B = 2/3. Then from eq 4


t  1.6 3  2( 4.8) 3  (8 27)(t" ) 2  (10 27)(t" )3 152 6 
t  3.612 sec

You might also like