Matlab's curve fitting toolbox results make no sense
8 views (last 30 days)
Show older comments
So, this is driving me crazy and I don't understand why the results I get from the curve fitting toolbox is so unreasonable.
All I wanted to do is to fit a polynomial to my x and y (attached as a mat file).
I could get a good fit by using a polynomial of degree 6.
Now, as I tried to reproduce the nice figure cftool created using the coefficients it got me, it was very very far.
versus
, which is what I plotted with the provided coefficients. I used different ways to get the coefficients, too. From copying them from cftool to saving it to workspace and using a function or generating a code. All gave me something so off.
The results are so unreasoanable that when I tried a polynomial of degree 1 to fit this (p1(x) + p2) it gave me both p1 and p2 to be negative!! It is clear that whatever the fit is, p2 has to be positive.
I don't understand what I'm doing wrong. Will be very thankful if someone could help.
0 Comments
Accepted Answer
Walter Roberson
on 18 May 2019
You are not getting a good fit. You should be normalizing your data:
[p, ~, mu] = polyfit(x, y, 6);
plot(x, polyval(p, x, mu))
Anyhow. If you tell cftool to save your fit to the default variable name fittedmodel then,
plot(x,polyval(coeffvalues(fittedmodel),x))
3 Comments
Walter Roberson
on 18 May 2019
cftool, choose x, choose y, choose polynomial, degree 6.
fit -> save to workspace. Leave variable names the same as default; in particular, fittedmodel as the name of the fit object.
Then
plot(x,polyval(coeffvalues(fittedmodel),x), 'b', x, y, '.k')
Looks pretty good to me.
If I use polynomial degree 1, then I get the same plot you post (except the legend), and p2 shows up as positive. I save to workspace as fittedmodel1 and then,
>> coeffvalues(fittedmodel1)
ans =
-11037758.4418226 10305509.0653276
which is positive intercept.
More Answers (0)
See Also
Categories
Find more on Get Started with Curve Fitting Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!