# CurveFitter.fit(ParametricRealFunction, double[]) always returns the same value as the initial guess when used with the LevenbergMarquardtOptimizer

XMLWordPrintableJSON

#### Details

• Bug
• Status: Closed
• Major
• Resolution: Invalid
• 2.0
• None
• Java, Ubuntu 9.04 (64 bit)

#### Description

CurveFitter.fit(ParametricRealFunction, double[]) always returns the same value as the initial guess when used with the LevenbergMarquardtOptimizer and the length of the initial guess array is 1. Here is my example code:

CurveFitter with LevenbergMarquardtOptimizer
```  LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer();
CurveFitter fitter = new CurveFitter(optimizer);
SimpleInverseFunction sif = new SimpleInverseFunction(); // Class provided below
double[] initialguess = new double[1];
initialguess[0] = 1.0d;
double[] bestCoefficients = fitter.fit(sif, initialguess); // <---- ALWAYS RETURNS A VALUE OF initialguess !

/**
* This is my implementation of ParametricRealFunction
* Implements y = ax^-1 + b for use with an Apache CurveFitter implementation
*/
private class SimpleInverseFunction implements ParametricRealFunction
{
public double value(double x, double[] doubles) throws FunctionEvaluationException
{
//y = ax^-1 + b
//"double[] must include at least 1 but not more than 2 coefficients."
if(doubles == null || doubles.length ==0 || doubles.length > 2) throw new FunctionEvaluationException(doubles);
double a = doubles[0];
double b = 0;
if(doubles.length >= 2) b = doubles[1];
return a * Math.pow(x, -1d) + b;
}
public double[] gradient(double x, double[] doubles) throws FunctionEvaluationException
{
//derivative: -ax^-2
//"double[] must include at least 1 but not more than 2 coefficients."
if(doubles == null || doubles.length ==0 || doubles.length > 2) throw new FunctionEvaluationException(doubles);
double a = doubles[0];
double b = 0;
if(doubles.length >= 2) b = doubles[1];
double derivative = -a * Math.pow(x, -2d);
}
}
```

#### People

Unassigned
Daren Drummond