Search in sources :

Example 1 with LeastSquaresOptimizer

use of org.apache.commons.math3.fitting.leastsquares.LeastSquaresOptimizer in project imagingbook-common by imagingbook.

the class NonlinearLeastSquares method solveGaussNewton.

/**
 * Solves the nonlinear least-squares problem defined by the arguments
 * using Gauss-Newton optimization.
 *
 * @param V the "value" function, V(p) must return a vector for the current parameters p
 * @param J the "Jacobian" function, J(p) must return a matrix for the current parameters p
 * @param z the vector of observed ("target") values
 * @param p0 initial parameter vector
 * @return the vector of optimal parameters
 */
public static RealVector solveGaussNewton(MultivariateVectorFunction V, MultivariateMatrixFunction J, RealVector z, RealVector p0) {
    LeastSquaresProblem problem = makeProblem(V, J, z, p0);
    LeastSquaresOptimizer optimizer = new GaussNewtonOptimizer();
    Optimum solution = optimizer.optimize(problem);
    // System.out.println("iterations = " + solution.getIterations());
    return solution.getPoint();
}
Also used : Optimum(org.apache.commons.math3.fitting.leastsquares.LeastSquaresOptimizer.Optimum) GaussNewtonOptimizer(org.apache.commons.math3.fitting.leastsquares.GaussNewtonOptimizer) LeastSquaresProblem(org.apache.commons.math3.fitting.leastsquares.LeastSquaresProblem) LeastSquaresOptimizer(org.apache.commons.math3.fitting.leastsquares.LeastSquaresOptimizer)

Example 2 with LeastSquaresOptimizer

use of org.apache.commons.math3.fitting.leastsquares.LeastSquaresOptimizer in project imagingbook-common by imagingbook.

the class NonlinearLeastSquares method solveLevenvergMarquardt.

/**
 * Solves the nonlinear least-squares problem defined by the arguments
 * using Levenberg-Marquardt optimization.
 *
 * @param V the "value" function, V(p) must return a vector for the current parameters p
 * @param J the "Jacobian" function, J(p) must return a matrix for the current parameters p
 * @param z the vector of observed ("target") values
 * @param p0 initial parameter vector
 * @return the vector of optimal parameters
 */
public static RealVector solveLevenvergMarquardt(MultivariateVectorFunction V, MultivariateMatrixFunction J, RealVector z, RealVector p0) {
    LeastSquaresProblem problem = makeProblem(V, J, z, p0);
    LeastSquaresOptimizer optimizer = new LevenbergMarquardtOptimizer();
    Optimum solution = optimizer.optimize(problem);
    // System.out.println("iterations = " + solution.getIterations());
    return solution.getPoint();
}
Also used : Optimum(org.apache.commons.math3.fitting.leastsquares.LeastSquaresOptimizer.Optimum) LevenbergMarquardtOptimizer(org.apache.commons.math3.fitting.leastsquares.LevenbergMarquardtOptimizer) LeastSquaresProblem(org.apache.commons.math3.fitting.leastsquares.LeastSquaresProblem) LeastSquaresOptimizer(org.apache.commons.math3.fitting.leastsquares.LeastSquaresOptimizer)

Example 3 with LeastSquaresOptimizer

use of org.apache.commons.math3.fitting.leastsquares.LeastSquaresOptimizer in project imagingbook-common by imagingbook.

the class NonlinearLeastSquares method solveNLS2.

// --------------------------------------------------------------------
// book version
// --------------------------------------------------------------------
public static RealVector solveNLS2(MultivariateVectorFunction V, MultivariateMatrixFunction J, RealVector z, RealVector p0) {
    LeastSquaresProblem problem = LeastSquaresFactory.create(model(V, J), z, p0, null, MaxEvaluations, MaxIterations);
    LeastSquaresOptimizer optimizer = new LevenbergMarquardtOptimizer();
    Optimum solution = optimizer.optimize(problem);
    // System.out.println("iterations = " + solution.getIterations());
    if (solution.getIterations() > MaxIterations)
        return null;
    else
        return solution.getPoint();
}
Also used : Optimum(org.apache.commons.math3.fitting.leastsquares.LeastSquaresOptimizer.Optimum) LevenbergMarquardtOptimizer(org.apache.commons.math3.fitting.leastsquares.LevenbergMarquardtOptimizer) LeastSquaresProblem(org.apache.commons.math3.fitting.leastsquares.LeastSquaresProblem) LeastSquaresOptimizer(org.apache.commons.math3.fitting.leastsquares.LeastSquaresOptimizer)

Aggregations

LeastSquaresOptimizer (org.apache.commons.math3.fitting.leastsquares.LeastSquaresOptimizer)3 Optimum (org.apache.commons.math3.fitting.leastsquares.LeastSquaresOptimizer.Optimum)3 LeastSquaresProblem (org.apache.commons.math3.fitting.leastsquares.LeastSquaresProblem)3 LevenbergMarquardtOptimizer (org.apache.commons.math3.fitting.leastsquares.LevenbergMarquardtOptimizer)2 GaussNewtonOptimizer (org.apache.commons.math3.fitting.leastsquares.GaussNewtonOptimizer)1