Sunteți pe pagina 1din 5

Curve fitting by Lagrange interpolation

G. Bäckström

Citation: Computers in Physics 7, 213 (1993); doi: 10.1063/1.168461


View online: https://doi.org/10.1063/1.168461
View Table of Contents: https://aip.scitation.org/toc/cip/7/2
Published by the American Institute of Physics

ARTICLES YOU MAY BE INTERESTED IN

Parametric curve fitting: An alternative to Lagrange interpolation and splines


Computers in Physics 8, 722 (1994); https://doi.org/10.1063/1.168489

Savitzky-Golay Smoothing Filters


Computers in Physics 4, 669 (1990); https://doi.org/10.1063/1.4822961

Parametric cubic spline-fitting programs for open and closed curves


Computers in Physics 6, 472 (1992); https://doi.org/10.1063/1.168433

Chebyshev Polynomials: After the Spelling the Rest is Easy


Computers in Physics 8, 161 (1994); https://doi.org/10.1063/1.4823278

Data smoothing using low-pass digital filters


Review of Scientific Instruments 48, 1447 (1977); https://doi.org/10.1063/1.1134918

Binomial smoothing filter: A way to avoid some pitfalls of least-squares polynomial smoothing
Review of Scientific Instruments 54, 1034 (1983); https://doi.org/10.1063/1.1137498
Curvefitting by Lagrangeinterpolation
G. Backstrom
Physics Institute, University of Ume& S-901 87 Urned, Sweden
(ReceivedI May 1992; accepted5 November 1992)

It is demonstrated that interpolation by quadratic polynomials using the Lagrange formula


may compete in accuracy with cubic spline interpolation, while being simpler to implement. It
is also shown that Lagrange interpolation may easily be used to fit an almost arbitrary function
to experimental data. FORTRAN routines for Lagrange interpolation as well as Lagrange fitting
are supplied separately.

lNTRODUCTlOW I. INTERPOLATIONBY A POLYNOMIAL


In experimental research one frequently measuressome Before proceeding further, we need to consider some dif-
quantity Q) against a vector X of independent variables ferent interpolation procedures.’Let the function values
(say: frequency, magnetic induction and temperature). fj=f(Xj) be known for j=l...m. Our preliminary
One eventually wishes to fit a function f(X) to the data problem is to estimate f(x) for intermediate values of x.
yti such that the standard deviation becomesa minimum. The most popular method is to generate a polyno-
Usually a probable form for the function may be derived mial Q(x) of order m, having the required property:
from a mathematical model of the process. If a falling Q(xj) = fj. The polynomial coefficients may be found in
body is observed, for instance, its vertical coordinate varrous ways, e.g., by solving a system of m linear
may be taken to be f(t) =pl +p2t+p3?, t being the time equations. However, unless f(x) is particularly simple,
and P=Cp1p2p3] the vector of parameters to be the range of x restricted and the number of sample
determined. More generally, the fitting function may be points (m) small, the result may be discouraging. The
written f (X,P), where X is the vector of independent polynomial often oscillates wildly between sample points
variables (if more than one) and P the vector of at large magnitudes of x.
parameters to be varied in searching for the best fit.
Procedures for least-squaresfitting in the case of a
single independent variable (X=x) are now available in
some plot packages for IBM-compatible PCs, such as II. INTERPOLATIONBY SPLICED POLYNOMIALS
SigmaPlot from Jandel Scientific and EasyPlot from Spi- A safer interpolation scheme consists in using a polyno-
ral Software. More general routines are found in FOR- mial of low order, and to determine its coefficients from
TRAN libraries, such as NAG from the Numerical Algo- neighboring points (Xi, fj) according to specific rules. A
rithms Group (Oxford, UK). set of coefficients is thus determined for each interval
However, it also happensthat no functional form is between adjacent sample points Xi (knots). If a third-
available at the time of the measurements,and that one order polynomial is chosen, say, the three coefficients
just wishes to obtain a curve which conveniently summa- pertaining to the interval xl<x <x2 is derived from
rizes the measured values. In that case one may replace f(xi), f(xz), and f(xl) and so on for each following
the fitting function by samplesfj of such a function at Xj interval, except the last one where f(xmmz), f(x,_l),
( i= l...m), together with a detailed recipe of and f(x,) must be used. Since the polynomials
interpolation between the values given. (The vector (Xi) generally are different on the left and right side of a given
typically has much fewer elements than the vector (xi) knot Xj, the derivatives of the interpolating functions are
of measured values.) This is equivalent to a closed also slightly different.
expression for the fitting function over the span of the As an example, let us interpolate f(x) = sin(x) over
values Xi. The samples fj may then be considered to the interval shown in Fig. 1 This function is a suitable
constitute the parameter vector P of the fitting function. object for tests, since all derivatives vary to a large extent.
The vector of sample points [Xi) is chosen by the user. Figure 1 also illustrates a particular choice of the sample
An appropriate fitting procedure would then permit the vector {xi). Using these knot values only, we now
unknown vector P to be determined, after which the construct a set of m- 1 polynomials of order 3
best-fitting function would be completely defined. The (quadratic) as described above. Such polynomials may
main purpose of this paper is to describe a simple fitting be expressedby the Lagrange formula ( 1) as follows,
procedure pertinent to this case. using the index j to number the intervals:

