MATLAB: Nonlinear fit to multiple data sets with shared parameters

nonlinear curve fitting

Hello all,
I need to fit a nonlinear model to several data sets simultaneously. The model has the same functional form for all sets, and the values of some model parameters are the same for all sets, but the value of at least one parameter is different for each set. For example, three exponential decay curves might have the same decay constant but a different amplitude for each data set. The global fit to all three curves would produce one decay constant and three amplitudes.
What is the best way to do this in MatLab? (Apologizing in advance for a novice question!)
ELELAB

Best Answer

Take a look at this example of a function that partitions the parameters into a set sharing values across all datasets and a set taking different values for different datasets. You may be able to modify this for your purposes.
function sharedparams
t = (0:10)';
T = [t; t; t];
Y = 3 + [exp(-t/2); 2*exp(-t/2); 3*exp(-t/2)] + randn(33,1)/10;
dsid = [ones(11,1); 2*ones(11,1); 3*ones(11,1)];
gscatter(T,Y,dsid)
X = [T dsid];
b = nlinfit(X,Y,@subfun,ones(1,5))
line(t,b(1)+b(2)*exp(-t/b(5)),'color','r')
line(t,b(1)+b(3)*exp(-t/b(5)),'color','g')
line(t,b(1)+b(4)*exp(-t/b(5)),'color','b')
function yfit = subfun(params,X)
T = X(:,1); % time
dsid = X(:,2); % dataset id
A0 = params(1); % same A0 for all datasets
A1 = params(2:4)'; % different A1 for each dataset
tau = params(5); % same tau
yfit = A0 + A1(dsid).*exp(-T/tau);
This uses nlinfit from the Statistics Toolbox, but you could use other fitting routines instead.