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

57 views (last 30 days)
Seo Kyung Ahn on 19 Jul 2024 at 2:43
Edited: Matt J on 20 Jul 2024 at 1:46
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.
Mathieu NOE on 19 Jul 2024 at 9:09
can you share some data ? and IC for the fit
Matt J on 19 Jul 2024 at 11:20
Edited: Matt J on 19 Jul 2024 at 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)

Matt J on 19 Jul 2024 at 11:02
Edited: Matt J on 20 Jul 2024 at 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.

Torsten on 19 Jul 2024 at 9:47
Edited: Torsten on 19 Jul 2024 at 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.

### Categories

Find more on Linear and Nonlinear Regression in Help Center and File Exchange

R2023b

### Community Treasure Hunt

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

Start Hunting!