cftool with no log scale issue

4 vues (au cours des 30 derniers jours)
Birsen Ayaz-Maierhafer
Birsen Ayaz-Maierhafer le 18 Jan 2022
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.

Réponses (1)

Saarthak Gupta
Saarthak Gupta le 22 Déc 2023
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

Catégories

En savoir plus sur Linear and Nonlinear Regression dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by