Question regarding the calling of cfit coefficients

3 vues (au cours des 30 derniers jours)
TPazz
TPazz le 28 Nov 2023
Commenté : TPazz le 28 Nov 2023
I have used the 'fit' function to apply a power2 curve fit to some data. The cfit object is called fitobject. Shown below:
fitobject = fit(xdata, ydata, 'power2')
As a result I get three coefficients in the form y = ax^b + c.
I know the correct way to call the coefficients in a script is:
a = fitobject.a; b = fitobject.b; c = fitobject.c;
But if I call the following:
a = fitobject(1); b = fitobject(2); c = fitobject(3);
I get slightly different values of the coefficients. Could someone please explain where the values above come from, and thus why they differ. E.g why fitobject.a is not equal to fitobject(1) etc?
I have been scouring the internet and cannot find an explanation. I suspect it's something to do with the confidence bounds?
Many thanks!

Réponse acceptée

Steven Lord
Steven Lord le 28 Nov 2023
The way you gave, asking for the a, b, and c properties of the cfit object named fitobject, is one way to get the coefficients. This documentation page lists another: use the coeffvalues function. You'd probably also want to call the coeffnames function as well.
The second syntax you gave does not extract any coefficients at all. It evaluates the fit for x = 1, x = 2, and x = 3 respectively. Adapting the example on that documentation page:
load census
curvefit = fit(cdate,pop,'poly3','normalize','on')
curvefit =
Linear model Poly3: curvefit(x) = p1*x^3 + p2*x^2 + p3*x + p4 where x is normalized by mean 1890 and std 62.05 Coefficients (with 95% confidence bounds): p1 = 0.921 (-0.9743, 2.816) p2 = 25.18 (23.57, 26.79) p3 = 73.86 (70.33, 77.39) p4 = 61.74 (59.69, 63.8)
Let's get one of the coefficients.
curvefit.p1
ans = 0.9210
Let's get all the coefficients and display them as a table.
N = coeffnames(curvefit)
N = 4×1 cell array
{'p1'} {'p2'} {'p3'} {'p4'}
V = coeffvalues(curvefit)
V = 1×4
0.9210 25.1834 73.8598 61.7444
results = table(N, V.', 'VariableNames', ["Coefficient names", "Coefficient values"])
results = 4×2 table
Coefficient names Coefficient values _________________ __________________ {'p1'} 0.92102 {'p2'} 25.183 {'p3'} 73.86 {'p4'} 61.744
What's the value of the fitted curve for the year 1925?
pop1925 = curvefit(1925)
pop1925 = 111.5852
Does this look right compared to the fitted curve and the data?
plot(curvefit)
hold on
plot(cdate, pop, 'ko', 1925, pop1925, 'bd')

Plus de réponses (0)

Catégories

En savoir plus sur Fit Postprocessing dans Help Center et File Exchange

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by