MATLAB: How to fit a curve using “power” fitting or “custom fitting”

curve fittingfit one linepowerStatistics and Machine Learning Toolbox

I have data which I need to fit using following equation:
y= f(x)= a*x^b+c.
Code:
r=scatter(npp7,lk_2k1);
r.MarkerEdgeColor = 'r';
r.MarkerFaceColor = [0.9 0.9 0.9];
hold on
% Power fit – %y=f(x)=a*x^b+c
a=28.54;
b=0.4634
c=-3.289;
x = data(:);
y = a*x^b+c;
%f = fit(x,y);
p = plot(x,y)
p(1).LineWidth = 2;
c = p.Color;
p.Color = 'r';
It shows: Error using ^ One argument must be a square matrix and the other must be a scalar. Use POWER (.^) for elementwise power.
But if I use(.^), it shows multiple fit lines as shown in attached figure. I want just one fit line for same equation.

Best Answer

I attach a demo to do an exponential fit. Adapt as needed.
Related Question