COMPUTERS IN PHYSICS, VOL. 7. NO. 2, MAWAPR 1993 213


, 1 " 8 II", I "&'I' 0 '1,
1.0 -
_.*-'---m.
.b _
/=

,I

c
P* 5- /

,/"

FIG. 2. Residual after quadratic Lagrange interpolation between the 11


!' sample points (knots) indicated in Fig. 1.

o-/:,
I I I, I I I I I I I, I, I I
0 5 1.0 1.5 2.0 that of Fj(X); hence the residual will be small. Figure 2
x
also shows that the first derivative generally is
discontinuous at the knots.
FIG. 1. Graph of the function y=sin(x), showing the 11 sample points Ill. INTERPOlATlON BY SPLINES
used for interpolation. This and the following figures were produced
using EasyPlot (Spiral Software). In order to avoid discontinuous derivatives, spline meth-
ods have been devised. Since the subject of splines is well
established and has been reviewed several times,2’3’4the
reader will only be reminded of the main principles. In
(x-xj+l 1 (X-Xj+2)
the cubic spline scheme a fourth-order polynomial is
defined in each subinterval, using the conditions that not
F”x)=(xi-Xj+*)(Xj-Xj+2) fj only the function value but also the first and second
derivatives be continuous at every knot. This provides an
(x-xj+Z) (X-Xi)
effective way of interpolating analytic functions, but
+(xj+l-xj+~)(Xj+l-Xj) fi+l there is one problem: The procedure leaves two
coefficients undetermined for the total set of
txexj) (X-Xj+l)
(1) polynomials. Unless these can be found from known
+ (xj+2-Xj) (Xj+2-Xj+l) fj+2. relations between derivatives, say, the resulting
This expression obviously is quadratic in x and satisfies interpolation function may oscillate viciously between
Fj(X) = fj at the knots Xi, Xj+l, and Xj+~ It will prove knots.
convenient to use the following abbreviated form: Figure 3 shows the residual of a spline interpolation
of the function in Fig. 1, using the routines included in
the standard MatLab package (MathWorks,MA). These
routines contain algorithms for optimizing the two addi-
tional coefficients. Evidently, the standard deviation of
(2) the spline is about an order of magnitude smaller than
that of the Lagrange interpolation, if compared at the
A FORTRAN implementation of this function is available same number of knots. However, the spline requires
through the Internet service offered by this journal. [The about four times as many coefficients for calculating the
FORTRAN source code referred to in the text is listed polynomial functions than the Lagrange method, the lat-
under the file names, backstroml, backstrom2, and back- ter being based directly on the knot values. Hence, it
Strom3 in CIP’s anonymous ftp directory. To accessthese would be more reasonableto compare at the same num-
listings, users should ftp to pinet.aip.org and go to the ber of data values. Figure 4 shows that the standard de-
subdirectory “tip sourcecode.”For more information on viation of a Lagrange interpolation with 41 knots is com-
this service, contact the FIP Account Manager at
pumOpinet.aip.org.1
If we apply the Lagrange formula (2) to the ten
intervals of Fig. 1, we obtain the interpolation indicated 4x10.’
by the dashed curve. On this scale, it is indistinguishable
from the original sine curve, but we may assessthe qual- z
f 2xw’
ity of interpolation by plotting the residual, i.e., the dif- A
g
ference between the interpolated values and the original 0
function sin(x) . As may be seenfrom Fig. 2, the residual
takes its largest values at small x and virtually vanishesat -2xy)”
0 5 70 15 20
about x = 1S. The second derivative of Fj (x) is constant x
for a given interval ( j), but its value depends on j.
Where the original function has a small third derivative, FIG. 3. Residual after spline interpolation between the sample points
its second derivative will be nearly constant and close to indicated in Fig. 1.

