Fit two data sets with 3 fit parameters, one parameter is the same

조회 수: 2 (최근 30일)
Luke Smith
Luke Smith 2020년 5월 22일
댓글: Luke Smith 2020년 5월 22일
I have 2 data sets: x1,y1 & x2,y2 (different lengths).
% k is a constant
I want to fit:
f1 = @ (p,x1) p(1)./sqrt((x1-k).^2+p(2)^2)+p(3);
f2 =@ (p, x2) p(4)./sqrt((x2-k).^2+p(5)^2)+p(3);
Both data sets should be fit to the same p(3). I would like the fitting to return [p(1) p(2) p(3) p(4) p(5)]
Can I do this with lsqcurvefit?
I hope this makes sense,
Luke

채택된 답변

Rik
Rik 2020년 5월 22일
As far as I'm aware, you can only fit a single function at once, so you can try something like this:
f1 = @ (p,x1) p(1)./sqrt((x1-k).^2+p(2)^2)+p(3);
f2 =@ (p, x2) p(4)./sqrt((x2-k).^2+p(5)^2)+p(3);
cost_function=@(p) sum((f1(p,x1) - y1).^2) + sum((f2(p,x2) - y2).^2);
Now you can minimize cost_function, which will find the optimal values of p.
Alternatively (and that is probably a better idea), you can just concatenate the two:
L=[true(numel(x1),1);false(numel(x2),1)]
f_=@(p,x)[f1(p,x(L));f2(p,x(~L))];
  댓글 수: 1
Luke Smith
Luke Smith 2020년 5월 22일
Thank you! I concatenated the two and it works really well, thank you for such a quick reply.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by