Fitting two sets of data to one function simultaneously
2 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
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)
See Also
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!