MATLAB: Inconsistent results produced by lsqcurvefit using the same setting and initial starting points

curve fittinglsqcurvefitmultistartnonlinear least squareOptimization Toolbox

Hi. I tried to estimate the optimal parameters using the lsqcurvefit function in the optimization toolbox but I found that I always got different results using exactly the same setting and the same initial starting points! As you may find when you run the code, the answers lsqcurvefit gives you are not the same! (The algorithm I used is the default trust region reflective) This seems pretty spooky to me because every time I run the script I might get different answers and I have completely no idea where I went wrong. Please help! Thank you guys so much in advance!!
% See attachment for yData,S,H and function Predict
%%Please load in yData,S,H and set path for Predict before running
XRadius=12.4; % x range
YRadius=11.4; % y range
[xX,yY] = meshgrid(-XRadius:0.2:XRadius,-YRadius:0.2:YRadius);
xData = zeros(horzcat(size(xX),2));
xData(:,:,1) = xX; % x-coordinate of the Gaussian grid
xData(:,:,2) = yY; % y-coordinate of the Gaussian grid
for times = 1:30
InitGuess = [1.0000 -4.2000 -11.4000 0.2000
1.0000 -4.6000 -11.4000 0.2000]; % Initial searching points
options = optimoptions(@lsqcurvefit, 'MaxFunEvals', 250000,'MaxIter', 15000,'TolX',0.01);
PHandle = @(PInit,xData) Predict(PInit,xData,S,H);
Problem = createOptimProblem ('lsqcurvefit','x0', InitGuess(1,:),'objective',PHandle,... % Set up the PROBLEM structure
'lb',[-Inf,-XRadius,-YRadius,0.2],'ub',[Inf,XRadius,YRadius,5],'xdata',xData,'ydata',yData,'options',options);
tpoints = CustomStartPointSet(InitGuess);
ms = MultiStart('UseParallel', true); % Use parallel processing
[Para,RSS]= run(ms,Problem,size(InitGuess,1));
disp([Para,RSS]); % Display results
end
As mentioned, I got two different answers:
Running the local solvers in parallel.
Run Local Local Local Local First-order
Index exitflag f(x) # iter F-count optimality
1 2 9.751 3 20 0.007618
2 2 9.751 20 105 7.013e-05
MultiStart completed the runs from all start points.
All 2 local solver runs converged with a positive local solver exit flag. 0.5601 -3.9837 -11.4000 0.2000 9.7510
Running the local solvers in parallel.
Run Local Local Local Local First-order
Index exitflag f(x) # iter F-count optimality
1 2 9.751 3 20 0.007618
2 2 5.545 18 95 0.001615
MultiStart completed the runs from all start points.
All 2 local solver runs converged with a positive local solver exit flag. 0.7281 -4.0394 1.4072 0.2116 5.5448

Best Answer

Thank you for providing complete data so that diagnosing your problem was easy.
You never passed in your custom start point set tpoints. You only passed in its size, 2, so MultiStart ran two independent randomly-started runs for each of your 30 requested calculations.
Instead, you should call it like this:
[Para,RSS]= run(ms,Problem,tpoints);
Alan Weiss
MATLAB mathematical toolbox documentation