lsqcurvefit error: Objective function is returning undefined values at initial point. lsqcurvefit cannot continue.

3 views (last 30 days)
Hello dear all, I am fitting a multiple set of measurements to a function (my non-linear model function) and trying to get the parameters that fit the measurement to the data at best. The model consists of two parameters to be fitted. I have around 4000 measurements (these are actually pixels of images, but irrelevant here...), so I call lsqcurvefit 4000 times. I use the lsqcurvefit with options Levenberg-Marquardt, upper and lower bounded, analytical Jacobian provided by me. I give the lsqcurvefit the same initial point for every measurement (i.e. for each call of the lsqcurvefit). For most of the measurement, the algorithm works and returns me proper values for the parameters. But for some measurements, it gives out the error:
"Objective function is returning undefined values at initial point. lsqcurvefit cannot continue."
I check where the error comes from, it is generated by the built-in matlab function 'snls' called by lsqcurvefit. When I monitor the workings in debug mode and look at the values the function has, it indeed has some 'Inf' values in it, so the error is correctly generated. However, I don't understand why I get 'Inf' for some values of the function, the model function never becomes infinity inside the boundaries I provide the lsqcurvefit with. Plus, I always give the same initialization to each call of lsqcurvefit and most of the times it works, sometimes not. Another plus, when I look at the values of the function in the debug mode, I see different values each time, which is wrong, because the model is the same and the initialization is the same, thus the evaluation of the model at the initialization has to yield the exact same result. Any help or comment will be appreciated. Thanks, Kerem

Accepted Answer

Matt J
Matt J on 2 May 2014
Edited: Matt J on 2 May 2014
I use the lsqcurvefit with options LevenbergMarquardt, upper and lower bounded.
Levenberg Marquardt does not support bound constraints. lsqcurvefit should have warned you about this. It's probably ignoring the bounds.
Plus, I always give the same initialization to each call of lsqcurvefit and most of the times it works, sometimes not.
You are fitting different data every time, so the least squares function is different every time.
  2 Comments
Matt J
Matt J on 5 May 2014
Kerem tezcan Commented:
Thanks Matt J, 1.Yeah, you're right LM does not accept boundaries, so it is just changing the algorithm to trust-region internally somewhere in the code. But it does not matter for my application for now, just gave it for clarity. 2. You are right again, the objective function is different, however it is sth of the form |model-data|||^2. And the reason that this becomes infinite is that the evaluation of the model with the given parameters has an infinite value in it. Otherwise, the data has to be infinite, which is not the case. So the function evaluation is giving infinity at some value, but this is also incorrect. I hope you will give me another reply. Thanks a lot,
Matt J
Matt J on 5 May 2014
Kerem tezcan Commented:
Dear Matt J, I have spotted the problem, thanks to your comment directing me to the objective function. This was the problem: The model function I have is an exponential function, thus it can never be equal to zero. The data also has to follow this, however due to noise, it takes the value zero at some points. Of course the fitting algorithm cannot work, if the model cannot possibly explain the data, whatever the parameters are, and the objective function goes to infinity at that data point. I will fix this zero problem and it should be fine. Thanks again, Kerem

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!