2

I have the following data:

T=[0,100,300]

and

a=[2.8796,2.8785,2.886]

and I want to extrapolate and know what a will I get at T=600 in Matlab. How can I do that?

Elin
  • 61
  • 1
  • 5
  • There is not one way to answer this question. Is there some underlying relationship between `T` and `a`? Do you need to [interpolate](http://www.mathworks.com/help/matlab/interpolation-1.html) your data? Do you want a linear interpolation, polynomial interpolation, spline interpolation, some other interpolation? – sco1 Nov 19 '15 at 16:12
  • Is there an underlying equation describing the relationship between a and T? What type of interpolation are you looking for? – GameOfThrows Nov 19 '15 at 16:12
  • I know that there are different ways of doing it. I do not have any equation for that and I do not know if it's linear or polynomial, so I have to try and check the value at T=600 and then decide which one gives me the best answer for what I want. :( – Elin Nov 19 '15 at 18:58
  • These are experimental values for temperature and thermal expansion which depending on the material can have different relations. – Elin Nov 19 '15 at 19:02

2 Answers2

4

If its linear the code below solves this

clear all
close all

T=[0,100,300];
a=[2.8796,2.8785,2.886];
reg = polyfit(T,a,1);

figure
hold on
plot(T,a,'bx')
plot(T,reg(2)+T.*reg(1),'k-')
plot(600,reg(2)+600*reg(1),'ro')
plot(600,interp1(T,a,600,'linear','extrap'),'md')
legend('observations','lin. regression','pred. at 600p polyfit','pred. at 600p interp1')

val_polyfit = reg(2)+600*reg(1)
val_interp1 = interp1(T,a,600,'linear','extrap')
diff = val_polyfit/val_interp1

yields

val_polyfit =

    2.8924


val_interp1 =

    2.8972


diff =

    0.9983
horseshoe
  • 1,437
  • 14
  • 42
  • Interesting, it does yield a different result compared to interp1. Probably because of the small number of observation samples – GameOfThrows Nov 19 '15 at 16:19
  • @GameOfThrows: thats indeed funny. I had that in the past when using excel and R, and I always considered R more trustworthy... Did not to expect that it would differ within one program. – horseshoe Nov 19 '15 at 16:25
  • @GameOfThrows Don't have matlab to execute right now, how much is the difference between this method and `interp1`? – RaGe Nov 19 '15 at 16:29
  • 1
    I guess polyfit fits a single linear, i.e. a least-squares approximation, not an interpolant. `interp1` gives a piecewise linear interpolation. – Andras Deak -- Слава Україні Nov 19 '15 at 16:34
  • @RaGe I think it is as Andras Deak says, the error is small 0.005. – GameOfThrows Nov 19 '15 at 16:37
  • @RaGe & @ GameOfThrows: I added the results above. Its 0.17%. – horseshoe Nov 19 '15 at 16:40
  • 1
    Since we don't know how the data can be shaped, it's probably more meaningful to use the piecewise linear interpolant for extrapolation (imagine the error in case where the 3 data points are on a parabola). Of course if the basis data points are already fairly linear, then it doesn't make much of a difference (and if it's *supposed to be* linear, then the `polyfit` is more meaningful, i.e. more robust). – Andras Deak -- Слава Україні Nov 19 '15 at 16:43
  • the code seems what I want to do but 17% error is quite large. Maybe I should try interp1 instead of polyfit! The value looks more reasonable for my purpose!!! :/ Thanks a lot. – Elin Nov 19 '15 at 19:05
  • @Elin not 17% but 0.17% ... – horseshoe Nov 19 '15 at 21:33
  • Oh, I see now. Thanks @horseshoe :) – Elin Nov 22 '15 at 08:36
2

For Linear Interpolation: aextra = interp1(T,a,600,'linear','extrap')

RaGe
  • 22,696
  • 11
  • 72
  • 104
  • Why are you using the interpolation function for extrapolation? – SDG Feb 21 '16 at 08:13
  • @SharanDuggirala there is no mathematical difference between inter and extra polation, just a name depending on where in the range your doing the fit – RaGe Feb 21 '16 at 16:18