- Parametric Fitting: https://www.mathworks.com/help/curvefit/parametric-fitting.html
- normrnd: https://www.mathworks.com/help/stats/normrnd.html
- tiledlayout: https://www.mathworks.com/help/matlab/ref/tiledlayout.html
cftool with no log scale issue
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Birsen Ayaz-Maierhafer
el 18 de En. de 2022
Respondida: Saarthak Gupta
el 22 de Dic. de 2023
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.
0 comentarios
Respuestas (1)
Saarthak Gupta
el 22 de Dic. de 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
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
0 comentarios
Ver también
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!