# How to get "x" value from a interpolating function?

I measured a parameter over time and get this List: (where fisrt value (x) is time and second the value (y) the vale of the parameter)

j = List[{0, 0.205004}, {0.1, 0.259237}, {0.2, 1.059125}, {0.3, 0.832184},
{0.4, 0.587992}, {0.5, 0.565537}, {0.6, 0.527323}];


I did Interpolation:

f = Interpolation[j, InterpolationOrder -> 3, Method -> "Spline"]


and got this Plot:

Plot[f[x], {x, 0, 0.6}]


I want to get the x value for a certain y point.

I tried:

f[0.4]


and

InverseFunction[f][0.4]


But it's giving me the opposite.

Thanks.

• Works fine for me: f[0.4] gives 0.587992. What is your \$Version? Jan 17, 2022 at 13:02
• Sorry, I mistaked. I want to get x value for a certain y value. Jan 17, 2022 at 13:04

Clear["Global*"]

j = {{0, 0.205004}, {0.1, 0.259237}, {0.2, 1.059125}, {0.3, 0.832184},
{0.4, 0.587992}, {0.5, 0.565537}, {0.6, 0.527323}};

f = Interpolation[j, InterpolationOrder -> 3, Method -> "Spline"];


The function range is

{fmin, fmax} = (#[{f[x], 0 <= x <= 0.6}, x] & /@ {MinValue, MaxValue})

(* {-0.0275476, 1.08927} *)


The range of x for this function range is

{xmin, xmax} = (#[{f[x], 0 <= x <= 0.6}, x] & /@ {ArgMin, ArgMax})

(* {0.0424198, 0.219855} *)


To restrict the function such that its inverse is single-valued, require xmin < x < xmax

f2[x_?NumericQ] :=
ConditionalExpression[f[x], xmin < x < xmax]


Plotting,

Plot[{f[x], f2[x]}, {x, 0, 0.6},
Frame -> True,
PlotStyle -> {AbsoluteThickness[0.75], {Red, Dashed}},
PlotLegends -> Placed[
{StringForm[
", multi-valued inverse",
HoldForm[f[x]]],
StringForm[", single-valued inverse",
HoldForm[f2[x]]]},
{0.6, 0.3}]]


The inverse of f2 is

g[y_?NumericQ] :=
x /. FindRoot[f2[x] == y, {x, 0.15}]


Plotting the inverse function,

Legended[
Show[
ParametricPlot[{f[x], x}, {x, 0, 0.6},
PlotStyle -> AbsoluteThickness[0.75]],
Plot[g[y], {y, fmin, fmax},
PlotStyle -> {Red, Dashed}],
Frame -> True,
AspectRatio -> GoldenRatio,
ImageSize -> 252],
Placed[
LineLegend[
{Directive[ColorData[97][1], AbsoluteThickness[0.75]],
Directive[Red, Dashed]}, {"multi-valued inverse",
StringForm[", single-valued inverse",
HoldForm[g[y]]]}],
{0.45, 0.39}]]


Plot[f[x], {x, 0, 0.52}]


As you can see, for some y values there are several x values. This makes the inverse function multivalued. And MMA seems to take randomly one of several values as you can see:

Plot[InverseFunction[f][x], {x, 0, 1}]


Therefore, using InverseFunction is not a good idea.

Instead try e.g. FindInstance with restrictions on x like:

FindInstance[{f[x] == 0.4, 0 < x, x < 0.2}, x]
(* {{x -> 0.118044}} *)

• NSolve[{f[x] == 0.4 , 0 < x < 0.6}, x]  should give all solutions ( but doesn't evaluate in MMA v12.2 ) Jan 17, 2022 at 14:03
• @Ulrich Neumann I think NSolve uses inverse functions. See above. Jan 17, 2022 at 14:29
• @DanialHuber Might be, but NSolve[{Sin[x] == .8, 0 < x < 6 Pi}, x] for example works!?! Jan 17, 2022 at 14:46
• @Ulrich Neumann "Sin" is not an a numerical interpolated function. Jan 17, 2022 at 15:23
• Understood, but InverseFunction of Sin` also isn't unique Jan 17, 2022 at 15:31