MATLAB: How to fit a power law relationship in a scatter plot

power law fit

I have a scatter plot and I need to fit a power law line in the plot. It is very simple in excel but, I don't know why it is that hard in matlab! Any ideas please.
thanks

Best Answer

You have to write your own functions to fit your data in MATLAB, but that is not difficult.
Try this:
x = indpendent_variable; % Substitute A Vector

y = dependent_variable; % Substitute A Vector
pwrlaw = (@b,x) b(1).*x.^b(2); % Objective Function
nrmrsd = @(b) norm(y - pwrlaw(b,x)); % Cost Function
B = fminsearch(nrmrsd, [1; 1]); % Estimate Parameters
figure(1)
scatter(x, y)
hold on
plot(sort(x), pwrlaw(B,sort(x)))
hold off
grid