How can I add the correct coefficients from exponential curve fitting to the code

5 visualizaciones (últimos 30 días)
I have this exponential curve fit. The coeffiencints obtained from this fit are for example a = -1951 , b = 0.6876 , c = 1949 , d = 0.6879. But when I generate the code and load the values values in my matlab I obtain something like this a=-5754.70201, b= 0.68769, c= 5752.208101, d= 0.68777. The b and d value remain the same but there is a huge difference in the a and d value. I have tried choosing the option center fit and scale, with and without LAR and bisquare, but I'm always getting a different set of coeficients. What can be the reason for this?
  1 comentario
John D'Errico
John D'Errico el 28 de Ag. de 2023
Editada: John D'Errico el 28 de Ag. de 2023
What exponential fit? You don't even show the model! Should we know what a,b,c,d all mean? Should we know how you fit the data? We cannot read your mind, any more than we can see inside your computer. If you want help, then make it possible for someone to help you. If you really want better help, then you could also attach the data itself, so someone can show how to best perform the fit.

Iniciar sesión para comentar.

Respuestas (4)

Image Analyst
Image Analyst el 29 de Ag. de 2023
I don't get anything like either of your two sets of coefficients. This is what I get:
% Uses fitnlm() to fit a non-linear model (an exponential decay curve) 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 = 10;
% Create the X and Y coordinates.
X = [3.5, 3.7, 4.45, 4.55, 4.75, 4.8, 4.95, 5.3];
Y = [-8, -10.2, -6.8, -6.75, -5.6, -4.2, -2.7, 2.5];
% 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.
tbl = table(X', Y');
% Define the model as Y = a + exp(-b*x)
% 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);
beta0 = [0.001, 1.5, -10]; % 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'}
coefficients = 3×1
0.000956737627347137 1.78524541391142 -9.67636027306516
% Create smoothed/regressed data using the model:
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);
xticks(3.6 : 0.2 : 5.2)
xlim([3.4, 5.4]);
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 = 10;
formulaString = sprintf('Y = %.6f * exp(%.6f * X) + %.6f', coefficients(1), coefficients(2), coefficients(3))
formulaString = 'Y = 0.000957 * exp(1.785245 * X) + -9.676360'
text(3.6, -1, formulaString, 'FontSize', 10, 'FontWeight', 'bold');
  3 comentarios
William Rose
William Rose el 29 de Ag. de 2023
Editada: William Rose el 29 de Ag. de 2023
That is a great insight. I thought of that and then I thought "but there's no vertical offset adjustment in that equation, and I woudl think that'd be useful".
But now that I look at the coefficients originally proposed by @Sidesh Ganesh Kumar, the equation you suggest makes perfect sense. With Sidesh's coeeficients, it is
In other words, there are two exponentials, both large but with oppostie sign. Their exponential growth factors are almost identical.
@Sidesh Ganesh Kumar, if @David Goodmanson is right, that is a poor choice of equation for fitting this data. The instability of the fitted coefficients shows that it is a bad choice of equation. It has large, opposite sign terms that are exquisitely balanced, which is unstable. Use the three-term equation of @Image Analyst for a much more robust fit. If you feel a fourth parameter is justified (and you can test this - see, for example, this paper of mine), try an equation such as which will be more stable than

Iniciar sesión para comentar.


William Rose
William Rose el 29 de Ag. de 2023
@John D'Errico is exactly right.
It seems that you have obtained four coefficients by one method. Then you fit the data in Matlab, and you got different coefficients, and you are wondering why they are different.
Please provide a file with the data points (x,y pairs). Supply the equation involving a,b,c,d. Explain how you obtained the first set of coefficients. Show the Matlab commands you used to obtain the second set of coefficients.
If the fitting equation has redundant coefficients, then the best-fit coefficients will not be uniquely determined. For example, a and c are redundant in the equation
But you probably know that, so I expect that is not the problem.
For nonlinear equations like yours, different initial guesses can lead to different "best" fits.
Good luck with your work.

Sidesh Ganesh Kumar
Sidesh Ganesh Kumar el 29 de Ag. de 2023
Answering to @John D'Errico @Image Analyst and @William Rose. I hope I've been bit more clear this time around
This is the full image of the curve fit along with the equations. I save this fit and try to genereate the code and insert this in thesw codes below:
[fitresult, gof] = createFitLfasTor2(LfasPassive, passive_torque_mean1)
coef_posLfasPasTor2=coeffvalues(fitresult);
xLfasPas=[3:0.1:9];
y4=zeros(1, length(xLfasPas));
for i =1:length (xLfasPas)
y4(i)= coef_posLfasPasTor(1)*exp(coef_posLfasPasTor(2)*xLfasPas(i)) + coef_posLfasPasTor(3)*exp(coef_posLfasPasTor(4)*xLfasPas(i));
end
When I try to load the above coefiecients value in the first line of the equation here I get different values. I already previous had a another exponential fit previoulsy which I would attach below:
For this fit when I try to load the data and insert it into the below codes I'm able to obtain the same set of coefficients. It is only for the first image attached above I'm getting varied results.
[fitresult, gof] = createFitFoot(ankle_angle, passive_torque_mean1)
coef_posFoot=coeffvalues(fitresult);
x_pos=(40:2:130);
y1=zeros(1, length(x_pos));
for i =1:length (x_pos)
y1(i) = coef_posFoot(1)*exp(coef_posFoot(2)*x_pos(i)) + coef_posFoot(3)*exp(coef_posFoot(4)*x_pos(i));
end
The above obtained coef_posFoot values are [0.000300129768339381 0.0925431992768093 -17.8828056032200 -0.0108767882795628] which coincides with the coefficient values.

William Rose
William Rose el 29 de Ag. de 2023
As I mentioned in a comment, this model leads to a fit with delicately balanced positive and negative terms. Therefore it is rather unstable. I would not use this equation unless there are strong physical reasons for doing so. If there are strong physical reasons, then the range of x-values acquired is not sufficient to identify the coefficients with high confidence, and I would try to collect data with more extreme x/values to determines the coefficients with greater confidence. Good luck!
  2 comentarios
Sidesh Ganesh Kumar
Sidesh Ganesh Kumar el 29 de Ag. de 2023
Even if I change the equation I'm still getting huge values in the y4(i) line, which doesn't suit my fit for later. Is there any other way how I can make use of the correct coef values?
William Rose
William Rose el 30 de Ag. de 2023
I have not used Mztlab's curvefitter tool. I would rather use the Marlab functions in a script that I write because I understand and control the options better, or at least I think I do. I cannot tell from the code and screenshots which you posted above if your script generates the same coefficients as the Curve FItter app. If the coefficients are not the same, that explains why you do not get te same predictions.
More importantly, I strongly urge you not the use the mathematical model that you are using. I am unaware of a biomechanical reason to use two exponentials for this data. Look at the 95% confidence intervals (CIs) for your fitted parameters. The CIs are enormous compared to the best fit values, and the CIs include both positive and negative values, which means that 0 is included in the intervals. That is not good. It means the data are not sufficient to constrain the coefficients well.
I have some famliarity with measuring passive torque and angle in ankles. I have assisted grad students in our department with analysis of such data. We used an ankle arthrometer that has been modified to record passive torque and angle data. See https://pubmed.ncbi.nlm.nih.gov/23672383/.

Iniciar sesión para comentar.

Categorías

Más información sobre Linear and Nonlinear Regression en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by