0
$\begingroup$

This question already has an answer here:

Consider the following example:

Module[{lst, iFn0},
    lst = {{5, 1}, {7, 4}, {12, 5}};
    iFn0 = Interpolation[lst, InterpolationOrder -> 0];
    iFn0[5.25]
];

This returns 4. Obviously, interpolation has been constructed by "pulling back" the values, as if to say that the correct value for everything up to 7 is the measurement at 7.

But I would like to construct an interpolation by "pushing forward" the values, so that the correct value in the closed-open interval [5, 7) is 1. I know that I can cycle the values forward in lst and sort of fake the outcome:

`lst2 = {{5, 1}, {7, 1}, {12, 4}, {12.01, 5}}`

But this is problematic, because then iFn0[7] = 1 and I need it to be 4.

Is there a setting for InterpolationOrder -> 0 that will do the interpolation in the desired way?

$\endgroup$

marked as duplicate by Michael E2, bbgodfrey, MarcoB, J. M. will be back soon Jun 5 '15 at 23:54

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ You can slide the y values one position to the right, and double up the value on the left end. slideValues[pts_] := Module[{yvals = RotateRight[pts[[All, 2]]]}, yvals[[1]] = yvals[[2]]; Interpolation[Transpose[{pts[[All, 1]], yvals}], InterpolationOrder -> 0]] $\endgroup$ – Daniel Lichtblau Jun 5 '15 at 21:51
  • $\begingroup$ @DanielLichtblau I did mention in my post that I can "fake" the desired outcome by doing just what you suggest. The problem, however, is that the interpolation function has the incorrect values at the points themselves. $\endgroup$ – Shredderroy Jun 5 '15 at 22:01
2
$\begingroup$

You can "reverse x axis"

lst = {{5, 1}, {7, 4}, {12, 5}};
iFn0 = Interpolation[{-#[[1]], #[[2]]} & /@ lst, 
   InterpolationOrder -> 0];
f[x_] := iFn0[-x];

f /@ {5.25, 7}
(*{1, 4} *)

Or the same can be achieved with:

InterpolationR[l_] := Interpolation[{-#[[1]], #[[2]]} & /@ l, 
     InterpolationOrder -> 0][-#] &;
a = InterpolationR[lst];
a /@ {5.25, 7}
$\endgroup$
  • $\begingroup$ Hmm, clever, but I had hoped that a structural solution was available. I'll accept it since there is no other solution. $\endgroup$ – Shredderroy Jun 5 '15 at 20:30
  • $\begingroup$ @Shredderroy How do you know there is no other solution? $\endgroup$ – Michael E2 Jun 5 '15 at 21:48
  • $\begingroup$ Similar to ArgentoSapiens's mathematica.stackexchange.com/a/30056, who used Dot instead of Map to negate the first coordinate. $\endgroup$ – Michael E2 Jun 5 '15 at 21:54

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