how to use fitnlm with constraints

조회 수: 24 (최근 30일)
Haneya Qureshi
Haneya Qureshi 2023년 3월 11일
댓글: Haneya Qureshi 2023년 3월 12일
I have a custom equation and want to fit its coeffients.
coefficients are k
x is data
Equation is as follows:
modelfun = @(k,x) k(1).*x(:,1)+k(2).*log10(x(:,2) +k(3).*x(:,3)^2)
Kinit is vector of initial variables that I give
tbl contains my data x
I fit like this:
mdl = fitnlm(tbl,modelfun,Kinit);
I want to impose certain contraints on coefficients,
i.e.,
k(1) should be between 20 and 40
k(2) should be positive
k(3) should be negative.
How can I do that?
Thanks a lot for helping me out.
  댓글 수: 2
Walter Roberson
Walter Roberson 2023년 3월 11일
log10*x(:,2)
could you confirm that you assigned a value to log10 such that you can multiply it by an input? Or did you miss some () ?
Haneya Qureshi
Haneya Qureshi 2023년 3월 11일
편집: Haneya Qureshi 2023년 3월 11일
@Walter Roberson sorry, i missed brackets..the function is definitely non-linear.
modelfun = @(k,x) k(1).*x(:,1)+k(2).*log10(x(:,2) +k(3).*x(:,3)^2)

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

채택된 답변

Walter Roberson
Walter Roberson 2023년 3월 11일
No, fitnlm does not provide any way to put in constraitns.
You might consider lsqcurvefit from the Optimization Toolbox. Or you could consider using the Curve Fitting Toolbox; https://www.mathworks.com/help/curvefit/linear-and-nonlinear-regression.html
  댓글 수: 9
Walter Roberson
Walter Roberson 2023년 3월 11일
Ah, yes. Curve Fitting Toolbox can support functions of two variables, creating a surface fit, but that is not enough for your purposes.
You could consider creating a residue function,
residue = @(k) sum((k(1) * x(:,1) + k(2) * log10(x(:,2)) + k(3)*x(:,3).^2) - Y).^2)
and minimizing that sum-of-squares using fmincon.
Haneya Qureshi
Haneya Qureshi 2023년 3월 12일
Got it, makes sense! Thanks so much!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Nonlinear Regression에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by