Curve fitting

Curve fittingis the process of constructing acurve, ormathematical function, that has the best fit to a series ofdata points,possibly subject to constraints.Curve fitting can involve eitherinterpolation,where an exact fit to the data is required, orsmoothing,in which a "smooth" function is constructed that approximately fits the data.  A related topic isregression analysis,which focuses more on questions ofstatistical inferencesuch as how much uncertainty is present in a curve that is fitted to data observed with random errors. Fitted curves can be used as an aid for data visualization,to infer values of a function where no data are available,and to summarize the relationships among two or more variables.Extrapolationrefers to the use of a fitted curve beyond therangeof the observed data,and is subject to adegree of uncertaintysince it may reflect the method used to construct the curve as much as it reflects the observed data.
For linear-algebraic analysis of data, "fitting" usually means trying to find the curve that minimizes the vertical (y-axis) displacement of a point from the curve (e.g.,ordinary least squares). However, for graphical and image applications, geometric fitting seeks to provide the best visual fit; which usually means trying to minimize theorthogonal distanceto the curve (e.g.,total least squares), or to otherwise include both axes of displacement of a point from the curve. Geometric fits are not popular because they usually require non-linear and/or iterative calculations, although they have the advantage of a more aesthetic and geometrically accurate result.
Most commonly, one fits a function of the formy=f(x).
The first degreepolynomialequation
is a line withslopea.  A line will connect any two points, so a first degree polynomial equation is an exact fit through any two points with distinct x coordinates.
If the order of the equation is increased to a second degree polynomial, the following results:
This will exactly fit a simple curve to three points.
If the order of the equation is increased to a third degree polynomial, the following is obtained:
This will exactly fit four points.
A more general statement would be to say it will exactly fit fourconstraints.  Each constraint can be a point,angle, orcurvature(which is the reciprocal of the radius of anosculating circle).  Angle and curvature constraints are most often added to the ends of a curve, and in such cases are calledend conditions.  Identical end conditions are frequently used to ensure a smooth transition between polynomial curves contained within a singlespline.  Higher-order constraints, such as "the change in the rate of curvature", could also be added.  This, for example, would be useful in highwaycloverleafdesign to understand the rate of change of the forces applied to a car (seejerk), as it follows the cloverleaf, and to set reasonable speed limits, accordingly.
The first degree polynomial equation could also be an exact fit for a single point and an angle while the third degree polynomial equation could also be an exact fit for two points, an angle constraint, and a curvature constraint.  Many other combinations of constraints are possible for these and for higher order polynomial equations.
If there are more thann+ 1 constraints (nbeing the degree of the polynomial), the polynomial curve can still be run through those constraints. An exact fit to all constraints is not certain (but might happen, for example, in the case of a first degree polynomial exactly fitting threecollinear points). In general, however, some method is then needed to evaluate each approximation. Theleast squaresmethod is one way to compare the deviations.
There are several reasons given to get an approximate fit when it is possible to simply increase the degree of the polynomial equation and get an exact match.:
The degree of the polynomial curve being higher than needed for an exact fit is undesirable for all the reasons listed previously for high order polynomials, but also leads to a case where there are an infinite number of solutions.  For example, a first degree polynomial (a line) constrained by only a single point, instead of the usual two, would give an infinite number of solutions.  This brings up the problem of how to compare and choose just one solution, which can be a problem for software and for humans, as well.  For this reason, it is usually best to choose as low a degree as possible for an exact match on all constraints, and perhaps an even lower degree, if an approximate fit is acceptable.
Other types of curves, such astrigonometric functions(such as sine and cosine), may also be used, in certain cases.
In spectroscopy, data may be fitted withGaussian,Lorentzian,Voigtand related functions.
In biology, ecology, demography, epidemiology, and many other disciplines, thegrowth of a population, the spread of infectious disease, etc. can be fitted using thelogistic function.
Inagriculturethe inverted logisticsigmoid function(S-curve) is used to describe the relation between crop yield and growth factors. The blue figure was made by a sigmoid regression of data measured in farm lands. It can be seen that initially, i.e. at low soil salinity, the crop yield reduces slowly at increasing soil salinity, while thereafter the decrease progresses faster.
If a function of the formy=f(x){\displaystyle y=f(x)}cannot be postulated, one can still try to fit aplane curve.
Other types of curves, such asconic sections(circular, elliptical, parabolic, and hyperbolic arcs) ortrigonometric functions(such as sine and cosine), may also be used, in certain cases.  For example, trajectories of objects under the influence of gravity follow a parabolic path, when air resistance is ignored.  Hence, matching trajectory data points to a parabolic curve would make sense.  Tides follow sinusoidal patterns, hence tidal data points should be matched to a sine wave, or the sum of two sine waves of different periods, if the effects of the Moon and Sun are both considered.
For aparametric curve, it is effective to fit each of its coordinates as a separate function ofarc length; assuming that data points can be ordered, thechord distancemay be used.
Coopeapproaches the problem of trying to find the best visual fit of circle to a set of 2D data points. The method elegantly transforms the ordinarily non-linear problem into a linear problem that can be solved without using iterative numerical methods, and is hence much faster than previous techniques.
The above technique is extended to general ellipsesby adding a non-linear step, resulting in a method that is fast, yet finds visually pleasing ellipses of arbitrary orientation and displacement.
Note that while this discussion was in terms of 2D curves, much of this logic also extends to 3D surfaces, each patch of which is defined by a net of curves in two parametric directions, typically calleduandv.  A surface may be composed of one or more surface patches in each direction.
Manystatistical packagessuch asRandnumerical softwaresuch as thegnuplot,GNU Scientific Library,Igor Pro,MLAB,Maple,MATLAB, TK Solver 6.0,Scilab,Mathematica,GNU Octave, andSciPyinclude commands for doing curve fitting in a variety of scenarios. There are also programs specifically written to do curve fitting; they can be found in thelists of statisticalandnumerical-analysis programsas well as inCategory:Regression and curve fitting software.