Use the polyfit to predict the future data tendency
이전 댓글 표시
I was using the polyfit function to find out the polynomial coefficients from a set of given points. Domain is [0 300]. After that, I was trying to predict the future basing on the given data [0 400] using polyval, but the matlab just didn't give me any value after [0 300]
stocks = hist_stock_data('25012008','25042009','SF','PAG');
Data = [1:1:300];
Date_value = stocks(1).Close(1:1:300)
p = polyfit(Data,Date_value,18);
DD = [300:1:400];
y = polyval(p,DD);
figure
hold on
plot(DD,y,'rp')
axis([0 400 25 60]);
답변 (2개)
Star Strider
2017년 3월 17일
You are extrapolating an 18-degree polynomial. I will say no more.
The polyval function is ‘predicting’ values far outside the range of your plot, that being:
axis([0 400 25 60])
댓글 수: 2
Jianpeng Liu
2017년 3월 17일
Star Strider
2017년 3월 17일
That depends on what your data are.
I would limit it to 3 in most ‘real world’ situations, and not more than 7.
Image Analyst
2017년 3월 17일
18th order? Have you ever heard of overfitting? Or extrapolating using such a high order? Non-training points in between the training points will oscillate wildly, and outside the training range the fit will very rapidly go to -inf or +inf.
Anyway, as a learning experience, try this:
fittedDates = [0:1:400];
fittedY = polyval(p, fittedDates);
% Plot fitted y along with extrapolated y:
plot(fittedDates, fittedY, 'b-', 'LineWidth', 2);
I'm pretty sure, even without looking at the plot or running the code, that the data from 300 to 400 will skyrocket towards infinity. When you run the code, you'll see what I mean.
카테고리
도움말 센터 및 File Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!