Matlab: Fitting to two functions simultaneously

In summary: Data_With_Origin_8In summary, Origin 8 can fit to both functions at the same time, but it's a pain to set it up.
  • #1
Sonolum
39
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
  • #2
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.
 
  • #3
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!
 
  • #4
I also have same problem for fitting my data, help me toooooooooo
 
  • #5
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!
 
  • #6
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
 

1. What is Matlab?

Matlab is a high-level programming language and interactive environment commonly used in scientific and engineering applications. It allows users to perform complex mathematical computations, create visualizations, and analyze data.

2. How do I fit two functions simultaneously in Matlab?

To fit two functions simultaneously in Matlab, you can use the "lsqcurvefit" function. This function allows you to specify multiple functions and their corresponding parameters, and then finds the best fit for those functions to your data.

3. What is the advantage of fitting two functions simultaneously?

Fitting two functions simultaneously allows you to model complex data that may not be accurately represented by a single function. By using multiple functions, you can capture different aspects of the data and get a more accurate fit.

4. Can I fit more than two functions simultaneously in Matlab?

Yes, you can fit as many functions as you need in Matlab using the "lsqcurvefit" function. However, keep in mind that fitting too many functions can lead to overfitting, so it is important to carefully consider the number of functions you use.

5. Are there any limitations to fitting two functions simultaneously in Matlab?

One limitation of fitting two functions simultaneously in Matlab is that the functions must be defined in the form of a function handle. This means that the functions must be written as separate .m files or as anonymous functions in the Matlab workspace. Additionally, the functions must be compatible with the data being fitted, as well as with each other.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
12
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top