How do I extrapolate a line and plot it?

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?

Antworten (1)

David Goodmanson
David Goodmanson am 8 Mär. 2018

1 Stimme

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 Kommentar

Stephen23
Stephen23 am 8 Mär. 2018
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!

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Get Started with Curve Fitting Toolbox finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 8 Mär. 2018

Kommentiert:

am 8 Mär. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by