## 13 Curve Fitting## 13.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 -
create an instance of the fitter using the
`create` factory method of the appropriate class, -
call the fit
with a
`Collection` of observed data points as argument, which will return an array with the parameters that best fit the given data points.
`fit` can be built by incrementally
adding instances to an instance of WeightedObservedPoints,
and then retrieve the list of `WeightedObservedPoint` by calling the `toList`
method on that container.
A weight can be associated with each observed point; it allows to take into account uncertainty
on some points when they come from noisy measurements for example. If no such information exist and
all points should be treated the same, it is safe to put 1.0 as the weight for all points (and this
is the default when no weight is passed to the `add` .
Some fitters require that initial values for the parameters are provided by the user,
through the ## 13.2 Implemented FunctionsFitting of specific functions are provided through the following classes: - PolynomialFitter fits a polynomial function.
- HarmonicFitter fits a harmonic function. An instance of the inner class ParameterGuesser can be used to retrieve initial values for the fitting procedure.
- GaussianFitter fits a Gaussian function. An instance of the inner class ParameterGuesser can be used to retrieve initial values for the fitting procedure.
The following example shows how to fit data with a polynomial function. // Collect data. final WeightedObservedPoints obs = new WeightedObservedPoints(); obs.add(-1.00, 2.021170021833143); obs.add(-0.99, 2.221135431136975); obs.add(-0.98, 2.09985277659314); obs.add(-0.97, 2.0211192647627025); // ... Lots of lines omitted ... obs.addt(0.99, -2.4345814727089854); // Instantiate a third-degree polynomial fitter. final PolynomialCurveFitter fitter = PolynomialCurveFitter.create(3); // Retrieve fitted parameters (coefficients of the polynomial function). final double[] coeff = fitter.fit(obs.toList()); ## 13.3 General CaseThe AbstractCurveFitter class provides the basic functionality for implementing other curve fitting classes. Users must provide their own implementation of the curve template as a class that implements the ParametricUnivariateFunction interface. |