# How can I change the behaviour of InterpolationOrder -> 0? [duplicate]

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 = 1 and I need it to be 4.

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

• 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[] = yvals[]; Interpolation[Transpose[{pts[[All, 1]], yvals}], InterpolationOrder -> 0]] – Daniel Lichtblau Jun 5 '15 at 21:51
• @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. – Shredderroy Jun 5 '15 at 22:01

You can "reverse x axis"

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

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


Or the same can be achieved with:

InterpolationR[l_] := Interpolation[{-#[], #[]} & /@ l,
InterpolationOrder -> 0][-#] &;
a = InterpolationR[lst];
a /@ {5.25, 7}

• Hmm, clever, but I had hoped that a structural solution was available. I'll accept it since there is no other solution. – Shredderroy Jun 5 '15 at 20:30
• @Shredderroy How do you know there is no other solution? – Michael E2 Jun 5 '15 at 21:48
• Similar to ArgentoSapiens's mathematica.stackexchange.com/a/30056, who used Dot instead of Map to negate the first coordinate. – Michael E2 Jun 5 '15 at 21:54