Thanks for reading my question. I try to fit a set of data in matlab by a custum function, which is no more than a Gaussian distribution with an additional $x^d$ term. However, an error is encountered. Despite many efforts, I can’t solve this problem.
- At the very beginning, I fit the following data, “data_x”:
0 0.0500000000000000 0.100000000000000 0.150000000000000 0.200000000000000 0.250000000000000 0.300000000000000 0.350000000000000 0.400000000000000 0.450000000000000 0.500000000000000 0.550000000000000 0.600000000000000 0.650000000000000 0.700000000000000 0.750000000000000 0.800000000000000 0.850000000000000 0.900000000000000 0.950000000000000 1 1.05000000000000 1.10000000000000 1.15000000000000 1.20000000000000 1.25000000000000 1.30000000000000 1.35000000000000 1.40000000000000 1.45000000000000
and “data_y”:
4.82569292042591 6.15748191498009 4.31807282776559 2.46795497033244 1.17871657319353 0.518857189303422 0.242237665610014 0.121149313175648 0.0619970738844184 0.0346663415427132 0.0237950093473137 0.0164594001463058 0.00944891489880517 0.00268227261643502 0.00288547508737706 0.00247907014549297 0.00195074372104365 0.00213362594489149 0.00174754125010160 0.00168658050881899 0.00140209704950012 0.00138177680240592 0.00146305779078274 0.00125985531984069 0.00111761359018126 0.00123953507274648 4.06404941884093e-05 0 0 0
via a Gaussian distribution. My code is as follows:
fit_result = fit(data_x, data_y, 'gauss1');
There is no problem, and the fitting coefficients are a1=5.79, b1=0.04453, c1=0.1165
.
- Then, I tried to fit the data using a custom function, which is a generalized gamma distribution that includes an $x^d$ term. I used the following code:
gamma_distribution = @(a, d, b, c, x) a * x.^d .* exp(-((x-b)/c).^2);
fit_result = fit(data_x, data_y, gamma_distribution);
However, I encountered an error:
Error using fit>iFit (line 340)
Inf computed by model function, fitting cannot continue.
Try using or tightening upper and lower bounds on coefficients.
- After searching on Google, I found some suggestions. Adding constraints on the lower and upper bounds seems reasonable. The updated code is as follows:
gamma_distribution = @(a, d, b, c, x) a * x.^d .* exp(-((x-b)/c).^2);
fit_result = fit(data_x, data_y, gamma_distribution, 'Lower', [-10, -10, -10, -10], 'Upper', [10, 10, 10, 10]);
However, it did not solve the error.
- Then, I was suggested to add initial conditions. So I improved my code as follows:
gamma_distribution = @(a, d, b, c, x) a * x.^d .* exp(-((x-b)/c).^2);
fit_result = fit(data_x, data_y, gamma_distribution, 'Start', [5.79, 0, 0.04453, 0.1165], 'Lower', [-10, -10, -10, -10], 'Upper', [10, 10, 10, 10]);
Please note that all the initial values are chosen to be the ones from the Gaussian fitting results, and the external parameter d
is set to 0
. This approach worked, but the results were not satisfactory. It only gave all the parameters the same value, especially d=0
. Are there any further suggestions or standard answers to this question?
yang lee is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.