# Setting bounds for constants in a fit() object in terms of other constants in the fit object

조회 수: 5 (최근 30일)
Seo Kyung Ahn 2024년 7월 19일
편집: Matt J 2024년 7월 20일
Hello, I am currently trying to fit experimental data that I theorize fits a logarithmic function.
To this end, I am using the fit() and fitoptions() function in order to create a logarithmic equation.
Since the default MATLAB logarithmic fit model does not seem to account for horizontal translation and scaling, I have created a custom fit type as seen below:
shiftedLog = fittype('A*log((B*x)-C) + D', 'independent', 'x', 'coefficients', {'A','B','C','D'});
% set the bounds
opts.StartPoint = [...]; % Initial guesses for [A, B, C, D]
opts.Lower = [...]; % Lower bounds for [A, B, C, D]
opts.Upper = [...]; % Upper bounds for [A, B, C, D]
I have noticed that when I try to run this program, I have ran into errors where the fit object returns either infinity or a complex value.
Since the log() function is defined only when its inner argument > 0, I need to specify the bounds for variables B and C to keep the entire argument positive.
However, When I try to specify the bounds in terms of the constants of the fit object, for example:
opts.Lower = [-Inf, C/min(x),...]
MATLAB gives me an error.
I know that in the main script code, the constants A, B, C, and D are not standalone variables so the main script has this issue, but as (to my knowledge) fit() finds the best values for the constants through an iterative process. Given this, It is impractical to specify a hard-coded value for the limits.
How should I approach this problem? Is there another way to find fit of a logarithmic function that incorporates 'shifting' and 'stretching' in both horizontal and vertical directions? Or is there a workaround? Thank you in advance.
##### 댓글 수: 2없음 표시없음 숨기기
Mathieu NOE 2024년 7월 19일
can you share some data ? and IC for the fit
Matt J 2024년 7월 19일
편집: Matt J 2024년 7월 19일
The model is over-parametrized. The D parameter can be removed.
To see this, make the change of variables D=A*log(E). Then the model becomes,
y = A*log(B*x-C)+A*log(E)
= A*log(B*E*x-C*E)
which is equivalent to the 3-parameter model,
y = A*log(b*x-c)

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

### 채택된 답변

Matt J 2024년 7월 19일
편집: Matt J 2024년 7월 20일
As a more indirect approach, instead of fitting,
y=A*log(B*x-C) %D has been deliberately removed - it is unnecessary
you could invert the model equation and fit,
x=(1/B)*exp(y/A) + C/B
which is the same as,
x=a*exp(b*y)+c
which is the same as the built-in 'exp2' fittype with d=0.
If you like, you can then go back to your original formulation, using the above fit to derive the StartPoint. Hopefully, this will put you close enough to the solution that B*x-C>0 will be innately satisfied.

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

### 추가 답변 (1개)

Torsten 2024년 7월 19일
편집: Torsten 2024년 7월 19일
Use "lsqcurvefit" and set A(1,2) = -1, A(1,3) = 1/min(x) and b(1) = 0 for the linear constraint condition A*x<=b.

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

### 카테고리

Help CenterFile Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기

R2023b

### Community Treasure Hunt

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

Start Hunting!

Translated by