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 ![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1574687/image.png)
Refer to the following code:
set(gca, "YScale", "Log");
createFit(x,y);
Warning: Negative data ignored
set(gca, "YScale", "Log");
title("Noise-free data");
function [fitresult, gof] = createFit(x, y)
[xData, yData] = prepareCurveData( x, y );
ft = fittype( 'power1' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.StartPoint = [9.45000000000003e-08 -3.28];
[fitresult, gof] = fit( xData, yData, ft, opts );
h = plot( fitresult, xData, yData );
legend( h, 'y vs. x', 'power series fit', 'Location', 'NorthEast', 'Interpreter', 'none' );
xlabel( 'x', 'Interpreter', 'none' );
ylabel( 'y', 'Interpreter', 'none' );
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