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

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

Introduction

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

Prototype

public VectorialPointValuePair optimize(final DifferentiableMultivariateVectorialFunction f,
        final double[] target, final double[] weights, final double[] startPoint)
        throws FunctionEvaluationException, OptimizationException, IllegalArgumentException 

Source Link

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. j  av  a  2  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");
    }
}

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

public void optimise() {
    LevenbergMarquardtOptimizer lmopt = new LevenbergMarquardtOptimizer();
    ObjectiveFunction f = new ObjectiveFunction(2);

    try {/*from   w ww. j ava  2s. c  o  m*/
        lmopt.optimize(f, dataset.getData(), weights, startPoint);
    } catch (OptimizationException e) {
        e.printStackTrace();
    } catch (FunctionEvaluationException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    }
}