Using polyfit() to get 'p' vector
6 views (last 30 days)
Show older comments
Hi MATLAB community!
I'm using MATLAB R2020a.
I was using polyfit() function to find a 1st order polynomial fitting of my data. I tried to use two ways to get p vector as below:
[p, S, mu] = polyfit(R1, avg_E, 1); % polyfit of R1 and avg_E, 1st order polynomial
p_1op = polyfit(R1, avg_E, 1); % polyfit of R1 and avg_E, 1st order polynomial
After I ran this code, I checekd 'p' and 'p_1op', and they look completely different:
p = [-345.733272503816 717.117508770000]
p_1op = [-1234.10283170532 1484.39195156382]
Would you share some thoughts or experience related to this? I was not sure why this happened and how I should fix it.
Thank you for reading!
0 Comments
Answers (1)
Pankil Kalra
on 14 Jun 2021
Hi Juyong,
The question you have asked has already been answered at this community post. To summarise the reason for the difference – When you call the function with 3 return variables: [p, S, mu], the third output mu tells matlab to center and scale the data. The centering and scaling values are stored in mu. This centered and scaled data is used to generate the polynomial fit. The polynomial fit in the function call without mu is created using the raw R1 data. Hence, there is a difference between the parameter values or p vector in the two cases.
You can read more about the polynomial fit function at the documentation page
2 Comments
Steven Lord
on 14 Jun 2021
Please don't delete your question.
If someone else has the same question as you did in the future, they can search and hopefully find your question and its answer more quickly than if they posted and waited for someone else to answer.
See Also
Categories
Find more on Polynomials 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!