i'm trying to rewrite my Matalab prototype for some DSP to C++ and encountering a displeasing problem. I'm trying to fit data to a function $y = a * (\pi / 2 + arctg(b * x))$. In Matlab it works well with the following code:
fo = fitoptions('Method', 'NonlinearLeastSquares',...
'Lower', [0, 0],...
'Upper', [Inf, max(x)],...
'StartPoint', [1, 1]);
[curve2, gof2] = fit(x,y,ft);
In curve2
I get a
and b
coefficients, which are pretty well.
I try to reproduce this in C++ with Alglib's lsfit
using Levenberg–Marquardt algorithm:
alglib::real_1d_array y, c;
alglib::real_2d_array x;
... // filling x and y arrays with data
c = "[1.0, 1.0]";
alglib::ae_int_t maxits = 0;
alglib::ae_int_t info;
alglib::lsfitstate state;
alglib::lsfitreport rep;
alglib::lsfitcreatef(x, y, c, 1e-4, state);
alglib::lsfitsetcond(state, 1e-5, maxits);
lsfitsetbc(state, "[0.0, 0.0]", "[+inf, 5.0]");
alglib::lsfitfit(state, func);
lsfitresults(state, info, c, rep);
At the output (c
) I get completely different coefficeints which are doesn't have any correlation with the same from Matlab.
x
and y
are the same in Matlab and C++ program. The only difference I see is the optimization algorithm: in Matlab I use trust-region method when in C++ I use Levenberg–Marquardt algorithm.
Could you explain me that strange behaviour?