Fitting a complicated PDF
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello All,
I have a data set that is a sum of a gaussian and exponential. I am attaching the x and y of it. I have the curve fitting toolbox but count not find a proper fit this this type of data. Could you please help?
I tried fitting this function:
a1*exp(-((x-b1)/c1)^2)+ a*exp(-b*x)
But it wont fit the tail. See picture.
Thanks!
load x.mat;
load y.mat;
stairs(y,x);
3 commentaires
Réponses (1)
Shivansh
le 28 Déc 2023
Hi Sanchit,
I understand that you are trying to fit a data set that is a sum of a gaussian and an exponential distribution using the curve fitting toolbox. You are facing issues in fitting the tail.
The below code can be used to fit the data:
% Load your data
load x.mat;
load y.mat;
% Ensure x and y are column vectors
x = x(:);
y = y(:);
% Define the custom model with a stretched exponential component
ft = fittype('a1*exp(-((x-b1)/c1)^2) + a2*exp(-(b2*x)^c2)', ...
'independent', 'x', ...
'dependent', 'y', ...
'coefficients', {'a1', 'b1', 'c1', 'a2', 'b2', 'c2'});
% Provide initial guesses for the parameters
% These should be based on your data and may require adjustment
initialGuesses = [max(y), mean(x), std(x), min(y), 1/mean(x), 0.5];
% Set lower and upper bounds for the coefficients
% Adjust these bounds based on the physical constraints of your problem
lb = [0, min(x), 0, 0, 0, 0]; % Lower bounds
ub = [Inf, max(x), Inf, Inf, Inf, 1]; % Upper bounds
% Set options for fitting, including bounds and initial guesses
options = fitoptions(ft);
options.StartPoint = initialGuesses;
options.Lower = lb;
options.Upper = ub;
% Perform the fitting with bound
[fitresult, gof] = fit(x, y, ft, options);
% Plot the original data and the fit
figure;
stairs(x, y, 'b'); hold on;
plot(x, fitresult(x), 'r', 'LineWidth', 2);
legend('Data', 'Fit');
xlabel('x');
ylabel('y');
title('Gaussian + Stretched Exponential Fit');
% Display fitting results
disp(fitresult);
disp(gof);
Output of the aboce code:
General model:
fitresult(x) = a1*exp(-((x-b1)/c1)^2) + a2*exp(-(b2*x)^c2)
Coefficients (with 95% confidence bounds):
a1 = 0.08091 (0.07971, 0.08211)
b1 = 28.39 (28.36, 28.43)
c1 = 3.151 (3.097, 3.205)
a2 = 0.001456 (-6.031, 6.034)
b2 = 2.345 (-6959, 6964)
c2 = 0.8275 (-838.4, 840.1)
sse: 3.8381e-04
rsquare: 0.9920
dfe: 194
adjrsquare: 0.9918
rmse: 0.0014
If the tail is not fitting well, you can try following workarounds to improve the fit:
- Removing the outliers from the data.
- If the exponential decay is not captured correctly, try using a different parameterization for the exponential, such as “a2*exp(-b2*(x-b3))”.
- If the tail is long, you can try fitting with a stretched exponential distribution instead of the exponential distribution.
You can also work with the initial guess for the parameters or the expected behaviour of tail if you have domain knowledge of the problem.
You can refer to the following link for more information on the "fit" function of Curve fitting toolbox: https://in.mathworks.com/help/curvefit/fit.html.
Hope it helps!
0 commentaires
Voir également
Catégories
En savoir plus sur Get Started with Curve Fitting Toolbox dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!