What is the proposed approach if one wants to simultaneously fit multiple functions to multiple datasets with shared parameters?

As an example consider the following case: We have to measurements of Gaussian line profiles and we would like to fit a Gaussian to each of them but we expect them to be at the same line center, i.e. the fitting should use the same line center for both Gaussians.

The solution I came up with looks a little clumsy. Any ideas on how to do this better, especially in cases where we have more than 2 datasets and more than one shared parameter?

As stated above the magnitude of the variance won't affect our point estimates in the model, so we can use the same code above, and just inject the newly estimated variance into the log-likelihood function. This seems to be equivalent to the default behaviour of NonlinearModelFit.

As you seem to indicate that you are fitting spectra from a counting experiment, you might have better performance if you assume Poisson counting noise instead, then the variance for each channel is estimated as the number of counts in that channel:
$$
\sigma^2_k \approx data_k
$$
You might also want to consider adding a background model (a constant background is a simple extension of the above), depending on the noise level.

Welcome to our site. This is a well presented answer, and I would like to see more of your expertise on this site. So, consider registering your account, as it will allow you to participate more fully.
–
rcollyerMay 8 '12 at 0:48

Thank you for the answer. I tried something similar, but was not satisfied with the fit. Yours looks much better. The advantage of the NonlinearModelFit is that it provides error estimates of the fit parameters. Do you know how to estimate the error using NMinimize?
–
Markus RoelligJan 28 '12 at 17:29

(Note: you could compare the a1 and a2 parameters with the maximum of each dataset to automate associating the fit parameters with their datasets. You would do this by inspection in the way presented here.)

Thanks for the idea. Shifting and joining works for many of my applications but not for all of them. Consider two data sets containing not one but multiple peaks. If they, e.g. represent fine-structure emission their individual components have a fixed distance. Joining the data sets might lead to confusion problems. Still a good idea.
–
Markus RoelligJan 29 '12 at 10:30

@Markus Can you add to the question a more realistic example where my approach would fail?
–
JxBJan 29 '12 at 13:36

actually I can't come up with a good counter example. All answers were really helpful and I wish I could accept all of them.
–
Markus RoelligFeb 8 '12 at 12:20

Mathematica is a registered trademark of Wolfram Research, Inc. While the mark is used herein with the limited permission of Wolfram Research, Stack Exchange and this site disclaim all affiliation therewith.