Updated userguide and added unit test.


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1347211 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gilles Sadowski 2012-06-06 23:48:49 +00:00
parent 6525baf194
commit 3e83661989
2 changed files with 44 additions and 9 deletions

View File

@ -607,25 +607,36 @@ C: 16.324008168386605
the <a
href="../apidocs/org/apache/commons/math3/analysis/ParametricUnivariateFunction.html">
ParametricUnivariateFunction</a> interface and they must provide the initial guess of the
parameters. The more specialized <a
href="../apidocs/org/apache/commons/math3/optimization/fitting/PolynomialFitter.html">
PolynomialFitter</a> and <a
href="../apidocs/org/apache/commons/math3/optimization/fitting/HarmonicFitter.html">
HarmonicFitter</a> classes require neither an implementation of the parametric real function
not an initial guess as they are able to compute them by themselves.
parameters.
</p>
<p>
An example of fitting a polynomial is given here:
The following example shows how to fit data with a polynomial function.
</p>
<source>PolynomialFitter fitter = new PolynomialFitter(degree, new LevenbergMarquardtOptimizer());
<source>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);
PolynomialFunction fitted = fitter.fit();
// 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);
</source>
<p>
The more specialized <a href="../apidocs/org/apache/commons/math3/optimization/fitting/HarmonicFitter.html">
HarmonicFitter</a> classes requires neither an implementation of the parametric real function
nor an initial guess as it is are able to compute them internally.
</p>
</subsection>
</section>
</body>

View File

@ -26,6 +26,8 @@ import org.apache.commons.math3.optimization.general.GaussNewtonOptimizer;
import org.apache.commons.math3.optimization.general.LevenbergMarquardtOptimizer;
import org.apache.commons.math3.optimization.SimpleVectorValueChecker;
import org.apache.commons.math3.util.FastMath;
import org.apache.commons.math3.random.RandomDataImpl;
import org.apache.commons.math3.TestUtils;
import org.junit.Test;
import org.junit.Assert;
@ -35,6 +37,28 @@ import org.junit.Assert;
* polynomial.
*/
public class PolynomialFitterTest {
@Test
public void testFit() {
final RandomDataImpl rng = new RandomDataImpl();
rng.reSeed(64925784252L);
final LevenbergMarquardtOptimizer optim = new LevenbergMarquardtOptimizer();
final CurveFitter fitter = new CurveFitter(optim);
final double[] coeff = { 12.9, -3.4, 2.1 }; // 12.9 - 3.4 x + 2.1 x^2
final PolynomialFunction f = new PolynomialFunction(coeff);
// Collect data from a known polynomial.
for (int i = 0; i < 100; i++) {
final double x = rng.nextUniform(-100, 100);
fitter.addObservedPoint(x, f.value(x));
}
// Start fit from initial guesses that are far from the optimal values.
final double[] best = fitter.fit(new PolynomialFunction.Parametric(),
new double[] { -1e-20, 3e15, -5e25 });
TestUtils.assertEquals("best != coeff", coeff, best, 1e-12);
}
@Test
public void testNoError() {