Effacer les filtres
Effacer les filtres

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

53 vues (au cours des 30 derniers jours)
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 commentaires
Mathieu NOE
Mathieu NOE le 19 Juil 2024 à 9:09
can you share some data ? and IC for the fit
Matt J
Matt J le 19 Juil 2024 à 11:20
Modifié(e) : Matt J le 19 Juil 2024 à 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)

Connectez-vous pour commenter.

Réponse acceptée

Matt J
Matt J le 19 Juil 2024 à 11:02
Modifié(e) : Matt J le 20 Juil 2024 à 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.

Plus de réponses (1)

Torsten
Torsten le 19 Juil 2024 à 9:47
Modifié(e) : Torsten le 19 Juil 2024 à 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.

Catégories

En savoir plus sur Linear and Nonlinear Regression dans Help Center et File Exchange

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by