Inconsistencies between using cftool and plot
1 view (last 30 days)
Show older comments
Hi,
I am using the GUI of cftool, and get this
```
Fit Name: untitled fit 1
Exponential Curve Fit (exp2)
f(x) = a*exp(b*x) + c*exp(d*x)
Coefficients and 95% Confidence Bounds
Value Lower Upper
a 6.7397e+11 -4.3032e+21 4.3032e+21
b -0.1407 -2.8050e+04 2.8049e+04
c -6.7395e+11 -4.3032e+21 4.3032e+21
d -0.1407 -2.8052e+04 2.8052e+04
Goodness of Fit
Value
SSE 1.0357e+11
R-square 0.9960
DFE 1
Adj R-sq 0.9839
RMSE 3.2182e+05
```
and the plot is
then I plot it in a script with same x
x = linspace(1,10,100)
a1 = 6.7397e+11;
b1 = -0.1407;
c1 = -6.7395e+11;
d1 = -0.1407;
fit_y1 = a1 * exp(b1 * x) + c1 * exp(d1 * x);
figure;
plot(x, fit_y1, 'r-', 'LineWidth', 1.5);
Thanks
1 Comment
Accepted Answer
Sam Chak
on 4 Jan 2024
Oh, I see. The reason for the inconsistency in the plot is due to the fact that the cftool app only displays 5 digits of precision. If you increase the precision, both plots should be the same.
0 Comments
More Answers (1)
Drew
on 4 Jan 2024
Edited: Drew
on 4 Jan 2024
The ordinary way to reproduce the plot is to use the cfit "plot" method. If the curve fit was done in the Curve Fitter app, export the fitted model from the app using the "Export"->"Export to Workspace" option, then use plot(fittedmodel) at the command line. To re-create the fit at the commandline, choose the "Export" "Generate Code" option in the app. The generated code will re-create the fit and create a plot. For both of these options, see the doc page https://www.mathworks.com/help/curvefit/generating-code-and-exporting-fits-to-the-workspace.html
If you want to access the full-precision coefficients, use the cfit method "coeffvalues", that is:
>> coeffvalues(fittedmodel)
You can see all methods for the cfit object using
>>methods(fittedmodel)
Below is an example using the command line interface. As mentioned above, the fitting could alternately be done in the Curve Fitter app, followed by exporting the fittedmodel from the app, and/or generating code from the app to reproduce the fit and plot.
% These points were estimated from the graph provided in the question
x=[1 2 4 8 10];
y=[2.43 2.57 2.6 2.28 1.98]*10^7;
% The fit function expects a single column for each of x and y, so use
% simple prepareCurveData helper function to achieve that
[xData,yData]=prepareCurveData(x,y);
% Create the fit. "fittedmodel" is a cfit object.
fittedmodel = fit(xData,yData,'exp2');
% Use the cfit "plot" method to plot the curve fit along with the data points
plot(fittedmodel,xData,yData)
% If you want to plot the curve "manually" without using the cfit plot method,
% use the cfit "coeffvalues" method to retrieve the full-precision
% coefficients.
coefficients = coeffvalues(fittedmodel);
a1 = coefficients(1);
b1 = coefficients(2);
c1 = coefficients(3);
d1 = coefficients(4);
% Choose x values
x1 = linspace(1,10,100);
% Calculate values on the fitted curve
fit_y1 = a1 * exp(b1 * x1) + c1 * exp(d1 * x1);
% plot the resulting curve
plot(x1, fit_y1, 'r-', 'LineWidth', 1.5);
% To make the plot look more like the one in the curve fitter app, plot the
% curve with the default first color, then add data points in black.
plot(x1,fit_y1,'LineWidth',1.5);
grid on; hold on;
% Add the data points in black
plot(xData, yData,'.k')
% See all methods (the output may have a horizontal scroll bar)
methods(fittedmodel)
1 Comment
See Also
Categories
Find more on Linear and Nonlinear Regression 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!