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

3 views (last 30 days)
Javier Agustin Romero on 27 Mar 2019
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);
Javier Agustin Romero on 27 Mar 2019
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 on 27 Mar 2019
Edited: Catalytic on 27 Mar 2019
This might be what you mean -
ydata=[pks_locs2(2:end); pks_locs2(2:end)].';
init=[100 100 1000]; %[delta_HG1, delta_HG2 K]
delta_HG1=params(1);
delta_HG2=params(2);
K= params(3);
curve1=delta([delta_HG1,K] ,R,pks_locs1,G);
curve2=delta([delta_HG2,K] ,R,pks_locs2,G);
totalcurve=[curve1(:),curve2(:)];
end
Javier Agustin Romero on 28 Mar 2019
Yup, that does it. Thanks for the comment Matt!

Jhon MT on 10 Oct 2019
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
Javier Agustin Romero on 10 Oct 2019
Hello Jhon.
delta is the function func from the original post. Somewhere along the way I changed the name of it, sorry about that. Good luck!

### Categories

Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!