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

6 次查看(过去 30 天)
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);
  5 个评论
Catalytic
Catalytic 2019-3-27
The model function that you are trying to have is not clear. In the code you have posted, your model function has two unknowns, but your init has 4 unknowns. How many unknowns are there supposed to be?
Javier Agustin Romero
Well I don't know how to feed the function properly, that's pretty much the whole deal. In that example I'm trying to fit 2 data sets, so there are 3 unlnowns: the 2 delta_HG values and the K value. I thought I had to give initial parameter values as if calculating 4 unknowns, but clearly that is not the case.

请先登录,再进行评论。

采纳的回答

Catalytic
Catalytic 2019-3-27
编辑:Catalytic 2019-3-27
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
  3 个评论
Matt J
Matt J 2019-3-28
编辑:Matt J 2019-3-28
A small typo,
ydata=[pks_locs1(2:end); pks_locs2(2:end)].';
With that fixed, the rest of Catalytic's solution works fine for me.
delta_HG1 =
58.3229
delta_HG2 =
1.5406e+03
K =
1.7500e+03

请先登录,再进行评论。

更多回答(1 个)

Jhon MT
Jhon MT 2019-10-10
Hi everyone, please could you explain me what does "delta" do here? : "curve1=delta([delta_HG1,K] ,R,pks_locs1,G);", I have matlab R2017a and when I run the code this is what I got: Undefined function or variable 'delta'.
Thanks a lot.
Regards

类别

Help CenterFile Exchange 中查找有关 Get Started with Optimization Toolbox 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by