I want to perform a least square fitting on my data set using this code :
Code:
void function(const alglib::real_1d_array &coeff, const alglib::real_1d_array &x, double &func, void *ptr)
{
func = coeff[0] / pow( 1 + coeff[1] * coeff[2] * x[0] , 1 / coeff[3] );
}
void main()
{
int N = 10;
int C = 4;
double epsx = 0.000001;
double diffstep = 0.0001;
alglib::lsfitstate state;
alglib::lsfitreport report;
alglib::ae_int_t maxits = 0;
alglib::ae_int_t info;
alglib::real_2d_array xAxis;
xAxis.setlength(N, 1);
alglib::real_1d_array yAxis;
yAxis.setlength(N);
alglib::real_1d_array cArray;
cArray.setlength(C);
/* Filling arrays .... */
std::cout << " x " << xAxis.tostring(2) << std::endl; //printing xarray
std::cout << " y " << yAxis.tostring(2) << std::endl; // printing yarray
std::cout << " c " << cArray.tostring(10) << std::endl; //printing yarray
alglib::lsfitcreatef(xAxis,yAxis, cArray, diffstep, state);
alglib::lsfitsetcond(state, epsx, maxits);
alglib::lsfitfit(state, function);
lsfitresults(state, info, cArray, report);
std::cout << " Best fit coefficients " << cArray.tostring(12) << std::endl;
std::cout << " Informations " << int(info) << std::endl;
}
Here is what i get in my console :
x [[0.00],[1.00],[2.00],[3.00],[4.00],[5.00],[6.00],[7.00],[8.00],[9.00]]
y [0.00,184.00,184.00,184.00,184.00,184.00,554.00,356.00,340.00,340.00]
c [554.0000000000,1.0000000000,0.0060000000,554.0000000000]
Best fit coefficients []
Informations about algorithm -8
I dont know what is the signification of the error code -8.
I dont get any coeffcients as a result. But i think my mistake comes from the data x, y i am using because when i use the dataset(only x and y array, i still keep my function that i want to fit my data and the coeff array) provided in alglib documentation, i get this results :
x [[-1.00],[-0.80],[-0.60],[-0.40],[-0.20],[0.00],[0.20],[0.40],[0.60],[0.80],[1.00]]
y [0.22,0.38,0.58,0.79,0.94,1.00,0.94,0.79,0.58,0.38,0.22]
c [554.0000000000,1.0000000000,0.0060000000,554.0000000000]
Best fit coefficients [0.621302545335,0.999951946642,0.000084880705,553.999951049188]
Informations 2
I obtain coefficients . So, my first guess is that my mistake comes from the x and y data i am using but i dont know how to explain it or maybe i am missing smth. I would appreciate some help. I apoligize for my poor english and if you need further informations about my code, just let me know.
Thank in advance,
Julien.