Using polyfit() to get 'p' vector

6 views (last 30 days)
Juyoung Leem
Juyoung Leem on 10 Jun 2021
Commented: Steven Lord on 14 Jun 2021
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!

Answers (1)

Pankil Kalra
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
Juyoung Leem
Juyoung Leem on 14 Jun 2021
Thank you for your answer! I couldn't find existing thread about this issue before, but I just checked the link you shared. I appreciate your additional explanation too. Now all functions and coefficient make more sense to me.
As this discussion already exists, do you want me to delete my question?
Steven Lord
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.

Sign in to comment.

Categories

Find more on Polynomials in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!