Curve Fitting and Interpolation
Curve Fitting and Interpolation
Curve Fitting and Interpolation
1 Introduction
Data is often given in discrete values along a continuum. However, you may require estimates at points
between the discrete values. This chapter describes techniques to fit curves to such data to obtain inter-
mediate estimates.
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 or noise, the strategy is to derive a single
curve that represents the general trend of the data. Because any individual data point may
be incorrect, we make no effort to intersect every point. Rather, the curve is designed
to follow the pattern of the points taken as a group. One approach of this nature is called
least-squares regression (fig.1).
Fig.1
• 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 points. The estimation of values between well-known
discrete points is called interpolation (fig.2).
Fig.2
1
technique for fitting a best polynomial. Thus, you will learn to derive a parabolic, cubic, or higher-order
polynomial that optimally fits uncertain data. Linear regression is a subset of this more general approach,
which is called polynomial regression.
y = ao + a1 x + e
where ao and a1 are coefficients representing the intercept and the slope, respectively, and e is the error,
or residual, between the model and the observations, which can be represented by rearranging the previous
equation as
e = y − ao − a1 x
Thus, the error, or residual, is the discrepancy between the true value of y and the approximate value,
ao + a1 x, predicted by the linear equation.
• Criteria for a ”Best” fit
One strategy for fitting a best line through the data would be to minimize the sum of the squares of
residual errors for all the available data, as in
n
X n
X
Er = e2i = (yi − ao − a1 xi )2
i=1 i=1
To determine ao and a1 , the previous equation is differentiated with respect to each coefficient.
∂Er
X
= −2 (yi − ao − a1 xi )
∂ao
∂Er X
= −2 [(yi − ao − a1 xi )xi ]
∂a1
Setting these derivatives equal to zero will result in a minimum Er . When solved for ao and a1 it
gives
n n
! n !
X X X
n xi yi − xi yi
i=1 i=1 i=1
a1 =
n n
!2
X X
2
n xi − xi
i=1 i=1
n
! n
! n
! n
!
X X X X
x2i
yi − xi yi xi
i=1 i=1 i=1 i=1
ao = !2
n n
X X
x2i −
n xi
i=1 i=1
2
Any line other than the one computed results in a larger sum of the squares of the residuals. Thus,
the line is unique and in terms of our chosen criterion is a best line through the points.
Fig.3
y = a1 eb1 x
where a1 and b1 are constants. This model is used in many fields of engineering to characterize quantities
that increase (positive b1 ) or decrease (negative b1 ) at a rate that is directly proportional to their own
magnitude. Another example of a nonlinear model is the simple power equation
y = a2 xb2
where a2 and b2 are constant coefficients. This model has wide applicability in all fields of engineering.
Nonlinear regression techniques (not discussed in this chapter) are available to fit these equations
to experimental data directly. However, a simpler alternative is to use mathematical manipulations to
transform the equations into a linear form. Then, simple linear regression can be employed to fit the
equations to data.
For example the first equation can be linearized by taking its natural logarithm to yield
ln(y) = ln(a1 ) + b1 x
3
Thus, a plot of ln(y) versus x will yield a straight line with a slope of b1 and an intercept of ln(a1 ). The
second equation is linearized by taking its logarithm to give
In their transformed forms, these models can use linear regression to evaluate the constant coefficients.
They could then be transformed back to their original state and used for predictive purposes.
Many other nonlinear equations can be transformed into linear form in a similar way. The next table
lists several such equations.
Fig.4
For given data points it is possible to foresee, to some extent, if a proposed nonlinear function has a
potential for providing a good fit. This is done by plotting the data points in a specific way and examining
whether the points appear to fit a straight line. For the functions listed in the previous table this is shown
in the fifth (last) column of the table. For power and exponential functions, this can be done by plotting
4
the data using different combinations of linear and logarithmic axes. For all functions it can be done by
plotting the transformed values of the data points in plots with linear axes.
y = ao + a1 x + a2 x2 + e
For this case the sum of the squares of the residuals is
n
X
Er = (yi − ao − a1 xi − a2 x2i )2
i=1
Following the procedure of the previous section, we take the derivative of the above equation with
respect to each of the unknown coefficients of the polynomial, as in
∂Er X
= −2 (yi − ao − a1 xi − a2 x2i )
∂ao
∂Er
X
= −2 [(yi − ao − a1 xi − a2 x2i )xi ]
∂a1
∂Er
X
= −2 [(yi − ao − a1 xi − a2 x2i )x2i ]
∂a 2
These equations can be set equal to zero and arranged to develop the following set of linear equations
X X X
nao + xi a1 + x2i a2 = yi
X X
X X
x2i a1 + x3i a2 =
xi ao + xi yi
X X X X
x2i ao + x3i a1 + x4i a2 = x2i yi
where all summations are from i = 1 to i = n. Note that the above three equations are linear and
have three unknowns: ao , a1 , and a2 . The coefficients of the unknowns can be calculated directly from
the observed data. For this case, we see that the problem of determining a least-squares second-order
polynomial is equivalent to solving a system of three simultaneous linear equations. Techniques to solve
such equations were discussed in the previous chapter.
y = ao + a1 x + a2 x2 + + am xm + e
The foregoing analysis can be easily extended to this more general case. Thus, we can recognize
that determining the coefficients of an mth-order polynomial is equivalent to solving a system of (m + 1)
simultaneous linear equations.
5
3 Interpolation
You will frequently have occasions to estimate intermediate values between precise data points. The most
common method used for this purpose is polynomial interpolation. Recall that the general formula for
an nth-order polynomial is
f (x) = ao + a1 x + a2 x2 + + an xn
For n + 1 data points, there is one and only one polynomial of order n that passes through all the
points. For example, there is only one straight line (that is, a first-order polynomial) that connects two
points. Similarly, only one parabola connects a set of three points. Polynomial interpolation consists of
determining the unique nth-order polynomial that fits n + 1 data points. This polynomial then provides a
formula to compute intermediate values.
Although there is one and only one nth-order polynomial that fits n + 1 points, there are a variety of
mathematical formats in which this polynomial can be expressed. In this chapter, we will describe three
alternatives: the standard, Newton and Lagrange polynomials.
f (x) = ao + a1 x + a2 x2 + + am xm
The coefficient a0i s are determined by solving a system of m + 1 linear equations that are obtained by
writing the polynomial explicitly for each point. The resulting system is of the form shown below and can
be solved using one of the techniques discussed in the previous chapter.
x21 .... xm
1 x1 1 ao y1
1
x2 x22 .... xm2
a1 y2
=
2 m
1 xm+1 xm+1 .... xm+1 am ym+1
6
for n point (x1 , y1 )......, (xn , yn ) one can show that the coefficients ai stistfy
n
Y 1
ai = yi
(xi − xj )
j=1,j6=i
and
n n n
X Y (x − xj ) X
f (x) = yi = yi Li (x)
i=1
(xi − xj ) i=1
j=1,j6=i
n
Y (x − xj )
where Li (x) = are called the Lagrange functions
(xi − xj )
j=1,j6=i
f (x) = a1 (x − x2 ) + a2 (x − x1 )
where
y1 y2
a1 = and a2 =
x1 − x2 x2 − x1
• The spacing between the data points does not have to be equal.
• If an interpolated value is calculated for a given set of data points, and then the data set is enlarged
to include additional points, all the terms of the Lagrange polynomial have to be calculated again.
f (x) − y1 y2 − y1
=
x − x1 x2 − x1
which can be rearranged to yield
y2 − y1
f (x) = y1 + (x − x1 ) = ao + a1 (x − x1 )
x2 − x1
where
y2 − y1
ao = y1 a1 =
x2 − x1
7
1. In general, the smaller the interval between the data points, the better the approximation. This is
due to the fact that, as the interval decreases, a continuous function will be better approximated by a
straight line. This characteristic is demonstrated in the following example. Two linear interpolations
to estimate ln(2). Note how the smaller interval provides a better estimate.
Fig.9
The error in the previous example resulted from our approximating a curve with a straight line.
Consequently, a strategy for improving the estimate is to introduce some curvature into the line
connecting the points. If three data points are available, this can be accomplished with a second-
order polynomial (also called a quadratic polynomial or a parabola). A particularly convenient form
for this purpose is
f2 (x) = ao + a1 (x − x1 ) + a2 (x − x1 )(x − x2 )
Note that although the above equation might seem to differ from the general polynomial introduced
at the beginning of this section, the two equations are equivalent. This can be shown by multiplying
the terms in the above equation to yield
a simple procedure can be used to determine the values of the coefficients, and it results in
8
ao = y1
y − y1
a1 = 2
x2 − x1
y3 − y2 y2 − y1
−
a2 =
x 3 − x 2 x 2 − x1
x3 − x1
Notice that, as was the case with linear interpolation, b1 still represents the slope of the line connecting
points x1 and x2 . Thus, the first two terms are equivalent to linear interpolation from x1 and x2 .
The last term, b2 (x − x1 )(x − x2 ), introduces the second-order curvature into the formula.
As was done previously with the linear and quadratic interpolations, data points can be used to
evaluate the coefficients ao , a1 , ..., an . For an nth-order polynomial, n + 1 data points are required:
[x1 , f (x1 )], [x2 , f (x2 )]..., [xn , f (xn )]. We use these data points and the following equations to evaluate the
coefficients:
ao = f (x1 )
a1 = f [x2 , x1 ]
an = f [xn+1 , xm , ....., x1 ]
where the bracketed function evaluations are finite divided differences. For example, the first finite
divided difference is represented generally as
9
f (xi ) − f (xj )
f [xi , xj ] =
xi − xj
The second finite divided difference, which represents the difference of two first divided differences, is
expressed generally as
f [xi , xj ] − f [xj , xk ]
f [xi , xj , xk ] =
xi − xk
Similarly, the nth finite divided difference is
fn (x) = f (x1 )+(x−x1 )f [x2 , x1 ]+(x−x1 )(x−x2 )f [x3 , x2 , x1 ]+.....+(x−x1 )(x−x2 )(x−xn )f [xn+1 , xn , ..., x1 ]
which is called Newton’s ’interpolating polynomial. It should be noted that it is not necessary that the
data points used be equally spaced or that the abscissa values necessarily be in ascending order.
The procedure for finding the coefficients by using divided differences can be followed in a divided
difference table as the one shown in the figure below for n = 5
• The spacings between the data points do not have to be the same.
• For a given set of n points, once the coefficients a1 through an are determined, they can be used for
interpolation at any point between the data points.
• After the coefficients a1 through an are determined (for a given set of n points), additional data
points can be added (they do not have to be in order), and only the additional coefficients have to
be determined.
10
In this section, simple linear functions will first be used to introduce some basic concepts and problems
associated with spline interpolation. Then we derive an algorithm for fitting quadratic splines to data.
Finally, we present material on the cubic spline, which is the most common and useful version in engineering
practice.
f (xi+1 ) − f (xi )
mi =
xi+1 − xi
These equations can be used to evaluate the function at any point between xo and xn by first locating
the interval within which the point lies. Then the appropriate equation is used to determine the function
value within the interval. The method is obviously identical to linear interpolation.
Visual inspection the primary disadvantage of first-order splines is that they are not smooth. In essence,
at the data points where two splines meet (called a knot), the slope changes abruptly. In formal terms, the
first derivative of the function is discontinuous at these points. This deficiency is overcome by using higher-
order polynomial splines that ensure smoothness at the knots by equating derivatives at these points, as
discussed in the next section.
11
3.4.2 Quadratic spline
To ensure that the mth derivatives are continuous at the knots, a spline of at least m + 1 order must be
used. Quadratic splines that ensure continuous first derivative at the knots and third-order polynomials
or cubic splines that ensure continuous first and second derivatives are most frequently used in practice.
We first illustrate the concept of spline interpolation using second-order polynomials. Although quadratic
splines do not ensure equal second derivatives at the knots, they serve nicely to demonstrate the general
procedure for developing higher-order splines.
The objective in quadratic splines is to derive a second-order polynomial for each interval between data
points. The polynomial for each interval can be represented generally as
fi (x) = ai x2 + bi x + ci
Fig.13
For n data points (i = 1, 2, . . . , n), there are n − 1 intervals and, consequently, 3n − 3 unknown
constants (the as, bs, and cs) to evaluate. Therefore, 3n−3 equations or conditions are required to evaluate
the unknowns. These are:
a1 x21 + b1 x1 + c1 = f (x1 )
• For i = 1 to i = n − 2
12
following: Assume that the second derivative is zero at the first point. Because the second derivative
is 2a1 , this condition can be expressed mathematically as
a1 = 0
The visual interpretation of this condition is that the first two points will be connected by a straight
line.
fi (x) = ai x3 + bi x2 + ci x + di
The conditions to be satisfied are
(xi+1 − x)3 (x − xi )3
fi (x) = fi00 (xi ) + fi00 (xi+1 ) +
6 (xi+1 − xi ) 6 (xi+1 − xi )
f 00 (xi )(xi+1 − xi
yi
− i (xi+1 − x)+
xi+1 − xi 6
f 00 (xi+1 )(xi+1 − xi
yi+1
− i (x − xi )
xi+1 − xi 6
The number of unknowns in each interval is two fi00 (xi ) and fi00 (xi+1 ). A total of 2(n − 1) = 2n − 2
unknowns. However since
13
fi0 (xi+1 ) = fi+1
0
(xi+1 )
14