How to optimize a fit with nlinfit

19 views (last 30 days)
Michal
Michal on 13 Jul 2015
Edited: Walter Roberson on 19 Jul 2015
I wrote a model (with 7 ode) and want to fit it to experimental data. Most of the parameters values were taken from scientific literature. When I run the non-linear fit (using nlinfit), I get:
(1) lot of warnings like:
Warning: Rank deficient, rank = 10, tol = 2.103668e+005.
> In nlinfit>LMfit at 327
In nlinfit at 162
In main at 37
(2) some non physical parameter values (like a negative value for size), or extremely different estimation comparing to the literature measurement.
(3) when I slightly change my parameter guess, I get either a different curve, or errors (like: Error using nlinfit>checkFunVals (line 362) The function you provided as the MODELFUN input has returned Inf or NaN values, or: Failure at t=2.403593e+00...), or even no output at all.
Does anyone no what to do to get a good and reasonable fit?
Attached is my code.
Thanks

Answers (2)

John D'Errico
John D'Errico on 19 Jul 2015
Edited: Walter Roberson on 19 Jul 2015
Too often people think that there is some magic salve they can apply to a nonlinear optimization to make it work. I suppose this is because others seem to make them work with no apparent problems. So what is wrong here? Where is the disconnect? There may be several reasons for failure. I'll just guess at a couple of them.
  • Your data may be pure crap, insufficient to fit the model you desperately need to use. (And, no, I'm not insulting you or your data. But it is often the case that people have wildly insufficient data, and have no idea this is the case.) I don't know that, since I've not seen any data from you. But it is very often the case. A rank deficiency warning may well be a good hint of that possibility though. The answer is to get better data that will indeed support that model.
  • On the other end, your data may be reasonable, but you may have simply chosen a model that has no possibility of being fit, thus an insane choice of model for this data. Again, I've seen no model, so I can only hazard a wild guess. But too often people try to fit high order polynomial models, or sums of many exponential terms. In either case it is trivially easy to generate problems that will see rank deficiencies, and there are many other poor choices of model one can make. The answer is probably to choose a better model.
  • You may have serious scaling issues. So if some of your parameters are scaled to be on the order of 1e10, and others on the order of 1, then expect to see issues in the numerical linear algebra. This is easily fixable of course, by a simple scaling. Just use a better choice of units, thus perhaps kilometers versus nanometers.
  • You may have chosen poor starting values, that cause the optimizer to diverge into limbo, to get lost. The answer to this is to choose better starting values.
It is not always easy to get better data. You may have what you have and cannot get more. Or you may be forced to use a specific model. And better starting values are often difficult to guess. Sorry, but not all things are trivial to do. The easy problems are already solved anyway.
Of course, one reason why some people manage to make their optimization work more easily is they know these things already. Throwing a different optimizer at the problem usually won't help. It will still probably see problems.
SOMETIMES a constrained optimizer might help IF you can find an intelligent set of constraints. That is, keep the problem from going to the bad place.
Another thing that often can help is to use a partitioned nonlinear least squares estimation scheme, since the reduces the effective nonlinear dimensionality of the problem. It makes the search space smaller, and thus easier to search. You can find tools for this in my own fminspleas, or in the pleas code I posted with my optimization tips and tricks submission.)
  1 Comment
Michal
Michal on 19 Jul 2015
Hi John,
Thank you for your reply.
I've attached to my original question an m. file of my model including the experimental data, so you can have a look. Yes, my model might be crap, but I can say for sure that I have problem no. 3 (non scaled values), and as I've mentioned, I chose parameter values from scientific literature (which could be crap as well).
I'll try to see how this partitioned nonlinear least squares estimation scheme works..
Thanks,
Michal

Sign in to comment.


Abhishek Pandey
Abhishek Pandey on 16 Jul 2015
Edited: Abhishek Pandey on 16 Jul 2015
Hello,
I understand that you want to fit a model to experimental data.
I tried running your code, and to find the line where the warning is generated, I executed the following command on MATLAB Command Window before running your code:
dbstop if warning
This stops execution of the code at the line wherever a warning is generated.
This showed that the warning is generated in line 574 of “nlinfit.m”. Here, I saw that the rank of the matrix used for the solution is 9, whereas the number of unknowns is 11 which results in an ill-conditioned matrix.
You could consider using the “ fmincon ” function. If your problem can be formulated as an optimization problem, the function would be able to find a solution. You would have to set up the objective function and define the error (for example least square, weighted least square, maximum likelihood and so on) to be used with this formulation. Please note that if there are multiple solutions, “fmincon” function may provide different solutions for different initial conditions.
I hope that helps!
- Abhishek
  2 Comments
Michal
Michal on 19 Jul 2015
Thank you Abhishek!
Let me see if I understand. Suppose now my model (M) is a function of 7 variables (let's call them X) with a set of parameters p0, so I can say: M(X,p0). Now I need to write a new function f(p) where my new variables are a set of parameters p: f(p)=error(M(X,p)-data). I should look for min(f) with fmincon which hopefully lead my to the best non-linear fit.
I looked at the fmincon help information. So for x = fmincon(fun,x0,A,b), fun=f(b), x0=p0, but how can I choose A and b?
And if there are multiple solutions for different initial conditions, that brings me back to the problem I had with nlinfit: how can I choose the best set?
Thank you so much for your help!
Michal
John D'Errico
John D'Errico on 19 Jul 2015
Reformulating it as an fmincon problem will probably NOT help here.
The problem will still be that the data is inadequate to estimate the parameters.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!