# InverseFunction on an Interpolating Function is misbehaving

I am getting strange behavior when I try to calculate the inverse of an interpolating function for two similar data sets.

My data sets are:

Array1 = {25, 425, 825, 1175, 1575, 1975, 2025, 2625, 3225, 3775, 4375, 4975, 5250, 9250,
13250, 16750, 20750, 24750, 27500, 47500, 67500, 82500, 102500}

Array2 = {13, 88, 188, 263, 338, 413, 513, 588, 613, 688, 788, 863, 938, 1013, 1113, 1188,
1250, 1950, 2650, 3350, 4050, 4750, 5450, 6150, 6700, 13700, 20700, 27700, 34700, 41700,
48700, 55700}


Let's define:

int1 = Interpolation[Array1];
int2 = Interpolation[Array2];

inv1 = InverseFunction[int1];
inv2 = InverseFunction[int2];


Now if these are behaving, then taking the inverse of the interpolating function's value for X should return X. Here's what happens when we try that for, say, 5:

N[inv1[int1[5]]]


Out: 5.

N[inv2[int2[5]]


Out: -1.73315

Err...this makes no sense. Is there any reason that the inverse of an interpolating function would behave as expected with one data set but not another? These data sets aren't very different so it isn't clear to me what about the second set could be causing the routine heartburn.

-
Quick update on this: apparently, it is the second value in Array2 that offends Mathematica's delicate sensibilities. If I change it from 88 to 89, still get gobbledygook. If I change it to 90 or greater, then inv2[int2[x]] = x, as expected. (If I keep it below 89, it stays weird). – Jeff Jun 13 '13 at 18:22
I cannot reproduce this. Can you try a fresh session and see if it persists? What version are you in? Also, I (simultaneously with @belisarius it seems) had to edit your post so that the code is indented. See mathematica.stackexchange.com/editing-help for some help with this in the future. – gpap Jun 13 '13 at 18:32
Try N[inv2[int2[5.]]] and let us know the result – Dr. belisarius Jun 13 '13 at 18:36
It's version 8.0. It's definitely still doing it, even after I abort the kernel and start over. – Jeff Jun 13 '13 at 18:41
@belisarius :) ok, then a fun-preserving compromise; setting InterpolationOrder->1 fixes the above issue – gpap Jun 14 '13 at 8:20

I think this is an issue of the interpolation order being too high and the resulting function not being 1 - 1. A way is to set the InterpolationOrder to 1 or to create the inverse function by inverting the data and then interpolating:

inv2 = Interpolation[MapIndexed[List[#1, Sequence @@ #2] &, Array2]];


so that

inv2[int2[5.]]


5.

---EDIT---

Actually, Alexey Popkov rightly points out that using interpolation order higher than 1 doesn't guarantee that monotonicity of the data is preserved in the interpolating function, meaning that the two interpolation functions will not be inverses of each other everywhere. So, although it is "safer" to interpolate after inverting the data (cause an interpolation function will exist) it isn't guaranteed that function will be the inverse of the original interpolating function. In this particular data set this is because there is a sharp change in the gradient of the interpolating polynomial around points 18 and 24:

Show[Plot[inv2[int2[x]], {x, 1, 32}],
Epilog -> Point[Transpose@ConstantArray[Range [Length[Array2]], 2]]]


so the only way out (at least as far as me and Alexey Popkov can see) is using linear interpolation.

In any case all these problems reduce to the fact that an interpolated function need not be invertible in the first place so this is not a bug.

-
I hate OPs that do not care! :) – Kuba Apr 25 '14 at 7:18
yep, completely agree :) – gpap Apr 25 '14 at 8:33
@gpap Try Plot[inv2[int2[x]], {x, 1, 32}] with your inv2: it is not the straight line! I think in the case of monotonic datasets InterpolationOrder->1 is necessary in the both methods. Of course there are methods of higher order monotonic interpolation which may be preferred in some cases, but I do not know any built-in. If the dataset is not monotonic, the problem is ill-posed. – Alexey Popkov Apr 26 '14 at 1:19
@AlexeyPopkov haha - I mindlessly copied and pasted my comment from a while back but you are very right. I have now edited this. – gpap Apr 28 '14 at 9:56