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

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

 채택된 답변

Star Strider
Star Strider 2019년 1월 8일
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.

댓글 수: 5

thank you very !much it was really what I was looking for!
As always, my pleasure!
Is it possible to give an upper and lower limit of parameters in fitnml?
Thanks
This is quite old, and I am only just seeing it, so sorry if this information is not useful anymore. But the answer is yes, you can get confidence intervals for the fitted parameters. Take a look at this documentation section, regarding the coefficients in the model object.
If I understand Ankit's question correctly, they are asking about whether you can constrain parameters to only be within a certain range before performing the fit. I think doing so is possible in other toolboxes like the curve fitting toolbox, but I haven't seen a clear way of doing so with fitnlm.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

질문:

2019년 1월 8일

댓글:

2020년 7월 21일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by