# Extend a line made with polyfit and polyval

25 views (last 30 days)
Andreas Grøvan Aspaas on 17 Dec 2018
Commented: Star Strider on 18 Dec 2018
Hi
I have a problem with extending my polyfit line. I tried to extend it by changing "T" for polyval (which is temperature and x-axis) to contain the whole dataset for temperature to get an extended line ranging the whole plot. I wanted a line that was extended like 4 degrees celsius, but wanted to checking if it worked by including the whole data set first. It did not. All the y-values decreased and the extended line does not look linear any more.
Should this solution work, and if so what have I done wrong?
Is there another solution to extend the line?
Thanks a lot for the help
% Getting index for temperature values
[d,posmin] = min(abs(x11-0));
[d,negmax] = min(x11-10);
x111 = x11(negmax:lastrow11);
[d,negmin1] = min(abs(x111-0));
negmin = negmax+negmin1-1;
% Getting x and y values
T11 = x11(1:posmin); A11 = y11(1:posmin);
T12 = x11(posmin:negmax); A12 = y11(posmin:negmax);
T13 = x11(negmax:negmin); A13 = y11(negmax:negmin);
T14 = x11(negmin:lastrow11); A14 = y11(negmin:lastrow11);
% Fitting values
[p11,s11] = polyfit(T11,A11,1);
f11 = polyval(p11,wholedataset);
[p12,s12] = polyfit(T12,A12,1);
f12 = polyval(p12,T12);
[p13,s13] = polyfit(T13,A13,1);
f13 = polyval(p13,T13);
[p14,s14] = polyfit(T14,A14,1);
f14 = polyval(p14,T14);
I think it would be best if you could share a sample of your data

Star Strider on 17 Dec 2018
You are doing linear regressions, so extending the line is not a problem. (It easily could be a problem with higher-order polynomials.)
It is not necessary to do new regressions to extend the line. Just the same polynomial values with new limits:
p11 = polyfit((1:10)', 2*(1:10)'+randn(1,10)', 1); % Original Linear Regressopm
orig = polyval(p11, (1:10)'); % Evaluated
xtnd1 = polyval(p11, [-5 15]); % Extend Using ‘p11’ With New Limits
figure
plot((1:10), orig, 'b-', [-5 15], xtnd1, '--r')
grid
Andreas Grøvan Aspaas on 18 Dec 2018
Thank you so much! That worked perfectly!
Star Strider on 18 Dec 2018
As always, my pleasure!