Documente Academic
Documente Profesional
Documente Cultură
G. Bäckström
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)
,I
c
P* 5- /
,/"
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.
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.
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).