Using Polyfiti, Polyval, and plotting

39 views (last 30 days)
say I have a data file with the first column being days and the second being prices. How would I go about creating an approximation I have named f for prices and plotting it the approximation against the original. So far I have tried.
day = Data(:,1);
price =Data(:,2) ;
p=polyfit(day,price,5); %N=5
f=polyval(p,day); %Approximation
hold on
plot(day,price)
plot(day,f)
I really appreciate any help if im in the right direction
  2 Comments
John D'Errico
John D'Errico on 31 Oct 2019
Why do you think this did not work? In fact, it seems you did create a polynomial that approximates the data. Then you plotted the original data, as well as the approximation values. You used the command hold to tell MATLAB to plot them on the same figure.
I might only have suggested using different colors and plot markers. Perhaps a different line type.
But you should have gotten some result there. So you need to explain clearly why you think there is a problem. Without that, my only guess would be that a 5th degree polynomial just happened to fit so accurately that you cannot distinguish between the original data and the approximation in the plot.
jacob Mitch
jacob Mitch on 31 Oct 2019
Hi John, I believe I have the correct result, I just wasn't sure if I was miss using the functions to create false approximations.

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 31 Oct 2019
Ok, now that I know where you are coming from, I'll pose this as an answer. It looks correct to me.
The call to polyfit will create a polynomial model that tries to best approximate your data, thus it is an approximation as you desire, with some caveats, so let me mention them.
The errors people make when using polynomial fits almost always seem to cluster in two basic categories. They have too little data to support the degree polynomial they want to fit, or they have poorly scaled data. In fact, the latter almost can be put in the first category too.
So what does it mean to have insufficient data? For example, 2 points exactly determine a straight line. 3 points determine a quadratic polynomial. 6 points will exactly determine a 5th degree polynomial, etc. But really, you want more than an exact fit, because those exact fits will be interpolating polynomials. They will not be approximants, but interpolants. An interpolant passes exactly through the original data points. An approximant allows for error in the data, and tries to smooth through the noise.
More data is better than less. And if you have fewer than N+1 data points to fit an Nth degree polynomial, then polyfit will fail to work properly. It will also give you a nasty message telling you it is upset.
Note that i said N+1 data points, and that more is better. But replicate data points don't count. (They do help in the fit, but the fit will still fail unless you have N+1 points or more with DISTINCT x locations.) Again though, MORE is better, and significantly more is better yet.
So insufficient data is the most common failure mode for those using a polynomial fit. The second most common failure mode comes from poorly scaled data. This causes numerical problems in the linear algebra used for the fit, even when in theory, you have a technically sufficient amount of data. For example...
x = 1:6;
y = rand(1,6);
P = polyfit(x,y,5)
P =
0.070949 -1.2754 8.548 -26.224 35.858 -16.163
So polyfit produces an interpolating polynomial there. It is total garbage, because I used random data, but who cares? It is an interpolant.
pred = polyval(P,x)
pred =
0.81472 0.90579 0.12699 0.91338 0.63236 0.09754
y
y =
0.81472 0.90579 0.12699 0.91338 0.63236 0.09754
If you want an approximant, then use a lower order polynomial. Here, a quadratic polynomial.
P = polyfit(x,y,2)
P =
-0.020326 0.038862 0.75407
pred = polyval(P,x)
pred =
0.7726 0.75048 0.68771 0.58429 0.44021 0.25548
As you can see, it misses the garbage data I generated.
plot(x,y,'bo',x,pred,'-Sr')
untitled.jpg
The red curve is an approximation to that random data. I guess you can't expect too much from random data. ;-)
But now let me change things in a way that should be trivial. I'll add 10000 to each x.
x2 = x + 10000;
P2 = polyfit(x2,y,5)
Warning: Polynomial is badly conditioned. Add points with distinct X values, reduce the degree of the polynomial, or try centering and scaling as
described in HELP POLYFIT.
> In polyfit (line 79)
P2 =
-1.4651e-05 0.72213 -14234 1.4026e+08 -6.9088e+11 1.3609e+15
Now polyfit throws a hissy fit, even though theoretically a polynomial should be able to interpolate those 6 points as easily as the first set.
Here, I needed to use the centering/scaling capability of polyfit.
[P2,~,mu] = polyfit(x2,y,5)
P2 =
1.626 -0.41381 -4.0344 0.73114 1.7511 0.47007
mu =
10004
1.8708
As you should see, polyfit is now happy. The only difference is you need to pass the vector mu into polyval when you evaluate the function.
pred = polyval(P2,x2,[],mu)
pred =
0.81472 0.90579 0.12699 0.91338 0.63236 0.09754
y
y =
0.81472 0.90579 0.12699 0.91338 0.63236 0.09754
As you can see, it now predicts exactly, an interpolant again.
There are lots of other subtly different errors you can make with a polynomial fit, but those are the main issues you will see.
  1 Comment
jacob Mitch
jacob Mitch on 31 Oct 2019
Thank you for the detailed answer so much, this has helped me understand.

Sign in to comment.

More Answers (0)

Categories

Find more on Interpolation 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!