Filter löschen
Filter löschen

Fitting a complicated PDF

9 Ansichten (letzte 30 Tage)
Sanchit Sharma
Sanchit Sharma am 4 Mai 2022
Beantwortet: Shivansh am 28 Dez. 2023
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.
load x.mat;
load y.mat;
  3 Kommentare
Sanchit Sharma
Sanchit Sharma am 4 Mai 2022
I want to fit the tail of the plot as well.
Alex Sha
Alex Sha am 5 Mai 2022
and then you have to change the fitting model to meet your requirements

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Shivansh am 28 Dez. 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
stairs(x, y, 'b'); hold on;
plot(x, fitresult(x), 'r', 'LineWidth', 2);
legend('Data', 'Fit');
title('Gaussian + Stretched Exponential Fit');
% Display fitting results
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:
  1. Removing the outliers from the data.
  2. If the exponential decay is not captured correctly, try using a different parameterization for the exponential, such as “a2*exp(-b2*(x-b3))”.
  3. 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:
Hope it helps!




Community Treasure Hunt

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

Start Hunting!

Translated by