Problem in curve fitting using curvefitter app
Show older comments
I am trying to curvefit some data on a customised exponential equation: y=A*exp(-(x-x0)/t) using curvefitter application in MATLAB.
While doing so, for x ranging from 30 to 70, the curvefitter is not able to curve fit though I was able to do the same when I was using the equation y=A*(1-exp(-(x-x0)/t)) when x ranges from 70 to 80.
Also, the curvefitting was successful when I tried to curvefit for the equation y=A*(1-exp(-x/t)), x0 being omitted here because the starting point is of x is 0 and x ranges from 0 to 30.
For the curvefitting, A, t and x0 are the parameters to be found by curvefitting the data.
x0 is close to the starting point of x, it is being included to avoid very large exponent values.
The curvefitter is giving a horizontal line only.
I have attached images for both cases.
Case 1:

Case 2:

Case 3:

8 Comments
Shashi Kiran
on 20 Sep 2024
Edited: Shashi Kiran
on 20 Sep 2024
From my initial understanding, it seems the issue might be related to the initial conditions for A,x0 and t.
Could you please provide the data, if possible, so I can examine the issue more closely?
Farkhanda
on 20 Sep 2024
Shashi Kiran
on 20 Sep 2024

In case of custom equation, you can set the start points by going to the Advanced options as shown.
Sam Chak
on 20 Sep 2024
Could you provide or attach the data for analysis?
From the trend, I believe that data may be fitted using the piecewise function:
.
Shashi Kiran
on 20 Sep 2024
Try adding a constant term to the custom equation and set intial alue of x0 to 30(as x starts from 30) as shown.
This helps in corrrect fit.

Answers (1)
I fitted the data using a logarithm function:

%% Data
load('yvalues_curvefit_case.mat');
y = y3a_DOD_dot1;
x = linspace(30, 70, numel(y))';
%% Fitting model
fo = fitoptions('Method', 'NonlinearLeastSquares',...
'Lower', [0.01, 200, 28, 0.21, 85, 1.03],...
'Upper', [0.03, 220, 30, 0.23, 95, 1.05],...
'StartPoint', [0.02, 210, 29, 0.22, 90, 1.04]);
ft = fittype('a*log(b*(x - c)^d + e*(x - c)^f)', ...
'dependent', {'prob'}, 'independent', {'x'}, ...
'coefficients', {'a', 'b', 'c', 'd', 'e', 'f'}, ...
'options', fo);
%% Fit curve to data
[yfit, gof] = fit(x, y, ft)
%% Plot results
plot(yfit, x, y), grid on
xlabel('t'), ylabel('x(t)')
legend('Data', 'Fitted model')
5 Comments
Farkhanda
on 20 Sep 2024
You can fit the data using any desired model; however, the best fit always depends on the constraints of the parameters you supply to the curve-fitting algorithm.
Additionally, you should already know that your model is an exponentially decaying function for the amplitude
. Since your data shows a monotonically increasing trend, the exponentially decaying function must be reflected about the x-axis so that the parameter
.
The reason @Shashi Kiran added a constant k is to provide a bias to the function, allowing the fitted model to be adjusted up or down along the y-axis.
%% Data
load('yvalues_curvefit_case.mat');
y = y3a_DOD_dot1;
x = linspace(30, 70, numel(y))';
%% Fitting model
fo = fitoptions('Method', 'NonlinearLeastSquares',...
'Lower', [-0.07, 0.17, 11, 29],...
'Upper', [-0.05, 0.19, 13, 31]);
ft = fittype('A*exp(-(x-x0)/t) + d', ...
'dependent', {'prob'}, 'independent', {'x'}, ...
'coefficients', {'A', 'd', 't', 'x0'});
%% Fit curve to data
[yfit, gof] = fit(x, y, ft, 'StartPoint', [-0.06, 0.18, 12, 30])
%% Plot results
plot(yfit, x, y), grid on
xlabel('t'), ylabel('x(t)')
legend('Data', 'Fitted model')
Farkhanda
on 20 Sep 2024
Alex Sha
on 21 Sep 2024
@Sam Chak: the fitting function of "a*log(b*(x - c)^d + e*(x - c)^f)" gives wonderful result, however, the best solution will be as fellow, the objective function value of SSE is little better, but with much different parameters:
Sum Squared Error (SSE): 2.3362432323886E-6
Root of Mean Square Error (RMSE): 7.64238711462033E-5
Correlation Coef. (R): 0.999988328851169
R-Square: 0.999976657838553
Parameter Best Estimate
--------- -------------
a 0.00767031100705701
b 5971471.14414276
c 30.0935323605759
d 1.00821070650148
e 911138.477416586
f 2.54707754950121
Thank you for providing the values with a better sum of squared errors. I forgot to mention to the OP (@Farkhanda Azmi) that the constraint for parameter c should be less than 30 (
), rather than exactly 30, as displayed due to rounding. The reason for this is that the data begins at
, and the initial value of
should be finite. If
, then the fitting function will produce complex values.
Another candidate function is the surd function; however, I did not conduct a more thorough investigation.
format long g
%% Fit curve to data
[yfit, gof] = fit(x, y, ft)
c = yfit.c
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
