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

52 次查看(过去 30 天)
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 个评论
Matt J
Matt J 2024-7-19,11:20
编辑:Matt J 2024-7-19,15:02
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)

请先登录,再进行评论。

回答(2 个)

Torsten
Torsten 2024-7-19,9:47
编辑:Torsten 2024-7-19,9:48
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.

Matt J
Matt J 2024-7-19,11:02
编辑:Matt J 2024-7-20,1:46
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.

类别

Help CenterFile Exchange 中查找有关 Linear and Nonlinear Regression 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by