214 COMPUTERS IN PHYSICS, VOL. 7, NO. 2, MAWAPR 1993


I
.osraJ !, 10 q.5 20 to 1.5
5 5

FIG. 4. Residual after quadratic Lagrange interpolation using four FIG. 5. Residual after quadratic Lagrange least-squares fit to the 11
times shorter intervals than in Fig. 1. points shown in Fig. 1.

n 2 2
parableto that of a spline with 11 knots. Thus it appears
that in caseswhere knot values are easy to calculate, X2= izl W i Yi- C. qj, jo2(xi)fj+j, * (4)
( i02=0 )
Lagrangeinterpolation would be an equally good choice
as spline interpolation. Furthermore,Lagrangeinterpola- The parametervector ( fk] which m inimizes x2 is the
tion is unique, whereasa spline leavestwo extra coeffi- one which makesthe following derivativesequal to zero
cients to be determined.F inally, the Lagrangedata are for k= l...m :
the knots themselves,e.g., those shown in F ig. 1, which
already give a good idea of the behaviorof the function. ax’ j,
afk=2 W i Yi- 2 Vj, jo2(xi)fj+j02
The spline coefficients,on the other hand, bear no simple ( j02=0 1
relation to the function curve.
Any of the abovemethodsmay be used,with some X (-qj,k--j(Xi))Ak-j=Q (5)
caution, when interpolating data known to high precision where A,_j= 1 if k- j=O...2 and, otherwise, A,-i=O.
at all Xi. Routines using a single polynomial up to sixth Rearranging the terms, we obtain a system of linear
order, as well as cubic splines,are normally included in equations(k= l...m) in its usual form:
plot packages.Even if splines in particular may be
n
excellent for interpolation of mathematical functions,
2 Wgj,k-j(Xi)Ak-j i qj, jo2(xi)fj+jo2
known to high precisionat a lim ited number points, one i=1 io2=0
may have serious reservations about their use with
experimentally measuredvalues. Clearly, least-squares
fitting would be the preferredtreatment of experimental
data.
This system of equations may be solved for ( fk) by
IV. CONVENTIONALLEAST-SQUARESFITS standard methods. A FORTRAN implementation usin
the Gauss-Jordan routine from Numerical Recipes,?
Least-squaresfitting routines for polynomials’ up to converted to double precision, is available through the
sixth order are commonly availablein plot packagesfor Internet serviceof this journal. Sourcelistings may also
PCs, but there are no correspondingroutines for splines. be suppliedby the author on request(seeSec. II).
FORTRAN subroutinesfor both purposesare availablein F igure 5 showsan application of the Lagrangefit to
libraries such as IMSL (Houston, Texas) and NAG data points taken from F ig. 1 (n=600, m= 11). Al-
(Numerical Algorithms Group, Oxford, UK). However, though the generalshapeof the residualis similar to that
a single polynomial may not yield an acceptablefit over obtainedby interpolation (Fig. 2)) the standarddeviation
an extendedrange of x, and a spline fit may oscillate if hasbeenreducedto about one-half.Hence,a Lagrangefit
the two free parametersare not properly controlled. may be used to advantageeven when approximatingan
No fitting routines basedon low-order interpolation exact function.
betweenneighboringknots seemto be availablecommer- F igure 6 shows a Lagrangefit to simulateddata (n
cially. An implementation of such a simple method of = 600, m = 2 1), obtainedby adding Gaussiannoise to a
fitting will be describedin the following. function consisting of a polynomial plus two Gaussian
peaks (which m ight occur in spectroscopy).Constant
V. lA6RAN6E LEAST-SQUARESFIT weights and constant interval lengths were still used in
this example. F igure 7 demonstratesthe expectedran-
W e are now ready to consider a least-squaresfit using domnessof the residual.
Lagrangefunctions F.(x), as defined in Eqs. ( 1) and The usefulnessof low-order Lagrangeinterpolation
(2). The objective is thus to m inimize the expression has thus been illustrated in the caseof one independent
n variable.The extensionto severalvariablesis possibleand
x2= C W k i-F j(Xi))2, only requirestrivial repetition.
i=l The Lagrangefit may obviouslybe generalized,per-
where W i is the statistical weight of yi and the index j is haps advantageously,to higher-orderpolynomials.
determined by the value of Xi as describedin Sec. II. Routines have also been given for Lagrangefitting
Introducing the expressionfor Fj(X), we obtain and its potential demonstratedusing a realistic example.

