Author: erans Date: Wed Jun 6 23:48:49 2012 New Revision: 1347211 URL: http://svn.apache.org/viewvc?rev=1347211&view=rev Log: MATH-800 Updated userguide and added unit test.
Modified: commons/proper/math/trunk/src/site/xdoc/userguide/optimization.xml commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/fitting/PolynomialFitterTest.java Modified: commons/proper/math/trunk/src/site/xdoc/userguide/optimization.xml URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/site/xdoc/userguide/optimization.xml?rev=1347211&r1=1347210&r2=1347211&view=diff ============================================================================== --- commons/proper/math/trunk/src/site/xdoc/userguide/optimization.xml (original) +++ commons/proper/math/trunk/src/site/xdoc/userguide/optimization.xml Wed Jun 6 23:48:49 2012 @@ -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> Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/fitting/PolynomialFitterTest.java URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/fitting/PolynomialFitterTest.java?rev=1347211&r1=1347210&r2=1347211&view=diff ============================================================================== --- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/fitting/PolynomialFitterTest.java (original) +++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/fitting/PolynomialFitterTest.java Wed Jun 6 23:48:49 2012 @@ -26,6 +26,8 @@ import org.apache.commons.math3.optimiza 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() {