fitting data to a decaying exponential distribution

8 vues (au cours des 30 derniers jours)
Hozifa
Hozifa le 24 Fév 2023
Commenté : Image Analyst le 28 Fév 2023
Hello there,
I have this data (attached) and i dont know how to fit it to a decaying exponential distribution, if it possible I want to have the mean and the decaying constant
  2 commentaires
Star Strider
Star Strider le 24 Fév 2023
What are we suppolsed to do with these?
Data = readmatrix('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1306265/data.txt')
Data = 1070×2
0 0.2635 2.3400 0.0471 0 0.0387 0 0.0379 0 0.0245 0 0.0355 0.4000 0.0178 0 0.0293 0.3000 0.0178 0.7000 0.0178
% Data = sortrows(Data,2)
figure
yyaxis left
plot(Data(:,1))
yyaxis right
plot(Data(:,2))
grid
figure
plot(Data(:,1), Data(:,2))
grid
Sorting by either column does not help.
.
Hozifa
Hozifa le 25 Fév 2023
Thank you,
I want something close to figure 2 you made, where a fitting decaying line is shown (the curve has mean and decay constant)

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 25 Fév 2023
  4 commentaires
Hozifa
Hozifa le 27 Fév 2023
Thank you @Image Analyst,
May I know something, in the figure uploaded, what are the mean of the distribution and the time decay constant?
Image Analyst
Image Analyst le 28 Fév 2023
If you want to use fitnlm to fit the histogram of that data to an exponential decay, see the code below:
% Uses fitnlm() to fit a non-linear model (an exponential decay curve, Y = a * exp(-b*x) + c) through noisy data.
% Requires the Statistics and Machine Learning Toolbox, which is where fitnlm() is contained.
% Initialization steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
%------------------------------------------------------------------------------------------------------
% Read in the data samples drawn from an exponential decay distribution.
data = importdata('data.txt')
[rows, columns] = size(data)
%------------------------------------------------------------------------------------------------------
% Plot the histograms of each column.
% I don't know what each column represents though!!!
subplot(1, 2, 1);
histObject1 = histogram(data(:, 1))
grid on;
title('Histogram of Column 1', 'FontSize',fontSize);
subplot(1, 2, 2);
histObject2 = histogram(data(:, 2))
title('Histogram of Column 2', 'FontSize',fontSize);
grid on
%------------------------------------------------------------------------------------------------------
% Create the Y coordinates from the first histogram object,
% which is for data column 1.
% (You can repeat later to fit column #2).
X = histObject1.BinEdges(1:end-1);
Y = histObject1.Values;
%--------------------------------------------------------------------------------------------------------------------------------------
% Now we have noisy training data that we can send to fitnlm().
% Plot the noisy initial data.
figure;
plot(X, Y, 'b*', 'LineWidth', 2, 'MarkerSize', 15);
grid on;
% Convert X and Y into a table, which is the form fitnlm() likes the input data to be in.
% Note: it doesn't matter if X and Y are row vectors or column vectors since we use (:) to get them into column vectors for the table.
tbl = table(X(:), Y(:));
% Define the model as Y = a * exp(-b*x) + c
% Note how this "x" of modelfun is related to big X and big Y.
% x((:, 1) is actually X and x(:, 2) is actually Y - the first and second columns of the table.
modelfun = @(b,x) b(1) * exp(-b(2)*x(:, 1)) + b(3);
%------------------------------------------------------------------------------------------------------
% Guess values to start with. Just make your best guess.
aGuessed = 500 % Arbitrary sample values I picked.
bGuessed = 1
cGuessed = 10
beta0 = [aGuessed, bGuessed, cGuessed]; % Guess values to start with. Just make your best guess.
%------------------------------------------------------------------------------------------------------
% Now the next line is where the actual model computation is done.
mdl = fitnlm(tbl, modelfun, beta0);
% Now the model creation is done and the coefficients have been determined.
% YAY!!!!
%------------------------------------------------------------------------------------------------------
% Extract the coefficient values from the the model object.
% The actual coefficients are in the "Estimate" column of the "Coefficients" table that's part of the mode.
coefficients = mdl.Coefficients{:, 'Estimate'}
% Create smoothed/regressed data using the model:
% First get a lot more X values so the curve will look smooth and
% not be evaluated only at the training points.
xFitted = linspace(min(X), max(X), 1000);
yFitted = coefficients(1) * exp(-coefficients(2)*xFitted) + coefficients(3);
%------------------------------------------------------------------------------------------------------
% Now we're done and we can plot the smooth model as a red line going through the noisy blue markers.
hold on;
plot(xFitted, yFitted, 'r-', 'LineWidth', 2);
grid on;
title('Exponential Regression with fitnlm()', 'FontSize', fontSize);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
legendHandle = legend('Noisy Y', 'Fitted Y', 'Location', 'north');
legendHandle.FontSize = 30;
%------------------------------------------------------------------------------------------------------
% Place formula text roughly in the middle of the plot.
formulaString = sprintf('Y = %.3f * exp(-%.3f * X) + %.3f', coefficients(1), coefficients(2), coefficients(3))
xl = xlim;
yl = ylim;
xt = xl(1) + abs(xl(2)-xl(1)) * 0.325;
yt = yl(1) + abs(yl(2)-yl(1)) * 0.59;
text(xt, yt, formulaString, 'FontSize', 25, 'FontWeight', 'bold');
%------------------------------------------------------------------------------------------------------
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
Note: the fit above is only for the first column of your data (the left histogram). You'd need to repeat it on the second histogram to get the other equation.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by