Difference fit result form nlinfit and lsqcurvefit

25 views (last 30 days)
Hi all
I have found online that nlinfit and lsqcurvefit are based on the same principles, but could it be that I got a different fit from one than from the other? I used both, but got better results using lsqcurvefit.
Thank you!

Accepted Answer

John D'Errico
John D'Errico on 4 Feb 2023
And, so?
Many problems will have multiple locally optimal solutions. Neither of those tools even hopes to be a globally optimal solver. So at best, they find a solution based on where you start them out. The solution is one where no search direction will yield an improvement over the current point, but it may easily be a local minimum.
However, they are different solvers. Given the same problem, two different solvers need not converge to the same solution. This is a term called a basin of attraction.
On this specific example, you got a better result from lsqcurvefit. That is irrelevant, as on the next problem, nlinfit might do better.
If you want better results, then use better starting values. That is always true. If you want better help than this, then show your data and tell us what starting values you used, and of course the model being fit.
  5 Comments
John D'Errico
John D'Errico on 4 Feb 2023
@Wout Laeremans And you clearly did not read my answer.
Yes. It is entirely possible they will generate a different solution, given the same model, AND the same starting values. This is entirely possible. I said EXACTLY that. Different optimizers do not need to converge to the same solution.
Do the two codes use (by default) the same algoritm? NO. They do not.
Nlinfit uses Levenberg-Marquardt, whereas lsqcurvefit uses a trust-region-reflective code. Levenberg-Marquardt is an OPTION for lsqcurvefit, but not the default. Even if you tell lsqcurvefit to use LM, I still would not gaurantee the two implementations are identical, so they may still have different basins of attraction.
Have you made some mistake in your code? It appears the two calls you made are similar. But since they were made to two different algorithms, go back and read my answer. They do not need to converge to the same result.
Wout Laeremans
Wout Laeremans on 4 Feb 2023
Dear Jhon
Thank you for your answer, now it is perfectly clear.
However if you answer questions in the future, you might want to be more helpful. If my question is not clear, you can give suggestions how to improve. I did read your answer carefully and you said "If you want better results, then use better starting values." This has nothing to do with the algorithm behind a model. That is why your answer was not clear.

Sign in to comment.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!