fit custom equation to dataset
5 ビュー (過去 30 日間)
Julie Christoffersen 2022 年 9 月 19 日
I have some different datasets, that I would like to fit the following equation to:
As an example I have the following dataset:
x = [0.1418 0.1655 0.1970 0.2309 0.2770 0.3194 0.3697 0.6509 0.7036 0.8242 1.5176 2.4467 3.7230 5.7430 9.5691 11.8164]'
y = [0.0478 0.0741 0.0916 0.1025 0.1064 0.1111 0.1107 0.0640 0.0680 0.0682 0.0401 0.0273 0.0197 0.0140 0.0091 0.0077]'
I have tried fitting it using the following approach. However, the initial guess seems to change the function a lot.
fitfun = fittype ( @(A,B,C,n,x) 1./((A.*((x).^(n)))+(B.*(x))+C)) ;
[fitted_curve] = fit(x,y,fitfun,'StartPoint',x0) ;
Is there a way to optimize the process, to provide a best fit estimate (for instance using least square method) such that the final function does not depend on the initial guess? I have many datasets so the equations should not be estimated by a subject opinion, but instead automatically based on a best fit.
William Rose 2022 年 9 月 19 日
Different methods for fitting a nonlinear function (such as your nonlinear function) are at risk of finding a local minimum that is not a global minimum. In other words, the initial guess will matter, and there is no simple way to fix that. The non-simple solution is to try a bunch of different initial guesses and then choose the best of the best fits. If you can determine bounds for the parameters to be fitted, then make initial gueeses that are near the bounds. In your case, you are fitting A, B, C, and n. Suppose you are confident that -1<=A<=+1, 0<=B<=10, 1<=C<=100, and 1<=n<=5. Fit with fmincon() (since the "con" in fmincon() indicates that it can accomodate constraints). Try starting at 3 different values of each parameter: 0.1, .5, and 0.9 of the way along the range of possible values for each. Since you are fitting 4 parameters, you would have 3^4=81 different starting points to try. You try all 81. Your parameter estimate is the parameters that give the lowest error among the 81 "best" fits.
I have used this approach for multiple models and published results.