How to get a polynomial fit to a set of data without using "fit" function?

6 views (last 30 days)
Here is my matlab code for trying to get a fourth degree polynomial fit to a graph. The first code I attached is a code I wrote for a second degree polynomial code that does work, but I'm not sure why the fourth-degree one doesn't work. The second code is my attempt at the fourth degree one.

Accepted Answer

Star Strider
Star Strider on 19 Nov 2014
You’re calculating your ‘polynomialerror’ incorrectly. You start by modeling your system as: Y = B*V, and you correctly calculate your estimated ‘V2’ parameter vector as: V2 = B\Y. You have to calculate the estimated fit as: Yfit = B*V2, and compare Y'-Yfit to calculate your error. Otherwise, you got everything correct, and your fit looks good when plotted:
X=[1 1.5 2 2.5 3 3.5 4 4.5 5 5.5 6 6.5 7 7.5 8];
Y=[0.5 0.61 0.66 0.57 0.45 0.028 -0.16 -0.49 -0.45 -0.5 -0.32 -0.017 0.36 0.59 0.69];
B=[X'.^4,X'.^3,X'.^2,X',ones(15,1)];
V2=B\Y';
Yfit = B*V2;
polynomialerror=norm(Y'-Yfit)
figure(1)
plot(X,Y,'*r')
hold on
plot(X, Yfit)
hold off
grid

More Answers (0)

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!