Create a power law fit from imported data

I am importing data from an Excel spreadsheet on which I ultimately want to create a power law fit in MATLAB. In the code below, I removed my personal information in the filename.txt in the InputData line, so please ignore if you think this would create an error as written. The data is importing fine.
clear;
%Read the data for the fit
InputData = readtable('C:\MATLAB_Power_Law_Fitting.xlsx');
%remove any rows containing NAN
cleanData = rmmissing(InputData);
cleanX = cleanData.x;
cleanY = cleanData.y;
scatter(cleanData,'x','y');
ft = fit(cleanX,cleanY,'power1')
Here is the error I am getting on the last line.
_____________________________________________________
Error using fit>iFit (line 348)
NaN computed by model function, fitting cannot continue.
Try using or tightening upper and lower bounds on coefficients.
Error in fit (line 116)
[fitobj, goodness, output, convmsg] = iFit( xdatain, ydatain, fittypeobj, ...
Error in MatLab_Power_Law_Fits (line 16)
ft = fit(cleanX,cleanY,'power1');
________________________________________________________
There is no NAN data in cleanData nor are any of the values negative or zero. There is some repeating data, but this is research data.
Also, while I can create a scatter plot, MatLab throws up an error when I try to create a semilogx plot using the following line:
semilogx(cleanData,'x','y');
________________________________________________________
Error using semilogx
Data must be numeric, datetime, duration or an array convertible to double.
Error in MatLab_Power_Law_Fits (line 14)
semilogx(cleanData,'x','y');
__________________________________________________________
Thanks for any help.

1 Comment

Back in R2021b, semilogx did not yet support table variables; that was added in R2022a (the release after yours)

Sign in to comment.

 Accepted Answer

Error using fit>iFit (line 348)
NaN computed by model function, fitting cannot continue.
Try using or tightening upper and lower bounds on coefficients.
You are not using any bounds on your fit() call. There coefficient space explored happens to include 0, leading to log(0) which is -inf . Arithmetic on those -inf leads to NaN.
You need to pass bounds to your fit() call to avoid this possibility, probably by passing in the 'Lower' option.

1 Comment

Just curious: under what data conditions will calling fit() with model type "power1" trigger a NaN error message?
x = linspace(0, 1, 101);
a = [2.0, -2.0];
b = [0.5, -0.5];
n = 1;
t = tiledlayout(2, 2, 'TileSpacing', 'Compact');
for i = 1:length(a)
for j = 1:length(b)
y = a(i)*x.^b(j); % power1 model
nexttile
plot(x, y)
title(sprintf('Case %d: a = %.1f, b = %.1f', n, a(i), b(j)))
n = n + 1;
end
end
xlabel(t, {'$x$'}, 'interpreter', 'latex')
ylabel(t, {'$y$'}, 'interpreter', 'latex')
title(t, {'Power model: $f(x) = a x^{b}$'}, 'interpreter', 'latex')
% If a = 0, and b < 0
0*(0^(-0.5))
ans = NaN
% Case #1
cleanX = [0.1000; 0.2000; 0.3000; 0.4000; 0.5000; 0.6000; 0.7000; 0.8000; 0.9000; 1.0000];
cleanY = [0.6325; 0.8944; 1.0954; 1.2649; 1.4142; 1.5492; 1.6733; 1.7889; 1.8974; 2.0000];
ft = fit(cleanX, cleanY, 'power1')
ft =
General model Power1: ft(x) = a*x^b Coefficients (with 95% confidence bounds): a = 2 (2, 2) b = 0.5 (0.5, 0.5)
% Case #2
cleanX = [0.0000; 0.1000; 0.2000; 0.3000; 0.4000; 0.5000; 0.6000; 0.7000; 0.8000; 0.9000; 1.0000];
cleanY = [0.0000; 0.6325; 0.8944; 1.0954; 1.2649; 1.4142; 1.5492; 1.6733; 1.7889; 1.8974; 2.0000];
ft = fit(cleanX, cleanY, 'power1')
Error using fit>iFit (line 315)
Unable to fit a power function when the x data has nonpositive values.

Error in fit (line 117)
[fitobj, goodness, output, convmsg] = iFit( xdatain, ydatain, fittypeobj, ...
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Sign in to comment.

More Answers (0)

Products

Release

R2021b

Community Treasure Hunt

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

Start Hunting!