MATLAB: How to fit a general curve on different datasets

fitted curvesnon-linear least squares

Hi,
I have different datasets that I would like to fit together in order to create a general equation.
Yet I have fitted each dataset independently as : y_i=a_i + b_i * log10(x)
%% Example of one dataset
x= [1 10 100]';
y= [165 145 124]';
fo = fitoptions('Method','NonlinearLeastSquares',...
'Lower',[1],...
'Upper',[200],...
'StartPoint',[180 180]);
ft = fittype('a + b*log10(x)','dependent',{'y'},'independent',{'x'},'coefficients',{'a','b'},'options',fo);
myfit=fit(x(:),y(:),ft);
plot(x,y, 'ko', 'LineWidth',1.5, 'MarkerSize',7)
hold on
plot(myfit, 'k--')
But now I would like to test this thing : y_i = a_i + b * log10(x) with a general b for all the dataset. So I need to fit this new model into my whole dataset to get an estimation of b that match with every dataset.
Does anyone as an idea on how to do this in matlab ?
Thank you very much for your help.

Best Answer

You need to set up separate independent variables for each dataset, something like this:
%% Example of two datasets (3 scores each)
i1= [1 1 1 0 0 0]; % 1 indicates member of 1st data set
i2= [0 0 0 1 1 1]; % 1 indicates member of 2nd data set
x= [1 10 100 1 10 100]'; % concatenated x's for both data sets
y= [165 145 124 155 135 114]'; % concatenated y's for both data sets
% Then fit a model like this
'a1*i1 + a2*i2 + b*log10(x)'
But I don't think the 'fit' function will do that--at least not for more than 2 datasets. You could do it with regress, at least if you make a new independent variable:
log10x = log10(x);
% Then fit
'a1*i1 + a2*i2 + b*log10x'
MATLAB probably has some other nonlinear fitting routines that would also work, using the same basic indicator variable trick.