# lsqcurvefit: is the output of this function unique for a given set of initial conditions (and deterministic model)?

7 ビュー (過去 30 日間)
David Haw 2019 年 11 月 11 日
Answered: John D'Errico 2019 年 11 月 11 日
I am getting different parameter estimates when i re-run. My code is as follows:
xdata=4:13;
ydata=data(xdata,:);
fun=@(x,xdata)pandemic1Dall2(x,xdata,0,0);
x0=[3,220,.77,.7,1.4,-.9];
x=lsqcurvefit(fun,x0,xdata,ydata);
Any insight appreciated,
Thanks,
David

#### 0 件のコメント

サインイン to comment.

### 件の回答 (2)

John D'Errico 2019 年 11 月 11 日
The problem is, that is not your code. Not all of it. That is just how you call lsqcurvefit. We do not see the pandemic1Dall2 code, nor do we have your data.
What I cannot be sure of is if your function really is deterministic. I've seen many people claim that their function is deterministic, when it is not, since something inside their code uses random numbers. Sometimes they used something in MATLAB that uses something random as a starting value. For example, eigs and svds are not deterministic. fit from the curve fitting toolbox is not, when used wihout starting values.
Is lsqcurvefit deterministic? Yes, I am pretty sure that it is. A quick test that I just ran shows results for consecutive calls to be identical to every single bit of the result on a simple 4 parameter problem.
If you will not show us what we need to determine if you are using something that is not in fact deterministic, then all I can suggest is you do a few simple tests:
1. Call your function (fun) TWICE, with the same set of values as coefficients. Do not reset the random seed in MATLAB between calls.
2. Compare the two sets of results from fun. Are they IDENTICALLY equal, down to the least significant bit?
This completely isolates the problem from lsqcurvefit. If they are not identical, then you have your answer. Your function is not indeed deterministic.
Next, perform the same operation, but now setting the random seed for MATLAB before each call to a fixed known number. Again, compare the results after each call.
This second test, even if your function was not deterministic, should now return the same results.
You can also perform the same pair of tests, but now calling lsqcurvefit too. But if your function is deterministic, then lscurvefit is too. I am confidant that lsqcurvefit is deterministic, and I have seen no indications it is not. For example, if you search the code for lsqcurvefit, the string "rand" (or any other random number generator in MATLAB) is NOT to be found in the code.
As well, depite the fact that lsqcurvefit "can" be sensitive to starting values, if you pass it exactly the same inputs, there is noting in it that will ever return any different result.
So, next, make sure that ALL data was identical between calls. Far too often I have seen people not realize that something was not indeed identical, even though being willing to swear on a stack of bibles that they had done exactly the same thing.
For example, it is terribly common for someone to use a set of numbers from one computation in their code. Then they copy those numbers from the command window, using them again, or so they think. But in fact, when they do that, they only copy a short approximation to the numbers, since what appears in the command window is only correct to 4 significant digits. Then they get upset, not understanding why they got different results. It happens all the time.
But if you want a better answer, then you really need to provide both your data AND all of your code for this problem.

#### 0 件のコメント

サインイン to comment.

Star Strider 2019 年 11 月 11 日
All nonlinear parameter estimation algorithms are sensitive to the initial parameter estimates. It is quite possible that they will encounter a local minimum rather than a global minimum if the initial estimates are not in the region of the global minimum. For this reason, there are several optimisation functions that search the global parameter space to get the best fit. See Global Optimization Toolbox — Functions for a list of several.

#### 2 件のコメント

David Haw 2019 年 11 月 11 日
Thanks for your answer, though this happens with the same initial parameter estimates. I literally just re-run exactly the same code, and get a different answer.
Star Strider 2019 年 11 月 11 日
My pleasure.
It could be that the values you chose are either in a relatively ‘flat’ area of the parameter space, so that they could go in any number of directions, or that you are encountering the effects of floating-point approximation error. See the documentation section on Floating-Point Numbers for that discussion.
Experiment with different initial estimates to see if the estimation improves.

サインイン to comment.

サインイン してこの質問に回答します。

R2016a

Translated by