MATLAB Answers

Testing Data for Power Law Relationship

67 views (last 30 days)
Hi All
I am looking for help testing some data for a power-law relationship. I am very much a beginner to Matlab, so I'd appreciate a very detailed answer to make sure I'm not missing anything.
I have 2 vectors, X & Y and I want to check if Y = k * X^α.
1) How best to approach this? 2) Part of the vectors consist of NaN - how can I tell Matlab to jump these positions?
Many thanks

Accepted Answer

Ramnarayan Krishnamurthy
Ramnarayan Krishnamurthy on 29 Dec 2017
Edited: Ramnarayan Krishnamurthy on 29 Dec 2017
Part 1: A few possible approaches to approaching this is as follows:
a) Without the Curve Fitting Toolbox
i) Using polyfit
% Setting up data
X = [0.5 2.4 3.2 4.9 6.5 7.8]';
Y = [0.8 9.3 37.9 68.2 155 198]';
% Plotting the data
plot(X,Y,'+r'), hold on
% Polynomial curve fitting of log values so that we have a linear equation
% Simplifying, log(Y) = log(k*X.^a) = log(X)*a + log(k)
p = polyfit(log(X),log(Y),1);
% Evaluating Coefficients
a = p(1);
% Accounting for the log transformation
k = exp(p(2));
% Final plot
ezplot(@(X) k*X.^a,[X(1) X(end)])
ii) Interactively using Basic Fitting Tool
plot(log(X),log(Y))
Then in the Figure, Tools --> Basic Fitting --> Linear (plot fit)--> Check "Show Equations" --> Click Forward Arrow
Here again, k = exp(p(2));
b) Using the Curve Fitting Toolbox
i) Programmatically:
[f, gof] = fit(X, Y, 'power1');
f is the fitobject and gof gives you the goodness of fit statistics.
ii) Interactively using cftool
The following link contains an elaborate description of using the cftool for power series models. In addition, it includes examples of using the fit function for power series models.
Part 2: Omitting NaN values
You could consider pre-processing X and Y such that they do not contain NaN values (but make sure x and y have the same number of rows)
Example:
X = [0.5 2.4 NaN 4.9 NaN 7.8]';
Y = [0.8 NaN 37.9 68.2 155 NaN]';
% Remove all NaN values from X and Y
X(isnan(X)) = []
Y(isnan(Y)) = []
% Now fit the data without NaNs
[f, gof] = fit(X, Y, 'power1');

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by