Matlab: Fitting to two functions simultaneously

Click For Summary

Discussion Overview

The discussion revolves around the challenge of fitting two sets of data to two different functions simultaneously in Matlab, where both functions share the same coefficients. Participants explore various approaches to achieve this, including the use of matrix formulations and alternative software solutions.

Discussion Character

  • Technical explanation
  • Exploratory
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant describes their struggle with fitting two data sets to two functions in Matlab, noting that while they can fit each separately, simultaneous fitting has proven difficult.
  • Another participant suggests a matrix approach to solve the least squares problem, proposing the equation [ATA + BTB]z = ATy1 + BTy2 as a potential solution.
  • A different participant expresses optimism about the matrix method, indicating that it may simplify the fitting process compared to using an if/else loop in Matlab.
  • One participant shares their ongoing efforts to use the surface fitting tool in Matlab to fit both functions, promising to update the thread with further instructions.
  • Another participant mentions that Origin 8 software can handle complex fitting, suggesting it as an alternative solution.

Areas of Agreement / Disagreement

Participants do not reach a consensus on a single solution, as multiple approaches are proposed and explored, indicating that the problem remains unresolved.

Contextual Notes

Participants express uncertainty about the compatibility of their proposed methods with Matlab, and there are indications of varying levels of familiarity with the software's capabilities.

Who May Find This Useful

Individuals working with data fitting in Matlab, particularly those facing challenges with simultaneous fitting of multiple functions or seeking alternative software solutions.

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 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 12 ·
Replies
12
Views
4K
  • · Replies 6 ·
Replies
6
Views
1K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 6 ·
Replies
6
Views
6K