How do I extrapolate a line and plot it?

24 次查看(过去 30 天)
I'm trying to extrapolate a very linear data set using the interp1 function and I'm getting weird results. I can do this in Excel (using the trendline feature with a forward forecast) but I want to add this to an existing Matlab code that I have. Here's a pic of the Excel method (first image ... I was hoping they'd be inline).
However, I get weird results when I do this with Matlab (second image).
Here's the command that I use to grab the last 300 points of my data and use it to linearly extrapolate it forward (with vector Xextend).
Xextend = X(end) Ynew = interp1(X(end-300:end),Y(end-300:end,:),Xextend,'linear','extrap');
Any ideas why it doesn't extend the line like I expected?

回答(1 个)

David Goodmanson
David Goodmanson 2018-3-8
Hi Michael,
Since you are using Xextend = X(end) I don't see how you are extrapolating out to X = 2100. However, if Xextend were equal to 2100, then the problem with interp1 is that it merely takes the slope between the second-to-last point and the last point and uses that to extend out to X=2100. It’s doing exactly what it says, but the history of points between (end-300) and (end-2) is not even considered.
If Xextrap is a set of X values between X(end) and X=2100, what you are looking for is probably more along the lines of
c = polyfit(X(end-300:end), Y(end-300:end),1); % linear fit
Yextrap = polyval(c,Xextrap);
  1 个评论
Stephen23
Stephen23 2018-3-8
Michael's "Answer" moved here:
It should've been Xextend = X(end):1:2100 but either way I guess I was incorrectly interpreting what interp1 was doing, assuming that it took the entire span instead of simply last 2 points.
I was investigating the polyfit idea when I saw your reply so it looks like that will be the best way. Thanks!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by