How do I extrapolate a line and plot it?

조회 수: 23 (최근 30일)
Michael
Michael 2018년 3월 8일
댓글: Stephen23 2018년 3월 8일
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