Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Is there some sort of functionality built into Mathematica that will let me linearly interpolate between (x, y) pairs of data in an array?

Example: I have the two points (1, 10) and (2, 20), how can I find out what the interpolant at 1.5 would be?

share|improve this question
5  
Did searching for "interpolate" or "interpolation" in the documentation not give any results? –  rm -rf Mar 5 '13 at 2:19
1  
@rm-rf Yes, but I want Interpolate[{Lena, Toady}] and it doesn't work –  belisarius Mar 5 '13 at 9:19
    
Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Read the faq! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! –  belisarius Mar 5 '13 at 13:01

4 Answers 4

Apparently, it has been missed that InterpolatingPolynomial[] can be used for "lerping". Here's halirutan's example as a one-liner:

InterpolatingPolynomial[{{1, 10}, {2, 20}}, 1.5]
   15.
share|improve this answer

Not as elegant as above approaches, but Fit can also be used. To try different shape functions:

data = {{1, 10}, {2, 20}};
trial = {{1, x}, {1, x, x^2}, {1, x, x^2, x^3}, {1, Sin[x]}, {1, Cos[x]}, 
         {1, Sin[x], Cos[x]}, {1, x, Exp[x]}, {1, x, x^2, Exp[x]}};
fits = Fit[data, #, x] & /@ trial;
x0 = 1.5;
Grid[Partition[
  Plot[#, {x, 0, 3}, Frame -> True, ImageSize -> 230, 
     FrameLabel -> {{None, None}, {None, 
        Grid[{{"f(x)=", #, SpanFromLeft}, {"f(", x0,")=", # /. x -> x0}}, 
            Spacings -> 0]}}, 
     Epilog -> {Red, PointSize[.1], Point[{x0, # /. x -> x0}]}, 
     ImagePadding -> {{30, 30}, {5, 50}}] & /@ fits, 3], Frame -> All]

Mathematica graphics

share|improve this answer

Although not as elegant as halirutan's post, this helps alternatively,

f:=Rescale[#,{1,2},{10,20}]&; 
f[1.5]
share|improve this answer

What about using Interpolation?

f = Interpolation[{{1, 10}, {2, 20}}, InterpolationOrder -> 1]
f[1.5]
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.