Hello,
I need to perform curve fitting where the function is of the form: ax^b.
I saw that polynomial fitting is already implemented in
commons.math, but a function like the one I need is not.
Then I found the CurveFitter class and proceeded like below,
but I haven't succeeded. Any suggestion?
Thanks, Fabio
class PowerFunction implements ParametricRealFunction {
// overriding
public double[] gradient(double x, double[] parameters) {
double vet[] = new double[10];
double coef, exp, newExp, coefTimesExp, power;
if ( x==0 ) {
vet[0] = 0;
}
else {
coef = parameters[0];
exp = parameters[1];
coefTimesExp = coef * exp;
newExp = exp-1;
power = Math.pow( x, newExp );
vet[0] = coefTimesExp * power;
}
return vet;
} // gradient
// overriding
public double value(double x, double[] parameters) {
double coef, exp, power;
coef = parameters[0];
exp = parameters[1];
power = Math.pow( x, exp );
return ( coef * power );
} // value
} // class PowerFunction