Effacer les filtres
Effacer les filtres

Fitting two sets of data to one function simultaneously

1 vue (au cours des 30 derniers jours)
Nick M.
Nick M. le 4 Oct 2011
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

Réponse acceptée

Matt Tearle
Matt Tearle le 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);
  1 commentaire
Nick M.
Nick M. le 5 Oct 2011
Thank you so much!
Awesome help!
Thanks again! :)

Connectez-vous pour commenter.

Plus de réponses (1)

Nick M.
Nick M. le 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.

Catégories

En savoir plus sur Get Started with Curve Fitting Toolbox dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by