How to solve exponential variables

4 Ansichten (letzte 30 Tage)
Thomas Bulenga
Thomas Bulenga am 15 Mai 2023
Bearbeitet: John D'Errico am 15 Mai 2023
I have this exponential equation.
Y = A + Bexp(−CX)
For various values of Y, A and X I need to determine values for B and C. Solve function doen't seem to work.
Any idea will be appreaciated.
  6 Kommentare
Image Analyst
Image Analyst am 15 Mai 2023
Verschoben: John D'Errico am 15 Mai 2023
This is also not an exponential growth:
John D'Errico
John D'Errico am 15 Mai 2023
Bearbeitet: John D'Errico am 15 Mai 2023
Please don't post an answer to your question, when it is merely a comment. Learn how to make a comment.
Regardless, there is no sign of exponential growth OR decay. You data does not fit the model that you want to pose. Now, I suppose, IF you discard the last data point, it MIGHT look like exponential growth, as there can be some curvature seen.
X = [2 4 6 8 10 12 16 20];
Y = [0.00357246 0.003976621 0.004660491 0.0055569 0.007139916 0.009158161 0.012885998 0.010789951];
plot(X(1:end-1),Y(1:end-1),'bo-',X(end),Y(end),'rs')
However that last data point will completely blow away any estimates of those coefficients. Even the next to last data point will be a significant problem.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Image Analyst
Image Analyst am 15 Mai 2023
OK, here it is adapted with your actual data. Note that it does not look like an exponential decay at all.
% 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;
% Create the X coordinates from 0 to 20 every 0.5 units.
X = [2 4 6 8 10 12 16 20];
Y = [0.035 0.04 0.045 0.048 0.053 0.059 0.063 0.07];
%--------------------------------------------------------------------------------------------------------------------------------------
% Now we have noisy training data that we can send to fitnlm().
% Plot the noisy initial data.
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 = 1 % Arbitrary sample values I picked.
bGuessed = -0.5
cGuessed = -1
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:
yFitted = coefficients(1) * exp(-coefficients(2)*X) + 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(X, yFitted, 'r-', 'LineWidth', 2);
grid on;
title('Exponential Regression with fitnlm()', 'FontSize', fontSize);
xlabel('X', 'FontSize', fontSize);
ylabel('Y', 'FontSize', fontSize);
legendHandle = legend('Original Training Y', 'Fitted Y', 'Location', 'north');
legendHandle.FontSize = 30;
% Place formula text roughly in the middle of the plot.
formulaString = sprintf('Y = %.3f * exp(-%.4f * X) + %.3f', coefficients(1), coefficients(2), coefficients(3))
xl = xlim;
yl = ylim;
xt = xl(1) + abs(xl(2)-xl(1)) * 0.025;
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')

Weitere Antworten (2)

Image Analyst
Image Analyst am 15 Mai 2023
See attached demo. Adapt it to use your data instead of the data created by the demo code.

John D'Errico
John D'Errico am 15 Mai 2023
Bearbeitet: John D'Errico am 15 Mai 2023
The problem is, you have been trying to SOLVE them.
More approproately, you want to use a tool that will FIT your model to the data. As such, the curve fitting toolbox would be a very good choice. You don't want to use the symbolic toolbox at all. No need to use syms or symvar.
I'm not at all sure why you want to define A as some fixed value though, instead of also estimating it. That seems utterly strange to me. But before we do ANYTHING, lets look at the data you are trying to fit.
% Define X and Y as simple vectors of data.
X = [2 4 6 8 10 12 16 20];
Y = [0.035 0.04 0.045 0.048 0.053 0.059 0.063 0.07];
plot(X,Y,'o')
LOOK AT THE PLOT.
An exponential curve does not look like that data. It will be curved, either going to infinity, or leveling out at some constant value. This data does neither of those thigns. So attempting to fit it with an exponential model will be a complete waste of your time. This is that you should expect to see in your data, IF an exponential model is appropriate:
fplot(@(t) exp(-t),[0,5])
fplot(@(t) exp(t),[0,5])
Your data is essentially a STRAIGHT LINE.
P1 = fit(X',Y','poly1')
P1 =
Linear model Poly1: P1(x) = p1*x + p2 Coefficients (with 95% confidence bounds): p1 = 0.001939 (0.001692, 0.002187) p2 = 0.03272 (0.02992, 0.03551)
plot(P1,X,Y,'bo')
Any attempt to fit an exponential model to that data is like trying to force a square peg into a round hole. The fit will be complete crapola.

Community Treasure Hunt

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

Start Hunting!

Translated by