MATLAB: Lsqcurvefit and multistart visibly not giving me a good fit. What am I doing wrong

bad fitglobal fitGlobal Optimization Toolboxlsqcurvefitmultistart

I'm trying to do minimization for two variables in this code to match some output data that I got experimentally. I first tried it with just the lsqcurvefit but I kept getting that the "initial point is a local minimum". So I have tried now to use the MultiStart command with the Global optimization toolbox to get a "better" local minimum (hopefully to find the global minimum). However, upon plotting the results, it is obvious that the fit found for my two variables is no a very good one to my data. I have tried varying the starting point but end up with fits that are just as bad.
black circles is the fit and the colours are my collected data.
Here is my code:
%data=readtable('808nm/data808.xlsx');
X=data{:,2};
Y=data{:,1};
options = optimoptions('lsqcurvefit','OptimalityTolerance', 1e-16, 'FunctionTolerance', 1e-16);
x0=[10 ;10];
lb=[0 0];
ub=[10 50];
[x,resnorm]=lsqcurvefit(@myfunc,x0,X,Y,lb,ub,options)
problem= createOptimProblem('lsqcurvefit','x0',x0,'objective',@myfunc,'lb',lb,'ub',ub,'xdata',X,'ydata',Y);
ms=MultiStart('PlotFcns',@gsplotbestf);
[xprob,errormulti]=run(ms,problem,2000)
figure(1);
plot(source5_808(:,2),log(source5_808(:,1)),'b+',source6_808(:,2),log(source6_808(:,1)),'r+',source7_808(:,2),log(source7_808(:,1)),'g+');
title('808nm');
xlabel('Distance (cm)');
ylabel ('Ln(Fluence) (W cm^{-2})');
hold on;
xx=2:1:9;
plot(X,log(myfunc(xprob,X)),'ko')
and here is my function
% function Y = myfunc(x,X,Y)
Y=((0.2./(4.*pi.*(1./(3.*(x(1)+x(2)))).*X))).*(exp(-1.*((3.*x(1).*(x(1)+x(2))).^(0.5)).*X));
return
end
and finally the input file for the X and Y data attached

Best Answer

I am not sure that your objective function is defined properly. The objective function is supposed to have two input variables, x and xdata (which I think you call X). It should NOT have Y as an input variable, and I don't understand why you have a return statement. I think that it should be this:
function Y = myfunc(x,X)
Y=((0.2./(4.*pi.*(1./(3.*(x(1)+x(2)))).*X))).*(exp(-1.*((3.*x(1).*(x(1)+x(2))).^(0.5)).*X));
end
Also, your plot seems to be the log of the response against the input data. Is your model the log of the response or is it the response itself? If you are fitting just the response, not the log of the response, then I think that you are making a modeling error, as the absolute difference between your model and the response is quite small, but the difference of the logs is large.
Alan Weiss
MATLAB mathematical toolbox documentation