Example usage for org.apache.commons.math.optimization.general LevenbergMarquardtOptimizer getRMS

List of usage examples for org.apache.commons.math.optimization.general LevenbergMarquardtOptimizer getRMS

Introduction

In this page you can find the example usage for org.apache.commons.math.optimization.general LevenbergMarquardtOptimizer getRMS.

Prototype

public double getRMS() 

Source Link

Document

Get the Root Mean Square value.

Usage

From source file:uk.ac.diamond.scisoft.analysis.fitting.AngleDerivativeFunction.java

/**
 * Fit points given by x, y datasets to an ellipse. If no initial parameters are given, then
 * an algebraic fit is performed then a non-linear least squares fitting routine is used to
 * provide the best geometric fit./*from   w  w w  .java2  s . c o  m*/
 * @param x
 * @param y
 * @param init parameters (can be null)
 */
public void geometricFit(AbstractDataset x, AbstractDataset y, double[] init) {

    if (x.getSize() < PARAMETERS || y.getSize() < PARAMETERS) {
        throw new IllegalArgumentException("Need " + PARAMETERS + " or more points");
    }

    if (init == null)
        init = quickfit(x, y);
    else if (init.length < PARAMETERS)
        throw new IllegalArgumentException("Need " + PARAMETERS + " parameters");

    EllipseCoordinatesFunction f = new EllipseCoordinatesFunction(x, y);
    LevenbergMarquardtOptimizer opt = new LevenbergMarquardtOptimizer();

    try {
        VectorialPointValuePair result = opt.optimize(f, f.getTarget(), f.getWeight(),
                f.calcAllInitValues(init));

        double[] point = result.getPointRef();
        parameters[0] = point[0] * point[0];
        parameters[1] = point[1] * point[1];
        for (int i = 2; i < PARAMETERS; i++)
            parameters[i] = point[i];

        logger.info("Ellipse fit: rms = {}, x^2 = {}", opt.getRMS(), opt.getChiSquare());
    } catch (FunctionEvaluationException e) {
        // cannot happen
    } catch (IllegalArgumentException e) {
        // should not happen!
    } catch (ConvergenceException e) {
        throw new IllegalArgumentException("Problem with optimizer converging");
    }
}