The CurveFitting Package
The CurveFitting package, new in Maple 7, 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
|
|
>
|
|
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.
>
|
|
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)}.
>
|
|
| (1.1) |
The points can also be provided as a list of lists.
>
|
|
| (1.2) |
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.
>
|
|
|
|
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.)
>
|
|
The next command uses the PolynomialInterpolation function to compute the unique polynomial that interpolates the data points.
>
|
|
| (2.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.
>
|
|
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.
>
|
|
| (2.2) |
The plot below shows that a smoother curve, without the unwanted oscillations, is produced.
>
|
|
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.
>
|
|
|
|
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.
>
|
|
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
,
you can take the logarithm of both sides of the equation and apply the transformation
to obtain the model
,
which is linear in the parameters c and b.
>
|
|
| (3.1) |
This result gives values for c and b. The following commands show the resulting plot, after transforming back to the original model.
|
|
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.
>
|
|
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
|