Hello,
i have problem with nonlinear fitting. If I am using only method without gradient, its all Ok. But then I want to use gradient and I cant ger correct result. What is wrong with my code?
Thanks for answer.
Code:
Code:
void function_cx_1_func(const alglib::real_1d_array &c, const alglib::real_1d_array &x, double &func, void *ptr){
func = c[0]+c[1]*x[0];
}
void function_cx_1_grad(const alglib::real_1d_array &c, const alglib::real_1d_array &x, double &func, alglib::real_1d_array &grad, void *ptr) {
func = c[0]+c[1]*x[0];
grad = "[0, 0]";
grad[0] = 1;
grad[1] = x[0];
}
int main(int argc, char **argv){
alglib::real_2d_array x = "[[1], [2], [3], [4], [5]]";
alglib::real_1d_array y = "[10, 15, 20, 25, 30]";
alglib::real_1d_array c = "[1, 1]";;
double epsf = 0;
double epsx = 0.00001;
alglib::ae_int_t maxits = 0;
alglib::ae_int_t info;
alglib::lsfitstate state;
alglib::lsfitreport rep;
//
// Fitting without weights
//
alglib::lsfitcreatefg(x, y, c, true, state);
alglib::lsfitsetcond(state, epsf, epsx, maxits);
alglib::lsfitfit(state, function_cx_1_func, function_cx_1_grad);
alglib::lsfitresults(state, info, c, rep);
std::cout<<(int)info<<" "<<rep.rmserror<<" "<<c.tostring(1)<<std::endl;
return 0;
}