Fitting two sets of data to one function simultaneously

2 views (last 30 days)
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

Accepted Answer

Matt Tearle
Matt Tearle on 4 Oct 2011
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);

More Answers (1)

Nick M.
Nick M. on 10 Oct 2011
So, is there anyway we can get goodness of fit statistic after doing fminsearch or not? I minimized the function using fminsearch and I minimized chi-squared, Is there anyway I can take the uncertainty matrix after using fminsearch? apparently you can only have this by using fit toolbox.

Community Treasure Hunt

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

Start Hunting!