MATLAB: How to fix the coefficients values using the fitnlm() function

coefficientsfitnlm

Hi all,
I am trying to use non linear regression fit using the fitnlm function. Is there a way to fix a specific
coefficient to a known value? in this specific case I would like to fix b(2) to 50.
Thank you in advance for your help.
I report here my code
x = data (:,1);
y = data (:,2);
plot (x, y, 'o')
hold on
%----- Model function for 1:1 binding -------
b0 = [0.06; 50; 0.5];
modelfun = @(b,x)b(1)*(b(2)+x+b(3)-sqrt((b(2)+x+b(3)).^2-4*b(2)*x))./(2*b(2));
%options for fitnlm
options = statset('Display','iter','TolFun',1e-10)
modelFit=fitnlm(x,y,modelfun, b0,'Options',options)
results=table2array(modelFit.Coefficients)
plot(x, modelfun(results(:,1), x))
hold off

Best Answer

There are likely several approaches you could trake to this.
This one seems to work:
b0 = [0.06; 50; 0.5];
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),50,b(3)],x)
%options for fitnlm
options = statset('Display','iter','TolFun',1e-10)
modelFit=fitnlm(x,y,modelfun, b0,'Options',options)
results=table2array(modelFit.Coefficients)
plot(x, modelfun(results(:,1), x))
hold off
It avoids your having to re-write your ‘modelfun’ function to ‘freeze’ ‘b(2)’ at 50.