Calculating Residuals from polyfit

I just wanted to check that I am calculating residuals correctly as I am gettign a different answer compared to mathcad. M is my matrix of data, of which I plot column 1 (x-axis) against column 5 (y axis)
I am performing both 3rd and 5th order polynomial fits
x=M(:,1);
y=M(:,5);
p3=polyfit(x,y,3);
f3=polyval(p3,x);
p5=polyfit(x,y,5);
f5=polyval(p5,x);
plot(x,f3,'b--')
plot(x,f5,'g--')
legend('Zmx','3rd','5th')
%Calc Residuals
y3=y-f3;
y5=y-f5
axes(handles.axes2)
cla
plot(x,y3,'bd--','MarkerSize', 3)
grid on
hold on
plot(x,y5,'rd--','MarkerSize', 3)
legend('3rd','5th')
Is that correct for the residuals e.g. y3 = y-f3 ?? thanks Jason

 Accepted Answer

Star Strider
Star Strider on 9 Apr 2015
It looks correct to me.

5 Comments

Thankyou for confirmation
My pleasure.
What is Mathcad giving you? (I haven’t used it in a long time, and so I don’t have it available to compare.) Are the polynomial coefficients the same as those that MATLAB calculates?
For the 5th order polyfit,
matlab gives: p5 =
-347.8636 390.0972 -971.9331 37.2882 -2.3688 0.0123
Mathcad gives:
-341.56157
378.81022
-964.45718
35.05398
-2.07866
Error on my part, I hadn't included the constant for mathcad! It now exactly matches
No worries. I was just curious as to how you managed to get different results from both. There could be slight differences depending on precision and how both deal with floating point approximation errors, but they should be close.
So apparently they’re now both giving the same results and residuals?

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!