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:
- 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
Hope this help!
Best regards,
Saarthak