MATLAB Matlab: Fitting to two functions simultaneously

AI Thread Summary
The discussion centers on a challenge with fitting two sets of data in MATLAB, where each xdata value corresponds to two ydata values that need to be fitted to different functions, F1 and F2, sharing the same coefficients. The user initially succeeded in a graphing program, Sigmaplot, by combining the datasets and using an if statement to direct the fitting process. However, they seek a more automated solution in MATLAB, struggling with the lsqcurvefit function and the creation of an if/else loop for the fitting functions. Suggestions include reformulating the problem as a matrix minimization, which may be more compatible with MATLAB's capabilities. There is also mention of using the surface fitting tool and a reference to Origin 8, which can handle complex fitting. The user expresses a willingness to share updates on their progress and seeks further assistance if needed.
Sonolum
Messages
39
Reaction score
0
Okay, here's a problem that I've been struggling with for a few weeks.

I'm conducting a series of measurements, where I get two sets of data, i.e. one xdata value gives me two ydata values. I want to curve fit the two data sets to two different functions, F1 and F2, that depend on the same coefficients. By fitting ydata1 to F1 and ydata2 to F2, I hope to extract the coefficients that best fits to both data sets.

While I can fit to F1 or F2 separately, I can't seem to fit to both at once. The curve-fitting tool doesn't seem capable of this, and I've been playing around with lsqcurvefit but with no success. I haven't found any other functions that seem able to do what I want.

The thing is, I was able to set this fit up in a graphing program called Sigmaplot. In that program, I combined my data sets into one by copying one after the other, and then created another variable d which I defined as 0 for all the ydata1 points and 1 for all ydata2 points--

i.e. xdata=[xdata , xdata]; ydata=[ydata1 , ydata2]; d=[0,...,0,1,...,1]

Then I added an if statement in my fitting model, saying "if d==0, fit to F1, else fit to F2".

It works, but it's a pain because I have to manually enter the data and set up the fits and initial guess values by hand. I was hoping to use Matlab to set everything up automatically, but my attempts to do something similar haven't worked. Any suggestions would be greatly appreciated.
 
Physics news on Phys.org
Seems a little odd that both functions would have the same parameters, but if that's the case, I think the least squares problem would be:

[ATA + BTB]z = ATy1 + BTy2

Can't you just assemble the matrices A & B and perform the calculations with Matlab to solve for z? Maybe I'm looking at this too simplistically.
 
I think you may be onto something here!

I have been trying to write an if/else loop to create the two functions to fit to, which MatLab seems to vehemently dislike. Phrasing the problem in the manner you've suggested makes it a minimization of a matrix problem, instead of an lsqcurvefit problem, which (hopefully) MatLab will like a bit better.

Thank you, I will give it a try and see if it'll work this way, I really appreciate it! However, if it's all fail, then I'll be back to prod the internet community's collective brain for additional assistance. In the meantime, thank you again!
 
I also have same problem for fitting my data, help me toooooooooo
 
Soooooo... I think we might have a solution, though I'm still working out all the details on Matlab...

We're trying to use the surface fitting tool to fit to both functions.

I will update this thread as soon as I get it working with better instructions!

Best wishes, and if you figure this out before I do, please let me know!
 
I found!

Origin 8 can do this. We can do complex fitting also.

try...
http://wiki.originlab.com/~originla/howto/index.php?title=Tutorial:Fitting_Complex_Function
 

Similar threads

Replies
1
Views
3K
Replies
2
Views
1K
Replies
12
Views
4K
Replies
1
Views
2K
Replies
9
Views
5K
Replies
6
Views
5K
Back
Top