MATLAB: Fit multiple sets of data with same function model to get optimal global parameter using lsqcurvefit

fittingGlobal Optimization ToolboxlsqcurvefitOptimization Toolbox

Hello everyone. I have been using lsqcurvefit to get optimal parameter values for different sets of data. I want to find optimal values for delta_HG and K for each set of data. I can already do that separately, which means using lsqcurvefit on each set of data. The values I get are delta_HG=58 and K=1742 for data set pks_locs1, and delta_HG=1541 and K=1750 for data set pks_locs2. As you can see, delta_HG values are very different, but K values are very similar (as it should be). What I want to do now is to combine these fits into a single one. Output should be both delta_HG values and a single K value (the value which best fits BOTH data sets). I have been looking at similar questions asked here but none really helped me. Below is the code of what I've got so far. Any comments and/or suggestions are much appreciated.
G=2.529e-4;
R=[0.1:0.1:0.4 0.6:0.2:1.8 2.1 2.4:0.4:3.2 3.8 4.8 6 8.4 12.8 20 36];
pks_locs1=[136 123.5 121 120.5 124 119 118 114 111.5 109.5 108 103 103.5 101.5 98 97 96 90 84 77.5 74.5 56 63.5];
pks_locs2=[903 920 951.5 964.5 944 1006 1026 1050.5 1105.5 1112.5 1122.5 1169 1177 1201 1229 1247 1235.5 1321 1354 1404.5 1444 1499.5 1474.5];
init=[100 1000;100 1000];
R=[R;R];
pks=[pks_locs1;pks_locs2];
fitted=lsqcurvefit(@(param,x) func(param,x,pks,G),init,R,pks(:,2:end));
function f=func(param,R,pks_locs,G)
delta_HG=param(1);
K=param(2);
f=pks_locs(1)+(delta_HG-pks_locs(1))*(G*K.*(1+R)+1-sqrt((G*K.*(1+R)+1).^2-R.*(2*K*G).^2))./(2*K*G);

Best Answer

This might be what you mean -
ExtraData.R=R;
ExtraData.G=G;
ExtraData.pks_locs1=pks_locs1;
ExtraData.pks_locs2=pks_locs2;
ydata=[pks_locs2(2:end); pks_locs2(2:end)].';
init=[100 100 1000]; %[delta_HG1, delta_HG2 K]
paramsFitted = lsqcurvefit(@combinedModel,init,ExtraData,ydata);
function totalcurve = combinedModel(params, ExtraData)
delta_HG1=params(1);
delta_HG2=params(2);
K= params(3);
R=ExtraData.R;
G=ExtraData.G;
pks_locs1=ExtraData.pks_locs1;
pks_locs2=ExtraData.pks_locs2;
curve1=delta([delta_HG1,K] ,R,pks_locs1,G);
curve2=delta([delta_HG2,K] ,R,pks_locs2,G);
totalcurve=[curve1(:),curve2(:)];
end