## 17 Curve Fitting## 17.1 Overview
The fitting package deals with curve fitting for univariate real functions.
When a univariate real function y = f(x) does depend on some unknown parameters
p
For all provided curve fitters, the operating principle is the same. Users must first
create an instance of the fitter, then add the observed points and once the complete
sample of observed points has been added they must call the ## 17.2 General CaseThe CurveFitter class provides curve fitting for general curves. Users must provide their own implementation of the curve template as a class implementing the ParametricUnivariateFunction interface and they must provide the initial guess of the parameters. The following example shows how to fit data with a polynomial function. final CurveFitter fitter = new CurveFitter(new LevenbergMarquardtOptimizer()); fitter.addObservedPoint(-1.00, 2.021170021833143); fitter.addObservedPoint(-0.99, 2.221135431136975); fitter.addObservedPoint(-0.98, 2.09985277659314); fitter.addObservedPoint(-0.97, 2.0211192647627025); // ... Lots of lines omitted ... fitter.addObservedPoint( 0.99, -2.4345814727089854); // The degree of the polynomial is deduced from the length of the array containing // the initial guess for the coefficients of the polynomial. final double[] init = { 12.9, -3.4, 2.1 }; // 12.9 - 3.4 x + 2.1 x^2 // Compute optimal coefficients. final double[] best = fitter.fit(new PolynomialFunction.Parametric(), init); // Construct the polynomial that best fits the data. final PolynomialFunction fitted = new PolynomialFunction(best); ## 17.3 Special CasesThere are more specialized classes, for which the appropriate parametric function is implicitly used: - PolynomialFitter fits a polynomial function
- HarmonicFitter fits a harmonic function
- GaussianFitter fits a Gaussian function
The |