Fitting two sets of data to one function simultaneously
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi
I have a question regarding fitting two sets of data to a non-linear function simultaneously. My real problem is much more complicated than this example, but if I understand how to do this, I can also do my real problem.
Assume that you have dataset 1: (x1,y1) dataset 2: (x2,y2)
I want to fit these data to a function F=mx+b. (a linear function, is not actually non-linear, my real problem is non-linear though). I also know that my two sets of data are common in "b" and are different in "m". , so the output of this simultaneous fitting, should be 3 parameters: m1,m2, b. I need to do a simultaneous for because I know that these two sets of data are actually related via than common parameter b.
I did a lot of research in MATLAB files on the web, some people suggest for simultaneous fits to non-linear functions, lsqfitcurve should be used. but I do not know how to define the function for a simultaneous fit. Function F should be a vector?
Thanks in advance for any help Nick
댓글 수: 0
채택된 답변
Matt Tearle
2011년 10월 4일
I think a simple fminsearch might be easier in this case. You'll have to figure out how you want to define your total error to minimize, but something like this seems to work:
x1 = linspace(0,pi);
x2 = linspace(-pi/2,pi/2);
y1 = 2*x1 + 7 + 0.1*randn(size(x1));
y2 = -3*x2 + 7 + 0.1*randn(size(x2));
f = @(c) twofunctions(x1,y1,x2,y2,c);
cfit = fminsearch(f,rand(3,1))
with
function err = twofunctions(x1,y1,x2,y2,c)
m1 = c(1);
m2 = c(2);
b = c(3);
err = norm(y1-m1*x1-b) + norm(y2-m2*x2-b);
추가 답변 (1개)
참고 항목
카테고리
Help Center 및 File 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!