MATLAB: Global fitting with one shared paramenter

global fittingshared parameters

Hello all,
First I would apologize for the code. I am sure there are smarter ways to write it but I am pretty new in matlab and programming syntax.
I am struggling to write nonlinear model to fit several data sets simultaneously. In particular I need to global fit some experimental data and one of these (b(3) in the following code ) is shared among them. I went throw the previous dscussion on the topic but I could not correct my code. My problem is that I did not undertand how to specify that the parameters for the fitting [b(1) and b(2)] can be different among the 4 dataset and b(3) is the same in order to plot them finally.
The function I need to use for the globla fitting is the same as my model function
I upload here the excell data I was using to test the script
Thank you in advance, Fil.
%----- Model function for 1:1 binding -------
modelfun = @(b,x)b(1)*(b(2)+x+b(3)-sqrt((b(2)+x+b(3)).^2-4*b(2)*x))./(2*b(2));
modelfun = @(b,x) modelfun([b(1),30,b(3)],x) %fixing parameters
b0 = [1; 30; 20]; %initialize parameters
% b(1) = maximum asintot
% b(2) = P0: total protein concentration
% b(3) = kd value
input = 'Global_fitting'; %take excel file as input
sheet = 1;
xlrange= 'A2:H11'; %data range
data=xlsread(input,sheet,xlrange);
[nr,nc]=size(data); % size of the data: nc [number of columns], nr[number of rows]'
x= data(:,1); % data x
for i = 1:nc % prepare y data
if (rem(i,2)==0)
y(:,i) = data (:,i);
y( :, all(~y,1) ) = []; % delete zero values columns
end
end
[ry, cy] = size(y); % size of the data: cy [number of columns], ry[number of rows]'
output_params = zeros (3,4);
for n= 1:cy
output_params(:,n)=lsqcurvefit(modelfun, b0, x,y(:,n)); % first independent fit of the (x,y) dataset
end
output_param0 = output_params % output parameters of the fitting
%---- global fitting ------
Fitting = @(params,x) modelfun([params(1:4),params(3)], x)
params0 = output_param0
for n= 1:cy
m = lsqcurvefit(Fitting,params0,x,y(:,n))
end
plot (x,y, 'o')
hold on

Best Answer

> I did not undertand how to specify that the parameters for the fitting [b(1) and b(2)] can be different
> among the 4 dataset and b(3) is the same in order to plot them finally.
Using fminsearch, one way to approach this problem is to imagine that there is one combined dataset (instead of 4 separate ones) and that there are 9 parameters instead of 3: that is, 4 b1's (one for each dataset), 4 b2's, and one b3.
For any proposed set of 9 parameter values, you compute the predicted y's, and return the error score sum( (Y-Ypred).^2 ) to fminsearch. fminsearch will find the best parameter values for you, and the same b3 will be used for all datasets since there is only one of them.
Maybe you can do something similar with lsqcurvfit.