fitting data to a decaying exponential distribution
조회 수: 11 (최근 30일)
이전 댓글 표시
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
Star Strider
2023년 2월 24일
What are we suppolsed to do with these?
Data = readmatrix('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1306265/data.txt')
% 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.
.
채택된 답변
Image Analyst
2023년 2월 25일
See attached demo of fitnlm
댓글 수: 4
Image Analyst
2023년 2월 28일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Nonlinear Regression에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!