cftool with no log scale issue

4 次查看(过去 30 天)
I am trying to fit my data set using cftool. For example I did Power fitting and the R-square=0.997 (also see the attachment). I save as "create code. cftool does not have the option to plot in log scale. Later I use this function in my MATLAB code with set(gca, 'YScale', 'log').
It is a tottaly different plot, has noting to do with that perfect fit. See the other plot in the attachment.
I don't know maybe there is a way to use the log scale in the cftool but I could not see. I just wanted to bring this issue to the developers attention.

回答(1 个)

Saarthak Gupta
Saarthak Gupta 2023-12-22
Hi Birsen,
I see from the figures you have attached that the data does not fit well when the Y axis is converted to a logarithmic scale.
The apparent ill fit arises from the presence of noise in the data.
A power series model can be described as: . The parameters a and b are estimated such that the residual is minimized.
Although this method guarantees that the estimated parameters provide the optimal fit, it does not ensure that the curve will perfectly interpolate the data. Some deviation is expected, which is why the R-squared value you achieved was 0.997 instead of a perfect 1. Transforming to a logarithmic scale simply makes this deviation more apparent when graphed.
Mathematically
Refer to the following code:
a=9.45e-08;
b=-3.28;
x = 2:2:100;
y = a*x.^b;
% Gaussian noise with spread of 1e-10, to add to data
errs = normrnd(y,1e-10);
tiledlayout(1,2);
nexttile
createFit(x,y+errs); % fit noisy data
set(gca, "YScale", "Log");
title("Noisy data");
nexttile
createFit(x,y); % fit noise free data
Warning: Negative data ignored
set(gca, "YScale", "Log");
title("Noise-free data");
function [fitresult, gof] = createFit(x, y)
%CREATEFIT(X,Y)
% Create a fit.
%
% Data for 'untitled fit 1' fit:
% X Input: x
% Y Output: y
% Output:
% fitresult : a fit object representing the fit.
% gof : structure with goodness-of fit info.
%
% See also FIT, CFIT, SFIT.
% Auto-generated by MATLAB on 22-Dec-2023 11:57:05
[xData, yData] = prepareCurveData( x, y );
% Set up fittype and options.
ft = fittype( 'power1' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = [9.45000000000003e-08 -3.28];
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
% figure( 'Name', 'untitled fit 1' );
h = plot( fitresult, xData, yData );
legend( h, 'y vs. x', 'power series fit', 'Location', 'NorthEast', 'Interpreter', 'none' );
% Label axes
xlabel( 'x', 'Interpreter', 'none' );
ylabel( 'y', 'Interpreter', 'none' );
grid on
end
Despite applying a logarithmic transformation to the Y-axis, the noisy data exhibits a poor fit (visually), while the data without noise aligns correctly.
Refer to the following MATLAB documentation for further reference:
Hope this help!
Best regards,
Saarthak

类别

Help CenterFile Exchange 中查找有关 Linear and Nonlinear Regression 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by