MATLAB: How to get a and b using fit(x,y,power1)

Curve Fitting Toolbox

I have XY data and fit it with y=ax^b. I use f = fit(x,y,power1) but don't know how to get "a" and "b" to continue working with them. f(1) gives a, [a,b] = fit(x,y,power1) doesn't help also.

Best Answer

Learn to use MATLAB classes.
x= rand(10,1);y = rand(10,1);
mdl = fit(x,y,'power1')
mdl =
General model Power1:
mdl(x) = a*x^b
Coefficients (with 95% confidence bounds):
a = 0.7421 (0.4012, 1.083)
b = 0.1755 (-0.3554, 0.7065)
>> methods(mdl)
Methods for class cfit:
argnames coeffnames dependnames fitoptions integrate numcoeffs probnames type
category coeffvalues differentiate formula islinear plot probvalues
cfit confint feval indepnames numargs predint setoptions
coeffvalues(mdl)
ans =
0.74207 0.17555
That is, when you don't know how to extract those coefficients, use methods to see which function gives you what you need.
When all else fails, learn to use struct as a tool.
struct(mdl)
Warning: Calling STRUCT on an object prevents the object from hiding its implementation details and should thus be avoided. Use DISP or DISPLAY to see the visible public
details of an object. See 'help struct' for more information.
ans =
struct with fields:
coeffValues: {[0.74207] [0.17555]}
probValues: {1×0 cell}
sse: 0.90412
dfe: 8
rinv: [2×2 double]
meanx: 0
stdx: 1
activebounds: [0 0]
xlim: [0.09754 0.96489]
version: 2
fType: 'power1'
fTypename: 'Power1'
fCategory: 'library'
defn: 'a*x^b'
fFeval: 1
expr: @power1
Adefn: {}
Aexpr: {}
linear: 0
derexpr: @power1der
intexpr: @power1int
args: [3×1 char]
isEmpty: 0
numArgs: 3
numCoeffs: 2
assignCoeff: ' a = FITTYPE_INPUTS_{1}; b = FITTYPE_INPUTS_{2};'
assignData: ' x = FITTYPE_INPUTS_{3};'
assignProb: ''
indep: 'x'
depen: 'y'
coeff: [2×1 char]
prob: ''
fConstants: {}
fNonlinearcoeffs: 2
fFitoptions: [1×1 curvefit.nlsqoptions]
fStartpt: @power1start
Related Question