The CurveFitting Package
The CurveFitting package contains functions to fit various types of curves to given data points. This worksheet provides several simple examples to get you started with the package. Additional information and examples can be found on the help pages for the package and for the individual package members.
Introduction to the CurveFitting Package
restart
The following command allows you to use the short form of the function names in the CurveFitting package. Note that the PolynomialInterpolation, RationalInterpolation, Spline, and ThieleInterpolation functions replace the obsolete functions interp, ratinterp, spline, and thiele, respectively.
with⁡CurveFitting:
A feature of the CurveFitting package is that the routines allow you to specify the data points in two ways. The points can be provided as two lists, the first containing the independent values and the second containing the dependent values. The following command computes a polynomial that interpolates the points {(0,1), (1,3), (2,-1), (3,2)}.
PolynomialInterpolation⁡0,1,2,3,1,3,−1,2,v
136⁢v3−192⁢v2+283⁢v+1
The points can also be provided as a list of lists.
PolynomialInterpolation⁡0,1,1,3,2,−1,3,2,v
The ability to specify the data as a list of lists facilitates plotting the original data with the result. Example plots are provided in the next section. The following command allows you to use the short form of the function names in the plots package.
with⁡plots:
Fitting a Curve Through a Set of Points
Below, we define a list of 8 points, then plot these points by using the plots:-pointplot function. (Note that the short form of the function name is used below.)
points1:=0,1,1,2.5,3,2.3,4.2,5,5,3.5,5.8,4.2,7,7,8,10:pntplot1 ≔ pointplotpoints1,symbol=box:
The next command uses the PolynomialInterpolation function to compute the unique polynomial that interpolates the data points.
polycurve:=PolynomialInterpolation⁡points1,v
polycurve≔0.008728350920⁢v7−0.2626950683⁢v6+3.147340034⁢v5−18.98393186⁢v4+59.68279449⁢v3−89.75801494⁢v2+47.66577899⁢v+1
The plot command shown below generates a plot of the result. The plots:-display function displays both this plot and the previously computed plot of the data points. It can be seen that the degree 7 polynomial passes through each of the 8 data points.
polyplot:=plotpolycurve,v=0..8:displaypntplot1,polyplot
If n data points are supplied, then the interpolating polynomial has degree less than or equal to n-1. One well-known problem with fitting an interpolating polynomial through a large set of data points is the undesirable oscillations produced by a high-degree polynomial. In the plot above, the function value for v=2 is far from the value for v=1 or v=3.
A smoother curve can be obtained by using the Spline function. This function returns a piecewise polynomial of default degree 3 that passes through the 8 data points. The second derivative is set to zero at the endpoints, and this results in a "natural" spline. The degree of the spline can be controlled by the degree option, described on the Spline help page.
splcurve:=Spline⁡points1,v
splcurve≔1.+2.04045418559824⁢v−0.540454185598238⁢v3v<12.08090837119648+0.419091628803524⁢v−1.62136255679471⁢v−12+0.680908371196475⁢v−13v<3−4.01362556794713+2.10454185598238⁢v+2.46408767038414⁢v−32−1.95239379197454⁢v−33v<4.26.74715344898802−0.415988916425719⁢v−4.56452998072422⁢v−4.22+3.42595765782046⁢v−4.23v<59.20699091284593−1.14139818256919⁢v+3.65776839804488⁢v−52−1.42158833729175⁢v−53v<5.8−7.29317355087429+1.98158164670246⁢v+0.245956388544678⁢v−5.82+0.0393083474842046⁢v−5.83v<7−12.1918232823902+2.74168904034146⁢v+0.387466439487815⁢v−72−0.129155479829272⁢v−73otherwise
The plot below shows that a smoother curve, without the unwanted oscillations, is produced.
splplot:=plotsplcurve,v=0..8:displaypntplot1,splplot
The CurveFitting package includes two more interpolation functions, RationalInterpolation and ThieleInterpolation. The commands below show how to compute and plot a rational function that passes through a given set of points. Note that RationalInterpolation requires exact data, so floating-point values are not accepted.
points2:=0,1,1,3,32,1,3,4,4,5:pntplot2 ≔ pointplotpoints2,symbol=box:ratcurve ≔ RationalInterpolationpoints2,v;ratplot ≔ plotratcurve,v=0..4,discont=true:displaypntplot2,ratplot,view=0..4,0..5
ratcurve≔25⁢v2−19⁢v−24v2+17⁢v−24
Computing a Least Squares Fit
The LeastSquares function can be used to find a curve that best fits the data in a least-squares sense, that is, minimizes the sum of the squares of the differences between the estimated values and the actual data. Unlike the curves described in the previous section, the least-squares curve may not necessarily pass through the given points. The least-squares method is often used to fit models to experimental data.
The commands below show how the LeastSquares function is used to compute the best linear fit through the points defined at the beginning of the previous section. In the resulting plot, it can be seen that the curve passes near but not necessarily through all the points.
lscurve:=LeastSquarespoints1,v;lsplot ≔ plotlscurve,v=0..8:displaypntplot1,lsplot
lscurve≔0.528477546549837+0.919769989047098⁢v
In the previous example, the default curve used to fit the points is a linear polynomial av+b with parameters a and b. The LeastSquares function allows you to provide a different type of curve and to specify the parameters to optimize. In addition, weights associated with the data points can be defined. See the LeastSquares help page for more details about these options.
The LeastSquares function requires that the model curve be linear in the parameters. In some cases, a nonlinear model can be transformed to allow a least-squares fit. For example, if you wish to use the model
w=a⁢ⅇb⁢v,
you can take the logarithm of both sides of the equation and apply the transformation
y=log⁡w,c=log⁡a
to obtain the model
y=c+b⁢v,
which is linear in the parameters c and b.
newpoints1:=mapx→x1,evalf⁡log⁡x2,points1;newlscurve ≔ LeastSquaresnewpoints1,v
newpoints1≔0,0.,1,0.9162907319,3,0.8329091229,4.2,1.609437912,5,1.252762968,5.8,1.435084525,7,1.945910149,8,2.302585093
newlscurve≔0.274386709329829+0.238231965504746⁢v
This result gives values for c and b. The following commands show the resulting plot, after transforming back to the original model.
a:=ⅇcoeff⁡newlscurve,v,0:b≔coeffnewlscurve,v,1:newlsplot≔plota⁢ⅇb⁢v,v=0..8:displaypntplot1,newlsplot
Modeling with B-Spline Curves
The CurveFitting package contains the function BSplineCurve to compute a B-spline curve using a given set of control points. This function should not be confused with the BSpline function, which computes a single B-spline basis function. B-spline curves are commonly used in computer-aided design.
The commands below show how a B-spline curve is computed from 6 control points. The output from the BSplineCurve function is not shown, but it is a piecewise function in parametric form. The dependence of parts of the curve on various control points is defined by a set of "knots" automatically chosen by the algorithm. These knots also determine the range of the curve. A cubic, or order 4, B-spline curve is produced as the default. Refer to the BSplineCurve help page for information about the options to control the degree and to adjust the knots placement.
points3:=0,0,0.5,1,1.3,5,3,6,7.4,4,8,2:pntplot3≔pointplotpoints3,symbol=box:bsplcurve≔BSplineCurvepoints3,t:bsplplot:=plotbsplcurve:display⁡pntplot3,bsplplot
In the example above, try changing the values of the points or adding more points and then re-executing the series of commands to view the effect on the resulting B-spline curve.
References
Baker, M. and Hearn, D. Computer Graphics, Second Edition. Prentice-Hall, 1994.
Beckermann, B. and Labahn, G. "Fraction-free computation of matrix rational interpolants and matrix GCDs." SIAM Journal on Matrix Analysis and Applications, Vol. 22, No. 1. (2000): 114-144.
Beckermann, B. and Labahn, G. "Numeric and symbolic computation of problems defined by structured linear systems." Reliable Computing, Vol. 6, No. 4. (2000): 365-390.
Gerard, C. F. and Wheatley, P. O. Applied Numerical Analysis. Addison-Wesley Publishing Company, 1994.
Return to Index for Example Worksheets
Download Help Document