# [Solved] ‘fitnlm’ or ‘lsqcurvefit’ for non-linear least squares regression?

I am trying to fit experimental data to a third degree polynomial equation, using least squares. I have two independent variables and one dependent variable, which makes it a non-linear fit. I have calculated the coefficients with the functions ‘fitnlm’ and ‘lsqcurvefit’, both of which are recommended for nonlinear regression fits. I obtained different values of the coefficients from the two functions, although I input the same initial coefficient (guess) values. Kindly advise as to which of the two functions is better and whose coefficients I can trust. And, how do I check the value of the Root Mean Squared Error when using lsqcurvefit?

Thanks very much for any assistance/advise/helpful comments.

##
Solution #1:

It appears according to this matlab central discussion that nlinfit (and by extension fitnlm) uses the Levenberg-Marquardt algorithm. Also according to the doc page for lsqnonlin (which is the underlying function for lsqcurvefit) the default algorithm is ‘trust-region-reflective’ but Levenberg-Marquardt is also an option. If you specify the use of the L-M algorithm option in the lsqcurvefit function, do the results more closely match your fitnlm result?

As for how to choose which of the algorithm options in lsqcurvefit is better…well that’s the fun part of the science ðŸ˜‰ L-M does incorporate trust-region principles in its approach so there may be some theoretical overlap, and both are considered more robust than something like Nelder-Mead so I can’t think of much reason a priori to favor one over the other.

edit: Here is a mathworks source with discussion of the various non-linear equation solving algorithms MATLAB uses.