COMPUTERS IN PHYSICS, VOL. 7, NO. 2, MAR/APR 1993 21s


. . .
t 1
. St0 -3 0 .s 10
x

FIG. 7. Residual after the quadratic Lagrange fit illustrated by Fig. 6.

(xk) and take averagesfor the two overlappingelements


-.501 . . a ’ ’ a * ’
-1.0 -5 0 5 1.0 of t fkl.
Whether one wishes to fit an interpolating function
x
to measureddata or simply approximatea mathematical
function, the standard deviation may be reducedby op-
FIG. 6. Quadratic Lagrange fit to 600 simulated data points obtained by timizing the size of the intervals of [xk j , using unequally
adding Gaussian noise to a polynomial plus two Gaussian peaks. The spacedknots at constant total number m.
fitting function was based on 21 equally spaced sample points.
ACKNOWLEDGMENT

The author expectstheseroutines to be included, in suit- The author wishesto thank R. G . Ross for his comments.
ably modified form, in future packagesfor plotting and
analysison PCs. For commercialuse,the generalpurpose
equation solver should be replacedby an algorithm that
exploits the banded character of the coefficient matrix. 1. W. H. Press,B. P. Plannery,S. A. Teukolsky,and W. T. V&Ming, Namer-
ical Recipes The Art of Scientifi Computing (FORTRAN) (CambridgeU. P.,
Thousands of data points might then be managed. It Cambridge,MA, 1989).
would also be advantageousto sort the input x data in 2. G. E. Forsythe, M. A. Malcolm, and C. B. Moler, Computer Methods for
Mathematical Computations (Prentice-Hall, EngelwoodCliffs, NJ, 1971).
nondescending order, since this would shorten the 3. C. de Boor, A Practical Guide to Splines in Applied Math. Sciences Vol. 27
j-search loop. Finally, it would be possibleto splice con- (Springer-Verlag,New York, 1978).
secutivefits, since there is no long-rangedependencybe- 4. W. J .Tbompson,Computing for Scientbts and Engineers (Wiley, New York,
1992).
tween the elementsof the output vector { fk). It should 5. Ph. R. Bevington,Data Reduction and Ermr Analysisfor the Physical Sciences
be sufficient to overlap successivefits by one interval of (McGraw-Hill, New York, 1969).

216 COMPUTERS IN PHYSICS, VOL 7, NO. 2, MAR/APR 1993

S-ar putea să vă placă și